diff --git "a/data/literate-agda/data.json" "b/data/literate-agda/data.json" new file mode 100644--- /dev/null +++ "b/data/literate-agda/data.json" @@ -0,0 +1,100 @@ +{"size":23282,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Adequacy: of denotational semantics with respect to operational semantics\"\nlayout : page\nprev : \/Soundness\/\npermalink : \/Adequacy\/\nnext : \/ContextualEquivalence\/\n---\n\n\\begin{code}\nmodule plfa.Adequacy where\n\\end{code}\n\n## Introduction\n\nHaving proved a preservation property in the last chapter, a natural\nnext step would be to prove progress. That is, to prove a property\nof the form\n\n If \u03b3 \u22a2 M \u2193 v, then either M is a lambda abstraction or M \u2014\u2192 N for some N.\n\nSuch a property would tell us that having a denotation implies either\nreduction to normal form or divergence. This is indeed true, but we\ncan prove a much stronger property! In fact, having a denotation that\nis a function value (not `\u22a5`) implies reduction to a lambda\nabstraction (no divergence).\n\nThis stronger property, reformulated a bit, is known as _adequacy_.\nThat is, if a term `M` is denotationally equal to a lambda abstraction,\nthen `M` reduces to a lambda abstraction.\n\n \u2130 M \u2243 \u2130 (\u019b N) implies M \u2014\u21a0 \u019b N' for some N'\n\nRecall that `\u2130 M \u2243 \u2130 (\u019b N)` is equivalent to saying that\n`\u03b3 \u22a2 M \u2193 (v \u21a6 w)` for some `v` and `w`. We will show that\n`\u03b3 \u22a2 M \u2193 (v \u21a6 w)` implies reduction a lambda abstraction.\n\nIt is well known that a term can reduce to a lambda abstraction using\nfull \u03b2 reduction if and only if it can reduce to a lambda abstraction\nusing the call-by-name reduction strategy. So we shall prove that\n`\u03b3 \u22a2 M \u2193 (v \u21a6 w)` implies that `M` halts under call-by-name evaluation,\nwhich we define with a big-step semantics written `\u03b3' \u22a2 M \u21d3 c`, where\n`c` is a closure (a term paired with an environment) and `\u03b3'` is an\nenvironment that maps variables to closures\n\nSo we will show that `\u03b3 \u22a2 M \u2193 (v \u21a6 w)` implies `\u03b3' \u22a2 M \u21d3 c`,\nprovided `\u03b3` and `\u03b3'` are appropriate related. The proof will\nbe an induction on the derivation of `\u03b3 \u22a2 M \u2193 v`, and to\nstrengthen the induction hypothesis, we will relate semantic values to\nclosures using a _logical relation_ `\ud835\udd4d`.\n\nThe rest of this chapter is organized as follows.\n\n* We loosen the requirement that `M` result in a function value and\n instead require that `M` result in a value that is greater than or\n equal to a function value. We establish several properties about\n being ``greater than a function''.\n\n* We define the call-by-name big-step semantics of the lambda calculus\n and prove that it is deterministic.\n\n* We define the logical relation `\ud835\udd4d` that relates values and closures,\n and extend it to a relation on terms `\ud835\udd3c` and environments `\ud835\udd3e`.\n\n* We prove the main lemma,\n that if `\ud835\udd3e \u03b3 \u03b3'` and `\u03b3 \u22a2 M \u2193 v`, then `\ud835\udd3c v (clos M \u03b3')`.\n\n* We prove adequacy as a corollary to the main lemma.\n\n\n## Imports\n\n\\begin{code}\nopen import plfa.Untyped\n using (Context; _\u22a2_; \u2605; _\u220b_; \u2205; _,_; Z; S_; `_; \u019b_; _\u00b7_;\n rename; subst; ext; exts; _[_]; subst-zero)\nopen import plfa.LambdaReduction\n using (_\u2014\u21a0_; _\u2014\u2192\u27e8_\u27e9_; _[]; _\u2014\u2192_; \u03be\u2081; \u03be\u2082; \u03b2; \u03b6)\nopen import plfa.CallByName\n using (Clos; clos; ClosEnv; \u2205'; _,'_; _\u22a2_\u21d3_; \u21d3-var; \u21d3-lam; \u21d3-app; \u21d3-determ;\n cbn\u2192reduce)\nopen import plfa.Denotational\n using (Value; Env; `\u2205; _`,_; _\u21a6_; _\u2291_; _\u22a2_\u2193_; \u22a5; Funs\u2208; _\u2294_; \u2208\u2192\u2291;\n var; \u21a6-elim; \u21a6-intro; \u2294-intro; \u22a5-intro; sub; \u2130; _\u2243_; _iff_;\n Trans\u2291; ConjR1\u2291; ConjR2\u2291; ConjL\u2291; Refl\u2291; Fun\u2291; Bot\u2291; Dist\u2291;\n sub-inv-fun)\nopen import plfa.Soundness using (soundness)\nopen import plfa.Substitution using (ids; sub-id)\n\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; _\u2262_; refl; trans; sym; cong; cong\u2082; cong-app)\nopen Eq.\u2261-Reasoning using (begin_; _\u2261\u27e8\u27e9_; _\u2261\u27e8_\u27e9_; _\u220e)\nopen import Data.Product using (_\u00d7_; \u03a3; \u03a3-syntax; \u2203; \u2203-syntax; proj\u2081; proj\u2082)\n renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Nullary.Negation using (contradiction)\nopen import Data.Empty using (\u22a5-elim) renaming (\u22a5 to Bot)\nopen import Data.Unit\nopen import Relation.Nullary using (Dec; yes; no)\nopen import Function using (_\u2218_)\n\\end{code}\n\n\n## The property of being greater or equal to a function\n\nWe define the following short-hand for saying that a value is\ngreather-than or equal to a function value.\n\n\\begin{code}\nAboveFun : Value \u2192 Set\nAboveFun u = \u03a3[ v \u2208 Value ] \u03a3[ w \u2208 Value ] v \u21a6 w \u2291 u\n\\end{code}\n\nIf a value `u` is greater than a function, then an even greater value `u'`\nis too.\n\n\\begin{code}\nAboveFun-\u2291 : \u2200{u u' : Value}\n \u2192 AboveFun u \u2192 u \u2291 u'\n -------------------\n \u2192 AboveFun u'\nAboveFun-\u2291 \u27e8 v , \u27e8 w , lt' \u27e9 \u27e9 lt = \u27e8 v , \u27e8 w , Trans\u2291 lt' lt \u27e9 \u27e9\n\\end{code}\n\nThe bottom value `\u22a5` is not greater than a function.\n\n\\begin{code}\nAboveFun\u22a5 : \u00ac AboveFun \u22a5\nAboveFun\u22a5 \u27e8 v , \u27e8 w , lt \u27e9 \u27e9\n with sub-inv-fun lt\n... | \u27e8 \u0393 , \u27e8 f , \u27e8 \u0393\u2286\u22a5 , \u27e8 lt1 , lt2 \u27e9 \u27e9 \u27e9 \u27e9\n with Funs\u2208 f\n... | \u27e8 A , \u27e8 B , m \u27e9 \u27e9\n with \u0393\u2286\u22a5 m\n... | ()\n\\end{code}\n\nIf the join of two values `u` and `u'` is greater than a function, then\nat least one of them is too.\n\n\\begin{code}\nAboveFun-\u2294 : \u2200{u u'}\n \u2192 AboveFun (u \u2294 u')\n \u2192 AboveFun u \u228e AboveFun u'\nAboveFun-\u2294{u}{u'} \u27e8 v , \u27e8 w , v\u21a6w\u2291u\u2294u' \u27e9 \u27e9 \n with sub-inv-fun v\u21a6w\u2291u\u2294u'\n... | \u27e8 \u0393 , \u27e8 f , \u27e8 \u0393\u2286u\u2294u' , \u27e8 lt1 , lt2 \u27e9 \u27e9 \u27e9 \u27e9\n with Funs\u2208 f\n... | \u27e8 A , \u27e8 B , m \u27e9 \u27e9\n with \u0393\u2286u\u2294u' m\n... | inj\u2081 x = inj\u2081 \u27e8 A , \u27e8 B , (\u2208\u2192\u2291 x) \u27e9 \u27e9\n... | inj\u2082 x = inj\u2082 \u27e8 A , \u27e8 B , (\u2208\u2192\u2291 x) \u27e9 \u27e9\n\\end{code}\n\nOn the other hand, if neither of `u` and `u'` is greater than a function,\nthen their join is also not greater than a function.\n\n\\begin{code}\nnot-AboveFun-\u2294 : \u2200{u u' : Value}\n \u2192 \u00ac AboveFun u \u2192 \u00ac AboveFun u'\n \u2192 \u00ac AboveFun (u \u2294 u')\nnot-AboveFun-\u2294 naf1 naf2 af12\n with AboveFun-\u2294 af12\n... | inj\u2081 af1 = contradiction af1 naf1\n... | inj\u2082 af2 = contradiction af2 naf2\n\\end{code}\n\nThe converse is also true. If the join of two values is not above a\nfunction, then neither of them is individually.\n\n\\begin{code}\nnot-AboveFun-\u2294-inv : \u2200{u u' : Value} \u2192 \u00ac AboveFun (u \u2294 u')\n \u2192 \u00ac AboveFun u \u00d7 \u00ac AboveFun u'\nnot-AboveFun-\u2294-inv af = \u27e8 f af , g af \u27e9\n where\n f : \u2200{u u' : Value} \u2192 \u00ac AboveFun (u \u2294 u') \u2192 \u00ac AboveFun u\n f{u}{u'} af12 \u27e8 v , \u27e8 w , lt \u27e9 \u27e9 =\n contradiction \u27e8 v , \u27e8 w , ConjR1\u2291 lt \u27e9 \u27e9 af12\n g : \u2200{u u' : Value} \u2192 \u00ac AboveFun (u \u2294 u') \u2192 \u00ac AboveFun u'\n g{u}{u'} af12 \u27e8 v , \u27e8 w , lt \u27e9 \u27e9 =\n contradiction \u27e8 v , \u27e8 w , ConjR2\u2291 lt \u27e9 \u27e9 af12\n\\end{code}\n\nThe property of being greater than a function value is decidable, as\nexhibited by the following function.\n\n\\begin{code}\nAboveFun? : (v : Value) \u2192 Dec (AboveFun v)\nAboveFun? \u22a5 = no AboveFun\u22a5\nAboveFun? (v \u21a6 w) = yes \u27e8 v , \u27e8 w , Refl\u2291 \u27e9 \u27e9\nAboveFun? (u \u2294 u')\n with AboveFun? u | AboveFun? u'\n... | yes \u27e8 v , \u27e8 w , lt \u27e9 \u27e9 | _ = yes \u27e8 v , \u27e8 w , (ConjR1\u2291 lt) \u27e9 \u27e9\n... | no _ | yes \u27e8 v , \u27e8 w , lt \u27e9 \u27e9 = yes \u27e8 v , \u27e8 w , (ConjR2\u2291 lt) \u27e9 \u27e9\n... | no x | no y = no (not-AboveFun-\u2294 x y)\n\\end{code}\n\n\n## Relating values to closures\n\nNext we relate semantic values to closures. The relation `\ud835\udd4d` is for\nclosures whose term is a lambda abstraction, i.e., in weak-head normal\nform (WHNF). The relation \ud835\udd3c is for any closure. Roughly speaking,\n`\ud835\udd3c v c` will hold if, when `v` is greater than a function value, `c` evaluates\nto a closure `c'` in WHNF and `\ud835\udd4d v c'`. Regarding `\ud835\udd4d v c`, it will hold when\n`c` is in WHNF, and if `v` is a function, the body of `c` evaluates\naccording to `v`.\n\n\\begin{code}\n\ud835\udd4d : Value \u2192 Clos \u2192 Set\n\ud835\udd3c : Value \u2192 Clos \u2192 Set\n\\end{code}\n\nWe define `\ud835\udd4d` as a function from values and closures to `Set` and not as a\ndata type because it is mutually recursive with `\ud835\udd3c` in a negative\nposition (to the left of an implication). We first perform case\nanalysis on the term in the closure. If the term is a variable or\napplication, then `\ud835\udd4d` is false (`Bot`). If the term is a lambda\nabstraction, we define `\ud835\udd4d` by recursion on the value, which we\ndescribe below.\n\n\\begin{code}\n\ud835\udd4d v (clos (` x\u2081) \u03b3) = Bot\n\ud835\udd4d v (clos (M \u00b7 M\u2081) \u03b3) = Bot\n\ud835\udd4d \u22a5 (clos (\u019b M) \u03b3) = \u22a4\n\ud835\udd4d (v \u21a6 w) (clos (\u019b N) \u03b3) =\n (\u2200{c : Clos} \u2192 \ud835\udd3c v c \u2192 AboveFun w \u2192 \u03a3[ c' \u2208 Clos ]\n (\u03b3 ,' c) \u22a2 N \u21d3 c' \u00d7 \ud835\udd4d w c')\n\ud835\udd4d (u \u2294 v) (clos (\u019b N) \u03b3) = \ud835\udd4d u (clos (\u019b N) \u03b3) \u00d7 \ud835\udd4d v (clos (\u019b N) \u03b3)\n\\end{code}\n\n* If the value is `\u22a5`, then the result is true (`\u22a4`).\n\n* If the value is a join (u \u2294 v), then the result is the pair\n (conjunction) of \ud835\udd4d is true for both u and v.\n\n* The important case is for a function value `v \u21a6 w` and closure\n `clos (\u019b N) \u03b3`. Given any closure `c` such that `\ud835\udd3c v c`, if `w` is\n greater than a function, then `N` evaluates (with `\u03b3` extended with `c`)\n to some closure `c'` and we have `\ud835\udd4d w c'`.\n\n\nThe definition of `\ud835\udd3c` is straightforward. If `v` is a greater than a\nfunction, then `M` evaluates to a closure related to `v`.\n\n\\begin{code}\n\ud835\udd3c v (clos M \u03b3') = AboveFun v \u2192 \u03a3[ c \u2208 Clos ] \u03b3' \u22a2 M \u21d3 c \u00d7 \ud835\udd4d v c\n\\end{code}\n\nThe proof of the main lemma is by induction on `\u03b3 \u22a2 M \u2193 v`, so it goes\nunderneath lambda abstractions and must therefore reason about open\nterms (terms with variables). So we must relate environments of\nsemantic values to environments of closures. In the following, `\ud835\udd3e`\nrelates `\u03b3` to `\u03b3'` if the corresponding values and closures are related\nby `\ud835\udd3c`.\n\n\\begin{code}\n\ud835\udd3e : \u2200{\u0393} \u2192 Env \u0393 \u2192 ClosEnv \u0393 \u2192 Set\n\ud835\udd3e {\u0393} \u03b3 \u03b3' = \u2200{x : \u0393 \u220b \u2605} \u2192 \ud835\udd3c (\u03b3 x) (\u03b3' x)\n\n\ud835\udd3e-\u2205 : \ud835\udd3e `\u2205 \u2205'\n\ud835\udd3e-\u2205 {()}\n\n\ud835\udd3e-ext : \u2200{\u0393}{\u03b3 : Env \u0393}{\u03b3' : ClosEnv \u0393}{v c}\n \u2192 \ud835\udd3e \u03b3 \u03b3' \u2192 \ud835\udd3c v c \u2192 \ud835\udd3e (\u03b3 `, v) (\u03b3' ,' c)\n\ud835\udd3e-ext {\u0393} {\u03b3} {\u03b3'} g e {Z} = e\n\ud835\udd3e-ext {\u0393} {\u03b3} {\u03b3'} g e {S x} = g\n\\end{code}\n\n\nWe need a few properties of the `\ud835\udd4d` and `\ud835\udd3c` relations. The first is that\na closure in the `\ud835\udd4d` relation must be in weak-head normal form. We\ndefine WHNF has follows.\n\n\\begin{code}\ndata WHNF : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 A \u2192 Set where\n \u019b_ : \u2200 {\u0393} {N : \u0393 , \u2605 \u22a2 \u2605}\n \u2192 WHNF (\u019b N)\n\\end{code}\n\nThe proof goes by cases on the term in the closure.\n\n\\begin{code}\n\ud835\udd4d\u2192WHNF : \u2200{\u0393}{\u03b3 : ClosEnv \u0393}{M : \u0393 \u22a2 \u2605}{v}\n \u2192 \ud835\udd4d v (clos M \u03b3) \u2192 WHNF M\n\ud835\udd4d\u2192WHNF {M = ` x} {v} ()\n\ud835\udd4d\u2192WHNF {M = \u019b N} {v} vc = \u019b_\n\ud835\udd4d\u2192WHNF {M = L \u00b7 M} {v} ()\n\\end{code}\n\nNext we have an introduction rule for `\ud835\udd4d` that mimics the `\u2294-intro`\nrule. If both `u` and `v` are related to a closure `c`, then their join is\ntoo.\n\n\\begin{code}\n\ud835\udd4d\u2294-intro : \u2200{c u v}\n \u2192 \ud835\udd4d u c \u2192 \ud835\udd4d v c\n ---------------\n \u2192 \ud835\udd4d (u \u2294 v) c\n\ud835\udd4d\u2294-intro {clos (` x) \u03b3} () vc\n\ud835\udd4d\u2294-intro {clos (\u019b N) \u03b3} uc vc = \u27e8 uc , vc \u27e9\n\ud835\udd4d\u2294-intro {clos (L \u00b7 M) \u03b3} () vc\n\\end{code}\n\nIn a moment we prove that `\ud835\udd4d` is preserved when going from a greater\nvalue to a lesser value: if `\ud835\udd4d v c` and `v' \u2291 v`, then `\ud835\udd4d v' c`.\nThis property, named `\ud835\udd4d-sub`, is needed by the main lemma in\nthe case for the `sub` rule.\n\nTo prove `\ud835\udd4d-sub`, we in turn need the following property concerning\nvalues that are not greater than a function, that is, values that are\nequivalent to `\u22a5`. In such cases, `\ud835\udd4d v (clos (\u019b N) \u03b3')` is trivially true.\n\n\\begin{code}\nnot-AboveFun-\ud835\udd4d : \u2200{v : Value}{\u0393}{\u03b3' : ClosEnv \u0393}{N : \u0393 , \u2605 \u22a2 \u2605 }\n \u2192 \u00ac AboveFun v\n -------------------\n \u2192 \ud835\udd4d v (clos (\u019b N) \u03b3')\nnot-AboveFun-\ud835\udd4d {\u22a5} af = tt\nnot-AboveFun-\ud835\udd4d {v \u21a6 v'} af = \u22a5-elim (contradiction \u27e8 v , \u27e8 v' , Refl\u2291 \u27e9 \u27e9 af)\nnot-AboveFun-\ud835\udd4d {v\u2081 \u2294 v\u2082} af\n with not-AboveFun-\u2294-inv af\n... | \u27e8 af1 , af2 \u27e9 = \u27e8 not-AboveFun-\ud835\udd4d af1 , not-AboveFun-\ud835\udd4d af2 \u27e9\n\\end{code}\n\nThe proofs of `\ud835\udd4d-sub` and `\ud835\udd3c-sub` are intertwined.\n\n\\begin{code}\nsub-\ud835\udd4d : \u2200{c : Clos}{v v'} \u2192 \ud835\udd4d v c \u2192 v' \u2291 v \u2192 \ud835\udd4d v' c\nsub-\ud835\udd3c : \u2200{c : Clos}{v v'} \u2192 \ud835\udd3c v c \u2192 v' \u2291 v \u2192 \ud835\udd3c v' c\n\\end{code}\n\nWe prove `\ud835\udd4d-sub` by case analysis on the closure's term, to dispatch the\ncases for variables and application. We then proceed by induction on\n`v' \u2291 v`. We describe each case below.\n\n\\begin{code}\nsub-\ud835\udd4d {clos (` x) \u03b3} {v} () lt\nsub-\ud835\udd4d {clos (L \u00b7 M) \u03b3} () lt\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} vc Bot\u2291 = tt\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} vc (ConjL\u2291 lt1 lt2) = \u27e8 (sub-\ud835\udd4d vc lt1) , sub-\ud835\udd4d vc lt2 \u27e9\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} \u27e8 vv1 , vv2 \u27e9 (ConjR1\u2291 lt) = sub-\ud835\udd4d vv1 lt\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} \u27e8 vv1 , vv2 \u27e9 (ConjR2\u2291 lt) = sub-\ud835\udd4d vv2 lt\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} vc (Trans\u2291{v = v\u2082} lt1 lt2) = sub-\ud835\udd4d (sub-\ud835\udd4d vc lt2) lt1\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} vc (Fun\u2291 lt1 lt2) ev1 sf\n with vc (sub-\ud835\udd3c ev1 lt1) (AboveFun-\u2291 sf lt2)\n... | \u27e8 c , \u27e8 Nc , v4 \u27e9 \u27e9 = \u27e8 c , \u27e8 Nc , sub-\ud835\udd4d v4 lt2 \u27e9 \u27e9\nsub-\ud835\udd4d {clos (\u019b N) \u03b3} {v \u21a6 w \u2294 v \u21a6 w'} \u27e8 vcw , vcw' \u27e9 Dist\u2291 ev1c sf\n with AboveFun? w | AboveFun? w'\n... | yes af2 | yes af3\n with vcw ev1c af2 | vcw' ev1c af3\n... | \u27e8 clos L \u03b4 , \u27e8 L\u21d3c\u2082 , \ud835\udd4dw \u27e9 \u27e9\n | \u27e8 c\u2083 , \u27e8 L\u21d3c\u2083 , \ud835\udd4dw' \u27e9 \u27e9 rewrite \u21d3-determ L\u21d3c\u2083 L\u21d3c\u2082 with \ud835\udd4d\u2192WHNF \ud835\udd4dw\n... | \u019b_ =\n \u27e8 clos L \u03b4 , \u27e8 L\u21d3c\u2082 , \u27e8 \ud835\udd4dw , \ud835\udd4dw' \u27e9 \u27e9 \u27e9\nsub-\ud835\udd4d {c} {v \u21a6 w \u2294 v \u21a6 w'} \u27e8 vcw , vcw' \u27e9 Dist\u2291 ev1c sf\n | yes af2 | no naf3\n with vcw ev1c af2\n... | \u27e8 clos {\u0393'} L \u03b3\u2081 , \u27e8 L\u21d3c2 , \ud835\udd4dw \u27e9 \u27e9\n with \ud835\udd4d\u2192WHNF \ud835\udd4dw\n... | \u019b_ {N = N'} =\n let \ud835\udd4dw' = not-AboveFun-\ud835\udd4d{w'}{\u0393'}{\u03b3\u2081}{N'} naf3 in\n \u27e8 clos (\u019b N') \u03b3\u2081 , \u27e8 L\u21d3c2 , \ud835\udd4d\u2294-intro \ud835\udd4dw \ud835\udd4dw' \u27e9 \u27e9\nsub-\ud835\udd4d {c} {v \u21a6 w \u2294 v \u21a6 w'} \u27e8 vcw , vcw' \u27e9 Dist\u2291 ev1c sf\n | no naf2 | yes af3\n with vcw' ev1c af3\n... | \u27e8 clos {\u0393'} L \u03b3\u2081 , \u27e8 L\u21d3c3 , \ud835\udd4dw'c \u27e9 \u27e9 \n with \ud835\udd4d\u2192WHNF \ud835\udd4dw'c\n... | \u019b_ {N = N'} =\n let \ud835\udd4dwc = not-AboveFun-\ud835\udd4d{w}{\u0393'}{\u03b3\u2081}{N'} naf2 in\n \u27e8 clos (\u019b N') \u03b3\u2081 , \u27e8 L\u21d3c3 , \ud835\udd4d\u2294-intro \ud835\udd4dwc \ud835\udd4dw'c \u27e9 \u27e9\nsub-\ud835\udd4d {c} {v \u21a6 w \u2294 v \u21a6 w'} \u27e8 vcw , vcw' \u27e9 Dist\u2291 ev1c \u27e8 v' , \u27e8 w'' , lt \u27e9 \u27e9\n | no naf2 | no naf3\n with AboveFun-\u2294 \u27e8 v' , \u27e8 w'' , lt \u27e9 \u27e9\n... | inj\u2081 af2 = \u22a5-elim (contradiction af2 naf2)\n... | inj\u2082 af3 = \u22a5-elim (contradiction af3 naf3)\n\\end{code}\n\n* Case `Bot\u2291`. We immediately have `\ud835\udd4d \u22a5 (clos (\u019b N) \u03b3)`.\n\n* Case `ConjL\u2291`.\n\n v\u2081' \u2291 v v\u2082' \u2291 v\n -------------------\n (v\u2081' \u2294 v\u2082') \u2291 v\n\n The induction hypotheses gives us `\ud835\udd4d v\u2081' (clos (\u019b N) \u03b3)`\n and `\ud835\udd4d v\u2082' (clos (\u019b N) \u03b3)`, which is all we need for this case. \n\n* Case `ConjR1\u2291`.\n\n v' \u2291 v\u2081\n -------------\n v' \u2291 (v\u2081 \u2294 v\u2082)\n\n The induction hypothesis gives us `\ud835\udd4d v' (clos (\u019b N) \u03b3)`.\n\n* Case `ConjR2\u2291`.\n\n v' \u2291 v\u2082\n -------------\n v' \u2291 (v\u2081 \u2294 v\u2082)\n\n Again, the induction hypothesis gives us `\ud835\udd4d v' (clos (\u019b N) \u03b3)`.\n\n* Case `Trans\u2291`.\n\n v' \u2291 v\u2082 v\u2082 \u2291 v\n -----------------\n v' \u2291 v\n\n The induction hypothesis for `v\u2082 \u2291 v` gives us\n `\ud835\udd4d v\u2082 (clos (\u019b N) \u03b3)`. We apply the induction hypothesis\n for `v' \u2291 v\u2082` to conclude that `\ud835\udd4d v' (clos (\u019b N) \u03b3)`.\n\n* Case `Dist\u2291`. This case is the most difficult. We have\n\n \ud835\udd4d (v \u21a6 w) (clos (\u019b N) \u03b3)\n \ud835\udd4d (v \u21a6 w') (clos (\u019b N) \u03b3)\n\n and need to show that \n\n \ud835\udd4d (v \u21a6 (w \u2294 w')) (clos (\u019b N) \u03b3)\n \n Let `c` be an arbtrary closure such that `\ud835\udd3c v c`.\n Assume `w \u2294 w'` is greater than a function.\n Unfortunately, this does not mean that both `w` and `w'`\n are above functions. But thanks to the lemma `AboveFun-\u2294`,\n we know that at least one of them is greater than a function.\n \n * Suppose both of them are greater than a function. Then we have\n `\u03b3 \u22a2 N \u21d3 clos L \u03b4` and `\ud835\udd4d w (clos L \u03b4)`. We also have `\u03b3 \u22a2 N \u21d3 c\u2083` and\n `\ud835\udd4d w' c\u2083`. Because the big-step semantics is deterministic, we have\n `c\u2083 \u2261 clos L \u03b4`. Also, from `\ud835\udd4d w (clos L \u03b4)` we know that `L \u2261 \u019b N'`\n for some `N'`. We conclude that `\ud835\udd4d (w \u2294 w') (clos (\u019b N') \u03b4)`.\n\n * Suppose one of them is greater than a function and the other is\n not: say `AboveFun w` and `\u00ac AboveFun w'`. Then from\n `\ud835\udd4d (v \u21a6 w) (clos (\u019b N) \u03b3)`\n we have `\u03b3 \u22a2 N \u21d3 clos L \u03b3\u2081` and `\ud835\udd4d w (clos L \u03b3\u2081)`. From this we have\n `L \u2261 \u019b N'` for some `N'`. Meanwhile, from `\u00ac AboveFun w'` we have\n `\ud835\udd4d w' (clos L \u03b3\u2081)`. We conclude that\n `\ud835\udd4d (w \u2294 w') (clos (\u019b N') \u03b3\u2081)`.\n \n\nThe proof of `sub-\ud835\udd3c` is direct and explained below.\n\n\\begin{code}\nsub-\ud835\udd3c {clos M \u03b3} {v} {v'} \ud835\udd3cv v'\u2291v fv'\n with \ud835\udd3cv (AboveFun-\u2291 fv' v'\u2291v)\n... | \u27e8 c , \u27e8 M\u21d3c , \ud835\udd4dv \u27e9 \u27e9 =\n \u27e8 c , \u27e8 M\u21d3c , sub-\ud835\udd4d \ud835\udd4dv v'\u2291v \u27e9 \u27e9\n\\end{code}\n\nFrom `AboveFun v'` and `v' \u2291 v` we have `AboveFun v`. Then with `\ud835\udd3c v c` we\nobtain a closure `c` such that `\u03b3 \u22a2 M \u21d3 c` and `\ud835\udd4d v c`. We conclude with an\napplication of `sub-\ud835\udd4d` with `v' \u2291 v` to show `\ud835\udd4d v' c`.\n\n\n## Programs with function denotation terminate via call-by-name\n\nThe main lemma proves that if a term has a denotation that is above a\nfunction, then it terminates via call-by-name. More formally, if\n`\u03b3 \u22a2 M \u2193 v` and `\ud835\udd3e \u03b3 \u03b3'`, then `\ud835\udd3c v (clos M \u03b3')`. The proof is by\ninduction on the derivation of `\u03b3 \u22a2 M \u2193 v` we discuss each case below.\n\nThe following lemma, kth-x, is used in the case for the `var` rule.\n\n\\begin{code}\nkth-x : \u2200{\u0393}{\u03b3' : ClosEnv \u0393}{x : \u0393 \u220b \u2605}\n \u2192 \u03a3[ \u0394 \u2208 Context ] \u03a3[ \u03b4 \u2208 ClosEnv \u0394 ] \u03a3[ M \u2208 \u0394 \u22a2 \u2605 ]\n \u03b3' x \u2261 clos M \u03b4\nkth-x{\u03b3' = \u03b3'}{x = x} with \u03b3' x\n... | clos{\u0393 = \u0394} M \u03b4 = \u27e8 \u0394 , \u27e8 \u03b4 , \u27e8 M , refl \u27e9 \u27e9 \u27e9\n\\end{code}\n\n\\begin{code}\n\u2193\u2192\ud835\udd3c : \u2200{\u0393}{\u03b3 : Env \u0393}{\u03b3' : ClosEnv \u0393}{M : \u0393 \u22a2 \u2605 }{v}\n \u2192 \ud835\udd3e \u03b3 \u03b3' \u2192 \u03b3 \u22a2 M \u2193 v \u2192 \ud835\udd3c v (clos M \u03b3')\n\u2193\u2192\ud835\udd3c {\u0393} {\u03b3} {\u03b3'} \ud835\udd3e\u03b3\u03b3' (var{x = x}) f\u03b3x\n with kth-x{\u0393}{\u03b3'}{x} | \ud835\udd3e\u03b3\u03b3'{x = x}\n... | \u27e8 \u0394 , \u27e8 \u03b4 , \u27e8 M' , eq \u27e9 \u27e9 \u27e9 | \ud835\udd3e\u03b3\u03b3'x rewrite eq\n with \ud835\udd3e\u03b3\u03b3'x f\u03b3x\n... | \u27e8 c , \u27e8 M'\u21d3c , \ud835\udd4d\u03b3x \u27e9 \u27e9 =\n \u27e8 c , \u27e8 (\u21d3-var eq M'\u21d3c) , \ud835\udd4d\u03b3x \u27e9 \u27e9\n\u2193\u2192\ud835\udd3c {\u0393} {\u03b3} {\u03b3'} \ud835\udd3e\u03b3\u03b3' (\u21a6-elim{L = L}{M = M}{v = v\u2081}{w = v} d\u2081 d\u2082) fv\n with \u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2081 \u27e8 v\u2081 , \u27e8 v , Refl\u2291 \u27e9 \u27e9\n... | \u27e8 clos L' \u03b4 , \u27e8 L\u21d3L' , \ud835\udd4dv\u2081\u21a6v \u27e9 \u27e9 \n with \ud835\udd4d\u2192WHNF \ud835\udd4dv\u2081\u21a6v\n... | \u019b_ {N = N} \n with \ud835\udd4dv\u2081\u21a6v {clos M \u03b3'} (\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2082) fv\n... | \u27e8 c' , \u27e8 N\u21d3c' , \ud835\udd4dv \u27e9 \u27e9 =\n \u27e8 c' , \u27e8 \u21d3-app L\u21d3L' N\u21d3c' , \ud835\udd4dv \u27e9 \u27e9\n\u2193\u2192\ud835\udd3c {\u0393} {\u03b3} {\u03b3'} \ud835\udd3e\u03b3\u03b3' (\u21a6-intro{N = N}{v = v}{w = w} d) fv\u21a6w =\n \u27e8 clos (\u019b N) \u03b3' , \u27e8 \u21d3-lam , E \u27e9 \u27e9\n where E : {c : Clos} \u2192 \ud835\udd3c v c \u2192 AboveFun w\n \u2192 \u03a3[ c' \u2208 Clos ] (\u03b3' ,' c) \u22a2 N \u21d3 c' \u00d7 \ud835\udd4d w c'\n E {c} \ud835\udd3cvc fw = \u2193\u2192\ud835\udd3c (\u03bb {x} \u2192 \ud835\udd3e-ext{\u0393}{\u03b3}{\u03b3'} \ud835\udd3e\u03b3\u03b3' \ud835\udd3cvc {x}) d fw\n\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' \u22a5-intro f\u22a5 = \u22a5-elim (AboveFun\u22a5 f\u22a5)\n\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' (\u2294-intro{v = v\u2081}{w = v\u2082} d\u2081 d\u2082) fv12\n with AboveFun? v\u2081 | AboveFun? v\u2082\n... | yes fv1 | yes fv2\n with \u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2081 fv1 | \u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2082 fv2 \n... | \u27e8 c\u2081 , \u27e8 M\u21d3c\u2081 , \ud835\udd4dv\u2081 \u27e9 \u27e9 | \u27e8 c\u2082 , \u27e8 M\u21d3c\u2082 , \ud835\udd4dv\u2082 \u27e9 \u27e9\n rewrite \u21d3-determ M\u21d3c\u2082 M\u21d3c\u2081 =\n \u27e8 c\u2081 , \u27e8 M\u21d3c\u2081 , \ud835\udd4d\u2294-intro \ud835\udd4dv\u2081 \ud835\udd4dv\u2082 \u27e9 \u27e9\n\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' (\u2294-intro{v = v\u2081}{w = v\u2082} d\u2081 d\u2082) fv12 | yes fv1 | no nfv2\n with \u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2081 fv1 \n... | \u27e8 clos {\u0393'} M' \u03b3\u2081 , \u27e8 M\u21d3c\u2081 , \ud835\udd4dv\u2081 \u27e9 \u27e9\n with \ud835\udd4d\u2192WHNF \ud835\udd4dv\u2081\n... | \u019b_ {N = N} =\n let \ud835\udd4dv\u2082 = not-AboveFun-\ud835\udd4d{v\u2082}{\u0393'}{\u03b3\u2081}{N} nfv2 in\n \u27e8 clos (\u019b N) \u03b3\u2081 , \u27e8 M\u21d3c\u2081 , \ud835\udd4d\u2294-intro \ud835\udd4dv\u2081 \ud835\udd4dv\u2082 \u27e9 \u27e9\n\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' (\u2294-intro{v = v\u2081}{w = v\u2082} d\u2081 d\u2082) fv12 | no nfv1 | yes fv2\n with \u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' d\u2082 fv2\n... | \u27e8 clos {\u0393'} M' \u03b3\u2081 , \u27e8 M'\u21d3c\u2082 , \ud835\udd4d2c \u27e9 \u27e9\n with \ud835\udd4d\u2192WHNF \ud835\udd4d2c\n... | \u019b_ {N = N} =\n let \ud835\udd4d1c = not-AboveFun-\ud835\udd4d{v\u2081}{\u0393'}{\u03b3\u2081}{N} nfv1 in\n \u27e8 clos (\u019b N) \u03b3\u2081 , \u27e8 M'\u21d3c\u2082 , \ud835\udd4d\u2294-intro \ud835\udd4d1c \ud835\udd4d2c \u27e9 \u27e9\n\u2193\u2192\ud835\udd3c \ud835\udd3e\u03b3\u03b3' (\u2294-intro d\u2081 d\u2082) fv12 | no nfv1 | no nfv2\n with AboveFun-\u2294 fv12\n... | inj\u2081 fv1 = \u22a5-elim (contradiction fv1 nfv1)\n... | inj\u2082 fv2 = \u22a5-elim (contradiction fv2 nfv2)\n\u2193\u2192\ud835\udd3c {\u0393} {\u03b3} {\u03b3'} {M} {v'} \ud835\udd3e\u03b3\u03b3' (sub{v = v} d v'\u2291v) fv'\n with \u2193\u2192\ud835\udd3c {\u0393} {\u03b3} {\u03b3'} {M} \ud835\udd3e\u03b3\u03b3' d (AboveFun-\u2291 fv' v'\u2291v)\n... | \u27e8 c , \u27e8 M\u21d3c , \ud835\udd4dv \u27e9 \u27e9 =\n \u27e8 c , \u27e8 M\u21d3c , sub-\ud835\udd4d \ud835\udd4dv v'\u2291v \u27e9 \u27e9\n\\end{code}\n\n* Case `var`. Looking up `x` in `\u03b3'` yields some closure, `clos M' \u03b4`,\n and from `\ud835\udd3e \u03b3 \u03b3'` we have `\ud835\udd3c (\u03b3 x) (clos M' \u03b4)`. With the premise\n `AboveFun (\u03b3 x)`, we obtain a closure `c` such that `\u03b4 \u22a2 M' \u21d3 c`\n and `\ud835\udd4d (\u03b3 x) c`. To conclude `\u03b3' \u22a2 x \u21d3 c` via `\u21d3-var`, we \n need `\u03b3' x \u2261 clos M' \u03b4`, which is obvious, but it requires some\n Agda shananigans via the `kth-x` lemma to get our hands on it.\n\n* Case `\u21a6-elim`. We have `\u03b3 \u22a2 L \u00b7 M \u2193 v`.\n The induction hypothesis for `\u03b3 \u22a2 L \u2193 v\u2081 \u21a6 v`\n gives us `\u03b3' \u22a2 L \u21d3 clos L' \u03b4` and `\ud835\udd4d v (clos L' \u03b4)`.\n Of course, `L' \u2261 \u019b N` for some `N`.\n By the induction hypothesis for `\u03b3 \u22a2 M \u2193 v\u2081`,\n we have `\ud835\udd3c v\u2081 (clos M \u03b3')`.\n Together with the premise `AboveFun v` and `\ud835\udd4d v (clos L' \u03b4)`,\n we obtain a closure `c'` such that `\u03b4 \u22a2 N \u21d3 c'` and `\ud835\udd4d v c'`.\n We conclude that `\u03b3' \u22a2 L \u00b7 M \u21d3 c'` by rule `\u21d3-app`.\n\n* Case `\u21a6-intro`. We have `\u03b3 \u22a2 \u019b N \u2193 v \u21a6 w`.\n We immediately have `\u03b3' \u22a2 \u019b M \u21d3 clos (\u019b M) \u03b3'` by rule `\u21d3-lam`.\n But we also need to prove `\ud835\udd4d (v \u21a6 w) (clos (\u019b N) \u03b3')`.\n Let `c` by an arbitrary closure such that `\ud835\udd3c v c`.\n Suppose `v'` is greater than a function value.\n We need to show that `\u03b3' , c \u22a2 N \u21d3 c'` and `\ud835\udd4d v' c'` for some `c'`.\n We prove this by the induction hypothesis for `\u03b3 , v \u22a2 N \u2193 v'`\n but we must first show that `\ud835\udd3e (\u03b3 , v) (\u03b3' , c)`. We prove\n that by the lemma `\ud835\udd3e-ext`, using facts `\ud835\udd3e \u03b3 \u03b3'` and `\ud835\udd3c v c`.\n\n* Case `\u22a5-intro`. We have the premise `AboveFun \u22a5`, but that's impossible.\n\n* Case `\u2294-intro`. We have `\u03b3 \u22a2 M \u2193 (v\u2081 \u2294 v\u2082)` and `AboveFun (v\u2081 \u2294 v\u2082)`\n and need to show `\u03b3' \u22a2 M \u2193 c` and `\ud835\udd4d (v\u2081 \u2294 v\u2082) c` for some `c`.\n Again, by `AboveFun-\u2294`, at least one of `v\u2081` or `v\u2082` is greater than\n a function.\n\n * Suppose both `v\u2081` and `v\u2082` are greater than a function value.\n By the induction hypotheses for `\u03b3 \u22a2 M \u2193 v\u2081` and `\u03b3 \u22a2 M \u2193 v\u2082`\n we have `\u03b3' \u22a2 M \u21d3 c\u2081`, `\ud835\udd4d v\u2081 c\u2081`, `\u03b3' \u22a2 M \u21d3 c\u2082`, and `\ud835\udd4d v\u2082 c\u2082`\n for some `c\u2081` and `c\u2082`. Because `\u21d3` is deterministic, we have `c\u2082 \u2261 c\u2081`.\n Then by `\ud835\udd4d\u2294-intro` we conclude that `\ud835\udd4d (v\u2081 \u2294 v\u2082) c\u2081`.\n\n * Without loss of generality, suppose `v\u2081` is greater than a function\n value but `v\u2082` is not. By the induction hypotheses for `\u03b3 \u22a2 M \u2193 v\u2081`,\n and using `\ud835\udd4d\u2192WHNF`, we have `\u03b3' \u22a2 M \u21d3 clos (\u019b N) \u03b3\u2081`\n and `\ud835\udd4d v\u2081 (clos (\u019b N) \u03b3\u2081)`.\n Then because `v\u2082` is not greater than a function, we also have\n `\ud835\udd4d v\u2082 (clos (\u019b N) \u03b3\u2081)`. We conclude that `\ud835\udd4d (v\u2081 \u2294 v\u2082) (clos (\u019b N) \u03b3\u2081)`.\n \n* Case `sub`. We have `\u03b3 \u22a2 M \u2193 v`, `v' \u2291 v`, and `AboveFun v'`.\n We need to show that `\u03b3' \u22a2 M \u21d3 c` and `\ud835\udd4d v' c` for some `c`.\n We have `AboveFun v` by `AboveFun-\u2291`,\n so the induction hypothesis for `\u03b3 \u22a2 M \u2193 v` gives us a closure `c`\n such that `\u03b3' \u22a2 M \u21d3 c` and `\ud835\udd4d v c`. We conclude that `\ud835\udd4d v' c` by `sub-\ud835\udd4d`.\n\n\n## Proof of denotational adequacy\n\nThe adequacy property is a corollary of the main lemma.\nWe have `\u2205 \u22a2 \u019b N \u2193 \u22a5 \u21a6 \u22a5`, so `\u2130 M \u2243 \u2130 (\u019b N)`\ngives us `\u2205 \u22a2 M \u2193 \u22a5 \u21a6 \u22a5`. Then the main lemma gives us\n`\u2205 \u22a2 M \u21d3 clos (\u019b N\u2032) \u03b3` for some `N\u2032` and `\u03b3`.\n\n\\begin{code}\nadequacy : \u2200{M : \u2205 \u22a2 \u2605}{N : \u2205 , \u2605 \u22a2 \u2605} \u2192 \u2130 M \u2243 \u2130 (\u019b N)\n \u2192 \u03a3[ \u0393 \u2208 Context ] \u03a3[ N\u2032 \u2208 (\u0393 , \u2605 \u22a2 \u2605) ] \u03a3[ \u03b3 \u2208 ClosEnv \u0393 ]\n \u2205' \u22a2 M \u21d3 clos (\u019b N\u2032) \u03b3\nadequacy{M}{N} eq\n with \u2193\u2192\ud835\udd3c \ud835\udd3e-\u2205 ((proj\u2082 (eq `\u2205 (\u22a5 \u21a6 \u22a5))) (\u21a6-intro \u22a5-intro))\n \u27e8 \u22a5 , \u27e8 \u22a5 , Refl\u2291 \u27e9 \u27e9\n... | \u27e8 clos {\u0393} M\u2032 \u03b3 , \u27e8 M\u21d3c , Vc \u27e9 \u27e9\n with \ud835\udd4d\u2192WHNF Vc\n... | \u019b_ {N = N\u2032} = \n \u27e8 \u0393 , \u27e8 N\u2032 , \u27e8 \u03b3 , M\u21d3c \u27e9 \u27e9 \u27e9\n\\end{code}\n\n\n## Call-by-name is equivalent to beta reduction\n\nAs promised, we return to the question of whether call-by-name\nevaluation is equivalent to beta reduction. In the chapter CallByName\nwe established the forward direction: that if call-by-name produces a\nresult, then the program beta reduces to a lambda abstraction. We now\nprove the backward direction of the if-and-only-if, leveraging our\nresults about the denotational semantics.\n\n\\begin{code}\nreduce\u2192cbn : \u2200 {M : \u2205 \u22a2 \u2605} {N : \u2205 , \u2605 \u22a2 \u2605}\n \u2192 M \u2014\u21a0 \u019b N\n \u2192 \u03a3[ \u0394 \u2208 Context ] \u03a3[ N\u2032 \u2208 \u0394 , \u2605 \u22a2 \u2605 ] \u03a3[ \u03b4 \u2208 ClosEnv \u0394 ] \n \u2205' \u22a2 M \u21d3 clos (\u019b N\u2032) \u03b4\nreduce\u2192cbn M\u2014\u21a0\u019bN = adequacy (soundness M\u2014\u21a0\u019bN)\n\\end{code}\n\nSuppose `M \u2014\u21a0 \u019b N`. Soundness of the denotational semantics gives us\n`\u2130 M \u2243 \u2130 (\u019b N)`. Then by adequacy we conclude that\n`\u2205' \u22a2 M \u21d3 clos (\u019b N\u2032) \u03b4` for some `N\u2032` and `\u03b4`.\n\nPutting the two directions of the if-and-only-if together, we\nestablish that call-by-name evaluation is equivalent to beta reduction\nin the following sense.\n\n\\begin{code}\ncbn\u2194reduce : \u2200 {M : \u2205 \u22a2 \u2605}\n \u2192 (\u03a3[ N \u2208 \u2205 , \u2605 \u22a2 \u2605 ] (M \u2014\u21a0 \u019b N))\n iff\n (\u03a3[ \u0394 \u2208 Context ] \u03a3[ N\u2032 \u2208 \u0394 , \u2605 \u22a2 \u2605 ] \u03a3[ \u03b4 \u2208 ClosEnv \u0394 ]\n \u2205' \u22a2 M \u21d3 clos (\u019b N\u2032) \u03b4)\ncbn\u2194reduce {M} = \u27e8 (\u03bb x \u2192 reduce\u2192cbn (proj\u2082 x)) ,\n (\u03bb x \u2192 cbn\u2192reduce (proj\u2082 (proj\u2082 (proj\u2082 x)))) \u27e9\n\\end{code}\n\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \ud835\udd3c U+1D53C MATHEMATICAL DOUBLE-STRUCK CAPITAL E (\\bE)\n \ud835\udd3e U+1D53E MATHEMATICAL DOUBLE-STRUCK CAPITAL G (\\bG)\n \ud835\udd4d U+1D53E MATHEMATICAL DOUBLE-STRUCK CAPITAL V (\\bV)\n \n","avg_line_length":35.8184615385,"max_line_length":87,"alphanum_fraction":0.5676917791} +{"size":744,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe #-}\n\nmodule Data.Maybe.Sugar where\n\nopen import Prelude\nopen import Data.Maybe\n\ninfixl 4 _<*>_\n\\end{code}\n%<*bind>\n\\begin{code}\n_>>=_ : Maybe A \u2192 (A \u2192 Maybe B) \u2192 Maybe B\nnothing >>= f = nothing\njust x >>= f = f x\n\\end{code}\n%<\/bind>\n\\begin{code}\n\n\\end{code}\n%<*pure>\n\\begin{code}\npure : A \u2192 Maybe A\npure = just\n\\end{code}\n%<\/pure>\n%<*ap>\n\\begin{code}\n_<*>_ : Maybe (A \u2192 B) \u2192\n Maybe A \u2192\n Maybe B\nnothing <*> xs = nothing\njust f <*> nothing = nothing\njust f <*> just x = just (f x)\n\\end{code}\n%<\/ap>\n\\begin{code}\n_>>_ : Maybe A \u2192 Maybe B \u2192 Maybe B\nnothing >> _ = nothing\njust _ >> y = y\n\nguard : Bool \u2192 Maybe \u22a4\nguard false = nothing\nguard true = just tt\n\\end{code}\n","avg_line_length":16.1739130435,"max_line_length":41,"alphanum_fraction":0.5739247312} +{"size":1965,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe --guardedness #-}\n\nmodule Codata.Stream where\n\nopen import Prelude\nopen import Data.List using (List; _\u2237_; [])\nopen import Data.List.Kleene\nimport Data.List.Kleene.Membership as Kleene\nopen import Data.Fin\n\nprivate\n module AsCodata where\n\\end{code}\n%<*codata-stream>\n\\begin{code}\n record Stream {a} (A : Type a) : Type a where\n coinductive\n field\n head : A\n tail : Stream A\n\\end{code}\n%<\/codata-stream>\n\\begin{code}\n\nStream : Type a \u2192 Type a\n\\end{code}\n%<*stream-def>\n\\begin{code}\nStream A = \u2115 \u2192 A\n\\end{code}\n%<\/stream-def>\n\\begin{code}\n\n\ninfixr 5 _\u2208_\n_\u2208_ : A \u2192 Stream A \u2192 Type _\nx \u2208 xs = fiber xs x\n\ntoList : \u2115 \u2192 Stream A \u2192 List A\ntoList zero xs = []\ntoList (suc n) xs = xs zero \u2237 toList n (xs \u2218 suc)\n\nmutual\n concat\u22c6 : A \u22c6 \u2192 Stream (A \u207a) \u2192 Stream A\n concat\u22c6 [] xs = concat\u207a (xs zero) (xs \u2218 suc)\n concat\u22c6 (\u2239 x) xs = concat\u207a x xs\n\n concat\u207a : A \u207a \u2192 Stream (A \u207a) \u2192 Stream A\n concat\u207a x xs zero = x .head\n concat\u207a x xs (suc n) = concat\u22c6 (x .tail) xs n\n\nconcat : Stream (A \u207a) \u2192 Stream A\nconcat xs = concat\u207a (xs zero) (xs \u2218 suc)\n\ninfixr 5 _\u2208\u00b2_\n_\u2208\u00b2_ : A \u2192 Stream (A \u207a) \u2192 Type _\nx \u2208\u00b2 xs = \u2203[ n ] x Kleene.\u2208\u207a xs n\n\nmutual\n \u25c7++\u22c6 : \u2200 (x : A) y ys \u2192 x Kleene.\u2208\u22c6 y \u2192 x \u2208 concat\u22c6 y ys\n \u25c7++\u22c6 x (\u2239 y) ys x\u2208y = \u25c7++\u207a x y ys x\u2208y\n\n \u25c7++\u207a : \u2200 (x : A) y ys \u2192 x Kleene.\u2208\u207a y \u2192 x \u2208 concat\u207a y ys\n \u25c7++\u207a x y ys (f0 , x\u2208y) = zero , x\u2208y\n \u25c7++\u207a x y ys (fs n , x\u2208y) = let m , p = \u25c7++\u22c6 x (y .tail) ys (n , x\u2208y) in suc m , p\n\nmutual\n ++\u25c7\u22c6 : \u2200 (x : A) y ys \u2192 x \u2208 concat ys \u2192 x \u2208 concat\u22c6 y ys\n ++\u25c7\u22c6 x [] ys x\u2208ys = x\u2208ys\n ++\u25c7\u22c6 x (\u2239 y) ys x\u2208ys = ++\u25c7\u207a x y ys x\u2208ys\n\n ++\u25c7\u207a : \u2200 (x : A) y ys \u2192 x \u2208 concat ys \u2192 x \u2208 concat\u207a y ys\n ++\u25c7\u207a x y ys x\u2208ys = let n , p = ++\u25c7\u22c6 x (y .tail) ys x\u2208ys in suc n , p\n\nconcat-\u2208 : \u2200 (x : A) xs \u2192 x \u2208\u00b2 xs \u2192 x \u2208 concat xs\nconcat-\u2208 x xs (zero , x\u2208xs) = \u25c7++\u207a x (xs zero) (xs \u2218 suc) x\u2208xs\nconcat-\u2208 x xs (suc n , x\u2208xs) = ++\u25c7\u207a x (xs zero) (xs \u2218 suc) (concat-\u2208 x (xs \u2218 suc) (n , x\u2208xs))\n\\end{code}\n","avg_line_length":24.5625,"max_line_length":93,"alphanum_fraction":0.5312977099} +{"size":19577,"ext":"lagda","lang":"Literate Agda","max_stars_count":1003.0,"content":"---\ntitle : \"Typed: Typed Lambda term representation\"\nlayout : page\npermalink : \/Typed\n---\n\n\n## Imports\n\n\\begin{code}\nmodule Typed where\n\\end{code}\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr; filter)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _\u2238_; _\u2264_; _\u2294_)\nopen import Data.Nat.Properties using (\u2264-refl; \u2264-trans; m\u2264m\u2294n; n\u2264m\u2294n; 1+n\u2270n)\nopen import Data.String using (String)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Function using (_\u2218_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Negation using (\u00ac?)\nopen import Collections\n\nimport Data.Nat as Nat\nimport Data.String as String\n\npattern [_] x = x \u2237 []\npattern [_,_] x y = x \u2237 y \u2237 []\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\n\\end{code}\n\n\n## Identifiers\n\n\\begin{code}\ndata Id : Set where\n id : String \u2192 \u2115 \u2192 Id\n\n_\u225f_ : \u2200 (x y : Id) \u2192 Dec (x \u2261 y)\nid s m \u225f id t n with s String.\u225f t | m Nat.\u225f n\n... | yes refl | yes refl = yes refl\n... | yes refl | no m\u2262n = no (\u03bb {refl \u2192 m\u2262n refl})\n... | no s\u2262t | _ = no (\u03bb {refl \u2192 s\u2262t refl})\n\n{-\n_\u2260_ : \u2200 (x y : Id) \u2192 x \u2262 y\nx \u2260 y with x \u225f y\n... | no x\u2262y = x\u2262y\n... | yes _ = impossible\n where postulate impossible : _\n-}\n\\end{code}\n\n\n## Syntax\n\n\\begin{code}\ninfixr 5 _\u21d2_\ninfixl 5 _,_\u2982_\ninfix 4 _\u220b_\u2982_\ninfix 4 _\u22a2_\u2982_\ninfix 5 `\u03bb_\u21d2_\ninfixl 6 `if0_then_else_\ninfix 7 `suc_ `pred_ `Y_\ninfixl 8 _\u00b7_\ninfix 9 `_\n\ndata Type : Set where\n `\u2115 : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n\ndata Env : Set where\n \u03b5 : Env\n _,_\u2982_ : Env \u2192 Id \u2192 Type \u2192 Env\n\ndata Term : Set where\n `_ : Id \u2192 Term\n `\u03bb_\u21d2_ : Id \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n `zero : Term \n `suc_ : Term \u2192 Term\n `pred_ : Term \u2192 Term\n `if0_then_else_ : Term \u2192 Term \u2192 Term \u2192 Term\n `Y_ : Term \u2192 Term\n\ndata _\u220b_\u2982_ : Env \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 A x}\n -----------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 A B x w}\n \u2192 w \u2262 x\n \u2192 \u0393 \u220b w \u2982 B\n -----------------\n \u2192 \u0393 , x \u2982 A \u220b w \u2982 B\n\ndata _\u22a2_\u2982_ : Env \u2192 Term \u2192 Type \u2192 Set where\n\n Ax : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x \u2982 A\n ---------------------\n \u2192 \u0393 \u22a2 ` x \u2982 A\n\n \u22a2\u03bb : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2982 B\n ------------------------\n \u2192 \u0393 \u22a2 (`\u03bb x \u21d2 N) \u2982 A \u21d2 B\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2982 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2982 A\n --------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2982 B\n\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2982 `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2982 `\u2115\n\n \u22a2pred : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\n ----------------\n \u2192 \u0393 \u22a2 `pred M \u2982 `\u2115\n\n \u22a2if0 : \u2200 {\u0393 L M N A}\n \u2192 \u0393 \u22a2 L \u2982 `\u2115\n \u2192 \u0393 \u22a2 M \u2982 A\n \u2192 \u0393 \u22a2 N \u2982 A\n ----------------------------\n \u2192 \u0393 \u22a2 `if0 L then M else N \u2982 A\n\n \u22a2Y : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M \u2982 A \u21d2 A\n ---------------\n \u2192 \u0393 \u22a2 `Y M \u2982 A\n\\end{code}\n\n## Test examples\n\n\\begin{code}\nm n s z : Id\np = id \"p\" 0 -- 0\nm = id \"m\" 0 -- 1\nn = id \"n\" 0 -- 2\ns = id \"s\" 0 -- 3\nz = id \"z\" 0 -- 4\n\ns\u2262z : s \u2262 z\ns\u2262z ()\n\nn\u2262z : n \u2262 z\nn\u2262z ()\n\nn\u2262s : n \u2262 s\nn\u2262s ()\n\nm\u2262z : m \u2262 z\nm\u2262z ()\n\nm\u2262s : m \u2262 s\nm\u2262s ()\n\nm\u2262n : m \u2262 n\nm\u2262n ()\n\np\u2262n : p \u2262 n\np\u2262n ()\n\np\u2262m : p \u2262 m\np\u2262m ()\n\ntwo : Term\ntwo = `suc `suc `zero\n\n\u22a2two : \u03b5 \u22a2 two \u2982 `\u2115\n\u22a2two = (\u22a2suc (\u22a2suc \u22a2zero))\n\nplus : Term\nplus = `Y (`\u03bb p \u21d2 `\u03bb m \u21d2 `\u03bb n \u21d2 `if0 ` m then ` n else ` p \u00b7 (`pred ` m) \u00b7 ` n)\n\n\u22a2plus : \u03b5 \u22a2 plus \u2982 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\n\u22a2plus = (\u22a2Y (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2if0 (Ax \u22a2m) (Ax \u22a2n) (Ax \u22a2p \u00b7 (\u22a2pred (Ax \u22a2m)) \u00b7 Ax \u22a2n))))))\n where\n \u22a2p = S p\u2262n (S p\u2262m Z)\n \u22a2m = S m\u2262n Z\n \u22a2n = Z\n\nfour : Term\nfour = plus \u00b7 two \u00b7 two\n\n\u22a2four : \u03b5 \u22a2 four \u2982 `\u2115\n\u22a2four = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\n\nCh : Type\nCh = (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115\n\ntwoCh : Term\ntwoCh = `\u03bb s \u21d2 `\u03bb z \u21d2 (` s \u00b7 (` s \u00b7 ` z))\n\n\u22a2twoCh : \u03b5 \u22a2 twoCh \u2982 Ch\n\u22a2twoCh = (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2s \u00b7 (Ax \u22a2s \u00b7 Ax \u22a2z))))\n where\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nplusCh : Term\nplusCh = `\u03bb m \u21d2 `\u03bb n \u21d2 `\u03bb s \u21d2 `\u03bb z \u21d2 ` m \u00b7 ` s \u00b7 (` n \u00b7 ` s \u00b7 ` z)\n\n\u22a2plusCh : \u03b5 \u22a2 plusCh \u2982 Ch \u21d2 Ch \u21d2 Ch\n\u22a2plusCh = (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2m \u00b7 Ax \u22a2s \u00b7 (Ax \u22a2n \u00b7 Ax \u22a2s \u00b7 Ax \u22a2z))))))\n where\n \u22a2m = S m\u2262z (S m\u2262s (S m\u2262n Z))\n \u22a2n = S n\u2262z (S n\u2262s Z)\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nfromCh : Term\nfromCh = `\u03bb m \u21d2 ` m \u00b7 (`\u03bb s \u21d2 `suc ` s) \u00b7 `zero\n\n\u22a2fromCh : \u03b5 \u22a2 fromCh \u2982 Ch \u21d2 `\u2115\n\u22a2fromCh = (\u22a2\u03bb (Ax \u22a2m \u00b7 (\u22a2\u03bb (\u22a2suc (Ax \u22a2s))) \u00b7 \u22a2zero))\n where\n \u22a2m = Z\n \u22a2s = Z\n\nfourCh : Term\nfourCh = fromCh \u00b7 (plusCh \u00b7 twoCh \u00b7 twoCh)\n\n\u22a2fourCh : \u03b5 \u22a2 fourCh \u2982 `\u2115\n\u22a2fourCh = \u22a2fromCh \u00b7 (\u22a2plusCh \u00b7 \u22a2twoCh \u00b7 \u22a2twoCh)\n\\end{code}\n\n\n## Erasure\n\n\\begin{code}\nlookup : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x \u2982 A \u2192 Id\nlookup {\u0393 , x \u2982 A} Z = x\nlookup {\u0393 , x \u2982 A} (S _ \u22a2w) = lookup {\u0393} \u22a2w\n\nerase : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2982 A \u2192 Term\nerase (Ax \u22a2w) = ` lookup \u22a2w\nerase (\u22a2\u03bb {x = x} \u22a2N) = `\u03bb x \u21d2 erase \u22a2N\nerase (\u22a2L \u00b7 \u22a2M) = erase \u22a2L \u00b7 erase \u22a2M\nerase (\u22a2zero) = `zero\nerase (\u22a2suc \u22a2M) = `suc (erase \u22a2M)\nerase (\u22a2pred \u22a2M) = `pred (erase \u22a2M)\nerase (\u22a2if0 \u22a2L \u22a2M \u22a2N) = `if0 (erase \u22a2L) then (erase \u22a2M) else (erase \u22a2N)\nerase (\u22a2Y \u22a2M) = `Y (erase \u22a2M)\n\\end{code}\n\n### Properties of erasure\n\n\\begin{code}\ncong\u2083 : \u2200 {A B C D : Set} (f : A \u2192 B \u2192 C \u2192 D) {s t u v x y} \u2192 \n s \u2261 t \u2192 u \u2261 v \u2192 x \u2261 y \u2192 f s u x \u2261 f t v y\ncong\u2083 f refl refl refl = refl\n\nlookup-lemma : \u2200 {\u0393 x A} \u2192 (\u22a2x : \u0393 \u220b x \u2982 A) \u2192 lookup \u22a2x \u2261 x\nlookup-lemma Z = refl\nlookup-lemma (S _ \u22a2w) = lookup-lemma \u22a2w\n\nerase-lemma : \u2200 {\u0393 M A} \u2192 (\u22a2M : \u0393 \u22a2 M \u2982 A) \u2192 erase \u22a2M \u2261 M\nerase-lemma (Ax \u22a2x) = cong `_ (lookup-lemma \u22a2x)\nerase-lemma (\u22a2\u03bb {x = x} \u22a2N) = cong (`\u03bb x \u21d2_) (erase-lemma \u22a2N)\nerase-lemma (\u22a2L \u00b7 \u22a2M) = cong\u2082 _\u00b7_ (erase-lemma \u22a2L) (erase-lemma \u22a2M)\nerase-lemma (\u22a2zero) = refl\nerase-lemma (\u22a2suc \u22a2M) = cong `suc_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2pred \u22a2M) = cong `pred_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) = cong\u2083 `if0_then_else_\n (erase-lemma \u22a2L) (erase-lemma \u22a2M) (erase-lemma \u22a2N)\nerase-lemma (\u22a2Y \u22a2M) = cong `Y_ (erase-lemma \u22a2M)\n\\end{code}\n\n\n## Substitution\n\n### Lists as sets\n\n\\begin{code}\nopen Collections.CollectionDec (Id) (_\u225f_)\n\\end{code}\n\n### Free variables\n\n\\begin{code}\nfree : Term \u2192 List Id\nfree (` x) = [ x ]\nfree (`\u03bb x \u21d2 N) = free N \\\\ x\nfree (L \u00b7 M) = free L ++ free M\nfree (`zero) = []\nfree (`suc M) = free M\nfree (`pred M) = free M\nfree (`if0 L then M else N) = free L ++ free M ++ free N\nfree (`Y M) = free M\n\\end{code}\n\n### Fresh identifier\n\n\\begin{code}\nbump : String \u2192 Id \u2192 \u2115\nbump s (id t n) with s String.\u225f t\n... | yes refl = suc n\n... | no _ = 0\n\nnext : String \u2192 List Id \u2192 \u2115\nnext s = foldr _\u2294_ 0 \u2218 map (bump s)\n\n\u2294-lemma : \u2200 {s w xs} \u2192 w \u2208 xs \u2192 bump s w \u2264 next s xs\n\u2294-lemma {s} {_} {_ \u2237 xs} here = m\u2264m\u2294n _ (next s xs)\n\u2294-lemma {s} {_} {_ \u2237 xs} (there x\u2208) = \u2264-trans (\u2294-lemma x\u2208) (n\u2264m\u2294n _ (next s xs))\n\nfresh : Id \u2192 List Id \u2192 Id\nfresh (id s _) xs = id s (next s xs)\n\nfresh-lemma : \u2200 {w x xs} \u2192 w \u2208 xs \u2192 w \u2262 fresh x xs\nfresh-lemma {w @ (id t n)} {x @ (id s _)} {xs} w\u2208 w\u2262fr = {! (\u2294-lemma {s} {w} {xs} w\u2208)!} -- with s String.\u225f t\n{-\n... | yes refl = {! (\u2294-lemma {s} {w} {xs} w\u2208)!}\n... | no s\u2262t = {!!}\n\n\nwith s String.\u225f t | fresh x xs\n... | yes refl | fr = {! (\u2294-lemma {s} {w} {xs} w\u2208)!}\n... | no s\u2262t | _ = s\u2262t refl \n\n\nnext-lemma : \u2200 {x xs} \u2192 x \u2208 xs \u2192 x \u2262 next xs\nnext-lemma x\u2208 refl = 1+n\u2270n (\u2294-lemma x\u2208)\n\\end{code}\n\n### Identifier maps\n\n\\begin{code}\n\u2205 : Id \u2192 Term\n\u2205 x = ` x\n\ninfixl 5 _,_\u21a6_\n\n_,_\u21a6_ : (Id \u2192 Term) \u2192 Id \u2192 Term \u2192 (Id \u2192 Term)\n(\u03c1 , x \u21a6 M) w with w \u225f x\n... | yes _ = M\n... | no _ = \u03c1 w\n\\end{code}\n\n### Substitution\n\n\\begin{code}\nsubst : List Id \u2192 (Id \u2192 Term) \u2192 Term \u2192 Term\nsubst ys \u03c1 (` x) = \u03c1 x\nsubst ys \u03c1 (`\u03bb x \u21d2 N) = `\u03bb y \u21d2 subst (y \u2237 ys) (\u03c1 , x \u21a6 ` y) N\n where\n y = fresh ys\nsubst ys \u03c1 (L \u00b7 M) = subst ys \u03c1 L \u00b7 subst ys \u03c1 M\nsubst ys \u03c1 (`zero) = `zero\nsubst ys \u03c1 (`suc M) = `suc (subst ys \u03c1 M)\nsubst ys \u03c1 (`pred M) = `pred (subst ys \u03c1 M)\nsubst ys \u03c1 (`if0 L then M else N)\n = `if0 (subst ys \u03c1 L) then (subst ys \u03c1 M) else (subst ys \u03c1 N)\nsubst ys \u03c1 (`Y M) = `Y (subst ys \u03c1 M) \n \n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\nN [ x := M ] = subst (free M ++ (free N \\\\ x)) (\u2205 , x \u21a6 M) N\n\\end{code}\n\n### Testing substitution\n\n\\begin{code}\n_ : (` s \u00b7 ` s \u00b7 ` z) [ z := `zero ] \u2261 (` s \u00b7 ` s \u00b7 `zero)\n_ = refl\n\n_ : (` s \u00b7 ` s \u00b7 ` z) [ s := (`\u03bb m \u21d2 `suc ` m) ] [ z := `zero ] \n \u2261 ((`\u03bb p \u21d2 `suc ` p) \u00b7 (`\u03bb p \u21d2 `suc ` p) \u00b7 `zero)\n_ = refl\n\n_ : (`\u03bb m \u21d2 ` m \u00b7 ` n) [ n := ` m ] \u2261 (`\u03bb n \u21d2 ` n \u00b7 ` m)\n_ = refl\n\n_ : subst [ m , n ] (\u2205 , m \u21a6 ` n , n \u21a6 ` m) (` m \u00b7 ` n) \u2261 (` n \u00b7 ` m)\n_ = refl\n\\end{code}\n\n\n## Values\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n\n Zero :\n ----------\n Value `zero\n\n Suc : \u2200 {V}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n \n Fun : \u2200 {x N}\n ---------------\n \u2192 Value (`\u03bb x \u21d2 N)\n\\end{code}\n\n## Reduction\n\n\\begin{code}\ninfix 4 _\u27f6_\n\ndata _\u27f6_ : Term \u2192 Term \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\n \u2192 L \u27f6 L\u2032\n ----------------\n \u2192 L \u00b7 M \u27f6 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\n \u2192 Value V\n \u2192 M \u27f6 M\u2032\n ----------------\n \u2192 V \u00b7 M \u27f6 V \u00b7 M\u2032\n\n \u03b2-\u21d2 : \u2200 {x N V}\n \u2192 Value V\n ------------------------------\n \u2192 (`\u03bb x \u21d2 N) \u00b7 V \u27f6 N [ x := V ]\n\n \u03be-suc : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n ------------------\n \u2192 `suc M \u27f6 `suc M\u2032\n\n \u03be-pred : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n --------------------\n \u2192 `pred M \u27f6 `pred M\u2032\n\n \u03b2-pred-zero :\n ---------------------\n `pred `zero \u27f6 `zero\n\n \u03b2-pred-suc : \u2200 {V}\n \u2192 Value V\n --------------------\n \u2192 `pred (`suc V) \u27f6 V\n\n \u03be-if0 : \u2200 {L L\u2032 M N}\n \u2192 L \u27f6 L\u2032\n ----------------------------------------------\n \u2192 `if0 L then M else N \u27f6 `if0 L\u2032 then M else N\n\n \u03b2-if0-zero : \u2200 {M N}\n ------------------------------\n \u2192 `if0 `zero then M else N \u27f6 M\n \n \u03b2-if0-suc : \u2200 {V M N}\n \u2192 Value V\n ---------------------------------\n \u2192 `if0 (`suc V) then M else N \u27f6 N\n\n \u03be-Y : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n --------------\n \u2192 `Y M \u27f6 `Y M\u2032\n\n \u03b2-Y : \u2200 {V x N}\n \u2192 Value V\n \u2192 V \u2261 `\u03bb x \u21d2 N\n ------------------------\n \u2192 `Y V \u27f6 N [ x := `Y V ]\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\ninfix 2 _\u27f6*_\ninfix 1 begin_\ninfixr 2 _\u27f6\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u27f6*_ : Term \u2192 Term \u2192 Set where\n\n _\u220e : \u2200 {M}\n -------------\n \u2192 M \u27f6* M\n\n _\u27f6\u27e8_\u27e9_ : \u2200 (L : Term) {M N}\n \u2192 L \u27f6 M\n \u2192 M \u27f6* N\n ---------\n \u2192 L \u27f6* N\n\nbegin_ : \u2200 {M N} \u2192 (M \u27f6* N) \u2192 (M \u27f6* N)\nbegin M\u27f6*N = M\u27f6*N\n\\end{code}\n\n## Canonical forms\n\n\\begin{code}\ndata Canonical : Term \u2192 Type \u2192 Set where\n\n Zero : \n ------------------\n Canonical `zero `\u2115\n\n Suc : \u2200 {V}\n \u2192 Canonical V `\u2115\n ---------------------\n \u2192 Canonical (`suc V) `\u2115\n \n Fun : \u2200 {x N A B}\n \u2192 \u03b5 , x \u2982 A \u22a2 N \u2982 B\n ------------------------------\n \u2192 Canonical (`\u03bb x \u21d2 N) (A \u21d2 B)\n\\end{code}\n\n## Canonical forms lemma\n\nEvery typed value is canonical.\n\n\\begin{code}\ncanonical : \u2200 {V A}\n \u2192 \u03b5 \u22a2 V \u2982 A\n \u2192 Value V\n -------------\n \u2192 Canonical V A\ncanonical \u22a2zero Zero = Zero\ncanonical (\u22a2suc \u22a2V) (Suc VV) = Suc (canonical \u22a2V VV)\ncanonical (\u22a2\u03bb \u22a2N) Fun = Fun \u22a2N\n\\end{code}\n\nEvery canonical form has a type and a value.\n\n\\begin{code}\ntype : \u2200 {V A}\n \u2192 Canonical V A\n -------------\n \u2192 \u03b5 \u22a2 V \u2982 A\ntype Zero = \u22a2zero\ntype (Suc CV) = \u22a2suc (type CV)\ntype (Fun \u22a2N) = \u22a2\u03bb \u22a2N\n\nvalue : \u2200 {V A}\n \u2192 Canonical V A\n -------------\n \u2192 Value V\nvalue Zero = Zero\nvalue (Suc CV) = Suc (value CV)\nvalue (Fun \u22a2N) = Fun\n\\end{code}\n \n## Progress\n\n\\begin{code}\ndata Progress (M : Term) (A : Type) : Set where\n step : \u2200 {N}\n \u2192 M \u27f6 N\n ----------\n \u2192 Progress M A\n done :\n Canonical M A\n -------------\n \u2192 Progress M A\n\nprogress : \u2200 {M A} \u2192 \u03b5 \u22a2 M \u2982 A \u2192 Progress M A\nprogress (Ax ())\nprogress (\u22a2\u03bb \u22a2N) = done (Fun \u22a2N)\nprogress (\u22a2L \u00b7 \u22a2M) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-\u00b7\u2081 L\u27f6L\u2032)\n... | done (Fun _) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-\u00b7\u2082 Fun M\u27f6M\u2032)\n... | done CM = step (\u03b2-\u21d2 (value CM))\nprogress \u22a2zero = done Zero\nprogress (\u22a2suc \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-suc M\u27f6M\u2032)\n... | done CM = done (Suc CM)\nprogress (\u22a2pred \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-pred M\u27f6M\u2032)\n... | done Zero = step \u03b2-pred-zero\n... | done (Suc CM) = step (\u03b2-pred-suc (value CM))\nprogress (\u22a2if0 \u22a2L \u22a2M \u22a2N) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-if0 L\u27f6L\u2032)\n... | done Zero = step \u03b2-if0-zero\n... | done (Suc CM) = step (\u03b2-if0-suc (value CM))\nprogress (\u22a2Y \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-Y M\u27f6M\u2032)\n... | done (Fun _) = step (\u03b2-Y Fun refl)\n\\end{code}\n\n\n## Preservation\n\n### Domain of an environment\n\n\\begin{code}\ndom : Env \u2192 List Id\ndom \u03b5 = []\ndom (\u0393 , x \u2982 A) = x \u2237 dom \u0393\n\ndom-lemma : \u2200 {\u0393 y B} \u2192 \u0393 \u220b y \u2982 B \u2192 y \u2208 dom \u0393\ndom-lemma Z = here\ndom-lemma (S x\u2262y \u22a2y) = there (dom-lemma \u22a2y)\n\nfree-lemma : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2982 A \u2192 free M \u2286 dom \u0393\nfree-lemma (Ax \u22a2x) w\u2208 with w\u2208\n... | here = dom-lemma \u22a2x\n... | there () \nfree-lemma {\u0393} (\u22a2\u03bb {N = N} \u22a2N) = \u2237-to-\\\\ (free-lemma \u22a2N)\nfree-lemma (\u22a2L \u00b7 \u22a2M) w\u2208 with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208M = free-lemma \u22a2M \u2208M\nfree-lemma \u22a2zero ()\nfree-lemma (\u22a2suc \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2pred \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) w\u2208\n with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208MN with ++-to-\u228e \u2208MN\n... | inj\u2081 \u2208M = free-lemma \u22a2M \u2208M\n... | inj\u2082 \u2208N = free-lemma \u22a2N \u2208N\nfree-lemma (\u22a2Y \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208 \n\\end{code}\n\n### Renaming\n\n\\begin{code}\n\u22a2rename : \u2200 {\u0393 \u0394 xs}\n \u2192 (\u2200 {x A} \u2192 x \u2208 xs \u2192 \u0393 \u220b x \u2982 A \u2192 \u0394 \u220b x \u2982 A)\n --------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 free M \u2286 xs \u2192 \u0393 \u22a2 M \u2982 A \u2192 \u0394 \u22a2 M \u2982 A)\n\u22a2rename \u22a2\u03c3 \u2286xs (Ax \u22a2x) = Ax (\u22a2\u03c3 \u2208xs \u22a2x)\n where\n \u2208xs = \u2286xs here\n\u22a2rename {\u0393} {\u0394} {xs} \u22a2\u03c3 \u2286xs (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb (\u22a2rename {\u0393\u2032} {\u0394\u2032} {xs\u2032} \u22a2\u03c3\u2032 \u2286xs\u2032 \u22a2N)\n where\n \u0393\u2032 = \u0393 , x \u2982 A\n \u0394\u2032 = \u0394 , x \u2982 A\n xs\u2032 = x \u2237 xs\n\n \u22a2\u03c3\u2032 : \u2200 {w B} \u2192 w \u2208 xs\u2032 \u2192 \u0393\u2032 \u220b w \u2982 B \u2192 \u0394\u2032 \u220b w \u2982 B\n \u22a2\u03c3\u2032 w\u2208\u2032 Z = Z\n \u22a2\u03c3\u2032 w\u2208\u2032 (S w\u2262 \u22a2w) = S w\u2262 (\u22a2\u03c3 \u2208w \u22a2w)\n where\n \u2208w = there\u207b\u00b9 w\u2208\u2032 w\u2262\n\n \u2286xs\u2032 : free N \u2286 xs\u2032\n \u2286xs\u2032 = \\\\-to-\u2237 \u2286xs\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2L \u00b7 \u22a2M) = \u22a2rename \u22a2\u03c3 L\u2286 \u22a2L \u00b7 \u22a2rename \u22a2\u03c3 M\u2286 \u22a2M\n where\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\n M\u2286 = trans-\u2286 \u2286-++\u2082 \u2286xs\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2zero) = \u22a2zero\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2suc \u22a2M) = \u22a2suc (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2pred \u22a2M) = \u22a2pred (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2if0 {L = L} \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2rename \u22a2\u03c3 L\u2286 \u22a2L) (\u22a2rename \u22a2\u03c3 M\u2286 \u22a2M) (\u22a2rename \u22a2\u03c3 N\u2286 \u22a2N)\n where\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\n M\u2286 = trans-\u2286 \u2286-++\u2081 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\n N\u2286 = trans-\u2286 \u2286-++\u2082 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2Y \u22a2M) = \u22a2Y (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\n \n\\end{code}\n\n\n### Substitution preserves types\n\n\\begin{code}\n\u22a2subst : \u2200 {\u0393 \u0394 xs ys \u03c1}\n \u2192 (\u2200 {x} \u2192 x \u2208 xs \u2192 free (\u03c1 x) \u2286 ys)\n \u2192 (\u2200 {x A} \u2192 x \u2208 xs \u2192 \u0393 \u220b x \u2982 A \u2192 \u0394 \u22a2 \u03c1 x \u2982 A)\n -------------------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 free M \u2286 xs \u2192 \u0393 \u22a2 M \u2982 A \u2192 \u0394 \u22a2 subst ys \u03c1 M \u2982 A)\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (Ax \u22a2x)\n = \u22a2\u03c1 (\u2286xs here) \u22a2x\n\u22a2subst {\u0393} {\u0394} {xs} {ys} {\u03c1} \u03a3 \u22a2\u03c1 \u2286xs (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb {x = y} {A = A} (\u22a2subst {\u0393\u2032} {\u0394\u2032} {xs\u2032} {ys\u2032} {\u03c1\u2032} \u03a3\u2032 \u22a2\u03c1\u2032 \u2286xs\u2032 \u22a2N)\n where\n y = fresh ys\n \u0393\u2032 = \u0393 , x \u2982 A\n \u0394\u2032 = \u0394 , y \u2982 A\n xs\u2032 = x \u2237 xs\n ys\u2032 = y \u2237 ys\n \u03c1\u2032 = \u03c1 , x \u21a6 ` y\n\n \u03a3\u2032 : \u2200 {w} \u2192 w \u2208 xs\u2032 \u2192 free (\u03c1\u2032 w) \u2286 ys\u2032\n \u03a3\u2032 {w} w\u2208\u2032 with w \u225f x\n ... | yes refl = \u2286-++\u2081\n ... | no w\u2262 = \u2286-++\u2082 \u2218 \u03a3 (there\u207b\u00b9 w\u2208\u2032 w\u2262)\n \n \u2286xs\u2032 : free N \u2286 xs\u2032\n \u2286xs\u2032 = \\\\-to-\u2237 \u2286xs\n\n \u22a2\u03c3 : \u2200 {w C} \u2192 w \u2208 ys \u2192 \u0394 \u220b w \u2982 C \u2192 \u0394\u2032 \u220b w \u2982 C\n \u22a2\u03c3 w\u2208 \u22a2w = S (fresh-lemma w\u2208) \u22a2w\n\n \u22a2\u03c1\u2032 : \u2200 {w C} \u2192 w \u2208 xs\u2032 \u2192 \u0393\u2032 \u220b w \u2982 C \u2192 \u0394\u2032 \u22a2 \u03c1\u2032 w \u2982 C\n \u22a2\u03c1\u2032 {w} _ Z with w \u225f x\n ... | yes _ = Ax Z\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1\u2032 {w} w\u2208\u2032 (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = \u22a2rename {\u0394} {\u0394\u2032} {ys} \u22a2\u03c3 (\u03a3 w\u2208) (\u22a2\u03c1 w\u2208 \u22a2w)\n where\n w\u2208 = there\u207b\u00b9 w\u2208\u2032 w\u2262\n\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2L \u00b7 \u22a2M)\n = \u22a2subst \u03a3 \u22a2\u03c1 L\u2286 \u22a2L \u00b7 \u22a2subst \u03a3 \u22a2\u03c1 M\u2286 \u22a2M\n where\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\n M\u2286 = trans-\u2286 \u2286-++\u2082 \u2286xs\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2zero = \u22a2zero\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2suc \u22a2M) = \u22a2suc (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M)\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2pred \u22a2M) = \u22a2pred (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M)\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2if0 {L = L} \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2subst \u03a3 \u22a2\u03c1 L\u2286 \u22a2L) (\u22a2subst \u03a3 \u22a2\u03c1 M\u2286 \u22a2M) (\u22a2subst \u03a3 \u22a2\u03c1 N\u2286 \u22a2N)\n where\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\n M\u2286 = trans-\u2286 \u2286-++\u2081 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\n N\u2286 = trans-\u2286 \u2286-++\u2082 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2Y \u22a2M) = \u22a2Y (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M) \n\n\u22a2substitution : \u2200 {\u0393 x A N B M} \u2192\n \u0393 , x \u2982 A \u22a2 N \u2982 B \u2192\n \u0393 \u22a2 M \u2982 A \u2192\n --------------------\n \u0393 \u22a2 N [ x := M ] \u2982 B\n\u22a2substitution {\u0393} {x} {A} {N} {B} {M} \u22a2N \u22a2M =\n \u22a2subst {\u0393\u2032} {\u0393} {xs} {ys} {\u03c1} \u03a3 \u22a2\u03c1 {N} {B} \u2286xs \u22a2N\n where\n \u0393\u2032 = \u0393 , x \u2982 A\n xs = free N\n ys = free M ++ (free N \\\\ x)\n \u03c1 = \u2205 , x \u21a6 M\n\n \u03a3 : \u2200 {w} \u2192 w \u2208 xs \u2192 free (\u03c1 w) \u2286 ys\n \u03a3 {w} w\u2208 y\u2208 with w \u225f x\n ... | yes _ = \u2286-++\u2081 y\u2208\n ... | no w\u2262 rewrite \u2208-[_] y\u2208 = \u2286-++\u2082 (\u2208-\u2262-to-\\\\ w\u2208 w\u2262)\n \n \u22a2\u03c1 : \u2200 {w B} \u2192 w \u2208 xs \u2192 \u0393\u2032 \u220b w \u2982 B \u2192 \u0393 \u22a2 \u03c1 w \u2982 B\n \u22a2\u03c1 {w} w\u2208 Z with w \u225f x\n ... | yes _ = \u22a2M\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1 {w} w\u2208 (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = Ax \u22a2w\n\n \u2286xs : free N \u2286 xs\n \u2286xs x\u2208 = x\u2208\n\\end{code}\n\n### Preservation\n\n\\begin{code}\npreservation : \u2200 {\u0393 M N A}\n \u2192 \u0393 \u22a2 M \u2982 A\n \u2192 M \u27f6 N\n ---------\n \u2192 \u0393 \u22a2 N \u2982 A\npreservation (Ax \u22a2x) ()\npreservation (\u22a2\u03bb \u22a2N) ()\npreservation (\u22a2L \u00b7 \u22a2M) (\u03be-\u00b7\u2081 L\u27f6) = preservation \u22a2L L\u27f6 \u00b7 \u22a2M\npreservation (\u22a2V \u00b7 \u22a2M) (\u03be-\u00b7\u2082 _ M\u27f6) = \u22a2V \u00b7 preservation \u22a2M M\u27f6\npreservation ((\u22a2\u03bb \u22a2N) \u00b7 \u22a2W) (\u03b2-\u21d2 _) = \u22a2substitution \u22a2N \u22a2W\npreservation (\u22a2zero) ()\npreservation (\u22a2suc \u22a2M) (\u03be-suc M\u27f6) = \u22a2suc (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2M) (\u03be-pred M\u27f6) = \u22a2pred (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2zero) (\u03b2-pred-zero) = \u22a2zero\npreservation (\u22a2pred (\u22a2suc \u22a2M)) (\u03b2-pred-suc _) = \u22a2M\npreservation (\u22a2if0 \u22a2L \u22a2M \u22a2N) (\u03be-if0 L\u27f6) = \u22a2if0 (preservation \u22a2L L\u27f6) \u22a2M \u22a2N\npreservation (\u22a2if0 \u22a2zero \u22a2M \u22a2N) \u03b2-if0-zero = \u22a2M\npreservation (\u22a2if0 (\u22a2suc \u22a2V) \u22a2M \u22a2N) (\u03b2-if0-suc _) = \u22a2N\npreservation (\u22a2Y \u22a2M) (\u03be-Y M\u27f6) = \u22a2Y (preservation \u22a2M M\u27f6)\npreservation (\u22a2Y (\u22a2\u03bb \u22a2N)) (\u03b2-Y _ refl) = \u22a2substitution \u22a2N (\u22a2Y (\u22a2\u03bb \u22a2N))\n-}\n\\end{code}\n\n\n","avg_line_length":24.8124207858,"max_line_length":111,"alphanum_fraction":0.3980691628} +{"size":1224,"ext":"lagda","lang":"Literate Agda","max_stars_count":1299.0,"content":"\\begin{code}\nmodule Declarative.Examples.StdLib.ChurchNat where\n\\end{code}\n\n\\begin{code}\nopen import Utils\nopen import Type\nopen import Declarative\nopen import Builtin\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\n\nopen import Data.Unit\n\\end{code}\n\n\\begin{code}\n-- all (r :: *). r -> (r -> r) -> r\nN : \u2200{\u03a6} \u2192 \u03a6 \u22a2\u22c6 *\nN = \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 ` Z)\n\n-- \/\\(r :: *) -> \\(z : r) (f : r -> r) -> z\nZero : \u2205 \u22a2 N\nZero = \u039b (\u019b (\u019b (` (S Z))))\n\n-- \\(n : nat) -> \/\\(r :: *) -> \\(z : r) (f : r -> r) -> f (n {r} z f)\nSucc : \u2205 \u22a2 N \u21d2 N\nSucc = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (` Z) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n\nIter : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 N \u21d2 (` Z))\nIter = \u039b (\u019b (\u019b (\u019b ((` Z) \u00b7\u22c6 (` Z) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\nopen import Builtin.Constant.Type\nopen import Data.Integer\nopen import Data.Nat\nopen import Agda.Builtin.Sigma renaming (_,_ to _,,_)\n\ncon0 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon0 = con (integer (\u2124.pos 0))\n\ncon1 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon1 = con (integer (\u2124.pos 1))\n\ninc : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer \u21d2 con integer\ninc = \u019b (ibuiltin addInteger \u00b7 con1 \u00b7 ` Z)\n\nNat2Int : \u2205 \u22a2 N \u21d2 con integer\nNat2Int = \u019b (Iter\n \u00b7\u22c6 con integer\n \u00b7 con0\n \u00b7 inc\n \u00b7 ` Z)\n\\end{code}\n","avg_line_length":23.5384615385,"max_line_length":77,"alphanum_fraction":0.5057189542} +{"size":1772,"ext":"lagda","lang":"Literate Agda","max_stars_count":12.0,"content":"\\begin{code}\nmodule Declarative.Evaluation where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nopen import Declarative.Term\nopen import Declarative.Term.Reduction\n\\end{code}\n\n## Evaluation\n\nTransitive closure of reduction\n\\begin{code}\ndata _\u2014\u21a0_ {J \u0393}{A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} (L : \u0393 \u22a2 A) : (\u0393 \u22a2 A) \u2192 Set where\n done : L \u2014\u21a0 L\n continue : \u2200 {M N} \u2192 L \u2014\u2192 M \u2192 M \u2014\u21a0 N \u2192 L \u2014\u21a0 N \n\\end{code}\n\nAs previously, gas is specified by a natural number.\n\\begin{code}\nopen import Data.Nat\ndata Gas : Set where\n gas : \u2115 \u2192 Gas\n\\end{code}\nWhen our evaluator returns a term `N`, it will either give evidence that\n`N` is a value or indicate that it ran out of gas.\n\\begin{code}\ndata Finished {\u0393 J}{A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} : (N : \u0393 \u22a2 A) \u2192 Set where\n\n done : \u2200 N \u2192 \n Value N\n ----------\n \u2192 Finished N\n\n out-of-gas : \u2200{N} \u2192 \n ----------\n Finished N\n\\end{code}\nGiven a term `L` of type `A`, the evaluator will, for some `N`, return\na reduction sequence from `L` to `N` and an indication of whether\nreduction finished.\n\\begin{code}\ndata Steps : \u2200 {J}{A : \u2205 \u22a2\u22c6 J} \u2192 \u2205 \u22a2 A \u2192 Set where\n\n steps : \u2200 {J}{A : \u2205 \u22a2\u22c6 J} {L N : \u2205 \u22a2 A}\n \u2192 L \u2014\u21a0 N\n \u2192 Finished N\n ----------\n \u2192 Steps L\n\n error : \u2200 {J}{A : \u2205 \u22a2\u22c6 J} {L : \u2205 \u22a2 A} \u2192 Steps L\n\\end{code}\nThe evaluator takes gas and a term and returns the corresponding steps.\n\\begin{code}\neval : \u2200 {A : \u2205 \u22a2\u22c6 *}\n \u2192 Gas\n \u2192 (M : \u2205 \u22a2 A)\n -----------\n \u2192 Steps M\neval (gas zero) M = steps done out-of-gas\neval (gas (suc n)) M with progress M\n... | error = error\neval (gas (suc n)) M | step {N} p with eval (gas n) N\n... | error = error\neval (gas (suc n)) M | step {N} p | steps ps q = steps (continue p ps) q\neval (gas (suc n)) M | done vM = steps done (done _ vM)\n\\end{code}\n","avg_line_length":24.9577464789,"max_line_length":72,"alphanum_fraction":0.5558690745} +{"size":8144,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Algorithmic.Completeness where\n\nopen import Type\nopen import Type.Equality\nopen import Type.RenamingSubstitution\nimport Declarative as Syn\nimport Algorithmic as Norm\nopen import Type.BetaNormal\nopen import Type.BetaNormal.Equality\nopen import Type.BetaNBE\nopen import Type.BetaNBE.Completeness\nopen import Type.BetaNBE.RenamingSubstitution\n\nopen import Relation.Binary.PropositionalEquality renaming (subst to substEq) hiding ([_])\nopen import Function\nopen import Data.Vec hiding ([_];length)\nopen import Data.Sum\n\nnfCtx : \u2200 {\u03a6} \u2192 Syn.Ctx \u03a6 \u2192 Norm.Ctx \u03a6\nnfCtx Syn.\u2205 = Norm.\u2205\nnfCtx (\u0393 Syn.,\u22c6 K) = nfCtx \u0393 Norm.,\u22c6 K\nnfCtx (\u0393 Syn., A) = nfCtx \u0393 Norm., nf A\n\nnfTyVar : \u2200{\u03a6 \u0393}\n \u2192 {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 Syn.\u220b A\n \u2192 nfCtx \u0393 Norm.\u220b nf A\nnfTyVar Syn.Z = Norm.Z\nnfTyVar (Syn.S \u03b1) = Norm.S (nfTyVar \u03b1)\nnfTyVar (Syn.T {A = A} \u03b1) = Norm.conv\u220b refl (ren-nf S A) (Norm.T (nfTyVar \u03b1))\n\nopen import Type.BetaNormal.Equality\n\nlem\u03a0 : \u2200{\u0393 K }(B : \u0393 ,\u22c6 K \u22a2\u22c6 *) \u2192\n nf (\u03a0 B) \u2261 \u03a0 (nf B)\nlem\u03a0 B = cong \u03a0 (sym (subNf-lemma' B))\n\nopen import Type.BetaNBE.Soundness\n\nstability-\u03bc : \u2200{\u03a6 K}(A : \u03a6 \u22a2\u22c6 _)(B : \u03a6 \u22a2\u22c6 K) \u2192\n nf (A \u00b7 \u019b (\u03bc (weaken A) (` Z)) \u00b7 B)\n \u2261\n nf (embNf (nf A) \u00b7 \u019b (\u03bc (embNf (weakenNf (nf A))) (` Z)) \u00b7 embNf (nf B))\nstability-\u03bc A B = completeness\n (\u00b7\u2261\u03b2\n (\u00b7\u2261\u03b2\n (soundness A)\n (\u019b\u2261\u03b2 (\u03bc\u2261\u03b2 (trans\u2261\u03b2\n (soundness (ren S A))\n (\u22612\u03b2 (sym (cong embNf (ren-nf S A))))) (refl\u2261\u03b2 (` Z)))))\n (soundness B))\n\nopen import Type.BetaNBE.Completeness\nopen import Type.BetaNBE.Soundness\nopen import Type.BetaNBE.Stability\n\n\nlem[] : \u2200{\u0393 K}(A : \u0393 \u22a2\u22c6 K)(B : \u0393 ,\u22c6 K \u22a2\u22c6 *) \u2192\n (nf B [ nf A ]Nf) \u2261 nf (B [ A ])\nlem[] A B = trans\n (sub-eval (embNf (nf B)) idCR (embNf \u2218 subNf-cons (ne \u2218 `) (nf A)))\n (trans\n (fund\n (\u03bb {Z \u2192 symCR (fund idCR (soundness A)) ; (S \u03b1) \u2192 idCR _})\n (sym\u2261\u03b2 (soundness B)))\n (sym (sub-eval B idCR (sub-cons ` A))))\n\nimport Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2\u22c6_ ` con\n as SSig\nimport Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con\n as NSig\nopen import Builtin hiding (length)\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con as STermCon\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con as NTermCon\n\n\nnfTypeTC : \u2200{\u03c6}{A : \u03c6 \u22a2\u22c6 *} \u2192 STermCon.TermCon A \u2192 NTermCon.TermCon (nf A)\nnfTypeTC (STermCon.integer i) = NTermCon.integer i\nnfTypeTC (STermCon.bytestring b) = NTermCon.bytestring b\nnfTypeTC (STermCon.string s) = NTermCon.string s\nnfTypeTC (STermCon.bool b) = NTermCon.bool b\nnfTypeTC (STermCon.char c) = NTermCon.char c\nnfTypeTC STermCon.unit = NTermCon.unit\n\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List\n\nnfTypeSIG\u2261\u2081 : (bn : Builtin) \u2192 proj\u2081 (SSig.SIG bn) \u2261 proj\u2081 (NSig.SIG bn)\nnfTypeSIG\u2261\u2081 addInteger = refl\nnfTypeSIG\u2261\u2081 subtractInteger = refl\nnfTypeSIG\u2261\u2081 multiplyInteger = refl\nnfTypeSIG\u2261\u2081 divideInteger = refl\nnfTypeSIG\u2261\u2081 quotientInteger = refl\nnfTypeSIG\u2261\u2081 remainderInteger = refl\nnfTypeSIG\u2261\u2081 modInteger = refl\nnfTypeSIG\u2261\u2081 lessThanInteger = refl\nnfTypeSIG\u2261\u2081 lessThanEqualsInteger = refl\nnfTypeSIG\u2261\u2081 equalsInteger = refl\nnfTypeSIG\u2261\u2081 concatenate = refl\nnfTypeSIG\u2261\u2081 takeByteString = refl\nnfTypeSIG\u2261\u2081 dropByteString = refl\nnfTypeSIG\u2261\u2081 lessThanByteString = refl\nnfTypeSIG\u2261\u2081 greaterThanByteString = refl\nnfTypeSIG\u2261\u2081 sha2-256 = refl\nnfTypeSIG\u2261\u2081 sha3-256 = refl\nnfTypeSIG\u2261\u2081 verifySignature = refl\nnfTypeSIG\u2261\u2081 equalsByteString = refl\nnfTypeSIG\u2261\u2081 ifThenElse = refl\nnfTypeSIG\u2261\u2081 charToString = refl\nnfTypeSIG\u2261\u2081 append = refl\nnfTypeSIG\u2261\u2081 trace = refl\n\nlem\u03c3 : \u2200{\u0393 \u0394 \u0394'}\n \u2192 (\u03c3 : Sub \u0394 \u0393)\n \u2192 (C : \u0394 \u22a2\u22c6 *)\n \u2192 (C' : \u0394' \u22a2Nf\u22c6 *)\n \u2192 (q : \u0394' \u2261 \u0394)\n \u2192 nf C \u2261 substEq (_\u22a2Nf\u22c6 *) q C' \u2192\n subNf\n (\u03bb {J} \u03b1 \u2192\n nf\n (\u03c3 (substEq (_\u220b\u22c6 J) q \u03b1)))\n C'\n \u2261\n nf (sub \u03c3 C)\nlem\u03c3 \u03c3 C _ refl q = trans\n (subNf-cong' (nf \u2218 \u03c3) (sym q))\n (trans\n (trans\n (sub-eval (embNf (nf C)) idCR (embNf \u2218 nf \u2218 \u03c3))\n (fund (\u03bb \u03b1 \u2192 fund idCR (sym\u2261\u03b2 (soundness (\u03c3 \u03b1)))) (sym\u2261\u03b2 (soundness C))))\n (sym (sub-eval C idCR \u03c3)))\n\n-- this should be a lemma in NBE\/RenSubst\n-- subNf (nf \u2218 \u03c3) (nf C) \u2261 nf (sub \u03c3 C)\n\nnfTypeSIG\u2261\u2082 : (bn : Builtin) \u2192\n nf (proj\u2082 (proj\u2082 (SSig.SIG bn))) \u2261\n substEq (_\u22a2Nf\u22c6 *) (sym (nfTypeSIG\u2261\u2081 bn))\n (proj\u2082 (proj\u2082 (NSig.SIG bn)))\nnfTypeSIG\u2261\u2082 addInteger = refl\nnfTypeSIG\u2261\u2082 subtractInteger = refl\nnfTypeSIG\u2261\u2082 multiplyInteger = refl\nnfTypeSIG\u2261\u2082 divideInteger = refl\nnfTypeSIG\u2261\u2082 quotientInteger = refl\nnfTypeSIG\u2261\u2082 remainderInteger = refl\nnfTypeSIG\u2261\u2082 modInteger = refl\nnfTypeSIG\u2261\u2082 lessThanInteger = refl\nnfTypeSIG\u2261\u2082 lessThanEqualsInteger = refl\nnfTypeSIG\u2261\u2082 equalsInteger = refl\nnfTypeSIG\u2261\u2082 concatenate = refl\nnfTypeSIG\u2261\u2082 takeByteString = refl\nnfTypeSIG\u2261\u2082 dropByteString = refl\nnfTypeSIG\u2261\u2082 lessThanByteString = refl\nnfTypeSIG\u2261\u2082 greaterThanByteString = refl\nnfTypeSIG\u2261\u2082 sha2-256 = refl\nnfTypeSIG\u2261\u2082 sha3-256 = refl\nnfTypeSIG\u2261\u2082 verifySignature = refl\nnfTypeSIG\u2261\u2082 equalsByteString = refl\nnfTypeSIG\u2261\u2082 ifThenElse = refl\nnfTypeSIG\u2261\u2082 charToString = refl\nnfTypeSIG\u2261\u2082 append = refl\nnfTypeSIG\u2261\u2082 trace = refl\n\nnfTypeSIG\u2261\u2083 : (bn : Builtin) \u2192 length (proj\u2081 (proj\u2082 (SSig.SIG bn))) \u2261 length (proj\u2081 (proj\u2082 (NSig.SIG bn)))\nnfTypeSIG\u2261\u2083 addInteger = refl\nnfTypeSIG\u2261\u2083 subtractInteger = refl\nnfTypeSIG\u2261\u2083 multiplyInteger = refl\nnfTypeSIG\u2261\u2083 divideInteger = refl\nnfTypeSIG\u2261\u2083 quotientInteger = refl\nnfTypeSIG\u2261\u2083 remainderInteger = refl\nnfTypeSIG\u2261\u2083 modInteger = refl\nnfTypeSIG\u2261\u2083 lessThanInteger = refl\nnfTypeSIG\u2261\u2083 lessThanEqualsInteger = refl\nnfTypeSIG\u2261\u2083 equalsInteger = refl\nnfTypeSIG\u2261\u2083 concatenate = refl\nnfTypeSIG\u2261\u2083 takeByteString = refl\nnfTypeSIG\u2261\u2083 dropByteString = refl\nnfTypeSIG\u2261\u2083 lessThanByteString = refl\nnfTypeSIG\u2261\u2083 greaterThanByteString = refl\nnfTypeSIG\u2261\u2083 sha2-256 = refl\nnfTypeSIG\u2261\u2083 sha3-256 = refl\nnfTypeSIG\u2261\u2083 verifySignature = refl\nnfTypeSIG\u2261\u2083 equalsByteString = refl\nnfTypeSIG\u2261\u2083 ifThenElse = refl\nnfTypeSIG\u2261\u2083 charToString = refl\nnfTypeSIG\u2261\u2083 append = refl\nnfTypeSIG\u2261\u2083 trace = refl\n\nopen import Builtin.Constant.Type\n\nlemcon : \u2200{\u03a6 \u03a6'}(p : \u03a6 \u2261 \u03a6')(tcn : TyCon)\n \u2192 con tcn \u2261 substEq (_\u22a2Nf\u22c6 *) p (con tcn)\nlemcon refl tcn = refl\n\nsubstTC : \u2200{\u03a6 \u03a6' : Ctx\u22c6}(p : \u03a6 \u2261 \u03a6')(tcn : TyCon)\n \u2192 NTermCon.TermCon {\u03a6 = \u03a6} (con tcn)\n \u2192 NTermCon.TermCon {\u03a6 = \u03a6'}(con tcn)\nsubstTC refl tcn t = t\n\nnfList : \u2200{\u0394} \u2192 List (\u0394 \u22a2\u22c6 *) \u2192 List (\u0394 \u22a2Nf\u22c6 *)\nnfList [] = []\nnfList (A \u2237 As) = nf A \u2237 nfList As\n\nlemList : (bn : Builtin)\n \u2192 substEq (\u03bb \u03a6 \u2192 List (\u03a6 \u22a2Nf\u22c6 *)) (sym (nfTypeSIG\u2261\u2081 bn))\n (proj\u2081 (proj\u2082 (NSig.SIG bn)))\n \u2261 nfList (proj\u2081 (proj\u2082 (SSig.SIG bn)))\nlemList addInteger = refl\nlemList subtractInteger = refl\nlemList multiplyInteger = refl\nlemList divideInteger = refl\nlemList quotientInteger = refl\nlemList remainderInteger = refl\nlemList modInteger = refl\nlemList lessThanInteger = refl\nlemList lessThanEqualsInteger = refl\nlemList equalsInteger = refl\nlemList concatenate = refl\nlemList takeByteString = refl\nlemList dropByteString = refl\nlemList lessThanByteString = refl\nlemList greaterThanByteString = refl\nlemList sha2-256 = refl\nlemList sha3-256 = refl\nlemList verifySignature = refl\nlemList equalsByteString = refl\nlemList ifThenElse = refl\nlemList charToString = refl\nlemList append = refl\nlemList trace = refl\n\npostulate itype-lem : \u2200 {\u03a6} b \u2192 Norm.itype {\u03a6} b \u2261 nf (Syn.itype b)\n\nnfType : \u2200{\u03a6 \u0393}\n \u2192 {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 Syn.\u22a2 A\n \u2192 nfCtx \u0393 Norm.\u22a2 nf A\nnfType (Syn.` \u03b1) = Norm.` (nfTyVar \u03b1)\nnfType (Syn.\u019b t) = Norm.\u019b (nfType t)\nnfType (t Syn.\u00b7 u) = nfType t Norm.\u00b7 nfType u\nnfType (Syn.\u039b {B = B} t) =\n Norm.\u039b (Norm.conv\u22a2 refl (subNf-lemma' B) (nfType t))\nnfType (Syn._\u00b7\u22c6_ {B = B} t A) = Norm.conv\u22a2\n refl\n (lem[] A B)\n (Norm._\u00b7\u22c6_ (Norm.conv\u22a2 refl (lem\u03a0 B) (nfType t)) (nf A))\nnfType (Syn.wrap A B t) = Norm.wrap\n (nf A)\n (nf B)\n (Norm.conv\u22a2 refl (stability-\u03bc A B) (nfType t))\nnfType (Syn.unwrap {A = A}{B = B} t) = Norm.conv\u22a2\n refl\n (sym (stability-\u03bc A B))\n (Norm.unwrap (nfType t))\nnfType (Syn.conv p t) = Norm.conv\u22a2 refl (completeness p) (nfType t)\nnfType (Syn.con t) = Norm.con (nfTypeTC t)\nnfType (Syn.ibuiltin b) = Norm.conv\u22a2 refl (itype-lem b) (Norm.ibuiltin b)\nnfType (Syn.error A) = Norm.error (nf A)\n\ncompletenessT : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 Syn.\u22a2 A\n \u2192 nfCtx \u0393 Norm.\u22a2 nf A \u00d7 (A \u2261\u03b2 embNf (nf A))\ncompletenessT {A = A} t = nfType t ,, soundness A\n\\end{code}\n","avg_line_length":30.5018726592,"max_line_length":106,"alphanum_fraction":0.6735019646} +{"size":43308,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Lambda: Introduction to Lambda Calculus\"\nlayout : page\nprev : \/Lists\/\npermalink : \/Lambda\/\nnext : \/Properties\/\n---\n\n\\begin{code}\nmodule plfa.Lambda where\n\\end{code}\n\nThe _lambda-calculus_, first published by the logician Alonzo Church in\n1932, is a core calculus with only three syntactic constructs:\nvariables, abstraction, and application. It captures the key concept of\n_functional abstraction_, which appears in pretty much every programming\nlanguage, in the form of either functions, procedures, or methods.\nThe _simply-typed lambda calculus_ (or STLC) is a variant of the\nlambda calculus published by Church in 1940. It has the three\nconstructs above for function types, plus whatever else is required\nfor base types. Church had a minimal base type with no operations.\nWe will instead echo Plotkin's _Programmable Computable\nFunctions_ (PCF), and add operations on natural numbers and\nrecursive function definitions.\n\nThis chapter formalises the simply-typed lambda calculus, giving its\nsyntax, small-step semantics, and typing rules. The next chapter\n[Properties][plfa.Properties]\nproves its main properties, including\nprogress and preservation. Following chapters will look at a number\nof variants of lambda calculus.\n\nBe aware that the approach we take here is _not_ our recommended\napproach to formalisation. Using De Bruijn indices and\ninherently-typed terms, as we will do in\nChapter [DeBruijn][plfa.DeBruijn],\nleads to a more compact formulation. Nonetheless, we begin with named\nvariables, partly because such terms are easier to read and partly\nbecause the development is more traditional.\n\nThe development in this chapter was inspired by the corresponding\ndevelopment in Chapter _Stlc_ of _Software Foundations_ \n(_Programming Language Foundations_). We differ by\nrepresenting contexts explicitly (as lists pairing identifiers with\ntypes) rather than as partial maps (which take identifiers to types),\nwhich corresponds better to our subsequent development of DeBruijn\nnotation. We also differ by taking natural numbers as the base type\nrather than booleans, allowing more sophisticated examples. In\nparticular, we will be able to show (twice!) that two plus two is\nfour.\n\n## Imports\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; _\u2262_; refl)\nopen import Data.String using (String)\nopen import Data.String.Unsafe using (_\u225f_)\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Relation.Nullary using (Dec; yes; no; \u00ac_)\nopen import Relation.Nullary.Negation using (\u00ac?)\nopen import Data.List using (List; _\u2237_; [])\n\\end{code}\n\n## Syntax of terms\n\nTerms have seven constructs. Three are for the core lambda calculus:\n\n * Variables `` ` x ``\n * Abstractions `\u019b x \u21d2 N`\n * Applications `L \u00b7 M`\n\nThree are for the naturals:\n\n * Zero `` `zero ``\n * Successor `` `suc ``\n * Case `` case L [zero\u21d2 M |suc x \u21d2 N ] ``\n\nAnd one is for recursion:\n\n * Fixpoint `\u03bc x \u21d2 M`\n\nAbstraction is also called _lambda abstraction_, and is the construct\nfrom which the calculus takes its name.\n\nWith the exception of variables and fixpoints, each term\nform either constructs a value of a given type (abstractions yield functions,\nzero and successor yield natural numbers) or deconstructs it (applications use functions,\ncase terms use naturals). We will see this again when we come\nto the rules for assigning types to terms, where constructors\ncorrespond to introduction rules and deconstructors to eliminators.\n\nHere is the syntax of terms in BNF:\n\n L, M, N ::=\n ` x | \u019b x \u21d2 N | L \u00b7 M |\n `zero | `suc M | case L [zero\u21d2 M |suc x \u21d2 N] |\n \u03bc x \u21d2 M\n\nAnd here it is formalised in Agda:\n\\begin{code}\nId : Set\nId = String\n\ninfix 5 \u019b_\u21d2_\ninfix 5 \u03bc_\u21d2_\ninfixl 7 _\u00b7_\ninfix 8 `suc_\ninfix 9 `_\n\ndata Term : Set where\n `_ : Id \u2192 Term\n \u019b_\u21d2_ : Id \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n `zero : Term\n `suc_ : Term \u2192 Term\n case_[zero\u21d2_|suc_\u21d2_] : Term \u2192 Term \u2192 Id \u2192 Term \u2192 Term\n \u03bc_\u21d2_ : Id \u2192 Term \u2192 Term\n\\end{code}\nWe represent identifiers by strings. We choose precedence so that\nlambda abstraction and fixpoint bind least tightly, then application,\nthen successor, and tightest of all is the constructor for variables.\nCase expressions are self-bracketing.\n\n\n### Example terms\n\nHere are some example terms: the natural number two,\na function that adds naturals,\nand a term that computes two plus two:\n\\begin{code}\ntwo : Term\ntwo = `suc `suc `zero\n\nplus : Term\nplus = \u03bc \"+\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\"\n [zero\u21d2 ` \"n\"\n |suc \"m\" \u21d2 `suc (` \"+\" \u00b7 ` \"m\" \u00b7 ` \"n\") ]\n\\end{code}\nThe recursive definition of addition is similar to our original\ndefinition of `_+_` for naturals, as given in\nChapter [Naturals][plfa.Naturals#plus].\nHere the variable \"m\" is bound twice, once in a lambda abstraction and once in\nthe successor branch of the case; the first use of \"m\" refers to\nthe former and the second to the latter. Any use of \"m\" in the successor branch\nmust refer to the latter binding, and so we say that the latter binding _shadows_\nthe former. Later we will confirm that two plus two is four, in other words that\nthe term\n\n plus \u00b7 two \u00b7 two\n\nreduces to `` `suc `suc `suc `suc `zero ``.\n\nAs a second example, we use higher-order functions to represent\nnatural numbers. In particular, the number _n_ is represented by a\nfunction that accepts two arguments and applies the first _n_ times to the\nsecond. This is called the _Church representation_ of the\nnaturals. Here are some example terms: the Church numeral two, a\nfunction that adds Church numerals, a function to compute successor,\nand a term that computes two plus two:\n\\begin{code}\ntwo\u1d9c : Term\ntwo\u1d9c = \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")\n\nplus\u1d9c : Term\nplus\u1d9c = \u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2\n ` \"m\" \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\")\n\nsuc\u1d9c : Term\nsuc\u1d9c = \u019b \"n\" \u21d2 `suc (` \"n\")\n\\end{code}\nThe Church numeral for two takes two arguments `s` and `z`\nand applies `s` twice to `z`.\nAddition takes two numerals `m` and `n`, a\nfunction `s` and an argument `z`, and it uses `m` to apply `s` to the\nresult of using `n` to apply `s` to `z`; hence `s` is applied `m` plus\n`n` times to `z`, yielding the Church numeral for the sum of `m` and\n`n`. For convenience, we define a function that computes successor;\nto convert a Church numeral to the corresponding natural, we apply\nit to this function and the natural number zero.\nAgain, later we will confirm that two plus two is four,\nin other words that the term\n\n plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n\nreduces to `` `suc `suc `suc `suc `zero ``.\n\n\n#### Exercise `mul` (recommended)\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers. Your definition may use `plus` as\ndefined earlier.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `mul\u1d9c`\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers represented as Church numerals. Your\ndefinition may use `plus\u1d9c` as defined earlier (or may not\n\u2014 there are nice definitions both ways).\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `primed` (stretch)\n\nWe can make examples with lambda terms slightly easier to write\nby adding the following definitions:\n\\begin{code}\n\u019b\u2032_\u21d2_ : Term \u2192 Term \u2192 Term\n\u019b\u2032 (` x) \u21d2 N = \u019b x \u21d2 N\n\u019b\u2032 _ \u21d2 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\ncase\u2032_[zero\u21d2_|suc_\u21d2_] : Term \u2192 Term \u2192 Term \u2192 Term \u2192 Term\ncase\u2032 L [zero\u21d2 M |suc (` x) \u21d2 N ] = case L [zero\u21d2 M |suc x \u21d2 N ]\ncase\u2032 _ [zero\u21d2 _ |suc _ \u21d2 _ ] = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n\u03bc\u2032_\u21d2_ : Term \u2192 Term \u2192 Term\n\u03bc\u2032 (` x) \u21d2 N = \u03bc x \u21d2 N\n\u03bc\u2032 _ \u21d2 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\nThe definition of `plus` can now be written as follows:\n\\begin{code}\nplus\u2032 : Term\nplus\u2032 = \u03bc\u2032 + \u21d2 \u019b\u2032 m \u21d2 \u019b\u2032 n \u21d2\n case\u2032 m\n [zero\u21d2 n\n |suc m \u21d2 `suc (+ \u00b7 m \u00b7 n) ]\n where\n + = ` \"+\"\n m = ` \"m\"\n n = ` \"n\"\n\\end{code}\nWrite out the definition of multiplication in the same style.\n\n\n### Formal vs informal\n\nIn informal presentation of formal semantics, one uses choice of\nvariable name to disambiguate and writes `x` rather than `` ` x ``\nfor a term that is a variable. Agda requires we distinguish.\n\nSimilarly, informal presentation often use the same notation for\nfunction types, lambda abstraction, and function application in both\nthe _object language_ (the language one is describing) and the\n_meta-language_ (the language in which the description is written),\ntrusting readers can use context to distinguish the two. Agda is\nnot quite so forgiving, so here we use `\u019b x \u21d2 N` and `L \u00b7 M` for the\nobject language, as compared to `\u03bb x \u2192 N` and `L M` in our\nmeta-language, Agda.\n\n\n### Bound and free variables\n\nIn an abstraction `\u019b x \u21d2 N` we call `x` the _bound_ variable\nand `N` the _body_ of the abstraction. A central feature\nof lambda calculus is that consistent renaming of bound variables\nleaves the meaning of a term unchanged. Thus the five terms\n\n* `` \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n* `` \u019b \"f\" \u21d2 \u019b \"x\" \u21d2 ` \"f\" \u00b7 (` \"f\" \u00b7 ` \"x\") ``\n* `` \u019b \"sam\" \u21d2 \u019b \"zelda\" \u21d2 ` \"sam\" \u00b7 (` \"sam\" \u00b7 ` \"zelda\") ``\n* `` \u019b \"z\" \u21d2 \u019b \"s\" \u21d2 ` \"z\" \u00b7 (` \"z\" \u00b7 ` \"s\") ``\n* `` \u019b \"\ud83d\ude07\" \u21d2 \u019b \"\ud83d\ude08\" \u21d2 ` \"\ud83d\ude07\" \u00b7 (` \"\ud83d\ude07\" \u00b7 ` \"\ud83d\ude08\" ) ``\n\nare all considered equivalent. Following the convention introduced\nby Haskell Curry, who used the Greek letter `\u03b1` (_alpha_) to label such rules,\nthis equivalence relation is called _alpha renaming_.\n\nAs we descend from a term into its subterms, variables\nthat are bound may become free. Consider the following terms:\n\n* `` \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has both `s` and `z` as bound variables.\n\n* `` \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has `z` bound and `s` free.\n\n* `` ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has both `s` and `z` as free variables.\n\nWe say that a term with no free variables is _closed_; otherwise it is\n_open_. Of the three terms above, the first is closed and the other\ntwo are open. We will focus on reduction of closed terms.\n\nDifferent occurrences of a variable may be bound and free.\nIn the term\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 ` \"x\"\n\nthe inner occurrence of `x` is bound while the outer occurrence is free.\nBy alpha renaming, the term above is equivalent to\n\n (\u019b \"y\" \u21d2 ` \"y\") \u00b7 ` \"x\"\n\nin which `y` is bound and `x` is free. A common convention, called the\n_Barendregt convention_, is to use alpha renaming to ensure that the bound\nvariables in a term are distinct from the free variables, which can\navoid confusions that may arise if bound and free variables have the\nsame names.\n\nCase and recursion also introduce bound variables, which are also subject\nto alpha renaming. In the term\n\n \u03bc \"+\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\"\n [zero\u21d2 ` \"n\"\n |suc \"m\" \u21d2 `suc (` \"+\" \u00b7 ` \"m\" \u00b7 ` \"n\") ]\n\nnotice that there are two binding occurrences of `m`, one in the first\nline and one in the last line. It is equivalent to the following term,\n\n \u03bc \"plus\" \u21d2 \u019b \"x\" \u21d2 \u019b \"y\" \u21d2\n case ` \"x\"\n [zero\u21d2 ` \"y\"\n |suc \"x\u2032\" \u21d2 `suc (` \"plus\" \u00b7 ` \"x\u2032\" \u00b7 ` \"y\") ]\n\nwhere the two binding occurrences corresponding to `m` now have distinct\nnames, `x` and `x\u2032`.\n\n\n## Values\n\nA _value_ is a term that corresponds to an answer.\nThus, `` `suc `suc `suc `suc `zero `` is a value,\nwhile `` plus \u00b7 two \u00b7 two `` is not.\nFollowing convention, we treat all function abstractions\nas values; thus, `` plus `` by itself is considered a value.\n\nThe predicate `Value M` holds if term `M` is a value:\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n\n V-\u019b : \u2200 {x N}\n ---------------\n \u2192 Value (\u019b x \u21d2 N)\n\n V-zero :\n -----------\n Value `zero\n\n V-suc : \u2200 {V}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n\\end{code}\n\nIn what follows, we let `V` and `W` range over values.\n\n\n### Formal vs informal\n\nIn informal presentations of formal semantics, using\n`V` as the name of a metavariable is sufficient to\nindicate that it is a value. In Agda, we must explicitly\ninvoke the `Value` predicate.\n\n### Other approaches\n\nAn alternative is not to focus on closed terms,\nto treat variables as values, and to treat\n`\u019b x \u21d2 N` as a value only if `N` is a value.\nIndeed, this is how Agda normalises terms.\nWe consider this approach in\nChapter [Untyped][plfa.Untyped].\n\n\n## Substitution\n\nThe heart of lambda calculus is the operation of\nsubstituting one term for a variable in another term.\nSubstitution plays a key role in defining the\noperational semantics of function application.\nFor instance, we have\n\n (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 \"z\")) \u00b7 `zero\n \u2014\u2192\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n\nwhere we substitute `suc\u1d9c` for `` ` \"s\" `` and `` `zero `` for `` ` \"z\" ``\nin the body of the function abstraction.\n\nWe write substitution as `N [ x := V ]`, meaning\n\"substitute term `V` for free occurrences of variable `x` in term `N`\",\nor, more compactly, \"substitute `V` for `x` in `N`\",\nor equivalently, \"in `N` replace `x` by `V`\".\nSubstitution works if `V` is any closed term;\nit need not be a value, but we use `V` since in fact we\nusually substitute values.\n\nHere are some examples:\n\n* `` (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) [ \"s\" := suc\u1d9c ] `` yields\n `` \u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\") ``\n* `` (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) [ \"z\" := `zero ] `` yields\n `` suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero) ``\n* `` (\u019b \"x\" \u21d2 ` \"y\") [ \"y\" := `zero ] `` yields `` \u019b \"x\" \u21d2 `zero ``\n* `` (\u019b \"x\" \u21d2 ` \"x\") [ \"x\" := `zero ] `` yields `` \u019b \"x\" \u21d2 ` \"x\" ``\n* `` (\u019b \"y\" \u21d2 ` \"y\") [ \"x\" := `zero ] `` yields `` \u019b \"y\" \u21d2 ` \"y\" ``\n\nIn the last but one example, substituting `` `zero `` for `x` in\n`` \u019b \"x\" \u21d2 ` \"x\" `` does _not_ yield `` \u019b \"x\" \u21d2 `zero ``,\nsince `x` is bound in the lambda abstraction.\nThe choice of bound names is irrelevant: both\n`` \u019b \"x\" \u21d2 ` \"x\" `` and `` \u019b \"y\" \u21d2 ` \"y\" `` stand for the\nidentity function. One way to think of this is that `x` within\nthe body of the abstraction stands for a _different_ variable than\n`x` outside the abstraction, they just happen to have the same name.\n\nWe will give a definition of substitution that is only valid\nwhen term substituted for the variable is closed. This is because\nsubstitution by terms that are _not_ closed may require renaming\nof bound variables. For example:\n\n* `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"y\") [ \"y\" := ` \"x\" \u00b7 `zero] `` should not yield \n `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 (` \"x\" \u00b7 `zero)) ``\n\nInstead, we should rename the bound variable to avoid capture:\n\n* `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"y\") [ \"y\" := ` \"x\" \u00b7 `zero ] `` should yield \n `` \u019b \"x\u2032\" \u21d2 ` \"x\u2032\" \u00b7 (` \"x\" \u00b7 `zero) ``\n\nHere `x\u2032` is a fresh variable distinct from `x`.\nFormal definition of substitution with suitable renaming is considerably\nmore complex, so we avoid it by restricting to substitution by closed terms,\nwhich will be adequate for our purposes.\n\nHere is the formal definition of substitution by closed terms in Agda:\n\n\\begin{code}\ninfix 9 _[_:=_]\n\n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\n(` x) [ y := V ] with x \u225f y\n... | yes _ = V\n... | no _ = ` x\n(\u019b x \u21d2 N) [ y := V ] with x \u225f y\n... | yes _ = \u019b x \u21d2 N\n... | no _ = \u019b x \u21d2 N [ y := V ]\n(L \u00b7 M) [ y := V ] = L [ y := V ] \u00b7 M [ y := V ]\n(`zero) [ y := V ] = `zero\n(`suc M) [ y := V ] = `suc M [ y := V ]\n(case L [zero\u21d2 M |suc x \u21d2 N ]) [ y := V ] with x \u225f y\n... | yes _ = case L [ y := V ] [zero\u21d2 M [ y := V ] |suc x \u21d2 N ]\n... | no _ = case L [ y := V ] [zero\u21d2 M [ y := V ] |suc x \u21d2 N [ y := V ] ]\n(\u03bc x \u21d2 N) [ y := V ] with x \u225f y\n... | yes _ = \u03bc x \u21d2 N\n... | no _ = \u03bc x \u21d2 N [ y := V ]\n\\end{code}\n\nLet's unpack the first three cases:\n\n* For variables, we compare `y`, the substituted variable,\nwith `x`, the variable in the term. If they are the same,\nwe yield `V`, otherwise we yield `x` unchanged.\n\n* For abstractions, we compare `y`, the substituted variable,\nwith `x`, the variable bound in the abstraction. If they are the same,\nwe yield the abstraction unchanged, otherwise we substitute inside the body.\n\n* For application, we recursively substitute in the function\nand the argument.\n\nCase expressions and recursion also have bound variables that are\ntreated similarly to those in lambda abstractions. Otherwise we\nsimply push substitution recursively into the subterms.\n\n\n### Examples\n\nHere is confirmation that the examples above are correct:\n\n\\begin{code}\n_ : (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) [ \"s\" := suc\u1d9c ] \u2261 \u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")\n_ = refl\n\n_ : (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) [ \"z\" := `zero ] \u2261 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n_ = refl\n\n_ : (\u019b \"x\" \u21d2 ` \"y\") [ \"y\" := `zero ] \u2261 \u019b \"x\" \u21d2 `zero\n_ = refl\n\n_ : (\u019b \"x\" \u21d2 ` \"x\") [ \"x\" := `zero ] \u2261 \u019b \"x\" \u21d2 ` \"x\"\n_ = refl\n\n_ : (\u019b \"y\" \u21d2 ` \"y\") [ \"x\" := `zero ] \u2261 \u019b \"y\" \u21d2 ` \"y\"\n_ = refl\n\\end{code}\n\n\n#### Quiz\n\nWhat is the result of the following substitution?\n\n (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) [ \"x\" := `zero ]\n\n1. `` (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) ``\n2. `` (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 `zero)) ``\n3. `` (\u019b \"y\" \u21d2 `zero \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) ``\n4. `` (\u019b \"y\" \u21d2 `zero \u00b7 (\u019b \"x\" \u21d2 `zero)) ``\n\n\n#### Exercise `_[_:=_]\u2032` (stretch)\n\nThe definition of substitution above has three clauses (`\u019b`, `case`,\nand `\u03bc`) that invoke a `with` clause to deal with bound variables.\nRewrite the definition to factor the common part of these three\nclauses into a single function, defined by mutual recursion with\nsubstitution.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n## Reduction\n\nWe give the reduction rules for call-by-value lambda calculus. To\nreduce an application, first we reduce the left-hand side until it\nbecomes a value (which must be an abstraction); then we reduce the\nright-hand side until it becomes a value; and finally we substitute\nthe argument for the variable in the abstraction.\n\nIn an informal presentation of the operational semantics,\nthe rules for reduction of applications are written as follows:\n\n L \u2014\u2192 L\u2032\n --------------- \u03be-\u00b7\u2081\n L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n M \u2014\u2192 M\u2032\n -------------- \u03be-\u00b7\u2082\n V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n ----------------------------- \u03b2-\u019b\n (\u019b x \u21d2 N) \u00b7 V \u2014\u2192 N [ x := V ] \n\nThe Agda version of the rules below will be similar, except that universal\nquantifications are made explicit, and so are the predicates that indicate\nwhich terms are values.\n\nThe rules break into two sorts. Compatibility rules direct us to\nreduce some part of a term. We give them names starting with the\nGreek letter `\u03be` (_xi_). Once a term is sufficiently reduced, it will\nconsist of a constructor and a deconstructor, in our case `\u019b` and `\u00b7`,\nwhich reduces directly. We give them names starting with the Greek\nletter `\u03b2` (_beta_) and such rules are traditionally called _beta rules_.\n\nA bit of terminology: A term that matches the left-hand side of a\nreduction rule is called a _redex_. In the redex `(\u019b x \u21d2 N) \u00b7 V`, we\nmay refer to `x` as the _formal parameter_ of the function, and `V`\nas the _actual parameter_ of the function application. Beta reduction\nreplaces the formal parameter by the actual parameter.\n\nIf a term is a value, then no reduction applies; conversely,\nif a reduction applies to a term then it is not a value.\nWe will show in the next chapter that for well-typed terms\nthis exhausts the possibilities: for every well-typed term\neither a reduction applies or it is a value.\n\nFor numbers, zero does not reduce and successor reduces the subterm.\nA case expression reduces its argument to a number, and then chooses\nthe zero or successor branch as appropriate. A fixpoint replaces\nthe bound variable by the entire fixpoint term; this is the one\ncase where we substitute by a term that is not a value.\n\nHere are the rules formalised in Agda:\n\n\\begin{code}\ninfix 4 _\u2014\u2192_\n\ndata _\u2014\u2192_ : Term \u2192 Term \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03b2-\u019b : \u2200 {x N V}\n \u2192 Value V\n ------------------------------\n \u2192 (\u019b x \u21d2 N) \u00b7 V \u2014\u2192 N [ x := V ]\n\n \u03be-suc : \u2200 {M M\u2032}\n \u2192 M \u2014\u2192 M\u2032\n ------------------\n \u2192 `suc M \u2014\u2192 `suc M\u2032\n\n \u03be-case : \u2200 {x L L\u2032 M N}\n \u2192 L \u2014\u2192 L\u2032\n -----------------------------------------------------------------\n \u2192 case L [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 case L\u2032 [zero\u21d2 M |suc x \u21d2 N ]\n\n \u03b2-zero : \u2200 {x M N}\n ----------------------------------------\n \u2192 case `zero [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 M\n\n \u03b2-suc : \u2200 {x V M N}\n \u2192 Value V\n ---------------------------------------------------\n \u2192 case `suc V [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 N [ x := V ]\n\n \u03b2-\u03bc : \u2200 {x M}\n ------------------------------\n \u2192 \u03bc x \u21d2 M \u2014\u2192 M [ x := \u03bc x \u21d2 M ]\n\\end{code}\n\nThe reduction rules are carefully designed to ensure that subterms\nof a term are reduced to values before the whole term is reduced.\nThis is referred to as _call by value_ reduction.\n\nFurther, we have arranged that subterms are reduced in a\nleft-to-right order. This means that reduction is _deterministic_:\nfor any term, there is at most one other term to which it reduces.\nPut another way, our reduction relation `\u2014\u2192` is in fact a (partial) function.\n\n\n#### Quiz\n\nWhat does the following term step to?\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u2014\u2192 ???\n\n1. `` (\u019b \"x\" \u21d2 ` \"x\") ``\n2. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n3. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n\nWhat does the following term step to?\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u2014\u2192 ???\n\n1. `` (\u019b \"x\" \u21d2 ` \"x\") ``\n2. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n3. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n\nWhat does the following term step to? (Where `two\u1d9c` and `suc\u1d9c` are as\ndefined above.)\n\n two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2014\u2192 ???\n\n1. `` suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero) ``\n2. `` (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero ``\n3. `` `zero ``\n\n\n## Reflexive and transitive closure\n\nA single step is only part of the story. In general, we wish to repeatedly\nstep a closed term until it reduces to a value. We do this by defining\nthe reflexive and transitive closure `\u2014\u21a0` of the step relation `\u2014\u2192`.\n\nWe define reflexive and transitive closure as a sequence of zero or\nmore steps of the underlying relation, along lines similar to that for\nreasoning about chains of equalities in\nChapter [Equality][plfa.Equality]:\n\\begin{code}\ninfix 2 _\u2014\u21a0_\ninfix 1 begin_\ninfixr 2 _\u2014\u2192\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u2014\u21a0_ : Term \u2192 Term \u2192 Set where\n _\u220e : \u2200 M\n ---------\n \u2192 M \u2014\u21a0 M\n\n _\u2014\u2192\u27e8_\u27e9_ : \u2200 L {M N}\n \u2192 L \u2014\u2192 M\n \u2192 M \u2014\u21a0 N\n ---------\n \u2192 L \u2014\u21a0 N\n\nbegin_ : \u2200 {M N}\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 M \u2014\u21a0 N\nbegin M\u2014\u21a0N = M\u2014\u21a0N\n\\end{code}\nWe can read this as follows:\n\n* From term `M`, we can take no steps, giving a step of type `M \u2014\u21a0 M`.\n It is written `M \u220e`.\n\n* From term `L` we can take a single step of type `L \u2014\u2192 M` followed by zero\n or more steps of type `M \u2014\u21a0 N`, giving a step of type `L \u2014\u21a0 N`. It is\n written `L \u2014\u2192\u27e8 L\u2014\u2192M \u27e9 M\u2014\u21a0N`, where `L\u2014\u2192M` and `M\u2014\u21a0N` are steps of the\n appropriate type.\n\nThe notation is chosen to allow us to lay out example reductions in an\nappealing way, as we will see in the next section.\n\nAn alternative is to define reflexive and transitive closure directly,\nas the smallest relation that includes `\u2014\u2192` and is also reflexive\nand transitive. We could do so as follows:\n\\begin{code}\ndata _\u2014\u21a0\u2032_ : Term \u2192 Term \u2192 Set where\n\n step\u2032 : \u2200 {M N}\n \u2192 M \u2014\u2192 N\n -------\n \u2192 M \u2014\u21a0\u2032 N\n\n refl\u2032 : \u2200 {M}\n -------\n \u2192 M \u2014\u21a0\u2032 M\n\n trans\u2032 : \u2200 {L M N}\n \u2192 L \u2014\u21a0\u2032 M\n \u2192 M \u2014\u21a0\u2032 N\n -------\n \u2192 L \u2014\u21a0\u2032 N\n\\end{code}\nThe three constructors specify, respectively, that `\u2014\u21a0\u2032` includes `\u2014\u2192`\nand is reflexive and transitive. A good exercise is to show that\nthe two definitions are equivalent (indeed, one embeds in the other).\n\n#### Exercise `\u2014\u21a0\u2272\u2014\u21a0\u2032`\n\nShow that the first notion of reflexive and transitive closure\nabove embeds into the second. Why are they not isomorphic?\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n## Confluence\n\nOne important property a reduction relation might satisfy is\nto be _confluent_. If term `L` reduces to two other terms,\n`M` and `N`, then both of these reduce to a common term `P`.\nIt can be illustrated as follows:\n\n L\n \/ \\\n \/ \\\n \/ \\\n M N\n \\ \/\n \\ \/\n \\ \/\n P\n\nHere `L`, `M`, `N` are universally quantified while `P`\nis existentially quantified. If each line stands for zero\nor more reduction steps, this is called confluence,\nwhile if the top two lines stand for a single reduction\nstep and the bottom two stand for zero or more reduction\nsteps it is called the diamond property. In symbols:\n\n confluence : \u2200 {L M N} \u2192 \u2203[ P ]\n ( ((L \u2014\u21a0 M) \u00d7 (L \u2014\u21a0 N))\n --------------------\n \u2192 ((M \u2014\u21a0 P) \u00d7 (N \u2014\u21a0 P)) )\n\n diamond : \u2200 {L M N} \u2192 \u2203[ P ]\n ( ((L \u2014\u2192 M) \u00d7 (L \u2014\u2192 N))\n --------------------\n \u2192 ((M \u2014\u21a0 P) \u00d7 (N \u2014\u21a0 P)) )\n\nAll of the reduction systems studied in this text are deterministic.\nIn symbols:\n\n deterministic : \u2200 {L M N}\n \u2192 L \u2014\u2192 M\n \u2192 L \u2014\u2192 N\n ------\n \u2192 M \u2261 N\n\nIt is easy to show that every deterministic relation satisfies\nthe diamond property, and that every relation that satisfies\nthe diamond property is confluent. Hence, all the reduction\nsystems studied in this text are trivially confluent.\n\n\n## Examples\n\nWe start with a simple example. The Church numeral two applied to the\nsuccessor function and zero yields the natural number two:\n\\begin{code}\n_ : two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2014\u21a0 `suc `suc `zero\n_ =\n begin\n two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero\n \u2014\u2192\u27e8 \u03b2-\u019b V-zero \u27e9\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero) \u27e9\n suc\u1d9c \u00b7 `suc `zero\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc V-zero) \u27e9\n `suc (`suc `zero)\n \u220e\n\\end{code}\n\nHere is a sample reduction demonstrating that two plus two is four:\n\\begin{code}\n_ : plus \u00b7 two \u00b7 two \u2014\u21a0 `suc `suc `suc `suc `zero\n_ =\n begin\n plus \u00b7 two \u00b7 two\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc) \u27e9\n (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two \u00b7 two\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n (\u019b \"n\" \u21d2\n case two [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc V-zero)) \u27e9\n case two [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ]\n \u2014\u2192\u27e8 \u03b2-suc (V-suc V-zero) \u27e9\n `suc (plus \u00b7 `suc `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc)) \u27e9\n `suc ((\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 `suc `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u019b (V-suc V-zero))) \u27e9\n `suc ((\u019b \"n\" \u21d2\n case `suc `zero [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n `suc (case `suc `zero [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ])\n \u2014\u2192\u27e8 \u03be-suc (\u03b2-suc V-zero) \u27e9\n `suc `suc (plus \u00b7 `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc))) \u27e9\n `suc `suc ((\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u019b V-zero))) \u27e9\n `suc `suc ((\u019b \"n\" \u21d2\n case `zero [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u019b (V-suc (V-suc V-zero)))) \u27e9\n `suc `suc (case `zero [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ])\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc \u03b2-zero) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\nAnd here is a similar sample reduction for Church numerals:\n\\begin{code}\n_ : plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2014\u21a0 `suc `suc `suc `suc `zero\n_ =\n begin\n (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"m\" \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\"))\n \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b))) \u27e9\n (\u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 two\u1d9c \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\"))\n \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b)) \u27e9\n (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 two\u1d9c \u00b7 ` \"s\" \u00b7 (two\u1d9c \u00b7 ` \"s\" \u00b7 ` \"z\")) \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 two\u1d9c \u00b7 suc\u1d9c \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero\n \u2014\u2192\u27e8 \u03b2-\u019b V-zero \u27e9\n two\u1d9c \u00b7 suc\u1d9c \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 ((\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero))\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (suc\u1d9c \u00b7 (`suc `zero))\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b (V-suc V-zero)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (`suc `suc `zero)\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc V-zero)) \u27e9\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `suc `suc `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n suc\u1d9c \u00b7 (`suc `suc `suc `zero)\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc (V-suc V-zero))) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\nIn the next chapter, we will see how to compute such reduction sequences.\n\n\n#### Exercise `plus-example`\n\nWrite out the reduction sequence demonstrating that one plus one is two.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n## Syntax of types\n\nWe have just two types:\n\n * Functions, `A \u21d2 B`\n * Naturals, `` `\u2115 ``\n\nAs before, to avoid overlap we use variants of the names used by Agda.\n\nHere is the syntax of types in BNF:\n\n A, B, C ::= A \u21d2 B | `\u2115\n\nAnd here it is formalised in Agda:\n\n\\begin{code}\ninfixr 7 _\u21d2_\n\ndata Type : Set where\n _\u21d2_ : Type \u2192 Type \u2192 Type\n `\u2115 : Type\n\\end{code}\n\n### Precedence\n\nAs in Agda, functions of two or more arguments are represented via\ncurrying. This is made more convenient by declaring `_\u21d2_` to\nassociate to the right and `_\u00b7_` to associate to the left.\nThus:\n\n* ``(`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115`` stands for ``((`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115))``\n* `plus \u00b7 two \u00b7 two` stands for `(plus \u00b7 two) \u00b7 two`.\n\n### Quiz\n\n* What is the type of the following term?\n\n `` \u019b \"s\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 `zero) ``\n\n 1. `` (`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 2. `` (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 ``\n 3. `` `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 4. `` `\u2115 \u21d2 `\u2115 \u21d2 `\u2115 ``\n 5. `` `\u2115 \u21d2 `\u2115 ``\n 6. `` `\u2115 ``\n\n Give more than one answer if appropriate.\n\n* What is the type of the following term?\n\n `` (\u019b \"s\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 `zero)) \u00b7 suc\u1d9c ``\n\n 1. `` (`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 2. `` (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 ``\n 3. `` `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 4. `` `\u2115 \u21d2 `\u2115 \u21d2 `\u2115 ``\n 5. `` `\u2115 \u21d2 `\u2115 ``\n 6. `` `\u2115 ``\n\n Give more than one answer if appropriate.\n\n\n## Typing\n\n### Contexts\n\nWhile reduction considers only closed terms, typing must\nconsider terms with free variables. To type a term,\nwe must first type its subterms, and in particular in the\nbody of an abstraction its bound variable may appear free.\n\nA _context_ associates variables with types. We let `\u0393` and `\u0394` range\nover contexts. We write `\u2205` for the empty context, and `\u0393 , x \u2982 A`\nfor the context that extends `\u0393` by mapping variable `x` to type `A`.\nFor example,\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 ``\n\nis the context that associates variable ` \"s\" ` with type `` `\u2115 \u21d2 `\u2115 ``,\nand variable ` \"z\" ` with type `` `\u2115 ``.\n\nContexts are formalised as follows:\n\n\\begin{code}\ninfixl 5 _,_\u2982_\n\ndata Context : Set where\n \u2205 : Context\n _,_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Context\n\\end{code}\n\n\n#### Exercise `Context-\u2243`\n\nShow that `Context` is isomorphic to `List (Id \u00d7 Type)`.\nFor instance, the isomorphism relates the context\n\n `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 ``\n\nto the list\n\n `` [ \u27e8 \"z\" , `\u2115 \u27e9 , \u27e8 \"s\" , `\u2115 \u21d2 `\u2115 \u27e9 ] ``.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n### Lookup judgment\n\nWe have two forms of _judgment_. The first is written\n\n \u0393 \u220b x \u2982 A\n\nand indicates in context `\u0393` that variable `x` has type `A`.\nIt is called _lookup_.\nFor example,\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u220b \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u220b \"s\" \u2982 `\u2115 \u21d2 `\u2115 ``\n\ngive us the types associated with variables `` \"z\" `` and `` \"s\" ``,\nrespectively. The symbol `\u220b` (pronounced \"ni\", for \"in\"\nbackwards) is chosen because checking that `\u0393 \u220b x \u2982 A` is analogous to\nchecking whether `x \u2982 A` appears in a list corresponding to `\u0393`.\n\nIf two variables in a context have the same name, then lookup\nshould return the most recently bound variable, which _shadows_\nthe other variables. For example,\n\n* `` \u2205 , \"x\" : `\u2115 \u21d2 `\u2115 , \"x\" : `\u2115 \u220b \"x\" \u2982 `\u2115 ``\n\nHere `` \"x\" \u2982 `\u2115 \u21d2 `\u2115 `` is shadowed by `` \"x\" \u2982 `\u2115 ``.\n\nLookup is formalised as follows:\n\\begin{code}\ninfix 4 _\u220b_\u2982_\n\ndata _\u220b_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 x A}\n ------------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 x y A B}\n \u2192 x \u2262 y\n \u2192 \u0393 \u220b x \u2982 A\n ------------------\n \u2192 \u0393 , y \u2982 B \u220b x \u2982 A\n\\end{code}\n\nThe constructors `Z` and `S` correspond roughly to the constructors\n`here` and `there` for the element-of relation `_\u2208_` on lists.\nConstructor `S` takes an additional parameter, which ensures that\nwhen we look up a variable that it is not _shadowed_ by another\nvariable with the same name earlier in the list.\n\n### Typing judgment\n\nThe second judgment is written\n\n \u0393 \u22a2 M \u2982 A\n\nand indicates in context `\u0393` that term `M` has type `A`.\nContext `\u0393` provides types for all the free variables in `M`.\nFor example:\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"s\" \u2982 `\u2115 \u21d2 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` ` \"s\" \u00b7 ` \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 \u22a2 (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u2982 `\u2115 \u21d2 `\u2115 ``\n* `` \u2205 \u22a2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u2982 (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115 ``\n\nTyping is formalised as follows:\n\\begin{code}\ninfix 4 _\u22a2_\u2982_\n\ndata _\u22a2_\u2982_ : Context \u2192 Term \u2192 Type \u2192 Set where\n\n -- Axiom\n \u22a2` : \u2200 {\u0393 x A}\n \u2192 \u0393 \u220b x \u2982 A\n -------------\n \u2192 \u0393 \u22a2 ` x \u2982 A\n\n -- \u21d2-I\n \u22a2\u019b : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2982 B\n -------------------\n \u2192 \u0393 \u22a2 \u019b x \u21d2 N \u2982 A \u21d2 B\n\n -- \u21d2-E\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2982 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2982 A\n -------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2982 B\n\n -- \u2115-I\u2081\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2982 `\u2115\n\n -- \u2115-I\u2082\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2982 `\u2115\n\n -- \u2115-E\n \u22a2case : \u2200 {\u0393 L M x N A}\n \u2192 \u0393 \u22a2 L \u2982 `\u2115\n \u2192 \u0393 \u22a2 M \u2982 A\n \u2192 \u0393 , x \u2982 `\u2115 \u22a2 N \u2982 A\n -------------------------------------\n \u2192 \u0393 \u22a2 case L [zero\u21d2 M |suc x \u21d2 N ] \u2982 A\n\n \u22a2\u03bc : \u2200 {\u0393 x M A}\n \u2192 \u0393 , x \u2982 A \u22a2 M \u2982 A\n -----------------\n \u2192 \u0393 \u22a2 \u03bc x \u21d2 M \u2982 A\n\\end{code}\n\nEach type rule is named after the constructor for the\ncorresponding term.\n\nMost of the rules have a second name,\nderived from a convention in logic, whereby the rule is\nnamed after the type connective that it concerns;\nrules to introduce and to\neliminate each connective are labeled `-I` and `-E`, respectively. As we\nread the rules from top to bottom, introduction and elimination rules\ndo what they say on the tin: the first _introduces_ a formula for the\nconnective, which appears in the conclusion but not in the premises;\nwhile the second _eliminates_ a formula for the connective, which appears in\na premise but not in the conclusion. An introduction rule describes\nhow to construct a value of the type (abstractions yield functions,\n`` `suc `` and `` `zero `` yield naturals), while an elimination rule describes\nhow to deconstruct a value of the given type (applications use\nfunctions, case expressions use naturals).\n\nThe rules are deterministic, in that at most one rule applies to every term.\n\n\n### Checking inequality and postulating the impossible {#impossible}\n\nThe following function makes it convenient to assert an inequality:\n\\begin{code}\n_\u2260_ : \u2200 (x y : Id) \u2192 x \u2262 y\nx \u2260 y with x \u225f y\n... | no x\u2262y = x\u2262y\n... | yes _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\nHere `_\u225f_` is the function that tests two identifiers for equality.\nWe intend to apply the function only when the\ntwo arguments are indeed unequal, and indicate that the second\ncase should never arise by postulating a term `impossible` of\nwith the empty type `\u22a5`. If we use C-c C-n to normalise the term\n\n \"a\" \u2260 \"a\"\n\nAgda will return an answer warning us that the impossible has occurred:\n\n \u22a5-elim (.plfa.Lambda.impossible \"a\" \"a\" refl)\n\nWhile postulating the impossible is a useful technique, it must be\nused with care, since such postulation could allow us to provide\nevidence of _any_ proposition whatsoever, regardless of its truth.\n\n\n### Example type derivations {#derivation}\n\nType derivations correspond to trees. In informal notation, here\nis a type derivation for the Church numberal two,\n\n \u220bs \u220bz\n ------------------ \u22a2` -------------- \u22a2`\n \u220bs \u0393\u2082 \u22a2 ` \"s\" \u2982 A \u21d2 A \u0393\u2082 \u22a2 ` \"z\" \u2982 A\n ------------------ \u22a2` ------------------------------------- _\u00b7_\n \u0393\u2082 \u22a2 ` \"s\" \u2982 A \u21d2 A \u0393\u2082 \u22a2 ` \"s\" \u00b7 ` \"z\" \u2982 A\n ---------------------------------------------- _\u00b7_\n \u0393\u2082 \u22a2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 A\n -------------------------------------------- \u22a2\u019b\n \u0393\u2081 \u22a2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 A \u21d2 A\n ------------------------------------------------------------- \u22a2\u019b\n \u0393 \u22a2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 (A \u21d2 A) \u21d2 A \u21d2 A\n\nwhere `\u220bs` and `\u220bz` abbreviate the two derivations,\n\n ---------------- Z\n \"s\" \u2262 \"z\" \u0393\u2081 \u220b \"s\" \u2982 A \u21d2 A\n ----------------------------- S ------------- Z\n \u0393\u2082 \u220b \"s\" \u2982 A \u21d2 A \u0393\u2082 \u220b \"z\" \u2982 A\n\nand where `\u0393\u2081 = \u0393 , \"s\" \u2982 A \u21d2 A` and `\u0393\u2082 = \u0393 , \"s\" \u2982 A \u21d2 A , \"z\" \u2982 A`.\nThe typing derivation is valid for any `\u0393` and `A`, for instance,\nwe might take `\u0393` to be `\u2205` and `A` to be `` `\u2115 ``.\n\nHere is the above typing derivation formalised in Agda:\n\\begin{code}\nCh : Type \u2192 Type\nCh A = (A \u21d2 A) \u21d2 A \u21d2 A\n\n\u22a2two\u1d9c : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 two\u1d9c \u2982 Ch A\n\u22a2two\u1d9c = \u22a2\u019b (\u22a2\u019b (\u22a2` \u220bs \u00b7 (\u22a2` \u220bs \u00b7 \u22a2` \u220bz)))\n where\n \u220bs = S (\"s\" \u2260 \"z\") Z\n \u220bz = Z\n\\end{code}\n\nHere are the typings corresponding to computing two plus two:\n\\begin{code}\n\u22a2two : \u2200 {\u0393} \u2192 \u0393 \u22a2 two \u2982 `\u2115\n\u22a2two = \u22a2suc (\u22a2suc \u22a2zero)\n\n\u22a2plus : \u2200 {\u0393} \u2192 \u0393 \u22a2 plus \u2982 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\n\u22a2plus = \u22a2\u03bc (\u22a2\u019b (\u22a2\u019b (\u22a2case (\u22a2` \u220bm) (\u22a2` \u220bn)\n (\u22a2suc (\u22a2` \u220b+ \u00b7 \u22a2` \u220bm\u2032 \u00b7 \u22a2` \u220bn\u2032)))))\n where\n \u220b+ = (S (\"+\" \u2260 \"m\") (S (\"+\" \u2260 \"n\") (S (\"+\" \u2260 \"m\") Z)))\n \u220bm = (S (\"m\" \u2260 \"n\") Z)\n \u220bn = Z\n \u220bm\u2032 = Z\n \u220bn\u2032 = (S (\"n\" \u2260 \"m\") Z)\n\n\u22a22+2 : \u2205 \u22a2 plus \u00b7 two \u00b7 two \u2982 `\u2115\n\u22a22+2 = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\n\\end{code}\nIn contrast to our earlier examples, here we have typed `two` and `plus`\nin an arbitrary context rather than the empty context; this makes it easy\nto use them inside other binding contexts as well as at the top level.\nHere the two lookup judgments `\u220bm` and `\u220bm\u2032` refer to two different\nbindings of variables named `\"m\"`. In contrast, the two judgments `\u220bn` and\n`\u220bn\u2032` both refer to the same binding of `\"n\"` but accessed in different\ncontexts, the first where \"n\" is the last binding in the context, and\nthe second after \"m\" is bound in the successor branch of the case.\n\nAnd here are typings for the remainder of the Church example:\n\\begin{code}\n\u22a2plus\u1d9c : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 plus\u1d9c \u2982 Ch A \u21d2 Ch A \u21d2 Ch A\n\u22a2plus\u1d9c = \u22a2\u019b (\u22a2\u019b (\u22a2\u019b (\u22a2\u019b (\u22a2` \u220bm \u00b7 \u22a2` \u220bs \u00b7 (\u22a2` \u220bn \u00b7 \u22a2` \u220bs \u00b7 \u22a2` \u220bz)))))\n where\n \u220bm = S (\"m\" \u2260 \"z\") (S (\"m\" \u2260 \"s\") (S (\"m\" \u2260 \"n\") Z))\n \u220bn = S (\"n\" \u2260 \"z\") (S (\"n\" \u2260 \"s\") Z)\n \u220bs = S (\"s\" \u2260 \"z\") Z\n \u220bz = Z\n\n\u22a2suc\u1d9c : \u2200 {\u0393} \u2192 \u0393 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n\u22a2suc\u1d9c = \u22a2\u019b (\u22a2suc (\u22a2` \u220bn))\n where\n \u220bn = Z\n\n\u22a22+2\u1d9c : \u2205 \u22a2 plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2982 `\u2115\n\u22a22+2\u1d9c = \u22a2plus\u1d9c \u00b7 \u22a2two\u1d9c \u00b7 \u22a2two\u1d9c \u00b7 \u22a2suc\u1d9c \u00b7 \u22a2zero\n\\end{code}\n\n### Interaction with Agda\n\nConstruction of a type derivation may be done interactively.\nStart with the declaration:\n\n \u22a2suc\u1d9c : \u2205 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n \u22a2suc\u1d9c = ?\n\nTyping C-c C-l causes Agda to create a hole and tell us its expected type:\n\n \u22a2suc\u1d9c = { }0\n ?0 : \u2205 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n\nNow we fill in the hole by typing C-c C-r. Agda observes that\nthe outermost term in `suc\u1d9c` is `\u019b`, which is typed using `\u22a2\u019b`. The\n`\u22a2\u019b` rule in turn takes one argument, which Agda leaves as a hole:\n\n \u22a2suc\u1d9c = \u22a2\u019b { }1\n ?1 : \u2205 , \"n\" \u2982 `\u2115 \u22a2 `suc ` \"n\" \u2982 `\u2115\n\nWe can fill in the hole by type C-c C-r again:\n\n \u22a2suc\u1d9c = \u22a2\u019b (\u22a2suc { }2)\n ?2 : \u2205 , \"n\" \u2982 `\u2115 \u22a2 ` \"n\" \u2982 `\u2115\n\nAnd again:\n\n \u22a2suc\u2032 = \u22a2\u019b (\u22a2suc (\u22a2` { }3))\n ?3 : \u2205 , \"n\" \u2982 `\u2115 \u220b \"n\" \u2982 `\u2115\n\nA further attempt with C-c C-r yields the message:\n\n Don't know which constructor to introduce of Z or S\n\nWe can fill in `Z` by hand. If we type C-c C-space, Agda will confirm we are done:\n\n \u22a2suc\u2032 = \u22a2\u019b (\u22a2suc (\u22a2` Z))\n\nThe entire process can be automated using Agsy, invoked with C-c C-a.\n\nChapter [Inference][plfa.DeBruijn]\nwill show how to use Agda to compute type derivations directly.\n\n\n### Lookup is injective\n\nThe lookup relation `\u0393 \u220b x \u2982 A` is injective, in that for each `\u0393` and `x`\nthere is at most one `A` such that the judgment holds:\n\\begin{code}\n\u220b-injective : \u2200 {\u0393 x A B} \u2192 \u0393 \u220b x \u2982 A \u2192 \u0393 \u220b x \u2982 B \u2192 A \u2261 B\n\u220b-injective Z Z = refl\n\u220b-injective Z (S x\u2262 _) = \u22a5-elim (x\u2262 refl)\n\u220b-injective (S x\u2262 _) Z = \u22a5-elim (x\u2262 refl)\n\u220b-injective (S _ \u220bx) (S _ \u220bx\u2032) = \u220b-injective \u220bx \u220bx\u2032\n\\end{code}\n\nThe typing relation `\u0393 \u22a2 M \u2982 A` is not injective. For example, in any `\u0393`\nthe term `\u019b \"x\" \u21d2 \"x\"` has type `A \u21d2 A` for any type `A`.\n\n### Non-examples\n\nWe can also show that terms are _not_ typeable. For example, here is\na formal proof that it is not possible to type the term\n`` `zero \u00b7 `suc `zero ``. It cannot be typed, because doing so\nrequires that the first term in the application is both a natural and\na function:\n\n\\begin{code}\nnope\u2081 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 `zero \u00b7 `suc `zero \u2982 A)\nnope\u2081 (() \u00b7 _)\n\\end{code}\n\nAs a second example, here is a formal proof that it is not possible to\ntype `` \u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"x\" `` It cannot be typed, because\ndoing so requires types `A` and `B` such that `A \u21d2 B \u2261 A`:\n\n\\begin{code}\nnope\u2082 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 \u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"x\" \u2982 A)\nnope\u2082 (\u22a2\u019b (\u22a2` \u220bx \u00b7 \u22a2` \u220bx\u2032)) = contradiction (\u220b-injective \u220bx \u220bx\u2032)\n where\n contradiction : \u2200 {A B} \u2192 \u00ac (A \u21d2 B \u2261 A)\n contradiction ()\n\\end{code}\n\n\n#### Quiz\n\nFor each of the following, give a type `A` for which it is derivable,\nor explain why there is no such `A`.\n\n1. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 , \"x\" \u2982 `\u2115 \u22a2 ` \"y\" \u00b7 ` \"x\" \u2982 A ``\n2. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 , \"x\" \u2982 `\u2115 \u22a2 ` \"x\" \u00b7 ` \"y\" \u2982 A ``\n3. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 \u22a2 \u019b \"x\" \u21d2 ` \"y\" \u00b7 ` \"x\" \u2982 A ``\n\nFor each of the following, give types `A`, `B`, and `C` for which it is derivable,\nor explain why there are no such types.\n\n1. `` \u2205 , \"x\" \u2982 A \u22a2 ` \"x\" \u00b7 ` \"x\" \u2982 B ``\n2. `` \u2205 , \"x\" \u2982 A , \"y\" \u2982 B \u22a2 \u019b \"z\" \u21d2 ` \"x\" \u00b7 (` \"y\" \u00b7 ` \"z\") \u2982 C ``\n\n\n#### Exercise `mul-type` (recommended)\n\nUsing the term `mul` you defined earlier, write out the derivation\nshowing that it is well-typed.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `mul\u1d9c-type`\n\nUsing the term `mul\u1d9c` you defined earlier, write out the derivation\nshowing that it is well-typed.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u21d2 U+21D2 RIGHTWARDS DOUBLE ARROW (\\=>)\n \u019b U+019B LATIN SMALL LETTER LAMBDA WITH STROKE (\\Gl-)\n \u00b7 U+00B7 MIDDLE DOT (\\cdot)\n \u2014 U+2014 EM DASH (\\em)\n \u21a0 U+21A0 RIGHTWARDS TWO HEADED ARROW (\\rr-)\n \u03be U+03BE GREEK SMALL LETTER XI (\\Gx or \\xi)\n \u03b2 U+03B2 GREEK SMALL LETTER BETA (\\Gb or \\beta)\n \u220b U+220B CONTAINS AS MEMBER (\\ni)\n \u2205 U+2205 EMPTY SET (\\0)\n \u22a2 U+22A2 RIGHT TACK (\\vdash or \\|-)\n \u2982 U+2982 Z NOTATION TYPE COLON (\\:)\n \ud83d\ude07 U+1F607 SMILING FACE WITH HALO\n \ud83d\ude08 U+1F608 SMILING FACE WITH HORNS\n\nWe compose reduction `\u2014\u2192` from an em dash `\u2014` and an arrow `\u2192`.\nSimilarly for reflexive and transitive closure `\u2014\u21a0`.\n","avg_line_length":30.8241992883,"max_line_length":89,"alphanum_fraction":0.5774452757} +{"size":1549,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule Type.BetaNBE.Stability where\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Type.BetaNormal\nopen import Type.BetaNormal.Equality\nopen import Type.BetaNBE\nopen import Type.BetaNBE.Completeness\n\nopen import Relation.Binary.PropositionalEquality\nopen import Function\n\\end{code}\n\nIf you take a normal form, embed it back into syntax and then\nnormalize it again, you get the same result. This is an important\nproperty for substitution on normal forms: we don't want to eta expand\nvariables otherwise substituting in by the identity substitution can\nperturb the expression.\n\n\\begin{code}\nstability : \u2200{\u03a6 K}(n : \u03a6 \u22a2Nf\u22c6 K) \u2192 nf (embNf n) \u2261 n\nstabilityNe : \u2200{\u03a6 K}(n : \u03a6 \u22a2Ne\u22c6 K) \u2192 CR K (eval (embNe n) (idEnv _)) (reflect n)\n\nstability (\u03a0 B) =\n cong \u03a0 (trans (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 renVal-reflect S (` \u03b1)})\n (embNf B))\n (stability B))\nstability (A \u21d2 B) = cong\u2082 _\u21d2_ (stability A) (stability B)\nstability (\u019b B) =\n cong \u019b (trans (reifyCR (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 renVal-reflect S (` \u03b1)})\n (embNf B)))\n (stability B))\nstability (con tcn) = refl\nstability {K = *} (ne n) = stabilityNe n\nstability {K = K \u21d2 J} (ne n) = reifyCR (stabilityNe n)\n\nstabilityNe (` \u03b1) = reflectCR refl\nstabilityNe (n \u00b7 n') = transCR (AppCR (stabilityNe n) (idext idCR (embNf n'))) (reflectCR (cong\u2082 _\u00b7_ refl (stability n')))\nstabilityNe \u03bc1 = refl\n\\end{code}\n","avg_line_length":34.4222222222,"max_line_length":122,"alphanum_fraction":0.6249193028} +{"size":445,"ext":"lagda","lang":"Literate Agda","max_stars_count":1003.0,"content":"## Our first corollary: rearranging\n\nWe can apply associativity to rearrange parentheses however we like.\nHere is an example.\n\n\\begin{code}\n+-rearrange : \u2200 (m n p q : \u2115) \u2192 (m + n) + (p + q) \u2261 m + ((n + p) + q)\n+-rearrange m n p q =\n begin\n (m + n) + (p + q)\n \u2261\u27e8 +-assoc m n (p + q) \u27e9\n m + (n + (p + q))\n \u2261\u27e8 cong (m +_) (sym (+-assoc n p q)) \u27e9\n m + ((n + p) + q)\n \u2261\u27e8 sym (+-assoc m (n + p) q) \u27e9\n (m + (n + p)) + q\n \u220e\n\\end{code}\n\n","avg_line_length":22.25,"max_line_length":69,"alphanum_fraction":0.4741573034} +{"size":7849,"ext":"lagda","lang":"Literate Agda","max_stars_count":12.0,"content":"\\begin{code}\nmodule Declarative.Examples where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nimport Type.RenamingSubstitution as \u22c6\nopen import Type.Equality\nopen import Declarative.Term\nopen import Declarative.Term.RenamingSubstitution\nopen import Declarative.Evaluation\nopen import Builtin\nopen import Builtin.Constant.Type\nopen import Builtin.Constant.Term\nopen import Builtin.Signature\nopen import Declarative.StdLib.Function\n\nopen import Relation.Binary.PropositionalEquality renaming (subst to substEq) hiding ([_])\nopen import Function\nopen import Agda.Builtin.Int\nopen import Data.Integer\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Nat\nopen import Data.Unit\n\nimport Declarative.StdLib.ChurchNat\n\\end{code}\n\n## Examples\n\n\\begin{code}\nmodule Builtins where\n open Declarative.StdLib.ChurchNat\n\n con2 : \u2200{\u0393} \u2192 \u0393 \u22a2 con integer (size\u22c6 8)\n con2 = con (integer 8 (pos 2) (-\u2264+ ,, +\u2264+ (s\u2264s (s\u2264s (s\u2264s z\u2264n)))))\n\n builtin2plus2 : \u2205 \u22a2 con integer (size\u22c6 8)\n builtin2plus2 = builtin\n addInteger\n (\u03bb { Z \u2192 size\u22c6 8 ; (S x) \u2192 ` x})\n (con2 ,, con2 ,, tt)\n\n inc8 : \u2205 \u22a2 con integer (size\u22c6 8) \u21d2 con integer (size\u22c6 8)\n inc8 = \u019b (builtin\n addInteger\n (\u03bb { Z \u2192 size\u22c6 8 ; (S x) \u2192 ` x})\n (con1 ,, ` Z ,, tt))\n\n builtininc2 : \u2205 \u22a2 con integer (size\u22c6 8)\n builtininc2 = inc8 \u00b7 con2\n\n builtininc2' : \u2205 \u22a2 con integer (size\u22c6 8)\n builtininc2' = (inc \u00b7\u22c6 size\u22c6 8) \u00b7 con2\n\n\\end{code}\n\n\n### Scott Numerals\n\nFrom http:\/\/lucacardelli.name\/Papers\/Notes\/scott2.pdf\n\nM = \u03bc X . G X\nG X = \u2200 R. R \u2192 (X \u2192 R) \u2192 R)\n\u03bc X . G X = \u2200 X . (G X \u2192 X) \u2192 X -- what is the status of this?\nN = G M\nin : N \u2192 M\nout : M \u2192 N\n\n0 = \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . x\n : N\nsucc = \u03bb n : N . \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . y (in n)\n : N \u2192 N\ncase = \u03bb n : N . \u039b R . \u03bb a : R . \u03bb f : N \u2192 N . n [R] a (f \u2218 out)\n : N \u2192 \u2200 R . R \u2192 (N \u2192 R) \u2192 R\n\n\n--\n\n\\begin{code}\n{-\nmodule ScottE where\n G : \u2200{\u0393} \u2192 \u0393 ,\u22c6 * \u22a2\u22c6 *\n G = \u03a0 (` Z \u21d2 (` (S Z) \u21d2 ` Z) \u21d2 ` Z)\n \n M : \u2200{\u0393} \u2192 \u0393 \u22a2\u22c6 *\n M = \u03bc G\n \n N : \u2200{\u0393} \u2192 \u0393 \u22a2\u22c6 *\n N = G \u22c6.[ M ]\n \n Zero : \u2200{\u0393} \u2192 \u0393 \u22a2 N\n Zero = \u039b (\u019b (\u019b (` (S (Z )))))\n \n Succ : \u2200{\u0393} \u2192 \u0393 \u22a2 N \u21d2 N\n Succ = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 wrap G \u2022 (` (S (S (T Z)))) refl))))\n \n One : \u2200{\u0393} \u2192 \u0393 \u22a2 N\n One = Succ \u00b7 Zero\n \n Two : \u2200{\u0393} \u2192 \u0393 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n case : \u2200{\u0393} \u2192 \u0393 \u22a2 N \u21d2 (\u03a0 (` Z \u21d2 (N \u21d2 ` Z) \u21d2 ` Z))\n case = \u019b (\u039b (\u019b (\u019b ((` (S (S (T Z)))) \u00b7\u22c6 (` Z) \u00b7 (` (S Z)) \u00b7 (\u019b (` (S Z) \u00b7 unwrap \u2022 refl (` Z)))))))\n\n -- Y : (a -> a) -> a\n -- Y f = (\\x. f (x x)) (\\x. f (x x))\n -- Y f = (\\x : mu x. x -> a. f (x x)) (\\x : mu x. x -> a. f (x x)) \n\n Y-comb : \u2200{\u0393} \u2192 \u0393 \u22a2 \u03a0 ((` Z \u21d2 ` Z) \u21d2 ` Z)\n Y-comb = \u039b (\u019b ((\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) \u00b7 wrap (` Z \u21d2 ` (S Z)) \u2022 (\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) refl ))\n\n -- Z : ((a -> b) -> a -> b) -> a -> b\n -- Z f = (\\r. f (\\x. r r x)) (\\r. f (\\x. r r x))\n -- Z f = (\\r : mu x. x -> a -> b. (\\x : a. r r x)) (\\r : mu x. x -> a -> b. (\\x : a. r r x))\n\n Z-comb : \u2200{\u0393} \u2192\n \u0393 \u22a2 \u03a0 {- a -} (\u03a0 {- b -} (((` (S Z) \u21d2 ` Z) \u21d2 ` (S Z) \u21d2 ` Z) \u21d2 ` (S Z) \u21d2 ` Z))\n Z-comb = \u039b {- a -} (\u039b {- b -} (\u019b {- f -} (\u019b {- r -} (` (S Z) \u00b7 \u019b {- x -} (unwrap \u2022 refl (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z)) \u00b7 wrap (` Z \u21d2 ` (S (S Z)) \u21d2 ` (S Z)) \u2022 (\u019b {- r -} (` (S Z) \u00b7 \u019b {- x -} (unwrap \u2022 refl (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z))) refl)))\n\n TwoPlus : \u2200{\u0393} \u2192 \u0393 \u22a2 (N \u21d2 N) \u21d2 N \u21d2 N\n TwoPlus = \u019b (\u019b ((((case \u00b7 (` Z)) \u00b7\u22c6 N) \u00b7 Two) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 (` Z))))))\n\n TwoPlusOne : \u2205 \u22a2 N\n -- TwoPlusTwo = Y-comb \u00b7\u22c6 (N \u21d2 N) \u00b7 TwoPlus \u00b7 Two\n TwoPlusOne = (Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 TwoPlus \u00b7 One\n\n\n -- Roman's more efficient version\n Plus : \u2200 {\u0393} \u2192 \u0393 \u22a2 N \u21d2 N \u21d2 N\n Plus = \u019b (\u019b ((Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 (\u019b (\u019b ((((case \u00b7 ` Z) \u00b7\u22c6 N) \u00b7 ` (S (S (S Z)))) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 ` Z)))))) \u00b7 ` (S Z)))\n\n TwoPlusTwo : \u2205 \u22a2 N\n TwoPlusTwo = (Plus \u00b7 Two) \u00b7 Two\n-}\n\\end{code}\n\n\neval (gas 10000000) Scott.Four\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\neval (gas 10000000) Scott.Two\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\n\n\n\\begin{code}\nmodule Scott1 where\n open import Declarative.StdLib.Nat\n \n One : \u2200{\u0393} \u2192 \u0393 \u22a2 N\n One = Succ \u00b7 Zero\n \n Two : \u2200{\u0393} \u2192 \u0393 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n case : \u2200{\u0393} \u2192 \u0393 \u22a2 N \u21d2 (\u03a0 (` Z \u21d2 (N \u21d2 ` Z) \u21d2 ` Z))\n case = \u019b (\u039b (\u019b (\u019b (` (S (S (T Z))) \u00b7\u22c6 ` Z \u00b7 ` (S Z) \u00b7 \u019b (` (S Z) \u00b7 conv (\u03b2\u2261\u03b2 _ _) (unwrap0 _ (` Z)))))))\n\n TwoPlus : \u2200{\u0393} \u2192 \u0393 \u22a2 (N \u21d2 N) \u21d2 N \u21d2 N\n TwoPlus = \u019b (\u019b ((((case \u00b7 (` Z)) \u00b7\u22c6 N) \u00b7 Two) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 (` Z))))))\n\n TwoPlusOne : \u2205 \u22a2 N\n TwoPlusOne = (Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 TwoPlus \u00b7 One\n\n -- Roman's more efficient version\n Plus : \u2200 {\u0393} \u2192 \u0393 \u22a2 N \u21d2 N \u21d2 N\n Plus = \u019b (\u019b ((Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 (\u019b (\u019b ((((case \u00b7 ` Z) \u00b7\u22c6 N) \u00b7 ` (S (S (S Z)))) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 ` Z)))))) \u00b7 ` (S Z)))\n\n TwoPlusTwo : \u2205 \u22a2 N\n TwoPlusTwo = (Plus \u00b7 Two) \u00b7 Two\n\\end{code}\n\n\n### Church Numerals\n\n\\begin{code}\nmodule Church where\n open Declarative.StdLib.ChurchNat\n \n -- two plus two\n One : \u2205 \u22a2 N\n One = Succ \u00b7 Zero\n\n Two : \u2205 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n Plus : \u2205 \u22a2 N \u2192 \u2205 \u22a2 N \u2192 \u2205 \u22a2 N\n Plus x y = Iter \u00b7\u22c6 N \u00b7 x \u00b7 Succ \u00b7 y -- by induction on the second y\n\n TwoPlusTwo = Plus Two Two\n\n TwoPlusTwo' : \u2205 \u22a2 N\n TwoPlusTwo' = Two \u00b7\u22c6 N \u00b7 Two \u00b7 Succ\n\n--open Church public\n\\end{code}\n\n-- Church \"4\"\neval (gas 100000000) Four\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using iterator\neval (gas 100000000) (Plus Two Two)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using the numerals directly\neval (gas 10000000) (Two \u00b7\u22c6 N \u00b7 Two \u00b7 Succ)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z))) \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n V-\u039b_)\n","avg_line_length":22.6195965418,"max_line_length":240,"alphanum_fraction":0.3667983183} +{"size":25412,"ext":"lagda","lang":"Literate Agda","max_stars_count":2.0,"content":"\\begin{code}\nmodule Algorithmic.Reduction where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality hiding ([_]) renaming (subst to substEq)\nopen import Agda.Builtin.String using (primStringFromList; primStringAppend)\nopen import Data.Empty\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Sum\nopen import Function hiding (_\u220b_)\nopen import Data.Integer using (_? j\n... | no \u00acp = _ ,, inj\u2081 (V-con (bool false))\n... | yes p = _ ,, inj\u2081 (V-con (bool true))\nIBUILTIN greaterThanEqualsInteger \u03c3 ((tt ,, _ ,, V-con (integer i)) ,, _ ,, V-con (integer j)) with i I\u2265? j\n... | no \u00acp = _ ,, inj\u2081 (V-con (bool false))\n... | yes p = _ ,, inj\u2081 (V-con (bool true))\nIBUILTIN equalsInteger \u03c3 ((tt ,, _ ,, V-con (integer i)) ,, _ ,, V-con (integer j)) with i \u225f j\n... | no \u00acp = _ ,, inj\u2081 (V-con (bool false))\n... | yes p = _ ,, inj\u2081 (V-con (bool true))\nIBUILTIN concatenate \u03c3 ((tt ,, _ ,, V-con (bytestring b)) ,, _ ,, V-con (bytestring b')) = _ ,, inj\u2081 (V-con (bytestring (concat b b')))\nIBUILTIN takeByteString \u03c3 ((tt ,, _ ,, V-con (integer i)) ,, _ ,, V-con (bytestring b)) = _ ,, inj\u2081 (V-con (bytestring (take i b)))\nIBUILTIN dropByteString \u03c3 ((tt ,, _ ,, V-con (integer i)) ,, _ ,, V-con (bytestring b)) = _ ,, inj\u2081 (V-con (bytestring (drop i b)))\nIBUILTIN lessThanByteString \u03c3 ((tt ,, _ ,, V-con (bytestring b)) ,, _ ,, V-con (bytestring b')) = _ ,, inj\u2081 (V-con (bool (B< b b')))\nIBUILTIN greaterThanByteString \u03c3 ((tt ,, _ ,, V-con (bytestring b)) ,, _ ,, V-con (bytestring b')) = _ ,, inj\u2081 (V-con (bool (B> b b')))\nIBUILTIN sha2-256 \u03c3 (tt ,, _ ,, V-con (bytestring b)) = _ ,, inj\u2081 (V-con (bytestring (SHA2-256 b)))\nIBUILTIN sha3-256 \u03c3 (tt ,, _ ,, V-con (bytestring b)) = _ ,, inj\u2081 (V-con (bytestring (SHA3-256 b)))\nIBUILTIN verifySignature \u03c3 (((tt ,, _ ,, V-con (bytestring k)) ,, _ ,, V-con (bytestring d)) ,, _ ,, V-con (bytestring c)) with verifySig k d c\n... | just b = _ ,, inj\u2081 (V-con (bool b))\n... | nothing = _ ,, inj\u2082 E-error -- not sure what this is for\nIBUILTIN equalsByteString \u03c3 ((tt ,, _ ,, V-con (bytestring b)) ,, _ ,, V-con (bytestring b')) = _ ,, inj\u2081 (V-con (bool (equals b b')))\nIBUILTIN ifThenElse \u03c3 ((((tt ,, A) ,, _ ,, V-con (bool false)) ,, t) ,, f) =\n _ ,, inj\u2081 (proj\u2082 f)\nIBUILTIN ifThenElse \u03c3 ((((tt ,, A) ,, _ ,, V-con (bool true)) ,, t) ,, f) =\n _ ,, inj\u2081 (proj\u2082 t)\nIBUILTIN charToString \u03c3 (tt ,, _ ,, V-con (char c)) =\n _ ,, inj\u2081 (V-con (string (primStringFromList List.[ c ])))\nIBUILTIN append \u03c3 ((tt ,, _ ,, V-con (string s)) ,, _ ,, V-con (string s')) =\n _ ,, inj\u2081 (V-con (string (primStringAppend s s')))\nIBUILTIN trace \u03c3 _ = _ ,, inj\u2081 (V-con unit)\n\nIBUILTIN' : (b : Builtin)\n \u2192 let \u03a6 ,, \u0393 ,, C = ISIG b in\n \u2200{\u03a6'}{\u0393' : Ctx \u03a6'}\n \u2192 (p : \u03a6 \u2261 \u03a6')\n \u2192 (q : substEq Ctx p \u0393 \u2261 \u0393')\n (\u03c3 : SubNf \u03a6' \u2205)\n \u2192 (tel : ITel b \u0393' \u03c3)\n \u2192 (C' : \u03a6' \u22a2Nf\u22c6 *)\n \u2192 (r : substEq (_\u22a2Nf\u22c6 *) p C \u2261 C')\n -----------------------------\n \u2192 \u03a3 (\u2205 \u22a2 subNf \u03c3 C') \u03bb t \u2192 Value t \u228e Error t\n \nIBUILTIN' b refl refl \u03c3 tel _ refl = IBUILTIN b \u03c3 tel\n\\end{code}\n\n## Intrinsically Type Preserving Reduction\n\n\\begin{code}\ninfix 2 _\u2014\u2192_\n\ndata _\u2014\u2192_ : {A : \u2205 \u22a2Nf\u22c6 *} \u2192 (\u2205 \u22a2 A) \u2192 (\u2205 \u22a2 A) \u2192 Set where\n\n \u03be-\u00b7\u2081 : {A B : \u2205 \u22a2Nf\u22c6 *} {L L\u2032 : \u2205 \u22a2 A \u21d2 B} {M : \u2205 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : {A B : \u2205 \u22a2Nf\u22c6 *}{V : \u2205 \u22a2 A \u21d2 B} {M M\u2032 : \u2205 \u22a2 A}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n --------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03be-\u00b7\u22c6 : \u2200 {K}{B : \u2205 ,\u22c6 K \u22a2Nf\u22c6 *}{L L' : \u2205 \u22a2 \u03a0 B}{A}\n \u2192 L \u2014\u2192 L'\n -----------------\n \u2192 L \u00b7\u22c6 A \u2014\u2192 L' \u00b7\u22c6 A\n\n \u03b2-\u019b : {A B : \u2205 \u22a2Nf\u22c6 *}{N : \u2205 , A \u22a2 B} {V : \u2205 \u22a2 A}\n \u2192 Value V\n -------------------\n \u2192 (\u019b N) \u00b7 V \u2014\u2192 N [ V ]\n\n \u03b2-\u039b : \u2200 {K}{B : \u2205 ,\u22c6 K \u22a2Nf\u22c6 *}{N : \u2205 ,\u22c6 K \u22a2 B}{A}\n -------------------\n \u2192 (\u039b N) \u00b7\u22c6 A \u2014\u2192 N [ A ]\u22c6\n\n \u03b2-wrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 {M : \u2205 \u22a2 _}\n \u2192 Value M\n \u2192 unwrap (wrap A B M) \u2014\u2192 M\n\n \u03be-unwrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 {M M' : \u2205 \u22a2 \u03bc A B}\n \u2192 M \u2014\u2192 M'\n \u2192 unwrap M \u2014\u2192 unwrap M'\n \n \u03be-wrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 {M M' : \u2205 \u22a2 nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B)}\n \u2192 M \u2014\u2192 M'\n \u2192 wrap A B M \u2014\u2192 wrap A B M'\n\n E-\u00b7\u2082 : {A B : \u2205 \u22a2Nf\u22c6 *} {L : \u2205 \u22a2 A \u21d2 B}\n \u2192 Value L\n \u2192 L \u00b7 error A \u2014\u2192 error B\n E-\u00b7\u2081 : {A B : \u2205 \u22a2Nf\u22c6 *}{M : \u2205 \u22a2 A}\n \u2192 error (A \u21d2 B) \u00b7 M \u2014\u2192 error B\n E-\u00b7\u22c6 : \u2200{K}{B : \u2205 ,\u22c6 K \u22a2Nf\u22c6 *}{A : \u2205 \u22a2Nf\u22c6 K}\n \u2192 error (\u03a0 B) \u00b7\u22c6 A \u2014\u2192 error (B [ A ]Nf)\n E-unwrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 unwrap (error (\u03bc A B))\n \u2014\u2192 error (nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B))\n E-wrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 wrap A B (error _) \u2014\u2192 error (\u03bc A B) \n\n \u03b2-sbuiltin :\n (b : Builtin)\n \u2192 let \u03a6 ,, \u0393 ,, C = ISIG b in\n \u2200{\u03a6'}{\u0393' : Ctx \u03a6'}{A : \u03a6' \u22a2Nf\u22c6 *}\n \u2192 (\u03c3 : SubNf \u03a6' \u2205)\n \u2192 (p : \u03a6 \u2261 \u03a6')\n \u2192 (q : substEq Ctx p \u0393 \u2261 \u0393' , A)\n \u2192 (C' : \u03a6' \u22a2Nf\u22c6 *)\n \u2192 (r : substEq (_\u22a2Nf\u22c6 *) p C \u2261 C')\n \u2192 (t : \u2205 \u22a2 subNf \u03c3 A \u21d2 subNf \u03c3 C')\n \u2192 (u : \u2205 \u22a2 subNf \u03c3 A)\n \u2192 (tel : ITel b \u0393' \u03c3)\n \u2192 (v : Value u)\n -----------------------------\n \u2192 t \u00b7 u \u2014\u2192 proj\u2081 (IBUILTIN' b p q \u03c3 (tel ,, u ,, v) C' r)\n\n \u03b2-sbuiltin\u22c6 :\n (b : Builtin)\n \u2192 let \u03a6 ,, \u0393 ,, C = ISIG b in\n \u2200{\u03a6'}{\u0393' : Ctx \u03a6'}{K}{A : \u2205 \u22a2Nf\u22c6 K}\n \u2192 (\u03c3 : SubNf \u03a6' \u2205)\n \u2192 (p : \u03a6 \u2261 \u03a6' ,\u22c6 K)\n \u2192 (q : substEq Ctx p \u0393 \u2261 (\u0393' ,\u22c6 K))\n \u2192 (C' : \u03a6' ,\u22c6 K \u22a2Nf\u22c6 *)\n \u2192 (r : substEq (_\u22a2Nf\u22c6 *) p C \u2261 C')\n \u2192 (t : \u2205 \u22a2 subNf \u03c3 (\u03a0 C'))\n \u2192 (tel : ITel b \u0393' \u03c3)\n -----------------------------\n \u2192 t \u00b7\u22c6 A \u2014\u2192 conv\u22a2 refl (subNf-cons-[]Nf C') (proj\u2081 (IBUILTIN' b p q (subNf-cons \u03c3 A) (tel ,, A) C' r))\n\\end{code}\n\n\\begin{code}\ndata _\u2014\u21a0_ : {A A' : \u2205 \u22a2Nf\u22c6 *} \u2192 \u2205 \u22a2 A \u2192 \u2205 \u22a2 A' \u2192 Set\n where\n\n refl\u2014\u21a0 : \u2200{A}{M : \u2205 \u22a2 A}\n --------\n \u2192 M \u2014\u21a0 M\n\n trans\u2014\u21a0 : {A : \u2205 \u22a2Nf\u22c6 *}{M M' M'' : \u2205 \u22a2 A}\n \u2192 M \u2014\u2192 M'\n \u2192 M' \u2014\u21a0 M''\n ---------\n \u2192 M \u2014\u21a0 M''\n\n\n\\end{code}\n\n\\begin{code}\ndata Progress {A : \u2205 \u22a2Nf\u22c6 *} (M : \u2205 \u22a2 A) : Set where\n step : \u2200{N : \u2205 \u22a2 A}\n \u2192 M \u2014\u2192 N\n -------------\n \u2192 Progress M\n done :\n Value M\n ----------\n \u2192 Progress M\n\n error :\n Error M\n -------\n \u2192 Progress M\n\\end{code}\n\n\\begin{code}\nprogress-\u00b7V : {A B : \u2205 \u22a2Nf\u22c6 *}\n \u2192 {t : \u2205 \u22a2 A \u21d2 B} \u2192 Value t\n \u2192 {u : \u2205 \u22a2 A} \u2192 Progress u\n \u2192 Progress (t \u00b7 u)\nprogress-\u00b7V v (step q) = step (\u03be-\u00b7\u2082 v q)\nprogress-\u00b7V v (error E-error) = step (E-\u00b7\u2082 v)\nprogress-\u00b7V (V-\u019b t) (done w) = step (\u03b2-\u019b w)\nprogress-\u00b7V (V-I\u21d2 b p q r \u03c3 base vs t) (done v) =\n step (\u03b2-sbuiltin b \u03c3 p q _ r t (deval v) vs v)\n-- ^ we're done, call BUILTIN\nprogress-\u00b7V (V-I\u21d2 b p' q r \u03c3 (skip\u22c6 p) vs t) (done v) =\n done (V-I\u03a0 b p' q r \u03c3 p (vs ,, deval v ,, v) (t \u00b7 deval v))\nprogress-\u00b7V (V-I\u21d2 b p' q r \u03c3 (skip p) vs t) (done v) =\n done (V-I\u21d2 b p' q r \u03c3 p (vs ,, deval v ,, v) (t \u00b7 deval v))\n\nprogress-\u00b7 : {A B : \u2205 \u22a2Nf\u22c6 *}\n \u2192 {t : \u2205 \u22a2 A \u21d2 B} \u2192 Progress t\n \u2192 {u : \u2205 \u22a2 A} \u2192 Progress u\n \u2192 Progress (t \u00b7 u)\nprogress-\u00b7 (step p) q = step (\u03be-\u00b7\u2081 p)\nprogress-\u00b7 (done v) q = progress-\u00b7V v q\nprogress-\u00b7 (error E-error) q = step E-\u00b7\u2081\n\nconvValue : \u2200{A A'}{t : \u2205 \u22a2 A}(p : A \u2261 A') \u2192 Value (conv\u22a2 refl p t) \u2192 Value t\nconvValue refl v = v\n\nival : \u2200 b \u2192 Value (ibuiltin b)\nival addInteger = V-I\u21d2 addInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG addInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG addInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin addInteger)\nival subtractInteger = V-I\u21d2 subtractInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG subtractInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG subtractInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin subtractInteger)\nival multiplyInteger = V-I\u21d2 multiplyInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG multiplyInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG multiplyInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin multiplyInteger)\nival divideInteger = V-I\u21d2 divideInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG divideInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG divideInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin divideInteger)\nival quotientInteger = V-I\u21d2 quotientInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG quotientInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG quotientInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin quotientInteger)\nival remainderInteger = V-I\u21d2 remainderInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG remainderInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG remainderInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin remainderInteger)\nival modInteger = V-I\u21d2 modInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG modInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG modInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin modInteger)\nival lessThanInteger = V-I\u21d2 lessThanInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG lessThanInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG lessThanInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin lessThanInteger)\nival lessThanEqualsInteger = V-I\u21d2 lessThanEqualsInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG lessThanEqualsInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG lessThanEqualsInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin lessThanEqualsInteger)\nival greaterThanInteger = V-I\u21d2 greaterThanInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG greaterThanInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG greaterThanInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin greaterThanInteger)\nival greaterThanEqualsInteger = V-I\u21d2 greaterThanEqualsInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG greaterThanEqualsInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG greaterThanEqualsInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin greaterThanEqualsInteger)\nival equalsInteger = V-I\u21d2 equalsInteger {\u0393 = proj\u2081 (proj\u2082 (ISIG equalsInteger))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG equalsInteger))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin equalsInteger)\nival concatenate = V-I\u21d2 concatenate {\u0393 = proj\u2081 (proj\u2082 (ISIG concatenate))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG concatenate))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin concatenate)\nival takeByteString = V-I\u21d2 takeByteString {\u0393 = proj\u2081 (proj\u2082 (ISIG takeByteString))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG takeByteString))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin takeByteString)\nival dropByteString = V-I\u21d2 dropByteString {\u0393 = proj\u2081 (proj\u2082 (ISIG dropByteString))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG dropByteString))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin dropByteString)\nival lessThanByteString = V-I\u21d2 lessThanByteString {\u0393 = proj\u2081 (proj\u2082 (ISIG lessThanByteString))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG lessThanByteString))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin lessThanByteString)\nival greaterThanByteString = V-I\u21d2 greaterThanByteString {\u0393 = proj\u2081 (proj\u2082 (ISIG greaterThanByteString))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG greaterThanByteString))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin greaterThanByteString)\nival sha2-256 = V-I\u21d2 sha2-256 {\u0393 = proj\u2081 (proj\u2082 (ISIG sha2-256))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG sha2-256))} refl refl refl (\u03bb()) base tt (ibuiltin sha2-256)\nival sha3-256 = V-I\u21d2 sha3-256 {\u0393 = proj\u2081 (proj\u2082 (ISIG sha3-256))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG sha3-256))} refl refl refl (\u03bb()) base tt (ibuiltin sha3-256)\nival verifySignature = V-I\u21d2 verifySignature {\u0393 = proj\u2081 (proj\u2082 (ISIG verifySignature))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG verifySignature))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip (skip base))) tt (ibuiltin verifySignature)\nival equalsByteString = V-I\u21d2 equalsByteString {\u0393 = proj\u2081 (proj\u2082 (ISIG equalsByteString))}{\u0394 = \u2205}{C = proj\u2082 (proj\u2082 (ISIG equalsByteString))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin equalsByteString)\nival ifThenElse = V-I\u03a0 ifThenElse {\u0393 = proj\u2081 (proj\u2082 (ISIG ifThenElse))}{C = proj\u2082 (proj\u2082 (ISIG ifThenElse))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip (skip (skip base)))) tt (ibuiltin ifThenElse)\nival charToString = V-I\u21d2 charToString {\u0393 = proj\u2081 (proj\u2082 (ISIG charToString))}{C = proj\u2082 (proj\u2082 (ISIG charToString))} refl refl refl (\u03bb()) base tt (ibuiltin charToString)\nival append = V-I\u21d2 append {\u0393 = proj\u2081 (proj\u2082 (ISIG append))}{C = proj\u2082 (proj\u2082 (ISIG append))} refl refl refl (\u03bb()) (\u2264Cto\u2264C' (skip base)) tt (ibuiltin append)\nival trace = V-I\u21d2 trace {\u0393 = proj\u2081 (proj\u2082 (ISIG trace))}{C = proj\u2082 (proj\u2082 (ISIG trace))} refl refl refl (\u03bb()) base tt (ibuiltin trace)\n\nprogress-\u00b7\u22c6 : \u2200{K B}{t : \u2205 \u22a2 \u03a0 B} \u2192 Progress t \u2192 (A : \u2205 \u22a2Nf\u22c6 K)\n \u2192 Progress (t \u00b7\u22c6 A)\nprogress-\u00b7\u22c6 (step p) A = step (\u03be-\u00b7\u22c6 p)\nprogress-\u00b7\u22c6 (done (V-\u039b t)) A = step \u03b2-\u039b\nprogress-\u00b7\u22c6 (error E-error) A = step E-\u00b7\u22c6\nprogress-\u00b7\u22c6 (done (V-I\u03a0 b {C = C} p' q r \u03c3 (skip\u22c6 p) vs t)) A = done (convValue (\u03a0lem p A C \u03c3) (V-I\u03a0 b {C = C} p' q r (subNf-cons \u03c3 A) p (vs ,, A) (conv\u22a2 refl (\u03a0lem p A C \u03c3) (t \u00b7\u22c6 A))) )\nprogress-\u00b7\u22c6 (done (V-I\u03a0 b {C = C} p' q r \u03c3 (skip p) vs t)) A = done (convValue (\u21d2lem p \u03c3 C) (V-I\u21d2 b p' q r (subNf-cons \u03c3 A) p (vs ,, A) (conv\u22a2 refl (\u21d2lem p \u03c3 C) (t \u00b7\u22c6 A) )))\nprogress-\u00b7\u22c6 (done (V-I\u03a0 b p q r \u03c3 base vs t)) A = step (\u03b2-sbuiltin\u22c6 b \u03c3 p q _ r t vs)\n-- ^ it's the last one, call BUILTIN\n\nprogress-unwrap : \u2200{K}{A}{B : \u2205 \u22a2Nf\u22c6 K}{t : \u2205 \u22a2 \u03bc A B}\n \u2192 Progress t \u2192 Progress (unwrap t)\nprogress-unwrap (step q) = step (\u03be-unwrap q)\nprogress-unwrap (done (V-wrap v)) = step (\u03b2-wrap v)\nprogress-unwrap {A = A} (error E-error) =\n step (E-unwrap {A = A})\n\nprogress : {A : \u2205 \u22a2Nf\u22c6 *} \u2192 (M : \u2205 \u22a2 A) \u2192 Progress M\nprogress-wrap : \u2200{K}\n \u2192 {A : \u2205 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u2205 \u22a2Nf\u22c6 K}\n \u2192 {M : \u2205 \u22a2 nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B)}\n \u2192 Progress M \u2192 Progress (wrap A B M)\nprogress-wrap (step p) = step (\u03be-wrap p)\nprogress-wrap (done v) = done (V-wrap v)\nprogress-wrap (error E-error) = step E-wrap\nprogress (\u019b M) = done (V-\u019b M)\nprogress (M \u00b7 N) = progress-\u00b7 (progress M) (progress N)\nprogress (\u039b M) = done (V-\u039b M)\nprogress (M \u00b7\u22c6 A) = progress-\u00b7\u22c6 (progress M) A\nprogress (wrap A B M) = progress-wrap (progress M)\nprogress (unwrap M) = progress-unwrap (progress M)\nprogress (con c) = done (V-con c)\nprogress (ibuiltin b) = done (ival b)\nprogress (error A) = error E-error\n\nopen import Data.Nat\nprogressor : \u2115 \u2192 \u2200{A} \u2192 (t : \u2205 \u22a2 A) \u2192 Either RuntimeError (Maybe (\u2205 \u22a2 A))\nprogressor zero t = inj\u2081 gasError\nprogressor (suc n) t with progress t\n... | step {N = t'} _ = progressor n t'\n... | done v = inj\u2082 (just (deval v))\n... | error _ = inj\u2082 nothing -- should this be an runtime error?\n--\n\nopen import Data.Empty\n\n-- progress is disjoint:\n\n{-\n-- a value cannot make progress\nval-red : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 \u03c3} \u2192 Value t \u2192 \u00ac (\u03a3 (\u2205 \u22a2 \u03c3) (t \u2014\u2192_))\nval-red (V-wrap p) (.(wrap _ _ _) ,, \u03be-wrap q) = val-red p (_ ,, q)\nval-red (V-wrap v) (.(error (\u03bc _ _)) ,, E-wrap) = {!val-err v!}\n\nval-red (V-I\u21d2 b p\u2081 q r \u03c3 p' x t) (t' ,, p) = {!!}\n-- this is impossible because p' : \u0394 , A \u2264C' \u0393 and we can only compute\n-- if we have a full telescope... but, t can be anything here,\n-- perhaps it's better to match on the rules instead?\nval-red (V-I\u03a0 b p\u2081 q r \u03c3 p\u2082 x _) (t ,, p) = {!!}\n\nvalT-redT : \u2200 {\u0394}{\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u2205 \u22a2Nf\u22c6 K}{As : List (\u0394 \u22a2Nf\u22c6 *)}\n \u2192 {ts : Tel \u2205 \u0394 \u03c3 As} \u2192 VTel \u0394 \u03c3 As ts \u2192 \u00ac \u03a3 (Tel \u2205 \u0394 \u03c3 As) (ts \u2014\u2192T_)\nvalT-redT (v ,, vs) (.(_ \u2237 _) ,, here p) = val-red v (_ ,, p)\nvalT-redT (v ,, vs) (.(_ \u2237 _) ,, there w p) = valT-redT vs (_ ,, p)\n\n-- a value cannot be an error\nval-err : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 \u03c3} \u2192 Value t \u2192 \u00ac (Error t)\nval-err p E-error = {!!}\n\nvalT-errT : \u2200 {\u0394}{\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u2205 \u22a2Nf\u22c6 K}{As : List (\u0394 \u22a2Nf\u22c6 *)}\n \u2192 {ts : Tel \u2205 \u0394 \u03c3 As} \u2192 VTel \u0394 \u03c3 As ts \u2192 \u00ac (Any Error ts)\nvalT-errT (v ,, vs) (here p) = val-err v p\nvalT-errT (v ,, vs) (there w p) = valT-errT vs p\n\n-- an error cannot make progress\nred-err : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 \u03c3} \u2192 \u03a3 (\u2205 \u22a2 \u03c3) (t \u2014\u2192_) \u2192 \u00ac (Error t)\nred-err () E-error\n\nredT-errT : \u2200 {\u0394}{\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u2205 \u22a2Nf\u22c6 K}{As : List (\u0394 \u22a2Nf\u22c6 *)}\n \u2192 {ts : Tel \u2205 \u0394 \u03c3 As} \u2192 \u03a3 (Tel \u2205 \u0394 \u03c3 As) (ts \u2014\u2192T_) \u2192 \u00ac (Any Error ts)\nredT-errT (.(_ \u2237 _) ,, here p) (here q) = red-err (_ ,, p) q\nredT-errT (.(_ \u2237 _) ,, there v p) (here q) = val-err v q\nredT-errT (.(_ \u2237 _) ,, here p) (there w q) = val-red w (_ ,, p)\nredT-errT (.(_ \u2237 _) ,, there v p) (there w q) = redT-errT (_ ,, p) q\n\n-- values are unique for a term\nvalUniq : \u2200 {A : \u2205 \u22a2Nf\u22c6 *}(t : \u2205 \u22a2 A) \u2192 (v v' : Value t) \u2192 v \u2261 v'\nvalUniq .(\u019b _) (V-\u019b _) (V-\u019b _) = refl\nvalUniq .(\u039b _) (V-\u039b _) (V-\u039b _) = refl\nvalUniq .(wrap _ _ _) (V-wrap v) (V-wrap v') = cong V-wrap (valUniq _ v v')\nvalUniq .(con cn) (V-con cn) (V-con .cn) = refl\n--valUniq _ (V-pbuiltin\u22c6 _ _ _ _) (V-pbuiltin\u22c6 _ _ _ _) = refl\n--valUniq _ (V-pbuiltin _ _ _ _ _ _ ) (V-pbuiltin _ _ _ _ _ _ ) = refl\nvalUniq _ _ _ = {!!}\n\n-- telescopes of values are unique for that telescope\nvTelUniq : \u2200 \u0394 \u2192 (\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u2205 \u22a2Nf\u22c6 K)(As : List (\u0394 \u22a2Nf\u22c6 *))\n \u2192 (tel : Tel \u2205 \u0394 \u03c3 As)\n \u2192 (vtel vtel' : VTel \u0394 \u03c3 As tel)\n \u2192 vtel \u2261 vtel'\n<<<<<<< HEAD\nvTelUniq \u0394 \u03c3 [] [] vtel vtel' = refl\nvTelUniq \u0394 \u03c3 (A \u2237 As) (t \u2237 tel) (v ,, vtel) (v' ,, vtel') =\n cong\u2082 _,,_ (valUniq t v v') (vTelUniq \u0394 \u03c3 As tel vtel vtel') \n=======\nvTelUniq \u0393 \u0394 \u03c3 [] [] vtel vtel' = refl\nvTelUniq \u0393 \u0394 \u03c3 (A \u2237 As) (t \u2237 tel) (v ,, vtel) (v' ,, vtel') =\n cong\u2082 _,,_ (valUniq t v v') (vTelUniq \u0393 \u0394 \u03c3 As tel vtel vtel')\n>>>>>>> 3d0fa53911081de50fa6a795563663300ddc8952\n\n-- exclusive or\n_xor_ : Set \u2192 Set \u2192 Set\nA xor B = (A \u228e B) \u00d7 \u00ac (A \u00d7 B)\n\ninfixr 2 _xor_\n\n-- a term cannot make progress and be a value\n\nnotboth : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 \u03c3} \u2192 \u00ac (Value t \u00d7 \u03a3 (\u2205 \u22a2 \u03c3) (t \u2014\u2192_))\nnotboth (v ,, p) = val-red v p\n\n-- term cannot make progress and be error\n\nnotboth' : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 \u03c3} \u2192 \u00ac (\u03a3 (\u2205 \u22a2 \u03c3) (t \u2014\u2192_) \u00d7 Error t)\nnotboth' (p ,, e) = red-err p e\n\n-- armed with this, we can upgrade progress to an xor\n\nprogress-xor : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}(t : \u2205 \u22a2 \u03c3)\n \u2192 Value t xor (\u03a3 (\u2205 \u22a2 \u03c3) (t \u2014\u2192_)) xor Error t\nprogress-xor t with progress t\nprogress-xor t | step p = (inj\u2082 ((inj\u2081 (_ ,, p)) ,, \u03bb{(p ,, e) \u2192 red-err p e})) ,, \u03bb { (v ,, inj\u2081 p ,, q) \u2192 val-red v p ; (v ,, inj\u2082 e ,, q) \u2192 val-err v e}\nprogress-xor t | done v = (inj\u2081 v) ,, (\u03bb { (v' ,, inj\u2081 p ,, q) \u2192 val-red v p ; (v' ,, inj\u2082 e ,, q) \u2192 val-err v e})\nprogress-xor t | error e = (inj\u2082 ((inj\u2082 e) ,, (\u03bb { (p ,, e) \u2192 red-err p e}))) ,, \u03bb { (v ,, q) \u2192 val-err v e }\n-- the reduction rules are deterministic\ndet : \u2200{\u03c3 : \u2205 \u22a2Nf\u22c6 *}{t t' t'' : \u2205 \u22a2 \u03c3}\n \u2192 (p : t \u2014\u2192 t')(q : t \u2014\u2192 t'') \u2192 t' \u2261 t''\ndetT : \u2200{\u0394}{\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u2205 \u22a2Nf\u22c6 K}{As}{ts ts' ts'' : Tel \u2205 \u0394 \u03c3 As}\n \u2192 (p : ts \u2014\u2192T ts')(q : ts \u2014\u2192T ts'') \u2192 ts' \u2261 ts''\n\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2081 q) = cong (_\u00b7 _) (det p q)\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val-red w (_ ,, p))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2081 q) = \u22a5-elim (val-red v (_ ,, q))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2082 w q) = cong (_ \u00b7_) (det p q)\ndet (\u03be-\u00b7\u2082 v p) (\u03b2-\u019b w) = \u22a5-elim (val-red w (_ ,, p))\ndet (\u03be-\u00b7\u22c6 p) (\u03be-\u00b7\u22c6 q) = cong (_\u00b7\u22c6 _) (det p q)\ndet (\u03b2-\u019b v) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val-red v (_ ,, q))\ndet (\u03b2-\u019b v) (\u03b2-\u019b w) = refl\ndet \u03b2-\u039b \u03b2-\u039b = refl\ndet (\u03b2-wrap p) (\u03b2-wrap q) = refl\ndet (\u03b2-wrap p) (\u03be-unwrap q) = \u22a5-elim (val-red (V-wrap p) (_ ,, q))\ndet (\u03be-unwrap p) (\u03b2-wrap q) = \u22a5-elim (val-red (V-wrap q) (_ ,, p))\ndet (\u03be-unwrap p) (\u03be-unwrap q) = cong unwrap (det p q)\ndet (\u03be-wrap p) (\u03be-wrap q) = cong (wrap _ _) (det p q)\ndet (\u03b2-builtin bn \u03c3 ts vs) (\u03b2-builtin .bn .\u03c3 .ts ws) =\n cong (BUILTIN bn \u03c3 ts) (vTelUniq _ \u03c3 _ ts vs ws)\ndet (\u03b2-builtin bn \u03c3 ts vs) (\u03be-builtin .bn .\u03c3 p) =\n \u22a5-elim (valT-redT vs (_ ,, p))\ndet (\u03be-builtin bn \u03c3 p) (\u03b2-builtin .bn .\u03c3 ts vs) =\n \u22a5-elim (valT-redT vs (_ ,, p))\ndet (\u03be-builtin bn \u03c3 p) (\u03be-builtin .bn .\u03c3 p') = cong (builtin bn \u03c3) (detT p p')\ndet (\u03b2-builtin _ _ _ vs) (E-builtin _ _ _ p) = \u22a5-elim (valT-errT vs p)\ndet (\u03be-builtin _ _ p) (E-builtin _ _ _ q) = \u22a5-elim (redT-errT (_ ,, p) q)\ndet (E-builtin _ _ _ _) (E-builtin _ _ _ _) = refl\ndet (E-builtin bn \u03c3 ts p) (\u03b2-builtin .bn .\u03c3 .ts vs) = \u22a5-elim (valT-errT vs p)\ndet (E-builtin bn \u03c3 ts p) (\u03be-builtin .bn .\u03c3 q) = \u22a5-elim (redT-errT (_ ,, q) p)\ndet E-\u00b7\u2081 (\u03be-\u00b7\u2081 p) = {!!}\ndet (E-\u00b7\u2082 v) (\u03be-\u00b7\u2081 p) = \u22a5-elim (val-red v (_ ,, p))\ndet (E-\u00b7\u2082 v) (E-\u00b7\u2082 w) = refl\ndet (E-\u00b7\u2082 p) E-\u00b7\u2081 = {!!}\ndet (\u03be-\u00b7\u2081 p) (E-\u00b7\u2082 v) = \u22a5-elim (val-red v (_ ,, p))\ndet E-\u00b7\u2081 (E-\u00b7\u2082 p) = {!!}\ndet E-\u00b7\u2081 E-\u00b7\u2081 = refl\ndet E-\u00b7\u22c6 E-\u00b7\u22c6 = refl\ndet E-unwrap E-unwrap = refl\ndet E-wrap E-wrap = refl\ndet _ _ = {!!}\n\ndetT (here p) (there w q) = \u22a5-elim (val-red w (_ ,, p))\ndetT (there v p) (here q) = \u22a5-elim (val-red v (_ ,, q))\ndetT (there v p) (there w q) = cong (_ \u2237_) (detT p q)\ndetT (here p) (here q) = cong (_\u2237 _) (det p q)\n-}\n\n","avg_line_length":44.0415944541,"max_line_length":253,"alphanum_fraction":0.5341964426} +{"size":4356,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule Declarative where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nopen import Type.RenamingSubstitution\nopen import Type.Equality\nopen import Builtin\n\n-- these things should perhaps be rexported...\nopen import Builtin.Constant.Type\nopen import Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2\u22c6_ ` con\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\n\nopen import Relation.Binary.PropositionalEquality hiding ([_]; subst)\nopen import Agda.Builtin.Int\nopen import Data.Integer renaming (_*_ to _**_)\nopen import Data.Empty\nopen import Data.Product hiding (_,_)\nopen import Relation.Binary hiding (_\u21d2_)\nimport Data.Nat as \u2115\nopen import Data.Unit hiding (_\u2264_)\nopen import Data.Vec hiding ([_]; take; drop)\nopen import Data.List hiding ([_]; length; take; drop)\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Nat hiding (_^_; _\u2264_; _<_; _>_; _\u2265_)\nopen import Function hiding (_\u220b_)\nimport Data.Bool as Bool\nopen import Data.String\n\\end{code}\n\n## Fixity declarations\n\nTo begin, we get all our infix declarations out of the way.\nWe list separately operators for judgements, types, and terms.\n\\begin{code}\ninfix 4 _\u220b_\ninfix 3 _\u22a2_\ninfixl 5 _,_\n\\end{code}\n\n## Contexts and erasure\n\nWe need to mutually define contexts and their\nerasure to type contexts.\n\\begin{code}\n--data Ctx : Set\n--\u2225_\u2225 : Ctx \u2192 Ctx\u22c6\n\\end{code}\n\nA context is either empty, or extends a context by\na type variable of a given kind, or extends a context\nby a variable of a given type.\n\\begin{code}\ndata Ctx : Ctx\u22c6 \u2192 Set where\n \u2205 : Ctx \u2205\n _,\u22c6_ : \u2200{\u0393\u22c6} \u2192 Ctx \u0393\u22c6 \u2192 (J : Kind) \u2192 Ctx (\u0393\u22c6 ,\u22c6 J)\n _,_ : \u2200{\u0393\u22c6}(\u0393 : Ctx \u0393\u22c6) \u2192 \u0393\u22c6 \u22a2\u22c6 * \u2192 Ctx \u0393\u22c6\n\\end{code}\nLet `\u0393` range over contexts. In the last rule,\nthe type is indexed by the erasure of the previous\ncontext to a type context and a kind.\n\nThe erasure of a context is a type context.\n\\begin{code}\n--\u2225 \u2205 \u2225 = \u2205\n--\u2225 \u0393 ,\u22c6 J \u2225 = \u2225 \u0393 \u2225 ,\u22c6 J\n--\u2225 \u0393 , A \u2225 = \u2225 \u0393 \u2225\n\\end{code}\n\n## Variables\n\nA variable is indexed by its context and type. Notice there is only\none Z as a type variable cannot be a term.\n\\begin{code}\ndata _\u220b_ : \u2200{\u0393\u22c6}(\u0393 : Ctx \u0393\u22c6) \u2192 \u0393\u22c6 \u22a2\u22c6 * \u2192 Set where\n Z : \u2200 {\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}\n ----------\n \u2192 \u0393 , A \u220b A\n\n S : \u2200 {\u03a6 \u0393} {A B : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u220b A\n ----------\n \u2192 \u0393 , B \u220b A\n\n T : \u2200 {\u03a6 \u0393 K} {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u220b A\n -------------------\n \u2192 \u0393 ,\u22c6 K \u220b weaken A\n\\end{code}\nLet `x`, `y` range over variables.\n\n## Terms\n\nA term is indexed over by its context and type. A term is a variable,\nan abstraction, an application, a type abstraction, or a type\napplication.\n\n\\begin{code}\ndata Tel {\u03a6} \u0393 \u0394 (\u03c3 : Sub \u0394 \u03a6) : List (\u0394 \u22a2\u22c6 *) \u2192 Set\n \ndata _\u22a2_ {\u03a6} (\u0393 : Ctx \u03a6) : \u03a6 \u22a2\u22c6 * \u2192 Set where\n\n ` : {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u220b A\n ------\n \u2192 \u0393 \u22a2 A\n\n \u019b : \u2200 {A B}\n \u2192 \u0393 , A \u22a2 B\n -----------\n \u2192 \u0393 \u22a2 A \u21d2 B\n\n _\u00b7_ : \u2200 {A B}\n \u2192 \u0393 \u22a2 A \u21d2 B\n \u2192 \u0393 \u22a2 A\n -----------\n \u2192 \u0393 \u22a2 B\n\n \u039b : \u2200 {K}{B : \u03a6 ,\u22c6 K \u22a2\u22c6 *}\n \u2192 \u0393 ,\u22c6 K \u22a2 B\n ----------\n \u2192 \u0393 \u22a2 \u03a0 B\n\n _\u00b7\u22c6_ : \u2200 {K B}\n \u2192 \u0393 \u22a2 \u03a0 B\n \u2192 (A : \u03a6 \u22a2\u22c6 K)\n ---------------\n \u2192 \u0393 \u22a2 B [ A ]\n\n wrap1 : \u2200{K}\n \u2192 (pat : \u03a6 \u22a2\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *)\n \u2192 (arg : \u03a6 \u22a2\u22c6 K)\n \u2192 (term : \u0393 \u22a2 pat \u00b7 (\u03bc1 \u00b7 pat) \u00b7 arg)\n \u2192 \u0393 \u22a2 \u03bc1 \u00b7 pat \u00b7 arg\n\n unwrap1 : \u2200{K}\n \u2192 {pat : \u03a6 \u22a2\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {arg : \u03a6 \u22a2\u22c6 K}\n \u2192 (term : \u0393 \u22a2 \u03bc1 \u00b7 pat \u00b7 arg)\n \u2192 \u0393 \u22a2 pat \u00b7 (\u03bc1 \u00b7 pat) \u00b7 arg\n \n conv : {A B : \u03a6 \u22a2\u22c6 *}\n \u2192 A \u2261\u03b2 B\n \u2192 \u0393 \u22a2 A\n -----\n \u2192 \u0393 \u22a2 B\n\n con : \u2200{tcn}\n \u2192 TermCon {\u03a6} (con tcn)\n -------------------\n \u2192 \u0393 \u22a2 con tcn\n\n builtin : \n (bn : Builtin)\n \u2192 let \u0394 ,, As ,, C = SIG bn in\n (\u03c3 : Sub \u0394 \u03a6) -- substitutes for new vars introduced by the Sig\n \u2192 Tel \u0393 \u0394 \u03c3 As -- a telescope of terms M_i typed in subst \u03c3\n -----------------------------\n \u2192 \u0393 \u22a2 subst \u03c3 C\n\n error : (A : \u03a6 \u22a2\u22c6 *) \u2192 \u0393 \u22a2 A\n\ndata Tel {\u03a6} \u0393 \u0394 \u03c3 where\n [] : Tel \u0393 \u0394 \u03c3 []\n _\u2237_ : \u2200{A As} \u2192 \u0393 \u22a2 subst \u03c3 A \u2192 Tel \u0393 \u0394 \u03c3 As \u2192 Tel \u0393 \u0394 \u03c3 (A \u2237 As)\n\\end{code}\n\n\\begin{code}\nconv\u220b : \u2200 {\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u2261 \u0393'\n \u2192 A \u2261 A'\n \u2192 (\u0393 \u220b A)\n \u2192 \u0393' \u220b A'\nconv\u220b refl refl t = t\n\nconvTel : \u2200 {\u03a6 \u03a8}{\u0393 \u0393' : Ctx \u03a6}\n \u2192 \u0393 \u2261 \u0393'\n \u2192 (\u03c3 : \u2200{J} \u2192 \u03a8 \u220b\u22c6 J \u2192 \u03a6 \u22a2\u22c6 J)\n \u2192 (As : List (\u03a8 \u22a2\u22c6 *))\n \u2192 Tel \u0393 \u03a8 \u03c3 As \u2192 Tel \u0393' \u03a8 \u03c3 As\n\nconv\u22a2 : \u2200 {\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u2261 \u0393'\n \u2192 A \u2261 A'\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393' \u22a2 A'\nconv\u22a2 refl refl t = t\n\nconvTel p \u03c3 [] [] = []\nconvTel p \u03c3 (A \u2237 As) (t \u2237 ts) = conv\u22a2 p refl t \u2237 convTel p \u03c3 As ts\n\\end{code}\n","avg_line_length":22.1116751269,"max_line_length":70,"alphanum_fraction":0.5151515152} +{"size":8462,"ext":"lagda","lang":"Literate Agda","max_stars_count":135.0,"content":"\\begin{code}\nmodule Algorithmic where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Function hiding (_\u220b_)\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List hiding ([_])\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Data.Unit\nopen import Data.Sum\n\nopen import Utils hiding (TermCon)\nopen import Type\nopen import Type.BetaNormal\nimport Type.RenamingSubstitution as \u22c6\nopen import Type.BetaNBE\nopen import Type.BetaNBE.RenamingSubstitution renaming (_[_]Nf to _[_])\nopen import Builtin\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con\nopen import Builtin.Constant.Type Ctx\u22c6 (_\u22a2Nf\u22c6 *)\n\\end{code}\n\n## Fixity declarations\n\nTo begin, we get all our infix declarations out of the way.\nWe list separately operators for judgements, types, and terms.\n\\begin{code}\ninfix 4 _\u220b_\ninfix 4 _\u22a2_\ninfixl 5 _,_\n\\end{code}\n\n## Contexts and erasure\n\nWe need to mutually define contexts and their\nerasure to type contexts.\n\\begin{code}\n--data Ctx : Set\n--\u2225_\u2225 : Ctx \u2192 Ctx\u22c6\n\\end{code}\n\nA context is either empty, or extends a context by\na type variable of a given kind, or extends a context\nby a variable of a given type.\n\\begin{code}\ndata Ctx : Ctx\u22c6 \u2192 Set where\n \u2205 : Ctx \u2205\n _,\u22c6_ : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 (J : Kind) \u2192 Ctx (\u03a6 ,\u22c6 J)\n _,_ : \u2200 {\u03a6} (\u0393 : Ctx \u03a6) \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 Ctx \u03a6\n\\end{code}\nLet `\u0393` range over contexts. In the last rule,\nthe type is indexed by the erasure of the previous\ncontext to a type context and a kind.\n\nThe erasure of a context is a type context.\n\\begin{code}\n--\u2225 \u2205 \u2225 = \u2205\n--\u2225 \u0393 ,\u22c6 J \u2225 = \u2225 \u0393 \u2225 ,\u22c6 J\n--\u2225 \u0393 , A \u2225 = \u2225 \u0393 \u2225\n\\end{code}\n\n## Variables\n\nA variable is indexed by its context and type.\n\\begin{code}\nopen import Type.BetaNormal.Equality\ndata _\u220b_ : \u2200 {\u03a6} (\u0393 : Ctx \u03a6) \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 Set where\n\n Z : \u2200 {\u03a6 \u0393} {A : \u03a6 \u22a2Nf\u22c6 *}\n ----------\n \u2192 \u0393 , A \u220b A\n\n S : \u2200 {\u03a6 \u0393} {A : \u03a6 \u22a2Nf\u22c6 *} {B : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u220b A\n ----------\n \u2192 \u0393 , B \u220b A\n\n T : \u2200 {\u03a6 \u0393 K}{A : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u220b A\n -------------------\n \u2192 \u0393 ,\u22c6 K \u220b weakenNf A\n\\end{code}\nLet `x`, `y` range over variables.\n\n## Terms\n\nA term is indexed over by its context and type. A term is a variable,\nan abstraction, an application, a type abstraction, or a type\napplication.\n\\begin{code}\nsig : Builtin \u2192 \u03a3 Ctx\u22c6 \u03bb \u03a6 \u2192 Ctx \u03a6 \u00d7 \u03a6 \u22a2Nf\u22c6 *\nsig ifThenElse = _ ,, \u2205 ,\u22c6 * , con bool , ne (` Z) , ne (` Z) ,, ne (` Z)\nsig addInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig subtractInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig multiplyInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig divideInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig quotientInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig remainderInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig modInteger = _ ,, \u2205 , con integer , con integer ,, con integer\nsig lessThanInteger = _ ,, \u2205 , con integer , con integer ,, con bool\nsig lessThanEqualsInteger = _ ,, \u2205 , con integer , con integer ,, con bool\nsig equalsInteger = _ ,, \u2205 , con integer , con integer ,, con bool\nsig appendByteString = _ ,, \u2205 , con bytestring , con bytestring ,, con bytestring\nsig lessThanByteString = _ ,, \u2205 , con bytestring , con bytestring ,, con bool\nsig lessThanEqualsByteString = _ ,, \u2205 , con bytestring , con bytestring ,, con bool\nsig sha2-256 = _ ,, \u2205 , con bytestring ,, con bytestring\nsig sha3-256 = _ ,, \u2205 , con bytestring ,, con bytestring\nsig verifyEd25519Signature = _ ,, \u2205 , con bytestring , con bytestring , con bytestring ,, con bool\nsig equalsByteString = _ ,, \u2205 , con bytestring , con bytestring ,, con bool\nsig appendString = _ ,, \u2205 , con string , con string ,, con string\nsig trace = _ ,, \u2205 ,\u22c6 * , con string , ne (` Z) ,, ne (` Z)\nsig equalsString = _ ,, \u2205 , con string , con string ,, con bool\nsig encodeUtf8 = _ ,, \u2205 , con string ,, con bytestring\nsig decodeUtf8 = _ ,, \u2205 , con bytestring ,, con string\nsig fstPair =\n _ ,, \u2205 ,\u22c6 * ,\u22c6 * , con (pair (ne (` (S Z))) (ne (` Z))) ,, ne (` (S Z))\nsig sndPair = \n _ ,, \u2205 ,\u22c6 * ,\u22c6 * , con (pair (ne (` (S Z))) (ne (` Z))) ,, ne (` Z)\nsig nullList = _ ,, \u2205 ,\u22c6 * , con (list (ne (` Z))) ,, con bool\nsig headList = _ ,, \u2205 ,\u22c6 * , con (list (ne (` Z))) ,, ne (` Z)\nsig tailList = _ ,, \u2205 ,\u22c6 * , con (list (ne (` Z))) ,, con (list (ne (` Z)))\nsig chooseList =\n _\n ,,\n \u2205 ,\u22c6 * ,\u22c6 * , ne (` (S Z)) , ne (` (S Z)) , con (list (ne (` Z)))\n ,,\n ne (` (S Z)) \nsig constrData = _ ,, \u2205 , con integer , con (list (con Data)) ,, con Data\nsig mapData = _ ,, \u2205 , con (pair (con Data) (con Data)) ,, con Data\nsig listData = _ ,, \u2205 , con (list (con Data)) ,, con Data\nsig iData = _ ,, \u2205 , con integer ,, con Data\nsig bData = _ ,, \u2205 , con bytestring ,, con Data\nsig unConstrData =\n _ ,, \u2205 , con Data ,, con (pair (con integer) (con (list (con Data))))\nsig unMapData = _ ,, \u2205 , con Data ,, con (pair (con Data) (con Data))\nsig unListData = _ ,, \u2205 , con Data ,, con (list (con Data))\nsig unIData = _ ,, \u2205 , con Data ,, con integer\nsig unBData = _ ,, \u2205 , con Data ,, con bytestring\nsig equalsData = _ ,, \u2205 , con Data , con Data ,, con bool\nsig serialiseData = _ ,, \u2205 , con Data ,, con bytestring\nsig chooseData =\n _\n ,,\n \u2205 ,\u22c6 * , ne (` Z) , ne (` Z) , ne (` Z) , ne (` Z) , ne (` Z) , con Data\n ,,\n ne (` Z)\nsig chooseUnit = _ ,, \u2205 ,\u22c6 * , ne (` Z) , con unit ,, ne (` Z)\nsig mkPairData =\n _ ,, \u2205 , con Data , con Data ,, con (pair (con Data) (con Data)) \nsig mkNilData = _ ,, \u2205 , con unit ,, con (list (con Data))\nsig mkNilPairData = _ ,, \u2205 , con unit ,, con (list (con (pair (con Data) (con Data))))\nsig mkCons =\n _ ,, \u2205 , con Data , con (list (con Data)) ,, con (list (con Data))\nsig consByteString = _ ,, \u2205 , con integer , con bytestring ,, con bytestring\nsig sliceByteString =\n _ ,, \u2205 , con integer , con integer , con bytestring ,, con bytestring\nsig lengthOfByteString = _ ,, \u2205 , con bytestring ,, con integer\nsig indexByteString = _ ,, \u2205 , con bytestring , con integer ,, con integer\nsig blake2b-256 = _ ,, \u2205 , con bytestring ,, con bytestring\n\nsig2type : (\u03a6 : Ctx\u22c6) \u2192 Ctx \u03a6 \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 \u2205 \u22a2Nf\u22c6 *\nsig2type .\u2205 \u2205 C = C\nsig2type (\u03a6 ,\u22c6 J) (\u0393 ,\u22c6 J) C = sig2type \u03a6 \u0393 (\u03a0 C)\nsig2type \u03a6 (\u0393 , A) C = sig2type \u03a6 \u0393 (A \u21d2 C)\n\nbtype : \u2200{\u03a6} \u2192 Builtin \u2192 \u03a6 \u22a2Nf\u22c6 *\nbtype b = let \u03a6 ,, \u0393 ,, C = sig b in subNf (\u03bb()) (sig2type \u03a6 \u0393 C)\n\npostulate btype-ren : \u2200{\u03a6 \u03a8} b (\u03c1 : \u22c6.Ren \u03a6 \u03a8) \u2192 btype b \u2261 renNf \u03c1 (btype b)\npostulate btype-sub : \u2200{\u03a6 \u03a8} b (\u03c1 : SubNf \u03a6 \u03a8) \u2192 btype b \u2261 subNf \u03c1 (btype b)\n\ninfixl 7 _\u00b7\u22c6_\/_\n\ndata _\u22a2_ {\u03a6} (\u0393 : Ctx \u03a6) : \u03a6 \u22a2Nf\u22c6 * \u2192 Set where\n\n ` : \u2200 {A : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u220b A\n -----\n \u2192 \u0393 \u22a2 A\n\n \u019b : \u2200 {A B : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 , A \u22a2 B\n ---------\n \u2192 \u0393 \u22a2 A \u21d2 B\n\n _\u00b7_ : \u2200 {A B : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u22a2 A \u21d2 B\n \u2192 \u0393 \u22a2 A\n ---------\n \u2192 \u0393 \u22a2 B\n\n \u039b : \u2200 {K}\n \u2192 {B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 \u0393 ,\u22c6 K \u22a2 B\n -------------------\n \u2192 \u0393 \u22a2 \u03a0 B\n\n _\u00b7\u22c6_\/_ : \u2200 {K C}\n \u2192 {B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u22a2 \u03a0 B\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 C \u2261 B [ A ]\n --------------\n \u2192 \u0393 \u22a2 C\n\n wrap : \u2200{K}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *)\n \u2192 (B : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 \u0393 \u22a2 nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B)\n -------------------------------------------------------------\n \u2192 \u0393 \u22a2 \u03bc A B\n\n unwrap : \u2200{K C}\n \u2192 {A : \u03a6 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {B : \u03a6 \u22a2Nf\u22c6 K}\n \u2192 \u0393 \u22a2 \u03bc A B\n \u2192 C \u2261 nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B)\n -------------------------------------------------------------\n \u2192 \u0393 \u22a2 C\n\n con : \u2200{tcn}\n \u2192 TermCon {\u03a6} (con tcn)\n ---------------------\n \u2192 \u0393 \u22a2 con tcn\n\n builtin_\/_ : \u2200{C}\n \u2192 (b : Builtin)\n \u2192 C \u2261 btype b\n --------------\n \u2192 \u0393 \u22a2 C\n\n error :\n (A : \u03a6 \u22a2Nf\u22c6 *)\n --------------\n \u2192 \u0393 \u22a2 A\n\\end{code}\n\nUtility functions\n\n\\begin{code}\nopen import Type.BetaNormal.Equality\n\nconv\u220b : \u2200 {\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u2261 \u0393'\n \u2192 A \u2261 A'\n \u2192 \u0393 \u220b A\n \u2192 \u0393' \u220b A'\nconv\u220b refl refl x = x\n\nopen import Type.BetaNBE.Completeness\nopen import Type.Equality\nopen import Type.BetaNBE.RenamingSubstitution\n\nconv\u22a2 : \u2200 {\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u2261 \u0393'\n \u2192 A \u2261 A'\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393' \u22a2 A'\nconv\u22a2 refl refl t = t\n\nCtx2type : \u2200{\u03a6}(\u0393 : Ctx \u03a6) \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 \u2205 \u22a2Nf\u22c6 *\nCtx2type \u2205 C = C\nCtx2type (\u0393 ,\u22c6 J) C = Ctx2type \u0393 (\u03a0 C)\nCtx2type (\u0393 , x) C = Ctx2type \u0393 (x \u21d2 C)\n\ndata Arg : Set where\n Term Type : Arg\n\nArity = List Arg\n\nctx2bwdarity : \u2200{\u03a6}(\u0393 : Ctx \u03a6) \u2192 Bwd Arg\nctx2bwdarity \u2205 = []\nctx2bwdarity (\u0393 ,\u22c6 J) = ctx2bwdarity \u0393 :< Type\nctx2bwdarity (\u0393 , A) = ctx2bwdarity \u0393 :< Term\n\narity : Builtin \u2192 Arity\narity b = ctx2bwdarity (proj\u2081 (proj\u2082 (sig b))) <>> []\n\\end{code}\n","avg_line_length":30.0070921986,"max_line_length":98,"alphanum_fraction":0.5443157646} +{"size":2703,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe #-}\n\nmodule Snippets.Bool where\n\nopen import Data.Bool\nopen import Prelude hiding (_\u2227_)\n\ninfixl 6 _\u2227_\n\\end{code}\n%<*and-def>\n\\begin{code}\n_\u2227_ : Bool \u2192 Bool \u2192 Bool\nfalse \u2227 false = false\nfalse \u2227 true = false\ntrue \u2227 false = false\ntrue \u2227 true = true\n\\end{code}\n%<\/and-def>\n\\begin{code}\nopen import Cardinality.Finite.SplitEnumerable\nopen import Cardinality.Finite.SplitEnumerable.Search\nopen import Cardinality.Finite.SplitEnumerable.Instances\nopen import Cardinality.Finite.SplitEnumerable.Inductive\nopen import Cardinality.Finite.SplitEnumerable.Isomorphism\nopen import Cardinality.Finite.ManifestBishop\nopen import Data.Bool.Properties using (discreteBool)\n\ninfixl 4 _\u225f_\n_\u225f_ = discreteBool\n\\end{code}\n%<*bool-assoc-auto-proof>\n\\begin{code}\n\u2227-assoc : \u2200 x y z \u2192 (x \u2227 y) \u2227 z \u2261 x \u2227 (y \u2227 z)\n\u2227-assoc = \u2200\u21af\u207f 3 \u03bb x y z \u2192 (x \u2227 y) \u2227 z \u225f x \u2227 (y \u2227 z)\n\\end{code}\n%<\/bool-assoc-auto-proof>\n%<*some-assoc>\n\\begin{code}\nsome-assoc : \u03a3[ f \u2982 (Bool \u2192 Bool \u2192 Bool) ] \u2200 x y z \u2192 f (f x y) z \u2261 f x (f y z)\nsome-assoc = \u2203\u21af\u207f 1 \u03bb f \u2192 \u2200?\u207f 3 \u03bb x y z \u2192 f (f x y) z \u225f f x (f y z)\n\\end{code}\n%<\/some-assoc>\n%<*obvious>\n\\begin{code}\nobvious : true \u2227 false \u2261 false\nobvious = refl\n\\end{code}\n%<\/obvious>\n\\begin{code}\nprivate\n module SumDec where\n\\end{code}\n%<*is-true>\n\\begin{code}\n True : Dec A \u2192 Type\u2080\n True (yes _) = \u22a4\n True (no _) = \u22a5\n\\end{code}\n%<\/is-true>\n%<*from-true>\n\\begin{code}\n toWitness : (decision : Dec A) \u2192\n { _ : True decision } \u2192 A\n toWitness (yes x) = x\n\\end{code}\n%<\/from-true>\n\\begin{code}\nopen import Relation.Nullary.Decidable.Logic\nopen import Relation.Nullary.Decidable\n\nfrom-true : (dec : Dec A) \u2192 { _ : T (does dec) } \u2192 A\nfrom-true (yes p) = p\n\\end{code}\n%<*extremely-obvious>\n\\begin{code}\nextremely-obvious : true \u2262 false\nextremely-obvious = from-true (! (true \u225f false))\n\\end{code}\n%<\/extremely-obvious>\n\\begin{code}\nmodule PreInst\u2032 where\n open PreInst\n\\end{code}\n%<*pre-inst-proof>\n\\begin{code}\n \u2227-idem : \u2200 x \u2192 x \u2227 x \u2261 x\n \u2227-idem = \u2200\u21af \u2130!\u27e82\u27e9 \u03bb x \u2192 x \u2227 x \u225f x\n\\end{code}\n%<\/pre-inst-proof>\n\\begin{code}\nopen WithInst\n\\end{code}\n%<*with-inst-proof>\n\\begin{code}\n\u2227-idem : \u2200 x \u2192 x \u2227 x \u2261 x\n\u2227-idem = \u2200\u21af \u03bb x \u2192 x \u2227 x \u225f x\n\\end{code}\n%<\/with-inst-proof>\n\\begin{code}\nmodule BadCurrying where\n\\end{code}\n%<*and-comm>\n\\begin{code}\n \u2227-comm : \u2200 x y \u2192 x \u2227 y \u2261 y \u2227 x\n \u2227-comm = curry (\u2200\u21af (uncurry (\u03bb x y \u2192 x \u2227 y \u225f y \u2227 x )))\n\\end{code}\n%<\/and-comm>\n\\begin{code}\nopen import Data.Fin\n\\end{code}\n%<*finite-sigma-inst>\n\\begin{code}\n_ : \u2130! (\u03a3[ s \u2982 Bool ] (if s then Fin 3 else Fin 4))\n_ = it\n\\end{code}\n%<\/finite-sigma-inst>\n%<*and-comm-auto>\n\\begin{code}\n\u2227-comm : \u2200 x y \u2192 x \u2227 y \u2261 y \u2227 x\n\u2227-comm = \u2200\u21af\u207f 2 \u03bb x y \u2192 x \u2227 y \u225f y \u2227 x\n\\end{code}\n%<\/and-comm-auto>\n","avg_line_length":21.7983870968,"max_line_length":78,"alphanum_fraction":0.6259711432} +{"size":1466,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Declarative.StdLib.ChurchNat where\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Declarative\nopen import Builtin\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * # _\u22a2\u22c6_ con size\u22c6\n\nopen import Data.Unit\n\\end{code}\n\n\\begin{code}\n-- all (r :: *). r -> (r -> r) -> r\nN : \u2200{\u0393} \u2192 \u0393 \u22a2\u22c6 *\nN = \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 ` Z)\n\n-- \/\\(r :: *) -> \\(z : r) (f : r -> r) -> z\nZero : \u2205 \u22a2 N\nZero = \u039b (\u019b (\u019b (` (S Z))))\n\n-- \\(n : nat) -> \/\\(r :: *) -> \\(z : r) (f : r -> r) -> f (n {r} z f)\nSucc : \u2205 \u22a2 N \u21d2 N\nSucc = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (` Z) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n\nIter : \u2200{\u0393} \u2192 \u0393 \u22a2 \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 N \u21d2 (` Z))\nIter = \u039b (\u019b (\u019b (\u019b ((` Z) \u00b7\u22c6 (` Z) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\nopen import Builtin.Constant.Type\nopen import Data.Integer\nopen import Data.Nat\nopen import Agda.Builtin.Sigma renaming (_,_ to _,,_)\n\ncon0 : \u2200{\u0393} \u2192 \u0393 \u22a2 con integer (size\u22c6 8)\ncon0 = con (integer 8 (\u2124.pos 0) (-\u2264+ ,, (+\u2264+ (s\u2264s z\u2264n))))\n\ncon1 : \u2200{\u0393} \u2192 \u0393 \u22a2 con integer (size\u22c6 8)\ncon1 = con (integer 8 (\u2124.pos 1) (-\u2264+ ,, (+\u2264+ (s\u2264s (s\u2264s z\u2264n)))))\n\ninc : \u2200{\u0393} \u2192 \u0393 \u22a2 \u03a0 (con integer (` Z) \u21d2 con integer (` Z))\ninc = \u039b (\u019b (builtin addInteger (\u03bb { Z \u2192 ` Z ; (S ())}) ((builtin resizeInteger (\u03bb { Z \u2192 ` Z ; (S Z) \u2192 size\u22c6 8 ; (S (S ()))}) (builtin sizeOfInteger (\u03bb { Z \u2192 ` Z ; (S ())}) (` Z ,, tt) ,, (con1 ,, tt))) ,, (` Z) ,, tt)))\n\n\nNat2Int : \u2205 \u22a2 N \u21d2 con integer (size\u22c6 8)\nNat2Int = \u019b (Iter\n \u00b7\u22c6 con integer (size\u22c6 8)\n \u00b7 con0\n \u00b7 (inc \u00b7\u22c6 size\u22c6 8)\n \u00b7 ` Z)\n\\end{code}\n","avg_line_length":28.1923076923,"max_line_length":219,"alphanum_fraction":0.468622101} +{"size":3337,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"TSPL: Course notes\"\nlayout : page\npermalink : \/TSPL\/\n---\n\n## Staff\n\n* **Instructor**\n [Philip Wadler](https:\/\/homepages.inf.ed.ac.uk\/wadler)\n* **Teaching assistants**\n - [Wen Kokke](mailto:wen.kokke@ed.ac.uk)\n - [Chad Nester](mailto:chad.nester@gmail.com)\n\n## Lectures\n\nLectures take place Monday, Wednesday, and Friday in AT 7.02. (Moved from AT 5.07 and AT 4.12.)\n* **9.00--9.50am** Lecture\n* **10.00--10.50am** Tutorial\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n
Week<\/th>\n Mon<\/th>\n Wed<\/th>\n Fri<\/th>\n <\/tr>\n
1<\/td>\n 17 Sep<\/b> Naturals<\/a><\/td>\n 19 Sep<\/b> Induction<\/a><\/td>\n 21 Sep<\/b> Induction<\/a><\/td>\n <\/tr>\n
2<\/td>\n 24 Sep<\/b> Relations<\/a> (Chad)<\/td>\n 26 Sep<\/b> Relations<\/a> (Chad)<\/td>\n 28 Sep<\/b> (no class)<\/td>\n <\/tr>\n
3<\/td>\n 1 Oct<\/b> Equality<\/a> & Isomorphism<\/a><\/td>\n 3 Oct<\/b> Connectives<\/a><\/td>\n 5 Oct<\/b> Negation<\/a><\/td>\n <\/tr>\n
4<\/td>\n 8 Oct<\/b> Quantifiers<\/a><\/td>\n 10 Oct<\/b> Decidable<\/a><\/td>\n 12 Oct<\/b> (tutorial only)<\/td>\n <\/tr>\n
5<\/td>\n 15 Oct<\/b> Lists<\/a><\/td>\n 17 Oct<\/b> (tutorial only)<\/td>\n 19 Oct<\/b> Lists<\/a><\/td>\n <\/tr>\n
6<\/td>\n 22 Oct<\/b> Lambda<\/a><\/td>\n 24 Oct<\/b> (no class)<\/td>\n 26 Oct<\/b> Properties<\/a><\/td>\n <\/tr>\n
7<\/td>\n 29 Oct<\/b> DeBruijn<\/a><\/td>\n 31 Oct<\/b> More<\/a><\/td>\n 2 Nov<\/b> Inference<\/a><\/td>\n <\/tr>\n
8<\/td>\n 5 Nov<\/b> (no class)<\/td>\n 7 Nov<\/b> (tutorial only)<\/td>\n 9 Nov<\/b> Untyped<\/a><\/td>\n <\/tr>\n
9<\/td>\n 12 Nov<\/b> (no class)<\/td>\n 14 Nov<\/b> (tutorial only)<\/td>\n 16 Nov<\/b> (no class)<\/td>\n <\/tr>\n
10<\/td>\n 19 Nov<\/b> (no class)<\/td>\n 21 Nov<\/b> Propositions as Types<\/td>\n 23 Nov<\/b> (no class)<\/td>\n <\/tr>\n
11<\/td>\n 26 Nov<\/b> (no class)<\/td>\n 28 Nov<\/b> Quantitative (Wen)<\/td>\n 30 Nov<\/b> (mock exam)<\/td>\n <\/tr>\n<\/table>\n\n## Assignments\n\nFor instructions on how to set up Agda for PLFA see [Getting Started](\/GettingStarted\/).\n\n* [Assignment 1][Assignment1] cw1 due 4pm Thursday 4 October (Week 3)\n* [Assignment 2][Assignment2] cw2 due 4pm Thursday 18 October (Week 5)\n* [Assignment 3][Assignment3] cw3 due 4pm Thursday 1 November (Week 7)\n* [Assignment 4][Assignment4] cw4 due 4pm Thursday 15 November (Week 9)\n* [Assignment 5](\/tspl\/first-mock.pdf) cw5 due 4pm Thursday 22 November (Week 10)\n
\n Use file [Exam][Exam]. Despite the rubric, do **all three questions**.\n \n\nAssignments are submitted by running\n``` bash\nsubmit tspl cwN AssignmentN.lagda\n```\nwhere N is the number of the assignment.\n\n## Mock exam\n\nHere is the text of the [second mock](\/tspl\/second-mock.pdf)\nand the exam [instructions](\/tspl\/instructions.pdf).\n","avg_line_length":28.0420168067,"max_line_length":103,"alphanum_fraction":0.5894516032} +{"size":7552,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Preface: \u524d\u8a00\"\nlayout : page\nprev : \/Dedication\/\npermalink : \/Preface\/\nnext : \/Naturals\/\ntranslators : [\"Oling Cat\"]\nprogress : 100\n---\n\n{::comment}\nThe most profound connection between logic and computation is a pun.\nThe doctrine of Propositions as Types asserts that a certain kind of\nformal structure may be read in two ways: either as a proposition in\nlogic or as a type in computing. Further, a related structure may be\nread as either the proof of the proposition or as a programme of the\ncorresponding type. Further still, simplification of proofs\ncorresponds to evaluation of programs.\n{:\/}\n\n\u903b\u8f91\u4e0e\u8ba1\u7b97\u4e4b\u95f4\u6700\u6df1\u523b\u7684\u8054\u7cfb\u662f\u4e00\u79cd\u53cc\u5173\u3002\u300c\u547d\u9898\u5373\u7c7b\u578b\u300d\u7684\u5b66\u8bf4\u65ad\u8a00\uff0c\n\u5f62\u5f0f\u5316\u7684\u7ed3\u6784\u53ef\u4ee5\u6309\u4e24\u79cd\u65b9\u5f0f\u770b\u5f85\uff1a\u53ef\u4ee5\u770b\u505a\u903b\u8f91\u4e2d\u7684\u547d\u9898\uff0c\u4e5f\u53ef\u4ee5\u770b\u505a\u8ba1\u7b97\u4e2d\u7684\u7c7b\u578b\u3002\n\u6b64\u5916\uff0c\u76f8\u5173\u7684\u7ed3\u6784\u53ef\u4ee5\u770b\u505a\u547d\u9898\u7684\u8bc1\u660e\u6216\u8005\u5176\u76f8\u5e94\u7c7b\u578b\u7684\u7a0b\u5e8f\u3002\u66f4\u8fdb\u4e00\u6b65\u6765\u8bf4\uff0c\n\u8bc1\u660e\u7684\u5316\u7b80\u4e0e\u7a0b\u5e8f\u7684\u6c42\u503c\u5bf9\u5e94\u3002\n\n{::comment}\nAccordingly, the title of this book also has two readings. It may be\nparsed as \"(Programming Language) Foundations in Agda\" or \"Programming\n(Language Foundations) in Agda\" \u2014 the specifications we will write in\nthe proof assistant Agda both describe programming languages and are\nthemselves programmes.\n{:\/}\n\n\u4e0e\u6b64\u76f8\u5e94\uff0c\u672c\u4e66\u7684\u540d\u5b57\u4e5f\u6709\u4e24\u79cd\u542b\u4e49\u3002\u5b83\u53ef\u4ee5\u770b\u505a\u300c\u7f16\u7a0b\u8bed\u8a00\u7684\u57fa\u7840\u300d\uff0c\u4e5f\u53ef\u4ee5\u770b\u505a\n\u300c\u7f16\u7a0b\u7684\u8bed\u8a00\u57fa\u7840\u300d\u3002\u6211\u4eec\u5728 Agda \u8bc1\u660e\u52a9\u7406\u4e2d\u7f16\u5199\u7684\u89c4\u8303\uff08Specification\uff09\n\u540c\u65f6\u63cf\u8ff0\u4e86\u7f16\u7a0b\u8bed\u8a00\u4ee5\u53ca\u8be5\u8bed\u8a00\u7f16\u5199\u7684\u7a0b\u5e8f\u81ea\u8eab\u3002\n\n{::comment}\nThe book is aimed at students in the last year of an undergraduate\nhonours programme or the first year of a master or doctorate degree.\nIt aims to teach the fundamentals of operational semantics of\nprogramming languages, with simply-typed lambda calculus as the\ncentral example. The textbook is written as a literate script in\nAgda. The hope is that using a proof assistant will make the\ndevelopment more concrete and accessible to students, and give them\nrapid feedback to find and correct misapprehensions.\n{:\/}\n\n\u672c\u4e66\u9762\u5411\u672c\u79d1\u6700\u540e\u4e00\u5e74\u5b66\u6709\u4f59\u529b\u7684\u5b66\u751f\uff0c\u6216\u8005\u4e00\u5e74\u7ea7\u7684\u7814\u7a76\u751f\u6216\u535a\u58eb\u751f\u3002\n\u672c\u4e66\u4ee5\u7b80\u5355\u7c7b\u578b \u03bb-\u6f14\u7b97\uff08Simply-Typed Lambda Calculus\uff0c\u7b80\u79f0 STLC\uff09\u4f5c\u4e3a\u6838\u5fc3\u793a\u4f8b\uff0c\n\u65e8\u5728\u6559\u6388\u7f16\u7a0b\u8bed\u8a00\u7684\u64cd\u4f5c\u8bed\u4e49\u57fa\u7840\u3002\u5168\u4e66\u4ee5 Agda \u6587\u5b66\u811a\u672c\u7684\u5f62\u5f0f\u5199\u6210\u3002\n\u4f7f\u7528\u8bc1\u660e\u52a9\u7406\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8fc7\u7a0b\u53d8\u5f97\u66f4\u52a0\u5177\u4f53\u800c\u6e05\u6670\u6613\u61c2\uff0c\u8fd8\u53ef\u4ee5\u7ed9\u4e88\u5b66\u751f\u5373\u65f6\u53cd\u9988\uff0c\n\u5e2e\u52a9\u5b66\u751f\u53d1\u73b0\u7406\u89e3\u6709\u8bef\u7684\u5730\u65b9\u5e76\u53ca\u65f6\u7ea0\u6b63\u3002\n\n{::comment}\nThe book is broken into two parts. The first part, Logical\nFoundations, develops the needed formalisms. The second part,\nProgramming Language Foundations, introduces basic methods of\noperational semantics.\n{:\/}\n\n\u672c\u4e66\u5206\u4e3a\u4e24\u518c\u3002\u7b2c\u4e00\u5206\u518c\u4e3a\u903b\u8f91\u57fa\u7840\uff0c\u53d1\u5c55\u4e86\u6240\u9700\u7684\u5f62\u5f0f\u5316\u5de5\u5177\u3002\u7b2c\u4e8c\u5206\u518c\u4e3a\u7f16\u7a0b\u8bed\u8a00\u57fa\u7840\uff0c\n\u4ecb\u7ecd\u4e86\u64cd\u4f5c\u8bed\u4e49\u7684\u57fa\u672c\u65b9\u6cd5\u3002\n\n{::comment}\n## Personal remarks\n{:\/}\n\n## \u4e2a\u4eba\u8a00\u8bba\n\n{::comment}\nSince 2013, I have taught a course on Types and Semantics for\nProgramming Languages to fourth-year undergraduates and masters\nstudents at the University of Edinburgh. An earlier version of that\ncourse was based on Benjamin Pierce's excellent [TAPL][tapl]. My\nversion was based of Pierce's subsequent textbook, [Software\nFoundations][sf], written in collaboration with others and based on\nCoq. I am convinced of Pierce's claim that basing a course around a\nproof assistant aids learning, as summarised in his ICFP Keynote,\n[Lambda, The Ultimate TA][ta].\n{:\/}\n\n\u4ece 2013 \u5e74\u5f00\u59cb\uff0c\u6211\u5728\u7231\u4e01\u5821\u5927\u5b66\u4e3a\u56db\u5e74\u5236\u672c\u79d1\u751f\u548c\u7814\u7a76\u751f\u6559\u6388\u7f16\u7a0b\u8bed\u8a00\u7684\u7c7b\u578b\u548c\u8bed\u4e49\u7684\u8bfe\u7a0b\u3002\n\u8be5\u8bfe\u7a0b\u7684\u65e9\u671f\u7248\u672c\u57fa\u4e8e Benjamin Pierce \u7684\u8457\u4f5c [TAPL][tapl]\u3002\u6211\u7684\u7248\u672c\u5219\u57fa\u4e8e\nPierce \u7684\u540e\u7eed\u6559\u6750 [Software Foundations][sf]\uff08\u4e2d\u6587\u7248[\u300a\u8f6f\u4ef6\u57fa\u7840\u300b][sf-zh]\uff09\uff0c\u6b64\u4e66\u4e3a\nPierce \u4e0e\u4ed6\u4eba\u5408\u8457\uff0c\u57fa\u4e8e Coq \u7f16\u5199\u3002\u6b63\u5982 Pierce \u5728 ICFP \u7684\u4e3b\u9898\u6f14\u8bb2\n[Lambda, The Ultimate TA][ta] \u4e2d\u6240\u8a00\uff0c\u6211\u4e5f\u76f8\u4fe1\u57fa\u4e8e\u8bc1\u660e\u52a9\u7406\u7684\u8bfe\u7a0b\u4f1a\u5bf9\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002\n\n{::comment}\nHowever, after five years of experience, I have come to the conclusion\nthat Coq is not the best vehicle. Too much of the course needs to\nfocus on learning tactics for proof derivation, to the cost of\nlearning the fundamentals of programming language theory. Every\nconcept has to be learned twice: e.g., both the product data type, and\nthe corresponding tactics for introduction and elimination of\nconjunctions. The rules Coq applies to generate induction hypotheses\ncan sometimes seem mysterious. While the `notation` construct permits\npleasingly flexible syntax, it can be confusing that the same concept\nmust always be given two names, e.g., both `subst N x M` and `N [x :=\nM]`. Names of tactics are sometimes short and sometimes long; naming\nconventions in the standard library can be wildly inconsistent.\n*Propositions as types* as a foundation of proof is present but\nhidden.\n{:\/}\n\n\u7136\u800c\u6709\u4e86\u4e94\u5e74\u7684\u6559\u5b66\u7ecf\u9a8c\u540e\uff0c\u6211\u5f97\u51fa\u4e86 Coq \u5e76\u4e0d\u662f\u6700\u597d\u7684\u6388\u8bfe\u8f7d\u4f53\u7684\u7ed3\u8bba\u3002\n\u5bf9\u4e8e\u5b66\u4e60\u7f16\u7a0b\u8bed\u8a00\u7406\u8bba\u7684\u57fa\u7840\u800c\u8a00\uff0c\u6211\u4eec\u82b1\u8d39\u4e86\u592a\u591a\u8bfe\u7a0b\u53bb\u4e13\u95e8\u5b66\u4e60\u8bc1\u660e\u63a8\u5bfc\u7684\u7b56\u7565\uff08Tactic\uff09\u3002\n\u6bcf\u4e2a\u6982\u5ff5\u90fd\u9700\u8981\u5b66\u4e60\u4e24\u904d\uff1a\u4f8b\u5982\uff0c\u5728\u5b66\u8fc7\u4e00\u904d\u79ef\u6570\u636e\u7c7b\u578b\uff08Product Data Type\uff09\u4e4b\u540e\uff0c\n\u6211\u4eec\u8fd8\u8981\u518d\u5b66\u4e00\u904d\u4e0e\u4e4b\u5bf9\u5e94\u7684\u8fde\u8bcd\uff08Conjunction\uff09\u7684\u5f15\u5165\uff08Introduction\uff09\u548c\u6d88\u9664\uff08Elimination\uff09\u7b56\u7565\u3002\nCoq \u7528\u6765\u751f\u6210\u5f52\u7eb3\u5047\u8bbe\uff08Induction Hypothesis\uff09\u7684\u89c4\u5219\u6709\u65f6\u770b\u8d77\u6765\u5f88\u7384\u5b66\u3002\u800c `notation`\n\u6784\u9020\u5219\u5141\u8bb8\u76f4\u89c2\u4f46\u7075\u6d3b\u591a\u53d8\u7684\u8bed\u6cd5\uff0c\u540c\u4e00\u4e2a\u6982\u5ff5\u603b\u662f\u6709\u4e24\u4e2a\u540d\u5b57\u6709\u65f6\u4f1a\u4ee4\u4eba\u8ff7\u60d1\uff0c\n\u4f8b\u5982\uff0c`subst N x M` \u548c `N [x := M]`\u3002\u7b56\u7565\u7684\u540d\u5b57\u65f6\u77ed\u65f6\u957f\uff1b\u6807\u51c6\u5e93\u4e2d\u7684\u547d\u540d\u7ea6\u5b9a\u5219\u975e\u5e38\u4e0d\u4e00\u81f4\u3002\n**\u547d\u9898\u5373\u7c7b\u578b**\u4f5c\u4e3a\u8bc1\u660e\u7684\u57fa\u7840\u867d\u7136\u5b58\u5728\uff0c\u4f46\u5374\u88ab\u96ea\u85cf\u4e86\u3002\n\n{::comment}\nI found myself keen to recast the course in Agda. In Agda, there is\nno longer any need to learn about tactics: there is just\ndependently-typed programming, plain and simple. Introduction is\nalways by a constructor, elimination is always by pattern\nmatching. Induction is no longer a mysterious separate concept, but\ncorresponds to the familiar notion of recursion. Mixfix syntax is\nflexible while using just one name for each concept, e.g.,\nsubstitution is `_[_:=_]`. The standard library is not perfect, but\nthere is a fair attempt at consistency. *Propositions as types* as a\nfoundation of proof is on proud display.\n{:\/}\n\n\u6211\u53d1\u73b0\u81ea\u5df1\u70ed\u8877\u4e8e\u7528 Agda \u91cd\u6784\u6b64\u8bfe\u7a0b\u3002\u5728 Agda \u4e2d\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u5b66\u4e60\u7b56\u7565\u4e86\uff1a\n\u8fd9\u91cc\u53ea\u6709\u4f9d\u8d56\u7c7b\u578b\u7f16\u7a0b\uff0c\u7b80\u5355\u7eaf\u7cb9\u3002\u6211\u4eec\u603b\u662f\u901a\u8fc7\u6784\u9020\u5b50\u6765\u5f15\u5165\uff0c\u901a\u8fc7\u6a21\u5f0f\u5339\u914d\u6765\u6d88\u9664\u3002\n\u5f52\u7eb3\u4e0d\u518d\u662f\u8c1c\u4e4b\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u5b83\u4e0e\u6211\u4eec\u719f\u6089\u7684\u9012\u5f52\u6982\u5ff5\u76f4\u63a5\u5bf9\u5e94\u3002\u6df7\u7f00\u8bed\u6cd5\u5341\u5206\u7075\u6d3b\uff0c\n\u4f46\u6bcf\u4e2a\u6982\u5ff5\u53ea\u9700\u8981\u4e00\u4e2a\u540d\u5b57\uff0c\u4f8b\u5982\u4ee3\u6362\u5c31\u662f `_[_:=_]`\u3002\u6807\u51c6\u5e93\u867d\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u5b83\u7684\u4e00\u81f4\u6027\u5374\u5f88\u5408\u7406\u3002\n**\u547d\u9898\u5373\u7c7b\u578b**\u4f5c\u4e3a\u8bc1\u660e\u7684\u57fa\u7840\u5219\u88ab\u9a84\u50b2\u5730\u5c55\u793a\u4e86\u51fa\u6765\u3002\n\n{::comment}\nAlas, there is no textbook for programming language theory in\nAgda. Stump's [Verified Functional Programming in Agda][stump] covers\nrelated ground, but focusses more on programming with dependent\ntypes than on the theory of programming languages.\n{:\/}\n\n\u7136\u800c\uff0c\u6b64\u524d\u8fd8\u6ca1\u6709 Agda \u63cf\u8ff0\u7684\u7f16\u7a0b\u8bed\u8a00\u7406\u8bba\u6559\u6750\u3002\u867d\u7136 Stump \u7684\n[Verified Functional Programming in Agda][stump] \u6db5\u76d6\u4e86\u76f8\u5173\u7684\u8303\u56f4\uff0c\n\u4f46\u6bd4\u8d77\u7f16\u7a0b\u8bed\u8a00\u7406\u8bba\uff0c\u5374\u66f4\u591a\u5173\u6ce8\u4e8e\u4f9d\u8d56\u7c7b\u578b\u7f16\u7a0b\u3002\n\n{::comment}\nThe original goal was to simply adapt *Software Foundations*,\nmaintaining the same text but transposing the code from Coq to Agda.\nBut it quickly became clear to me that after five years in the\nclassroom I had my own ideas about how to present the material. They\nsay you should never write a book unless you cannot *not* write the\nbook, and I soon found that this was a book I could not not write.\n{:\/}\n\n\u672c\u4e66\u6700\u521d\u7684\u76ee\u6807\u53ea\u662f\u7b80\u5355\u5730\u6539\u7f16**\u300a\u8f6f\u4ef6\u57fa\u7840\u300b**\uff0c\u4fdd\u6301\u540c\u6837\u7684\u5185\u5bb9\uff0c\u800c\u53ea\u662f\u5c06\u4ee3\u7801\u4ece\nCoq \u7ffb\u8bd1\u6210 Agda\u3002\u4f46\u4e94\u5e74\u7684\u8bfe\u5802\u7ecf\u9a8c\u8ba9\u6211\u5f88\u5feb\u5c31\u660e\u767d\u4e86\uff0c\u81ea\u5df1\u6709\u4e00\u4e9b\u5173\u4e8e\u5982\u4f55\u5c55\u793a\u8fd9\u4e9b\u6750\u6599\u7684\u60f3\u6cd5\u3002\n\u6709\u4eba\u8bf4\u9664\u975e\u4f60**\u4e0d\u5f97\u4e0d**\u5199\u4e66\uff0c\u5426\u5219\u7edd\u5bf9\u4e0d\u8981\u5199\u4e66\u3002\u800c\u6211\u5f88\u5feb\u5c31\u53d1\u73b0\u8fd9\u662f\u4e00\u672c\u6211\u4e0d\u5f97\u4e0d\u5199\u7684\u4e66\u3002\n\n{::comment}\nI am fortunate that my student, [Wen Kokke][wen], was keen to help.\nShe guided me as a newbie to Agda and provided an infrastructure that\nis easy to use and produces pages that are a pleasure to view.\n{:\/}\n\n\u6211\u5f88\u5e78\u8fd0\u5730\u5f97\u5230\u4e86\u6211\u7684\u5b66\u751f [Wen Kokke][wen] \u7684\u70ed\u60c5\u5e2e\u52a9\u3002\u5979\u5f15\u5bfc\u7740\u6211\u7ad9\u5728 Agda\n\u65b0\u624b\u7684\u89c6\u89d2\u5199\u4e66\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5341\u5206\u6613\u7528\u7684\u5de5\u5177\u6765\u4ea7\u751f\u6e05\u6670\u6613\u8bfb\u7684\u7684\u9875\u9762\u3002\n\n{::comment}\nMost of the text was written during a sabbatical in the first half of 2018.\n{:\/}\n\n\u8fd9\u91cc\u7684\u5927\u90e8\u5206\u5185\u5bb9\u90fd\u662f\u5728 2018 \u5e74\u4e0a\u534a\u5e74\u7684\u4f11\u5047\u671f\u95f4\u5199\u7684\u3002\n\n\u2014 Philip Wadler, Rio de Janeiro, January\u2013June 2018\n\n[tapl]: https:\/\/www.cis.upenn.edu\/~bcpierce\/tapl\/\n[sf]: https:\/\/softwarefoundations.cis.upenn.edu\/\n[sf-zh]: https:\/\/coq-zh.github.io\/SF-zh\/\n[ta]: https:\/\/www.cis.upenn.edu\/~bcpierce\/papers\/plcurriculum.pdf\n[stump]: https:\/\/www.morganclaypoolpublishers.com\/catalog_Orig\/product_info.php?cPath=24&products_id=908\n[wen]: https:\/\/github.com\/wenkokke\n[phil]: https:\/\/homepages.inf.ed.ac.uk\/wadler\/\n\n{::comment}\n## A word on the exercises\n{:\/}\n\n## \u4e60\u9898\u8bf4\u660e\n\n{::comment}\nExercises labelled \"(recommended)\" are the ones students are\nrequired to do in the class taught at Edinburgh from this textbook.\n{:\/}\n\n\u6807\u6709\uff08\u63a8\u8350\uff09\u7684\u4e60\u9898\u662f\u7231\u4e01\u5821\u5927\u5b66\u4f7f\u7528\u672c\u6559\u6750\u7684\u8bfe\u7a0b\u4e2d\u5b66\u751f\u9700\u8981\u505a\u7684\u3002\n\n{::comment}\nExercises labelled \"(stretch)\" are there to provide an extra challenge.\nFew students do all of these, but most attempt at least a few.\n{:\/}\n\n\u6807\u6709\uff08\u5ef6\u4f38\uff09\u7684\u4e60\u9898\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u6311\u6218\u3002\u5f88\u5c11\u6709\u5b66\u751f\u5168\u90e8\u505a\u5b8c\uff0c\u4f46\u5927\u90e8\u5206\u7ec3\u4e60\u81f3\u5c11\u5e94\u8be5\u5c1d\u8bd5\u4e00\u4e0b\u3002\n\n{::comment}\nExercises without a label are included for those who want extra practice.\n{:\/}\n\n\u6ca1\u6709\u6807\u8bb0\u7684\u4e60\u9898\u4f9b\u60f3\u8981\u66f4\u591a\u5b9e\u8df5\u7684\u5b66\u751f\u7ec3\u4e60\u3002\n","avg_line_length":36.3076923077,"max_line_length":104,"alphanum_fraction":0.7833686441} +{"size":4768,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule AlgorithmicRed.Term where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Function hiding (_\u220b_)\n\n\n\nopen import Type\nopen import Type.RenamingSubstitution\nopen import Type.BetaNormal\n\nopen import Type.BetaNBE\nopen import Builtin\nopen import Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con booleanNf\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List hiding ([_])\nopen import Relation.Binary.PropositionalEquality hiding ([_]; subst)\nopen import Data.Unit\n\\end{code}\n\n## Fixity declarations\n\nTo begin, we get all our infix declarations out of the way.\nWe list separately operators for judgements, types, and terms.\n\\begin{code}\ninfix 4 _\u220b_\ninfix 4 _\u22a2_\ninfixl 5 _,_\n\\end{code}\n\n## Contexts and erasure\n\nWe need to mutually define contexts and their\nerasure to type contexts.\n\\begin{code}\ndata Ctx : Set\n\u2225_\u2225 : Ctx \u2192 Ctx\u22c6\n\\end{code}\n\nA context is either empty, or extends a context by\na type variable of a given kind, or extends a context\nby a variable of a given type.\n\\begin{code}\ndata Ctx where\n \u2205 : Ctx\n _,\u22c6_ : Ctx \u2192 Kind \u2192 Ctx\n _,_ : \u2200 {J} (\u0393 : Ctx) \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J \u2192 Ctx\n\\end{code}\nLet `\u0393` range over contexts. In the last rule,\nthe type is indexed by the erasure of the previous\ncontext to a type context and a kind.\n\nThe erasure of a context is a type context.\n\\begin{code}\n\u2225 \u2205 \u2225 = \u2205\n\u2225 \u0393 ,\u22c6 J \u2225 = \u2225 \u0393 \u2225 ,\u22c6 J\n\u2225 \u0393 , A \u2225 = \u2225 \u0393 \u2225\n\\end{code}\n\n## Variables\n\nA variable is indexed by its context and type.\n\\begin{code}\ndata _\u220b_ : \u2200 {J} (\u0393 : Ctx) \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J \u2192 Set where\n\n Z : \u2200 {\u0393 J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J}\n ----------\n \u2192 \u0393 , A \u220b A\n\n S : \u2200 {\u0393 J K} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} {B : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K}\n \u2192 \u0393 \u220b A\n ----------\n \u2192 \u0393 , B \u220b A\n\n T : \u2200 {\u0393 J K} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J}\n \u2192 \u0393 \u220b A\n -------------------\n \u2192 \u0393 ,\u22c6 K \u220b weakenNf A\n\\end{code}\nLet `x`, `y` range over variables.\n\n\n\n## Terms\n\nA term is indexed over by its context and type. A term is a variable,\nan abstraction, an application, a type abstraction, or a type\napplication.\n\\begin{code}\n\n\nopen import Type.Reduction\n_\u2014Nf\u2192\u22c6_ : \u2200{\u0393 K} \u2192 \u0393 \u22a2\u22c6 K \u2192 \u0393 \u22a2Nf\u22c6 K \u2192 Set\nA \u2014Nf\u2192\u22c6 N = \u03a3 (_ \u22a2\u22c6 _) \u03bb A' \u2192 (A \u2014\u2192\u22c6 A') \u00d7 Value\u22c6 A' \u00d7 embNf N \u2261 A'\n\n-- value predicate and normal forms coincide\n\nval2nf : \u2200{\u0393 K}{A : \u0393 \u22a2\u22c6 K} \u2192 Value\u22c6 A \u2192 \u03a3 (\u0393 \u22a2Nf\u22c6 K) \u03bb N \u2192 embNf N \u2261 A\nneu2nen : \u2200{\u0393 K}{A : \u0393 \u22a2\u22c6 K} \u2192 Neutral\u22c6 A \u2192 \u03a3 (\u0393 \u22a2Ne\u22c6 K) \u03bb N \u2192 embNe N \u2261 A\n\nval2nf (V-\u03a0 VN) with val2nf VN\n... | N ,, q = \u03a0 _ N ,, cong (\u03a0 _) q\nval2nf (VM V-\u21d2 VN) with val2nf VM | val2nf VN\n... | M ,, p | N ,, q = M \u21d2 N ,, cong\u2082 _\u21d2_ p q\nval2nf (V-\u019b VN) with val2nf VN\n... | N ,, p = \u019b _ N ,, cong (\u019b _) p\nval2nf (N- VN) with neu2nen VN\n... | N ,, p = ne N ,, p\nval2nf (V-con {tcn = tcn})= con tcn ,, refl\n\nneu2nen N-\u03bc1 = _ ,, refl\nneu2nen (N-\u00b7 NA VB) with neu2nen NA | val2nf VB\n... | A ,, p | B ,, q = A \u00b7 B ,, cong\u2082 _\u00b7_ p q\nneu2nen N-` = _ ,, refl\ninfix 4 _\u2014Nf\u2192\u22c6_\n\nTel : \u2200 \u0393 \u0394 \u2192 (\u2200 {J} \u2192 \u0394 \u220b\u22c6 J \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J) \u2192 List (\u0394 \u22a2Nf\u22c6 *) \u2192 Set\n\ndata _\u22a2_ : \u2200 {J} (\u0393 : Ctx) \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J \u2192 Set where\n\n ` : \u2200 {\u0393 J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J}\n \u2192 \u0393 \u220b A\n ------\n \u2192 \u0393 \u22a2 A\n\n \u019b : \u2200 {\u0393 A B A'}\n \u2192 A \u2014Nf\u2192\u22c6 A'\n \u2192 \u0393 , A' \u22a2 B\n -----------\n \u2192 \u0393 \u22a2 A' \u21d2 B\n\n _\u00b7_ : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A \u21d2 B\n \u2192 \u0393 \u22a2 A\n -----------\n \u2192 \u0393 \u22a2 B\n\n \u039b : \u2200 {\u0393 K}{x}\n \u2192 {B : \u2225 \u0393 \u2225 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 \u0393 ,\u22c6 K \u22a2 B\n ----------\n \u2192 \u0393 \u22a2 \u03a0 x B\n\n _\u00b7\u22c6_ : \u2200 {\u0393 K}{x}\n \u2192 {B : \u2225 \u0393 \u2225 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u22a2 \u03a0 x B\n \u2192 (A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K)\n \u2192 {R : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 *}\n \u2192 embNf B [ embNf A ] \u2014Nf\u2192\u22c6 R\n ---------------\n \u2192 \u0393 \u22a2 R\n\n wrap1 : \u2200{\u0393 K}\n \u2192 (pat : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *)\n \u2192 (arg : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K)\n \u2192 {R : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 *}\n \u2192 embNf pat \u00b7 (\u03bc1 \u00b7 embNf pat) \u00b7 embNf arg \u2014Nf\u2192\u22c6 R\n \u2192 (term : \u0393 \u22a2 R)\n \u2192 \u0393 \u22a2 ne (\u03bc1 \u00b7 pat \u00b7 arg)\n\n unwrap1 : \u2200{\u0393 K}\n \u2192 {pat : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {arg : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K}\n \u2192 (term : \u0393 \u22a2 ne (\u03bc1 \u00b7 pat \u00b7 arg))\n \u2192 {R : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 *}\n \u2192 embNf pat \u00b7 (\u03bc1 \u00b7 embNf pat) \u00b7 embNf arg \u2014Nf\u2192\u22c6 R\n \u2192 \u0393 \u22a2 R\n\n con : \u2200{\u0393 tcn}\n \u2192 TermCon {\u03a6 = \u2225 \u0393 \u2225} (con tcn )\n -------------------\n \u2192 \u0393 \u22a2 con tcn\n\n builtin : \u2200{\u0393}\n \u2192 (bn : Builtin)\n \u2192 let \u0394 ,, As ,, C = SIG bn in\n (\u03c3 : \u2200 {J} \u2192 \u0394 \u220b\u22c6 J \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J)\n \u2192 Tel \u0393 \u0394 \u03c3 As\n \u2192 {R : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 *}\n \u2192 subst (embNf \u2218 \u03c3) (embNf C) \u2014Nf\u2192\u22c6 R\n ----------------------------------\n \u2192 \u0393 \u22a2 R\n\n error : \u2200{\u0393} \u2192 (A : \u2225 \u0393 \u2225 \u22a2\u22c6 *){R : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 *} \u2192 A \u2014Nf\u2192\u22c6 R \u2192 \u0393 \u22a2 R\n\nTel \u0393 \u0394 \u03c3 [] = \u22a4\nTel \u0393 \u0394 \u03c3 (A \u2237 As) = \n \u03a3 (\u2225 \u0393 \u2225 \u22a2Nf\u22c6 *) \u03bb N \u2192\n subst (embNf \u2218 \u03c3) (embNf A) \u2014Nf\u2192\u22c6 N\n \u00d7 \u0393 \u22a2 N\n \u00d7 Tel \u0393 \u0394 \u03c3 As\n\\end{code}\n\n# Term Abbreviations\n\\begin{code}\n--void : \u2200{\u0393} \u2192 \u0393 \u22a2 unitNf\n--void = \u039b (\u019b (` Z))\n{-\ntrue : \u2200{\u0393} \u2192 \u0393 \u22a2 booleanNf\ntrue = \u039b (\u019b (\u019b (` (S Z))))\n\nfalse : \u2200{\u0393} \u2192 \u0393 \u22a2 booleanNf\nfalse = \u039b (\u019b (\u019b (` Z)))\n-}\n\\end{code}\n","avg_line_length":22.4905660377,"max_line_length":74,"alphanum_fraction":0.4815436242} +{"size":964,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Padova: Course notes\"\nlayout : page\npermalink : \/Padova\/\n---\n\n## Staff\n\n* **Instructor**\n [Philip Wadler](https:\/\/homepages.inf.ed.ac.uk\/wadler)\n\n* **Host**\n [Maria Emilia Maietti](https:\/\/www.math.unipd.it\/~maietti\/)\n\n## Schedule\n\n* 12.30--15.30, Wed 28 May 2019, Lecture and lab\n* 12.30--15.30, Thu 29 May 2019, Lecture and lab\n\n\n \n \n
Wed 28 May<\/b><\/td>\n Naturals<\/a>,\n Induction<\/a> & Relations<\/a><\/td>\n <\/tr>\n
Thu 29 May<\/b><\/td>\n Lambda<\/a> &\n Properties<\/a><\/td>\n <\/tr>\n<\/table>\n\n\n## Assignments\n\nFor instructions on how to set up Agda for PLFA see [Getting Started](\/GettingStarted\/).\n\n* Wed 28 May\n - Naturals (`_^_`)\n - Induction (`+-swap`, `*-distrib-+`)\n - Relations (`<-trans`)\n* Wed 29 May\n - Lambda (`mul`, `mul-type`)\n - Properties (`mul-eval`)\n","avg_line_length":21.4222222222,"max_line_length":88,"alphanum_fraction":0.5933609959} +{"size":5176,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Basics: Functional Programming in Agda\"\npermalink : \/Basics\n---\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; refl)\n\\end{code}\n\nThe functional programming style brings programming closer to\nsimple, everyday mathematics: If a procedure or method has no side\neffects, then (ignoring efficiency) all we need to understand\nabout it is how it maps inputs to outputs -- that is, we can think\nof it as just a concrete method for computing a mathematical\nfunction. This is one sense of the word \"functional\" in\n\"functional programming.\" The direct connection between programs\nand simple mathematical objects supports both formal correctness\nproofs and sound informal reasoning about program behavior.\n\nThe other sense in which functional programming is \"functional\" is\nthat it emphasizes the use of functions (or methods) as\n_first-class_ values -- i.e., values that can be passed as\narguments to other functions, returned as results, included in\ndata structures, etc. The recognition that functions can be\ntreated as data in this way enables a host of useful and powerful\nidioms.\n\nOther common features of functional languages include _algebraic\ndata types_ and _pattern matching_, which make it easy to\nconstruct and manipulate rich data structures, and sophisticated\n_polymorphic type systems_ supporting abstraction and code reuse.\nAgda shares all of these features.\n\nThis chapter introduces the most essential elements of Agda.\n\n## Enumerated Types\n\nOne unusual aspect of Agda is that its set of built-in\nfeatures is _extremely_ small. For example, instead of providing\nthe usual palette of atomic data types (booleans, integers,\nstrings, etc.), Agda offers a powerful mechanism for defining new\ndata types from scratch, from which all these familiar types arise\nas instances.\n\nNaturally, the Agda distribution comes with an extensive standard\nlibrary providing definitions of booleans, numbers, and many\ncommon data structures like lists and hash tables. But there is\nnothing magic or primitive about these library definitions. To\nillustrate this, we will explicitly recapitulate all the\ndefinitions we need in this course, rather than just getting them\nimplicitly from the library.\n\nTo see how this definition mechanism works, let's start with a\nvery simple example.\n\n### Days of the Week\n\nThe following declaration tells Agda that we are defining\na new set of data values -- a _type_.\n\n\\begin{code}\ndata Day : Set where\n monday : Day\n tuesday : Day\n wednesday : Day\n thursday : Day\n friday : Day\n saturday : Day\n sunday : Day\n\\end{code}\n\nThe type is called `day`, and its members are `monday`,\n`tuesday`, etc. The second and following lines of the definition\ncan be read \"`monday` is a `day`, `tuesday` is a `day`, etc.\"\n\nHaving defined `day`, we can write functions that operate on\ndays.\n\n\\begin{code}\nnextWeekday : Day -> Day\nnextWeekday monday = tuesday\nnextWeekday tuesday = wednesday\nnextWeekday wednesday = thursday\nnextWeekday thursday = friday\nnextWeekday friday = monday\nnextWeekday saturday = monday\nnextWeekday sunday = monday\n\\end{code}\n\nOne thing to note is that the argument and return types of\nthis function are explicitly declared. Like most functional\nprogramming languages, Agda can often figure out these types for\nitself when they are not given explicitly -- i.e., it performs\n_type inference_ -- but we'll include them to make reading\neasier.\n\nHaving defined a function, we should check that it works on\nsome examples. There are actually three different ways to do this\nin Agda.\n\nFirst, we can use the Emacs command `C-c C-n` to evaluate a\ncompound expression involving `nextWeekday`. For instance, `nextWeekday\nfriday` should evaluate to `monday`. If you have a computer handy, this\nwould be an excellent moment to fire up Agda and try this for yourself.\nLoad this file, `Basics.lagda`, load it using `C-c C-l`, submit the\nabove example to Agda, and observe the result.\n\nSecond, we can record what we _expect_ the result to be in the\nform of an Agda type:\n\n\\begin{code}\ntest-nextWeekday : nextWeekday (nextWeekday saturday) \u2261 tuesday\n\\end{code}\n\nThis declaration does two things: it makes an assertion (that the second\nweekday after `saturday` is `tuesday`), and it gives the assertion a name\nthat can be used to refer to it later.\n\nHaving made the assertion, we must also verify it. We do this by giving\na term of the above type:\n\n\\begin{code}\ntest-nextWeekday = refl\n\\end{code}\n\nThere is no essential difference between the definition for\n`test-nextWeekday` here and the definition for `nextWeekday` above,\nexcept for the new symbol for equality `\u2261` and the constructor `refl`.\nThe details of these are not important for now (we'll come back to them in\na bit), but essentially `refl` can be read as \"The assertion we've made\ncan be proved by observing that both sides of the equality evaluate to the\nsame thing, after some simplification.\"\n\nThird, we can ask Agda to _compile_ some program involving our definition,\nThis facility is very interesting, since it gives us a way to construct\n_fully certified_ programs. We'll come back to this topic in later chapters.\n","avg_line_length":37.7810218978,"max_line_length":76,"alphanum_fraction":0.7795595054} +{"size":454,"ext":"lagda","lang":"Literate Agda","max_stars_count":1989.0,"content":"\\documentclass{article}\n\n\\usepackage{agda}\n\n\\begin{document}\n\n%\\renewcommand{\\AgdaIndent}{\\;\\;}\n\n\\begin{code}\nrecord Sigma (A : Set) (B : A \u2192 Set) : Set where\n constructor _,_\n field\n proj\u2081 : A\n proj\u2082 : B proj\u2081\n\nopen Sigma public\n\\end{code}\n\n\\begin{code}\nuncurry : {A : Set} {B : A \u2192 Set} {C : Sigma A B \u2192 Set} \u2192\n ((x : A) \u2192 (y : B x) \u2192 C (x , y)) \u2192\n ((p : Sigma A B) \u2192 C p)\nuncurry f (x , y) = f x y\n\\end{code}\n\n\\end{document}\n","avg_line_length":16.8148148148,"max_line_length":57,"alphanum_fraction":0.5462555066} +{"size":31202,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Equality: \u76f8\u7b49\u6027\u4e0e\u7b49\u5f0f\u63a8\u7406\"\nlayout : page\nprev : \/Relations\/\npermalink : \/Equality\/\nnext : \/Isomorphism\/\ntranslators : [\"Fangyi Zhou\"]\nprogress : 100\n---\n\n\\begin{code}\nmodule plfa.Equality where\n\\end{code}\n\n{::comment}\nMuch of our reasoning has involved equality. Given two terms `M`\nand `N`, both of type `A`, we write `M \u2261 N` to assert that `M` and `N`\nare interchangeable. So far we have treated equality as a primitive,\nhere we show how to define it as an inductive datatype.\n{:\/}\n\n\u6211\u4eec\u5728\u8bba\u8bc1\u7684\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u4f1a\u4f7f\u7528\u76f8\u7b49\u6027\u3002\u7ed9\u5b9a\u4e24\u4e2a\u90fd\u4e3a `A` \u7c7b\u578b\u7684\u9879 `M` \u548c `N`\uff0c\n\u6211\u4eec\u7528 `M \u2261 N` \u6765\u8868\u793a `M` \u548c `N` \u53ef\u4ee5\u76f8\u4e92\u66ff\u6362\u3002\u5728\u6b64\u4e4b\u524d\uff0c\n\u6211\u4eec\u5c06\u76f8\u7b49\u6027\u4f5c\u4e3a\u4e00\u4e2a\u57fa\u7840\u8fd0\u7b97\uff0c\u800c\u73b0\u5728\u6211\u4eec\u6765\u8bf4\u660e\u5982\u679c\u5c06\u5176\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u5f52\u7eb3\u7684\u6570\u636e\u7c7b\u578b\u3002\n\n\n{::comment}\n## Imports\n{:\/}\n\n## \u5bfc\u5165\n\n{::comment}\nThis chapter has no imports. Every chapter in this book, and nearly\nevery module in the Agda standard library, imports equality.\nSince we define equality here, any import would create a conflict.\n{:\/}\n\n\u672c\u7ae0\u8282\u6ca1\u6709\u5bfc\u5165\u7684\u5185\u5bb9\u3002\u672c\u4e66\u7684\u6bcf\u4e00\u7ae0\u8282\uff0c\u4ee5\u53ca Agda \u6807\u51c6\u5e93\u7684\u6bcf\u4e2a\u6a21\u5757\u90fd\u5bfc\u5165\u4e86\u76f8\u7b49\u6027\u3002\n\u6211\u4eec\u5728\u6b64\u5b9a\u4e49\u76f8\u7b49\u6027\uff0c\u5bfc\u5165\u5176\u4ed6\u5185\u5bb9\u5c06\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002\n\n\n{::comment}\n## Equality\n{:\/}\n\n## \u76f8\u7b49\u6027\n\n{::comment}\nWe declare equality as follows:\n{:\/}\n\n\u6211\u4eec\u5982\u4e0b\u5b9a\u4e49\u76f8\u7b49\u6027\uff1a\n\n\\begin{code}\ndata _\u2261_ {A : Set} (x : A) : A \u2192 Set where\n refl : x \u2261 x\n\\end{code}\n\n{::comment}\nIn other words, for any type `A` and for any `x` of type `A`, the\nconstructor `refl` provides evidence that `x \u2261 x`. Hence, every value\nis equal to itself, and we have no other way of showing values\nequal. The definition features an asymmetry, in that the\nfirst argument to `_\u2261_` is given by the parameter `x : A`, while the\nsecond is given by an index in `A \u2192 Set`. This follows our policy\nof using parameters wherever possible. The first argument to `_\u2261_`\ncan be a parameter because it doesn't vary, while the second must be\nan index, so it can be required to be equal to the first.\n{:\/}\n\n\u7528\u5176\u4ed6\u7684\u8bdd\u6765\u8bf4\uff0c\u5bf9\u4e8e\u4efb\u610f\u7c7b\u578b `A` \u548c\u4efb\u610f `A` \u7c7b\u578b\u7684 `x`\uff0c\u6784\u9020\u5668 `refl` \u63d0\u4f9b\u4e86\n`x \u2261 x` \u7684\u8bc1\u660e\u3002\u6240\u4ee5\uff0c\u6bcf\u4e2a\u503c\u7b49\u540c\u4e8e\u5b83\u672c\u8eab\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u5176\u4ed6\u529e\u6cd5\u6765\u8bc1\u660e\u503c\u7684\u76f8\u7b49\u6027\u3002\n\u8fd9\u4e2a\u5b9a\u4e49\u91cc\u6709\u4e0d\u5bf9\u79f0\u7684\u5730\u65b9\uff0c`_\u2261_` \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08Argument\uff09\u7531 `x : A` \u7ed9\u51fa\uff0c\n\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff08Argument\uff09\u5219\u662f\u7531 `A \u2192 Set` \u7684\u7d22\u5f15\u7ed9\u51fa\u3002\n\u8fd9\u548c\u6211\u4eec\u5c3d\u53ef\u80fd\u591a\u7684\u4f7f\u7528\u53c2\u6570\uff08Parameter\uff09\u7684\u7406\u5ff5\u76f8\u7b26\u3002`_\u2261_` \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08Argument\uff09\n\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\uff08Parameter\uff09\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u53d8\uff0c\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff08Argument\uff09\u5219\u5fc5\u987b\u662f\u4e00\u4e2a\u7d22\u5f15\uff0c\n\u8fd9\u6837\u5b83\u624d\u53ef\u4ee5\u7b49\u7528\u4e8e\u7b2c\u4e00\u4e2a\u3002\n\n{::comment}\nWe declare the precedence of equality as follows:\n{:\/}\n\n\u6211\u4eec\u5982\u4e0b\u5b9a\u4e49\u76f8\u7b49\u6027\u7684\u4f18\u5148\u7ea7\uff1a\n\n\\begin{code}\ninfix 4 _\u2261_\n\\end{code}\n\n{::comment}\nWe set the precedence of `_\u2261_` at level 4, the same as `_\u2264_`,\nwhich means it binds less tightly than any arithmetic operator.\nIt associates neither to left nor right; writing `x \u2261 y \u2261 z`\nis illegal.\n{:\/}\n\n\u6211\u4eec\u5c06 `_\u2261_` \u7684\u4f18\u5148\u7ea7\u8bbe\u7f6e\u4e3a 4\uff0c\u4e0e `_\u2264_` \u76f8\u540c\uff0c\u6240\u4ee5\u5176\u5b83\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u7ed3\u5408\u90fd\u6bd4\u5b83\u7d27\u5bc6\u3002\n\u7531\u4e8e\u5b83\u65e2\u4e0d\u662f\u5de6\u7ed3\u5408\uff0c\u4e5f\u4e0d\u662f\u53f3\u7ed3\u5408\u7684\uff0c\u56e0\u6b64 `x \u2261 y \u2261 z` \u662f\u4e0d\u5408\u6cd5\u7684\u3002\n\n\n{::comment}\n## Equality is an equivalence relation\n{:\/}\n\n## \u76f8\u7b49\u6027\u662f\u4e00\u4e2a\u7b49\u4ef7\u5173\u7cfb\uff08Equivalence Relation\uff09\n\n{::comment}\nAn equivalence relation is one which is reflexive, symmetric, and transitive.\nReflexivity is built-in to the definition of equality, via the\nconstructor `refl`. It is straightforward to show symmetry:\n{:\/}\n\n\u4e00\u4e2a\u7b49\u4ef7\u5173\u7cfb\u662f\u81ea\u53cd\u3001\u5bf9\u79f0\u548c\u4f20\u9012\u7684\u3002\u5176\u4e2d\u81ea\u53cd\u6027\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u5668 `refl` \u76f4\u63a5\u4ece\u76f8\u7b49\u6027\u7684\u5b9a\u4e49\u4e2d\u5f97\u6765\u3002\n\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5730\u8bc1\u660e\u5176\u5bf9\u79f0\u6027\uff1a\n\n\\begin{code}\nsym : \u2200 {A : Set} {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 y \u2261 x\nsym refl = refl\n\\end{code}\n\n{::comment}\nHow does this proof work? The argument to `sym` has type `x \u2261 y`, but\non the left-hand side of the equation the argument has been\ninstantiated to the pattern `refl`, which requires that `x` and `y`\nare the same. Hence, for the right-hand side of the equation we need\na term of type `x \u2261 x`, and `refl` will do.\n{:\/}\n\n\u8fd9\u4e2a\u8bc1\u660e\u662f\u600e\u4e48\u8fd0\u4f5c\u7684\u5462\uff1f`sym` \u53c2\u6570\u7684\u7c7b\u578b\u662f `x \u2261 y`\uff0c\u4f46\u662f\u7b49\u5f0f\u7684\u5de6\u624b\u8fb9\u88ab `refl` \u6a21\u5f0f\u5b9e\u4f8b\u5316\u4e86\uff0c\n\u8fd9\u8981\u6c42 `x` \u548c `y` \u76f8\u7b49\u3002\u56e0\u6b64\uff0c\u7b49\u5f0f\u7684\u53f3\u624b\u8fb9\u9700\u8981\u4e00\u4e2a\u7c7b\u578b\u4e3a `x \u2261 x` \u7684\u9879\uff0c\u7528 `refl` \u5373\u53ef\u3002\n\n{::comment}\nIt is instructive to develop `sym` interactively. To start, we supply\na variable for the argument on the left, and a hole for the body on\nthe right:\n{:\/}\n\n\u4ea4\u4e92\u5f0f\u5730\u8bc1\u660e `sym` \u5f88\u6709\u6559\u80b2\u610f\u4e49\u3002\u9996\u5148\uff0c\u6211\u4eec\u5728\u5de6\u624b\u8fb9\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u8868\u793a\u53c2\u6570\uff0c\u5728\u53f3\u624b\u8fb9\u4f7f\u7528\u4e00\u4e2a\u6d1e\uff1a\n\n sym : \u2200 {A : Set} {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 y \u2261 x\n sym e = {! !}\n\n{::comment}\nIf we go into the hole and type `C-c C-,` then Agda reports:\n{:\/}\n\n\u5982\u679c\u6211\u4eec\u8fdb\u5165\u8fd9\u4e2a\u6d1e\uff0c\u4f7f\u7528 `C-c C-,`\uff0cAgda \u4f1a\u544a\u8bc9\u6211\u4eec\uff1a\n\n Goal: .y \u2261 .x\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n e : .x \u2261 .y\n .y : .A\n .x : .A\n .A : Set\n\n{::comment}\nIf in the hole we type `C-c C-c e` then Agda will instantiate `e` to\nall possible constructors, with one equation for each. There is only\none possible constructor:\n{:\/}\n\n\u5728\u8fd9\u4e2a\u6d1e\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 `C-c C-c e`\uff0cAgda \u4f1a\u5c06 `e` \u9010\u4e00\u5c55\u5f00\u4e3a\u5176\u6240\u6709\u53ef\u80fd\u7684\u6784\u9020\u5668\u3002\n\u6b64\u5904\u53ea\u6709\u4e00\u4e2a\u6784\u9020\u5668\uff1a\n\n sym : \u2200 {A : Set} {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 y \u2261 x\n sym refl = {! !}\n\n{::comment}\nIf we go into the hole again and type `C-c C-,` then Agda now reports:\n{:\/}\n\n\u5982\u679c\u6211\u4eec\u518d\u6b21\u8fdb\u5165\u8fd9\u4e2a\u6d1e\uff0c\u91cd\u65b0\u4f7f\u7528 `C-c C-,`\uff0c\u7136\u540e Agda \u73b0\u5728\u4f1a\u544a\u8bc9\u6211\u4eec\uff1a\n\n Goal: .x \u2261 .x\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n .x : .A\n .A : Set\n\n{::comment}\nThis is the key step---Agda has worked out that `x` and `y` must be\nthe same to match the pattern `refl`!\n{:\/}\n\n\u8fd9\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u2014\u2014 Agda \u53d1\u73b0\u4e86 `x` \u548c `y` \u5fc5\u987b\u76f8\u7b49\uff0c\u624d\u80fd\u4e0e\u6a21\u5f0f `refl` \u76f8\u5339\u914d\u3002\n\n{::comment}\nFinally, if we go back into the hole and type `C-c C-r` it will\ninstantiate the hole with the one constructor that yields a value of\nthe expected type:\n{:\/}\n\n\u6700\u540e\uff0c\u6211\u4eec\u56de\u5230\u6d1e\u91cc\uff0c\u4f7f\u7528 `C-c C-r`\uff0cAgda \u5c06\u4f1a\u628a\u6d1e\u53d8\u6210\u4e00\u4e2a\u53ef\u4ee5\u6ee1\u8db3\u7ed9\u5b9a\u7c7b\u578b\u7684\u6784\u9020\u5668\u5b9e\u4f8b\u3002\n\n sym : \u2200 {A : Set} {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 y \u2261 x\n sym refl = refl\n\n{::comment}\nThis completes the definition as given above.\n{:\/}\n\n\u6211\u4eec\u81f3\u6b64\u5b8c\u6210\u4e86\u4e0e\u4e4b\u524d\u7ed9\u51fa\u8bc1\u660e\u76f8\u540c\u7684\u8bc1\u660e\u3002\n\n{::comment}\nTransitivity is equally straightforward:\n{:\/}\n\n\u4f20\u9012\u6027\u4ea6\u662f\u5f88\u76f4\u63a5\uff1a\n\n\\begin{code}\ntrans : \u2200 {A : Set} {x y z : A}\n \u2192 x \u2261 y\n \u2192 y \u2261 z\n -----\n \u2192 x \u2261 z\ntrans refl refl = refl\n\\end{code}\n\n{::comment}\nAgain, a useful exercise is to carry out an interactive development,\nchecking how Agda's knowledge changes as each of the two arguments is\ninstantiated.\n{:\/}\n\n\u540c\u6837\uff0c\u4ea4\u4e92\u5f0f\u5730\u8bc1\u660e\u8fd9\u4e2a\u7279\u6027\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7ec3\u4e60\uff0c\u5c24\u5176\u662f\u89c2\u5bdf Agda \u7684\u5df2\u77e5\u5185\u5bb9\u6839\u636e\u53c2\u6570\u7684\u5b9e\u4f8b\u800c\u53d8\u5316\u7684\u8fc7\u7a0b\u3002\n\n\n{::comment}\n## Congruence and substitution {#cong}\n{:\/}\n\n## \u5408\u540c\u6027\u548c\u66ff\u6362\u6027 {#cong}\n\n{::comment}\nEquality satisfies _congruence_. If two terms are equal,\nthey remain so after the same function is applied to both:\n{:\/}\n\n\u76f8\u7b49\u6027\u6ee1\u8db3 *\u5408\u540c\u6027*\uff08Congurence\uff09\u3002\u5982\u679c\u4e24\u4e2a\u9879\u76f8\u7b49\uff0c\u90a3\u4e48\u5bf9\u5b83\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u51fd\u6570\uff0c\n\u5176\u7ed3\u679c\u4ecd\u7136\u76f8\u7b49\uff1a\n\n\\begin{code}\ncong : \u2200 {A B : Set} (f : A \u2192 B) {x y : A}\n \u2192 x \u2261 y\n ---------\n \u2192 f x \u2261 f y\ncong f refl = refl\n\\end{code}\n\n{::comment}\nCongruence of functions with two arguments is similar:\n{:\/}\n\n\u4e24\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u4e5f\u6ee1\u8db3\u5408\u540c\u6027\uff1a\n\n\\begin{code}\ncong\u2082 : \u2200 {A B C : Set} (f : A \u2192 B \u2192 C) {u x : A} {v y : B}\n \u2192 u \u2261 x\n \u2192 v \u2261 y\n -------------\n \u2192 f u v \u2261 f x y\ncong\u2082 f refl refl = refl\n\\end{code}\n\n{::comment}\nEquality is also a congruence in the function position of an application.\nIf two functions are equal, then applying them to the same term\nyields equal terms:\n{:\/}\n\n\u5728\u51fd\u6570\u4e0a\u7684\u7b49\u4ef7\u6027\u4e5f\u6ee1\u8db3\u5408\u540c\u6027\u3002\u5982\u679c\u4e24\u4e2a\u51fd\u6570\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u5b83\u4eec\u4f5c\u7528\u5728\u540c\u4e00\u9879\u4e0a\u7684\u7ed3\u679c\u662f\u76f8\u7b49\u7684\uff1a\n\n\\begin{code}\ncong-app : \u2200 {A B : Set} {f g : A \u2192 B}\n \u2192 f \u2261 g\n ---------------------\n \u2192 \u2200 (x : A) \u2192 f x \u2261 g x\ncong-app refl x = refl\n\\end{code}\n\n{::comment}\nEquality also satisfies *substitution*.\nIf two values are equal and a predicate holds of the first then it also holds of the second:\n{:\/}\n\n\u76f8\u7b49\u6027\u4e5f\u6ee1\u8db3*\u66ff\u6362\u6027*\uff08Substitution\uff09\u3002\n\u5982\u679c\u4e24\u4e2a\u503c\u76f8\u7b49\uff0c\u5176\u4e2d\u4e00\u4e2a\u6ee1\u8db3\u67d0\u8c13\u8bcd\uff0c\u90a3\u4e48\u53e6\u4e00\u4e2a\u4e5f\u6ee1\u8db3\u6b64\u8c13\u8bcd\u3002\n\n\\begin{code}\nsubst : \u2200 {A : Set} {x y : A} (P : A \u2192 Set)\n \u2192 x \u2261 y\n ---------\n \u2192 P x \u2192 P y\nsubst P refl px = px\n\\end{code}\n\n\n{::comment}\n## Chains of equations\n{:\/}\n\n## \u7b49\u5f0f\u4e32\n\n{::comment}\nHere we show how to support reasoning with chains of equations, as\nused throughout the book. We package the declarations into a module,\nnamed `\u2261-Reasoning`, to match the format used in Agda's standard\nlibrary:\n{:\/}\n\n\u6211\u4eec\u5728\u6b64\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u7b49\u5f0f\u4e32\u6765\u8bba\u8bc1\uff0c\u6b63\u5982\u672c\u4e66\u4e2d\u4f7f\u7528\u8bc1\u660e\u5f62\u5f0f\u3002\u6211\u4eec\u8bb2\u58f0\u660e\u653e\u5728\u4e00\u4e2a\u53eb\u505a\n`\u2261-Reasoning` \u7684\u6a21\u5757\u91cc\uff0c\u4e0e Agda \u6807\u51c6\u5e93\u4e2d\u7684\u683c\u5f0f\u76f8\u5bf9\u5e94\u3002\n\n\\begin{code}\nmodule \u2261-Reasoning {A : Set} where\n\n infix 1 begin_\n infixr 2 _\u2261\u27e8\u27e9_ _\u2261\u27e8_\u27e9_\n infix 3 _\u220e\n\n begin_ : \u2200 {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 x \u2261 y\n begin x\u2261y = x\u2261y\n\n _\u2261\u27e8\u27e9_ : \u2200 (x : A) {y : A}\n \u2192 x \u2261 y\n -----\n \u2192 x \u2261 y\n x \u2261\u27e8\u27e9 x\u2261y = x\u2261y\n\n _\u2261\u27e8_\u27e9_ : \u2200 (x : A) {y z : A}\n \u2192 x \u2261 y\n \u2192 y \u2261 z\n -----\n \u2192 x \u2261 z\n x \u2261\u27e8 x\u2261y \u27e9 y\u2261z = trans x\u2261y y\u2261z\n\n _\u220e : \u2200 (x : A)\n -----\n \u2192 x \u2261 x\n x \u220e = refl\n\nopen \u2261-Reasoning\n\\end{code}\n\n{::comment}\nThis is our first use of a nested module. It consists of the keyword\n`module` followed by the module name and any parameters, explicit or\nimplicit, the keyword `where`, and the contents of the module indented.\nModules may contain any sort of declaration, including other nested modules.\nNested modules are similar to the top-level modules that constitute\neach chapter of this book, save that the body of a top-level module\nneed not be indented. Opening the module makes all of the definitions\navailable in the current environment.\n{:\/}\n\n\u8fd9\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u4f7f\u7528\u5d4c\u5957\u7684\u6a21\u5757\u3002\u5b83\u5305\u62ec\u4e86\u5173\u952e\u5b57 `module` \u548c\u540e\u7eed\u7684\u6a21\u5757\u540d\u3001\u9690\u5f0f\u6216\u663e\u5f0f\u53c2\u6570\uff0c\n\u5173\u952e\u5b57 `where`\uff0c\u548c\u6a21\u5757\u4e2d\u7684\u5185\u5bb9\uff08\u5728\u7f29\u8fdb\u5185\uff09\u3002\u6a21\u5757\u91cc\u53ef\u4ee5\u5305\u62ec\u4efb\u4f55\u5f62\u5f0f\u7684\u58f0\u660e\uff0c\u4e5f\u53ef\u4ee5\u5305\u62ec\u5176\u4ed6\u6a21\u5757\u3002\n\u5d4c\u5957\u7684\u6a21\u5757\u548c\u672c\u4e66\u6bcf\u7ae0\u8282\u6240\u5b9a\u4e49\u7684\u9876\u5c42\u6a21\u5757\u76f8\u4f3c\uff0c\u53ea\u662f\u9876\u5c42\u6a21\u5757\u4e0d\u9700\u8981\u7f29\u8fdb\u3002\n\u6253\u5f00\uff08Open\uff09\u4e00\u4e2a\u6a21\u5757\u4f1a\u628a\u6a21\u5757\u5185\u7684\u6240\u6709\u5b9a\u4e49\u5bfc\u5165\u8fdb\u5f53\u524d\u7684\u73af\u5883\u4e2d\u3002\n\n{::comment}\nAs an example, let's look at a proof of transitivity\nas a chain of equations:\n{:\/}\n\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u7528\u7b49\u5f0f\u4e32\u8bc1\u660e\u4f20\u9012\u6027\uff1a\n\n\\begin{code}\ntrans\u2032 : \u2200 {A : Set} {x y z : A}\n \u2192 x \u2261 y\n \u2192 y \u2261 z\n -----\n \u2192 x \u2261 z\ntrans\u2032 {A} {x} {y} {z} x\u2261y y\u2261z =\n begin\n x\n \u2261\u27e8 x\u2261y \u27e9\n y\n \u2261\u27e8 y\u2261z \u27e9\n z\n \u220e\n\\end{code}\n\n{::comment}\nAccording to the fixity declarations, the body parses as follows:\n{:\/}\n\n\u6839\u636e\u5176\u5b9a\u4e49\uff0c\u7b49\u5f0f\u53f3\u8fb9\u4f1a\u88ab\u89e3\u6790\u6210\u5982\u4e0b\uff1a\n\n begin (x \u2261\u27e8 x\u2261y \u27e9 (y \u2261\u27e8 y\u2261z \u27e9 (z \u220e)))\n\n{::comment}\nThe application of `begin` is purely cosmetic, as it simply returns\nits argument. That argument consists of `_\u2261\u27e8_\u27e9_` applied to `x`,\n`x\u2261y`, and `y \u2261\u27e8 y\u2261z \u27e9 (z \u220e)`. The first argument is a term, `x`,\nwhile the second and third arguments are both proofs of equations, in\nparticular proofs of `x \u2261 y` and `y \u2261 z` respectively, which are\ncombined by `trans` in the body of `_\u2261\u27e8_\u27e9_` to yield a proof of `x \u2261\nz`. The proof of `y \u2261 z` consists of `_\u2261\u27e8_\u27e9_` applied to `y`, `y\u2261z`,\nand `z \u220e`. The first argument is a term, `y`, while the second and\nthird arguments are both proofs of equations, in particular proofs of\n`y \u2261 z` and `z \u2261 z` respectively, which are combined by `trans` in the\nbody of `_\u2261\u27e8_\u27e9_` to yield a proof of `y \u2261 z`. Finally, the proof of\n`z \u2261 z` consists of `_\u220e` applied to the term `z`, which yields `refl`.\nAfter simplification, the body is equivalent to the term:\n{:\/}\n\n\u8fd9\u91cc `begin` \u7684\u4f7f\u7528\u7eaf\u7cb9\u662f\u88c5\u9970\u6027\u7684\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u8fd4\u56de\u4e86\u5176\u53c2\u6570\u3002\u5176\u53c2\u6570\u5305\u62ec\u4e86\n`_\u2261\u27e8_\u27e9_` \u4f5c\u7528\u4e8e `x`\u3001`x\u2261y` \u548c `y \u2261\u27e8 y\u2261z \u27e9 (z \u220e)`\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u9879 `x`\uff0c\n\u800c\u7b2c\u4e8c\u3001\u7b2c\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u662f\u7b49\u5f0f `x \u2261 y`\u3001`y \u2261 z` \u7684\u8bc1\u660e\uff0c\u5b83\u4eec\u5728 `_\u2261\u27e8_\u27e9_` \u7684\u5b9a\u4e49\u4e2d\u7528\n`trans` \u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210 `x \u2261 z` \u7684\u8bc1\u660e\u3002`y \u2261 z` \u7684\u8bc1\u660e\u5305\u62ec\u4e86 `_\u2261\u27e8_\u27e9_` \u4f5c\u7528\u4e8e `y`\u3001\n`y\u2261z` \u548c `z \u220e`\u3002\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u9879 `y`\uff0c\u800c\u7b2c\u4e8c\u3001\u7b2c\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u662f\u7b49\u5f0f `y \u2261 z`\u3001`z \u2261 z` \u7684\u8bc1\u660e\uff0c\n\u5b83\u4eec\u5728 `_\u2261\u27e8_\u27e9_` \u7684\u5b9a\u4e49\u4e2d\u7528 `trans` \u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210 `y \u2261 z` \u7684\u8bc1\u660e\u3002\u6700\u540e\uff0c`z \u2261 z`\n\u7684\u8bc1\u660e\u5305\u62ec\u4e86 `_\u220e` \u4f5c\u7528\u4e8e `z` \u4e4b\u4e0a\uff0c\u4f7f\u7528\u4e86 `refl`\u3002\u7ecf\u8fc7\u5316\u7b80\uff0c\u4e0a\u8ff0\u5b9a\u4e49\u7b49\u540c\u4e8e\uff1a\n\n trans x\u2261y (trans y\u2261z refl)\n\n{::comment}\nWe could replace any use of a chain of equations by a chain of\napplications of `trans`; the result would be more compact but harder\nto read. The trick behind `\u220e` means that a chain of equalities\nsimplifies to a chain of applications of `trans` that ends in `trans e\nrefl`, where `e` is a term that proves some equality, even though `e`\nalone would do.\n{:\/}\n\n\u6211\u4eec\u53ef\u4ee5\u628a\u4efb\u610f\u7b49\u5f0f\u4e32\u8f6c\u5316\u6210\u4e00\u7cfb\u5217\u7684 `trans` \u7684\u4f7f\u7528\u3002\u8fd9\u6837\u7684\u8bc1\u660e\u66f4\u52a0\u7cbe\u7b80\uff0c\u4f46\u662f\u66f4\u96be\u4ee5\u9605\u8bfb\u3002\n`\u220e` \u7684\u5c0f\u7a8d\u95e8\u610f\u5473\u7740\u7b49\u5f0f\u4e32\u5316\u7b80\u6210\u4e3a\u7684\u4e00\u7cfb\u5217 `trans` \u4f1a\u4ee5 `trans e refl` \u7ed3\u5c3e\uff0c\u5c3d\u7ba1\u53ea\u9700\u8981 `e`\n\u5c31\u8db3\u591f\u4e86\uff0c\u8fd9\u91cc\u7684 `e` \u662f\u7b49\u5f0f\u7684\u8bc1\u660e\u3002\n\n\n{::comment}\n## Chains of equations, another example\n{:\/}\n\n## \u7b49\u5f0f\u4e32\u7684\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\n\n{::comment}\nAs a second example of chains of equations, we repeat the proof that addition\nis commutative. We first repeat the definitions of naturals and addition.\nWe cannot import them because (as noted at the beginning of this chapter)\nit would cause a conflict:\n{:\/}\n\n\u6211\u4eec\u91cd\u65b0\u8bc1\u660e\u52a0\u6cd5\u7684\u4ea4\u6362\u5f8b\u6765\u4f5c\u4e3a\u7b49\u5f0f\u4e32\u7684\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u3002\u6211\u4eec\u9996\u5148\u91cd\u590d\u81ea\u7136\u6570\u548c\u52a0\u6cd5\u7684\u5b9a\u4e49\u3002\n\u6211\u4eec\u4e0d\u80fd\u5bfc\u5165\u5b83\u4eec\uff08\u6b63\u5982\u672c\u7ae0\u8282\u5f00\u5934\u4e2d\u6240\u89e3\u91ca\u7684\u90a3\u6837\uff09\uff0c\u56e0\u4e3a\u90a3\u6837\u4f1a\u4ea7\u751f\u4e00\u4e2a\u51b2\u7a81\uff1a\n\n\\begin{code}\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + n = n\n(suc m) + n = suc (m + n)\n\\end{code}\n\n{::comment}\nTo save space we postulate (rather than prove in full) two lemmas:\n{:\/}\n\n\u4e3a\u4e86\u8282\u7ea6\u7a7a\u95f4\uff0c\u6211\u4eec\u5047\u8bbe\u4e24\u6761\u5f15\u7406\uff08\u800c\u4e0d\u662f\u8bc1\u660e\u5b83\u4eec\uff09\uff1a\n\n\\begin{code}\npostulate\n +-identity : \u2200 (m : \u2115) \u2192 m + zero \u2261 m\n +-suc : \u2200 (m n : \u2115) \u2192 m + suc n \u2261 suc (m + n)\n\\end{code}\n\n{::comment}\nThis is our first use of a _postulate_. A postulate specifies a\nsignature for an identifier but no definition. Here we postulate\nsomething proved earlier to save space. Postulates must be used with\ncaution. If we postulate something false then we could use Agda to\nprove anything whatsoever.\n{:\/}\n\n\u8fd9\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u4f7f\u7528*\u5047\u8bbe*\uff08Postulate\uff09\u3002\u5047\u8bbe\u4e3a\u4e00\u4e2a\u6807\u8bc6\u7b26\u6307\u5b9a\u4e00\u4e2a\u7b7e\u540d\uff0c\u4f46\u662f\u4e0d\u63d0\u4f9b\u5b9a\u4e49\u3002\n\u6211\u4eec\u5728\u8fd9\u91cc\u5047\u8bbe\u4e4b\u524d\u8bc1\u660e\u8fc7\u7684\u4e1c\u897f\uff0c\u6765\u8282\u7ea6\u7a7a\u95f4\u3002\u5047\u8bbe\u5728\u4f7f\u7528\u65f6\u5fc5\u987b\u52a0\u4ee5\u6ce8\u610f\u3002\u5982\u679c\u5047\u8bbe\u7684\u5185\u5bb9\u4e3a\u5047\uff0c\n\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u8bc1\u660e\u51fa\u4efb\u4f55\u4e1c\u897f\u3002\n\n{::comment}\nWe then repeat the proof of commutativity:\n{:\/}\n\n\u6211\u4eec\u63a5\u4e0b\u6765\u91cd\u590d\u4ea4\u6362\u5f8b\u7684\u8bc1\u660e\uff1a\n\n\\begin{code}\n+-comm : \u2200 (m n : \u2115) \u2192 m + n \u2261 n + m\n+-comm m zero =\n begin\n m + zero\n \u2261\u27e8 +-identity m \u27e9\n m\n \u2261\u27e8\u27e9\n zero + m\n \u220e\n+-comm m (suc n) =\n begin\n m + suc n\n \u2261\u27e8 +-suc m n \u27e9\n suc (m + n)\n \u2261\u27e8 cong suc (+-comm m n) \u27e9\n suc (n + m)\n \u2261\u27e8\u27e9\n suc n + m\n \u220e\n\\end{code}\n\n{::comment}\nThe reasoning here is similar to that in the\npreceding section. We use\n`_\u2261\u27e8\u27e9_` when no justification is required.\nOne can think of `_\u2261\u27e8\u27e9_` as equivalent to `_\u2261\u27e8 refl \u27e9_`.\n{:\/}\n\n\u8bba\u8bc1\u7684\u8fc7\u7a0b\u548c\u4e4b\u524d\u7684\u76f8\u4f3c\u3002\u6211\u4eec\u5728\u4e0d\u9700\u8981\u89e3\u91ca\u7684\u5730\u65b9\u4f7f\u7528 `_\u2261\u27e8\u27e9_`\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba4\u4e3a\n`_\u2261\u27e8\u27e9_` \u548c `_\u2261\u27e8 refl \u27e9_` \u662f\u7b49\u4ef7\u7684\u3002\n\n{::comment}\nAgda always treats a term as equivalent to its\nsimplified term. The reason that one can write\n{:\/}\n\nAgda \u603b\u662f\u8ba4\u4e3a\u4e00\u4e2a\u9879\u4e0e\u5176\u5316\u7b80\u7684\u9879\u662f\u7b49\u4ef7\u7684\u3002\u6211\u4eec\u4e4b\u6240\u4ee5\u53ef\u4ee5\u5199\u51fa\n\n suc (n + m)\n \u2261\u27e8\u27e9\n suc n + m\n\n{::comment}\nis because Agda treats both terms as the same.\nThis also means that one could instead interchange\nthe lines and write\n{:\/}\n\n\u662f\u56e0\u4e3a Agda \u8ba4\u4e3a\u5b83\u4eec\u662f\u4e00\u6837\u7684\u3002\u8fd9\u4e5f\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4ea4\u6362\u4e24\u884c\u7684\u987a\u5e8f\uff0c\u5199\u51fa\n\n suc n + m\n \u2261\u27e8\u27e9\n suc (n + m)\n\n{::comment}\nand Agda would not object. Agda only checks that the terms separated\nby `\u2261\u27e8\u27e9` have the same simplified form; it's up to us to write them in\nan order that will make sense to the reader.\n{:\/}\n\n\u800c Agda \u5e76\u4e0d\u4f1a\u53cd\u5bf9\u3002Agda \u53ea\u4f1a\u68c0\u67e5\u7531 `\u2261\u27e8\u27e9` \u9694\u5f00\u7684\u9879\u662f\u5426\u5316\u7b80\u540e\u76f8\u540c\u3002\n\u800c\u4e66\u5199\u7684\u987a\u5e8f\u5408\u4e0d\u5408\u7406\u5219\u662f\u7531\u6211\u4eec\u81ea\u884c\u51b3\u5b9a\u3002\n\n\n{::comment}\n#### Exercise `\u2264-Reasoning` (stretch)\n{:\/}\n\n#### \u7ec3\u4e60 `\u2264-Reasoning` (\u5ef6\u4f38)\n\n{::comment}\nThe proof of monotonicity from\nChapter [Relations][plfa.Relations]\ncan be written in a more readable form by using an analogue of our\nnotation for `\u2261-Reasoning`. Define `\u2264-Reasoning` analogously, and use\nit to write out an alternative proof that addition is monotonic with\nregard to inequality. Rewrite all of `+-mono\u02e1-\u2264`, `+-mono\u02b3-\u2264`, and `+-mono-\u2264`.\n{:\/}\n\n[Relations][plfa.Relations] \u7ae0\u8282\u4e2d\u7684\u5355\u8c03\u6027\u8bc1\u660e\u4ea6\u53ef\u4ee5\u7528\u76f8\u4f3c\u4e8e `\u2261-Reasoning` \u7684\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u7684\u5f62\u5f0f\u7ed9\u51fa\u3002\n\u76f8\u4f3c\u5730\u6765\u5b9a\u4e49 `\u2264-Reasoning`\uff0c\u5e76\u7528\u5176\u91cd\u65b0\u7ed9\u51fa\u52a0\u6cd5\u5bf9\u4e8e\u4e0d\u7b49\u5f0f\u662f\u5355\u8c03\u7684\u8bc1\u660e\u3002\u91cd\u5199 `+-mono\u02e1-\u2264`\u3001`+-mono\u02b3-\u2264`\n\u548c `+-mono-\u2264`\u3002\n\n{::comment}\n\\begin{code}\n-- Your code goes here\n\\end{code}\n{:\/}\n\n\\begin{code}\n-- \u8bf7\u5c06\u4ee3\u7801\u5199\u5728\u6b64\u5904\u3002\n\\end{code}\n\n\n{::comment}\n## Rewriting\n{:\/}\n\n## \u91cd\u5199\n\n{::comment}\nConsider a property of natural numbers, such as being even.\nWe repeat the earlier definition:\n{:\/}\n\n\u8003\u8651\u4e00\u4e2a\u81ea\u7136\u6570\u7684\u6027\u8d28\uff0c\u6bd4\u5982\u8bf4\u4e00\u4e2a\u6570\u662f\u5076\u6570\u3002\u6211\u4eec\u91cd\u590d\u4e4b\u524d\u7ed9\u51fa\u7684\u5b9a\u4e49\uff1a\n\n\\begin{code}\ndata even : \u2115 \u2192 Set\ndata odd : \u2115 \u2192 Set\n\ndata even where\n\n even-zero : even zero\n\n even-suc : \u2200 {n : \u2115}\n \u2192 odd n\n ------------\n \u2192 even (suc n)\n\ndata odd where\n odd-suc : \u2200 {n : \u2115}\n \u2192 even n\n -----------\n \u2192 odd (suc n)\n\\end{code}\n\n{::comment}\nIn the previous section, we proved addition is commutative. Given\nevidence that `even (m + n)` holds, we ought also to be able to take\nthat as evidence that `even (n + m)` holds.\n{:\/}\n\n\u5728\u524d\u9762\u7684\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u8bc1\u660e\u4e86\u52a0\u6cd5\u6ee1\u8db3\u4ea4\u6362\u5f8b\u3002\u7ed9\u5b9a `even (m + n)` \u6210\u7acb\u7684\u8bc1\u636e\uff0c\u6211\u4eec\u5e94\u5f53\u53ef\u4ee5\u7528\u5b83\u6765\u505a\n`even (n + m)` \u6210\u7acb\u7684\u8bc1\u636e\u3002\n\n{::comment}\nAgda includes special notation to support just this kind of reasoning,\nthe `rewrite` notation we encountered earlier.\nTo enable this notation, we use pragmas to tell Agda which type\ncorresponds to equality:\n{:\/}\n\nAgda \u5bf9\u8fd9\u79cd\u8bba\u8bc1\u6709\u7279\u6b8a\u8bb0\u6cd5\u7684\u652f\u6301\u2014\u2014\u6211\u4eec\u4e4b\u524d\u63d0\u5230\u8fc7\u7684 `rewrite` \u8bb0\u6cd5\u3002\u6765\u542f\u7528\u8fd9\u79cd\u8bb0\u6cd5\uff0c\n\u6211\u4eec\u53ea\u7528\u7f16\u8bd1\u7a0b\u5e8f\u6307\u4ee4\u6765\u544a\u8bc9 Agda \u4ec0\u4e48\u7c7b\u578b\u5bf9\u5e94\u76f8\u7b49\u6027\uff1a\n\n\\begin{code}\n{-# BUILTIN EQUALITY _\u2261_ #-}\n\\end{code}\n\n{::comment}\nWe can then prove the desired property as follows:\n{:\/}\n\n\u6211\u4eec\u7136\u540e\u5c31\u53ef\u4ee5\u5982\u4e0b\u8bc1\u660e\u6c42\u8bc1\u7684\u6027\u8d28\uff1a\n\n\\begin{code}\neven-comm : \u2200 (m n : \u2115)\n \u2192 even (m + n)\n ------------\n \u2192 even (n + m)\neven-comm m n ev rewrite +-comm n m = ev\n\\end{code}\n\n{::comment}\nHere `ev` ranges over evidence that `even (m + n)` holds, and we show\nthat it also provides evidence that `even (n + m)` holds. In\ngeneral, the keyword `rewrite` is followed by evidence of an\nequality, and that equality is used to rewrite the type of the\ngoal and of any variable in scope.\n{:\/}\n\n\u5728\u8fd9\u91cc\uff0c`ev` \u5305\u62ec\u4e86\u6240\u6709 `even (m + n)` \u6210\u7acb\u7684\u8bc1\u636e\uff0c\u6211\u4eec\u8bc1\u660e\u5b83\u4ea6\u53ef\u4f5c\u4e3a `even (n + m)`\n\u6210\u7acb\u7684\u8bc1\u636e\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5173\u952e\u5b57 `rewrite` \u4e4b\u540e\u8ddf\u7740\u4e00\u4e2a\u7b49\u5f0f\u7684\u8bc1\u660e\uff0c\u8fd9\u4e2a\u7b49\u5f0f\u88ab\u7528\u4e8e\u91cd\u5199\u76ee\u6807\u548c\u4efb\u610f\u4f5c\u7528\u57df\u5185\u53d8\u91cf\u7684\u7c7b\u578b\u3002\n\n{::comment}\nIt is instructive to develop `even-comm` interactively. To start, we\nsupply variables for the arguments on the left, and a hole for the\nbody on the right:\n{:\/}\n\n\u4ea4\u4e92\u6027\u5730\u8bc1\u660e `even-comm` \u662f\u5f88\u6709\u5e2e\u52a9\u7684\u3002\u4e00\u5f00\u59cb\uff0c\u6211\u4eec\u5148\u7ed9\u5de6\u8fb9\u7684\u53c2\u6570\u8d4b\u4e88\u53d8\u91cf\uff0c\u7ed9\u53f3\u624b\u8fb9\u653e\u4e0a\u4e00\u4e2a\u6d1e\uff1a\n\n even-comm : \u2200 (m n : \u2115)\n \u2192 even (m + n)\n ------------\n \u2192 even (n + m)\n even-comm m n ev = {! !}\n\n{::comment}\nIf we go into the hole and type `C-c C-,` then Agda reports:\n{:\/}\n\n\u5982\u679c\u6211\u4eec\u8fdb\u5165\u6d1e\u91cc\uff0c\u8f93\u5165 `C-c C-,`\uff0cAgda \u4f1a\u62a5\u544a\uff1a\n\n Goal: even (n + m)\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n ev : even (m + n)\n n : \u2115\n m : \u2115\n\n{::comment}\nNow we add the rewrite:\n{:\/}\n\n\u73b0\u5728\u6211\u4eec\u52a0\u5165\u91cd\u5199\uff1a\n\n even-comm : \u2200 (m n : \u2115)\n \u2192 even (m + n)\n ------------\n \u2192 even (n + m)\n even-comm m n ev rewrite +-comm n m = {! !}\n\n{::comment}\nIf we go into the hole again and type `C-c C-,` then Agda now reports:\n{:\/}\n\n\u5982\u679c\u6211\u4eec\u518d\u6b21\u8fdb\u5165\u6d1e\u91cc\uff0c\u5e76\u8f93\u5165 `C-c C-,`\uff0cAgda \u73b0\u5728\u4f1a\u62a5\u544a\uff1a\n\n Goal: even (m + n)\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n ev : even (m + n)\n n : \u2115\n m : \u2115\n\n{::comment}\nThe arguments have been swapped in the goal. Now it is trivial to see\nthat `ev` satisfies the goal, and typing `C-c C-a` in the hole causes\nit to be filled with `ev`. The command `C-c C-a` performs an\nautomated search, including checking whether a variable in scope has\nthe same type as the goal.\n{:\/}\n\n\u76ee\u6807\u91cc\u7684\u53c2\u6570\u88ab\u4ea4\u6362\u4e86\u3002\u73b0\u5728 `ev` \u663e\u7136\u6ee1\u8db3\u76ee\u6807\u6761\u4ef6\uff0c\u8f93\u5165 `C-c C-a` \u4f1a\u7528 `ev` \u6765\u586b\u5145\u8fd9\u4e2a\u6d1e\u3002\n\u547d\u4ee4 `C-c C-a` \u53ef\u4ee5\u8fdb\u884c\u81ea\u52a8\u641c\u7d22\uff0c\u68c0\u67e5\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u662f\u5426\u548c\u76ee\u6807\u6709\u76f8\u540c\u7684\u7c7b\u578b\u3002\n\n\n{::comment}\n## Multiple rewrites\n{:\/}\n\n## \u591a\u91cd\u91cd\u5199\n\n{::comment}\nOne may perform multiple rewrites, each separated by a vertical bar. For instance,\nhere is a second proof that addition is commutative, relying on rewrites rather\nthan chains of equalities:\n{:\/}\n\n\u6211\u4eec\u53ef\u4ee5\u591a\u6b21\u4f7f\u7528\u91cd\u5199\uff0c\u4ee5\u7ad6\u7ebf\u9694\u5f00\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8fd9\u91cc\u662f\u52a0\u6cd5\u4ea4\u6362\u5f8b\u7684\u7b2c\u4e8c\u4e2a\u8bc1\u660e\uff0c\u4f7f\u7528\u91cd\u5199\u800c\u4e0d\u662f\u7b49\u5f0f\u4e32\uff1a\n\n\\begin{code}\n+-comm\u2032 : \u2200 (m n : \u2115) \u2192 m + n \u2261 n + m\n+-comm\u2032 zero n rewrite +-identity n = refl\n+-comm\u2032 (suc m) n rewrite +-suc n m | +-comm\u2032 m n = refl\n\\end{code}\n\n{::comment}\nThis is far more compact. Among other things, whereas the previous\nproof required `cong suc (+-comm m n)` as the justification to invoke\nthe inductive hypothesis, here it is sufficient to rewrite with\n`+-comm m n`, as rewriting automatically takes congruence into\naccount. Although proofs with rewriting are shorter, proofs as chains\nof equalities are easier to follow, and we will stick with the latter\nwhen feasible.\n{:\/}\n\n\u8fd9\u4e2a\u8bc1\u660e\u66f4\u52a0\u7684\u7b80\u77ed\u3002\u4e4b\u524d\u7684\u8bc1\u660e\u7528 `cong suc (+-comm m n)` \u4f5c\u4e3a\u4f7f\u7528\u5f52\u7eb3\u5047\u8bbe\u7684\u8bf4\u660e\uff0c\n\u800c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 `+-comm m n` \u6765\u91cd\u5199\u5c31\u8db3\u591f\u4e86\uff0c\u56e0\u4e3a\u91cd\u5199\u53ef\u4ee5\u5c06\u5408\u540c\u6027\u8003\u8651\u5728\u5176\u4e2d\u3002\u5c3d\u7ba1\u4f7f\u7528\u91cd\u5199\u7684\u8bc1\u660e\u66f4\u52a0\u7684\u7b80\u77ed\uff0c\n\u4f7f\u7528\u7b49\u5f0f\u4e32\u7684\u8bc1\u660e\u80fd\u5bb9\u6613\u7406\u89e3\uff0c\u6211\u4eec\u5c06\u5c3d\u53ef\u80fd\u7684\u4f7f\u7528\u540e\u8005\u3002\n\n\n{::comment}\n## Rewriting expanded\n{:\/}\n\n## \u6df1\u5165\u91cd\u5199\n\n{::comment}\nThe `rewrite` notation is in fact shorthand for an appropriate use of `with`\nabstraction:\n{:\/}\n\n`rewrite` \u8bb0\u6cd5\u5b9e\u9645\u4e0a\u662f `with` \u62bd\u8c61\u7684\u4e00\u79cd\u5e94\u7528\uff1a\n\n\\begin{code}\neven-comm\u2032 : \u2200 (m n : \u2115)\n \u2192 even (m + n)\n ------------\n \u2192 even (n + m)\neven-comm\u2032 m n ev with m + n | +-comm m n\n... | .(n + m) | refl = ev\n\\end{code}\n\n{::comment}\nIn general, one can follow `with` by any number of expressions,\nseparated by bars, where each following equation has the same number\nof patterns. We often write expressions and the corresponding\npatterns so they line up in columns, as above. Here the first column\nasserts that `m + n` and `n + m` are identical, and the second column\njustifies that assertion with evidence of the appropriate equality.\nNote also the use of the _dot pattern_, `.(n + m)`. A dot pattern\nconsists of a dot followed by an expression, and is used when other\ninformation forces the value matched to be equal to the value of the\nexpression in the dot pattern. In this case, the identification of\n`m + n` and `n + m` is justified by the subsequent matching of\n`+-comm m n` against `refl`. One might think that the first clause is\nredundant as the information is inherent in the second clause, but in\nfact Agda is rather picky on this point: omitting the first clause or\nreversing the order of the clauses will cause Agda to report an error.\n(Try it and see!)\n{:\/}\n\n\u603b\u7684\u6765\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 `with` \u540e\u9762\u8ddf\u4e0a\u4efb\u4f55\u6570\u91cf\u7684\u8868\u8fbe\u5f0f\uff0c\u7528\u7ad6\u7ebf\u5206\u9694\u5f00\uff0c\u5e76\u4e14\u5728\u6bcf\u4e2a\u7b49\u5f0f\u4e2d\u4f7f\u7528\u76f8\u540c\u4e2a\u6570\u7684\u6a21\u5f0f\u3002\n\u6211\u4eec\u7ecf\u5e38\u5c06\u8868\u8fbe\u5f0f\u548c\u6a21\u5f0f\u5982\u4e0a\u5bf9\u9f50\u3002\u8fd9\u4e2a\u7b2c\u4e00\u5217\u8868\u660e\u4e86 `m + n` \u548c `n + m` \u662f\u76f8\u540c\u7684\uff0c\u7b2c\u4e8c\u5217\u4f7f\u7528\u76f8\u5e94\u7b49\u5f0f\u6765\u8bc1\u660e\u7684\u524d\u8ff0\u7684\u65ad\u8a00\u3002\n\u6ce8\u610f\u5728\u8fd9\u91cc\u4f7f\u7528\u7684*\u70b9\u6a21\u5f0f*\uff08Dot Pattern\uff09\uff0c`.(n + m)`\u3002\u70b9\u6a21\u5f0f\u7531\u4e00\u4e2a\u70b9\u548c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7ec4\u6210\uff0c\n\u5728\u5176\u4ed6\u4fe1\u606f\u8feb\u4f7f\u8fd9\u4e2a\u503c\u548c\u70b9\u6a21\u5f0f\u4e2d\u7684\u503c\u76f8\u7b49\u65f6\u4f7f\u7528\u3002\u5728\u8fd9\u91cc\uff0c`m + n` \u548c `n + m` \u7531\u540e\u7eed\u7684\n`+-comm m n` \u4e0e `refl` \u7684\u5339\u914d\u6765\u8bc6\u522b\u3002\u6211\u4eec\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u7b2c\u4e00\u79cd\u60c5\u51b5\u662f\u591a\u4f59\u7684\uff0c\u56e0\u4e3a\u7b2c\u4e8c\u79cd\u60c5\u51b5\u4e2d\u624d\u8574\u542b\u4e86\u9700\u8981\u7684\u4fe1\u606f\u3002\n\u4f46\u5b9e\u9645\u4e0a Agda \u5728\u8fd9\u4ef6\u4e8b\u4e0a\u5f88\u6311\u5254\u2014\u2014\u7701\u7565\u7b2c\u4e00\u6761\u6216\u8005\u66f4\u6362\u987a\u5e8f\u4f1a\u8ba9 Agda \u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002\uff08\u8bd5\u4e00\u8bd5\u4f60\u5c31\u77e5\u9053\uff01\uff09\n\n{::comment}\nIn this case, we can avoid rewrite by simply applying the substitution\nfunction defined earlier:\n{:\/}\n\n\u5728\u8fd9\u79cd\u60c5\u51b5\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u7684\u66ff\u6362\u51fd\u6570\u6765\u907f\u514d\u4f7f\u7528\u91cd\u5199\uff1a\n\n\\begin{code}\neven-comm\u2033 : \u2200 (m n : \u2115)\n \u2192 even (m + n)\n ------------\n \u2192 even (n + m)\neven-comm\u2033 m n = subst even (+-comm m n)\n\\end{code}\n\n{::comment}\nNonetheless, rewrite is a vital part of the Agda toolkit. We will use\nit sparingly, but it is occasionally essential.\n{:\/}\n\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u91cd\u5199\u662f Agda \u5de5\u5177\u7bb1\u4e2d\u5f88\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u4f1a\u5076\u5c14\u4f7f\u7528\u5b83\uff0c\u4f46\u662f\u5b83\u6709\u7684\u65f6\u5019\u662f\u5fc5\u8981\u7684\u3002\n\n\n{::comment}\n## Leibniz equality\n{:\/}\n\n## \u83b1\u5e03\u5c3c\u5179\uff08Leibniz\uff09\u76f8\u7b49\u6027\n\n{::comment}\nThe form of asserting equality that we have used is due to Martin\nL\u00f6f, and was published in 1975. An older form is due to Leibniz, and\nwas published in 1686. Leibniz asserted the _identity of\nindiscernibles_: two objects are equal if and only if they satisfy the\nsame properties. This principle sometimes goes by the name Leibniz'\nLaw, and is closely related to Spock's Law, \"A difference that makes\nno difference is no difference\". Here we define Leibniz equality,\nand show that two terms satisfy Leibniz equality if and only if they\nsatisfy Martin L\u00f6f equality.\n{:\/}\n\n\u6211\u4eec\u4f7f\u7528\u7684\u76f8\u7b49\u6027\u65ad\u8a00\u7684\u5f62\u5f0f\u6e90\u4e8e Martin L\u00f6f\uff0c\u4e8e 1975 \u5e74\u53d1\u8868\u3002\u4e00\u4e2a\u66f4\u65e9\u7684\u5f62\u5f0f\u6e90\u4e8e\u83b1\u5e03\u5c3c\u5179\uff0c\n\u4e8e 1686 \u5e74\u53d1\u8868\u3002\u83b1\u5e03\u5c3c\u5179\u65ad\u8a00\u7684\u76f8\u7b49\u6027\u8868\u793a*\u4e0d\u53ef\u5206\u8fa8\u7684\u5b9e\u4f53*\uff08Identity of Indiscernibles\uff09\uff1a\n\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\u5f53\u4e14\u4ec5\u5f53\u5b83\u4eec\u6ee1\u8db3\u5b8c\u5168\u76f8\u540c\u7684\u6027\u8d28\u3002\u8fd9\u6761\u539f\u7406\u6709\u65f6\u88ab\u79f0\u4f5c\u83b1\u5e03\u5c3c\u5179\u5b9a\u5f8b\uff08Leibniz' Law\uff09\uff0c\n\u4e0e\u53f2\u6ce2\u514b\u5b9a\u5f8b\u7d27\u5bc6\u76f8\u5173\uff1a\u201c\u4e00\u4e2a\u4e0d\u9020\u6210\u533a\u522b\u7684\u533a\u522b\u4e0d\u662f\u533a\u522b\u201d\u3002\u6211\u4eec\u5728\u8fd9\u91cc\u5b9a\u4e49\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\uff0c\n\u5e76\u8bc1\u660e\u4e24\u4e2a\u9879\u6ee1\u8db3\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\u5f53\u4e14\u4ec5\u5f53\u5176\u6ee1\u8db3 Martin L\u00f6f \u76f8\u7b49\u6027\u3002\n\n{::comment}\nLeibniz equality is usually formalised to state that `x \u2250 y` holds if\nevery property `P` that holds of `x` also holds of `y`. Perhaps\nsurprisingly, this definition is sufficient to also ensure the\nconverse, that every property `P` that holds of `y` also holds of `x`.\n{:\/}\n\n\u83b1\u5e03\u5c3c\u5179\u4e0d\u7b49\u5f0f\u4e00\u822c\u5982\u4e0b\u6765\u5b9a\u4e49\uff1a`x \u2250 y` \u5f53\u6bcf\u4e2a\u5bf9\u4e8e `x` \u6210\u7acb\u7684\u6027\u8d28 `P` \u5bf9\u4e8e `y` \u4e5f\u6210\u7acb\u65f6\u6210\u7acb\u3002\n\u53ef\u80fd\u8fd9\u6709\u4e9b\u51fa\u4e4e\u610f\u6599\uff0c\u4f46\u662f\u8fd9\u4e2a\u5b9a\u4e49\u4ea6\u8db3\u591f\u4fdd\u8bc1\u5176\u76f8\u53cd\u7684\u547d\u9898\uff1a\u6bcf\u4e2a\u5bf9\u4e8e `y` \u6210\u7acb\u7684\u6027\u8d28 `P` \u5bf9\u4e8e `x` \u4e5f\u6210\u7acb\u3002\n\n{::comment}\nLet `x` and `y` be objects of type `A`. We say that `x \u2250 y` holds if\nfor every predicate `P` over type `A` we have that `P x` implies `P y`:\n{:\/}\n\n\u4ee4 `x` \u548c `y` \u4e3a\u7c7b\u578b `A` \u7684\u5bf9\u8c61\u3002\u6211\u4eec\u5b9a\u4e49 `x \u2250 y` \u6210\u7acb\uff0c\u5f53\u6bcf\u4e2a\u5bf9\u4e8e\u7c7b\u578b `A` \u6210\u7acb\u7684\u8c13\u8bcd `P`\uff0c\n\u6211\u4eec\u6709 `P x` \u8574\u542b\u4e86 `P y`\uff1a\n\n\\begin{code}\n_\u2250_ : \u2200 {A : Set} (x y : A) \u2192 Set\u2081\n_\u2250_ {A} x y = \u2200 (P : A \u2192 Set) \u2192 P x \u2192 P y\n\\end{code}\n\n{::comment}\nWe cannot write the left-hand side of the equation as `x \u2250 y`,\nand instead we write `_\u2250_ {A} x y` to provide access to the implicit\nparameter `A` which appears on the right-hand side.\n{:\/}\n\n\u6211\u4eec\u4e0d\u80fd\u5728\u5de6\u624b\u8fb9\u4f7f\u7528 `x \u2250 y`\uff0c\u53d6\u800c\u4ee3\u4e4b\u6211\u4eec\u4f7f\u7528 `_\u2250_ {A} x y` \u6765\u63d0\u4f9b\u9690\u5f0f\u53c2\u6570 `A`\uff0c\u8fd9\u6837 `A`\n\u53ef\u4ee5\u51fa\u73b0\u5728\u53f3\u624b\u8fb9\u3002\n\n{::comment}\nThis is our first use of _levels_. We cannot assign `Set` the type\n`Set`, since this would lead to contradictions such as Russell's\nParadox and Girard's Paradox. Instead, there is a hierarchy of types,\nwhere `Set : Set\u2081`, `Set\u2081 : Set\u2082`, and so on. In fact, `Set` itself\nis just an abbreviation for `Set\u2080`. Since the equation defining `_\u2250_`\nmentions `Set` on the right-hand side, the corresponding signature\nmust use `Set\u2081`. We say a bit more about levels below.\n{:\/}\n\n\u8fd9\u662f\u6211\u4eec\u7b2c\u4e00\u6b21\u4f7f\u7528*\u7b49\u7ea7*\uff08Levels\uff09\u3002\u6211\u4eec\u4e0d\u80fd\u5c06 `Set` \u8d4b\u4e88\u7c7b\u578b `Set`\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u81ea\u76f8\u77db\u76fe\uff0c\n\u6bd4\u5982\u7f57\u7d20\u6096\u8bba\uff08Russell's Paradox\uff09\u6216\u8005 Girard \u6096\u8bba\u3002\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u9636\u7ea7\u7684\u7c7b\u578b\uff1a\u5176\u4e2d\n`Set : Set\u2081`\uff0c`Set\u2081 : Set\u2082`\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u5b9e\u9645\u4e0a\uff0c`Set` \u672c\u8eab\u5c31\u662f `Set\u2080` \u7684\u7f29\u5199\u3002\u5b9a\u4e49\n`_\u2250_` \u7684\u7b49\u5f0f\u5728\u53f3\u624b\u8fb9\u63d0\u5230\u4e86 `Set`\uff0c\u56e0\u6b64\u7b7e\u540d\u4e2d\u5fc5\u987b\u4f7f\u7528 `Set\u2081`\u3002\u6211\u4eec\u7a0d\u540e\u5c06\u8fdb\u4e00\u6b65\u4ecb\u7ecd\u7b49\u7ea7\u3002\n\n{::comment}\nLeibniz equality is reflexive and transitive,\nwhere the first follows by a variant of the identity function\nand the second by a variant of function composition:\n{:\/}\n\n\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\u662f\u81ea\u53cd\u548c\u4f20\u9012\u7684\u3002\u81ea\u53cd\u6027\u7531\u6052\u7b49\u51fd\u6570\u7684\u53d8\u79cd\u5f97\u6765\uff0c\u4f20\u9012\u6027\u7531\u51fd\u6570\u7ec4\u5408\u7684\u53d8\u79cd\u5f97\u6765\uff1a\n\n\\begin{code}\nrefl-\u2250 : \u2200 {A : Set} {x : A}\n \u2192 x \u2250 x\nrefl-\u2250 P Px = Px\n\ntrans-\u2250 : \u2200 {A : Set} {x y z : A}\n \u2192 x \u2250 y\n \u2192 y \u2250 z\n -----\n \u2192 x \u2250 z\ntrans-\u2250 x\u2250y y\u2250z P Px = y\u2250z P (x\u2250y P Px)\n\\end{code}\n\n{::comment}\nSymmetry is less obvious. We have to show that if `P x` implies `P y`\nfor all predicates `P`, then the implication holds the other way round\nas well:\n{:\/}\n\n\u5bf9\u79f0\u6027\u5c31\u6ca1\u6709\u90a3\u4e48\u663e\u7136\u4e86\u3002\u6211\u4eec\u9700\u8981\u8bc1\u660e\u5982\u679c\u5bf9\u4e8e\u6240\u6709\u8c13\u8bcd `P`\uff0c`P x` \u8574\u542b `P y`\uff0c\n\u90a3\u4e48\u53cd\u65b9\u5411\u7684\u8574\u542b\u4e5f\u6210\u7acb\u3002\n\n\\begin{code}\nsym-\u2250 : \u2200 {A : Set} {x y : A}\n \u2192 x \u2250 y\n -----\n \u2192 y \u2250 x\nsym-\u2250 {A} {x} {y} x\u2250y P = Qy\n where\n Q : A \u2192 Set\n Q z = P z \u2192 P x\n Qx : Q x\n Qx = refl-\u2250 P\n Qy : Q y\n Qy = x\u2250y Q Qx\n\\end{code}\n\n{::comment}\nGiven `x \u2250 y`, a specific `P`, we have to construct a proof that `P y`\nimplies `P x`. To do so, we instantiate the equality with a predicate\n`Q` such that `Q z` holds if `P z` implies `P x`. The property `Q x`\nis trivial by reflexivity, and hence `Q y` follows from `x \u2250 y`. But\n`Q y` is exactly a proof of what we require, that `P y` implies `P x`.\n{:\/}\n\n\u7ed9\u5b9a `x \u2250 y` \u548c\u4e00\u4e2a\u7279\u5b9a\u7684 `P`\uff0c\u6211\u4eec\u9700\u8981\u6784\u9020\u4e00\u4e2a `P y` \u8574\u542b `P x` \u7684\u8bc1\u660e\u3002\n\u6211\u4eec\u9996\u5148\u7528\u4e00\u4e2a\u8c13\u8bcd `Q` \u5c06\u76f8\u7b49\u6027\u5b9e\u4f8b\u5316\uff0c\u4f7f\u5f97 `Q z` \u5728 `P z` \u8574\u542b `P x` \u65f6\u6210\u7acb\u3002\n`Q x` \u8fd9\u4e2a\u6027\u8d28\u662f\u663e\u7136\u7684\uff0c\u7531\u81ea\u53cd\u6027\u53ef\u4ee5\u5f97\u51fa\uff0c\u7531\u6b64\u901a\u8fc7 `x \u2250 y` \u5c31\u80fd\u63a8\u51fa `Q y` \u6210\u7acb\u3002\u800c `Q y`\n\u6b63\u662f\u6211\u4eec\u9700\u8981\u7684\u8bc1\u660e\uff0c\u5373 `P y` \u8574\u542b `P x`\u3002\n\n{::comment}\nWe now show that Martin L\u00f6f equality implies\nLeibniz equality, and vice versa. In the forward direction, if we know\n`x \u2261 y` we need for any `P` to take evidence of `P x` to evidence of `P y`,\nwhich is easy since equality of `x` and `y` implies that any proof\nof `P x` is also a proof of `P y`:\n{:\/}\n\n\u6211\u4eec\u73b0\u5728\u6765\u8bc1\u660e Martin L\u00f6f \u76f8\u7b49\u6027\u8574\u542b\u4e86\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\uff0c\u4ee5\u53ca\u5176\u9006\u547d\u9898\u3002\u5728\u6b63\u65b9\u5411\u4e0a\uff0c\n\u5982\u679c\u6211\u4eec\u5df2\u77e5 `x \u2261 y`\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u4e8e\u4efb\u610f\u7684 `P`\uff0c\u5c06 `P x` \u7684\u8bc1\u660e\u8f6c\u6362\u4e3a `P y` \u7684\u8bc1\u660e\u3002\n\u6211\u4eec\u5f88\u5bb9\u6613\u5c31\u53ef\u4ee5\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u56e0\u4e3a `x` \u4e0e `y` \u76f8\u7b49\u610f\u5473\u7740\u4efb\u4f55 `P x` \u7684\u8bc1\u660e\u5373\u662f `P y` \u7684\u8bc1\u660e\u3002\n\n\\begin{code}\n\u2261-implies-\u2250 : \u2200 {A : Set} {x y : A}\n \u2192 x \u2261 y\n -----\n \u2192 x \u2250 y\n\u2261-implies-\u2250 x\u2261y P = subst P x\u2261y\n\\end{code}\n\n{::comment}\nThis direction follows from substitution, which we showed earlier.\n{:\/}\n\n\u56e0\u4e3a\u8fd9\u4e2a\u65b9\u5411\u7531\u66ff\u6362\u6027\u53ef\u4ee5\u5f97\u6765\uff0c\u5982\u4e4b\u524d\u8bc1\u660e\u7684\u90a3\u6837\u3002\n\n{::comment}\nIn the reverse direction, given that for any `P` we can take a proof of `P x`\nto a proof of `P y` we need to show `x \u2261 y`:\n{:\/}\n\n\u5728\u53cd\u65b9\u5411\u4e0a\uff0c\u6211\u4eec\u5df2\u77e5\u5bf9\u4e8e\u4efb\u4f55 `P`\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 `P x` \u7684\u8bc1\u660e\u8f6c\u6362\u6210 `P y` \u7684\u8bc1\u660e\uff0c\n\u6211\u4eec\u9700\u8981\u8bc1\u660e `x \u2261 y`\uff1a\n\n\\begin{code}\n\u2250-implies-\u2261 : \u2200 {A : Set} {x y : A}\n \u2192 x \u2250 y\n -----\n \u2192 x \u2261 y\n\u2250-implies-\u2261 {A} {x} {y} x\u2250y = Qy\n where\n Q : A \u2192 Set\n Q z = x \u2261 z\n Qx : Q x\n Qx = refl\n Qy : Q y\n Qy = x\u2250y Q Qx\n\\end{code}\n\n{::comment}\nThe proof is similar to that for symmetry of Leibniz equality. We take\n`Q` to be the predicate that holds of `z` if `x \u2261 z`. Then `Q x` is\ntrivial by reflexivity of Martin L\u00f6f equality, and hence `Q y`\nfollows from `x \u2250 y`. But `Q y` is exactly a proof of what we\nrequire, that `x \u2261 y`.\n{:\/}\n\n\u6b64\u8bc1\u660e\u4e0e\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\u7684\u5bf9\u79f0\u6027\u8bc1\u660e\u76f8\u4f3c\u3002\u6211\u4eec\u53d6\u8c13\u8bcd `Q`\uff0c\u4f7f\u5f97 `Q z` \u5728 `x \u2261 z` \u6210\u7acb\u65f6\u6210\u7acb\u3002\n\u90a3\u4e48 `Q x` \u662f\u663e\u7136\u7684\uff0c\u7531 Martin L\u00f6f \u76f8\u7b49\u6027\u7684\u81ea\u53cd\u6027\u5f97\u6765\u3002\u4ece\u800c `Q y` \u7531 `x \u2250 y` \u53ef\u5f97\uff0c\n\u800c `Q y` \u5373\u662f\u6211\u4eec\u6240\u9700\u8981\u7684 `x \u2261 y` \u7684\u8bc1\u660e\u3002\n\n{::comment}\n(Parts of this section are adapted from *\u2250\u2243\u2261: Leibniz Equality is\nIsomorphic to Martin-L\u00f6f Identity, Parametrically*, by Andreas Abel,\nJesper Cockx, Dominique Devries, Andreas Nuyts, and Philip Wadler,\ndraft, 2017.)\n{:\/}\n\n\uff08\u672c\u90e8\u5206\u7684\u5185\u5bb9\u7531\u6b64\u5904\u6539\u7f16\u5f97\u6765\uff1a\n*\u2250\u2243\u2261: Leibniz Equality is\nIsomorphic to Martin-L\u00f6f Identity, Parametrically*\n\u4f5c\u8005\uff1aAndreas Abel\u3001Jesper Cockx\u3001Dominique Devries\u3001Andreas Nuyts \u4e0e Philip Wadler\uff0c\n\u8349\u7a3f\uff0c2017\uff09\n\n\n{::comment}\n## Universe polymorphism {#unipoly}\n{:\/}\n\n## \u5168\u4f53\u591a\u6001 {#unipoly}\n\n{::comment}\nAs we have seen, not every type belongs to `Set`, but instead every\ntype belongs somewhere in the hierarchy `Set\u2080`, `Set\u2081`, `Set\u2082`, and so on,\nwhere `Set` abbreviates `Set\u2080`, and `Set\u2080 : Set\u2081`, `Set\u2081 : Set\u2082`, and so on.\nThe definition of equality given above is fine if we want to compare two\nvalues of a type that belongs to `Set`, but what if we want to compare\ntwo values of a type that belongs to `Set \u2113` for some arbitrary level `\u2113`?\n{:\/}\n\n\u6b63\u5982\u6211\u4eec\u4e4b\u524d\u770b\u5230\u7684\u90a3\u6837\uff0c\u4e0d\u662f\u6bcf\u4e2a\u7c7b\u578b\u90fd\u5c5e\u4e8e `Set`\uff0c\u4f46\u662f\u6bcf\u4e2a\u7c7b\u578b\u90fd\u5c5e\u4e8e\u7c7b\u578b\u9636\u7ea7\u7684\u67d0\u5904\uff0c\n`Set\u2080`\u3001`Set\u2081`\u3001`Set\u2082`\u7b49\u7b49\u3002\u5176\u4e2d `Set` \u662f `Set\u2080` \u7684\u7f29\u5199\uff0c\u6b64\u5916 `Set\u2080 : Set\u2081`\uff0c`Set\u2081 : Set\u2082`\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\n\u5f53\u6211\u4eec\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u5c5e\u4e8e `Set` \u7684\u7c7b\u578b\u7684\u503c\u65f6\uff0c\u6211\u4eec\u4e4b\u524d\u7ed9\u51fa\u7684\u5b9a\u4e49\u662f\u8db3\u591f\u7684\uff0c\n\u4f46\u5982\u679c\u6211\u4eec\u9700\u8981\u6bd4\u8f83\u5bf9\u4e8e\u4efb\u4f55\u7b49\u7ea7 `\u2113`\uff0c\u4e24\u4e2a\u5c5e\u4e8e `Set \u2113` \u7684\u7c7b\u578b\u7684\u503c\u8be5\u600e\u4e48\u529e\u5462\uff1f\n\n{::comment}\nThe answer is _universe polymorphism_, where a definition is made\nwith respect to an arbitrary level `\u2113`. To make use of levels, we\nfirst import the following:\n{:\/}\n\n\u7b54\u6848\u662f*\u5168\u4f53\u591a\u6001*\uff08Universe Polymorphism\uff09\uff0c\u4e00\u4e2a\u5b9a\u4e49\u53ef\u4ee5\u6839\u636e\u4efb\u4f55\u7b49\u7ea7 `\u2113` \u6765\u505a\u51fa\u3002\n\u4e3a\u4e86\u4f7f\u7528\u7b49\u7ea7\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e0b\u5217\u5185\u5bb9\uff1a\n\n\\begin{code}\nopen import Level using (Level; _\u2294_) renaming (zero to lzero; suc to lsuc)\n\\end{code}\n\n{::comment}\nWe rename constructors `zero` and `suc` to `lzero` and `lsuc` to avoid confusion\nbetween levels and naturals.\n{:\/}\n\n\u6211\u4eec\u5c06\u6784\u9020\u5668 `zero` \u548c `suc` \u91cd\u547d\u540d\u81f3 `lzero` \u548c `lsuc`\uff0c\u4e3a\u4e86\u9632\u6b62\u81ea\u7136\u6570\u548c\u7b49\u7ea7\u4e4b\u95f4\u7684\u6df7\u6dc6\u3002\n\n{::comment}\nLevels are isomorphic to natural numbers, and have similar constructors:\n{:\/}\n\n\u7b49\u7ea7\u4e0e\u81ea\u7136\u6570\u662f\u540c\u6784\u7684\uff0c\u6709\u76f8\u4f3c\u7684\u6784\u9020\u5668\uff1a\n\n lzero : Level\n lsuc : Level \u2192 Level\n\n{::comment}\nThe names `Set\u2080`, `Set\u2081`, `Set\u2082`, and so on, are abbreviations for\n{:\/}\n\n`Set\u2080`\u3001`Set\u2081`\u3001`Set\u2082` \u7b49\u540d\u79f0\uff0c\u662f\u4e0b\u5217\u7684\u7b80\u5199\uff1a\n\n Set lzero\n Set (lsuc lzero)\n Set (lsuc (lsuc lzero))\n\n{::comment}\nand so on. There is also an operator\n{:\/}\n\n\u4ee5\u6b64\u7c7b\u63a8\u3002\u6211\u4eec\u8fd8\u6709\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff1a\n\n _\u2294_ : Level \u2192 Level \u2192 Level\n\n{::comment}\nthat given two levels returns the larger of the two.\n{:\/}\n\n\u7ed9\u5b9a\u4e24\u4e2a\u7b49\u7ea7\uff0c\u8fd4\u56de\u4e24\u8005\u4e2d\u8f83\u5927\u7684\u90a3\u4e2a\u3002\n\n{::comment}\nHere is the definition of equality, generalised to an arbitrary level:\n{:\/}\n\n\u4e0b\u9762\u662f\u76f8\u7b49\u6027\u7684\u5b9a\u4e49\uff0c\u63a8\u5e7f\u5230\u4efb\u610f\u7b49\u7ea7\uff1a\n\n\\begin{code}\ndata _\u2261\u2032_ {\u2113 : Level} {A : Set \u2113} (x : A) : A \u2192 Set \u2113 where\n refl\u2032 : x \u2261\u2032 x\n\\end{code}\n\n{::comment}\nSimilarly, here is the generalised definition of symmetry:\n{:\/}\n\n\u76f8\u4f3c\u7684\uff0c\u4e0b\u9762\u662f\u5bf9\u79f0\u6027\u7684\u63a8\u5e7f\u5b9a\u4e49\uff1a\n\n\\begin{code}\nsym\u2032 : \u2200 {\u2113 : Level} {A : Set \u2113} {x y : A}\n \u2192 x \u2261\u2032 y\n ------\n \u2192 y \u2261\u2032 x\nsym\u2032 refl\u2032 = refl\u2032\n\\end{code}\n\n{::comment}\nFor simplicity, we avoid universe polymorphism in the definitions given in\nthe text, but most definitions in the standard library, including those for\nequality, are generalised to arbitrary levels as above.\n{:\/}\n\n\u4e3a\u4e86\u7b80\u4ecb\uff0c\u6211\u4eec\u5728\u672c\u4e66\u4e2d\u7ed9\u51fa\u7684\u5b9a\u4e49\u5c06\u907f\u514d\u4f7f\u7528\u5168\u4f53\u591a\u6001\uff0c\u4f46\u662f\u5927\u591a\u6570\u6807\u51c6\u5e93\u4e2d\u7684\u5b9a\u4e49\uff0c\n\u5305\u62ec\u76f8\u7b49\u6027\u7684\u5b9a\u4e49\uff0c\u90fd\u63a8\u5e7f\u5230\u4e86\u4efb\u610f\u7b49\u7ea7\uff0c\u5982\u4e0a\u6240\u793a\u3002\n\n{::comment}\nHere is the generalised definition of Leibniz equality:\n{:\/}\n\n\u4e0b\u9762\u662f\u83b1\u5e03\u5c3c\u5179\u76f8\u7b49\u6027\u7684\u63a8\u5e7f\u5b9a\u4e49\uff1a\n\n\\begin{code}\n_\u2250\u2032_ : \u2200 {\u2113 : Level} {A : Set \u2113} (x y : A) \u2192 Set (lsuc \u2113)\n_\u2250\u2032_ {\u2113} {A} x y = \u2200 (P : A \u2192 Set \u2113) \u2192 P x \u2192 P y\n\\end{code}\n\n{::comment}\nBefore the signature used `Set\u2081` as the type of a term that includes\n`Set`, whereas here the signature uses `Set (lsuc \u2113)` as the type of a\nterm that includes `Set \u2113`.\n{:\/}\n\n\u4e4b\u524d\uff0c\u7b7e\u540d\u4e2d\u4f7f\u7528\u4e86 `Set\u2081` \u6765\u4f5c\u4e3a\u4e00\u4e2a\u503c\u5305\u62ec\u4e86 `Set` \u7684\u7c7b\u578b\uff1b\u800c\u6b64\u5904\uff0c\u6211\u4eec\u4f7f\u7528\n`Set (lsuc \u2113)` \u6765\u4f5c\u4e3a\u4e00\u4e2a\u503c\u5305\u62ec\u4e86 `Set \u2113` \u7684\u7c7b\u578b\u3002\n\n{::comment}\nFurther information on levels can be found in the [Agda Wiki][wiki].\n{:\/}\n\n\u66f4\u591a\u7684\u5173\u4e8e\u7b49\u7ea7\u7684\u4fe1\u606f\u53ef\u4ee5\u4ece[Agda \u7ef4\u57fa\uff08\u82f1\u6587\uff09][wiki]\u4e2d\u67e5\u8be2\u3002\n\n[wiki]: http:\/\/wiki.portal.chalmers.se\/agda\/pmwiki.php?n=ReferenceManual.UniversePolymorphism\n\n\n{::comment}\n## Standard library\n{:\/}\n\n## \u6807\u51c6\u5e93\n\n{::comment}\nDefinitions similar to those in this chapter can be found in the\nstandard library:\n{:\/}\n\n\u6807\u51c6\u5e93\u4e2d\u53ef\u4ee5\u627e\u5230\u4e0e\u672c\u7ae0\u8282\u4e2d\u76f8\u4f3c\u7684\u5b9a\u4e49\uff1a\n\n\\begin{code}\n-- import Relation.Binary.PropositionalEquality as Eq\n-- open Eq using (_\u2261_; refl; trans; sym; cong; cong-app; subst)\n-- open Eq.\u2261-Reasoning using (begin_; _\u2261\u27e8\u27e9_; _\u2261\u27e8_\u27e9_; _\u220e)\n\\end{code}\n\n{::comment}\nHere the imports are shown as comments rather than code to avoid\ncollisions, as mentioned in the introduction.\n{:\/}\n\n\u8fd9\u91cc\u7684\u5bfc\u5165\u4ee5\u6ce8\u91ca\u7684\u5f62\u5f0f\u7ed9\u51fa\uff0c\u4ee5\u9632\u6b62\u51b2\u7a81\uff0c\u5982\u5f15\u8a00\u4e2d\u89e3\u91ca\u7684\u90a3\u6837\u3002\n\n\n## Unicode\n\n{::comment}\nThis chapter uses the following unicode:\n\n \u2261 U+2261 IDENTICAL TO (\\==, \\equiv)\n \u27e8 U+27E8 MATHEMATICAL LEFT ANGLE BRACKET (\\<)\n \u27e9 U+27E9 MATHEMATICAL RIGHT ANGLE BRACKET (\\>)\n \u220e U+220E END OF PROOF (\\qed)\n \u2250 U+2250 APPROACHES THE LIMIT (\\.=)\n \u2113 U+2113 SCRIPT SMALL L (\\ell)\n \u2294 U+2294 SQUARE CUP (\\lub)\n \u2080 U+2080 SUBSCRIPT ZERO (\\_0)\n \u2081 U+2081 SUBSCRIPT ONE (\\_1)\n \u2082 U+2082 SUBSCRIPT TWO (\\_2)\n{:\/}\n\n\u672c\u7ae0\u8282\u4f7f\u7528\u4e0b\u5217 Unicode\uff1a\n\n \u2261 U+2261 \u7b49\u540c\u4e8e (\\==, \\equiv)\n \u27e8 U+27E8 \u6570\u5b66\u5de6\u5c16\u62ec\u53f7 (\\<)\n \u27e9 U+27E9 \u6570\u5b66\u53f3\u5c16\u62ec\u53f7 (\\>)\n \u220e U+220E \u8bc1\u6bd5 (\\qed)\n \u2250 U+2250 \u63a5\u8fd1\u4e8e\u6781\u9650 (\\.=)\n \u2113 U+2113 \u624b\u5199\u5c0f\u5199 L (\\ell)\n \u2294 U+2294 \u6b63\u65b9\u5f62\u5411\u4e0a\u5f00\u53e3 (\\lub)\n \u2080 U+2080 \u4e0b\u6807 0 (\\_0)\n \u2081 U+2081 \u4e0b\u6807 1 (\\_1)\n \u2082 U+2082 \u4e0b\u6807 2 (\\_2)\n","avg_line_length":25.1223832528,"max_line_length":93,"alphanum_fraction":0.6543490802} +{"size":26434,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"StlcOld: The Simply Typed Lambda-Calculus\"\npermalink : \/StlcOld\n---\n\n[Original version]\n\nThe _lambda-calculus_, first published by the logician Alonzo Church in\n1932, is a core calculus with only three syntactic constructs:\nvariables, abstraction, and application. It embodies the concept of\n_functional abstraction_, which shows up in almost every programming\nlanguage in some form (as functions, procedures, or methods).\nThe _simply-typed lambda calculus_ (or STLC) is a variant of the\nlambda calculus published by Church in 1940. It has just the three\nconstructs above for function types, plus whatever else is required\nfor base types. Church had a minimal base type with no operations;\nwe will be slightly more pragmatic and choose booleans as our base type.\n\nThis chapter formalises the STLC (syntax, small-step semantics, and typing rules),\nand the next chapter reviews its main properties (progress and preservation).\nThe new technical challenges arise from the mechanisms of\n_variable binding_ and _substitution_.\n\n\n\nWe choose booleans as our base type for simplicity. At the end of the\nchapter we'll see how to add numbers as a base type, and in later\nchapters we'll enrich STLC with useful constructs like pairs, sums,\nlists, records, subtyping, and mutable state.\n\n## Imports\n\n\\begin{code}\nopen import Maps using (Id; id; _\u225f_; PartialMap; module PartialMap)\nopen PartialMap using (\u2205; just-injective) renaming (_,_\u21a6_ to _,_\u2236_)\nopen import Data.Nat using (\u2115)\nopen import Data.Maybe using (Maybe; just; nothing)\nopen import Relation.Nullary using (Dec; yes; no; \u00ac_)\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; _\u2262_; refl)\n\\end{code}\n\n## Syntax\n\nWe have just two types.\n\n * Functions, `A \u21d2 B`\n * Booleans, `\ud835\udd39`\n\nWe require some form of base type, because otherwise the set of types\nwould be empty. Church used a trivial base type `o` with no operations.\nFor us, it is more convenient to use booleans. Later we will consider\nnumbers as a base type.\n\nHere is the syntax of types in BNF.\n\n A, B, C ::= A \u21d2 B | \ud835\udd39\n\nAnd here it is formalised in Agda.\n\n\\begin{code}\ninfixr 20 _\u21d2_\n\ndata Type : Set where\n _\u21d2_ : Type \u2192 Type \u2192 Type\n \ud835\udd39 : Type\n\\end{code}\n\nTerms have six constructs. Three are for the core lambda calculus:\n\n * Variables, `` ` x ``\n * Abstractions, `\u03bb[ x \u2236 A ] N`\n * Applications, `L \u00b7 M`\n\nand three are for the base type, booleans:\n\n * True, `true`\n * False, `false`\n * Conditions, `if L then M else N`\n\nAbstraction is also called lambda abstraction, and is the construct\nfrom which the calculus takes its name.\n\nWith the exception of variables, each term form either constructs\na value of a given type (abstractions yield functions, true and\nfalse yield booleans) or deconstructs it (applications use functions,\nconditionals use booleans). We will see this again when we come\nto the rules for assigning types to terms, where constructors\ncorrespond to introduction rules and deconstructors to eliminators.\n\nHere is the syntax of terms in BNF.\n\n L, M, N ::= ` x | \u03bb[ x \u2236 A ] N | L \u00b7 M | true | false | if L then M else N\n\nAnd here it is formalised in Agda.\n\n\\begin{code}\ninfixl 20 _\u00b7_\ninfix 15 \u03bb[_\u2236_]_\ninfix 15 if_then_else_\n\ndata Term : Set where\n ` : Id \u2192 Term\n \u03bb[_\u2236_]_ : Id \u2192 Type \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n true : Term\n false : Term\n if_then_else_ : Term \u2192 Term \u2192 Term \u2192 Term\n\\end{code}\n\n#### Special characters\n\nWe use the following special characters\n\n \u21d2 U+21D2: RIGHTWARDS DOUBLE ARROW (\\=>)\n ` U+0060: GRAVE ACCENT\n \u03bb U+03BB: GREEK SMALL LETTER LAMBDA (\\Gl or \\lambda)\n \u2236 U+2236: RATIO (\\:)\n \u00b7 U+00B7: MIDDLE DOT (\\cdot)\n\nNote that \u2236 (U+2236 RATIO) is not the same as : (U+003A COLON).\nColon is reserved in Agda for declaring types. Everywhere that we\ndeclare a type in the object language rather than Agda we use\nratio in place of colon.\n\nUsing ratio for this purpose is arguably a bad idea, because one must use context\nrather than appearance to distinguish it from colon. Arguably, it might be\nbetter to use a different symbol, such as `\u2208` or `::`. We reserve `\u2208`\nfor use later to indicate that a variable appears free in a term, and\neschew `::` because we consider it too ugly.\n\n\n#### Formal vs informal\n\nIn informal presentation of formal semantics, one uses choice of\nvariable name to disambiguate and writes `x` rather than `` ` x ``\nfor a term that is a variable. Agda requires we distinguish.\nOften researchers use `var x` rather than `` ` x ``, but we chose\nthe latter since it is closer to the informal notation `x`.\n\nSimilarly, informal presentation often use the notations `A \u2192 B` for\nfunctions, `\u03bb x . N` for abstractions, and `L M` for applications. We\ncannot use these, because they overlap with the notation used by Agda.\nIn `\u03bb[ x \u2236 A ] N`, recall that Agda treats square brackets `[]` as\nordinary symbols, while round parentheses `()` and curly braces `{}`\nhave special meaning. We would use `L @ M` for application, but\n`@` has a reserved meaning in Agda.\n\n\n#### Examples\n\nHere are a couple of example terms, `not` of type\n`\ud835\udd39 \u21d2 \ud835\udd39`, which complements its argument, and `two` of type\n`(\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39 \u21d2 \ud835\udd39` which takes a function and a boolean\nand applies the function to the boolean twice.\n\n\\begin{code}\nf x y : Id\nf = id 0\nx = id 1\ny = id 2\n\nnot two : Term\nnot = \u03bb[ x \u2236 \ud835\udd39 ] (if ` x then false else true)\ntwo = \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)\n\\end{code}\n\n\n#### Bound and free variables\n\nIn an abstraction `\u03bb[ x \u2236 A ] N` we call `x` the _bound_ variable\nand `N` the _body_ of the abstraction. One of the most important\naspects of lambda calculus is that names of bound variables are\nirrelevant. Thus the five terms\n\n* `` \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) ``\n* `` \u03bb[ g \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ y \u2236 \ud835\udd39 ] ` g \u00b7 (` g \u00b7 ` y) ``\n* `` \u03bb[ fred \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ xander \u2236 \ud835\udd39 ] ` fred \u00b7 (` fred \u00b7 ` xander) ``\n* `` \u03bb[ \ud83d\ude07 \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ \ud83d\ude08 \u2236 \ud835\udd39 ] ` \ud83d\ude07 \u00b7 (` \ud83d\ude07 \u00b7 ` \ud83d\ude08 ) ``\n* `` \u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ f \u2236 \ud835\udd39 ] ` x \u00b7 (` x \u00b7 ` f) ``\n\nare all considered equivalent. This equivalence relation\nis sometimes called _alpha renaming_.\n\nAs we descend from a term into its subterms, variables\nthat are bound may become free. Consider the following terms.\n\n\n\n\n* `` \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) ``\n Both variable `f` and `x` are bound.\n\n* `` \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) ``\n has `x` as a bound variable but `f` as a free variable.\n\n* `` ` f \u00b7 (` f \u00b7 ` x) ``\n has both `f` and `x` as free variables.\n\nWe say that a term with no free variables is _closed_; otherwise it is\n_open_. Of the three terms above, the first is closed and the other\ntwo are open. A formal definition of bound and free variables will be\ngiven in the next chapter.\n\nDifferent occurrences of a variable may be bound and free.\nIn the term\n\n (\u03bb[ x \u2236 \ud835\udd39 ] ` x) \u00b7 ` x\n\nthe inner occurrence of `x` is bound while the outer occurrence is free.\nNote that by alpha renaming, the term above is equivalent to\n\n (\u03bb[ y \u2236 \ud835\udd39 ] ` y) \u00b7 ` x\n\nin which `y` is bound and `x` is free. A common convention, called the\nBarendregt convention, is to use alpha renaming to ensure that the bound\nvariables in a term are distinct from the free variables, which can\navoid confusions that may arise if bound and free variables have the\nsame names.\n\n#### Special characters\n\n \ud83d\ude07 U+1F607: SMILING FACE WITH HALO\n \ud83d\ude08 U+1F608: SMILING FACE WITH HORNS\n\n#### Precedence\n\nAs in Agda, functions of two or more arguments are represented via\ncurrying. This is made more convenient by declaring `_\u21d2_` to\nassociate to the right and `_\u00b7_` to associate to the left.\nThus,\n\n* `(\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39 \u21d2 \ud835\udd39` abbreviates `(\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 (\ud835\udd39 \u21d2 \ud835\udd39)`\n* `two \u00b7 not \u00b7 true` abbreviates `(two \u00b7 not) \u00b7 true`.\n\nWe choose the binding strength for abstractions and conditionals\nto be weaker than application. For instance,\n\n* `` \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) ``\n - abbreviates `` (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] (\u03bb[ x \u2236 \ud835\udd39 ] (` f \u00b7 (` f \u00b7 ` x)))) ``\n - and not `` (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] (\u03bb[ x \u2236 \ud835\udd39 ] ` f)) \u00b7 (` f \u00b7 ` x) ``.\n\n\\begin{code}\nex\u2081 : (\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39 \u21d2 \ud835\udd39 \u2261 (\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 (\ud835\udd39 \u21d2 \ud835\udd39)\nex\u2081 = refl\n\nex\u2082 : two \u00b7 not \u00b7 true \u2261 (two \u00b7 not) \u00b7 true\nex\u2082 = refl\n\nex\u2083 : \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)\n \u2261 (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] (\u03bb[ x \u2236 \ud835\udd39 ] (` f \u00b7 (` f \u00b7 ` x))))\nex\u2083 = refl\n\\end{code}\n\n#### Quiz\n\n* What is the type of the following term?\n\n \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 true)\n\n 1. `\ud835\udd39 \u21d2 (\ud835\udd39 \u21d2 \ud835\udd39)`\n 2. `(\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39`\n 3. `\ud835\udd39 \u21d2 \ud835\udd39 \u21d2 \ud835\udd39`\n 4. `\ud835\udd39 \u21d2 \ud835\udd39`\n 5. `\ud835\udd39`\n\n Give more than one answer if appropriate.\n\n* What is the type of the following term?\n\n (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 true)) \u00b7 not\n\n 1. `\ud835\udd39 \u21d2 (\ud835\udd39 \u21d2 \ud835\udd39)`\n 2. `(\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39`\n 3. `\ud835\udd39 \u21d2 \ud835\udd39 \u21d2 \ud835\udd39`\n 4. `\ud835\udd39 \u21d2 \ud835\udd39`\n 5. `\ud835\udd39`\n\n Give more than one answer if appropriate.\n\n## Values\n\nWe only consider reduction of _closed_ terms,\nthose that contain no free variables. We consider\na precise definition of free variables in\n[StlcProp]({{ \"StlcProp\" | relative_url }}).\n\nA term is a value if it is fully reduced.\nFor booleans, the situation is clear, `true` and\n`false` are values, while conditionals are not.\nFor functions, applications are not values, because\nwe expect them to further reduce, and variables are\nnot values, because we focus on closed terms.\nFollowing convention, we treat all abstractions\nas values.\n\nThe predicate `Value M` holds if term `M` is a value.\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n value-\u03bb : \u2200 {x A N} \u2192 Value (\u03bb[ x \u2236 A ] N)\n value-true : Value true\n value-false : Value false\n\\end{code}\n\nWe let `V` and `W` range over values.\n\n\n#### Formal vs informal\n\nIn informal presentations of formal semantics, using\n`V` as the name of a metavariable is sufficient to\nindicate that it is a value. In Agda, we must explicitly\ninvoke the `Value` predicate.\n\n#### Other approaches\n\nAn alternative is not to focus on closed terms,\nto treat variables as values, and to treat\n`\u03bb[ x \u2236 A ] N` as a value only if `N` is a value.\nIndeed, this is how Agda normalises terms.\nFormalising this approach requires a more sophisticated\ndefinition of substitution. Here we only\nsubstitute closed terms for variables, while\nthe alternative requires the ability to substitute\nopen terms for variables.\n\n## Substitution\n\nThe heart of lambda calculus is the operation of\nsubstituting one term for a variable in another term.\nSubstitution plays a key role in defining the\noperational semantics of function application.\nFor instance, we have\n\n (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] `f \u00b7 (`f \u00b7 true)) \u00b7 not\n \u27f9\n not \u00b7 (not \u00b7 true)\n\nwhere we substitute `not` for `` `f `` in the body\nof the function abstraction.\n\nWe write substitution as `N [ x := V ]`, meaning\nsubstitute term `V` for free occurrences of variable `x` in term `N`,\nor, more compactly, substitute `V` for `x` in `N`.\nSubstitution works if `V` is any closed term;\nit need not be a value, but we use `V` since we\nalways substitute values.\n\nHere are some examples.\n\n* `` ` f [ f := not ] `` yields `` not ``\n* `` true [ f := not ] `` yields `` true ``\n* `` (` f \u00b7 true) [ f := not ] `` yields `` not \u00b7 true ``\n* `` (` f \u00b7 (` f \u00b7 true)) [ f := not ] `` yields `` not \u00b7 (not \u00b7 true) ``\n* `` (\u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)) [ f := not ] `` yields `` \u03bb[ x \u2236 \ud835\udd39 ] not \u00b7 (not \u00b7 ` x) ``\n* `` (\u03bb[ y \u2236 \ud835\udd39 ] ` y) [ x := true ] `` yields `` \u03bb[ y \u2236 \ud835\udd39 ] ` y ``\n* `` (\u03bb[ x \u2236 \ud835\udd39 ] ` x) [ x := true ] `` yields `` \u03bb[ x \u2236 \ud835\udd39 ] ` x ``\n\nThe last example is important: substituting `true` for `x` in\n`` (\u03bb[ x \u2236 \ud835\udd39 ] ` x) `` does _not_ yield `` (\u03bb[ x \u2236 \ud835\udd39 ] true) ``.\nThe reason for this is that `x` in the body of `` (\u03bb[ x \u2236 \ud835\udd39 ] ` x) ``\nis _bound_ by the abstraction. An important feature of abstraction\nis that the choice of bound names is irrelevant: both\n`` (\u03bb[ x \u2236 \ud835\udd39 ] ` x) `` and `` (\u03bb[ y \u2236 \ud835\udd39 ] ` y) `` stand for the\nidentity function. The way to think of this is that `x` within\nthe body of the abstraction stands for a _different_ variable than\n`x` outside the abstraction, they both just happen to have the same\nname.\n\nHere is the formal definition in Agda.\n\n\\begin{code}\n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\n(` x\u2032) [ x := V ] with x \u225f x\u2032\n... | yes _ = V\n... | no _ = ` x\u2032\n(\u03bb[ x\u2032 \u2236 A\u2032 ] N\u2032) [ x := V ] with x \u225f x\u2032\n... | yes _ = \u03bb[ x\u2032 \u2236 A\u2032 ] N\u2032\n... | no _ = \u03bb[ x\u2032 \u2236 A\u2032 ] (N\u2032 [ x := V ])\n(L\u2032 \u00b7 M\u2032) [ x := V ] = (L\u2032 [ x := V ]) \u00b7 (M\u2032 [ x := V ])\n(true) [ x := V ] = true\n(false) [ x := V ] = false\n(if L\u2032 then M\u2032 else N\u2032) [ x := V ] =\n if (L\u2032 [ x := V ]) then (M\u2032 [ x := V ]) else (N\u2032 [ x := V ])\n\\end{code}\n\nThe two key cases are variables and abstraction.\n\n* For variables, we compare `x`, the variable we are substituting for,\nwith `x\u2032`, the variable in the term. If they are the same,\nwe yield `V`, otherwise we yield `x\u2032` unchanged.\n\n* For abstractions, we compare `x`, the variable we are substituting for,\nwith `x\u2032`, the variable bound in the abstraction. If they are the same,\nwe yield abstraction unchanged, otherwise we substitute inside the body.\n\nIn all other cases, we push substitution recursively into\nthe subterms.\n\n#### Special characters\n\n \u2032 U+2032: PRIME (\\')\n\nNote that \u2032 (U+2032: PRIME) is not the same as ' (U+0027: APOSTROPHE).\n\n\n#### Examples\n\nHere is confirmation that the examples above are correct.\n\n\\begin{code}\nex\u2081\u2081 : ` f [ f := not ] \u2261 not\nex\u2081\u2081 = refl\n\nex\u2081\u2082 : true [ f := not ] \u2261 true\nex\u2081\u2082 = refl\n\nex\u2081\u2083 : (` f \u00b7 true) [ f := not ] \u2261 not \u00b7 true\nex\u2081\u2083 = refl\n\nex\u2081\u2084 : (` f \u00b7 (` f \u00b7 true)) [ f := not ] \u2261 not \u00b7 (not \u00b7 true)\nex\u2081\u2084 = refl\n\nex\u2081\u2085 : (\u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)) [ f := not ] \u2261 \u03bb[ x \u2236 \ud835\udd39 ] not \u00b7 (not \u00b7 ` x)\nex\u2081\u2085 = refl\n\nex\u2081\u2086 : (\u03bb[ y \u2236 \ud835\udd39 ] ` y) [ x := true ] \u2261 \u03bb[ y \u2236 \ud835\udd39 ] ` y\nex\u2081\u2086 = refl\n\nex\u2081\u2087 : (\u03bb[ x \u2236 \ud835\udd39 ] ` x) [ x := true ] \u2261 \u03bb[ x \u2236 \ud835\udd39 ] ` x\nex\u2081\u2087 = refl\n\\end{code}\n\n#### Quiz\n\nWhat is the result of the following substitution?\n\n (\u03bb[ y \u2236 \ud835\udd39 ] ` x \u00b7 (\u03bb[ x \u2236 \ud835\udd39 ] ` x)) [ x := true ]\n\n1. `` (\u03bb[ y \u2236 \ud835\udd39 ] ` x \u00b7 (\u03bb[ x \u2236 \ud835\udd39 ] ` x)) ``\n2. `` (\u03bb[ y \u2236 \ud835\udd39 ] ` x \u00b7 (\u03bb[ x \u2236 \ud835\udd39 ] true)) ``\n3. `` (\u03bb[ y \u2236 \ud835\udd39 ] true \u00b7 (\u03bb[ x \u2236 \ud835\udd39 ] ` x)) ``\n4. `` (\u03bb[ y \u2236 \ud835\udd39 ] true \u00b7 (\u03bb[ x \u2236 \ud835\udd39 ] ` true)) ``\n\n\n## Reduction\n\nWe give the reduction rules for call-by-value lambda calculus. To\nreduce an application, first we reduce the left-hand side until it\nbecomes a value (which must be an abstraction); then we reduce the\nright-hand side until it becomes a value; and finally we substitute\nthe argument for the variable in the abstraction. To reduce a\nconditional, we first reduce the condition until it becomes a value;\nif the condition is true the conditional reduces to the first\nbranch and if false it reduces to the second branch.a\n\nIn an informal presentation of the formal semantics,\nthe rules for reduction are written as follows.\n\n L \u27f9 L\u2032\n --------------- \u03be\u00b7\u2081\n L \u00b7 M \u27f9 L\u2032 \u00b7 M\n\n Value V\n M \u27f9 M\u2032\n --------------- \u03be\u00b7\u2082\n V \u00b7 M \u27f9 V \u00b7 M\u2032\n\n Value V\n --------------------------------- \u03b2\u03bb\u00b7\n (\u03bb[ x \u2236 A ] N) \u00b7 V \u27f9 N [ x := V ]\n\n L \u27f9 L\u2032\n ----------------------------------------- \u03beif\n if L then M else N \u27f9 if L\u2032 then M else N\n\n -------------------------- \u03b2if-true\n if true then M else N \u27f9 M\n\n --------------------------- \u03b2if-false\n if false then M else N \u27f9 N\n\nAs we will show later, the rules are deterministic, in that\nat most one rule applies to every term. As we will also show\nlater, for every well-typed term either a reduction applies\nor it is a value.\n\nThe rules break into two sorts. Compatibility rules\ndirect us to reduce some part of a term.\nWe give them names starting with the Greek letter xi, `\u03be`.\nOnce a term is sufficiently\nreduced, it will consist of a constructor and\na deconstructor, in our case `\u03bb` and `\u00b7`, or\n`if` and `true`, or `if` and `false`.\nWe give them names starting with the Greek letter beta, `\u03b2`,\nand indeed such rules are traditionally called beta rules.\n\nHere are the above rules formalised in Agda.\n\n\\begin{code}\ninfix 10 _\u27f9_\n\ndata _\u27f9_ : Term \u2192 Term \u2192 Set where\n \u03be\u00b7\u2081 : \u2200 {L L\u2032 M} \u2192\n L \u27f9 L\u2032 \u2192\n L \u00b7 M \u27f9 L\u2032 \u00b7 M\n \u03be\u00b7\u2082 : \u2200 {V M M\u2032} \u2192\n Value V \u2192\n M \u27f9 M\u2032 \u2192\n V \u00b7 M \u27f9 V \u00b7 M\u2032\n \u03b2\u03bb\u00b7 : \u2200 {x A N V} \u2192 Value V \u2192\n (\u03bb[ x \u2236 A ] N) \u00b7 V \u27f9 N [ x := V ]\n \u03beif : \u2200 {L L\u2032 M N} \u2192\n L \u27f9 L\u2032 \u2192\n if L then M else N \u27f9 if L\u2032 then M else N\n \u03b2if-true : \u2200 {M N} \u2192\n if true then M else N \u27f9 M\n \u03b2if-false : \u2200 {M N} \u2192\n if false then M else N \u27f9 N\n\\end{code}\n\n#### Special characters\n\nWe use the following special characters\n\n \u27f9 U+27F9: LONG RIGHTWARDS DOUBLE ARROW (\\r6)\n \u03be U+03BE: GREEK SMALL LETTER XI (\\Gx or \\xi)\n \u03b2 U+03B2: GREEK SMALL LETTER BETA (\\Gb or \\beta)\n\n#### Quiz\n\nWhat does the following term step to?\n\n (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) \u00b7 (\u03bb [ x \u2236 \ud835\udd39 ] ` x) \u27f9 ???\n\n1. `` (\u03bb [ x \u2236 \ud835\udd39 ] ` x) ``\n2. `` (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) ``\n3. `` (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) \u00b7 (\u03bb [ x \u2236 \ud835\udd39 ] ` x) ``\n\nWhat does the following term step to?\n\n (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) \u00b7 ((\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) (\u03bb [ x \u2236 \ud835\udd39 ] ` x)) \u27f9 ???\n\n1. `` (\u03bb [ x \u2236 \ud835\udd39 ] ` x) ``\n2. `` (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) ``\n3. `` (\u03bb[ x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] ` x) \u00b7 (\u03bb [ x \u2236 \ud835\udd39 ] ` x) ``\n\nWhat does the following term step to? (Where `not` is as defined above.)\n\n not \u00b7 true \u27f9 ???\n\n1. `` if ` x then false else true ``\n2. `` if true then false else true ``\n3. `` true ``\n4. `` false ``\n\nWhat does the following term step to? (Where `two` and `not` are as defined above.)\n\n two \u00b7 not \u00b7 true \u27f9 ???\n\n1. `` not \u00b7 (not \u00b7 true) ``\n2. `` (\u03bb[ x \u2236 \ud835\udd39 ] not \u00b7 (not \u00b7 ` x)) \u00b7 true ``\n4. `` true ``\n5. `` false ``\n\n## Reflexive and transitive closure\n\nA single step is only part of the story. In general, we wish to repeatedly\nstep a closed term until it reduces to a value. We do this by defining\nthe reflexive and transitive closure `\u27f9*` of the step function `\u27f9`.\nIn an informal presentation of the formal semantics, the rules\nare written as follows.\n\n ------- done\n M \u27f9* M\n\n L \u27f9 M\n M \u27f9* N\n ------- step\n L \u27f9* N\n\nHere it is formalised in Agda.\n\n\\begin{code}\ninfix 10 _\u27f9*_\ninfixr 2 _\u27f9\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u27f9*_ : Term \u2192 Term \u2192 Set where\n _\u220e : \u2200 M \u2192 M \u27f9* M\n _\u27f9\u27e8_\u27e9_ : \u2200 L {M N} \u2192 L \u27f9 M \u2192 M \u27f9* N \u2192 L \u27f9* N\n\\end{code}\n\nWe can read this as follows.\n\n* From term `M`, we can take no steps, giving `M \u220e` of type `M \u27f9* M`.\n\n* From term `L` we can take a single step `L\u27f9M` of type `L \u27f9 M`\n followed by zero or more steps `M\u27f9*N` of type `M \u27f9* N`,\n giving `L \u27e8 L\u27f9M \u27e9 M\u27f9*N` of type `L \u27f9* N`.\n\nThe names of the two clauses in the definition of reflexive\nand transitive closure have been chosen to allow us to lay\nout example reductions in an appealing way.\n\n\\begin{code}\nreduction\u2081 : not \u00b7 true \u27f9* false\nreduction\u2081 =\n not \u00b7 true\n \u27f9\u27e8 \u03b2\u03bb\u00b7 value-true \u27e9\n if true then false else true\n \u27f9\u27e8 \u03b2if-true \u27e9\n false\n \u220e\n\nreduction\u2082 : two \u00b7 not \u00b7 true \u27f9* true\nreduction\u2082 =\n two \u00b7 not \u00b7 true\n \u27f9\u27e8 \u03be\u00b7\u2081 (\u03b2\u03bb\u00b7 value-\u03bb) \u27e9\n (\u03bb[ x \u2236 \ud835\udd39 ] not \u00b7 (not \u00b7 ` x)) \u00b7 true\n \u27f9\u27e8 \u03b2\u03bb\u00b7 value-true \u27e9\n not \u00b7 (not \u00b7 true)\n \u27f9\u27e8 \u03be\u00b7\u2082 value-\u03bb (\u03b2\u03bb\u00b7 value-true) \u27e9\n not \u00b7 (if true then false else true)\n \u27f9\u27e8 \u03be\u00b7\u2082 value-\u03bb \u03b2if-true \u27e9\n not \u00b7 false\n \u27f9\u27e8 \u03b2\u03bb\u00b7 value-false \u27e9\n if false then false else true\n \u27f9\u27e8 \u03b2if-false \u27e9\n true\n \u220e\n\\end{code}\n\nMuch of the above, though not all, can be filled in using C-c C-r and C-c C-s.\n\n#### Special characters\n\nWe use the following special characters\n\n \u220e U+220E: END OF PROOF (\\qed)\n \u27e8 U+27E8: MATHEMATICAL LEFT ANGLE BRACKET (\\<)\n \u27e9 U+27E9: MATHEMATICAL RIGHT ANGLE BRACKET (\\>)\n\n## Typing\n\nWhile reduction considers only closed terms, typing must\nconsider terms with free variables. To type a term,\nwe must first type its subterms, and in particular in the\nbody of an abstraction its bound variable may appear free.\n\nIn general, we use typing _judgments_ of the form\n\n \u0393 \u22a2 M \u2236 A\n\nwhich asserts in type environment `\u0393` that term `M` has type `A`.\nEnvironment `\u0393` provides types for all the free variables in `M`.\n\nHere are three examples.\n\n* `` \u2205 , f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 , x \u2236 \ud835\udd39 \u22a2 ` f \u00b7 (` f \u00b7 ` x) \u2236 \ud835\udd39 ``\n* `` \u2205 , f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 \u22a2 (\u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)) \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ``\n* `` \u2205 \u22a2 (\u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x)) \u2236 (\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39 \u21d2 \ud835\udd39 ``\n\nEnvironments are partial maps from identifiers to types, built using `\u2205`\nfor the empty map, and `\u0393 , x \u2236 A` for the map that extends\nenvironment `\u0393` by mapping variable `x` to type `A`.\n\nIn an informal presentation of the formal semantics,\nthe rules for typing are written as follows.\n\n \u0393 x \u2261 A\n ----------- Ax\n \u0393 \u22a2 ` x \u2236 A\n\n \u0393 , x \u2236 A \u22a2 N \u2236 B\n ------------------------ \u21d2-I\n \u0393 \u22a2 \u03bb[ x \u2236 A ] N \u2236 A \u21d2 B\n\n \u0393 \u22a2 L \u2236 A \u21d2 B\n \u0393 \u22a2 M \u2236 A\n -------------- \u21d2-E\n \u0393 \u22a2 L \u00b7 M \u2236 B\n\n ------------- \ud835\udd39-I\u2081\n \u0393 \u22a2 true \u2236 \ud835\udd39\n\n -------------- \ud835\udd39-I\u2082\n \u0393 \u22a2 false \u2236 \ud835\udd39\n\n \u0393 \u22a2 L : \ud835\udd39\n \u0393 \u22a2 M \u2236 A\n \u0393 \u22a2 N \u2236 A\n -------------------------- \ud835\udd39-E\n \u0393 \u22a2 if L then M else N \u2236 A\n\nAs we will show later, the rules are deterministic, in that\nat most one rule applies to every term.\n\nThe proof rules come in pairs, with rules to introduce and to\neliminate each connective, labeled `-I` and `-E`, respectively. As we\nread the rules from top to bottom, introduction and elimination rules\ndo what they say on the tin: the first _introduces_ a formula for the\nconnective, which appears in the conclusion but not in the premises;\nwhile the second _eliminates_ a formula for the connective, which appears in\na premise but not in the conclusion. An introduction rule describes\nhow to construct a value of the type (abstractions yield functions,\ntrue and false yield booleans), while an elimination rule describes\nhow to deconstruct a value of the given type (applications use\nfunctions, conditionals use booleans).\n\nHere are the above rules formalised in Agda.\n\n\\begin{code}\nContext : Set\nContext = PartialMap Type\n\ninfix 10 _\u22a2_\u2236_\n\ndata _\u22a2_\u2236_ : Context \u2192 Term \u2192 Type \u2192 Set where\n Ax : \u2200 {\u0393 x A} \u2192\n \u0393 x \u2261 just A \u2192\n \u0393 \u22a2 ` x \u2236 A\n \u21d2-I : \u2200 {\u0393 x N A B} \u2192\n \u0393 , x \u2236 A \u22a2 N \u2236 B \u2192\n \u0393 \u22a2 \u03bb[ x \u2236 A ] N \u2236 A \u21d2 B\n \u21d2-E : \u2200 {\u0393 L M A B} \u2192\n \u0393 \u22a2 L \u2236 A \u21d2 B \u2192\n \u0393 \u22a2 M \u2236 A \u2192\n \u0393 \u22a2 L \u00b7 M \u2236 B\n \ud835\udd39-I\u2081 : \u2200 {\u0393} \u2192\n \u0393 \u22a2 true \u2236 \ud835\udd39\n \ud835\udd39-I\u2082 : \u2200 {\u0393} \u2192\n \u0393 \u22a2 false \u2236 \ud835\udd39\n \ud835\udd39-E : \u2200 {\u0393 L M N A} \u2192\n \u0393 \u22a2 L \u2236 \ud835\udd39 \u2192\n \u0393 \u22a2 M \u2236 A \u2192\n \u0393 \u22a2 N \u2236 A \u2192\n \u0393 \u22a2 if L then M else N \u2236 A\n\\end{code}\n\n#### Example type derivations\n\nHere are a couple of typing examples. First, here is how\nthey would be written in an informal description of the\nformal semantics.\n\nDerivation of `not`:\n\n ------------ Ax ------------- \ud835\udd39-I\u2082 ------------- \ud835\udd39-I\u2081\n \u0393\u2080 \u22a2 ` x \u2236 \ud835\udd39 \u0393\u2080 \u22a2 false \u2236 \ud835\udd39 \u0393\u2080 \u22a2 true \u2236 \ud835\udd39\n ------------------------------------------------------ \ud835\udd39-E\n \u0393\u2080 \u22a2 if ` x then false else true \u2236 \ud835\udd39\n --------------------------------------------------- \u21d2-I\n \u2205 \u22a2 \u03bb[ x \u2236 \ud835\udd39 ] if ` x then false else true \u2236 \ud835\udd39 \u21d2 \ud835\udd39\n\nwhere `\u0393\u2080 = \u2205 , x \u2236 \ud835\udd39`.\n\nDerivation of `two`:\n\n ----------------- Ax ------------ Ax\n \u0393\u2082 \u22a2 ` f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 \u0393\u2082 \u22a2 ` x \u2236 \ud835\udd39\n ----------------- Ax ------------------------------------- \u21d2-E\n \u0393\u2082 \u22a2 ` f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 \u0393\u2082 \u22a2 ` f \u00b7 ` x \u2236 \ud835\udd39\n ------------------------------------------- \u21d2-E\n \u0393\u2082 \u22a2 ` f \u00b7 (` f \u00b7 ` x) \u2236 \ud835\udd39\n ------------------------------------------ \u21d2-I\n \u0393\u2081 \u22a2 \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) \u2236 \ud835\udd39 \u21d2 \ud835\udd39\n ---------------------------------------------------------- \u21d2-I\n \u2205 \u22a2 \u03bb[ f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` f \u00b7 (` f \u00b7 ` x) \u2236 \ud835\udd39 \u21d2 \ud835\udd39\n\nwhere `\u0393\u2081 = \u2205 , f \u2236 \ud835\udd39 \u21d2 \ud835\udd39` and `\u0393\u2082 = \u2205 , f \u2236 \ud835\udd39 \u21d2 \ud835\udd39 , x \u2236 \ud835\udd39`.\n\nHere are the above derivations formalised in Agda.\n\n\\begin{code}\ntyping\u2081 : \u2205 \u22a2 not \u2236 \ud835\udd39 \u21d2 \ud835\udd39\ntyping\u2081 = \u21d2-I (\ud835\udd39-E (Ax refl) \ud835\udd39-I\u2082 \ud835\udd39-I\u2081)\n\ntyping\u2082 : \u2205 \u22a2 two \u2236 (\ud835\udd39 \u21d2 \ud835\udd39) \u21d2 \ud835\udd39 \u21d2 \ud835\udd39\ntyping\u2082 = \u21d2-I (\u21d2-I (\u21d2-E (Ax refl) (\u21d2-E (Ax refl) (Ax refl))))\n\\end{code}\n\n#### Interaction with Agda\n\nConstruction of a type derivation is best done interactively.\nStart with the declaration:\n\n typing\u2081 : \u2205 \u22a2 not \u2236 \ud835\udd39 \u21d2 \ud835\udd39\n typing\u2081 = ?\n\nTyping C-l causes Agda to create a hole and tell us its expected type.\n\n typing\u2081 = { }0\n ?0 : \u2205 \u22a2 not \u2236 \ud835\udd39 \u21d2 \ud835\udd39\n\nNow we fill in the hole by typing C-c C-r. Agda observes that\nthe outermost term in `not` in a `\u03bb`, which is typed using `\u21d2-I`. The\n`\u21d2-I` rule in turn takes one argument, which Agda leaves as a hole.\n\n typing\u2081 = \u21d2-I { }0\n ?0 : \u2205 , x \u2236 \ud835\udd39 \u22a2 if ` x then false else true \u2236 \ud835\udd39\n\nAgain we fill in the hole by typing C-c C-r. Agda observes that the\noutermost term is now `if_then_else_`, which is typed using `\ud835\udd39-E`. The\n`\ud835\udd39-E` rule in turn takes three arguments, which Agda leaves as holes.\n\n typing\u2081 = \u21d2-I (\ud835\udd39-E { }0 { }1 { }2)\n ?0 : \u2205 , x \u2236 \ud835\udd39 \u22a2 ` x \u2236\n ?1 : \u2205 , x \u2236 \ud835\udd39 \u22a2 false \u2236 \ud835\udd39\n ?2 : \u2205 , x \u2236 \ud835\udd39 \u22a2 true \u2236 \ud835\udd39\n\nAgain we fill in the three holes by typing C-c C-r in each. Agda observes\nthat `` ` x ``, `false`, and `true` are typed using `Ax`, `\ud835\udd39-I\u2082`, and\n`\ud835\udd39-I\u2081` respectively. The `Ax` rule in turn takes an argument, to show\nthat `(\u2205 , x \u2236 \ud835\udd39) x = just \ud835\udd39`, which can in turn be specified with a\nhole. After filling in all holes, the term is as above.\n\nThe entire process can be automated using Agsy, invoked with C-c C-a.\n\n#### Non-examples\n\nWe can also show that terms are _not_ typeable. For example, here is\na formal proof that it is not possible to type the term `` true \u00b7\nfalse ``. In other words, no type `A` is the type of this term. It\ncannot be typed, because doing so requires that the first term in the\napplication is both a boolean and a function.\n\n\\begin{code}\nnotyping\u2082 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 true \u00b7 false \u2236 A)\nnotyping\u2082 (\u21d2-E () _)\n\\end{code}\n\nAs a second example, here is a formal proof that it is not possible to\ntype `` \u03bb[ x \u2236 \ud835\udd39 ] \u03bb[ y \u2236 \ud835\udd39 ] ` x \u00b7 ` y `` It cannot be typed, because\ndoing so requires `x` to be both boolean and a function.\n\n\\begin{code}\ncontradiction : \u2200 {A B} \u2192 \u00ac (\ud835\udd39 \u2261 A \u21d2 B)\ncontradiction ()\n\nnotyping\u2081 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 \u03bb[ x \u2236 \ud835\udd39 ] \u03bb[ y \u2236 \ud835\udd39 ] ` x \u00b7 ` y \u2236 A)\nnotyping\u2081 (\u21d2-I (\u21d2-I (\u21d2-E (Ax \u0393x) _))) = contradiction (just-injective \u0393x)\n\\end{code}\n\n\n#### Quiz\n\nFor each of the following, given a type `A` for which it is derivable,\nor explain why there is no such `A`.\n\n1. `` \u2205 , y \u2236 A \u22a2 \u03bb[ x \u2236 \ud835\udd39 ] ` x \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ``\n2. `` \u2205 \u22a2 \u03bb[ y \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` y \u00b7 ` x \u2236 A ``\n3. `` \u2205 \u22a2 \u03bb[ y \u2236 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` x \u00b7 ` y \u2236 A ``\n4. `` \u2205 , x \u2236 A \u22a2 \u03bb[ y : \ud835\udd39 \u21d2 \ud835\udd39 ] `y \u00b7 `x : A ``\n\nFor each of the following, give type `A`, `B`, `C`, and `D` for which it is derivable,\nor explain why there are no such types.\n\n1. `` \u2205 \u22a2 \u03bb[ y \u2236 \ud835\udd39 \u21d2 \ud835\udd39 \u21d2 \ud835\udd39 ] \u03bb[ x \u2236 \ud835\udd39 ] ` y \u00b7 ` x \u2236 A ``\n2. `` \u2205 , x \u2236 A \u22a2 x \u00b7 x \u2236 B ``\n3. `` \u2205 , x \u2236 A , y \u2236 B \u22a2 \u03bb[ z \u2236 C ] ` x \u00b7 (` y \u00b7 ` z) \u2236 D ``\n\n\n\n","avg_line_length":30.2448512586,"max_line_length":93,"alphanum_fraction":0.5980555345} +{"size":2093,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"% Andreas, Bug filed by Stevan Andjelkovic, June 2013\n\n\\subsection{Examples}\n\\label{examples}\n\n\\AgdaHide{\n\\begin{code}\n{-# OPTIONS --sized-types #-}\n\nmodule Issue854 where\n\nopen import Function hiding (force)\nopen import Data.Unit\nopen import Data.Product\nopen import Data.List\nopen import Data.List.Relation.Unary.Any\nopen import Data.List.Relation.Binary.Pointwise hiding (refl)\nopen import Data.Container.FreeMonad using (rawMonad)\nopen import Relation.Binary.PropositionalEquality\nopen import Effect.Monad\n\nopen import Data.List.Relation.Binary.Subset.Propositional\nopen import Issue854.Types\nopen import Issue854.Context\nopen import Issue854.WellTyped\nopen import Issue854.WellTypedSemantics\n\\end{code}\n}\n\n\\begin{code}\n\u2032N : Sig\n\u2032N = (\ud835\udfd9 , \ud835\udfd8) \u2237 (\ud835\udfd9 , \ud835\udfd9) \u2237 []\n\nN = \u03bc \u2032N\n\nze : \u2200 {\u0393} \u2192 \u0393 \u22a2^v _ \u2236 N\nze = con (here refl) \u27e8\u27e9 (\ud835\udfd8-elim (var zero))\n\npattern su n = con (there (here refl)) \u27e8\u27e9 n\n\n#0 #1 : \u2200 {\u0393} \u2192 \u0393 \u22a2^v _ \u2236 N\n\n#0 = ze\n#1 = su #0\n\nState : VType \u2192 Sig\nState S = (\ud835\udfd9 , S) \u2237 (S , \ud835\udfd9) \u2237 []\n\n-- XXX: get : {m : True (\ud835\udfd9 , S) \u2208? \u03a3)} \u2192 \u03a3 \u22c6 S?\n\nstate^suc : \u2200 {\u0393} \u2192 \u0393 \u22a2^c _ \u2236 State N \u22c6 \ud835\udfd9\nstate^suc {\u0393} = _to_ (op get \u00b7 \u27e8\u27e9) (op put \u00b7 su\u2032) id id\n where\n get = here refl\n put = there (here refl)\n\n su\u2032 : \u0393 \u25bb N \u22a2^v _ \u2236 N\n su\u2032 = con (there (here refl)) \u27e8\u27e9 (var (suc zero))\n\nstate^Homo : \u2200 {\u0393 \u03a3 S X} \u2192 \u0393 \u22a2^cs _ \u2236 PHomo (State S) X S \u03a3 (X \u2297 S)\nstate^Homo =\n \u019b \u019b \u019b (((\u03c0\u2082 (force (var (suc zero)) \u00b7 var zero)) \u00b7 var zero)) \u2237\n \u019b \u019b \u019b ((\u03c0\u2082 (force (var (suc zero)) \u00b7 \u27e8\u27e9)) \u00b7 var (suc (suc zero))) \u2237\n \u019b \u019b return (var (suc zero) , var zero) \u2237 []\n\nprivate\n -- XXX: Move to std-lib?\n inl-++ : \u2200 {A : Set}{xs ys : List A} \u2192 xs \u2286 (xs ++ ys)\n inl-++ {xs = []} ()\n inl-++ {xs = x \u2237 xs} (here refl) = here refl\n inl-++ {xs = x \u2237 xs} (there p) = there (inl-++ p)\n\n inr-++ : \u2200 {A : Set}{xs ys : List A} \u2192 ys \u2286 (xs ++ ys)\n inr-++ {xs = []} p = p\n inr-++ {xs = x \u2237 xs} p = there (inr-++ {xs = xs} p)\n\nex-state : [] \u22a2^c _ \u2236 [] \u22c6 (\ud835\udfd9 \u2297 N)\nex-state = run {\u03a3\u2032 = State N}{[]} state^Homo state^suc inl-++ id \u00b7 #0\n\ntest-state : \u27e6 ex-state \u27e7^c tt \u2261 \u27e6 return (\u27e8\u27e9 , #1) \u27e7^c tt\ntest-state = refl\n\\end{code}\n","avg_line_length":25.2168674699,"max_line_length":69,"alphanum_fraction":0.5762064023} +{"size":243,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe #-}\n\nmodule Container.List where\n\nopen import Prelude\nopen import Data.Fin\nopen import Container\n\nList : Type a \u2192 Type a\n\\end{code}\n%<*list-def>\n\\begin{code}\nList = \u27e6 \u2115 , Fin \u27e7\n\\end{code}\n%<\/list-def>\n","avg_line_length":14.2941176471,"max_line_length":32,"alphanum_fraction":0.670781893} +{"size":166,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"module LearnYouAgda where\n data \u2115 : Set where\n zero : \u2115\n suc : \u2115 -> \u2115\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n zero + zero = zero\n zero + n = n\n (suc n) + n\u2032 = suc (n + n\u2032)\n","avg_line_length":16.6,"max_line_length":29,"alphanum_fraction":0.4759036145} +{"size":6946,"ext":"lagda","lang":"Literate Agda","max_stars_count":135.0,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\n\nmodule Algorithmic.Examples where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Utils using (*;_\u21d2_)\nopen import Type\nopen import Type.BetaNormal\nopen import Type.BetaNBE.RenamingSubstitution\nimport Type.RenamingSubstitution as \u22c6\nopen import Algorithmic\nopen import Algorithmic.RenamingSubstitution\nopen import Algorithmic.Evaluation\n\nopen import Relation.Binary.PropositionalEquality renaming (subst to substEq)\n\\end{code}\n\n## Examples\n\n### Scott Numerals\n\nFrom http:\/\/lucacardelli.name\/Papers\/Notes\/scott2.pdf\n\nM = \u03bc X . G X\nG X = \u2200 R. R \u2192 (X \u2192 R) \u2192 R)\n\u03bc X . G X = \u2200 X . (G X \u2192 X) \u2192 X -- what is the status of this?\nN = G M\nin : N \u2192 M\nout : M \u2192 N\n\n0 = \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . x\n : N\nsucc = \u03bb n : N . \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . y (in n)\n : N \u2192 N\ncase = \u03bb n : N . \u039b R . \u03bb a : R . \u03bb f : N \u2192 N . n [R] a (f \u2218 out)\n : N \u2192 \u2200 R . R \u2192 (N \u2192 R) \u2192 R\n\n\n--\n\n\\begin{code}\n-- bound variable names inserted below are not meaningful\nmodule Scott where\n open import Type.BetaNBE\n open import Type.BetaNBE.Stability\n\n _\u00b7Nf_ : \u2200{\u0393}{K J}\n \u2192 \u0393 \u22a2Nf\u22c6 K \u21d2 J\n \u2192 \u0393 \u22a2Nf\u22c6 K\n \u2192 \u0393 \u22a2Nf\u22c6 J\n f \u00b7Nf a = nf (embNf f \u00b7 embNf a)\n\n \u03bc0 : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 (* \u21d2 *) \u21d2 *\n \u03bc0 = \u019b (\u03bc (\u019b (\u019b (ne (` Z \u00b7 ne (` (S Z) \u00b7 ne (` Z)))))) (ne (` Z)))\n\n\n wrap0 : \u2200{\u03a6 \u0393}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 * \u21d2 *)\n \u2192 \u0393 \u22a2 A \u00b7Nf (\u03bc0 \u00b7Nf A)\n \u2192 \u0393 \u22a2 \u03bc0 \u00b7Nf A\n wrap0 A X rewrite stability A = wrap _ A X\n\n unwrap0 : \u2200{\u03a6 \u0393}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 * \u21d2 *)\n \u2192 \u0393 \u22a2 \u03bc0 \u00b7Nf A\n \u2192 \u0393 \u22a2 A \u00b7Nf (\u03bc0 \u00b7Nf A)\n unwrap0 A X rewrite stability A = unwrap X refl\n \n G : \u2200{\u0393} \u2192 \u0393 ,\u22c6 * \u22a2Nf\u22c6 *\n G = \u03a0 (ne (` Z) \u21d2 (ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` Z))\n \n M : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 *\n M = \u03bc0 \u00b7Nf \u019b G\n\n N : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 *\n N = G [ M ]Nf\n\n Zero : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n Zero = \u039b (\u019b (\u019b (` (S (Z )))))\n\n\n Succ : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 N\n Succ = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 wrap0 (\u019b G) (` (S (S (T Z))))))))\n\n One : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n One = Succ \u00b7 Zero\n \n Two : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n case : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 (\u03a0 (ne (` Z) \u21d2 (N \u21d2 ne (` Z)) \u21d2 ne (` Z)))\n case = \u019b (\u039b (\u019b (\u019b (((` (S (S (T Z)))) \u00b7\u22c6 ne (` Z) \/ refl) \u00b7 (` (S Z)) \u00b7 (\u019b (` (S Z) \u00b7 unwrap0 (\u019b G) (` Z) ))))))\n\n{-\n Y-comb : \u2200{\u0393} \u2192 \u0393 \u22a2 \u03a0 ((ne (` Z) \u21d2 ne (` Z)) \u21d2 ne (` Z))\n Y-comb = \u039b (\u019b ((\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) \u00b7 wrap (ne (` Z) \u21d2 ne (` (S Z))) \u2022 (\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) refl ))\n-}\n Z-comb : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192\n \u0393 \u22a2 \u03a0 (\u03a0 (((ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` (S Z)) \u21d2 ne (` Z)))\n Z-comb = \u039b (\u039b (\u019b (\u019b (` (S Z) \u00b7 \u019b (unwrap0 (\u019b (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z)) \u00b7 wrap0 (\u019b (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (\u019b (` (S Z) \u00b7 \u019b (unwrap0 (\u019b (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z))))))\n\n OnePlus : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 (N \u21d2 N) \u21d2 N \u21d2 N\n OnePlus = \u019b (\u019b ((((case \u00b7 (` Z)) \u00b7\u22c6 N \/ refl) \u00b7 One) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 (` Z))))))\n\n OnePlusOne : \u2205 \u22a2 N\n OnePlusOne = ((Z-comb \u00b7\u22c6 N \/ refl) \u00b7\u22c6 N \/ refl) \u00b7 OnePlus \u00b7 One\n\n -- Roman's more efficient version\n Plus : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 N \u21d2 N\n Plus = \u019b (\u019b (((Z-comb \u00b7\u22c6 N \/ refl) \u00b7\u22c6 N \/ refl) \u00b7 (\u019b (\u019b ((((case \u00b7 ` Z) \u00b7\u22c6 N \/ refl) \u00b7 ` (S (S (S Z)))) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 ` Z)))))) \u00b7 ` (S Z)))\n\n TwoPlusTwo : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n TwoPlusTwo = (Plus \u00b7 Two) \u00b7 Two\n\\end{code}\n\neval (gas 10000000) Scott.Four\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\neval (gas 10000000) Scott.Two\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\n### Church Numerals\n\n\\begin{code}\nmodule Church where\n\n N : \u2200{\u03a6} \u2192 \u03a6 \u22a2Nf\u22c6 *\n N = \u03a0 ((ne (` Z)) \u21d2 (ne (` Z) \u21d2 ne (` Z)) \u21d2 (ne (` Z)))\n\n Zero : \u2205 \u22a2 N\n Zero = \u039b (\u019b (\u019b (` (S Z))))\n\n Succ : \u2205 \u22a2 N \u21d2 N\n Succ = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 (((` (S (S (T Z)))) \u00b7\u22c6 (ne (` Z)) \/ refl) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n \n Iter : \u2205 \u22a2 \u03a0 (ne (` Z) \u21d2 (ne (` Z) \u21d2 ne (` Z)) \u21d2 N \u21d2 ne (` Z))\n Iter = \u039b (\u019b (\u019b (\u019b (((` Z) \u00b7\u22c6 ne (` Z) \/ refl) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\n -- two plus two\n One : \u2205 \u22a2 N\n One = Succ \u00b7 Zero\n\n Two : \u2205 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n Plus : \u2205 \u22a2 N \u2192 \u2205 \u22a2 N \u2192 \u2205 \u22a2 N\n Plus x y = (Iter \u00b7\u22c6 N \/ refl) \u00b7 x \u00b7 Succ \u00b7 y -- by induction on the second y\n\n TwoPlusTwo = Plus Two Two\n\n TwoPlusTwo' : \u2205 \u22a2 N\n TwoPlusTwo' = (Two \u00b7\u22c6 N \/ refl) \u00b7 Two \u00b7 Succ\n\nopen Church public\n\\end{code}\n\n-- Church \"4\"\neval (gas 100000000) Four\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using iterator\neval (gas 100000000) (Plus Two Two)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using the numerals directly\neval (gas 10000000) (Two \u00b7\u22c6 N \u00b7 Two \u00b7 Succ)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z))) \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n V-\u039b_)\n","avg_line_length":23.1533333333,"max_line_length":285,"alphanum_fraction":0.3335732796} +{"size":281,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\documentclass{article}\n\\usepackage{agda}\n\\begin{document}\n\n\\begin{code}\ndata Nat : Set where\n zero : Nat\n suc : Nat \u2192 Nat\n\npattern two = suc (suc zero)\npattern three = suc two\npattern four = suc three\n\npattern five = suc four\n\ndata Bot : Set where\n\\end{code}\n\n\\end{document}","avg_line_length":14.7894736842,"max_line_length":30,"alphanum_fraction":0.6939501779} +{"size":24670,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Connectives: Conjunction, disjunction, and implication\"\nlayout : page\nprev : \/Isomorphism\/\npermalink : \/Connectives\/\nnext : \/Negation\/\n---\n\n\\begin{code}\nmodule plfa.Connectives where\n\\end{code}\n\nThis chapter introduces the basic logical connectives, by observing a\ncorrespondence between connectives of logic and data types, a\nprinciple known as _Propositions as Types_:\n\n * _conjunction_ is _product_,\n * _disjunction_ is _sum_,\n * _true_ is _unit type_,\n * _false_ is _empty type_,\n * _implication_ is _function space_.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_)\nopen import Data.Nat.Properties using (+-suc)\nopen import Function using (_\u2218_)\nopen import plfa.Isomorphism using (_\u2243_; \u2243-sym; \u2243-trans; _\u2272_; extensionality)\nopen plfa.Isomorphism.\u2243-Reasoning\n\\end{code}\n\n\n## Conjunction is product\n\nGiven two propositions `A` and `B`, the conjunction `A \u00d7 B` holds\nif both `A` holds and `B` holds. We formalise this idea by\ndeclaring a suitable inductive type:\n\\begin{code}\ndata _\u00d7_ (A : Set) (B : Set) : Set where\n\n \u27e8_,_\u27e9 : \n A\n \u2192 B\n -----\n \u2192 A \u00d7 B\n\\end{code}\nEvidence that `A \u00d7 B` holds is of the form `\u27e8 M , N \u27e9`, where `M`\nprovides evidence that `A` holds and `N` provides evidence that `B`\nholds.\n\nGiven evidence that `A \u00d7 B` holds, we can conclude that either\n`A` holds or `B` holds:\n\\begin{code}\nproj\u2081 : \u2200 {A B : Set}\n \u2192 A \u00d7 B\n -----\n \u2192 A\nproj\u2081 \u27e8 x , y \u27e9 = x\n\nproj\u2082 : \u2200 {A B : Set}\n \u2192 A \u00d7 B\n -----\n \u2192 B\nproj\u2082 \u27e8 x , y \u27e9 = y\n\\end{code}\nIf `L` provides evidence that `A \u00d7 B` holds, then `proj\u2081 L` provides evidence\nthat `A` holds, and `proj\u2082 L` provides evidence that `B` holds.\n\nEquivalently, we could also declare conjunction as a record type:\n\\begin{code}\nrecord _\u00d7\u2032_ (A B : Set) : Set where\n field\n proj\u2081\u2032 : A\n proj\u2082\u2032 : B\nopen _\u00d7\u2032_\n\\end{code}\nHere record construction\n\n record\n { proj\u2081\u2032 = M\n ; proj\u2082\u2032 = N\n }\n\ncorresponds to the term\n\n \u27e8 M , N \u27e9\n\nwhere `M` is a term of type `A` and `N` is a term of type `B`.\n\nWhen `\u27e8_,_\u27e9` appears in a term on the right-hand side of an equation\nwe refer to it as a _constructor_, and when it appears in a pattern on\nthe left-hand side of an equation we refer to it as a _destructor_.\nWe may also refer to `proj\u2081` and `proj\u2082` as destructors, since they\nplay a similar role.\n\nOther terminology refers to `\u27e8_,_\u27e9` as the _introducing_ a conjunction, and\nto `proj\u2081` and `proj\u2082` as _eliminating_ a conjunction; indeed, the\nformer is sometimes given the name `\u00d7-I` and the latter two the names\n`\u00d7-E\u2081` and `\u00d7-E\u2082`. As we read the rules from top to bottom,\nintroduction and elimination do what they say on the tin: the first\n_introduces_ a formula for the connective, which appears in the\nconclusion but not in the hypotheses; the second _eliminates_ a\nformula for the connective, which appears in a hypothesis but not in\nthe conclusion. An introduction rule describes under what conditions\nwe say the connective holds---how to _define_ the connective. An\nelimination rule describes what we may conclude when the connective\nholds---how to _use_ the connective.\n\n(The paragraph above was adopted from \"Propositions as Types\", Philip Wadler,\n_Communications of the ACM_, December 2015.)\n\nIn this case, applying each destructor and reassembling the results with the\nconstructor is the identity over products:\n\\begin{code}\n\u03b7-\u00d7 : \u2200 {A B : Set} (w : A \u00d7 B) \u2192 \u27e8 proj\u2081 w , proj\u2082 w \u27e9 \u2261 w\n\u03b7-\u00d7 \u27e8 x , y \u27e9 = refl\n\\end{code}\nThe pattern matching on the left-hand side is essential, since\nreplacing `w` by `\u27e8 x , y \u27e9` allows both sides of the equation to\nsimplify to the same term.\n\nWe set the precedence of conjunction so that it binds less\ntightly than anything save disjunction:\n\\begin{code}\ninfixr 2 _\u00d7_\n\\end{code}\nThus, `m \u2264 n \u00d7 n \u2264 p` parses as `(m \u2264 n) \u00d7 (n \u2264 p)`.\n\nGiven two types `A` and `B`, we refer to `A x B` as the\n_product_ of `A` and `B`. In set theory, it is also sometimes\ncalled the _cartesian product_, and in computing it corresponds\nto a _record_ type. Among other reasons for\ncalling it the product, note that if type `A` has `m`\ndistinct members, and type `B` has `n` distinct members,\nthen the type `A \u00d7 B` has `m * n` distinct members.\nFor instance, consider a type `Bool` with two members, and\na type `Tri` with three members:\n\\begin{code}\ndata Bool : Set where\n true : Bool\n false : Bool\n\ndata Tri : Set where\n aa : Tri\n bb : Tri\n cc : Tri\n\\end{code}\nThen the type `Bool \u00d7 Tri` has six members:\n\n \u27e8 true , aa \u27e9 \u27e8 true , bb \u27e9 \u27e8 true , cc \u27e9\n \u27e8 false , aa \u27e9 \u27e8 false , bb \u27e9 \u27e8 false , cc \u27e9\n\nFor example, the following function enumerates all\npossible arguments of type `Bool \u00d7 Tri`:\n\\begin{code}\n\u00d7-count : Bool \u00d7 Tri \u2192 \u2115\n\u00d7-count \u27e8 true , aa \u27e9 = 1\n\u00d7-count \u27e8 true , bb \u27e9 = 2\n\u00d7-count \u27e8 true , cc \u27e9 = 3\n\u00d7-count \u27e8 false , aa \u27e9 = 4\n\u00d7-count \u27e8 false , bb \u27e9 = 5\n\u00d7-count \u27e8 false , cc \u27e9 = 6\n\\end{code}\n\nProduct on types also shares a property with product on numbers in\nthat there is a sense in which it is commutative and associative. In\nparticular, product is commutative and associative _up to\nisomorphism_.\n\nFor commutativity, the `to` function swaps a pair, taking `(x , y)` to\n`(y , x)`, and the `from` function does the same (up to renaming).\nInstantiating the patterns correctly in `from\u2218to` and `to\u2218from` is essential.\nReplacing the definition of `from\u2218to` by `\u03bb w \u2192 refl` will not work;\nand similarly for `to\u2218from`:\n\\begin{code}\n\u00d7-comm : \u2200 {A B : Set} \u2192 A \u00d7 B \u2243 B \u00d7 A\n\u00d7-comm =\n record\n { to = \u03bb{ \u27e8 x , y \u27e9 \u2192 \u27e8 y , x \u27e9 }\n ; from = \u03bb{ \u27e8 y , x \u27e9 \u2192 \u27e8 x , y \u27e9 }\n ; from\u2218to = \u03bb{ \u27e8 x , y \u27e9 \u2192 refl }\n ; to\u2218from = \u03bb{ \u27e8 y , x \u27e9 \u2192 refl }\n }\n\\end{code}\n\nBeing _commutative_ is different from being _commutative up to\nisomorphism_. Compare the two statements:\n\n m * n \u2261 n * m\n A \u00d7 B \u2243 B \u00d7 A\n\nIn the first case, we might have that `m` is `2` and `n` is `3`, and\nboth `m * n` and `n * m` are equal to `6`. In the second case, we\nmight have that `A` is `Bool` and `B` is `Tri`, and `Bool \u00d7 Tri` is\n_not_ the same as `Tri \u00d7 Bool`. But there is an isomorphism between\nthe two types. For instance, `(true , aa)`, which is a member of the\nformer, corresponds to `(aa , true)`, which is a member of the latter.\n\nFor associativity, the `to` function reassociates two uses of pairing,\ntaking `((x , y) , z)` to `(x , (y , z))`, and the `from` function does\nthe inverse. Again, the evidence of left and right inverse requires\nmatching against a suitable pattern to enable simplification:\n\\begin{code}\n\u00d7-assoc : \u2200 {A B C : Set} \u2192 (A \u00d7 B) \u00d7 C \u2243 A \u00d7 (B \u00d7 C)\n\u00d7-assoc =\n record\n { to = \u03bb{ \u27e8 \u27e8 x , y \u27e9 , z \u27e9 \u2192 \u27e8 x , \u27e8 y , z \u27e9 \u27e9 }\n ; from = \u03bb{ \u27e8 x , \u27e8 y , z \u27e9 \u27e9 \u2192 \u27e8 \u27e8 x , y \u27e9 , z \u27e9 }\n ; from\u2218to = \u03bb{ \u27e8 \u27e8 x , y \u27e9 , z \u27e9 \u2192 refl }\n ; to\u2218from = \u03bb{ \u27e8 x , \u27e8 y , z \u27e9 \u27e9 \u2192 refl }\n }\n\\end{code}\n\nBeing _associative_ is not the same as being _associative\nup to isomorphism_. Compare the two statements:\n\n (m * n) * p \u2261 m * (n * p)\n (A \u00d7 B) \u00d7 C \u2243 A \u00d7 (B \u00d7 C)\n\nFor example, the type `(\u2115 \u00d7 Bool) \u00d7 Tri` is _not_ the same as `\u2115 \u00d7\n(Bool \u00d7 Tri)`. But there is an isomorphism between the two types. For\ninstance `((1 , true) , aa)`, which is a member of the former,\ncorresponds to `(1 , (true , aa))`, which is a member of the latter.\n\n#### Exercise `\u21d4\u2243\u00d7` (recommended)\n\nShow that `A \u21d4 B` as defined [earlier][plfa.Isomorphism#iff]\nis isomorphic to `(A \u2192 B) \u00d7 (B \u2192 A)`.\n\n\n## Truth is unit\n\nTruth `\u22a4` always holds. We formalise this idea by\ndeclaring a suitable inductive type:\n\\begin{code}\ndata \u22a4 : Set where\n\n tt :\n --\n \u22a4\n\\end{code}\nEvidence that `\u22a4` holds is of the form `tt`.\n\nThere is an introduction rule, but no elimination rule.\nGiven evidence that `\u22a4` holds, there is nothing more of interest we\ncan conclude. Since truth always holds, knowing that it holds tells\nus nothing new.\n\nThe nullary case of `\u03b7-\u00d7` is `\u03b7-\u22a4`, which asserts that any\nvalue of type `\u22a4` must be equal to `tt`:\n\\begin{code}\n\u03b7-\u22a4 : \u2200 (w : \u22a4) \u2192 tt \u2261 w\n\u03b7-\u22a4 tt = refl\n\\end{code}\nThe pattern matching on the left-hand side is essential. Replacing\n`w` by `tt` allows both sides of the equation to simplify to the\nsame term.\n\nWe refer to `\u22a4` as the _unit_ type. And, indeed,\ntype `\u22a4` has exactly once member, `tt`. For example, the following\nfunction enumerates all possible arguments of type `\u22a4`:\n\\begin{code}\n\u22a4-count : \u22a4 \u2192 \u2115\n\u22a4-count tt = 1\n\\end{code}\n\nFor numbers, one is the identity of multiplication. Correspondingly,\nunit is the identity of product _up to isomorphism_. For left\nidentity, the `to` function takes `(tt , x)` to `x`, and the `from`\nfunction does the inverse. The evidence of left inverse requires\nmatching against a suitable pattern to enable simplification:\n\\begin{code}\n\u22a4-identity\u02e1 : \u2200 {A : Set} \u2192 \u22a4 \u00d7 A \u2243 A\n\u22a4-identity\u02e1 =\n record\n { to = \u03bb{ \u27e8 tt , x \u27e9 \u2192 x }\n ; from = \u03bb{ x \u2192 \u27e8 tt , x \u27e9 }\n ; from\u2218to = \u03bb{ \u27e8 tt , x \u27e9 \u2192 refl }\n ; to\u2218from = \u03bb{ x \u2192 refl }\n }\n\\end{code}\n\nHaving an _identity_ is different from having an identity\n_up to isomorphism_. Compare the two statements:\n\n 1 * m \u2261 m\n \u22a4 \u00d7 A \u2243 A\n\nIn the first case, we might have that `m` is `2`, and both\n`1 * m` and `m` are equal to `2`. In the second\ncase, we might have that `A` is `Bool`, and `\u22a4 \u00d7 Bool` is _not_ the\nsame as `Bool`. But there is an isomorphism between the two types.\nFor instance, `(tt, true)`, which is a member of the former,\ncorresponds to `true`, which is a member of the latter.\n\nRight identity follows from commutativity of product and left identity:\n\\begin{code}\n\u22a4-identity\u02b3 : \u2200 {A : Set} \u2192 (A \u00d7 \u22a4) \u2243 A\n\u22a4-identity\u02b3 {A} =\n \u2243-begin\n (A \u00d7 \u22a4)\n \u2243\u27e8 \u00d7-comm \u27e9\n (\u22a4 \u00d7 A)\n \u2243\u27e8 \u22a4-identity\u02e1 \u27e9\n A\n \u2243-\u220e\n\\end{code}\nHere we have used a chain of isomorphisms, analogous to that used for\nequality.\n\n\n## Disjunction is sum\n\nGiven two propositions `A` and `B`, the disjunction `A \u228e B` holds\nif either `A` holds or `B` holds. We formalise this idea by\ndeclaring a suitable inductive type:\n\\begin{code}\ndata _\u228e_ : Set \u2192 Set \u2192 Set where\n\n inj\u2081 : \u2200 {A B : Set}\n \u2192 A\n -----\n \u2192 A \u228e B\n\n inj\u2082 : \u2200 {A B : Set}\n \u2192 B\n -----\n \u2192 A \u228e B\n\\end{code}\nEvidence that `A \u228e B` holds is either of the form `inj\u2081 M`, where `M`\nprovides evidence that `A` holds, or `inj\u2082 N`, where `N` provides\nevidence that `B` holds.\n\nGiven evidence that `A \u2192 C` and `B \u2192 C` both hold, then given\nevidence that `A \u228e B` holds we can conclude that `C` holds:\n\\begin{code}\ncase-\u228e : \u2200 {A B C : Set}\n \u2192 (A \u2192 C)\n \u2192 (B \u2192 C)\n \u2192 A \u228e B\n -----------\n \u2192 C\ncase-\u228e f g (inj\u2081 x) = f x\ncase-\u228e f g (inj\u2082 y) = g y\n\\end{code}\nPattern matching against `inj\u2081` and `inj\u2082` is typical of how we exploit\nevidence that a disjunction holds.\n\nWhen `inj\u2081` and `inj\u2082` appear on the right-hand side of an equation we\nrefer to them as _constructors_, and when they appears on the\nleft-hand side we refer to them as _destructors_. We also refer to\n`case-\u228e` as a destructor, since it plays a similar role. Other\nterminology refers to `inj\u2081` and `inj\u2082` as _introducing_ a\ndisjunction, and to `case-\u228e` as _eliminating_ a disjunction; indeed\nthe former are sometimes given the names `\u228e-I\u2081` and `\u228e-I\u2082` and the\nlatter the name `\u228e-E`.\n\nApplying the destructor to each of the constructors is the identity:\n\\begin{code}\n\u03b7-\u228e : \u2200 {A B : Set} (w : A \u228e B) \u2192 case-\u228e inj\u2081 inj\u2082 w \u2261 w\n\u03b7-\u228e (inj\u2081 x) = refl\n\u03b7-\u228e (inj\u2082 y) = refl\n\\end{code}\nMore generally, we can also throw in an arbitrary function from a disjunction:\n\\begin{code}\nuniq-\u228e : \u2200 {A B C : Set} (h : A \u228e B \u2192 C) (w : A \u228e B) \u2192\n case-\u228e (h \u2218 inj\u2081) (h \u2218 inj\u2082) w \u2261 h w\nuniq-\u228e h (inj\u2081 x) = refl\nuniq-\u228e h (inj\u2082 y) = refl\n\\end{code}\nThe pattern matching on the left-hand side is essential. Replacing\n`w` by `inj\u2081 x` allows both sides of the equation to simplify to the\nsame term, and similarly for `inj\u2082 y`.\n\nWe set the precedence of disjunction so that it binds less tightly\nthan any other declared operator:\n\\begin{code}\ninfix 1 _\u228e_\n\\end{code}\nThus, `A \u00d7 C \u228e B \u00d7 C` parses as `(A \u00d7 C) \u228e (B \u00d7 C)`.\n\nGiven two types `A` and `B`, we refer to `A \u228e B` as the\n_sum_ of `A` and `B`. In set theory, it is also sometimes\ncalled the _disjoint union_, and in computing it corresponds\nto a _variant record_ type. Among other reasons for\ncalling it the sum, note that if type `A` has `m`\ndistinct members, and type `B` has `n` distinct members,\nthen the type `A \u228e B` has `m + n` distinct members.\nFor instance, consider a type `Bool` with two members, and\na type `Tri` with three members, as defined earlier.\nThen the type `Bool \u228e Tri` has five\nmembers:\n\n inj\u2081 true inj\u2082 aa\n inj\u2081 false inj\u2082 bb\n inj\u2082 cc\n\nFor example, the following function enumerates all\npossible arguments of type `Bool \u228e Tri`:\n\\begin{code}\n\u228e-count : Bool \u228e Tri \u2192 \u2115\n\u228e-count (inj\u2081 true) = 1\n\u228e-count (inj\u2081 false) = 2\n\u228e-count (inj\u2082 aa) = 3\n\u228e-count (inj\u2082 bb) = 4\n\u228e-count (inj\u2082 cc) = 5\n\\end{code}\n\nSum on types also shares a property with sum on numbers in that it is\ncommutative and associative _up to isomorphism_.\n\n#### Exercise `\u228e-comm` (recommended)\n\nShow sum is commutative up to isomorphism.\n\n#### Exercise `\u228e-assoc`\n\nShow sum is associative up to ismorphism. \n\n## False is empty\n\nFalse `\u22a5` never holds. We formalise this idea by declaring\na suitable inductive type:\n\\begin{code}\ndata \u22a5 : Set where\n -- no clauses!\n\\end{code}\nThere is no possible evidence that `\u22a5` holds.\n\nDual to `\u22a4`, for `\u22a5` there is no introduction rule but an elimination rule.\nSince false never holds, knowing that it holds tells us we are in a\nparadoxical situation. Given evidence that `\u22a5` holds, we might\nconclude anything! This is a basic principle of logic, known in\nmedieval times by the latin phrase _ex falso_, and known to children\nthrough phrases such as \"if pigs had wings, then I'd be the Queen of\nSheba\". We formalise it as follows.\n\\begin{code}\n\u22a5-elim : \u2200 {A : Set}\n \u2192 \u22a5\n --\n \u2192 A\n\u22a5-elim ()\n\\end{code}\nThis is our first use of the _absurd pattern_ `()`.\nHere since `\u22a5` is a type with no members, we indicate that it is\n_never_ possible to match against a value of this type by using\nthe pattern `()`.\n\nThe nullary case of `case-\u228e` is `\u22a5-elim`. By analogy,\nwe might have called it `case-\u22a5`, but chose to stick with the name\nin the standard library.\n\nThe nullary case of `uniq-\u228e` is `uniq-\u22a5`, which asserts that `\u22a5-elim`\nis equal to any arbitrary function from `\u22a5`:\n\\begin{code}\nuniq-\u22a5 : \u2200 {C : Set} (h : \u22a5 \u2192 C) (w : \u22a5) \u2192 \u22a5-elim w \u2261 h w\nuniq-\u22a5 h ()\n\\end{code}\nUsing the absurd pattern asserts there are no possible values for `w`,\nso the equation holds trivially.\n\nWe refer to `\u22a5` as the _empty_ type. And, indeed,\ntype `\u22a5` has no members. For example, the following function\nenumerates all possible arguments of type `\u22a5`:\n\\begin{code}\n\u22a5-count : \u22a5 \u2192 \u2115\n\u22a5-count ()\n\\end{code}\nHere again the absurd pattern `()` indicates that no value can match\ntype `\u22a5`.\n\nFor numbers, zero is the identity of addition. Correspondingly, empty\nis the identity of sums _up to isomorphism_.\n\n#### Exercise `\u22a5-identity\u02e1` (recommended)\n\nShow zero is the left identity of addition.\n\n#### Exercise `\u22a5-identity\u02b3`\n\nShow zero is the right identity of addition. \n\n## Implication is function {#implication}\n\nGiven two propositions `A` and `B`, the implication `A \u2192 B` holds if\nwhenever `A` holds then `B` must also hold. We formalise implication using\nthe function type, which has appeared throughout this book.\n\nEvidence that `A \u2192 B` holds is of the form\n\n \u03bb (x : A) \u2192 N\n\nwhere `N` is a term of type `B` containing as a free variable `x` of type `A`.\nGiven a term `L` providing evidence that `A \u2192 B` holds, and a term `M`\nproviding evidence that `A` holds, the term `L M` provides evidence that\n`B` holds. In other words, evidence that `A \u2192 B` holds is a function that\nconverts evidence that `A` holds into evidence that `B` holds.\n\nPut another way, if we know that `A \u2192 B` and `A` both hold,\nthen we may conclude that `B` holds:\n\\begin{code}\n\u2192-elim : \u2200 {A B : Set}\n \u2192 (A \u2192 B)\n \u2192 A\n -------\n \u2192 B\n\u2192-elim L M = L M\n\\end{code}\nIn medieval times, this rule was known by the name _modus ponens_.\nIt corresponds to function application.\n\nDefining a function, with an named definition or a lambda abstraction,\nis referred to as _introducing_ a function,\nwhile applying a function is referred to as _eliminating_ the function.\n\nElimination followed by introduction is the identity:\n\\begin{code}\n\u03b7-\u2192 : \u2200 {A B : Set} (f : A \u2192 B) \u2192 (\u03bb (x : A) \u2192 f x) \u2261 f\n\u03b7-\u2192 f = refl\n\\end{code}\n\nImplication binds less tightly than any other operator. Thus, `A \u228e B \u2192\nB \u228e A` parses as `(A \u228e B) \u2192 (B \u228e A)`.\n\nGiven two types `A` and `B`, we refer to `A \u2192 B` as the _function_\nspace from `A` to `B`. It is also sometimes called the _exponential_,\nwith `B` raised to the `A` power. Among other reasons for calling\nit the exponential, note that if type `A` has `m` distinct\nmembers, and type `B` has `n` distinct members, then the type\n`A \u2192 B` has `n\u1d50` distinct members. For instance, consider a\ntype `Bool` with two members and a type `Tri` with three members,\nas defined earlier. The the type `Bool \u2192 Tri` has nine (that is,\nthree squared) members:\n\n \u03bb{true \u2192 aa; false \u2192 aa} \u03bb{true \u2192 aa; false \u2192 bb} \u03bb{true \u2192 aa; false \u2192 cc}\n \u03bb{true \u2192 bb; false \u2192 aa} \u03bb{true \u2192 bb; false \u2192 bb} \u03bb{true \u2192 bb; false \u2192 cc}\n \u03bb{true \u2192 cc; false \u2192 aa} \u03bb{true \u2192 cc; false \u2192 bb} \u03bb{true \u2192 cc; false \u2192 cc}\n\nFor example, the following function enumerates all possible\narguments of the type `Bool \u2192 Tri`:\n\\begin{code}\n\u2192-count : (Bool \u2192 Tri) \u2192 \u2115\n\u2192-count f with f true | f false\n... | aa | aa = 1\n... | aa | bb = 2\n... | aa | cc = 3\n... | bb | aa = 4\n... | bb | bb = 5\n... | bb | cc = 6\n... | cc | aa = 7\n... | cc | bb = 8\n... | cc | cc = 9\n\\end{code}\n\nExponential on types also share a property with exponential on\nnumbers in that many of the standard identities for numbers carry\nover to the types.\n\nCorresponding to the law\n\n (p ^ n) ^ m \u2261 p ^ (n * m)\n\nwe have the isomorphism\n\n A \u2192 (B \u2192 C) \u2243 (A \u00d7 B) \u2192 C\n\nBoth types can be viewed as functions that given evidence that `A` holds\nand evidence that `B` holds can return evidence that `C` holds.\nThis isomorphism sometimes goes by the name *currying*.\nThe proof of the right inverse requires extensionality:\n\\begin{code}\ncurrying : \u2200 {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2243 (A \u00d7 B \u2192 C)\ncurrying =\n record\n { to = \u03bb{ f \u2192 \u03bb{ \u27e8 x , y \u27e9 \u2192 f x y }}\n ; from = \u03bb{ g \u2192 \u03bb{ x \u2192 \u03bb{ y \u2192 g \u27e8 x , y \u27e9 }}}\n ; from\u2218to = \u03bb{ f \u2192 refl }\n ; to\u2218from = \u03bb{ g \u2192 extensionality \u03bb{ \u27e8 x , y \u27e9 \u2192 refl }}\n }\n\\end{code}\n\nCurrying tells us that instead of a function that takes a pair of arguments,\nwe can have a function that takes the first argument and returns a function that\nexpects the second argument. Thus, for instance, our way of writing addition\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n\nis isomorphic to a function that accepts a pair of arguments:\n\n _+\u2032_ : (\u2115 \u00d7 \u2115) \u2192 \u2115\n\nAgda is optimised for currying, so `2 + 3` abbreviates `_+_ 2 3`.\nIn a language optimised for pairing, we would instead take `2 +\u2032 3` as\nan abbreviation for `_+\u2032_ \u27e8 2 , 3 \u27e9`.\n\nCorresponding to the law\n\n p ^ (n + m) = (p ^ n) * (p ^ m)\n\nwe have the isomorphism:\n\n (A \u228e B) \u2192 C \u2243 (A \u2192 C) \u00d7 (B \u2192 C)\n\nThat is, the assertion that if either `A` holds or `B` holds then `C` holds\nis the same as the assertion that if `A` holds then `C` holds and if\n`B` holds then `C` holds. The proof of the left inverse requires extensionality:\n\\begin{code}\n\u2192-distrib-\u228e : \u2200 {A B C : Set} \u2192 (A \u228e B \u2192 C) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n\u2192-distrib-\u228e =\n record\n { to = \u03bb{ f \u2192 \u27e8 f \u2218 inj\u2081 , f \u2218 inj\u2082 \u27e9 }\n ; from = \u03bb{ \u27e8 g , h \u27e9 \u2192 \u03bb{ (inj\u2081 x) \u2192 g x ; (inj\u2082 y) \u2192 h y } }\n ; from\u2218to = \u03bb{ f \u2192 extensionality \u03bb{ (inj\u2081 x) \u2192 refl ; (inj\u2082 y) \u2192 refl } }\n ; to\u2218from = \u03bb{ \u27e8 g , h \u27e9 \u2192 refl }\n }\n\\end{code}\n\nCorresponding to the law\n\n (p * n) ^ m = (p ^ m) * (n ^ m)\n\nwe have the isomorphism:\n\n A \u2192 B \u00d7 C \u2243 (A \u2192 B) \u00d7 (A \u2192 C)\n\nThat is, the assertion that if `A` holds then `B` holds and `C` holds\nis the same as the assertion that if `A` holds then `B` holds and if\n`A` holds then `C` holds. The proof of left inverse requires both extensionality\nand the rule `\u03b7-\u00d7` for products:\n\\begin{code}\n\u2192-distrib-\u00d7 : \u2200 {A B C : Set} \u2192 (A \u2192 B \u00d7 C) \u2243 (A \u2192 B) \u00d7 (A \u2192 C)\n\u2192-distrib-\u00d7 =\n record\n { to = \u03bb{ f \u2192 \u27e8 proj\u2081 \u2218 f , proj\u2082 \u2218 f \u27e9 }\n ; from = \u03bb{ \u27e8 g , h \u27e9 \u2192 \u03bb{ x \u2192 \u27e8 g x , h x \u27e9 } }\n ; from\u2218to = \u03bb{ f \u2192 extensionality \u03bb{ x \u2192 \u03b7-\u00d7 (f x) } }\n ; to\u2218from = \u03bb{ \u27e8 g , h \u27e9 \u2192 refl }\n }\n\\end{code}\n\n\n## Distribution\n\nProducts distribute over sum, up to isomorphism. The code to validate\nthis fact is similar in structure to our previous results:\n\\begin{code}\n\u00d7-distrib-\u228e : \u2200 {A B C : Set} \u2192 (A \u228e B) \u00d7 C \u2243 (A \u00d7 C) \u228e (B \u00d7 C)\n\u00d7-distrib-\u228e =\n record\n { to = \u03bb{ \u27e8 inj\u2081 x , z \u27e9 \u2192 (inj\u2081 \u27e8 x , z \u27e9)\n ; \u27e8 inj\u2082 y , z \u27e9 \u2192 (inj\u2082 \u27e8 y , z \u27e9)\n }\n ; from = \u03bb{ (inj\u2081 \u27e8 x , z \u27e9) \u2192 \u27e8 inj\u2081 x , z \u27e9\n ; (inj\u2082 \u27e8 y , z \u27e9) \u2192 \u27e8 inj\u2082 y , z \u27e9\n }\n ; from\u2218to = \u03bb{ \u27e8 inj\u2081 x , z \u27e9 \u2192 refl\n ; \u27e8 inj\u2082 y , z \u27e9 \u2192 refl\n }\n ; to\u2218from = \u03bb{ (inj\u2081 \u27e8 x , z \u27e9) \u2192 refl\n ; (inj\u2082 \u27e8 y , z \u27e9) \u2192 refl\n }\n }\n\\end{code}\n\nSums do not distribute over products up to isomorphism, but it is an embedding:\n\\begin{code}\n\u228e-distrib-\u00d7 : \u2200 {A B C : Set} \u2192 (A \u00d7 B) \u228e C \u2272 (A \u228e C) \u00d7 (B \u228e C)\n\u228e-distrib-\u00d7 =\n record\n { to = \u03bb{ (inj\u2081 \u27e8 x , y \u27e9) \u2192 \u27e8 inj\u2081 x , inj\u2081 y \u27e9\n ; (inj\u2082 z) \u2192 \u27e8 inj\u2082 z , inj\u2082 z \u27e9\n }\n ; from = \u03bb{ \u27e8 inj\u2081 x , inj\u2081 y \u27e9 \u2192 (inj\u2081 \u27e8 x , y \u27e9)\n ; \u27e8 inj\u2081 x , inj\u2082 z \u27e9 \u2192 (inj\u2082 z)\n ; \u27e8 inj\u2082 z , _ \u27e9 \u2192 (inj\u2082 z)\n }\n ; from\u2218to = \u03bb{ (inj\u2081 \u27e8 x , y \u27e9) \u2192 refl\n ; (inj\u2082 z) \u2192 refl\n }\n }\n\\end{code}\nNote that there is a choice in how we write the `from` function.\nAs given, it takes `\u27e8 inj\u2082 z , inj\u2082 z\u2032 \u27e9` to `inj\u2082 z`, but it is\neasy to write a variant that instead returns `inj\u2082 z\u2032`. We have\nan embedding rather than an isomorphism because the\n`from` function must discard either `z` or `z\u2032` in this case.\n\nIn the usual approach to logic, both of the distribution laws\nare given as equivalences, where each side implies the other:\n\n A \u00d7 (B \u228e C) \u21d4 (A \u00d7 B) \u228e (A \u00d7 C) \n A \u228e (B \u00d7 C) \u21d4 (A \u228e B) \u00d7 (A \u228e C)\n\nBut when we consider the functions that provide evidence for these\nimplications, then the first corresponds to an isomorphism while the\nsecond only corresponds to an embedding, revealing a sense in which\none of these laws is \"more true\" than the other.\n\n\n#### Exercise `\u228e-weak-\u00d7` (recommended)\n\nShow that the following property holds:\n\\begin{code}\npostulate\n \u228e-weak-\u00d7 : \u2200 {A B C : Set} \u2192 (A \u228e B) \u00d7 C \u2192 A \u228e (B \u00d7 C)\n\\end{code}\nThis is called a _weak distributive law_. Give the corresponding\ndistributive law, and explain how it relates to the weak version.\n\n\n#### Exercise `\u228e\u00d7-implies-\u00d7\u228e`\n\nShow that a disjunct of conjuncts implies a conjunct of disjuncts:\n\\begin{code}\npostulate\n \u228e\u00d7-implies-\u00d7\u228e : \u2200 {A B C D : Set} \u2192 (A \u00d7 B) \u228e (C \u00d7 D) \u2192 (A \u228e C) \u00d7 (B \u228e D)\n\\end{code}\nDoes the converse hold? If so, prove; if not, give a counterexample.\n\n\n## Standard library\n\nDefinitions similar to those in this chapter can be found in the standard library:\n\\begin{code}\nimport Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nimport Data.Unit using (\u22a4; tt)\nimport Data.Sum using (_\u228e_; inj\u2081; inj\u2082) renaming ([_,_] to case-\u228e)\nimport Data.Empty using (\u22a5; \u22a5-elim)\nimport Function.Equivalence using (_\u21d4_)\n\\end{code}\nThe standard library constructs pairs with `_,_` whereas we use `\u27e8_,_\u27e9`.\nThe former makes it convenient to build triples or larger tuples from pairs,\npermitting `a , b , c` to stand for `(a , (b , c))`. But it conflicts with\nother useful notations, such as `[_,_]` to construct a list of two elements in\nChapter [Lists][plfa.Lists]\nand `\u0393 , A` to extend environments in\nChapter [DeBruijn][plfa.DeBruijn].\nThe standard library `_\u21d4_` is similar to ours, but the one in the\nstandard library is less convenient, since it is parameterised with\nrespect to an arbitrary notion of equivalence.\n\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u00d7 U+00D7 MULTIPLICATION SIGN (\\x)\n \u228e U+228E MULTISET UNION (\\u+)\n \u22a4 U+22A4 DOWN TACK (\\top)\n \u22a5 U+22A5 UP TACK (\\bot)\n \u03b7 U+03B7 GREEK SMALL LETTER ETA (\\eta)\n \u2081 U+2081 SUBSCRIPT ONE (\\_1)\n \u2082 U+2082 SUBSCRIPT TWO (\\_2)\n \u21d4 U+21D4 LEFT RIGHT DOUBLE ARROW (\\<=>)\n","avg_line_length":32.4605263158,"max_line_length":82,"alphanum_fraction":0.6319416295} +{"size":13878,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"---\ntitle : \"Negation: Negation, with intuitionistic and classical logic\"\nlayout : page\nprev : \/Connectives\/\npermalink : \/Negation\/\nnext : \/Quantifiers\/\n---\n\n\\begin{code}\nmodule plfa.Negation where\n\\end{code}\n\nThis chapter introduces negation, and discusses intuitionistic\nand classical logic.\n\n## Imports\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; refl)\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Data.Product using (_\u00d7_)\nopen import plfa.Isomorphism using (_\u2243_; extensionality)\n\\end{code}\n\n\n## Negation\n\nGiven a proposition `A`, the negation `\u00ac A` holds if `A` cannot hold.\nWe formalise this idea by declaring negation to be the same\nas implication of false:\n\\begin{code}\n\u00ac_ : Set \u2192 Set\n\u00ac A = A \u2192 \u22a5\n\\end{code}\nThis is a form of _proof by contradiction_: if assuming `A` leads\nto the conclusion `\u22a5` (a contradiction), then we must have `\u00ac A`.\n\nEvidence that `\u00ac A` holds is of the form\n\n \u03bb{ x \u2192 N }\n\nwhere `N` is a term of type `\u22a5` containing as a free variable `x` of type `A`.\nIn other words, evidence that `\u00ac A` holds is a function that converts evidence\nthat `A` holds into evidence that `\u22a5` holds.\n\nGiven evidence that both `\u00ac A` and `A` hold, we can conclude that `\u22a5` holds.\nIn other words, if both `\u00ac A` and `A` hold, then we have a contradiction:\n\\begin{code}\n\u00ac-elim : \u2200 {A : Set}\n \u2192 \u00ac A\n \u2192 A\n ---\n \u2192 \u22a5\n\u00ac-elim \u00acx x = \u00acx x\n\\end{code}\nHere we write `\u00acx` for evidence of `\u00ac A` and `x` for evidence of `A`. This\nmeans that `\u00acx` must be a function of type `A \u2192 \u22a5`, and hence the application\n`\u00acx x` must be of type `\u22a5`. Note that this rule is just a special case of `\u2192-elim`.\n\nWe set the precedence of negation so that it binds more tightly\nthan disjunction and conjunction, but less tightly than anything else:\n\\begin{code}\ninfix 3 \u00ac_\n\\end{code}\nThus, `\u00ac A \u00d7 \u00ac B` parses as `(\u00ac A) \u00d7 (\u00ac B)` and `\u00ac m \u2261 n` as `\u00ac (m \u2261 n)`.\n\nIn _classical_ logic, we have that `A` is equivalent to `\u00ac \u00ac A`.\nAs we discuss below, in Agda we use _intuitionistic_ logic, where\nwe have only half of this equivalence, namely that `A` implies `\u00ac \u00ac A`:\n\\begin{code}\n\u00ac\u00ac-intro : \u2200 {A : Set}\n \u2192 A\n -----\n \u2192 \u00ac \u00ac A\n\u00ac\u00ac-intro x = \u03bb{\u00acx \u2192 \u00acx x}\n\\end{code}\nLet `x` be evidence of `A`. We show that assuming\n`\u00ac A` leads to a contradiction, and hence `\u00ac \u00ac A` must hold.\nLet `\u00acx` be evidence of `\u00ac A`. Then from `A` and `\u00ac A`\nwe have a contradiction, evidenced by `\u00acx x`. Hence, we have\nshown `\u00ac \u00ac A`.\n\nAn equivalent way to write the above is as follows:\n\\begin{code}\n\u00ac\u00ac-intro\u2032 : \u2200 {A : Set}\n \u2192 A\n -----\n \u2192 \u00ac \u00ac A\n\u00ac\u00ac-intro\u2032 x \u00acx = \u00acx x\n\\end{code}\nHere we have simply converted the argument of the lambda term\nto an additional argument of the function. We will usually\nuse this latter style, as it is more compact.\n\nWe cannot show that `\u00ac \u00ac A` implies `A`, but we can show that\n`\u00ac \u00ac \u00ac A` implies `\u00ac A`:\n\\begin{code}\n\u00ac\u00ac\u00ac-elim : \u2200 {A : Set}\n \u2192 \u00ac \u00ac \u00ac A\n -------\n \u2192 \u00ac A\n\u00ac\u00ac\u00ac-elim \u00ac\u00ac\u00acx = \u03bb x \u2192 \u00ac\u00ac\u00acx (\u00ac\u00ac-intro x)\n\\end{code}\nLet `\u00ac\u00ac\u00acx` be evidence of `\u00ac \u00ac \u00ac A`. We will show that assuming\n`A` leads to a contradiction, and hence `\u00ac A` must hold.\nLet `x` be evidence of `A`. Then by the previous result, we\ncan conclude `\u00ac \u00ac A`, evidenced by `\u00ac\u00ac-intro x`. Then from\n`\u00ac \u00ac \u00ac A` and `\u00ac \u00ac A` we have a contradiction, evidenced by\n`\u00ac\u00ac\u00acx (\u00ac\u00ac-intro x)`. Hence we have shown `\u00ac A`.\n\nAnother law of logic is _contraposition_,\nstating that if `A` implies `B`, then `\u00ac B` implies `\u00ac A`:\n\\begin{code}\ncontraposition : \u2200 {A B : Set}\n \u2192 (A \u2192 B)\n -----------\n \u2192 (\u00ac B \u2192 \u00ac A)\ncontraposition f \u00acy x = \u00acy (f x)\n\\end{code}\nLet `f` be evidence of `A \u2192 B` and let `\u00acy` be evidence of `\u00ac B`. We\nwill show that assuming `A` leads to a contradiction, and hence `\u00ac A`\nmust hold. Let `x` be evidence of `A`. Then from `A \u2192 B` and `A` we\nmay conclude `B`, evidenced by `f x`, and from `B` and `\u00ac B` we may\nconclude `\u22a5`, evidenced by `\u00acy (f x)`. Hence, we have shown `\u00ac A`.\n\nUsing negation, it is straightforward to define inequality:\n\\begin{code}\n_\u2262_ : \u2200 {A : Set} \u2192 A \u2192 A \u2192 Set\nx \u2262 y = \u00ac (x \u2261 y)\n\\end{code}\nIt is trivial to show distinct numbers are not equal:\n\\begin{code}\n_ : 1 \u2262 2\n_ = \u03bb()\n\\end{code}\nThis is our first use of an absurd pattern in a lambda expression.\nThe type `M \u2261 N` is occupied exactly when `M` and `N` simplify to\nidentical terms. Since `1` and `2` simplify to distinct normal forms,\nAgda determines that there is no possible evidence that `1 \u2261 2`.\nAs a second example, it is also easy to validate\nPeano's postulate that zero is not the successor of any number:\n\\begin{code}\npeano : \u2200 {m : \u2115} \u2192 zero \u2262 suc m\npeano = \u03bb()\n\\end{code}\nThe evidence is essentially the same, as the absurd pattern matches\nall possible evidence of type `zero \u2261 suc m`.\n\nGiven the correspondence of implication to exponentiation and\nfalse to the type with no members, we can view negation as\nraising to the zero power. This indeed corresponds to what\nwe know for arithmetic, where\n\n 0 ^ n \u2261 1, if n \u2261 0\n \u2261 0, if n \u2262 0\n\nIndeed, there is exactly one proof of `\u22a5 \u2192 \u22a5`. We can write\nthis proof two different ways:\n\\begin{code}\nid : \u22a5 \u2192 \u22a5\nid x = x\n\nid\u2032 : \u22a5 \u2192 \u22a5\nid\u2032 ()\n\\end{code}\nBut, using extensionality, we can prove these equal:\n\\begin{code}\nid\u2261id\u2032 : id \u2261 id\u2032\nid\u2261id\u2032 = extensionality (\u03bb())\n\\end{code}\nBy extensionality, `id \u2261 id\u2032` holds if for every\n`x` in their domain we have `id x \u2261 id\u2032 x`. But there\nis no `x` in their domain, so the equality holds trivially.\n\nIndeed, we can show any two proofs of a negation are equal:\n\\begin{code}\nassimilation : \u2200 {A : Set} (\u00acx \u00acx\u2032 : \u00ac A) \u2192 \u00acx \u2261 \u00acx\u2032\nassimilation \u00acx \u00acx\u2032 = extensionality (\u03bb x \u2192 \u22a5-elim (\u00acx x))\n\\end{code}\nEvidence for `\u00ac A` implies that any evidence of `A`\nimmediately leads to a contradiction. But extensionality\nquantifies over all `x` such that `A` holds, hence any\nsuch `x` immediately leads to a contradiction,\nagain causing the equality to hold trivially.\n\n\n#### Exercise `<-irreflexive` (recommended)\n\nUsing negation, show that\n[strict inequality][plfa.Relations#strict-inequality]\nis irreflexive, that is, `n < n` holds for no `n`.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `trichotomy`\n\nShow that strict inequality satisfies\n[trichotomy][plfa.Relations#trichotomy],\nthat is, for any naturals `m` and `n` exactly one of the following holds:\n\n* `m < n`\n* `m \u2261 n`\n* `m > n`\n\nHere \"exactly one\" means that not only one of the three must hold,\nbut that when one holds the negation of the other two must also hold.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n#### Exercise `\u228e-dual-\u00d7` (recommended)\n\nShow that conjunction, disjunction, and negation are related by a\nversion of De Morgan's Law.\n\n \u00ac (A \u228e B) \u2243 (\u00ac A) \u00d7 (\u00ac B)\n\nThis result is an easy consequence of something we've proved previously.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\nDo we also have the following?\n\n \u00ac (A \u00d7 B) \u2243 (\u00ac A) \u228e (\u00ac B)\n\nIf so, prove; if not, can you give a relation weaker than\nisomorphism that relates the two sides?\n\n\n## Intuitive and Classical logic\n\nIn Gilbert and Sullivan's _The Gondoliers_, Casilda is told that\nas an infant she was married to the heir of the King of Batavia, but\nthat due to a mix-up no one knows which of two individuals, Marco or\nGiuseppe, is the heir. Alarmed, she wails \"Then do you mean to say\nthat I am married to one of two gondoliers, but it is impossible to\nsay which?\" To which the response is \"Without any doubt of any kind\nwhatever.\"\n\nLogic comes in many varieties, and one distinction is between\n_classical_ and _intuitionistic_. Intuitionists, concerned\nby assumptions made by some logicians about the nature of\ninfinity, insist upon a constructionist notion of truth. In\nparticular, they insist that a proof of `A \u228e B` must show\n_which_ of `A` or `B` holds, and hence they would reject the\nclaim that Casilda is married to Marco or Giuseppe until one of the\ntwo was identified as her husband. Perhaps Gilbert and Sullivan\nanticipated intuitionism, for their story's outcome is that the heir\nturns out to be a third individual, Luiz, with whom Casilda is,\nconveniently, already in love.\n\nIntuitionists also reject the law of the excluded middle, which\nasserts `A \u228e \u00ac A` for every `A`, since the law gives no clue as to\n_which_ of `A` or `\u00ac A` holds. Heyting formalised a variant of\nHilbert's classical logic that captures the intuitionistic notion of\nprovability. In particular, the law of the excluded middle is provable\nin Hilbert's logic, but not in Heyting's. Further, if the law of the\nexcluded middle is added as an axiom to Heyting's logic, then it\nbecomes equivalent to Hilbert's. Kolmogorov showed the two logics\nwere closely related: he gave a double-negation translation, such that\na formula is provable in classical logic if and only if its\ntranslation is provable in intuitionistic logic.\n\nPropositions as Types was first formulated for intuitionistic logic.\nIt is a perfect fit, because in the intuitionist interpretation the\nformula `A \u228e B` is provable exactly when one exhibits either a proof\nof `A` or a proof of `B`, so the type corresponding to disjunction is\na disjoint sum.\n\n(Parts of the above are adopted from \"Propositions as Types\", Philip Wadler,\n_Communications of the ACM_, December 2015.)\n\n## Excluded middle is irrefutable\n\nThe law of the excluded middle can be formulated as follows:\n\\begin{code}\npostulate\n em : \u2200 {A : Set} \u2192 A \u228e \u00ac A\n\\end{code}\nAs we noted, the law of the excluded middle does not hold in\nintuitionistic logic. However, we can show that it is _irrefutable_,\nmeaning that the negation of its negation is provable (and hence that\nits negation is never provable):\n\\begin{code}\nem-irrefutable : \u2200 {A : Set} \u2192 \u00ac \u00ac (A \u228e \u00ac A)\nem-irrefutable = \u03bb k \u2192 k (inj\u2082 (\u03bb x \u2192 k (inj\u2081 x)))\n\\end{code}\nThe best way to explain this code is to develop it interactively:\n\n em-irrefutable k = ?\n\nGiven evidence `k` that `\u00ac (A \u228e \u00ac A)`, that is, a function that given a\nvalue of type `A \u228e \u00ac A` returns a value of the empty type, we must fill\nin `?` with a term that returns a value of the empty type. The only way\nwe can get a value of the empty type is by applying `k` itself, so let's\nexpand the hole accordingly:\n\n em-irrefutable k = k ?\n\nWe need to fill the new hole with a value of type `A \u228e \u00ac A`. We don't have\na value of type `A` to hand, so let's pick the second disjunct:\n\n em-irrefutable k = k (inj\u2082 \u03bb{ x \u2192 ? })\n\nThe second disjunct accepts evidence of `\u00ac A`, that is, a function\nthat given a value of type `A` returns a value of the empty type. We\nbind `x` to the value of type `A`, and now we need to fill in the hole\nwith a value of the empty type. Once again, the only way we can get a\nvalue of the empty type is by applying `k` itself, so let's expand the\nhole accordingly:\n\n em-irrefutable k = k (inj\u2082 \u03bb{ x \u2192 k ? })\n\nThis time we do have a value of type `A` to hand, namely `x`, so we can\npick the first disjunct:\n\n em-irrefutable k = k (inj\u2082 \u03bb{ x \u2192 k (inj\u2081 x) })\n\nThere are no holes left! This completes the proof.\n\nThe following story illustrates the behaviour of the term we have created.\n(With apologies to Peter Selinger, who tells a similar story\nabout a king, a wizard, and the Philosopher's stone.)\n\nOnce upon a time, the devil approached a man and made an offer:\n\"Either (a) I will give you one billion dollars, or (b) I will grant\nyou any wish if you pay me one billion dollars.\nOf course, I get to choose whether I offer (a) or (b).\"\n\nThe man was wary. Did he need to sign over his soul?\nNo, said the devil, all the man need do is accept the offer.\n\nThe man pondered. If he was offered (b) it was unlikely that he would\never be able to buy the wish, but what was the harm in having the\nopportunity available?\n\n\"I accept,\" said the man at last. \"Do I get (a) or (b)?\"\n\nThe devil paused. \"I choose (b).\"\n\nThe man was disappointed but not surprised. That was that, he thought.\nBut the offer gnawed at him. Imagine what he could do with his wish!\nMany years passed, and the man began to accumulate money. To get the\nmoney he sometimes did bad things, and dimly he realised that\nthis must be what the devil had in mind.\nEventually he had his billion dollars, and the devil appeared again.\n\n\"Here is a billion dollars,\" said the man, handing over a valise\ncontaining the money. \"Grant me my wish!\"\n\nThe devil took possession of the valise. Then he said, \"Oh, did I say\n(b) before? I'm so sorry. I meant (a). It is my great pleasure to\ngive you one billion dollars.\"\n\nAnd the devil handed back to the man the same valise that the man had\njust handed to him.\n\n(Parts of the above are adopted from \"Call-by-Value is Dual to Call-by-Name\",\nPhilip Wadler, _International Conference on Functional Programming_, 2003.)\n\n\n#### Exercise `Classical` (stretch)\n\nConsider the following principles:\n\n * Excluded Middle: `A \u228e \u00ac A`, for all `A`\n * Double Negation Elimination: `\u00ac \u00ac A \u2192 A`, for all `A`\n * Peirce's Law: `((A \u2192 B) \u2192 A) \u2192 A`, for all `A` and `B`.\n * Implication as disjunction: `(A \u2192 B) \u2192 \u00ac A \u228e B`, for all `A` and `B`.\n * De Morgan: `\u00ac (\u00ac A \u00d7 \u00ac B) \u2192 A \u228e B`, for all `A` and `B`.\n\nShow that each of these implies all the others.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `Stable` (stretch)\n\nSay that a formula is _stable_ if double negation elimination holds for it:\n\\begin{code}\nStable : Set \u2192 Set\nStable A = \u00ac \u00ac A \u2192 A\n\\end{code}\nShow that any negated formula is stable, and that the conjunction\nof two stable formulas is stable.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n## Standard Prelude\n\nDefinitions similar to those in this chapter can be found in the standard library:\n\\begin{code}\nimport Relation.Nullary using (\u00ac_)\nimport Relation.Nullary.Negation using (contraposition)\n\\end{code}\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u00ac U+00AC NOT SIGN (\\neg)\n \u2262 U+2262 NOT IDENTICAL TO (\\==n)\n","avg_line_length":33.3605769231,"max_line_length":84,"alphanum_fraction":0.6941201902} +{"size":30318,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\r\ntitle : \"TypedFresh: Fresh variables in substitution (broken)\"\r\npermalink : \/TypedFresh\r\n---\r\n\r\nThis version uses raw terms. Substitution generates\r\na fresh name whenever substituting for a lambda term.\r\nThe proof is complete, but computing a reduction sequence\r\nis currently intractable, as described at the end.\r\n\r\n\r\n## Imports\r\n\r\n\\begin{code}\r\nmodule TypedFresh where\r\n\\end{code}\r\n\r\n\\begin{code}\r\nimport Relation.Binary.PropositionalEquality as Eq\r\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\r\nopen import Data.Empty using (\u22a5; \u22a5-elim)\r\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr; filter; concat)\r\nopen import Data.Nat using (\u2115; zero; suc; _+_; _\u2238_; _\u2264_; _\u2294_; _\u225f_)\r\nopen import Data.Nat.Properties using (\u2264-refl; \u2264-trans; m\u2264m\u2294n; n\u2264m\u2294n; 1+n\u2270n)\r\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\r\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\r\nopen import Function using (_\u2218_)\r\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\r\nopen import Relation.Nullary.Negation using (\u00ac?)\r\nimport Collections\r\n\r\npattern [_] w = w \u2237 []\r\npattern [_,_] w x = w \u2237 x \u2237 []\r\npattern [_,_,_] w x y = w \u2237 x \u2237 y \u2237 []\r\npattern [_,_,_,_] w x y z = w \u2237 x \u2237 y \u2237 z \u2237 []\r\n\\end{code}\r\n\r\n\r\n## Syntax\r\n\r\n\\begin{code}\r\ninfixr 5 _\u21d2_\r\ninfixl 5 _,_\u2982_\r\ninfix 4 _\u220b_\u2982_\r\ninfix 4 _\u22a2_\u2982_\r\ninfix 5 `\u03bb_\u21d2_\r\ninfixl 6 `if0_then_else_\r\ninfix 7 `suc_ `pred_ `Y_\r\ninfixl 8 _\u00b7_\r\ninfix 9 `_\r\n\r\nId : Set\r\nId = \u2115\r\n\r\ndata Type : Set where\r\n `\u2115 : Type\r\n _\u21d2_ : Type \u2192 Type \u2192 Type\r\n\r\ndata Env : Set where\r\n \u03b5 : Env\r\n _,_\u2982_ : Env \u2192 Id \u2192 Type \u2192 Env\r\n\r\ndata Term : Set where\r\n `_ : Id \u2192 Term\r\n `\u03bb_\u21d2_ : Id \u2192 Term \u2192 Term\r\n _\u00b7_ : Term \u2192 Term \u2192 Term\r\n `zero : Term\r\n `suc_ : Term \u2192 Term\r\n `pred_ : Term \u2192 Term\r\n `if0_then_else_ : Term \u2192 Term \u2192 Term \u2192 Term\r\n `Y_ : Term \u2192 Term\r\n\r\ndata _\u220b_\u2982_ : Env \u2192 Id \u2192 Type \u2192 Set where\r\n\r\n Z : \u2200 {\u0393 A x}\r\n -----------------\r\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\r\n\r\n S : \u2200 {\u0393 A B x w}\r\n \u2192 w \u2262 x\r\n \u2192 \u0393 \u220b w \u2982 B\r\n -----------------\r\n \u2192 \u0393 , x \u2982 A \u220b w \u2982 B\r\n\r\ndata _\u22a2_\u2982_ : Env \u2192 Term \u2192 Type \u2192 Set where\r\n\r\n Ax : \u2200 {\u0393 A x}\r\n \u2192 \u0393 \u220b x \u2982 A\r\n ---------------------\r\n \u2192 \u0393 \u22a2 ` x \u2982 A\r\n\r\n \u22a2\u03bb : \u2200 {\u0393 x N A B}\r\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2982 B\r\n ------------------------\r\n \u2192 \u0393 \u22a2 (`\u03bb x \u21d2 N) \u2982 A \u21d2 B\r\n\r\n _\u00b7_ : \u2200 {\u0393 L M A B}\r\n \u2192 \u0393 \u22a2 L \u2982 A \u21d2 B\r\n \u2192 \u0393 \u22a2 M \u2982 A\r\n --------------\r\n \u2192 \u0393 \u22a2 L \u00b7 M \u2982 B\r\n\r\n \u22a2zero : \u2200 {\u0393}\r\n --------------\r\n \u2192 \u0393 \u22a2 `zero \u2982 `\u2115\r\n\r\n \u22a2suc : \u2200 {\u0393 M}\r\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\r\n ---------------\r\n \u2192 \u0393 \u22a2 `suc M \u2982 `\u2115\r\n\r\n \u22a2pred : \u2200 {\u0393 M}\r\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\r\n ----------------\r\n \u2192 \u0393 \u22a2 `pred M \u2982 `\u2115\r\n\r\n \u22a2if0 : \u2200 {\u0393 L M N A}\r\n \u2192 \u0393 \u22a2 L \u2982 `\u2115\r\n \u2192 \u0393 \u22a2 M \u2982 A\r\n \u2192 \u0393 \u22a2 N \u2982 A\r\n ----------------------------\r\n \u2192 \u0393 \u22a2 `if0 L then M else N \u2982 A\r\n\r\n \u22a2Y : \u2200 {\u0393 M A}\r\n \u2192 \u0393 \u22a2 M \u2982 A \u21d2 A\r\n ---------------\r\n \u2192 \u0393 \u22a2 `Y M \u2982 A\r\n\\end{code}\r\n\r\n## Test examples\r\n\r\n\\begin{code}\r\nm n s z : Id\r\np = 0\r\nm = 1\r\nn = 2\r\ns = 3\r\nz = 4\r\n\r\ns\u2262z : s \u2262 z\r\ns\u2262z ()\r\n\r\nn\u2262z : n \u2262 z\r\nn\u2262z ()\r\n\r\nn\u2262s : n \u2262 s\r\nn\u2262s ()\r\n\r\nm\u2262z : m \u2262 z\r\nm\u2262z ()\r\n\r\nm\u2262s : m \u2262 s\r\nm\u2262s ()\r\n\r\nm\u2262n : m \u2262 n\r\nm\u2262n ()\r\n\r\np\u2262n : p \u2262 n\r\np\u2262n ()\r\n\r\np\u2262m : p \u2262 m\r\np\u2262m ()\r\n\r\ntwo : Term\r\ntwo = `suc `suc `zero\r\n\r\n\u22a2two : \u03b5 \u22a2 two \u2982 `\u2115\r\n\u22a2two = (\u22a2suc (\u22a2suc \u22a2zero))\r\n\r\nplus : Term\r\nplus = `Y (`\u03bb p \u21d2 `\u03bb m \u21d2 `\u03bb n \u21d2 `if0 ` m then ` n else `suc (` p \u00b7 (`pred ` m) \u00b7 ` n))\r\n\r\n\u22a2plus : \u03b5 \u22a2 plus \u2982 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\r\n\u22a2plus = (\u22a2Y (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2if0 (Ax \u22a2m) (Ax \u22a2n) (\u22a2suc (Ax \u22a2p \u00b7 (\u22a2pred (Ax \u22a2m)) \u00b7 Ax \u22a2n)))))))\r\n where\r\n \u22a2p = S p\u2262n (S p\u2262m Z)\r\n \u22a2m = S m\u2262n Z\r\n \u22a2n = Z\r\n\r\nfour : Term\r\nfour = plus \u00b7 two \u00b7 two\r\n\r\n\u22a2four : \u03b5 \u22a2 four \u2982 `\u2115\r\n\u22a2four = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\r\n\r\nCh : Type\r\nCh = (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115\r\n\r\ntwoCh : Term\r\ntwoCh = `\u03bb s \u21d2 `\u03bb z \u21d2 (` s \u00b7 (` s \u00b7 ` z))\r\n\r\n\u22a2twoCh : \u03b5 \u22a2 twoCh \u2982 Ch\r\n\u22a2twoCh = (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2s \u00b7 (Ax \u22a2s \u00b7 Ax \u22a2z))))\r\n where\r\n \u22a2s = S s\u2262z Z\r\n \u22a2z = Z\r\n\r\nplusCh : Term\r\nplusCh = `\u03bb m \u21d2 `\u03bb n \u21d2 `\u03bb s \u21d2 `\u03bb z \u21d2 ` m \u00b7 ` s \u00b7 (` n \u00b7 ` s \u00b7 ` z)\r\n\r\n\u22a2plusCh : \u03b5 \u22a2 plusCh \u2982 Ch \u21d2 Ch \u21d2 Ch\r\n\u22a2plusCh = (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2m \u00b7 Ax \u22a2s \u00b7 (Ax \u22a2n \u00b7 Ax \u22a2s \u00b7 Ax \u22a2z))))))\r\n where\r\n \u22a2m = S m\u2262z (S m\u2262s (S m\u2262n Z))\r\n \u22a2n = S n\u2262z (S n\u2262s Z)\r\n \u22a2s = S s\u2262z Z\r\n \u22a2z = Z\r\n\r\nfromCh : Term\r\nfromCh = `\u03bb m \u21d2 ` m \u00b7 (`\u03bb s \u21d2 `suc ` s) \u00b7 `zero\r\n\r\n\u22a2fromCh : \u03b5 \u22a2 fromCh \u2982 Ch \u21d2 `\u2115\r\n\u22a2fromCh = (\u22a2\u03bb (Ax \u22a2m \u00b7 (\u22a2\u03bb (\u22a2suc (Ax \u22a2s))) \u00b7 \u22a2zero))\r\n where\r\n \u22a2m = Z\r\n \u22a2s = Z\r\n\r\nfourCh : Term\r\nfourCh = fromCh \u00b7 (plusCh \u00b7 twoCh \u00b7 twoCh)\r\n\r\n\u22a2fourCh : \u03b5 \u22a2 fourCh \u2982 `\u2115\r\n\u22a2fourCh = \u22a2fromCh \u00b7 (\u22a2plusCh \u00b7 \u22a2twoCh \u00b7 \u22a2twoCh)\r\n\\end{code}\r\n\r\n\r\n## Erasure\r\n\r\n\\begin{code}\r\nlookup : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x \u2982 A \u2192 Id\r\nlookup {\u0393 , x \u2982 A} Z = x\r\nlookup {\u0393 , x \u2982 A} (S _ \u22a2w) = lookup {\u0393} \u22a2w\r\n\r\nerase : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2982 A \u2192 Term\r\nerase (Ax \u22a2w) = ` lookup \u22a2w\r\nerase (\u22a2\u03bb {x = x} \u22a2N) = `\u03bb x \u21d2 erase \u22a2N\r\nerase (\u22a2L \u00b7 \u22a2M) = erase \u22a2L \u00b7 erase \u22a2M\r\nerase (\u22a2zero) = `zero\r\nerase (\u22a2suc \u22a2M) = `suc (erase \u22a2M)\r\nerase (\u22a2pred \u22a2M) = `pred (erase \u22a2M)\r\nerase (\u22a2if0 \u22a2L \u22a2M \u22a2N) = `if0 (erase \u22a2L) then (erase \u22a2M) else (erase \u22a2N)\r\nerase (\u22a2Y \u22a2M) = `Y (erase \u22a2M)\r\n\\end{code}\r\n\r\n### Properties of erasure\r\n\r\n\\begin{code}\r\ncong\u2083 : \u2200 {A B C D : Set} (f : A \u2192 B \u2192 C \u2192 D) {s t u v x y} \u2192\r\n s \u2261 t \u2192 u \u2261 v \u2192 x \u2261 y \u2192 f s u x \u2261 f t v y\r\ncong\u2083 f refl refl refl = refl\r\n\r\nlookup-lemma : \u2200 {\u0393 x A} \u2192 (\u22a2x : \u0393 \u220b x \u2982 A) \u2192 lookup \u22a2x \u2261 x\r\nlookup-lemma Z = refl\r\nlookup-lemma (S _ \u22a2w) = lookup-lemma \u22a2w\r\n\r\nerase-lemma : \u2200 {\u0393 M A} \u2192 (\u22a2M : \u0393 \u22a2 M \u2982 A) \u2192 erase \u22a2M \u2261 M\r\nerase-lemma (Ax \u22a2x) = cong `_ (lookup-lemma \u22a2x)\r\nerase-lemma (\u22a2\u03bb {x = x} \u22a2N) = cong (`\u03bb x \u21d2_) (erase-lemma \u22a2N)\r\nerase-lemma (\u22a2L \u00b7 \u22a2M) = cong\u2082 _\u00b7_ (erase-lemma \u22a2L) (erase-lemma \u22a2M)\r\nerase-lemma (\u22a2zero) = refl\r\nerase-lemma (\u22a2suc \u22a2M) = cong `suc_ (erase-lemma \u22a2M)\r\nerase-lemma (\u22a2pred \u22a2M) = cong `pred_ (erase-lemma \u22a2M)\r\nerase-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) = cong\u2083 `if0_then_else_\r\n (erase-lemma \u22a2L) (erase-lemma \u22a2M) (erase-lemma \u22a2N)\r\nerase-lemma (\u22a2Y \u22a2M) = cong `Y_ (erase-lemma \u22a2M)\r\n\\end{code}\r\n\r\n\r\n## Substitution\r\n\r\n### Lists as sets\r\n\r\n\\begin{code}\r\nopen Collections (Id) (_\u225f_)\r\n\\end{code}\r\n\r\n### Free variables\r\n\r\n\\begin{code}\r\nfree : Term \u2192 List Id\r\nfree (` x) = [ x ]\r\nfree (`\u03bb x \u21d2 N) = free N \\\\ x\r\nfree (L \u00b7 M) = free L ++ free M\r\nfree (`zero) = []\r\nfree (`suc M) = free M\r\nfree (`pred M) = free M\r\nfree (`if0 L then M else N) = free L ++ free M ++ free N\r\nfree (`Y M) = free M\r\n\\end{code}\r\n\r\n### Fresh identifier\r\n\r\n\\begin{code}\r\nfresh : List Id \u2192 Id\r\nfresh = foldr _\u2294_ 0 \u2218 map suc\r\n\r\n\u2294-lemma : \u2200 {w xs} \u2192 w \u2208 xs \u2192 suc w \u2264 fresh xs\r\n\u2294-lemma {_} {_ \u2237 xs} here = m\u2264m\u2294n _ (fresh xs)\r\n\u2294-lemma {w} {x \u2237 xs} (there x\u2208) = \u2264-trans (\u2294-lemma {w} {xs} x\u2208) (n\u2264m\u2294n (suc x) (fresh xs))\r\n\r\nfresh-lemma : \u2200 {x xs} \u2192 x \u2208 xs \u2192 x \u2262 fresh xs\r\nfresh-lemma x\u2208 refl = 1+n\u2270n (\u2294-lemma x\u2208)\r\n\\end{code}\r\n\r\n### Identifier maps\r\n\r\n\\begin{code}\r\n\u2205 : Id \u2192 Term\r\n\u2205 x = ` x\r\n\r\ninfixl 5 _,_\u21a6_\r\n\r\n_,_\u21a6_ : (Id \u2192 Term) \u2192 Id \u2192 Term \u2192 (Id \u2192 Term)\r\n(\u03c1 , x \u21a6 M) w with w \u225f x\r\n... | yes _ = M\r\n... | no _ = \u03c1 w\r\n\\end{code}\r\n\r\n### Substitution\r\n\r\n\\begin{code}\r\nsubst : List Id \u2192 (Id \u2192 Term) \u2192 Term \u2192 Term\r\nsubst ys \u03c1 (` x) = \u03c1 x\r\nsubst ys \u03c1 (`\u03bb x \u21d2 N) = `\u03bb y \u21d2 subst (y \u2237 ys) (\u03c1 , x \u21a6 ` y) N\r\n where\r\n y = fresh ys\r\nsubst ys \u03c1 (L \u00b7 M) = subst ys \u03c1 L \u00b7 subst ys \u03c1 M\r\nsubst ys \u03c1 (`zero) = `zero\r\nsubst ys \u03c1 (`suc M) = `suc (subst ys \u03c1 M)\r\nsubst ys \u03c1 (`pred M) = `pred (subst ys \u03c1 M)\r\nsubst ys \u03c1 (`if0 L then M else N)\r\n = `if0 (subst ys \u03c1 L) then (subst ys \u03c1 M) else (subst ys \u03c1 N)\r\nsubst ys \u03c1 (`Y M) = `Y (subst ys \u03c1 M)\r\n\r\n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\r\nN [ x := M ] = subst (free M ++ (free N \\\\ x)) (\u2205 , x \u21a6 M) N\r\n\\end{code}\r\n\r\n### Testing substitution\r\n\r\n\\begin{code}\r\n_ : (` s \u00b7 ` s \u00b7 ` z) [ z := `zero ] \u2261 (` s \u00b7 ` s \u00b7 `zero)\r\n_ = refl\r\n\r\n_ : (` s \u00b7 ` s \u00b7 ` z) [ s := (`\u03bb m \u21d2 `suc ` m) ] [ z := `zero ]\r\n \u2261 ((`\u03bb p \u21d2 `suc ` p) \u00b7 (`\u03bb p \u21d2 `suc ` p) \u00b7 `zero)\r\n_ = refl\r\n\r\n_ : (`\u03bb m \u21d2 ` m \u00b7 ` n) [ n := ` m ] \u2261 (`\u03bb n \u21d2 ` n \u00b7 ` m)\r\n_ = refl\r\n\r\n_ : subst [ m , n ] (\u2205 , m \u21a6 ` n , n \u21a6 ` m) (` m \u00b7 ` n) \u2261 (` n \u00b7 ` m)\r\n_ = refl\r\n\\end{code}\r\n\r\n\r\n## Values\r\n\r\n\\begin{code}\r\ndata Value : Term \u2192 Set where\r\n\r\n Zero :\r\n ----------\r\n Value `zero\r\n\r\n Suc : \u2200 {V}\r\n \u2192 Value V\r\n --------------\r\n \u2192 Value (`suc V)\r\n\r\n Fun : \u2200 {x N}\r\n ---------------\r\n \u2192 Value (`\u03bb x \u21d2 N)\r\n\\end{code}\r\n\r\n## Reduction\r\n\r\n\\begin{code}\r\ninfix 4 _\u27f6_\r\n\r\ndata _\u27f6_ : Term \u2192 Term \u2192 Set where\r\n\r\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\r\n \u2192 L \u27f6 L\u2032\r\n ----------------\r\n \u2192 L \u00b7 M \u27f6 L\u2032 \u00b7 M\r\n\r\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\r\n \u2192 Value V\r\n \u2192 M \u27f6 M\u2032\r\n ----------------\r\n \u2192 V \u00b7 M \u27f6 V \u00b7 M\u2032\r\n\r\n \u03b2-\u21d2 : \u2200 {x N V}\r\n \u2192 Value V\r\n ------------------------------\r\n \u2192 (`\u03bb x \u21d2 N) \u00b7 V \u27f6 N [ x := V ]\r\n\r\n \u03be-suc : \u2200 {M M\u2032}\r\n \u2192 M \u27f6 M\u2032\r\n ------------------\r\n \u2192 `suc M \u27f6 `suc M\u2032\r\n\r\n \u03be-pred : \u2200 {M M\u2032}\r\n \u2192 M \u27f6 M\u2032\r\n --------------------\r\n \u2192 `pred M \u27f6 `pred M\u2032\r\n\r\n \u03b2-pred-zero :\r\n ---------------------\r\n `pred `zero \u27f6 `zero\r\n\r\n \u03b2-pred-suc : \u2200 {V}\r\n \u2192 Value V\r\n --------------------\r\n \u2192 `pred (`suc V) \u27f6 V\r\n\r\n \u03be-if0 : \u2200 {L L\u2032 M N}\r\n \u2192 L \u27f6 L\u2032\r\n ----------------------------------------------\r\n \u2192 `if0 L then M else N \u27f6 `if0 L\u2032 then M else N\r\n\r\n \u03b2-if0-zero : \u2200 {M N}\r\n ------------------------------\r\n \u2192 `if0 `zero then M else N \u27f6 M\r\n\r\n \u03b2-if0-suc : \u2200 {V M N}\r\n \u2192 Value V\r\n ---------------------------------\r\n \u2192 `if0 (`suc V) then M else N \u27f6 N\r\n\r\n \u03be-Y : \u2200 {M M\u2032}\r\n \u2192 M \u27f6 M\u2032\r\n --------------\r\n \u2192 `Y M \u27f6 `Y M\u2032\r\n\r\n \u03b2-Y : \u2200 {V x N}\r\n \u2192 Value V\r\n \u2192 V \u2261 `\u03bb x \u21d2 N\r\n ------------------------\r\n \u2192 `Y V \u27f6 N [ x := `Y V ]\r\n\\end{code}\r\n\r\n## Reflexive and transitive closure\r\n\r\n\\begin{code}\r\ninfix 2 _\u27f6*_\r\ninfix 1 begin_\r\ninfixr 2 _\u27f6\u27e8_\u27e9_\r\ninfix 3 _\u220e\r\n\r\ndata _\u27f6*_ : Term \u2192 Term \u2192 Set where\r\n\r\n _\u220e : \u2200 (M : Term)\r\n -------------\r\n \u2192 M \u27f6* M\r\n\r\n _\u27f6\u27e8_\u27e9_ : \u2200 (L : Term) {M N}\r\n \u2192 L \u27f6 M\r\n \u2192 M \u27f6* N\r\n ---------\r\n \u2192 L \u27f6* N\r\n\r\nbegin_ : \u2200 {M N} \u2192 (M \u27f6* N) \u2192 (M \u27f6* N)\r\nbegin M\u27f6*N = M\u27f6*N\r\n\\end{code}\r\n\r\n## Canonical forms\r\n\r\n\\begin{code}\r\ndata Canonical : Term \u2192 Type \u2192 Set where\r\n\r\n Zero :\r\n ------------------\r\n Canonical `zero `\u2115\r\n\r\n Suc : \u2200 {V}\r\n \u2192 Canonical V `\u2115\r\n ---------------------\r\n \u2192 Canonical (`suc V) `\u2115\r\n\r\n Fun : \u2200 {x N A B}\r\n \u2192 \u03b5 , x \u2982 A \u22a2 N \u2982 B\r\n ------------------------------\r\n \u2192 Canonical (`\u03bb x \u21d2 N) (A \u21d2 B)\r\n\\end{code}\r\n\r\n## Canonical forms lemma\r\n\r\nEvery typed value is canonical.\r\n\r\n\\begin{code}\r\ncanonical : \u2200 {V A}\r\n \u2192 \u03b5 \u22a2 V \u2982 A\r\n \u2192 Value V\r\n -------------\r\n \u2192 Canonical V A\r\ncanonical \u22a2zero Zero = Zero\r\ncanonical (\u22a2suc \u22a2V) (Suc VV) = Suc (canonical \u22a2V VV)\r\ncanonical (\u22a2\u03bb \u22a2N) Fun = Fun \u22a2N\r\n\\end{code}\r\n\r\nEvery canonical form has a type and a value.\r\n\r\n\\begin{code}\r\ntype : \u2200 {V A}\r\n \u2192 Canonical V A\r\n -------------\r\n \u2192 \u03b5 \u22a2 V \u2982 A\r\ntype Zero = \u22a2zero\r\ntype (Suc CV) = \u22a2suc (type CV)\r\ntype (Fun \u22a2N) = \u22a2\u03bb \u22a2N\r\n\r\nvalue : \u2200 {V A}\r\n \u2192 Canonical V A\r\n -------------\r\n \u2192 Value V\r\nvalue Zero = Zero\r\nvalue (Suc CV) = Suc (value CV)\r\nvalue (Fun \u22a2N) = Fun\r\n\\end{code}\r\n\r\n## Progress\r\n\r\n\\begin{code}\r\ndata Progress (M : Term) (A : Type) : Set where\r\n step : \u2200 {N}\r\n \u2192 M \u27f6 N\r\n ----------\r\n \u2192 Progress M A\r\n done :\r\n Canonical M A\r\n -------------\r\n \u2192 Progress M A\r\n\r\nprogress : \u2200 {M A} \u2192 \u03b5 \u22a2 M \u2982 A \u2192 Progress M A\r\nprogress (Ax ())\r\nprogress (\u22a2\u03bb \u22a2N) = done (Fun \u22a2N)\r\nprogress (\u22a2L \u00b7 \u22a2M) with progress \u22a2L\r\n... | step L\u27f6L\u2032 = step (\u03be-\u00b7\u2081 L\u27f6L\u2032)\r\n... | done (Fun _) with progress \u22a2M\r\n... | step M\u27f6M\u2032 = step (\u03be-\u00b7\u2082 Fun M\u27f6M\u2032)\r\n... | done CM = step (\u03b2-\u21d2 (value CM))\r\nprogress \u22a2zero = done Zero\r\nprogress (\u22a2suc \u22a2M) with progress \u22a2M\r\n... | step M\u27f6M\u2032 = step (\u03be-suc M\u27f6M\u2032)\r\n... | done CM = done (Suc CM)\r\nprogress (\u22a2pred \u22a2M) with progress \u22a2M\r\n... | step M\u27f6M\u2032 = step (\u03be-pred M\u27f6M\u2032)\r\n... | done Zero = step \u03b2-pred-zero\r\n... | done (Suc CM) = step (\u03b2-pred-suc (value CM))\r\nprogress (\u22a2if0 \u22a2L \u22a2M \u22a2N) with progress \u22a2L\r\n... | step L\u27f6L\u2032 = step (\u03be-if0 L\u27f6L\u2032)\r\n... | done Zero = step \u03b2-if0-zero\r\n... | done (Suc CM) = step (\u03b2-if0-suc (value CM))\r\nprogress (\u22a2Y \u22a2M) with progress \u22a2M\r\n... | step M\u27f6M\u2032 = step (\u03be-Y M\u27f6M\u2032)\r\n... | done (Fun _) = step (\u03b2-Y Fun refl)\r\n\\end{code}\r\n\r\n\r\n## Preservation\r\n\r\n### Domain of an environment\r\n\r\n\\begin{code}\r\ndom : Env \u2192 List Id\r\ndom \u03b5 = []\r\ndom (\u0393 , x \u2982 A) = x \u2237 dom \u0393\r\n\r\ndom-lemma : \u2200 {\u0393 y B} \u2192 \u0393 \u220b y \u2982 B \u2192 y \u2208 dom \u0393\r\ndom-lemma Z = here\r\ndom-lemma (S x\u2262y \u22a2y) = there (dom-lemma \u22a2y)\r\n\r\nfree-lemma : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2982 A \u2192 free M \u2286 dom \u0393\r\nfree-lemma (Ax \u22a2x) w\u2208 with w\u2208\r\n... | here = dom-lemma \u22a2x\r\n... | there ()\r\nfree-lemma {\u0393} (\u22a2\u03bb {N = N} \u22a2N) = \u2237-to-\\\\ (free-lemma \u22a2N)\r\nfree-lemma (\u22a2L \u00b7 \u22a2M) w\u2208 with ++-to-\u228e w\u2208\r\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\r\n... | inj\u2082 \u2208M = free-lemma \u22a2M \u2208M\r\nfree-lemma \u22a2zero ()\r\nfree-lemma (\u22a2suc \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\r\nfree-lemma (\u22a2pred \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\r\nfree-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) w\u2208\r\n with ++-to-\u228e w\u2208\r\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\r\n... | inj\u2082 \u2208MN with ++-to-\u228e \u2208MN\r\n... | inj\u2081 \u2208M = free-lemma \u22a2M \u2208M\r\n... | inj\u2082 \u2208N = free-lemma \u22a2N \u2208N\r\nfree-lemma (\u22a2Y \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\r\n\\end{code}\r\n\r\n### Renaming\r\n\r\n\\begin{code}\r\n\u22a2rename : \u2200 {\u0393 \u0394 xs}\r\n \u2192 (\u2200 {x A} \u2192 x \u2208 xs \u2192 \u0393 \u220b x \u2982 A \u2192 \u0394 \u220b x \u2982 A)\r\n --------------------------------------------------\r\n \u2192 (\u2200 {M A} \u2192 free M \u2286 xs \u2192 \u0393 \u22a2 M \u2982 A \u2192 \u0394 \u22a2 M \u2982 A)\r\n\u22a2rename \u22a2\u03c3 \u2286xs (Ax \u22a2x) = Ax (\u22a2\u03c3 \u2208xs \u22a2x)\r\n where\r\n \u2208xs = \u2286xs here\r\n\u22a2rename {\u0393} {\u0394} {xs} \u22a2\u03c3 \u2286xs (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\r\n = \u22a2\u03bb (\u22a2rename {\u0393\u2032} {\u0394\u2032} {xs\u2032} \u22a2\u03c3\u2032 \u2286xs\u2032 \u22a2N)\r\n where\r\n \u0393\u2032 = \u0393 , x \u2982 A\r\n \u0394\u2032 = \u0394 , x \u2982 A\r\n xs\u2032 = x \u2237 xs\r\n\r\n \u22a2\u03c3\u2032 : \u2200 {w B} \u2192 w \u2208 xs\u2032 \u2192 \u0393\u2032 \u220b w \u2982 B \u2192 \u0394\u2032 \u220b w \u2982 B\r\n \u22a2\u03c3\u2032 w\u2208\u2032 Z = Z\r\n \u22a2\u03c3\u2032 w\u2208\u2032 (S w\u2262 \u22a2w) = S w\u2262 (\u22a2\u03c3 \u2208w \u22a2w)\r\n where\r\n \u2208w = there\u207b\u00b9 w\u2208\u2032 w\u2262\r\n\r\n \u2286xs\u2032 : free N \u2286 xs\u2032\r\n \u2286xs\u2032 = \\\\-to-\u2237 \u2286xs\r\n\u22a2rename \u22a2\u03c3 \u2286xs (_\u00b7_ {L = L} {M = M} \u22a2L \u22a2M) = \u22a2rename \u22a2\u03c3 L\u2286 \u22a2L \u00b7 \u22a2rename \u22a2\u03c3 M\u2286 \u22a2M\r\n where\r\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\r\n M\u2286 = trans-\u2286 (\u2286-++\u2082 {free L} {free M}) \u2286xs\r\n\r\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2zero) = \u22a2zero\r\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2suc \u22a2M) = \u22a2suc (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\r\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2pred \u22a2M) = \u22a2pred (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\r\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2if0 {L = L} {M = M} {N = N} \u22a2L \u22a2M \u22a2N)\r\n = \u22a2if0 (\u22a2rename \u22a2\u03c3 L\u2286 \u22a2L) (\u22a2rename \u22a2\u03c3 M\u2286 \u22a2M) (\u22a2rename \u22a2\u03c3 N\u2286 \u22a2N)\r\n where\r\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\r\n M\u2286 = trans-\u2286 \u2286-++\u2081 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\r\n N\u2286 = trans-\u2286 (\u2286-++\u2082 {free M} {free N}) (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\r\n\r\n\u22a2rename \u22a2\u03c3 \u2286xs (\u22a2Y \u22a2M) = \u22a2Y (\u22a2rename \u22a2\u03c3 \u2286xs \u22a2M)\r\n\r\n\\end{code}\r\n\r\n\r\n### Substitution preserves types\r\n\r\nI can remove the `ys` argument from `subst` by always\r\ncomputing it as `frees \u03c1 M`. As I step into an abstraction,\r\nthis will automatically add the renaming of the bound\r\nvariable, since the bound variable gets added to `M` and\r\nits renaming gets added to `\u03c1`.\r\n\r\nA possible new version.\r\nSimpler to state, is it simpler to prove?\r\n\r\nWhat may be tricky to establish is that sufficient\r\nrenaming occurs to justify the `\u2262` term added to `S`.\r\n\r\nIt may be hard to establish because we use it\r\nwhen we instantiate `\u22a2rename\u2032 {\u0394} {\u0394 , y \u2982 A} (S w\u2262y)`\r\nand the problem is that `w\u2262y` for every `w` in\r\n`free (\u03c1 x)`, but there may be other `w` in `\u0394`.\r\nBugger. But I think the theorems are true despite\r\nthis problem in the proof, so there may be a way\r\naround it.\r\n\r\n frees : (Id \u2192 Term) \u2192 Term \u2192 List Id\r\n frees \u03c1 M = concat (map (free \u2218 \u03c1) (free M))\r\n\r\n subst\u2032 : (Id \u2192 Term) \u2192 Term \u2192 Term\r\n subst\u2032 \u03c1 M = subst (frees \u03c1 M) \u03c1 M\r\n\r\n \u22a2rename\u2032 : \u2200 {\u0393 \u0394 M A}\r\n \u2192 (\u2200 {w B} \u2192 \u0393 \u220b w \u2982 B \u2192 \u0394 \u220b w \u2982 B)\r\n \u2192 \u0393 \u22a2 M \u2982 A\r\n ----------------------------------\r\n \u2192 \u0394 \u22a2 M \u2982 A\r\n \u22a2rename\u2032 = {!!}\r\n\r\n \u22a2subst\u2032 : \u2200 {\u0393 \u0394 \u03c1 M A}\r\n \u2192 (\u2200 {w B} \u2192 \u0393 \u220b w \u2982 B \u2192 \u0394 \u22a2 \u03c1 w \u2982 B)\r\n \u2192 \u0393 \u22a2 M \u2982 A\r\n ------------------------------------\r\n \u2192 \u0394 \u22a2 subst\u2032 \u03c1 M \u2982 A\r\n \u22a2subst\u2032 = {!!}\r\n\r\nI expect `\u22a2rename\u2032` is easy to show.\r\n\r\nI think `\u22a2subst\u2032` is true but hard to show.\r\nThe difficult part is to establish the argument to `\u22a2rename\u2032`,\r\nsince I can only create a variable that is fresh with regard to\r\nnames in frees, not all names in the domain of `\u0394`.\r\n\r\nCan I establish a counter-example, or convince myself that\r\n`\u22a2subst\u2032` is true anyway? The latter is easily seen to be true,\r\nsince the simpler formulations are immediate consequences of\r\nwhat I have already proved.\r\n\r\nStepping into a subterm, just need to precompose `\u03c1` with a\r\nlemma stating that the free variables of the subterm are\r\na subset of the free variables of the term.\r\n\r\n\r\nPrevious version\r\n\\begin{code}\r\n\u22a2subst : \u2200 {\u0393 \u0394 xs ys \u03c1}\r\n \u2192 (\u2200 {x} \u2192 x \u2208 xs \u2192 free (\u03c1 x) \u2286 ys)\r\n \u2192 (\u2200 {x A} \u2192 x \u2208 xs \u2192 \u0393 \u220b x \u2982 A \u2192 \u0394 \u22a2 \u03c1 x \u2982 A)\r\n -------------------------------------------------------------\r\n \u2192 (\u2200 {M A} \u2192 free M \u2286 xs \u2192 \u0393 \u22a2 M \u2982 A \u2192 \u0394 \u22a2 subst ys \u03c1 M \u2982 A)\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (Ax \u22a2x)\r\n = \u22a2\u03c1 (\u2286xs here) \u22a2x\r\n\u22a2subst {\u0393} {\u0394} {xs} {ys} {\u03c1} \u03a3 \u22a2\u03c1 \u2286xs (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\r\n = \u22a2\u03bb {x = y} {A = A} (\u22a2subst {\u0393\u2032} {\u0394\u2032} {xs\u2032} {ys\u2032} {\u03c1\u2032} \u03a3\u2032 \u22a2\u03c1\u2032 \u2286xs\u2032 \u22a2N)\r\n where\r\n y = fresh ys\r\n \u0393\u2032 = \u0393 , x \u2982 A\r\n \u0394\u2032 = \u0394 , y \u2982 A\r\n xs\u2032 = x \u2237 xs\r\n ys\u2032 = y \u2237 ys\r\n \u03c1\u2032 = \u03c1 , x \u21a6 ` y\r\n\r\n \u03a3\u2032 : \u2200 {w} \u2192 w \u2208 xs\u2032 \u2192 free (\u03c1\u2032 w) \u2286 ys\u2032\r\n \u03a3\u2032 {w} w\u2208\u2032 with w \u225f x\r\n ... | yes refl = \u2286-++\u2081\r\n ... | no w\u2262 = (there {x = y}) \u2218 \u03a3 (there\u207b\u00b9 w\u2208\u2032 w\u2262)\r\n -- (\u2286-++\u2082 {[ y ]} {ys}) \u2218 \u03a3 (there\u207b\u00b9 w\u2208\u2032 w\u2262)\r\n\r\n \u2286xs\u2032 : free N \u2286 xs\u2032\r\n \u2286xs\u2032 = \\\\-to-\u2237 \u2286xs\r\n\r\n \u22a2\u03c3 : \u2200 {w C} \u2192 w \u2208 ys \u2192 \u0394 \u220b w \u2982 C \u2192 \u0394\u2032 \u220b w \u2982 C\r\n \u22a2\u03c3 w\u2208 \u22a2w = S (fresh-lemma w\u2208) \u22a2w\r\n\r\n \u22a2\u03c1\u2032 : \u2200 {w C} \u2192 w \u2208 xs\u2032 \u2192 \u0393\u2032 \u220b w \u2982 C \u2192 \u0394\u2032 \u22a2 \u03c1\u2032 w \u2982 C\r\n \u22a2\u03c1\u2032 {w} _ Z with w \u225f x\r\n ... | yes _ = Ax Z\r\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\r\n \u22a2\u03c1\u2032 {w} w\u2208\u2032 (S w\u2262 \u22a2w) with w \u225f x\r\n ... | yes refl = \u22a5-elim (w\u2262 refl)\r\n ... | no _ = \u22a2rename {\u0394} {\u0394\u2032} {ys} \u22a2\u03c3 (\u03a3 w\u2208) (\u22a2\u03c1 w\u2208 \u22a2w)\r\n where\r\n w\u2208 = there\u207b\u00b9 w\u2208\u2032 w\u2262\r\n\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (_\u00b7_ {L = L} {M = M} \u22a2L \u22a2M)\r\n = \u22a2subst \u03a3 \u22a2\u03c1 L\u2286 \u22a2L \u00b7 \u22a2subst \u03a3 \u22a2\u03c1 M\u2286 \u22a2M\r\n where\r\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\r\n M\u2286 = trans-\u2286 (\u2286-++\u2082 {free L} {free M}) \u2286xs\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2zero = \u22a2zero\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2suc \u22a2M) = \u22a2suc (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M)\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2pred \u22a2M) = \u22a2pred (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M)\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2if0 {L = L} {M = M} {N = N} \u22a2L \u22a2M \u22a2N)\r\n = \u22a2if0 (\u22a2subst \u03a3 \u22a2\u03c1 L\u2286 \u22a2L) (\u22a2subst \u03a3 \u22a2\u03c1 M\u2286 \u22a2M) (\u22a2subst \u03a3 \u22a2\u03c1 N\u2286 \u22a2N)\r\n where\r\n L\u2286 = trans-\u2286 \u2286-++\u2081 \u2286xs\r\n M\u2286 = trans-\u2286 \u2286-++\u2081 (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\r\n N\u2286 = trans-\u2286 (\u2286-++\u2082 {free M} {free N}) (trans-\u2286 (\u2286-++\u2082 {free L}) \u2286xs)\r\n\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs (\u22a2Y \u22a2M) = \u22a2Y (\u22a2subst \u03a3 \u22a2\u03c1 \u2286xs \u22a2M)\r\n\r\n\u22a2substitution : \u2200 {\u0393 x A N B M} \u2192\r\n \u0393 , x \u2982 A \u22a2 N \u2982 B \u2192\r\n \u0393 \u22a2 M \u2982 A \u2192\r\n --------------------\r\n \u0393 \u22a2 N [ x := M ] \u2982 B\r\n\u22a2substitution {\u0393} {x} {A} {N} {B} {M} \u22a2N \u22a2M =\r\n \u22a2subst {\u0393\u2032} {\u0393} {xs} {ys} {\u03c1} \u03a3 \u22a2\u03c1 {N} {B} \u2286xs \u22a2N\r\n where\r\n \u0393\u2032 = \u0393 , x \u2982 A\r\n xs = free N\r\n ys = free M ++ (free N \\\\ x)\r\n \u03c1 = \u2205 , x \u21a6 M\r\n\r\n \u03a3 : \u2200 {w} \u2192 w \u2208 xs \u2192 free (\u03c1 w) \u2286 ys\r\n \u03a3 {w} w\u2208 y\u2208 with w \u225f x\r\n ... | yes _ = \u2286-++\u2081 y\u2208\r\n ... | no w\u2262 rewrite \u2208-[_] y\u2208 = \u2286-++\u2082 {free M} {(free N) \\\\ x} (\u2208-\u2262-to-\\\\ w\u2208 w\u2262)\r\n\r\n \u22a2\u03c1 : \u2200 {w B} \u2192 w \u2208 xs \u2192 \u0393\u2032 \u220b w \u2982 B \u2192 \u0393 \u22a2 \u03c1 w \u2982 B\r\n \u22a2\u03c1 {w} w\u2208 Z with w \u225f x\r\n ... | yes _ = \u22a2M\r\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\r\n \u22a2\u03c1 {w} w\u2208 (S w\u2262 \u22a2w) with w \u225f x\r\n ... | yes refl = \u22a5-elim (w\u2262 refl)\r\n ... | no _ = Ax \u22a2w\r\n\r\n \u2286xs : free N \u2286 xs\r\n \u2286xs x\u2208 = x\u2208\r\n\\end{code}\r\n\r\n### Preservation\r\n\r\n\\begin{code}\r\npreservation : \u2200 {\u0393 M N A}\r\n \u2192 \u0393 \u22a2 M \u2982 A\r\n \u2192 M \u27f6 N\r\n ---------\r\n \u2192 \u0393 \u22a2 N \u2982 A\r\npreservation (Ax \u22a2x) ()\r\npreservation (\u22a2\u03bb \u22a2N) ()\r\npreservation (\u22a2L \u00b7 \u22a2M) (\u03be-\u00b7\u2081 L\u27f6) = preservation \u22a2L L\u27f6 \u00b7 \u22a2M\r\npreservation (\u22a2V \u00b7 \u22a2M) (\u03be-\u00b7\u2082 _ M\u27f6) = \u22a2V \u00b7 preservation \u22a2M M\u27f6\r\npreservation ((\u22a2\u03bb \u22a2N) \u00b7 \u22a2W) (\u03b2-\u21d2 _) = \u22a2substitution \u22a2N \u22a2W\r\npreservation (\u22a2zero) ()\r\npreservation (\u22a2suc \u22a2M) (\u03be-suc M\u27f6) = \u22a2suc (preservation \u22a2M M\u27f6)\r\npreservation (\u22a2pred \u22a2M) (\u03be-pred M\u27f6) = \u22a2pred (preservation \u22a2M M\u27f6)\r\npreservation (\u22a2pred \u22a2zero) (\u03b2-pred-zero) = \u22a2zero\r\npreservation (\u22a2pred (\u22a2suc \u22a2M)) (\u03b2-pred-suc _) = \u22a2M\r\npreservation (\u22a2if0 \u22a2L \u22a2M \u22a2N) (\u03be-if0 L\u27f6) = \u22a2if0 (preservation \u22a2L L\u27f6) \u22a2M \u22a2N\r\npreservation (\u22a2if0 \u22a2zero \u22a2M \u22a2N) \u03b2-if0-zero = \u22a2M\r\npreservation (\u22a2if0 (\u22a2suc \u22a2V) \u22a2M \u22a2N) (\u03b2-if0-suc _) = \u22a2N\r\npreservation (\u22a2Y \u22a2M) (\u03be-Y M\u27f6) = \u22a2Y (preservation \u22a2M M\u27f6)\r\npreservation (\u22a2Y (\u22a2\u03bb \u22a2N)) (\u03b2-Y _ refl) = \u22a2substitution \u22a2N (\u22a2Y (\u22a2\u03bb \u22a2N))\r\n\\end{code}\r\n\r\n## Normalise\r\n\r\n\\begin{code}\r\ndata Normalise {M A} (\u22a2M : \u03b5 \u22a2 M \u2982 A) : Set where\r\n out-of-gas : \u2200 {N} \u2192 M \u27f6* N \u2192 \u03b5 \u22a2 N \u2982 A \u2192 Normalise \u22a2M\r\n normal : \u2200 {V} \u2192 \u2115 \u2192 Canonical V A \u2192 M \u27f6* V \u2192 Normalise \u22a2M\r\n\r\nnormalise : \u2200 {L A} \u2192 \u2115 \u2192 (\u22a2L : \u03b5 \u22a2 L \u2982 A) \u2192 Normalise \u22a2L\r\nnormalise {L} zero \u22a2L = out-of-gas (L \u220e) \u22a2L\r\nnormalise {L} (suc m) \u22a2L with progress \u22a2L\r\n... | done CL = normal (suc m) CL (L \u220e)\r\n... | step L\u27f6M with preservation \u22a2L L\u27f6M\r\n... | \u22a2M with normalise m \u22a2M\r\n... | out-of-gas M\u27f6*N \u22a2N = out-of-gas (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*N) \u22a2N\r\n... | normal n CV M\u27f6*V = normal n CV (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*V)\r\n\r\n_ : Normalise \u22a2four\r\n_ = normalise 4 \u22a2four -- as normalize-4.txt\r\n\r\n_ : Normalise \u22a2four\r\n_ = normalise 8 \u22a2four -- as normalize-8.txt\r\n\r\n_ : Normalise \u22a2four\r\n_ = normalise 16 \u22a2four -- as normalize-16.txt\r\n\r\n\\end{code}\r\n\r\n## Test case\r\n\r\n\\begin{code}\r\n_ : plus \u00b7 two \u00b7 two \u27f6* (`suc (`suc (`suc (`suc `zero))))\r\n_ = (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`suc (`suc `zero))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl)) \u27e9\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`suc (`suc `zero))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u21d2 (Suc (Suc Zero))) \u27e9\r\n (`\u03bb 0 \u21d2\r\n `if0 `suc (`suc `zero) then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03b2-\u21d2 (Suc (Suc Zero)) \u27e9\r\n `if0 `suc (`suc `zero) then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03b2-if0-suc (Suc Zero) \u27e9\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc (Suc Zero)))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`suc `zero)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u21d2 (Suc Zero))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n `if0 `suc `zero then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03b2-\u21d2 (Suc (Suc Zero))) \u27e9\r\n `suc\r\n (`if0 `suc `zero then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03b2-if0-suc Zero) \u27e9\r\n `suc\r\n (`suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl)))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc Zero)))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 `zero\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u21d2 Zero))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n `if0 `zero then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred `zero)\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u21d2 (Suc (Suc Zero)))) \u27e9\r\n `suc\r\n (`suc\r\n (`if0 `zero then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred `zero)\r\n \u00b7 (`suc (`suc `zero))))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc \u03b2-if0-zero) \u27e9\r\n `suc (`suc (`suc (`suc `zero)))\r\n \u220e\r\n\\end{code}\r\n\r\n### Test case for normalise\r\n\r\n\\begin{code}\r\n_ : normalise 100 \u22a2four \u2261\r\n normal 86 (Suc (Suc (Suc (Suc Zero))))\r\n ((`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`suc (`suc `zero))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl)) \u27e9\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`suc (`suc `zero))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u21d2 (Suc (Suc Zero))) \u27e9\r\n (`\u03bb 0 \u21d2\r\n `if0 `suc (`suc `zero) then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03b2-\u21d2 (Suc (Suc Zero)) \u27e9\r\n `if0 `suc (`suc `zero) then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03b2-if0-suc (Suc Zero) \u27e9\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`pred (`suc (`suc `zero)))\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc (Suc Zero)))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`suc `zero)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u21d2 (Suc Zero))) \u27e9\r\n `suc\r\n (`\u03bb 0 \u21d2\r\n `if0 `suc `zero then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero))\r\n \u27f6\u27e8 \u03be-suc (\u03b2-\u21d2 (Suc (Suc Zero))) \u27e9\r\n `suc\r\n (`if0 `suc `zero then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03b2-if0-suc Zero) \u27e9\r\n `suc\r\n (`suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y Fun refl)))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 (`pred (`suc `zero))\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc Zero)))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n `if0 ` 0 then ` 1 else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred ` 0)\r\n \u00b7 ` 1))\r\n \u00b7 `zero\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u21d2 Zero))) \u27e9\r\n `suc\r\n (`suc\r\n (`\u03bb 0 \u21d2\r\n `if0 `zero then ` 0 else\r\n `suc\r\n (`Y\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2\r\n (`\u03bb 3 \u21d2 `if0 ` 2 then ` 3 else `suc ` 1 \u00b7 (`pred ` 2) \u00b7 ` 3))))\r\n \u00b7 (`pred `zero)\r\n \u00b7 ` 0)\r\n \u00b7 (`suc (`suc `zero)))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u21d2 (Suc (Suc Zero)))) \u27e9\r\n `suc\r\n (`suc\r\n (`if0 `zero then `suc (`suc `zero) else\r\n `suc\r\n (`Y\r\n (`\u03bb 0 \u21d2\r\n (`\u03bb 1 \u21d2\r\n (`\u03bb 2 \u21d2 `if0 ` 1 then ` 2 else `suc ` 0 \u00b7 (`pred ` 1) \u00b7 ` 2))))\r\n \u00b7 (`pred `zero)\r\n \u00b7 (`suc (`suc `zero))))\r\n \u27f6\u27e8 \u03be-suc (\u03be-suc \u03b2-if0-zero) \u27e9\r\n `suc (`suc (`suc (`suc `zero)))\r\n \u220e)\r\n_ = refl\r\n\\end{code}\r\n","avg_line_length":25.7806122449,"max_line_length":95,"alphanum_fraction":0.3932317435} +{"size":5833,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Scoped.Erasure.RenamingSubstitution where\n\\end{code}\n\n\\begin{code}\nopen import Untyped\nimport Untyped.RenamingSubstitution as U\nopen import Scoped\nopen import Scoped.Erasure\nimport Scoped.RenamingSubstitution as S\n\nopen import Data.Nat\nopen import Data.Fin\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Function\nopen import Data.List\nopen import Utils\n\\end{code}\n\n\\begin{code}\nbackVar : \u2200{n}{w : Weird\u2115 n} \u2192 Fin (len w) \u2192 WeirdFin w\nbackVar {w = S w} zero = Z\nbackVar {w = S w} (suc i) = S (backVar i)\nbackVar {w = T w} i = T (backVar i)\n\nbackVar-eraseVar : \u2200{n}{w : Weird\u2115 n}(i : WeirdFin w)\n \u2192 backVar (eraseVar i) \u2261 i\nbackVar-eraseVar Z = refl\nbackVar-eraseVar (S i) = cong S (backVar-eraseVar i)\nbackVar-eraseVar (T i) = cong T (backVar-eraseVar i)\n\neraseVar-backVar : \u2200{n}{w : Weird\u2115 n}(i : Fin (len w))\n \u2192 eraseVar (backVar {n}{w} i) \u2261 i\neraseVar-backVar {w = S w} zero = refl\neraseVar-backVar {w = S w} (suc i) = cong suc (eraseVar-backVar {w = w} i)\neraseVar-backVar {w = T w} i = eraseVar-backVar {w = w} i\n\nerase-Ren : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 S.Ren w w'\n \u2192 U.Ren (len w) (len w')\nerase-Ren \u03c1 i = eraseVar (\u03c1 (backVar i))\n\nlift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (\u03b1 : Fin (len (S w)))\n \u2192 U.lift (erase-Ren \u03c1) \u03b1 \u2261 erase-Ren (S.lift \u03c1) \u03b1\nlift-erase \u03c1 zero = refl\nlift-erase \u03c1 (suc \u03b1) = refl\n\nren-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1\u22c6 : S.Ren\u22c6 n n')\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (t : ScopedTm w)\n \u2192 U.ren (erase-Ren \u03c1) (eraseTm t) \u2261 eraseTm (S.ren \u03c1\u22c6 \u03c1 t)\n\nren-eraseList : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1\u22c6 : S.Ren\u22c6 n n')\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (ts : List (ScopedTm w))\n \u2192 U.renList (erase-Ren \u03c1) (eraseList ts) \u2261 eraseList (S.renL \u03c1\u22c6 \u03c1 ts)\nren-eraseList \u03c1\u22c6 \u03c1 [] = refl\nren-eraseList \u03c1\u22c6 \u03c1 (t \u2237 ts) =\n cong\u2082 _\u2237_ (ren-erase \u03c1\u22c6 \u03c1 t) (ren-eraseList \u03c1\u22c6 \u03c1 ts)\n\nren-erase \u03c1\u22c6 \u03c1 (` x) =\n cong (` \u2218 eraseVar \u2218 \u03c1) (backVar-eraseVar x) \nren-erase \u03c1\u22c6 \u03c1 (\u039b K t) = cong \u019b (trans\n (sym (U.ren-comp suc (U.lift (erase-Ren \u03c1)) (eraseTm t)))\n (trans (U.ren-comp (erase-Ren (S.\u22c6lift \u03c1)) suc (eraseTm t))\n (cong U.weaken (ren-erase (S.lift\u22c6 \u03c1\u22c6) (S.\u22c6lift \u03c1) t))))\nren-erase \u03c1\u22c6 \u03c1 (t \u00b7\u22c6 A) = cong (_\u00b7 plc_dummy) (ren-erase \u03c1\u22c6 \u03c1 t)\nren-erase \u03c1\u22c6 \u03c1 (\u019b A t) = cong\n \u019b\n (trans (U.ren-cong (lift-erase \u03c1) (eraseTm t)) (ren-erase \u03c1\u22c6 (S.lift \u03c1) t))\nren-erase \u03c1\u22c6 \u03c1 (t \u00b7 u) =\n cong\u2082 _\u00b7_ (ren-erase \u03c1\u22c6 \u03c1 t) (ren-erase \u03c1\u22c6 \u03c1 u)\nren-erase \u03c1\u22c6 \u03c1 (con x) = refl\nren-erase \u03c1\u22c6 \u03c1 (error x) = refl\nren-erase \u03c1\u22c6 \u03c1 (builtin bn As ts) = cong (builtin bn) (ren-eraseList \u03c1\u22c6 \u03c1 ts)\nren-erase \u03c1\u22c6 \u03c1 (wrap pat ar t) = ren-erase \u03c1\u22c6 \u03c1 t\nren-erase \u03c1\u22c6 \u03c1 (unwrap t) = ren-erase \u03c1\u22c6 \u03c1 t\n\n--\n\nerase-Sub : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 S.Sub w w' \u2192 U.Sub (len w) (len w')\nerase-Sub \u03c3 i = eraseTm (\u03c3 (backVar i))\n\nslift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (\u03b1 : Fin (len (S w)))\n \u2192 U.lifts (erase-Sub \u03c3) \u03b1 \u2261 erase-Sub (S.slift \u03c3) \u03b1\nslift-erase \u03c3 zero = refl\nslift-erase {w' = w'} \u03c3 (suc \u03b1) = trans\n (U.ren-cong (cong suc \u2218 sym \u2218 eraseVar-backVar {w = w'})\n (eraseTm (\u03c3 (backVar \u03b1))))\n (ren-erase id S (\u03c3 (backVar \u03b1)))\n\n\u22c6slift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (\u03b1 : Fin (len w))\n \u2192 erase-Sub \u03c3 \u03b1 \u2261 erase-Sub (S.\u22c6slift \u03c3) \u03b1\n\u22c6slift-erase {w' = w'} \u03c3 \u03b1 = trans\n (trans (U.ren-id (eraseTm (\u03c3 (backVar \u03b1))))\n (U.ren-cong (sym \u2218 eraseVar-backVar {w = w'})\n (eraseTm (\u03c3 (backVar \u03b1)))))\n (ren-erase suc T (\u03c3 (backVar \u03b1)))\n\nsub-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3\u22c6 : S.Sub\u22c6 n n')\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (t : ScopedTm w)\n \u2192 U.sub (erase-Sub \u03c3) (eraseTm t) \u2261 eraseTm (S.sub \u03c3\u22c6 \u03c3 t)\n\nsubList-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3\u22c6 : S.Sub\u22c6 n n')\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (ts : List (ScopedTm w))\n \u2192 U.subList (erase-Sub \u03c3) (eraseList ts) \u2261 eraseList (S.subL \u03c3\u22c6 \u03c3 ts)\nsubList-erase \u03c3\u22c6 \u03c3 [] = refl\nsubList-erase \u03c3\u22c6 \u03c3 (t \u2237 ts) =\n cong\u2082 _\u2237_ (sub-erase \u03c3\u22c6 \u03c3 t) (subList-erase \u03c3\u22c6 \u03c3 ts)\n\nsub-erase \u03c3\u22c6 \u03c3 (` x) = cong (eraseTm \u2218 \u03c3) (backVar-eraseVar x)\nsub-erase \u03c3\u22c6 \u03c3 (\u039b K t) = cong \u019b (trans\n (sym (U.sub-ren suc (U.lifts (erase-Sub \u03c3)) (eraseTm t)))\n (trans (trans (U.sub-cong (cong U.weaken \u2218 \u22c6slift-erase \u03c3 ) (eraseTm t)) (U.ren-sub (erase-Sub (S.\u22c6slift \u03c3)) suc (eraseTm t)))\n (cong U.weaken (sub-erase (S.slift\u22c6 \u03c3\u22c6) (S.\u22c6slift \u03c3) t))))\nsub-erase \u03c3\u22c6 \u03c3 (t \u00b7\u22c6 A) = cong (_\u00b7 plc_dummy) (sub-erase \u03c3\u22c6 \u03c3 t)\nsub-erase \u03c3\u22c6 \u03c3 (\u019b A t) = cong\n \u019b\n (trans (U.sub-cong (slift-erase \u03c3) (eraseTm t))\n (sub-erase \u03c3\u22c6 (S.slift \u03c3) t))\nsub-erase \u03c3\u22c6 \u03c3 (t \u00b7 u) =\n cong\u2082 _\u00b7_ (sub-erase \u03c3\u22c6 \u03c3 t) (sub-erase \u03c3\u22c6 \u03c3 u)\nsub-erase \u03c3\u22c6 \u03c3 (con c) = refl\nsub-erase \u03c3\u22c6 \u03c3 (error A) = refl\nsub-erase \u03c3\u22c6 \u03c3 (builtin bn As ts) = cong\n (builtin bn)\n (subList-erase \u03c3\u22c6 \u03c3 ts)\nsub-erase \u03c3\u22c6 \u03c3 (wrap pat arg t) = sub-erase \u03c3\u22c6 \u03c3 t\nsub-erase \u03c3\u22c6 \u03c3 (unwrap t) = sub-erase \u03c3\u22c6 \u03c3 t\n\nerase-extend : \u2200{n}{w : Weird\u2115 n}\n \u2192 (u : ScopedTm w)\n \u2192 (\u03b1 : Fin (suc (len w)))\n \u2192 erase-Sub (S.ext ` u) \u03b1 \u2261 U.extend ` (eraseTm u) \u03b1 \nerase-extend u zero = refl\nerase-extend {w = w} u (suc \u03b1) = cong ` (eraseVar-backVar {w = w} \u03b1)\n\nlem[] : \u2200{n}{w : Weird\u2115 n}(t : ScopedTm (S w))(u : ScopedTm w) \u2192\n eraseTm (t S.[ u ]) \u2261 eraseTm t U.[ eraseTm u ]\nlem[] t u = trans\n (sym (sub-erase ` (S.ext ` u) t))\n (U.sub-cong (erase-extend u) (eraseTm t))\n\nlem[]\u22c6 : \u2200{n}{w : Weird\u2115 n}(t : ScopedTm (T w))(A : ScopedTy n) \u2192\n eraseTm (t S.[ A ]\u22c6) \u2261 eraseTm t\nlem[]\u22c6 {w = w} t A = trans\n (sym (sub-erase (S.ext\u22c6 ` A) (S.\u22c6ext `) t))\n (trans (U.sub-cong (cong ` \u2218 eraseVar-backVar {w = w})\n (eraseTm t))\n (sym (U.sub-id (eraseTm t))))\n\\end{code}\n","avg_line_length":35.3515151515,"max_line_length":128,"alphanum_fraction":0.556831819} +{"size":6219,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Type.BetaNBE.Soundness where\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Type.Equality\nopen import Type.RenamingSubstitution\nopen import Type.BetaNormal\nopen import Type.BetaNBE\n\nopen import Relation.Binary.PropositionalEquality\n renaming (subst to substEq)\n hiding ([_])\nopen import Function\nopen import Data.Sum\nopen import Data.Empty\nopen import Data.Product\nopen import Data.String\n\\end{code}\n\nThe Soundness Relation (SR) is a Kripke logical relation between types\nand their values. It is defined by induction on kinds. it says that a type\nis related to a value if when we reach ground kind (# or *) then the\ntype is beta-eta-equal to the result of reifying the value.\n\n\\begin{code}\nSR : \u2200{\u03a6} K \u2192 \u03a6 \u22a2\u22c6 K \u2192 Val \u03a6 K \u2192 Set\nSR * A v = A \u2261\u03b2 embNf v\nSR (K \u21d2 J) A (inj\u2081 n) = A \u2261\u03b2 embNe n\nSR (K \u21d2 J) A (inj\u2082 (x , f)) = \u03a3 (_ ,\u22c6 K \u22a2\u22c6 J) \u03bb B \u2192\n (A \u2261\u03b2 \u019b x B) -- this bit of indirection is needed as we have only \u03b2 not \u03b2\u03b7\n \u00d7\n \u2200{\u03a8}\n \u2192 (\u03c1 : Ren _ \u03a8)\n \u2192 {u : \u03a8 \u22a2\u22c6 K}\n \u2192 {v : Val \u03a8 K}\n \u2192 SR K u v\n -----------------------------------------------------\n \u2192 SR J (ren \u03c1 (\u019b x B) \u00b7 u) (renVal \u03c1 (inj\u2082 (x , f)) \u00b7V v)\n\\end{code}\n\n\\begin{code}\nreflectSR : \u2200{\u03a6 K}{A : \u03a6 \u22a2\u22c6 K}{n : \u03a6 \u22a2Ne\u22c6 K}\n \u2192 A \u2261\u03b2 embNe n\n ------------------\n \u2192 SR K A (reflect n)\nreflectSR {K = *} p = p\nreflectSR {K = K \u21d2 J} p = p\n\nreifySR : \u2200{\u03a6 K}{A : \u03a6 \u22a2\u22c6 K}{v : Val \u03a6 K}\n \u2192 SR K A v\n --------------------\n \u2192 A \u2261\u03b2 embNf (reify v)\nreifySR {K = *} p = p\nreifySR {K = K \u21d2 J} {v = inj\u2081 n} p = p\nreifySR {K = K \u21d2 J} {v = inj\u2082 (x , f)} (A' , p , q) =\n trans\u2261\u03b2 p (substEq (\u03bb B \u2192 \u019b x B \u2261\u03b2 \u019b x (embNf (reify (f S fresh))))\n (trans (sym (subst-ren A'))\n (trans (subst-cong (\u03bb { Z \u2192 refl\n ; (S x) \u2192 refl}) A')\n (subst-id A')))\n (\u019b\u2261\u03b2 (trans\u2261\u03b2 (sym\u2261\u03b2 (\u03b2\u2261\u03b2 _ _))\n (reifySR (q S (reflectSR (refl\u2261\u03b2 (` Z))))))))\n\\end{code}\n\nLifting SR from \u22a2\u22c6\/Val to Sub\/Env\n\n\\begin{code}\nSREnv : \u2200{\u03a6 \u03a8} \u2192 Sub \u03a6 \u03a8 \u2192 Env \u03a6 \u03a8 \u2192 Set\nSREnv {\u03a6} \u03c3 \u03b7 = \u2200{K}(\u03b1 : \u03a6 \u220b\u22c6 K) \u2192 SR K (\u03c3 \u03b1) (\u03b7 \u03b1)\n\\end{code}\n\nCons for SREnv\n\n\\begin{code}\nSR,,\u22c6 : \u2200{\u03a6 \u03a8}{\u03c3 : Sub \u03a6 \u03a8}{\u03b7 : Env \u03a6 \u03a8}\n \u2192 SREnv \u03c3 \u03b7\n \u2192 \u2200{K}{A : \u03a8 \u22a2\u22c6 K}{v : Val \u03a8 K}\n \u2192 SR K A v\n \u2192 SREnv (subst-cons \u03c3 A) (\u03b7 ,,\u22c6 v)\nSR,,\u22c6 p q Z = q\nSR,,\u22c6 p q (S \u03b1) = p \u03b1\n\\end{code}\n\nrenaming for SR\n\n\\begin{code}\nrenSR : \u2200{\u03a6 \u03a8}(\u03c1 : Ren \u03a6 \u03a8){K}{A : \u03a6 \u22a2\u22c6 K}{v : Val \u03a6 K}\n \u2192 SR K A v\n ---------------------------------\n \u2192 SR K (ren \u03c1 A) (renVal \u03c1 v)\nrenSR \u03c1 {*}{A}{n} p = \n substEq (ren \u03c1 A \u2261\u03b2_) (sym (ren-embNf \u03c1 n)) (ren\u2261\u03b2 \u03c1 p)\nrenSR \u03c1 {K \u21d2 J} {A} {inj\u2081 n} p rewrite ren-embNe \u03c1 n = ren\u2261\u03b2 \u03c1 p \nrenSR \u03c1 {K \u21d2 J} {A} {inj\u2082 (x , f)} (A' , p , q) =\n ren (ext \u03c1) A'\n ,\n ren\u2261\u03b2 \u03c1 p\n ,\n \u03bb \u03c1' {u}{v} r \u2192 substEq (\u03bb A \u2192 SR J (\u019b x A \u00b7 u) (f (\u03c1' \u2218 \u03c1) v))\n (trans (ren-cong ext-comp A') (ren-comp A'))\n (q (\u03c1' \u2218 \u03c1) r)\n\\end{code}\n\nExtending via exts is the same the same as weakening and cons on ` Z\n\n\\begin{code}\nexts-subst-cons : \u2200{\u03a6 \u03a8 K J}\n \u2192 (\u03c3 : Sub \u03a6 \u03a8)\n \u2192 (\u03b1 : \u03a6 ,\u22c6 J \u220b\u22c6 K)\n \u2192 exts \u03c3 \u03b1\u00a0\u2261 subst-cons (weaken \u2218 \u03c3) (` Z) \u03b1\nexts-subst-cons \u03c3 Z = refl\nexts-subst-cons \u03c3 (S _) = refl\n\\end{code}\n\nSREnv is closed under (pointwise) equality of environments\n\n\\begin{code}\nsubstSREnv : \u2200{\u03a6 \u03a8}{\u03c3 \u03c3' : Sub \u03a6 \u03a8}\n \u2192 (\u2200{K}(\u03b1 : \u03a6 \u220b\u22c6 K) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 {\u03b7 : Env \u03a6 \u03a8}\n \u2192 SREnv \u03c3 \u03b7\n -------------------------------\n \u2192 SREnv \u03c3' \u03b7\nsubstSREnv p q \u03b1 rewrite sym (p \u03b1) = q \u03b1\n\\end{code}\n\nSREnv is closed under exts\/extending the env\n(note: would this be cleaner if we used exte?)\n\n\\begin{code}\nSRweak : \u2200{\u03a6 \u03a8}{\u03c3 : Sub \u03a6 \u03a8}{\u03b7 : Env \u03a6 \u03a8}\n \u2192 SREnv \u03c3 \u03b7\n \u2192 \u2200 {K}\n -------------------------------------------------------\n \u2192 SREnv (exts \u03c3) ((renVal S \u2218 \u03b7) ,,\u22c6 fresh {\u03c3 = K})\nSRweak p = substSREnv (sym \u2218 exts-subst-cons _)\n (SR,,\u22c6 (renSR S \u2218 p) (reflectSR (refl\u2261\u03b2 (` Z)))) \n\\end{code}\n\nSR is closed under \u2261\u03b2\n\n\\begin{code}\nsubstSR : \u2200{\u03a6 K}{A A' : \u03a6 \u22a2\u22c6 K}\n \u2192 A' \u2261\u03b2 A\n \u2192 {v : Val \u03a6 K}\n \u2192 SR K A v\n ---------------------------\n \u2192 SR K A' v\nsubstSR {K = *} p q = trans\u2261\u03b2 p q\nsubstSR {K = K \u21d2 J} p {inj\u2081 n} q = trans\u2261\u03b2 p q\nsubstSR {K = K \u21d2 J} p {inj\u2082 f} (A' , q , r) = _ , trans\u2261\u03b2 p q , r\n\\end{code}\n\nSR is closed under \u00b7V\n\n\\begin{code}\nSRApp : \u2200{\u03a6 K J}\n \u2192 {A : \u03a6 \u22a2\u22c6 (K \u21d2 J)}\n \u2192 {f : Val \u03a6 (K \u21d2 J)}\n \u2192 SR (K \u21d2 J) A f\n \u2192 {u : \u03a6 \u22a2\u22c6 K}\n \u2192 {v : Val \u03a6 K}\n \u2192 SR K u v\n ---------------------\n \u2192 SR J (A \u00b7 u) (f \u00b7V v)\nSRApp {f = inj\u2081 n} p q = reflectSR (\u00b7\u2261\u03b2 (reflectSR p) (reifySR q))\nSRApp {f = inj\u2082 (x , f)} (A' , p , q) r =\n substSR (\u00b7\u2261\u03b2 (substEq\n (\u03bb B \u2192 _ \u2261\u03b2 \u019b x B)\n (trans (sym (ren-id A')) (ren-cong (sym \u2218 ext-id) A'))\n p)\n (refl\u2261\u03b2 _))\n (q id r)\n\\end{code}\n\nFundamental Theorem of Logical Relations for SR\n\n\\begin{code}\nevalSR : \u2200{\u03a6 \u03a8 K}(A : \u03a6 \u22a2\u22c6 K){\u03c3 : Sub \u03a6 \u03a8}{\u03b7 : Env \u03a6 \u03a8}\n \u2192 SREnv \u03c3 \u03b7\n \u2192 SR K (subst \u03c3 A) (eval A \u03b7)\nevalSR (` \u03b1) p = p \u03b1\nevalSR (\u03a0 x B) p = \u03a0\u2261\u03b2 (evalSR B (SRweak p))\nevalSR (A \u21d2 B) p = \u21d2\u2261\u03b2 (evalSR A p) (evalSR B p)\nevalSR (\u019b x B) {\u03c3}{\u03b7} p =\n subst (exts \u03c3) B\n ,\n refl\u2261\u03b2 _\n ,\n \u03bb \u03c1 {u}{v} q \u2192 substSR\n (\u03b2\u2261\u03b2 _ _)\n (substEq (\u03bb A \u2192 SR _ A (eval B ((renVal \u03c1 \u2218 \u03b7) ,,\u22c6 v)))\n (trans (trans (subst-cong (\u03bb\n { Z \u2192 refl\n ; (S x) \u2192 trans (trans (sym (subst-id (ren \u03c1 (\u03c3 x))))\n (sym (subst-ren (\u03c3 x))))\n (subst-ren (\u03c3 x))}) B)\n (subst-comp B))\n (subst-ren (subst (exts \u03c3) B)))\n (evalSR B (SR,,\u22c6 (renSR \u03c1 \u2218 p) q)) )\nevalSR (A \u00b7 B) p = SRApp (evalSR A p) (evalSR B p)\nevalSR \u03bc1 p = refl\u2261\u03b2 _\nevalSR (con tcn) p = refl\u2261\u03b2 _\n\\end{code}\n\nIdentity SREnv\n\n\\begin{code}\nidSR : \u2200{\u03a6} \u2192 SREnv ` (idEnv \u03a6)\nidSR = reflectSR \u2218 refl\u2261\u03b2 \u2218 `\n\\end{code}\n\nSoundness Result\n\n\\begin{code}\nsoundness : \u2200 {\u03a6 J} \u2192 (A : \u03a6 \u22a2\u22c6 J) \u2192 A \u2261\u03b2 embNf (nf A)\nsoundness A = substEq (_\u2261\u03b2 embNf (nf A)) (subst-id A) (reifySR (evalSR A idSR))\n\\end{code}\n","avg_line_length":28.1402714932,"max_line_length":80,"alphanum_fraction":0.4624537707} +{"size":29857,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Lists: Lists and higher-order functions\"\nlayout : page\nprev : \/Decidable\/\npermalink : \/Lists\/\nnext : \/Lambda\/\n---\n\n\\begin{code}\nmodule plfa.Lists where\n\\end{code}\n\nThis chapter discusses the list data type. It gives further examples\nof many of the techniques we have developed so far, and provides\nexamples of polymorphic types and higher-order functions.\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Bool using (Bool; true; false; T; _\u2227_; _\u2228_; not)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_; _\u2238_; _\u2264_; s\u2264s; z\u2264n)\nopen import Data.Nat.Properties using\n (+-assoc; +-identity\u02e1; +-identity\u02b3; *-assoc; *-identity\u02e1; *-identity\u02b3)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Data.Product using (_\u00d7_; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Function using (_\u2218_)\nopen import Level using (Level)\nopen import plfa.Isomorphism using (_\u2243_; _\u21d4_)\n\\end{code}\n\n\n## Lists\n\nLists are defined in Agda as follows:\n\\begin{code}\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\ninfixr 5 _\u2237_\n\\end{code}\nLet's unpack this definition. If `A` is a set, then `List A` is a set.\nThe next two lines tell us that `[]` (pronounced _nil_) is a list of\ntype `A` (often called the _empty_ list), and that `_\u2237_` (pronounced\n_cons_, short for _constructor_) takes a value of type `A` and a value\nof type `List A` and returns a value of type `List A`. Operator `_\u2237_`\nhas precedence level 5 and associates to the right.\n\nFor example,\n\\begin{code}\n_ : List \u2115\n_ = 0 \u2237 1 \u2237 2 \u2237 []\n\\end{code}\ndenotes the list of the first three natural numbers. Since `_::_`\nassociates to the right, the term parses as `0 \u2237 (1 \u2237 (2 \u2237 []))`.\nHere `0` is the first element of the list, called the _head_,\nand `1 \u2237 (2 \u2237 [])` is a list of the remaining elements, called the\n_tail_. A list is a strange beast: it has a head and a tail,\nnothing in between, and the tail is itself another list!\n\nAs we've seen, parameterised types can be translated to\nindexed types. The definition above is equivalent to the following:\n\\begin{code}\ndata List\u2032 : Set \u2192 Set where\n []\u2032 : \u2200 {A : Set} \u2192 List\u2032 A\n _\u2237\u2032_ : \u2200 {A : Set} \u2192 A \u2192 List\u2032 A \u2192 List\u2032 A\n\\end{code}\nEach constructor takes the parameter as an implicit argument.\nThus, our example list could also be written\n\\begin{code}\n_ : List \u2115\n_ = _\u2237_ {\u2115} 0 (_\u2237_ {\u2115} 1 (_\u2237_ {\u2115} 2 ([] {\u2115})))\n\\end{code}\nwhere here we have provided the implicit parameters explicitly.\n\nIncluding the pragma\n\n {-# BUILTIN LIST List #-}\n\ntells Agda that the type `List` corresponds to the Haskell type\nlist, and the constructors `[]` and `_\u2237_` correspond to nil and\ncons respectively, allowing a more efficient representation of lists.\n\n\n## List syntax\n\nWe can write lists more conveniently by introducing the following definitions:\n\\begin{code}\npattern [_] z = z \u2237 []\npattern [_,_] y z = y \u2237 z \u2237 []\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_] w x y z = w \u2237 x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_,_] v w x y z = v \u2237 w \u2237 x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_,_,_] u v w x y z = u \u2237 v \u2237 w \u2237 x \u2237 y \u2237 z \u2237 []\n\\end{code}\nThis is our first use of pattern declarations. For instance,\nthe third line tells us that `[ x , y , z ]` is equivalent to\n`x \u2237 y \u2237 z \u2237 []`, and permits the former to appear either in\na pattern on the left-hand side of an equation, or a term\non the right-hand side of an equation.\n\n\n## Append\n\nOur first function on lists is written `_++_` and pronounced\n_append_:\n\n\\begin{code}\ninfixr 5 _++_\n\n_++_ : \u2200 {A : Set} \u2192 List A \u2192 List A \u2192 List A\n[] ++ ys = ys\n(x \u2237 xs) ++ ys = x \u2237 (xs ++ ys)\n\\end{code}\nThe type `A` is an implicit argument to append, making it a\n_polymorphic_ function (one that can be used at many types). The\nempty list appended to another list yields the other list. A\nnon-empty list appended to another list yields a list with head the\nsame as the head of the first list and tail the same as the tail of\nthe first list appended to the second list.\n\nHere is an example, showing how to compute the result\nof appending two lists:\n\\begin{code}\n_ : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] \u2261 [ 0 , 1 , 2 , 3 , 4 ]\n_ =\n begin\n 0 \u2237 1 \u2237 2 \u2237 [] ++ 3 \u2237 4 \u2237 []\n \u2261\u27e8\u27e9\n 0 \u2237 (1 \u2237 2 \u2237 [] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 (2 \u2237 [] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 2 \u2237 ([] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 2 \u2237 3 \u2237 4 \u2237 []\n \u220e\n\\end{code}\nAppending two lists requires time linear in the\nnumber of elements in the first list.\n\n\n## Reasoning about append\n\nWe can reason about lists in much the same way that we reason\nabout numbers. Here is the proof that append is associative:\n\\begin{code}\n++-assoc : \u2200 {A : Set} (xs ys zs : List A)\n \u2192 (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc [] ys zs =\n begin\n ([] ++ ys) ++ zs\n \u2261\u27e8\u27e9\n ys ++ zs\n \u2261\u27e8\u27e9\n [] ++ (ys ++ zs)\n \u220e\n++-assoc (x \u2237 xs) ys zs =\n begin\n (x \u2237 xs ++ ys) ++ zs\n \u2261\u27e8\u27e9\n x \u2237 ((xs ++ ys) ++ zs)\n \u2261\u27e8 cong (x \u2237_) (++-assoc xs ys zs) \u27e9\n x \u2237 (xs ++ (ys ++ zs))\n \u2261\u27e8\u27e9\n x \u2237 xs ++ (ys ++ zs)\n \u220e\n\\end{code}\nThe proof is by induction on the first argument. The base case instantiates\nto `[]`, and follows by straightforward computation.\nThe inductive case instantiates to `x \u2237 xs`,\nand follows by straightforward computation combined with the\ninductive hypothesis. As usual, the inductive hypothesis is indicated by a recursive\ninvocation of the proof, in this case `++-assoc xs ys zs`.\n\nRecall that Agda supports [sections][plfa.Induction#sections].\nApplying `cong (x \u2237_)` promotes the inductive hypothesis\n\n xs ++ (ys ++ zs) \u2261 (xs ++ ys) ++ zs\n\nto the equality\n\n x \u2237 (xs ++ (ys ++ zs)) \u2261 x \u2237 ((xs ++ ys) ++ zs)\n\nwhich is needed in the proof.\n\nIt is also easy to show that `[]` is a left and right identity for `_++_`.\nThat it is a left identity is immediate from the definition:\n\\begin{code}\n++-identity\u02e1 : \u2200 {A : Set} (xs : List A) \u2192 [] ++ xs \u2261 xs\n++-identity\u02e1 xs =\n begin\n [] ++ xs\n \u2261\u27e8\u27e9\n xs\n \u220e\n\\end{code}\nThat it is a right identity follows by simple induction:\n\\begin{code}\n++-identity\u02b3 : \u2200 {A : Set} (xs : List A) \u2192 xs ++ [] \u2261 xs\n++-identity\u02b3 [] =\n begin\n [] ++ []\n \u2261\u27e8\u27e9\n []\n \u220e\n++-identity\u02b3 (x \u2237 xs) =\n begin\n (x \u2237 xs) ++ []\n \u2261\u27e8\u27e9\n x \u2237 (xs ++ [])\n \u2261\u27e8 cong (x \u2237_) (++-identity\u02b3 xs) \u27e9\n x \u2237 xs\n \u220e\n\\end{code}\nAs we will see later,\nthese three properties establish that `_++_` and `[]` form\na _monoid_ over lists.\n\n## Length\n\nOur next function finds the length of a list:\n\\begin{code}\nlength : \u2200 {A : Set} \u2192 List A \u2192 \u2115\nlength [] = zero\nlength (x \u2237 xs) = suc (length xs)\n\\end{code}\nAgain, it takes an implicit parameter `A`.\nThe length of the empty list is zero.\nThe length of a non-empty list\nis one greater than the length of the tail of the list.\n\nHere is an example showing how to compute the length of a list:\n\\begin{code}\n_ : length [ 0 , 1 , 2 ] \u2261 3\n_ =\n begin\n length (0 \u2237 1 \u2237 2\u00a0\u2237 [])\n \u2261\u27e8\u27e9\n suc (length (1 \u2237 2 \u2237 []))\n \u2261\u27e8\u27e9\n suc (suc (length (2 \u2237 [])))\n \u2261\u27e8\u27e9\n suc (suc (suc (length {\u2115} [])))\n \u2261\u27e8\u27e9\n suc (suc (suc zero))\n \u220e\n\\end{code}\nComputing the length of a list requires time\nlinear in the number of elements in the list.\n\nIn the second-to-last line, we cannot write simply `length []` but\nmust instead write `length {\u2115} []`. Since `[]` has no elements, Agda\nhas insufficient information to infer the implicit parameter.\n\n\n## Reasoning about length\n\nThe length of one list appended to another is the\nsum of the lengths of the lists:\n\\begin{code}\nlength-++ : \u2200 {A : Set} (xs ys : List A)\n \u2192 length (xs ++ ys) \u2261 length xs + length ys\nlength-++ {A} [] ys =\n begin\n length ([] ++ ys)\n \u2261\u27e8\u27e9\n length ys\n \u2261\u27e8\u27e9\n length {A} [] + length ys\n \u220e\nlength-++ (x \u2237 xs) ys =\n begin\n length ((x \u2237 xs) ++ ys)\n \u2261\u27e8\u27e9\n suc (length (xs ++ ys))\n \u2261\u27e8 cong suc (length-++ xs ys) \u27e9\n suc (length xs + length ys)\n \u2261\u27e8\u27e9\n length (x \u2237 xs) + length ys\n \u220e\n\\end{code}\nThe proof is by induction on the first arugment. The base case\ninstantiates to `[]`, and follows by straightforward computation. As\nbefore, Agda cannot infer the implicit type parameter to `length`, and\nit must be given explicitly. The inductive case instantiates to\n`x \u2237 xs`, and follows by straightforward computation combined with the\ninductive hypothesis. As usual, the inductive hypothesis is indicated\nby a recursive invocation of the proof, in this case `length-++ xs ys`,\nand it is promoted by the congruence `cong suc`.\n\n\n## Reverse\n\nUsing append, it is easy to formulate a function to reverse a list:\n\\begin{code}\nreverse : \u2200 {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ [ x ]\n\\end{code}\nThe reverse of the empty list is the empty list.\nThe reverse of a non-empty list\nis the reverse of its tail appended to a unit list\ncontaining its head.\n\nHere is an example showing how to reverse a list:\n\\begin{code}\n_ : reverse [ 0 , 1 , 2 ] \u2261 [ 2 , 1 , 0 ]\n_ =\n begin\n reverse (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n reverse (1 \u2237 2 \u2237 []) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (reverse (2 \u2237 []) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n ((reverse [] ++ [ 2 ]) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (([] ++ [ 2 ]) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (([] ++ 2 \u2237 []) ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n (2 \u2237 [] ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n 2 \u2237 ([] ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n (2 \u2237 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n 2 \u2237 (1 \u2237 [] ++ 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 ([] ++ 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 0 \u2237 []\n \u2261\u27e8\u27e9\n [ 2 , 1 , 0 ]\n \u220e\n\\end{code}\nReversing a list in this way takes time _quadratic_ in the length of\nthe list. This is because reverse ends up appending lists of lengths\n`1`, `2`, up to `n - 1`, where `n` is the length of the list being\nreversed, append takes time linear in the length of the first\nlist, and the sum of the numbers up to `n - 1` is `n * (n - 1) \/ 2`.\n(We will validate that last fact in an exercise later in this chapter.)\n\n#### Exercise `reverse-++-commute` (recommended)\n\nShow that the reverse of one list appended to another is the\nreverse of the second appended to the reverse of the first:\n\\begin{code}\npostulate\n reverse-++-commute : \u2200 {A : Set} {xs ys : List A}\n \u2192 reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\n\\end{code}\n\n#### Exercise `reverse-involutive` (recommended)\n\nA function is an _involution_ if when applied twice it acts\nas the identity function. Show that reverse is an involution:\n\\begin{code}\npostulate\n reverse-involutive : \u2200 {A : Set} {xs : List A}\n \u2192 reverse (reverse xs) \u2261 xs\n\\end{code}\n\n\n## Faster reverse\n\nThe definition above, while easy to reason about, is less efficient than\none might expect since it takes time quadratic in the length of the list.\nThe idea is that we generalise reverse to take an additional argument:\n\\begin{code}\nshunt : \u2200 {A : Set} \u2192 List A \u2192 List A \u2192 List A\nshunt [] ys = ys\nshunt (x \u2237 xs) ys = shunt xs (x \u2237 ys)\n\\end{code}\nThe definition is by recursion on the first argument. The second argument\nactually becomes _larger_, but this is not a problem because the argument\non which we recurse becomes _smaller_.\n\nShunt is related to reverse as follows:\n\\begin{code}\nshunt-reverse : \u2200 {A : Set} (xs ys : List A)\n \u2192 shunt xs ys \u2261 reverse xs ++ ys\nshunt-reverse [] ys =\n begin\n shunt [] ys\n \u2261\u27e8\u27e9\n ys\n \u2261\u27e8\u27e9\n reverse [] ++ ys\n \u220e\nshunt-reverse (x \u2237 xs) ys =\n begin\n shunt (x \u2237 xs) ys\n \u2261\u27e8\u27e9\n shunt xs (x \u2237 ys)\n \u2261\u27e8 shunt-reverse xs (x \u2237 ys) \u27e9\n reverse xs ++ (x \u2237 ys)\n \u2261\u27e8\u27e9\n reverse xs ++ ([ x ] ++ ys)\n \u2261\u27e8 sym (++-assoc (reverse xs) [ x ] ys) \u27e9\n (reverse xs ++ [ x ]) ++ ys\n \u2261\u27e8\u27e9\n reverse (x \u2237 xs) ++ ys\n \u220e\n\\end{code}\nThe proof is by induction on the first argument.\nThe base case instantiates to `[]`, and follows by straightforward computation.\nThe inductive case instantiates to `x \u2237 xs` and follows by the inductive\nhypothesis and associativity of append. When we invoke the inductive hypothesis,\nthe second argument actually becomes *larger*, but this is not a problem because\nthe argument on which we induct becomes *smaller*.\n\nGeneralising on an auxiliary argument, which becomes larger as the argument on\nwhich we recurse or induct becomes smaller, is a common trick. It belongs in\nyour quiver of arrows, ready to slay the right problem.\n\nHaving defined shunt be generalisation, it is now easy to respecialise to\ngive a more efficient definition of reverse:\n\\begin{code}\nreverse\u2032 : \u2200 {A : Set} \u2192 List A \u2192 List A\nreverse\u2032 xs = shunt xs []\n\\end{code}\n\nGiven our previous lemma, it is straightforward to show\nthe two definitions equivalent:\n\\begin{code}\nreverses : \u2200 {A : Set} (xs : List A)\n \u2192 reverse\u2032 xs \u2261 reverse xs\nreverses xs =\n begin\n reverse\u2032 xs\n \u2261\u27e8\u27e9\n shunt xs []\n \u2261\u27e8 shunt-reverse xs [] \u27e9\n reverse xs ++ []\n \u2261\u27e8 ++-identity\u02b3 (reverse xs) \u27e9 \n reverse xs\n \u220e\n\\end{code}\n\nHere is an example showing fast reverse of the list `[ 0 , 1 , 2 ]`:\n\\begin{code}\n_ : reverse\u2032 [ 0 , 1 , 2 ] \u2261 [ 2 , 1 , 0 ]\n_ =\n begin\n reverse\u2032 (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n shunt (0 \u2237 1 \u2237 2 \u2237 []) []\n \u2261\u27e8\u27e9\n shunt (1 \u2237 2 \u2237 []) (0 \u2237 [])\n \u2261\u27e8\u27e9\n shunt (2 \u2237 []) (1 \u2237 0 \u2237 [])\n \u2261\u27e8\u27e9\n shunt [] (2 \u2237 1 \u2237 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 0 \u2237 []\n \u220e\n\\end{code}\nNow the time to reverse a list is linear in the length of the list.\n\n## Map {#Map}\n\nMap applies a function to every element of a list to generate a corresponding list.\nMap is an example of a _higher-order function_, one which takes a function as an\nargument and returns a function as a result:\n\\begin{code}\nmap : \u2200 {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\\end{code}\nMap of the empty list is the empty list.\nMap of a non-empty list yields a list\nwith head the same as the function applied to the head of the given list,\nand tail the same as map of the function applied to the tail of the given list.\n\nHere is an example showing how to use map to increment every element of a list:\n\\begin{code}\n_ : map suc [ 0 , 1 , 2 ] \u2261 [ 1 , 2 , 3 ]\n_ =\n begin\n map suc (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 map suc (1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 map suc (2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 suc 2 \u2237 map suc []\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 suc 2 \u2237 []\n \u2261\u27e8\u27e9\n 1 \u2237 2 \u2237 3 \u2237 []\n \u220e\n\\end{code}\nMap requires time linear in the length of the list.\n\nIt is often convenient to exploit currying by applying\nmap to a function to yield a new function, and at a later\npoint applying the resulting function:\n\\begin{code}\nsucs : List \u2115 \u2192 List \u2115\nsucs = map suc\n\n_ : sucs [ 0 , 1 , 2 ] \u2261 [ 1 , 2 , 3 ]\n_ =\n begin\n sucs [ 0 , 1 , 2 ]\n \u2261\u27e8\u27e9\n map suc [ 0 , 1 , 2 ]\n \u2261\u27e8\u27e9\n [ 1 , 2 , 3 ]\n \u220e\n\\end{code}\n\nAny type that is parameterised on another type, such as lists, has a\ncorresponding map, which accepts a function and returns a function\nfrom the type parameterised on the domain of the function to the type\nparameterised on the range of the function. Further, a type that is\nparameterised on _n_ types will have a map that is parameterised on\n_n_ functions.\n\n\n#### Exercise `map-compose`\n\nProve that the map of a composition is equal to the composition of two maps:\n\\begin{code}\npostulate\n map-compose : \u2200 {A B C : Set} {f : A \u2192 B} {g : B \u2192 C}\n \u2192 map (g \u2218 f) \u2261 map g \u2218 map f\n\\end{code}\nThe last step of the proof requires extensionality.\n\n#### Exercise `map-++-commute`\n\nProve the following relationship between map and append:\n\\begin{code}\npostulate\n map-++-commute : \u2200 {A B : Set} {f : A \u2192 B} {xs ys : List A}\n \u2192 map f (xs ++ ys) \u2261 map f xs ++ map f ys\n\\end{code}\n\n#### Exercise `map-Tree`\n\nDefine a type of trees with leaves of type `A` and internal\nnodes of type `B`:\n\\begin{code}\ndata Tree (A B : Set) : Set where\n leaf : A \u2192 Tree A B\n node : Tree A B \u2192 B \u2192 Tree A B \u2192 Tree A B\n\\end{code}\nDefine a suitable map operator over trees:\n\\begin{code}\npostulate\n map-Tree : \u2200 {A B C D : Set}\n \u2192 (A \u2192 C) \u2192 (B \u2192 D) \u2192 Tree A B \u2192 Tree C D\n\\end{code}\n\n\n## Fold {#Fold}\n\nFold takes an operator and a value, and uses the operator to combine\neach of the elements of the list, taking the given value as the result\nfor the empty list:\n\\begin{code}\nfoldr : \u2200 {A B : Set} \u2192 (A \u2192 B \u2192 B) \u2192 B \u2192 List A \u2192 B\nfoldr _\u2297_ e [] = e\nfoldr _\u2297_ e (x \u2237 xs) = x \u2297 foldr _\u2297_ e xs\n\\end{code}\nFold of the empty list is the given value.\nFold of a non-empty list uses the operator to combine\nthe head of the list and the fold of the tail of the list.\n\nHere is an example showing how to use fold to find the sum of a list:\n\\begin{code}\n_ : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] \u2261 10\n_ =\n begin\n foldr _+_ 0 (1 \u2237 2 \u2237 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 1 + foldr _+_ 0 (2 \u2237 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 1 + (2 + foldr _+_ 0 (3 \u2237 4 \u2237 []))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + foldr _+_ 0 (4 \u2237 [])))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + (4 + foldr _+_ 0 [])))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + (4 + 0)))\n \u220e\n\\end{code}\nFold requires time linear in the length of the list.\n\nIt is often convenient to exploit currying by applying\nfold to an operator and a value to yield a new function,\nand at a later point applying the resulting function:\n\\begin{code}\nsum : List \u2115 \u2192 \u2115\nsum = foldr _+_ 0\n\n_ : sum [ 1 , 2 , 3 , 4 ] \u2261 10\n_ =\n begin\n sum [ 1 , 2 , 3 , 4 ]\n \u2261\u27e8\u27e9\n foldr _+_ 0 [ 1 , 2 , 3 , 4 ]\n \u2261\u27e8\u27e9\n 10\n \u220e\n\\end{code}\n\nJust as the list type has two constructors, `[]` and `_\u2237_`,\nso the fold function takes two arguments, `e` and `_\u2297_`\n(in addition to the list argument).\nIn general, a data type with _n_ constructors will have\na corresponding fold function that takes _n_ arguments.\n\n#### Exercise `product` (recommended)\n\nUse fold to define a function to find the product of a list of numbers.\nFor example:\n\n product [ 1 , 2 , 3 , 4 ] \u2261 24\n\n#### Exercise `foldr-++` (recommended)\n\nShow that fold and append are related as follows:\n\\begin{code}\npostulate\n foldr-++ : \u2200 {A B : Set} (_\u2297_ : A \u2192 B \u2192 B) (e : B) (xs ys : List A) \u2192\n foldr _\u2297_ e (xs ++ ys) \u2261 foldr _\u2297_ (foldr _\u2297_ e ys) xs\n\\end{code}\n\n\n#### Exercise `map-is-foldr`\n\nShow that map can be defined using fold:\n\\begin{code}\npostulate\n map-is-foldr : \u2200 {A B : Set} {f : A \u2192 B} \u2192\n map f \u2261 foldr (\u03bb x xs \u2192 f x \u2237 xs) []\n\\end{code}\nThis requires extensionality.\n\n#### Exercise `fold-Tree`\n\nDefine a suitable fold function for the type of trees given earlier:\n\\begin{code}\npostulate\n fold-Tree : \u2200 {A B C : Set}\n \u2192 (A \u2192 C) \u2192 (C \u2192 B \u2192 C \u2192 C) \u2192 Tree A B \u2192 C\n\\end{code}\n\n#### Exercise `map-is-fold-Tree`\n\nDemonstrate an anologue of `map-is-foldr` for the type of trees.\n\n#### Exercise `sum-downFrom` (stretch)\n\nDefine a function that counts down as follows:\n\\begin{code}\ndownFrom : \u2115 \u2192 List \u2115\ndownFrom zero = []\ndownFrom (suc n) = n \u2237 downFrom n\n\\end{code}\nFor example:\n\\begin{code}\n_ : downFrom 3 \u2261 [ 2 , 1 , 0 ]\n_ = refl\n\\end{code}\nProve that the sum of the numbers `(n - 1) + \u22ef + 0` is\nequal to `n * (n \u2238 1) \/ 2`:\n\\begin{code}\npostulate\n sum-downFrom : \u2200 (n : \u2115)\n \u2192 sum (downFrom n) * 2 \u2261 n * (n \u2238 1)\n\\end{code}\n\n\n## Monoids\n\nTypically when we use a fold the operator is associative and the\nvalue is a left and right identity for the value, meaning that the\noperator and the value form a _monoid_.\n\nWe can define a monoid as a suitable record type:\n\\begin{code}\nrecord IsMonoid {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) : Set where\n field\n assoc : \u2200 (x y z : A) \u2192 (x \u2297 y) \u2297 z \u2261 x \u2297 (y \u2297 z)\n identity\u02e1 : \u2200 (x : A) \u2192 e \u2297 x \u2261 x\n identity\u02b3 : \u2200 (x : A) \u2192 x \u2297 e \u2261 x\n\nopen IsMonoid\n\\end{code}\n\nAs examples, sum and zero, multiplication and one, and append and the empty\nlist, are all examples of monoids:\n\\begin{code}\n+-monoid : IsMonoid _+_ 0\n+-monoid =\n record\n { assoc = +-assoc\n ; identity\u02e1 = +-identity\u02e1\n ; identity\u02b3 = +-identity\u02b3\n }\n\n*-monoid : IsMonoid _*_ 1\n*-monoid =\n record\n { assoc = *-assoc\n ; identity\u02e1 = *-identity\u02e1\n ; identity\u02b3 = *-identity\u02b3\n }\n\n++-monoid : \u2200 {A : Set} \u2192 IsMonoid {List A} _++_ []\n++-monoid =\n record\n { assoc = ++-assoc\n ; identity\u02e1 = ++-identity\u02e1\n ; identity\u02b3 = ++-identity\u02b3\n }\n\\end{code}\n\nIf `_\u2297_` and `e` form a monoid, then we can re-express fold on the\nsame operator and an arbitrary value:\n\\begin{code}\nfoldr-monoid : \u2200 {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) \u2192 IsMonoid _\u2297_ e \u2192\n \u2200 (xs : List A) (y : A) \u2192 foldr _\u2297_ y xs \u2261 foldr _\u2297_ e xs \u2297 y\nfoldr-monoid _\u2297_ e \u2297-monoid [] y =\n begin\n foldr _\u2297_ y []\n \u2261\u27e8\u27e9\n y\n \u2261\u27e8 sym (identity\u02e1 \u2297-monoid y) \u27e9\n (e \u2297 y)\n \u2261\u27e8\u27e9\n foldr _\u2297_ e [] \u2297 y\n \u220e\nfoldr-monoid _\u2297_ e \u2297-monoid (x \u2237 xs) y =\n begin\n foldr _\u2297_ y (x \u2237 xs)\n \u2261\u27e8\u27e9\n x \u2297 (foldr _\u2297_ y xs)\n \u2261\u27e8 cong (x \u2297_) (foldr-monoid _\u2297_ e \u2297-monoid xs y) \u27e9\n x \u2297 (foldr _\u2297_ e xs \u2297 y)\n \u2261\u27e8 sym (assoc \u2297-monoid x (foldr _\u2297_ e xs) y) \u27e9\n (x \u2297 foldr _\u2297_ e xs) \u2297 y\n \u2261\u27e8\u27e9\n foldr _\u2297_ e (x \u2237 xs) \u2297 y\n \u220e\n\\end{code}\n\nAs a consequence, using a previous exercise, we have the following:\n\\begin{code}\nfoldr-monoid-++ : \u2200 {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) \u2192 IsMonoid _\u2297_ e \u2192\n \u2200 (xs ys : List A) \u2192 foldr _\u2297_ e (xs ++ ys) \u2261 foldr _\u2297_ e xs \u2297 foldr _\u2297_ e ys\nfoldr-monoid-++ _\u2297_ e monoid-\u2297 xs ys =\n begin\n foldr _\u2297_ e (xs ++ ys)\n \u2261\u27e8 foldr-++ _\u2297_ e xs ys \u27e9\n foldr _\u2297_ (foldr _\u2297_ e ys) xs\n \u2261\u27e8 foldr-monoid _\u2297_ e monoid-\u2297 xs (foldr _\u2297_ e ys) \u27e9\n foldr _\u2297_ e xs \u2297 foldr _\u2297_ e ys\n \u220e\n\\end{code}\n\n#### Exercise `foldl`\n\nDefine a function `foldl` which is analogous to `foldr`, but where\noperations associate to the left rather than the right. For example:\n\n foldr _\u2297_ e [ x , y , z ] = x \u2297 (y \u2297 (z \u2297 e))\n foldl _\u2297_ e [ x , y , z ] = ((e \u2297 x) \u2297 y) \u2297 z\n\n\n#### Exercise `foldr-monoid-foldl`\n\nShow that if `_\u2297_` and `e` form a monoid, then `foldr _\u2297_ e` and\n`foldl _\u2297_ e` always compute the same result.\n\n\n## All {#All}\n\nWe can also define predicates over lists. Two of the most important\nare `All` and `Any`.\n\nPredicate `All P` holds if predicate `P` is satisfied by every element of a list:\n\\begin{code}\ndata All {A : Set} (P : A \u2192 Set) : List A \u2192 Set where\n [] : All P []\n _\u2237_ : \u2200 {x : A} {xs : List A} \u2192 P x \u2192 All P xs \u2192 All P (x \u2237 xs)\n\\end{code}\nThe type has two constructors, reusing the names of the same constructors for lists.\nThe first asserts that `P` holds for every element of the empty list.\nThe second asserts that if `P` holds of the head of a list and for every\nelement of the tail of a list, then `P` holds for every element of the list.\nAgda uses types to disambiguate whether the constructor is building\na list or evidence that `All P` holds.\n\nFor example, `All (_\u2264 2)` holds of a list where every element is less\nthan or equal to two. Recall that `z\u2264n` proves `zero \u2264 n` for any\n`n`, and that if `m\u2264n` proves `m \u2264 n` then `s\u2264s m\u2264n` proves `suc m \u2264\nsuc n`, for any `m` and `n`:\n\\begin{code}\n_ : All (_\u2264 2) [ 0 , 1 , 2 ]\n_ = z\u2264n \u2237 s\u2264s z\u2264n \u2237 s\u2264s (s\u2264s z\u2264n) \u2237 []\n\\end{code}\nHere `_\u2237_` and `[]` are the constructors of `All P` rather than of `List A`.\nThe three items are proofs of `0 \u2264 2`, `1 \u2264 2`, and `2 \u2264 2`, respectively.\n\n(One might wonder whether a pattern such as `[_,_,_]` can be used to\nconstruct values of type `All` as well as type `List`, since both use\nthe same constructors. Indeed it can, so long as both types are in\nscope when the pattern is declared. That's not the case here, since\n`List` is defined before `[_,_,_]`, but `All` is defined later.)\n\n\n## Any\n\nPredicate `Any P` holds if predicate `P` is satisfied by some element of a list:\n\\begin{code}\ndata Any {A : Set} (P : A \u2192 Set) : List A \u2192 Set where\n here : \u2200 {x : A} {xs : List A} \u2192 P x \u2192 Any P (x \u2237 xs)\n there : \u2200 {x : A} {xs : List A} \u2192 Any P xs \u2192 Any P (x \u2237 xs)\n\\end{code}\nThe first constructor provides evidence that the head of the list\nsatisfies `P`, while the second provides evidence that some element of\nthe tail of the list satisfies `P`. For example, we can define list\nmembership as follows:\n\\begin{code}\ninfix 4 _\u2208_ _\u2209_\n\n_\u2208_ : \u2200 {A : Set} (x : A) (xs : List A) \u2192 Set\nx \u2208 xs = Any (x \u2261_) xs\n\n_\u2209_ : \u2200 {A : Set} (x : A) (xs : List A) \u2192 Set\nx \u2209 xs = \u00ac (x \u2208 xs)\n\\end{code}\nFor example, zero is an element of the list `[ 0 , 1 , 0 , 2 ]`. Indeed, we can demonstrate\nthis fact in two different ways, corresponding to the two different\noccurrences of zero in the list, as the first element and as the third element:\n\\begin{code}\n_ : 0 \u2208 [ 0 , 1 , 0 , 2 ]\n_ = here refl\n\n_ : 0 \u2208 [ 0 , 1 , 0 , 2 ]\n_ = there (there (here refl))\n\\end{code}\nFurther, we can demonstrate that three is not in the list, because\nany possible proof that it is in the list leads to contradiction:\n\\begin{code}\nnot-in : 3 \u2209 [ 0 , 1 , 0 , 2 ]\nnot-in (here ())\nnot-in (there (here ()))\nnot-in (there (there (here ())))\nnot-in (there (there (there (here ()))))\nnot-in (there (there (there (there ()))))\n\\end{code}\nThe five occurrences of `()` attest to the fact that there is no\npossible evidence for `3 \u2261 0`, `3 \u2261 1`, `3 \u2261 0`, `3 \u2261 2`, and\n`3 \u2208 []`, respectively.\n\n## All and append\n\nA predicate holds for every element of one list appended to another if and\nonly if it holds for every element of each list:\n\\begin{code}\nAll-++-\u21d4 : \u2200 {A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P (xs ++ ys) \u21d4 (All P xs \u00d7 All P ys)\nAll-++-\u21d4 xs ys =\n record\n { to = to xs ys\n ; from = from xs ys\n }\n where\n\n to : \u2200 {A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P (xs ++ ys) \u2192 (All P xs \u00d7 All P ys)\n to [] ys Pys = \u27e8 [] , Pys \u27e9\n to (x \u2237 xs) ys (Px \u2237 Pxs++ys) with to xs ys Pxs++ys\n ... | \u27e8 Pxs , Pys \u27e9 = \u27e8 Px \u2237 Pxs , Pys \u27e9\n\n from : \u2200 { A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P xs \u00d7 All P ys \u2192 All P (xs ++ ys)\n from [] ys \u27e8 [] , Pys \u27e9 = Pys\n from (x \u2237 xs) ys \u27e8 Px \u2237 Pxs , Pys \u27e9 = Px \u2237 from xs ys \u27e8 Pxs , Pys \u27e9\n\\end{code}\n\n#### Exercise `Any-++-\u21d4` (recommended)\n\nProve a result similar to `All-++-\u2194`, but with `Any` in place of `All`, and a suitable\nreplacement for `_\u00d7_`. As a consequence, demonstrate an equivalence relating\n`_\u2208_` and `_++_`.\n\n#### Exercise `All-++-\u2243` (stretch)\n\nShow that the equivalence `All-++-\u21d4` can be extended to an isomorphism.\n\n#### Exercise `\u00acAny\u2243All\u00ac` (stretch)\n\nFirst generalise composition to arbitrary levels, using\n[universe polymorphism][plfa.Equality#unipoly]:\n\\begin{code}\n_\u2218\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u2083 : Level} {A : Set \u2113\u2081} {B : Set \u2113\u2082} {C : Set \u2113\u2083}\n \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n(g \u2218\u2032 f) x = g (f x)\n\\end{code}\n\nShow that `Any` and `All` satisfy a version of De Morgan's Law:\n\\begin{code}\npostulate\n \u00acAny\u2243All\u00ac : \u2200 {A : Set} (P : A \u2192 Set) (xs : List A)\n \u2192 (\u00ac_ \u2218\u2032 Any P) xs \u2243 All (\u00ac_ \u2218\u2032 P) xs\n\\end{code}\n\nDo we also have the following?\n\\begin{code}\npostulate\n \u00acAll\u2243Any\u00ac : \u2200 {A : Set} (P : A \u2192 Set) (xs : List A)\n \u2192 (\u00ac_ \u2218\u2032 All P) xs \u2243 Any (\u00ac_ \u2218\u2032 P) xs\n\\end{code}\nIf so, prove; if not, explain why.\n\n\n## Decidability of All\n\nIf we consider a predicate as a function that yields a boolean,\nit is easy to define an analogue of `All`, which returns true if\na given predicate returns true for every element of a list:\n\\begin{code}\nall : \u2200 {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nall p = foldr _\u2227_ true \u2218 map p\n\\end{code}\nThe function can be written in a particularly compact style by\nusing the higher-order functions `map` and `foldr`.\n\nAs one would hope, if we replace booleans by decidables there is again\nan analogue of `All`. First, return to the notion of a predicate `P` as\na function of type `A \u2192 Set`, taking a value `x` of type `A` into evidence\n`P x` that a property holds for `x`. Say that a predicate `P` is _decidable_\nif we have a function that for a given `x` can decide `P x`:\n\\begin{code}\nDecidable : \u2200 {A : Set} \u2192 (A \u2192 Set) \u2192 Set\nDecidable {A} P = \u2200 (x : A) \u2192 Dec (P x)\n\\end{code}\nThen if predicate `P` is decidable, it is also decidable whether every\nelement of a list satisfies the predicate:\n\\begin{code}\nAll? : \u2200 {A : Set} {P : A \u2192 Set} \u2192 Decidable P \u2192 Decidable (All P)\nAll? P? [] = yes []\nAll? P? (x \u2237 xs) with P? x | All? P? xs\n... | yes Px | yes Pxs = yes (Px \u2237 Pxs)\n... | no \u00acPx | _ = no \u03bb{ (Px \u2237 Pxs) \u2192 \u00acPx Px }\n... | _ | no \u00acPxs = no \u03bb{ (Px \u2237 Pxs) \u2192 \u00acPxs Pxs }\n\\end{code}\nIf the list is empty, then trivially `P` holds for every element of\nthe list. Otherwise, the structure of the proof is similar to that\nshowing that the conjuction of two decidable propositions is itself\ndecidable, using `_\u2237_` rather than `\u27e8_,_\u27e9` to combine the evidence for\nthe head and tail of the list.\n\n\n#### Exercise `any?` (stretch)\n\nJust as `All` has analogues `all` and `all?` which determine whether a\npredicate holds for every element of a list, so does `Any` have\nanalogues `any` and `any?` which determine whether a predicates holds\nfor some element of a list. Give their definitions.\n\n\n#### Exercise `filter?` (stretch)\n\nDefine the following variant of the traditional `filter` function on lists,\nwhich given a list and a decidable predicate returns all elements of the\nlist satisfying the predicate:\n\\begin{code}\npostulate\n filter? : \u2200 {A : Set} {P : A \u2192 Set}\n \u2192 (P? : Decidable P) \u2192 List A \u2192 \u2203[ ys ]( All P ys )\n\\end{code}\n\n\n## Standard Library\n\nDefinitions similar to those in this chapter can be found in the standard library:\n\\begin{code}\nimport Data.List using (List; _++_; length; reverse; map; foldr; downFrom)\nimport Data.List.All using (All; []; _\u2237_)\nimport Data.List.Any using (Any; here; there)\nimport Data.List.Membership.Propositional using (_\u2208_)\nimport Data.List.Properties\n using (reverse-++-commute; map-compose; map-++-commute; foldr-++)\n renaming (mapIsFold to map-is-foldr)\nimport Algebra.Structures using (IsMonoid)\nimport Relation.Unary using (Decidable)\nimport Relation.Binary using (Decidable)\n\\end{code}\nThe standard library version of `IsMonoid` differs from the\none given here, in that it is also parameterised on an equivalence relation.\n\nBoth `Relation.Unary` and `Relation.Binary` define a version of `Decidable`,\none for unary relations (as used in this chapter where `P` ranges over\nunary predicates) and one for binary relations (as used earlier, where `_\u2264_`\nranges over a binary relation).\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u2237 U+2237 PROPORTION (\\::)\n \u2297 U+2297 CIRCLED TIMES (\\otimes, \\ox)\n \u2208 U+2208 ELEMENT OF (\\in)\n \u2209 U+2209 NOT AN ELEMENT OF (\\inn, \\notin)\n","avg_line_length":29.3579154376,"max_line_length":92,"alphanum_fraction":0.6201560773} +{"size":358,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Dedication: \u9898\u732e\"\nlayout : page\npermalink : \/Dedication\/\nnext : \/Preface\/\ntranslators : [Oling Cat]\nprogress : 100\n---\n\n{::comment}\n
\n

To Wanda<\/h2>\n

amor da minha vida<\/em><\/h3>\n

knock knock knock<\/em><\/h4>\n<\/center>\n{:\/}\n\n
\n

\u81f4 Wanda<\/h2>\n

\u6211\u751f\u547d\u4e2d\u7684\u81f3\u7231<\/em><\/h3>\n

\u549a \u549a \u549a<\/em><\/h4>\n<\/center>\n","avg_line_length":15.5652173913,"max_line_length":36,"alphanum_fraction":0.5893854749} +{"size":284,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe #-}\n\nmodule Snippets.PropTrunc where\n\nopen import Level\nopen import Path\n\\end{code}\n%<*prop-trunc-def>\n\\begin{code}\ndata \u2225_\u2225 (A : Type a) : Type a where\n \u2223_\u2223 : A \u2192 \u2225 A \u2225\n squash : (x y : \u2225 A \u2225) \u2192 x \u2261 y\n\\end{code}\n%<\/prop-trunc-def>\n","avg_line_length":17.75,"max_line_length":36,"alphanum_fraction":0.5845070423} +{"size":10277,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Main where\nopen import Agda.Builtin.IO\nopen import Agda.Builtin.Unit\nopen import Agda.Builtin.String\n\npostulate\n putStrLn : String \u2192 IO \u22a4\n\n{-# FOREIGN GHC import qualified Data.Text.IO as Text #-}\n{-# FOREIGN GHC import qualified Data.Text as T #-}\n{-# COMPILE GHC putStrLn = Text.putStrLn #-}\n\nopen import Type\nopen import Builtin\nopen import Builtin.Constant.Type\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\nopen import Builtin.Signature\n\nopen import Agda.Builtin.TrustMe\nopen import Relation.Binary.PropositionalEquality\nopen import Agda.Builtin.Nat\nopen import Data.Nat\nopen import Agda.Builtin.Int\nopen import Data.Integer\nimport Data.Maybe as M\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Bool\n\nopen import Check hiding (_>>=_; return)\nopen import Scoped.Extrication\nopen import Type.BetaNBE\n\n\nopen Agda.Builtin.IO\nopen import Data.String\npostulate\n return : \u2200 {a} {A : Set a} \u2192 A \u2192 IO A\n _>>=_ : \u2200 {a b} {A : Set a} {B : Set b} \u2192 IO A \u2192 (A \u2192 IO B) \u2192 IO B\n\n{-# COMPILE GHC return = \\_ _ -> return #-}\n{-# COMPILE GHC _>>=_ = \\_ _ _ _ -> (>>=) #-}\n\n_>>_ : \u2200 {a b} {A : Set a} {B : Set b} \u2192 IO A \u2192 IO B \u2192 IO B\nx >> y = x >>= \u03bb _ \u2192 y\n\nopen import Raw\nopen import Scoped\nopen import Utils\n\npostulate\n imap : \u2200{A B : Set} \u2192 (A \u2192 B) \u2192 IO A \u2192 IO B\n mmap : \u2200{A B : Set} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n mbind : \u2200{A B : Set} \u2192 (A \u2192 Maybe B) \u2192 Maybe A \u2192 Maybe B\n TermN : Set\n Term : Set\n TypeN : Set\n Type : Set\n ProgramN : Set\n Program : Set\n convTm : Term \u2192 RawTm\n convTy : Type \u2192 RawTy\n convP : Program \u2192 RawTm\n readFile : String \u2192 IO ByteString\n parse : ByteString \u2192 Maybe ProgramN\n parseTm : ByteString \u2192 Maybe TermN\n parseTy : ByteString \u2192 Maybe TypeN\n showTerm : RawTm \u2192 String\n getContents : IO ByteString\n exitFailure : IO \u22a4\n exitSuccess : IO \u22a4\n deBruijnify : ProgramN \u2192 Maybe Program\n deBruijnifyTm : TermN \u2192 Maybe Term\n deBruijnifyTy : TypeN \u2192 Maybe Type\n\n{-# FOREIGN GHC import Language.PlutusCore.Name #-}\n{-# FOREIGN GHC import Language.PlutusCore.Lexer #-}\n{-# FOREIGN GHC import Language.PlutusCore.Parser #-}\n{-# FOREIGN GHC import Language.PlutusCore.Pretty #-}\n{-# FOREIGN GHC import Language.PlutusCore.DeBruijn #-}\n{-# FOREIGN GHC import Data.Either #-}\n{-# FOREIGN GHC import System.Exit #-}\n{-# COMPILE GHC exitSuccess = exitSuccess #-}\n{-# COMPILE GHC exitFailure = exitFailure #-}\n{-# FOREIGN GHC import Control.Monad.Trans.Except #-}\n\n\n{-# FOREIGN GHC import Raw #-}\n{-# COMPILE GHC convP = convP #-}\n{-# COMPILE GHC convTm = conv #-}\n{-# COMPILE GHC convTy = convT #-}\n\n{-# FOREIGN GHC import qualified Data.ByteString.Lazy as BSL #-}\n{-# COMPILE GHC getContents = BSL.getContents #-}\n{-# COMPILE GHC imap = \\_ _ -> fmap #-}\n{-# COMPILE GHC mmap = \\_ _ -> fmap #-}\n{-# COMPILE GHC mbind = \\_ _ f a -> f =<< a #-}\n{-# FOREIGN GHC import Data.Either #-}\n{-# COMPILE GHC parse = either (\\_ -> Nothing) Just . parse #-}\n{-# COMPILE GHC parseTm = either (\\_ -> Nothing) Just . parseTm #-}\n{-# COMPILE GHC parseTy = either (\\_ -> Nothing) Just . parseTy #-}\n\n{-# COMPILE GHC deBruijnify = either (\\_ -> Nothing) Just . runExcept . deBruijnProgram #-}\n{-# COMPILE GHC deBruijnifyTm = either (\\_ -> Nothing) Just . runExcept . deBruijnTerm #-}\n{-# COMPILE GHC deBruijnifyTy = either (\\_ -> Nothing) Just . runExcept . deBruijnTy #-}\n{-# FOREIGN GHC import Language.PlutusCore #-}\n{-# COMPILE GHC ProgramN = type Language.PlutusCore.Program TyName Name DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Program = type Language.PlutusCore.Program TyDeBruijn DeBruijn DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC TermN = type Language.PlutusCore.Term TyName Name DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Term = type Language.PlutusCore.Term TyDeBruijn DeBruijn DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC TypeN = type Language.PlutusCore.Type TyName DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Type = type Language.PlutusCore.Type TyDeBruijn DefaultUni Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC readFile = \\ s -> BSL.readFile (T.unpack s) #-}\n{-# COMPILE GHC showTerm = T.pack . show #-}\nopen import Function\n\nopen import Untyped as U\nimport Untyped.Reduction as U\nimport Scoped as S\nimport Scoped.Reduction as S\n\nopen import Data.Sum\n\nmapper : {A B : Set} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmapper f nothing = nothing\nmapper f (just a) = just (f a)\n\nopen import Untyped\n\n-- untyped evaluation\n--utestPLC : ByteString \u2192 Maybe String\n--utestplc plc = mmap (U.ugly \u2218 (\u03bb (t : 0 \u22a2) \u2192 proj\u2081 (U.run t 100)) \u2218 erase\u22a2) (mbind (deBruijnifyTm nil) (mmap convP (parse plc)))\n\nopen import Data.Fin\n\npostulate\n prettyPrintTm : RawTm \u2192 String\n prettyPrintTy : RawTy \u2192 String\n\n{-# COMPILE GHC prettyPrintTm = prettyText . unconv (-1) (-1) #-}\n{-# COMPILE GHC prettyPrintTy = prettyText . unconvT (-1) #-}\n\nopen import Data.Vec hiding (_>>=_;_++_)\n\nopen import Scoped.CK\nopen import Algorithmic.CK\n\ndata EvalMode : Set where\n TCK CK : EvalMode\n\n-- extrinsically typed evaluation\nevalPLC : EvalMode \u2192 ByteString \u2192 String \u228e String\nevalPLC m plc with parse plc\nevalPLC m plc | nothing = inj\u2082 \"parse error\"\nevalPLC m plc | just nt with deBruijnify nt\nevalPLC m plc | just nt | nothing = inj\u2082 \"(Haskell) Scope Error\"\nevalPLC m plc | just nt | just t with scopeCheckTm {0}{Z} (shifter 0 Z (convP t))\nevalPLC m plc | just nt | just t | nothing = inj\u2082 $ \"(Agda) Scope Error\"\n ++ \"\\n\" ++ rawPrinter (shifter 0 Z (convP t))\nevalPLC CK plc | just nt | just t | just t' with Scoped.CK.stepper 1000000000 _ (\u03b5 \u25bb saturate t')\nevalPLC CK plc | just nt | just t | just t' | n ,, i ,, _ ,, just (\u25a1 {t = t''} V) =\n inj\u2081 (prettyPrintTm (extricateScope (unsaturate t'')))\nevalPLC CK plc | just nt | just t | just t' | _ ,, _ ,, _ ,, just _ =\n inj\u2082 (\"this shouldn't happen\")\nevalPLC CK plc | just nt | just t | just t' | _ ,, _ ,, _ ,, nothing = inj\u2082 \"out of fuel\"\nevalPLC TCK plc | just nt | just t | just t' with inferType _ t'\n... | inj\u2082 e = inj\u2082 \"typechecking error\"\n... | inj\u2081 (A ,, t'') with Algorithmic.CK.stepper 1000000000 _ (\u03b5 \u25bb t'')\n... | _ ,, _ ,, _ ,, _ ,, M.just (\u25a1 {t = t'''} V) =\n inj\u2081 (prettyPrintTm (extricateScope (extricate t''')))\n... | _ ,, _ ,, _ ,, _ ,, M.just _ = inj\u2082 \"this shouldn't happen\"\n... | _ ,, _ ,, _ ,, _ ,, M.nothing = inj\u2082 \"out of fuel\"\n\n\njunk : \u2200{n} \u2192 Vec String n\njunk {zero} = []\njunk {Nat.suc n} = Data.Integer.show (pos n) \u2237 junk\n\ntcPLC : ByteString \u2192 String \u228e String\ntcPLC plc with parse plc\n... | nothing = inj\u2082 \"Parse Error\"\n... | just nt with deBruijnify nt\n... | nothing = inj\u2082 \"(Haskell) Scope Error\"\n... | just t with scopeCheckTm {0}{Z} (shifter 0 Z (convP t))\n... | nothing = inj\u2082 \"(Agda) scope error\"\n... | just t' with inferType _ t'\n... | inj\u2081 (A ,, t'') =\n inj\u2081 (prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A)))\n... | inj\u2082 typeError = inj\u2082 \"typeError\"\n... | inj\u2082 kindEqError = inj\u2082 \"kindEqError\"\n... | inj\u2082 notTypeError = inj\u2082 \"notTypeError\"\n... | inj\u2082 notFunction = inj\u2082 \"notFunction\"\n... | inj\u2082 notPiError = inj\u2082 \"notPiError\"\n... | inj\u2082 notPat = inj\u2082 \"notPat\"\n... | inj\u2082 (nameError x x') = inj\u2082 (x Data.String.++ \" != \" Data.String.++ x')\n... | inj\u2082 (typeEqError n n') = inj\u2082 (\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 n))\n Data.String.++\n \"\\n != \\n\"\n Data.String.++\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 n')))\n\n... | inj\u2082 typeVarEqError = inj\u2082 \"typeVarEqError\"\n... | inj\u2082 tyConError = inj\u2082 \"tyConError\"\n... | inj\u2082 builtinError = inj\u2082 \"builtinError\"\n... | inj\u2082 unwrapError = inj\u2082 \"unwrapError\"\n\nalphaTm : ByteString \u2192 ByteString \u2192 Bool\nalphaTm plc1 plc2 with parseTm plc1 | parseTm plc2\nalphaTm plc1 plc2 | just plc1' | just plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nalphaTm plc1 plc2 | just plc1' | just plc2' | just plc1'' | just plc2'' = decRTm (convTm plc1'') (convTm plc2'')\nalphaTm plc1 plc2 | just plc1' | just plc2' | _ | _ = Bool.false\nalphaTm plc1 plc2 | _ | _ = Bool.false\n\n\n{-# COMPILE GHC alphaTm as alphaTm #-}\nprintTy : ByteString \u2192 String\nprintTy b with parseTy b\n... | nothing = \"parseTy error\"\n... | just A with deBruijnifyTy A\n... | nothing = \"deBruinjifyTy error\"\n... | just A' = rawTyPrinter (convTy A')\n\n{-# COMPILE GHC printTy as printTy #-}\n\n\nalphaTy : ByteString \u2192 ByteString \u2192 Bool\nalphaTy plc1 plc2 with parseTy plc1 | parseTy plc2\nalphaTy plc1 plc2 | just plc1' | just plc2' with deBruijnifyTy plc1' | deBruijnifyTy plc2'\nalphaTy plc1 plc2 | just plc1' | just plc2' | just plc1'' | just plc2'' = decRTy (convTy plc1'') (convTy plc2'')\nalphaTy plc1 plc2 | just plc1' | just plc2' | _ | _ = Bool.false\nalphaTy plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTy as alphaTy #-}\n\n\n{-# FOREIGN GHC import System.Environment #-}\n\nopen import Data.List\n\npostulate getArgs : IO (List String)\n\n{-# COMPILE GHC getArgs = (fmap . fmap) T.pack $ getArgs #-}\n\n{-# FOREIGN GHC import Opts #-}\n\ndata Input : Set where\n FileInput : String \u2192 Input\n StdInput : Input\n\n{-# COMPILE GHC Input = data Input (FileInput | StdInput) #-}\n\ndata EvalOptions : Set where\n EvalOpts : Input \u2192 EvalMode \u2192 EvalOptions\n\ndata TCOptions : Set where\n TCOpts : Input \u2192 TCOptions\n\ndata Command : Set where\n Evaluate : EvalOptions \u2192 Command\n TypeCheck : TCOptions \u2192 Command\n\npostulate execP : IO Command\n\n{-# COMPILE GHC EvalOptions = data EvalOptions (EvalOpts) #-}\n{-# COMPILE GHC TCOptions = data TCOptions (TCOpts) #-}\n{-# COMPILE GHC Command = data Command (Evaluate | TypeCheck) #-}\n{-# COMPILE GHC EvalMode = data EvalMode (TCK | CK ) #-}\n{-# COMPILE GHC execP = execP #-}\n\nevalInput : EvalMode \u2192 Input \u2192 IO (String \u228e String)\nevalInput m (FileInput fn) = imap (evalPLC m) (readFile fn)\nevalInput m StdInput = imap (evalPLC m) getContents\n\ntcInput : Input \u2192 IO (String \u228e String)\ntcInput (FileInput fn) = imap tcPLC (readFile fn)\ntcInput StdInput = imap tcPLC getContents\n\nmain' : Command \u2192 IO \u22a4\nmain' (Evaluate (EvalOpts i m)) =\n evalInput m i\n >>=\n Data.Sum.[ (\u03bb s \u2192 putStrLn s >> exitSuccess)\n , (\u03bb e \u2192 putStrLn e >> exitFailure)\n ]\nmain' (TypeCheck (TCOpts i)) =\n (tcInput i)\n >>=\n Data.Sum.[ (\u03bb s \u2192 putStrLn s >> exitSuccess)\n , (\u03bb e \u2192 putStrLn e >> exitFailure)\n ]\n\nmain : IO \u22a4\nmain = execP >>= main'\n\\end{code}\n","avg_line_length":34.7195945946,"max_line_length":130,"alphanum_fraction":0.6650773572} +{"size":5872,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Collections: Representing collections as lists\"\npermalink : \/Collections\n---\n\nThis chapter presents operations on collections and a number of\nuseful operations on them.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; _\u2262_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_; _\u2238_; _\u2264_; s\u2264s; z\u2264n)\n-- open import Data.Nat.Properties using\n-- (+-assoc; +-identity\u02e1; +-identity\u02b3; *-assoc; *-identity\u02e1; *-identity\u02b3)\nopen import Relation.Nullary using (\u00ac_)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Isomorphism using (_\u2243_)\nopen import Function using (_\u2218_)\nopen import Level using (Level)\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr; filter)\nopen import Data.List.All using (All; []; _\u2237_)\nopen import Data.List.Any using (Any; here; there)\nopen import Data.Maybe using (Maybe; just; nothing)\n-- open import Data.List.Any.Membership.Propositional using (_\u2208_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Negation using (contraposition; \u00ac?)\nopen import Relation.Nullary.Product using (_\u00d7-dec_)\n-- open import Relation.Binary using (IsEquivalence)\n\\end{code}\n\n\n## Collections\n\n\\begin{code}\ninfix 0 _\u2194_\n\n_\u2194_ : Set \u2192 Set \u2192 Set\nA \u2194 B = (A \u2192 B) \u00d7 (B \u2192 A)\n\nmodule CollectionDec (A : Set) (_\u225f_ : \u2200 (x y : A) \u2192 Dec (x \u2261 y)) where\n\n Coll : Set \u2192 Set\n Coll A = List A\n\n [_] : A \u2192 Coll A\n [ x ] = x \u2237 []\n\n infix 4 _\u2208_\n infix 4 _\u2286_\n infixl 5 _\u222a_\n infixl 5 _\\\\_\n\n data _\u2208_ : A \u2192 Coll A \u2192 Set where\n\n here : \u2200 {x xs} \u2192\n ----------\n x \u2208 x \u2237 xs\n\n there : \u2200 {w x xs} \u2192\n w \u2208 xs \u2192\n ----------\n w \u2208 x \u2237 xs\n\n _\u2286_ : Coll A \u2192 Coll A \u2192 Set\n xs \u2286 ys = \u2200 {w} \u2192 w \u2208 xs \u2192 w \u2208 ys\n\n _\u222a_ : Coll A \u2192 Coll A \u2192 Coll A\n _\u222a_ = _++_\n\n _\\\\_ : Coll A \u2192 A \u2192 Coll A\n xs \\\\ x = filter (\u00ac? \u2218 (_\u225f x)) xs\n\n refl-\u2286 : \u2200 {xs} \u2192 xs \u2286 xs\n refl-\u2286 \u2208xs = \u2208xs\n\n trans-\u2286 : \u2200 {xs ys zs} \u2192 xs \u2286 ys \u2192 ys \u2286 zs \u2192 xs \u2286 zs\n trans-\u2286 xs\u2286 ys\u2286 = ys\u2286 \u2218 xs\u2286\n\n lemma-[_] : \u2200 {w xs} \u2192 w \u2208 xs \u2194 [ w ] \u2286 xs\n lemma-[_] = \u27e8 forward , backward \u27e9\n where\n\n forward : \u2200 {w xs} \u2192 w \u2208 xs \u2192 [ w ] \u2286 xs\n forward \u2208xs here = \u2208xs\n forward \u2208xs (there ())\n\n backward : \u2200 {w xs} \u2192 [ w ] \u2286 xs \u2192 w \u2208 xs\n backward \u2286xs = \u2286xs here\n\n lemma-\\\\-\u2208-\u2262 : \u2200 {w x xs} \u2192 w \u2208 xs \\\\ x \u2194 w \u2208 xs \u00d7 w \u2262 x\n lemma-\\\\-\u2208-\u2262 = \u27e8 forward , backward \u27e9\n where\n\n next : \u2200 {w x y xs} \u2192 w \u2208 xs \u00d7 w \u2262 x \u2192 w \u2208 y \u2237 xs \u00d7 w \u2262 x\n next \u27e8 w\u2208 , w\u2262 \u27e9 = \u27e8 there w\u2208 , w\u2262 \u27e9\n\n forward : \u2200 {w x xs} \u2192 w \u2208 xs \\\\ x \u2192 w \u2208 xs \u00d7 w \u2262 x\n forward {_} {x} {[]} ()\n forward {_} {x} {y \u2237 _} w\u2208 with y \u225f x\n forward {_} {x} {y \u2237 _} w\u2208 | yes refl = next (forward w\u2208)\n forward {_} {x} {y \u2237 _} here | no y\u2262 = \u27e8 here , (\u03bb y\u2261 \u2192 y\u2262 y\u2261) \u27e9\n forward {_} {x} {y \u2237 _} (there w\u2208) | no _ = next (forward w\u2208)\n\n backward : \u2200 {w x xs} \u2192 w \u2208 xs \u00d7 w \u2262 x \u2192 w \u2208 xs \\\\ x\n backward {_} {x} {y \u2237 _} \u27e8 here , w\u2262 \u27e9\n with y \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = here\n backward {_} {x} {y \u2237 _} \u27e8 there w\u2208 , w\u2262 \u27e9\n with y \u225f x\n ... | yes refl = backward \u27e8 w\u2208 , w\u2262 \u27e9\n ... | no _ = there (backward \u27e8 w\u2208 , w\u2262 \u27e9)\n\n\n lemma-\\\\-\u2237 : \u2200 {x xs ys} \u2192 xs \\\\ x \u2286 ys \u2194 xs \u2286 x \u2237 ys\n lemma-\\\\-\u2237 = \u27e8 forward , backward \u27e9\n where\n\n forward : \u2200 {x xs ys} \u2192 xs \\\\ x \u2286 ys \u2192 xs \u2286 x \u2237 ys\n forward {x} \u2286ys {w} \u2208xs\n with w \u225f x\n ... | yes refl = here\n ... | no \u2262x = there (\u2286ys (proj\u2082 lemma-\\\\-\u2208-\u2262 \u27e8 \u2208xs , \u2262x \u27e9))\n\n backward : \u2200 {x xs ys} \u2192 xs \u2286 x \u2237 ys \u2192 xs \\\\ x \u2286 ys\n backward {x} xs\u2286 {w} w\u2208\n with proj\u2081 lemma-\\\\-\u2208-\u2262 w\u2208\n ... | \u27e8 \u2208xs , \u2262x \u27e9 with w \u225f x\n ... | yes refl = \u22a5-elim (\u2262x refl)\n ... | no w\u2262 with (xs\u2286 \u2208xs)\n ... | here = \u22a5-elim (\u2262x refl)\n ... | there \u2208ys = \u2208ys\n\n lemma-\u222a\u2081 : \u2200 {xs ys} \u2192 xs \u2286 xs \u222a ys\n lemma-\u222a\u2081 here = here\n lemma-\u222a\u2081 (there \u2208xs) = there (lemma-\u222a\u2081 \u2208xs)\n\n lemma-\u222a\u2082 : \u2200 {xs ys} \u2192 ys \u2286 xs \u222a ys\n lemma-\u222a\u2082 {[]} \u2208ys = \u2208ys\n lemma-\u222a\u2082 {x \u2237 xs} \u2208ys = there (lemma-\u222a\u2082 {xs} \u2208ys)\n\n lemma-\u228e-\u222a : \u2200 {w xs ys} \u2192 w \u2208 xs \u228e w \u2208 ys \u2194 w \u2208 xs \u222a ys\n lemma-\u228e-\u222a = \u27e8 forward , backward \u27e9\n where\n\n forward : \u2200 {w xs ys} \u2192 w \u2208 xs \u228e w \u2208 ys \u2192 w \u2208 xs \u222a ys\n forward (inj\u2081 \u2208xs) = lemma-\u222a\u2081 \u2208xs\n forward (inj\u2082 \u2208ys) = lemma-\u222a\u2082 \u2208ys\n\n backward : \u2200 {xs ys w} \u2192 w \u2208 xs \u222a ys \u2192 w \u2208 xs \u228e w \u2208 ys\n backward {[]} \u2208ys = inj\u2082 \u2208ys\n backward {x \u2237 xs} here = inj\u2081 here\n backward {x \u2237 xs} (there w\u2208) with backward {xs} w\u2208\n ... | inj\u2081 \u2208xs = inj\u2081 (there \u2208xs)\n ... | inj\u2082 \u2208ys = inj\u2082 \u2208ys\n\n\n\\end{code}\n\n\n## Standard Library\n\nDefinitions similar to those in this chapter can be found in the standard library.\n\\begin{code}\n-- EDIT\n\\end{code}\nThe standard library version of `IsMonoid` differs from the\none given here, in that it is also parameterised on an equivalence relation.\n\n\n## Unicode\n\nThis chapter uses the following unicode.\n\n EDIT\n \u2237 U+2237 PROPORTION (\\::)\n \u2297 U+2297 CIRCLED TIMES (\\otimes)\n \u2208 U+2208 ELEMENT OF (\\in)\n \u2209 U+2209 NOT AN ELEMENT OF (\\inn)\n","avg_line_length":31.5698924731,"max_line_length":87,"alphanum_fraction":0.4545299728} +{"size":7608,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Algorithmic.Completeness where\n\nopen import Type\nopen import Type.RenamingSubstitution\nimport Declarative as Syn\nimport Algorithmic as Norm\nopen import Type.BetaNormal\nopen import Type.BetaNBE\nopen import Type.BetaNBE.Completeness\nopen import Type.BetaNBE.RenamingSubstitution\n\nopen import Relation.Binary.PropositionalEquality renaming (subst to substEq) hiding ([_])\nopen import Function\n\nnfCtx : \u2200 {\u03a6} \u2192 Syn.Ctx \u03a6 \u2192 Norm.Ctx \u03a6\nnfCtx Syn.\u2205 = Norm.\u2205\nnfCtx (\u0393 Syn.,\u22c6 K) = nfCtx \u0393 Norm.,\u22c6 K\nnfCtx (\u0393 Syn., A) = nfCtx \u0393 Norm., nf A\n\nnfTyVar : \u2200{\u03a6 \u0393}\n \u2192 {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 Syn.\u220b A\n \u2192 nfCtx \u0393 Norm.\u220b nf A\nnfTyVar Syn.Z = Norm.Z\nnfTyVar (Syn.S \u03b1) = Norm.S (nfTyVar \u03b1)\nnfTyVar {\u0393 = \u0393 Syn.,\u22c6 K} (Syn.T {A = A} \u03b1) =\n Norm.conv\u220b (ren-nf S A) (Norm.T (nfTyVar \u03b1))\n\nlem\u03a0 : \u2200{\u0393 K }(B : \u0393 ,\u22c6 K \u22a2\u22c6 *){x} \u2192\n nf (\u03a0 x B) \u2261 \u03a0 x (nf B)\nlem\u03a0 B = cong (\u03a0 _) (sym (substNf-lemma' B))\n\nopen import Type.Equality\nopen import Type.BetaNBE.Soundness\n\nlemXX : \u2200{\u03a6 K}(pat : \u03a6 \u22a2\u22c6 _)(arg : \u03a6 \u22a2\u22c6 K) \u2192\n nf (pat \u00b7 (\u03bc1 \u00b7 pat) \u00b7 arg) \u2261\n nf\n (embNf (nf pat) \u00b7\n (\u03bc1 \u00b7\n embNf (nf pat))\n \u00b7 embNf (nf arg))\nlemXX {\u0393} pat arg = completeness\n (\u00b7\u2261\u03b2\n (\u00b7\u2261\u03b2 (soundness pat) (\u00b7\u2261\u03b2 (refl\u2261\u03b2 \u03bc1) (soundness pat)))\n (soundness arg))\n\nopen import Type.BetaNBE.Completeness\nopen import Type.BetaNBE.Soundness\nopen import Type.BetaNBE.Stability\n\nlem[] : \u2200{\u0393 K}(A : \u0393 \u22a2\u22c6 K)(B : \u0393 ,\u22c6 K \u22a2\u22c6 *) \u2192\n nf B [ nf A ]Nf \u2261 nf (B [ A ])\nlem[] A B = trans (subst-eval (embNf (nf B)) idCR (embNf \u2218 substNf-cons (ne \u2218 `) (nf A))) (trans (fund (\u03bb { Z \u2192 symCR (fund idCR (soundness A)) ; (S \u03b1) \u2192 idCR _}) (sym\u2261\u03b2 (soundness B))) (sym (subst-eval B idCR (subst-cons ` A)))) \n\nimport Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2\u22c6_ ` con boolean\n as SSig\nimport Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con booleanNf\n as NSig\nopen import Builtin\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con as STermCon\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con as NTermCon\n\n\nnfTypeTC : \u2200{\u03c6}{A : \u03c6 \u22a2\u22c6 *} \u2192 STermCon.TermCon A \u2192 NTermCon.TermCon (nf A)\nnfTypeTC (STermCon.integer i) = NTermCon.integer i\nnfTypeTC (STermCon.bytestring b) = NTermCon.bytestring b\n\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List\n\nnfTypeSIG\u2261\u2081 : (bn : Builtin) \u2192 proj\u2081 (SSig.SIG bn) \u2261 proj\u2081 (NSig.SIG bn)\nnfTypeSIG\u2261\u2081 addInteger = refl\nnfTypeSIG\u2261\u2081 subtractInteger = refl\nnfTypeSIG\u2261\u2081 multiplyInteger = refl\nnfTypeSIG\u2261\u2081 divideInteger = refl\nnfTypeSIG\u2261\u2081 quotientInteger = refl\nnfTypeSIG\u2261\u2081 remainderInteger = refl\nnfTypeSIG\u2261\u2081 modInteger = refl\nnfTypeSIG\u2261\u2081 lessThanInteger = refl\nnfTypeSIG\u2261\u2081 lessThanEqualsInteger = refl\nnfTypeSIG\u2261\u2081 greaterThanInteger = refl\nnfTypeSIG\u2261\u2081 greaterThanEqualsInteger = refl\nnfTypeSIG\u2261\u2081 equalsInteger = refl\nnfTypeSIG\u2261\u2081 concatenate = refl\nnfTypeSIG\u2261\u2081 takeByteString = refl\nnfTypeSIG\u2261\u2081 dropByteString = refl\nnfTypeSIG\u2261\u2081 sha2-256 = refl\nnfTypeSIG\u2261\u2081 sha3-256 = refl\nnfTypeSIG\u2261\u2081 verifySignature = refl\nnfTypeSIG\u2261\u2081 equalsByteString = refl\n\nlem\u03c3 : \u2200{\u0393 \u0394 \u0394'}\n \u2192 (\u03c3 : Sub \u0394 \u0393)\n \u2192 (C : \u0394 \u22a2\u22c6 *)\n \u2192 (C' : \u0394' \u22a2Nf\u22c6 *)\n \u2192 (q : \u0394' \u2261 \u0394)\n \u2192 nf C \u2261 substEq (_\u22a2Nf\u22c6 *) q C' \u2192\n substNf\n (\u03bb {J} \u03b1 \u2192\n nf\n (\u03c3 (substEq (_\u220b\u22c6 J) q \u03b1)))\n C'\n \u2261\n nf (subst \u03c3 C)\nlem\u03c3 \u03c3 C _ refl refl = trans\n-- this should be a lemma in NBE\/RenSubst\n-- substNf (nf \u2218 \u03c3) (nf C) \u2261 nf (subst \u03c3 C)\n (trans\n (subst-eval (embNf (nf C)) idCR (embNf \u2218 nf \u2218 \u03c3))\n (fund (\u03bb \u03b1 \u2192 fund idCR (sym\u2261\u03b2 (soundness (\u03c3 \u03b1)))) (sym\u2261\u03b2 (soundness C))))\n (sym (subst-eval C idCR \u03c3))\n\nnfTypeSIG\u2261\u2082 : (bn : Builtin) \u2192\n nf (proj\u2082 (proj\u2082 (SSig.SIG bn))) \u2261\n substEq (_\u22a2Nf\u22c6 *) (sym (nfTypeSIG\u2261\u2081 bn))\n (proj\u2082 (proj\u2082 (NSig.SIG bn)))\nnfTypeSIG\u2261\u2082 addInteger = refl\nnfTypeSIG\u2261\u2082 subtractInteger = refl\nnfTypeSIG\u2261\u2082 multiplyInteger = refl\nnfTypeSIG\u2261\u2082 divideInteger = refl\nnfTypeSIG\u2261\u2082 quotientInteger = refl\nnfTypeSIG\u2261\u2082 remainderInteger = refl\nnfTypeSIG\u2261\u2082 modInteger = refl\nnfTypeSIG\u2261\u2082 lessThanInteger = refl\nnfTypeSIG\u2261\u2082 lessThanEqualsInteger = refl\nnfTypeSIG\u2261\u2082 greaterThanInteger = refl\nnfTypeSIG\u2261\u2082 greaterThanEqualsInteger = refl\nnfTypeSIG\u2261\u2082 equalsInteger = refl\nnfTypeSIG\u2261\u2082 concatenate = refl\nnfTypeSIG\u2261\u2082 takeByteString = refl\nnfTypeSIG\u2261\u2082 dropByteString = refl\nnfTypeSIG\u2261\u2082 sha2-256 = refl\nnfTypeSIG\u2261\u2082 sha3-256 = refl\nnfTypeSIG\u2261\u2082 verifySignature = refl\nnfTypeSIG\u2261\u2082 equalsByteString = refl\nopen import Builtin.Constant.Type\n\nlemcon : \u2200{\u0393 \u0393'}(p : \u0393 \u2261 \u0393')(tcn : TyCon)\n \u2192 con tcn \u2261 substEq (_\u22a2Nf\u22c6 *) p (con tcn)\nlemcon refl tcn = refl\n\nsubstTC : \u2200{\u0393 \u0393' : Ctx\u22c6}(p : \u0393 \u2261 \u0393')(tcn : TyCon)\n \u2192 NTermCon.TermCon {\u03a6 = \u0393} (con tcn)\n \u2192 NTermCon.TermCon {\u03a6 = \u0393'}(con tcn)\nsubstTC refl tcn t = t\n\nnfList : \u2200{\u0394} \u2192 List (\u0394 \u22a2\u22c6 *) \u2192 List (\u0394 \u22a2Nf\u22c6 *)\nnfList [] = []\nnfList (A \u2237 As) = nf A \u2237 nfList As\n\nlemList : (bn : Builtin)\n \u2192 substEq (\u03bb \u0394 \u2192 List (\u0394 \u22a2Nf\u22c6 *)) (sym (nfTypeSIG\u2261\u2081 bn))\n (proj\u2081 (proj\u2082 (NSig.SIG bn)))\n \u2261 nfList (proj\u2081 (proj\u2082 (SSig.SIG bn)))\nlemList addInteger = refl\nlemList subtractInteger = refl\nlemList multiplyInteger = refl\nlemList divideInteger = refl\nlemList quotientInteger = refl\nlemList remainderInteger = refl\nlemList modInteger = refl\nlemList lessThanInteger = refl\nlemList lessThanEqualsInteger = refl\nlemList greaterThanInteger = refl\nlemList greaterThanEqualsInteger = refl\nlemList equalsInteger = refl\nlemList concatenate = refl\nlemList takeByteString = refl\nlemList dropByteString = refl\nlemList sha2-256 = refl\nlemList sha3-256 = refl\nlemList verifySignature = refl\nlemList equalsByteString = refl\n\nnfType : \u2200{\u03a6 \u0393}\n \u2192 {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 Syn.\u22a2 A\n \u2192 nfCtx \u0393 Norm.\u22a2 nf A\n \nnfTypeTel : \u2200{\u03a6 \u0393 \u0394}(\u03c3 : Sub \u0394 \u03a6)(As : List (\u0394 \u22a2\u22c6 *))\n \u2192 Syn.Tel \u0393 \u0394 \u03c3 As\n \u2192 Norm.Tel (nfCtx \u0393) \u0394 (nf \u2218 \u03c3) (nfList As)\n\nnfTypeTel \u03c3 [] _ = _\nnfTypeTel {\u0393} \u03c3 (A \u2237 As) (M ,, Ms) = Norm.conv\u22a2\n -- this should be a lemma in NBE\/RenSubst\n -- substNf (nf \u2218 \u03c3) (nf C) \u2261 nf (subst \u03c3 C)\n -- also it might go away if we simplify the builtins post size removal\n (sym (trans (trans\n (subst-eval (embNf (nf A)) idCR (embNf \u2218 nf \u2218 \u03c3))\n (fund (\u03bb \u03b1 \u2192 fund idCR (sym\u2261\u03b2 (soundness (\u03c3 \u03b1)))) (sym\u2261\u03b2 (soundness A))))\n (sym (subst-eval A idCR \u03c3))))\n (nfType M)\n ,,\n nfTypeTel \u03c3 As Ms\n\n\nnfTypeTel' : \u2200{\u03a6 \u0393 \u0394 \u0394'}(\u03c3 : Sub \u0394 \u03a6)(As : List (\u0394 \u22a2\u22c6 *))\n \u2192 (q : \u0394' \u2261 \u0394)\n \u2192 (As' : List (\u0394' \u22a2Nf\u22c6 *))\n \u2192 (substEq (\u03bb \u0394 \u2192 List (\u0394 \u22a2Nf\u22c6 *)) q As' \u2261 nfList As)\n \u2192 Syn.Tel \u0393 \u0394 \u03c3 As\n \u2192 Norm.Tel\n (nfCtx \u0393)\n \u0394'\n (nf \u2218 \u03c3 \u2218 substEq (_\u220b\u22c6 _) q)\n As'\nnfTypeTel' \u03c3 As refl .(nfList As) refl tel = nfTypeTel \u03c3 As tel\n\nnfType (Syn.` \u03b1) = Norm.` (nfTyVar \u03b1)\nnfType {\u0393} (Syn.\u019b x t) = Norm.\u019b x (nfType t)\nnfType {\u0393} (t Syn.\u00b7 u) = nfType t Norm.\u00b7 nfType u\nnfType {\u0393} (Syn.\u039b {B = B} x t) = Norm.\u039b x (Norm.conv\u22a2 (substNf-lemma' B) (nfType t))\nnfType {\u0393} (Syn._\u00b7\u22c6_ {B = B} t A) =\n Norm.conv\u22a2 (lem[] A B) (Norm.conv\u22a2 (lem\u03a0 B) (nfType t) Norm.\u00b7\u22c6 nf A)\nnfType {\u0393} (Syn.wrap1 pat arg t) =\n Norm.wrap1 (nf pat) (nf arg) (Norm.conv\u22a2 (lemXX pat arg) (nfType t))\nnfType {\u0393} (Syn.unwrap1 {pat = pat}{arg} t) =\n Norm.conv\u22a2 (sym (lemXX pat arg)) (Norm.unwrap1 (nfType t))\nnfType (Syn.conv p t) = Norm.conv\u22a2 (completeness p) (nfType t)\nnfType {\u0393} (Syn.con {tcn = tcn} t) = Norm.con (nfTypeTC t)\nnfType {\u0393} (Syn.builtin bn \u03c3 tel) = let\n \u0394 ,, As ,, C = SSig.SIG bn\n \u0394' ,, As' ,, C' = NSig.SIG bn\n in Norm.conv\u22a2\n (lem\u03c3 \u03c3 C C' (sym (nfTypeSIG\u2261\u2081 bn)) (nfTypeSIG\u2261\u2082 bn))\n (Norm.builtin\n bn\n (nf \u2218 \u03c3 \u2218 substEq (_\u220b\u22c6 _) (sym (nfTypeSIG\u2261\u2081 bn)))\n (nfTypeTel' \u03c3 As (sym (nfTypeSIG\u2261\u2081 bn)) As' (lemList bn) tel))\nnfType {\u0393} (Syn.error A) = Norm.error (nf A)\n\ncompletenessT : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 Syn.\u22a2 A\n \u2192 nfCtx \u0393 Norm.\u22a2 nf A \u00d7 (A \u2261\u03b2 embNf (nf A))\ncompletenessT {A = A} t = nfType t ,, soundness A\n\\end{code}\n","avg_line_length":31.7,"max_line_length":230,"alphanum_fraction":0.6382754995} +{"size":55,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\n\n\nmodule ThreeNewLines where\n\n\n\\end{code}\n","avg_line_length":6.875,"max_line_length":26,"alphanum_fraction":0.7272727273} +{"size":12823,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Scoped.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Scoped\nopen import Scoped.RenamingSubstitution\nopen import Builtin\nopen import Builtin.Constant.Type\n\nopen import Utils\n\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr)\nopen import Data.Product\nopen import Data.List hiding ([_]; drop; take)\nopen import Function\nopen import Data.Integer as I\nopen import Data.Nat as N hiding (_?_;_\u2265?_)\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality hiding ([_];trans)\nimport Data.Bool as B\n\\end{code}\n\n\\begin{code}\ninfix 2 _\u2014\u2192_\n\\end{code}\n\n\\begin{code}\ndata Value {n}{w : Weird\u2115 n} : ScopedTm w \u2192 Set where\n V-\u019b : \u2200 x (A : ScopedTy n)(t : ScopedTm (S w)) \u2192 Value (\u019b x A t)\n V-\u039b : \u2200 x {K}(t : ScopedTm (T w)) \u2192 Value (\u039b x K t)\n V-con : (tcn : TermCon) \u2192 Value (con {n} tcn)\n V-wrap : (A B : ScopedTy n)(t : ScopedTm w) \u2192 Value (wrap A B t)\n V-builtin : (b : Builtin)\n (As : List (ScopedTy n))\n (ts : List (ScopedTm w))\n \u2192 Value (builtin b As ts)\n\nTel : \u2200{n} \u2192 Weird\u2115 n \u2192 Set\nTel w = List (ScopedTm w)\n\nopen import Data.Unit\nVTel : \u2200{n}(w : Weird\u2115 n) \u2192 Tel w \u2192 Set\nVTel w [] = \u22a4\nVTel w (t \u2237 ts) = Value t \u00d7 VTel w ts\n\n\n-- a term that satisfies this predicate has an error term in it somewhere\n-- or we encountered a rumtime type error\ndata Error {n}{w : Weird\u2115 n} : ScopedTm w \u2192 Set where\n -- a genuine runtime error returned from a builtin\n E-error : (A : ScopedTy n) \u2192 Error (error A)\n\n -- error inside somewhere\n E-\u00b7\u2081 : {L M : ScopedTm w} \u2192 Error L \u2192 Error (L \u00b7 M)\n\n\n E-\u00b7\u2082 : {L M : ScopedTm w} \u2192 Error M \u2192 Error (L \u00b7 M)\n E-\u00b7\u22c6 : {L : ScopedTm w}{A : ScopedTy n} \u2192 Error L \u2192 Error (L \u00b7\u22c6 A)\n E-unwrap : {L : ScopedTm w} \u2192 Error L \u2192 Error (unwrap L)\n\n -- runtime type errors\n -- these couldn't happen in the intrinsically typed version\n E-\u039b\u00b7 : \u2200{x K}{L : ScopedTm (T w)}{M : ScopedTm w} \u2192 Error (\u039b x K L \u00b7 M)\n E-\u019b\u00b7\u22c6 : \u2200{x}{B : ScopedTy n}{L : ScopedTm (S w)}{A : ScopedTy n}\n \u2192 Error (\u019b x B L \u00b7\u22c6 A)\n E-con\u00b7 : \u2200{tcn}{M : ScopedTm w} \u2192 Error (con tcn \u00b7 M)\n E-con\u00b7\u22c6 : \u2200{tcn}{A : ScopedTy n} \u2192 Error (con tcn \u00b7\u22c6 A)\n E-wrap\u00b7 : {A B : ScopedTy n}{t M : ScopedTm w} \u2192 Error (wrap A B t \u00b7 M)\n E-wrap\u00b7\u22c6 : {A' B A : ScopedTy n}{t : ScopedTm w}\n \u2192 Error (wrap A' B t \u00b7\u22c6 A)\n E-\u019bunwrap : \u2200{x}{A : ScopedTy n}{t : ScopedTm (S w)}\n \u2192 Error (unwrap (\u019b x A t) )\n E-\u039bunwrap : \u2200{x K}{t : ScopedTm (T w)} \u2192 Error (unwrap (\u039b x K t))\n E-conunwrap : \u2200{tcn} \u2192 Error (unwrap (con tcn))\n\n -- this stuff is required due to unsaturated builtins in term args only\n E-builtin\u00b7\u22c6 : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {A : ScopedTy n}\n \u2192 Error (builtin b As ts \u00b7\u22c6 A)\n\n E-builtinunwrap : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n \u2192 Error (unwrap (builtin b As ts))\n\n -- an error occured in one of reducing an argument\n E-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {t : ScopedTm w}\n \u2192 Error t\n \u2192 Error (builtin b As ts)\n\nVERIFYSIG : \u2200{n}{w : Weird\u2115 n} \u2192 Maybe B.Bool \u2192 ScopedTm w\nVERIFYSIG (just B.false) = false\nVERIFYSIG (just B.true) = true\nVERIFYSIG nothing = error boolean\n\n\nBUILTIN : \u2200{n}{w : Weird\u2115 n} \u2192 Builtin\n \u2192 List (ScopedTy n) \u2192 (ts : Tel w) \u2192 VTel w ts \u2192 ScopedTm w\nBUILTIN addInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.+ i'))\nBUILTIN addInteger _ _ _ = error (con integer)\nBUILTIN subtractInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.- i'))\nBUILTIN subtractInteger _ _ _ = error (con integer)\nBUILTIN multiplyInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.* i'))\nBUILTIN multiplyInteger _ _ _ = error (con integer)\nBUILTIN divideInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (div i i')))\nBUILTIN divideInteger _ _ _ = error (con integer)\nBUILTIN quotientInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (quot i i')))\nBUILTIN quotientInteger _ _ _ = error (con integer)\nBUILTIN remainderInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (rem i i')))\nBUILTIN remainderInteger _ _ _ = error (con integer)\nBUILTIN modInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (mod i i')))\nBUILTIN modInteger _ _ _ = error (con integer)\n-- Int -> Int -> Bool\nBUILTIN lessThanInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i'), tt) =\n decIf (i ? i') true false\nBUILTIN greaterThanInteger _ _ _ = error boolean\nBUILTIN greaterThanEqualsInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (i \u2265? i') true false\nBUILTIN greaterThanEqualsInteger _ _ _ = error boolean\nBUILTIN equalsInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (i I.\u225f i') true false\nBUILTIN equalsInteger _ _ _ = error boolean\n-- BS -> BS -> BS\nBUILTIN concatenate _ (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) = con (bytestring (append b b'))\nBUILTIN concatenate _ _ _ = error (con bytestring)\n-- Int -> BS -> BS\nBUILTIN takeByteString _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) = con (bytestring (take i b))\nBUILTIN takeByteString _ _ _ = error (con bytestring)\nBUILTIN dropByteString _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) = con (bytestring (drop i b))\nBUILTIN dropByteString _ _ _ = error (con bytestring)\n-- BS -> BS\nBUILTIN sha2-256 _ (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA2-256 b))\nBUILTIN sha2-256 _ _ _ = error (con bytestring)\nBUILTIN sha3-256 _ (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA3-256 b))\nBUILTIN sha3-256 _ _ _ = error (con bytestring)\nBUILTIN verifySignature _ (_ \u2237 _ \u2237 _ \u2237 []) (V-con (bytestring k) , V-con (bytestring d) , V-con (bytestring c) , tt) = VERIFYSIG (verifySig k d c)\nBUILTIN verifySignature _ _ _ = error (con bytestring)\n-- Int -> Int\nBUILTIN equalsByteString _ (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n B.if equals b b' then true else false\nBUILTIN equalsByteString _ _ _ = error boolean\n\ndata _\u2014\u2192_ {n}{w : Weird\u2115 n} : ScopedTm w \u2192 ScopedTm w \u2192 Set where\n \u03be-\u00b7\u2081 : {L L' M : ScopedTm w} \u2192 L \u2014\u2192 L' \u2192 L \u00b7 M \u2014\u2192 L' \u00b7 M\n \u03be-\u00b7\u2082 : {L M M' : ScopedTm w} \u2192 Value L \u2192 M \u2014\u2192 M' \u2192 L \u00b7 M \u2014\u2192 L \u00b7 M'\n \u03be-\u00b7\u22c6 : {L L' : ScopedTm w}{A : ScopedTy n} \u2192 L \u2014\u2192 L' \u2192 L \u00b7\u22c6 A \u2014\u2192 L' \u00b7\u22c6 A\n \u03b2-\u019b : \u2200{x}{A : ScopedTy n}{L : ScopedTm (S w)}{M : ScopedTm w}\n \u2192 (\u019b x A L) \u00b7 M \u2014\u2192 (L [ M ])\n \u03b2-\u039b : \u2200{x K}{L : ScopedTm (T w)}{A : ScopedTy n}\n \u2192 (\u039b x K L) \u00b7\u22c6 A \u2014\u2192 (L [ A ]\u22c6)\n \u03be-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {tel : Tel w}\n {telA : Tel w}\n (vs : VTel w telA)\n {t t' : ScopedTm w}\n \u2192 t \u2014\u2192 t'\n \u2192 (telB : List (ScopedTm w))\n-- a proof that tel = telA ++ t ++ telB\n \u2192 builtin b As tel\n \u2014\u2192\n builtin b As (telA ++ Data.List.[ t' ] ++ telB)\n \u03b2-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : Tel w}\n (vs : VTel w ts)\n \u2192 builtin b As ts \u2014\u2192 BUILTIN b As ts vs\n sat-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {t : ScopedTm w}\n \u2192 builtin b As ts \u00b7 t \u2014\u2192 builtin b As (ts ++ Data.List.[ t ])\n\n \u03be-unwrap : {t t' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 unwrap t \u2014\u2192 unwrap t'\n \u03b2-wrap : {A B : ScopedTy n}{t : ScopedTm w} \u2192 unwrap (wrap A B t) \u2014\u2192 t\n\\end{code}\n\n\\begin{code}\ndata _\u2014\u2192\u22c6_ {n}{w : Weird\u2115 n} : ScopedTm w \u2192 ScopedTm w \u2192 Set where\n refl : {t : ScopedTm w} \u2192 t \u2014\u2192\u22c6 t\n trans : {t t' t'' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 t' \u2014\u2192\u22c6 t'' \u2192 t \u2014\u2192\u22c6 t''\n\\end{code}\n\n\\begin{code}\ndata Progress (t : ScopedTm Z) : Set where\n step : \u2200{t'} \u2192 t \u2014\u2192 t' \u2192 Progress t\n done : Value t \u2192 Progress t\n error : Error t \u2192 Progress t\n \ndata TelProgress {n}{w : Weird\u2115 n} : Tel w \u2192 Set where\n done : (tel : Tel w)(vtel : VTel w tel) \u2192 TelProgress tel\n step : (tel : Tel w)(telA : Tel w)(vtelA : VTel w telA)\n \u2192 {t t' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 (telB : Tel w) \u2192 TelProgress tel\n error : (tel : Tel w)(telA : Tel w)(vtelA : VTel w telA){t : ScopedTm w}\n \u2192 Error t \u2192 (telB : Tel w) \u2192 TelProgress tel\n\\end{code}\n\n\\begin{code}\nprogress\u00b7 : \u2200{t : ScopedTm Z} \u2192 Progress t \u2192 (u : ScopedTm Z) \u2192 Progress (t \u00b7 u)\nprogress\u00b7 (step p) u = step (\u03be-\u00b7\u2081 p)\nprogress\u00b7 (done (V-\u019b x A t)) u = step \u03b2-\u019b\nprogress\u00b7 (done (V-\u039b x p)) u = error E-\u039b\u00b7\nprogress\u00b7 (done (V-con tcn)) u = error E-con\u00b7\nprogress\u00b7 (done (V-wrap A B t)) u = error E-wrap\u00b7\nprogress\u00b7 (done (V-builtin b As ts)) u = step sat-builtin\nprogress\u00b7 (error e) u = error (E-\u00b7\u2081 e)\n\nprogress\u00b7\u22c6 : \u2200{t : ScopedTm Z} \u2192 Progress t \u2192 (A : ScopedTy 0)\n \u2192 Progress (t \u00b7\u22c6 A)\nprogress\u00b7\u22c6 (step p) A = step (\u03be-\u00b7\u22c6 p)\nprogress\u00b7\u22c6 (done (V-\u019b x B t)) A = error E-\u019b\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-\u039b x p)) A = step \u03b2-\u039b\nprogress\u00b7\u22c6 (done (V-con tcn)) A = error E-con\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-wrap pat arg t)) A = error E-wrap\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-builtin b As ts)) A = error E-builtin\u00b7\u22c6\nprogress\u00b7\u22c6 (error e) A = error (E-\u00b7\u22c6 e)\n\nprogress-unwrap : \u2200{t : ScopedTm Z} \u2192 Progress t \u2192 Progress (unwrap t)\nprogress-unwrap (step p) = step (\u03be-unwrap p)\nprogress-unwrap (done (V-\u019b x A t)) = error E-\u019bunwrap\nprogress-unwrap (done (V-\u039b x p)) = error E-\u039bunwrap\nprogress-unwrap (done (V-con tcn)) = error E-conunwrap\nprogress-unwrap (done (V-wrap A B t)) = step \u03b2-wrap\nprogress-unwrap (done (V-builtin b As ts)) = error E-builtinunwrap\nprogress-unwrap (error e) = error (E-unwrap e)\n\nprogress-builtin : \u2200 bn \u2192 (As : List (ScopedTy 0)) (tel : Tel Z)\n \u2192 TelProgress tel \u2192 Progress (builtin bn As tel)\nprogress-builtin bn As tel p with arity bn N.\u225f Data.List.length tel\nprogress-builtin bn As tel (done .tel vtel) | yes p =\n step (\u03b2-builtin vtel)\nprogress-builtin bn As tel (step .tel telA vtelA x telB) | yes p =\n step (\u03be-builtin vtelA x telB)\nprogress-builtin bn As tel (error .tel telA vtelA x telB) | yes p =\n error (E-builtin x)\nprogress-builtin bn As tel p | no \u00acp = done (V-builtin bn As tel)\n\nprogressTelCons : {t : ScopedTm Z} \u2192 Progress t \u2192 {tel : Tel Z}\n \u2192 TelProgress tel \u2192 TelProgress (t \u2237 tel)\nprogressTelCons {t}(step p){tel} q = step (t \u2237 tel) [] tt p tel\nprogressTelCons (done v) (done tel vtel) = done (_ \u2237 tel) (v , vtel)\nprogressTelCons (done v) (step tel telA vtelA p telB) =\n step (_ \u2237 tel) (_ \u2237 telA) (v , vtelA) p telB\nprogressTelCons (done v) (error tel telA vtelA p telB) =\n error (_ \u2237 tel) (_ \u2237 telA) (v , vtelA) p telB\nprogressTelCons {t}(error e){tel} q = error (t \u2237 tel) [] tt e tel\n\nprogress : (t : ScopedTm Z) \u2192 Progress t\n\nprogressTel : (tel : Tel Z) \u2192 TelProgress {w = Z} tel\nprogressTel [] = done [] tt\nprogressTel (t \u2237 tel) = progressTelCons (progress t) (progressTel tel)\n\nprogress (` ())\nprogress (\u039b x K t) = done (V-\u039b x t)\nprogress (t \u00b7\u22c6 A) = progress\u00b7\u22c6 (progress t) A\nprogress (\u019b x A t) = done (V-\u019b x A t)\nprogress (t \u00b7 u) = progress\u00b7 (progress t) u\nprogress (con c) = done (V-con c)\nprogress (error A) = error (E-error A)\nprogress (builtin b As ts) = progress-builtin b As ts (progressTel ts)\nprogress (wrap A B t) = done (V-wrap A B t)\nprogress (unwrap t) = progress-unwrap (progress t)\n\\end{code}\n\n\\begin{code}\nopen import Data.Nat\n\nSteps : ScopedTm Z \u2192 Set\nSteps t = \u03a3 (ScopedTm Z) \u03bb t' \u2192 t \u2014\u2192\u22c6 t' \u00d7 (Maybe (Value t') \u228e Error t')\n\nrun\u2014\u2192 : {t t' : ScopedTm Z} \u2192 t \u2014\u2192 t' \u2192 Steps t' \u2192 Steps t\nrun\u2014\u2192 p (t' , ps , q) = _ , ((trans p ps) , q)\n\nrun : (t : ScopedTm Z) \u2192 \u2115 \u2192 Steps t\nrunProg : \u2115 \u2192 {t : ScopedTm Z} \u2192 Progress t \u2192 Steps t\n\nrun t 0 = t , (refl , inl nothing) -- out of fuel\nrun t (suc n) = runProg n (progress t)\n\nrunProg n (step {t' = t'} p) = run\u2014\u2192 p (run t' n)\nrunProg n (done V) = _ , refl , inl (just V)\nrunProg n (error e) = _ , refl , inr e \n\n\\end{code}\n","avg_line_length":42.0426229508,"max_line_length":146,"alphanum_fraction":0.5749044685} +{"size":8609,"ext":"lagda","lang":"Literate Agda","max_stars_count":1299.0,"content":"\\begin{code}\nmodule Raw where\n\\end{code}\n\n\\begin{code}\nopen import Data.String using (String;_++_)\nopen import Data.Nat using (\u2115;_\u225f_)\nopen import Data.Integer using (\u2124)\nopen import Data.Integer.Show\nopen import Data.Unit using (\u22a4)\n\nopen import Builtin\nopen import Utils\n\nopen import Relation.Nullary using (Reflects;Dec;of\u02b8;of\u207f;_because_;yes;no)\nopen import Relation.Nullary.Decidable\nopen import Relation.Binary.PropositionalEquality using (_\u2261_;cong;cong\u2082;refl)\nopen import Data.Bool using (Bool;false;true)\n\\end{code}\n\nThe raw un-scope-checked and un-type-checked syntax\n\n\\begin{code}\ndata RawTy : Set\nopen import Builtin.Constant.Type \u22a4 (\u03bb _ \u2192 RawTy)\n\ndata RawTyCon : Set\n\ndata RawTy where\n ` : \u2115 \u2192 RawTy\n _\u21d2_ : RawTy \u2192 RawTy \u2192 RawTy\n \u03a0 : Kind \u2192 RawTy \u2192 RawTy\n \u019b : Kind \u2192 RawTy \u2192 RawTy\n _\u00b7_ : RawTy \u2192 RawTy \u2192 RawTy\n con : RawTyCon \u2192 RawTy\n \u03bc : RawTy \u2192 RawTy \u2192 RawTy\n\n{-# COMPILE GHC RawTy = data RType (RTyVar | RTyFun | RTyPi | RTyLambda | RTyApp | RTyCon | RTyMu) #-}\n\n{-# FOREIGN GHC import Raw #-}\n\ndata RawTyCon where\n integer : RawTyCon\n bytestring : RawTyCon\n string : RawTyCon\n unit : RawTyCon\n bool : RawTyCon\n list : RawTy \u2192 RawTyCon\n pair : RawTy \u2192 RawTy \u2192 RawTyCon\n Data : RawTyCon\n\n{-# COMPILE GHC RawTyCon = data RTyCon (RTyConInt | RTyConBS | RTyConStr | RTyConUnit | RTyConBool | RTyConList | RTyConPair | RTyConData) #-}\n\ndata RawTm : Set where\n ` : \u2115 \u2192 RawTm\n \u039b : Kind \u2192 RawTm \u2192 RawTm\n _\u00b7\u22c6_ : RawTm \u2192 RawTy \u2192 RawTm\n \u019b : RawTy \u2192 RawTm \u2192 RawTm\n _\u00b7_ : RawTm \u2192 RawTm \u2192 RawTm\n con : TermCon \u2192 RawTm\n error : RawTy \u2192 RawTm\n builtin : Builtin \u2192 RawTm\n wrap : RawTy \u2192 RawTy \u2192 RawTm \u2192 RawTm\n unwrap : RawTm \u2192 RawTm\n\n{-# COMPILE GHC RawTm = data RTerm (RVar | RTLambda | RTApp | RLambda | RApp | RCon | RError | RBuiltin | RWrap | RUnWrap) #-}\n\n-- \u03b1 equivalence\n\n-- we don't have a decicable equality instance for bytestring, so I\n-- converted this to bool for now\n\ndecRTyCon : (C C' : RawTyCon) \u2192 Bool\ndecRTyCon integer integer = true\ndecRTyCon bytestring bytestring = true\ndecRTyCon string string = true\ndecRTyCon unit unit = true\ndecRTyCon bool bool = true\ndecRTyCon _ _ = false\n\ndecTermCon : (C C' : TermCon) \u2192 Bool\ndecTermCon (integer i) (integer i') with i Data.Integer.\u225f i'\n... | yes p = true\n... | no \u00acp = false\ndecTermCon (bytestring b) (bytestring b') with equals b b'\ndecTermCon (bytestring b) (bytestring b') | false = false\ndecTermCon (bytestring b) (bytestring b') | true = true\ndecTermCon (string s) (string s') with s Data.String.\u225f s'\n... | yes p = true\n... | no \u00acp = false\ndecTermCon (bool b) (bool b') with b Data.Bool.\u225f b'\n... | yes p = true\n... | no \u00acp = false\ndecTermCon unit unit = true\ndecTermCon _ _ = false\n\ndecBuiltin : (b b' : Builtin) \u2192 Bool\ndecBuiltin addInteger addInteger = true\ndecBuiltin subtractInteger subtractInteger = true\ndecBuiltin multiplyInteger multiplyInteger = true\ndecBuiltin divideInteger divideInteger = true\ndecBuiltin quotientInteger quotientInteger = true\ndecBuiltin remainderInteger remainderInteger = true\ndecBuiltin modInteger modInteger = true\ndecBuiltin lessThanInteger lessThanInteger = true\ndecBuiltin lessThanEqualsInteger lessThanEqualsInteger = true\ndecBuiltin equalsInteger equalsInteger = true\ndecBuiltin appendByteString appendByteString = true\ndecBuiltin sha2-256 sha2-256 = true\ndecBuiltin sha3-256 sha3-256 = true\ndecBuiltin verifySignature verifySignature = true\ndecBuiltin equalsByteString equalsByteString = true\ndecBuiltin appendString appendString = true\ndecBuiltin trace trace = true\ndecBuiltin _ _ = false\n\ndecRKi : (K K' : Kind) \u2192 Bool\ndecRKi * * = true\ndecRKi * (K' \u21d2 J') = false\ndecRKi (K \u21d2 J) * = false\ndecRKi (K \u21d2 J) (K' \u21d2 J') with decRKi K K'\ndecRKi (K \u21d2 J) (K' \u21d2 J') | true with decRKi J J'\ndecRKi (K \u21d2 J) (K' \u21d2 J') | true | true = true\ndecRKi (K \u21d2 J) (K' \u21d2 J') | true | false = false\ndecRKi (K \u21d2 J) (K' \u21d2 J') | false = false\n\ndecRTy : (A A' : RawTy) \u2192 Bool\ndecRTy (` x) (` x') with x \u225f x'\n... | yes _ = true\n... | no _ = false\ndecRTy (` x) (A' \u21d2 B') = false\ndecRTy (` x) (\u03a0 K' A') = false\ndecRTy (` x) (\u019b K' A') = false\ndecRTy (` x) (A' \u00b7 A'') = false\ndecRTy (` x) (con c') = false\ndecRTy (` x) (\u03bc A' B') = false\ndecRTy (A \u21d2 B) (` x') = false\ndecRTy (A \u21d2 B) (A' \u21d2 B') with decRTy A A'\n... | false = false\n... | true with decRTy B B'\n... | false = false\n... | true = true\ndecRTy (A \u21d2 B) (\u03a0 K' A') = false\ndecRTy (A \u21d2 B) (\u019b K' A') = false\ndecRTy (A \u21d2 B) (A' \u00b7 A'') = false\ndecRTy (A \u21d2 B) (con c') = false\ndecRTy (A \u21d2 B) (\u03bc A' B') = false\ndecRTy (\u03a0 K A) (` x') = false\ndecRTy (\u03a0 K A) (A' \u21d2 B') = false\ndecRTy (\u03a0 K A) (\u03a0 K' A') with decRKi K K'\n... | false = false\n... | true with decRTy A A'\n... | false = false\n... | true = true\ndecRTy (\u03a0 K A) (\u019b K' A') = false\ndecRTy (\u03a0 K A) (A' \u00b7 A'') = false\ndecRTy (\u03a0 K A) (con c') = false\ndecRTy (\u03a0 K A) (\u03bc A' B') = false\ndecRTy (\u019b K A) (` x') = false\ndecRTy (\u019b K A) (A' \u21d2 B') = false\ndecRTy (\u019b K A) (\u03a0 K' A') = false\ndecRTy (\u019b K A) (\u019b K' A') with decRKi K K'\n... | false = false\n... | true with decRTy A A'\n... | false = false\n... | true = true\ndecRTy (\u019b K A) (A' \u00b7 A'') = false\ndecRTy (\u019b K A) (con c') = false\ndecRTy (\u019b K A) (\u03bc A' B') = false\ndecRTy (A \u00b7 B) (` x') = false\ndecRTy (A \u00b7 B) (A' \u21d2 B') = false\ndecRTy (A \u00b7 B) (\u03a0 K' A') = false\ndecRTy (A \u00b7 B) (\u019b K' A') = false\ndecRTy (A \u00b7 B) (A' \u00b7 B') with decRTy A A'\n... | false = false\n... | true with decRTy B B'\n... | false = false\n... | true = true\ndecRTy (A \u00b7 B) (con c') = false\ndecRTy (A \u00b7 B) (\u03bc A' B') = false\ndecRTy (con c) (` x') = false\ndecRTy (con c) (A' \u21d2 B') = false\ndecRTy (con c) (\u03a0 K' A') = false\ndecRTy (con c) (\u019b K' A') = false\ndecRTy (con c) (A' \u00b7 A'') = false\ndecRTy (con c) (con c') with decRTyCon c c'\n... | false = false\n... | true = true\ndecRTy (con c) (\u03bc A' B') = false\ndecRTy (\u03bc A B) (` x') = false\ndecRTy (\u03bc A B) (A' \u21d2 B') = false\ndecRTy (\u03bc A B) (\u03a0 K' A') = false\ndecRTy (\u03bc A B) (\u019b K' A') = false\ndecRTy (\u03bc A B) (A' \u00b7 A'') = false\ndecRTy (\u03bc A B) (con c') = false\ndecRTy (\u03bc A B) (\u03bc A' B') with decRTy A A'\n... | false = false\n... | true with decRTy B B'\n... | false = false\n... | true = true\n\ndecRTm : (t t' : RawTm) \u2192 Bool\ndecRTm (` x) (` x') with x \u225f x'\ndecRTm (` x) (` x') | yes _ = true\ndecRTm (` x) (` x') | no _ = false\ndecRTm (\u039b K t) (\u039b K' t') with decRKi K K'\n... | false = false\n... | true with decRTm t t'\n... | true = true\n... | false = false\ndecRTm (t \u00b7\u22c6 A) (t' \u00b7\u22c6 A') with decRTm t t'\n... | false = false\n... | true with decRTy A A'\n... | true = true\n... | false = false\ndecRTm (\u019b A t) (\u019b A' t') with decRTy A A'\n... | false = false\n... | true with decRTm t t'\n... | false = false\n... | true = true\ndecRTm (t \u00b7 u) (t' \u00b7 u') with decRTm t t'\n... | false = false\n... | true with decRTm u u'\n... | false = false\n... | true = true\ndecRTm (con c) (con c') = decTermCon c c'\ndecRTm (error A) (error A') with decRTy A A'\n... | true = true\n... | false = false\ndecRTm (builtin b) (builtin b') = decBuiltin b b'\ndecRTm (wrap pat arg t) (wrap pat' arg' t') with decRTy pat pat'\n... | false = false\n... | true with decRTy arg arg'\n... | false = false\n... | true with decRTm t t'\n... | false = false\n... | true = true\ndecRTm (unwrap t) (unwrap t') with decRTm t t'\ndecRTm (unwrap t) (unwrap t') | true = true\ndecRTm (unwrap t) (unwrap t') | false = false\ndecRTm _ _ = false\n\n-- We have to different approaches to de Bruijn terms.\n-- one counts type and term binders separately the other counts them together\n\nrawTyPrinter : RawTy \u2192 String\nrawTyPrinter (` x) = Data.Integer.Show.show (\u2124.pos x)\nrawTyPrinter (A \u21d2 B) = \"(\" ++ rawTyPrinter A ++ \"\u21d2\" ++ rawTyPrinter B ++ \")\"\nrawTyPrinter (\u03a0 K A) = \"(\u03a0\" ++ \"kind\" ++ rawTyPrinter A ++ \")\"\nrawTyPrinter (\u019b K A) = \"(\u019b\" ++ \"kind\" ++ rawTyPrinter A ++ \")\"\nrawTyPrinter (A \u00b7 B) = \"(\" ++ rawTyPrinter A ++ \"\u00b7\" ++ rawTyPrinter B ++ \")\"\nrawTyPrinter (con c) = \"(con)\"\nrawTyPrinter (\u03bc A B) = \"(\u03bc\" ++ rawTyPrinter A ++ rawTyPrinter B ++ \")\"\n\nrawPrinter : RawTm \u2192 String\nrawPrinter (` x) = Data.Integer.Show.show (\u2124.pos x)\nrawPrinter (\u039b K t) = \"(\" ++ \"\u039b\" ++ \"kind\" ++ rawPrinter t ++ \")\"\nrawPrinter (t \u00b7\u22c6 A) = \"(\" ++ rawPrinter t ++ \"\u00b7\u22c6\" ++ rawTyPrinter A ++ \")\"\nrawPrinter (\u019b A t) = \"(\" ++ \"\u019b\" ++ rawTyPrinter A ++ rawPrinter t ++ \")\"\nrawPrinter (t \u00b7 u) = \"(\" ++ rawPrinter t ++ \"\u00b7\" ++ rawPrinter u ++ \")\"\nrawPrinter (con c) = \"(con)\"\nrawPrinter (error A) = \"(error\" ++ rawTyPrinter A ++ \")\"\nrawPrinter (builtin b) = \"(builtin)\"\nrawPrinter (wrap pat arg t) = \"(wrap\" ++ \")\"\nrawPrinter (unwrap t) = \"(unwrap\" ++ rawPrinter t ++ \")\"\n\\end{code}\n","avg_line_length":32.2434456929,"max_line_length":142,"alphanum_fraction":0.6033221048} +{"size":1602,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule iso-exercise where\n\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; cong\u2082)\nopen Eq.\u2261-Reasoning\nopen import plfa.Isomorphism using (_\u2243_)\nopen import Data.List using (List; []; _\u2237_)\nopen import Data.List.All using (All; []; _\u2237_)\nopen import Data.List.Any using (Any; here; there)\nopen import Data.List.Membership.Propositional using (_\u2208_)\nopen import Function using (_\u2218_)\n\npostulate\n extensionality : \u2200 {A : Set} {B : A \u2192 Set} {C : A \u2192 Set} {f g : {x : A} \u2192 B x \u2192 C x}\n \u2192 (\u2200 {x : A} (bx : B x) \u2192 f {x} bx \u2261 g {x} bx)\n --------------------------------------------\n \u2192 (\u03bb {x} \u2192 f {x}) \u2261 (\u03bb {x} \u2192 g {x})\n\nf : \u2200 {A : Set} {P : A \u2192 Set} {xs : List A} \u2192 All P xs \u2192 (\u2200 {x} -> x \u2208 xs -> P x)\nf [] ()\nf (px \u2237 pxs) (here refl) = px\nf (px \u2237 pxs) (there x\u2208xs) = f pxs x\u2208xs\n\ng : \u2200 {A : Set} {P : A \u2192 Set} {xs : List A} \u2192 (\u2200 {x} -> x \u2208 xs -> P x) \u2192 All P xs\ng {xs = []} h = []\ng {xs = x \u2237 xs} h = h {x} (here refl) \u2237 g (h \u2218 there)\n\ngf : \u2200 {A : Set} {P : A \u2192 Set} {xs : List A} \u2192 \u2200 (pxs : All P xs) \u2192 g (f pxs) \u2261 pxs\ngf [] = refl\ngf (px \u2237 pxs) = Eq.cong\u2082 _\u2237_ refl (gf pxs)\n\nfg : \u2200 {A : Set} {P : A \u2192 Set} {xs : List A}\n \u2192 \u2200 (h : \u2200 {x} -> x \u2208 xs -> P x) \u2192 \u2200 {x} (x\u2208 : x \u2208 xs) \u2192 f (g h) {x} x\u2208 \u2261 h {x} x\u2208\nfg {xs = []} h ()\nfg {xs = x \u2237 xs} h (here refl) = refl\nfg {xs = x \u2237 xs} h (there x\u2208xs) = fg (h \u2218 there) x\u2208xs\n\nlemma : \u2200 {A : Set} {P : A \u2192 Set} {xs : List A} \u2192 All P xs \u2243 (\u2200 {x} -> x \u2208 xs -> P x)\nlemma =\n record\n { to = f\n ; from = g\n ; from\u2218to = gf\n ; to\u2218from = extensionality \u2218 fg\n }\n\n\\end{code}\n","avg_line_length":32.693877551,"max_line_length":88,"alphanum_fraction":0.4694132335} +{"size":13997,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Untyped.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Untyped\nopen import Untyped.RenamingSubstitution\nopen import Builtin\nopen import Agda.Builtin.String using (primStringFromList; primStringAppend)\nopen import Data.Bool using (Bool;true;false)\nopen import Data.Nat using (\u2115;suc;zero;_<\u2034_;_\u2264\u2034_;\u2264\u2034-refl;\u2264\u2034-step)\nopen import Data.Integer using (_+_;_-_;_*_;\u2223_\u2223;_? j) plc_true plc_false\nBUILTIN greaterThanEqualsInteger (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer j) , tt) =\n decIf (i I\u2265? j) plc_true plc_false\nBUILTIN equalsInteger (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer j) , tt) =\n decIf (i \u225f j) plc_true plc_false\nBUILTIN concatenate (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n con (bytestring (concat b b'))\nBUILTIN takeByteString (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) =\n con (bytestring (take i b))\nBUILTIN dropByteString (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) =\n con (bytestring (drop i b))\nBUILTIN sha2-256 (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA2-256 b))\nBUILTIN sha3-256 (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA3-256 b))\nBUILTIN verifySignature (_ \u2237 _ \u2237 _ \u2237 []) (V-con (bytestring k) , V-con (bytestring d) , V-con (bytestring c) , tt) = VERIFYSIG (verifySig k d c)\nBUILTIN equalsByteString (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n con (bool (equals b b'))\nBUILTIN ifThenElse (_ \u2237 _ \u2237 t \u2237 _ \u2237 []) (_ , V-con (bool true) , vt , _ , tt) = t\nBUILTIN ifThenElse (_ \u2237 _ \u2237 _ \u2237 u \u2237 []) (_ , V-con (bool false) , _ , vu , tt) = u\nBUILTIN charToString (_ \u2237 []) (V-con (char c) , tt) = con (string (primStringFromList List.[ c ]))\nBUILTIN append (_ \u2237 _ \u2237 []) (V-con (string s) , V-con (string t) , tt) =\n con (string (primStringAppend s t))\nBUILTIN trace (_ \u2237 []) (V-con (string s) , tt) = con (Debug.trace s unit)\nBUILTIN _ _ _ = error\n\ndata ProgTel {l n}(tel : Tel l n) : Set where\n done : VTel l n tel \u2192 ProgTel tel\n step : \u2200{tel'} \u2192 tel \u2014\u2192T tel' \u2192 ProgTel tel\n error : Any Error tel \u2192 ProgTel tel\n\ndata Progress {n}(M : n \u22a2) : Set where\n step : \u2200{N}\n \u2192 M \u2014\u2192 N\n -------------\n \u2192 Progress M\n done :\n Value M\n ----------\n \u2192 Progress M\n error :\n Error M\n -------\n \u2192 Progress M\n\nprogress-\u00b7V : \u2200{n}\n \u2192 {t : n \u22a2} \u2192 Value t\n \u2192 {u : n \u22a2} \u2192 Progress u\n \u2192 Progress (t \u00b7 u)\nprogress-\u00b7V (V-con tcn) v = step E-con\nprogress-\u00b7V (V-F v) (step q) = step (\u03be-\u00b7\u2082 v q)\nprogress-\u00b7V (V-F v) (error E-error) = step (E-\u00b7\u2082 v)\nprogress-\u00b7V (V-F (V-\u019b t)) (done v) = step (\u03b2-\u019b v)\nprogress-\u00b7V (V-F (V-builtin b ts p)) (done v) = step (sat-builtin v p)\n\nprogress-\u00b7 : \u2200{n}\n \u2192 {t : n \u22a2} \u2192 Progress t\n \u2192 {u : n \u22a2} \u2192 Progress u\n \u2192 Progress (t \u00b7 u)\nprogress-\u00b7 (done v) q = progress-\u00b7V v q\nprogress-\u00b7 (step p) q = step (\u03be-\u00b7\u2081 p)\nprogress-\u00b7 (error E-error) q = step E-\u00b7\u2081\n\nprogress : (t : 0 \u22a2) \u2192 Progress t\n\nprogressTel : \u2200{l}(tel : Tel l 0) \u2192 ProgTel tel\n\nprogressTel [] = done tt\nprogressTel (t \u2237 ts) with progress t\nprogressTel (t \u2237 ts) | step p = step (here p)\nprogressTel (t \u2237 ts) | done v with progressTel ts\nprogressTel (t \u2237 ts) | done v | done vs = done (v , vs)\nprogressTel (t \u2237 ts) | done v | step p = step (there v p)\nprogressTel (t \u2237 ts) | done v | error e = error (there v e)\nprogressTel (t \u2237 ts) | error e = error (here e)\n\nprogress (` ())\nprogress (\u019b t) = done (V-F (V-\u019b t))\nprogress (t \u00b7 u) = progress-\u00b7 (progress t) (progress u)\nprogress (con tcn) = done (V-con tcn)\nprogress (builtin b \u2264\u2034-refl ts) with progressTel ts\nprogress (builtin b \u2264\u2034-refl ts) | done vs = step (\u03b2-builtin ts vs)\nprogress (builtin b \u2264\u2034-refl ts) | step p = step (\u03be-builtin b p)\nprogress (builtin b \u2264\u2034-refl ts) | error p = step (E-builtin b ts p)\nprogress (builtin b (\u2264\u2034-step p) ts) = done (V-F (V-builtin b ts p))\nprogress error = error E-error\n\\end{code}\n\n\\begin{code}\nrun : \u2200(t : 0 \u22a2) \u2192 \u2115\n \u2192 \u03a3 (0 \u22a2) \u03bb t' \u2192 t \u2014\u2192\u22c6 t' \u00d7 (Maybe (Value t') \u228e Error t')\nrun t 0 = t , refl , inl nothing\nrun t (suc n) with progress t\nrun t (suc n) | done vt = t , refl , inl (just vt)\nrun t (suc n) | error et = t , refl , inr et\nrun t (suc n) | step {N = t'} p with run t' n\nrun t (suc n) | step p | t'' , q , mvt'' = t'' , trans\u2014\u2192\u22c6 p q , mvt''\n\\end{code}\n\n\\begin{code}\nopen import Data.Empty\nopen import Relation.Nullary\n\n-- a value cannot make progress\nval-red : \u2200{n}{t : n \u22a2} \u2192 Value t \u2192 \u00ac (\u03a3 (n \u22a2) (t \u2014\u2192_))\nval-red (V-F (V-\u019b t)) (t' , ())\nval-red (V-F (V-builtin b ts p)) (t' , ())\nval-red (V-con tcn) (t' , ())\n\nval-err : \u2200{n}{t : n \u22a2} \u2192 Value t \u2192 \u00ac (Error t)\nval-err (V-con tcn) ()\nval-err (V-F (V-\u019b t)) ()\nval-err (V-F (V-builtin b ts p)) ()\n\nerr-red : \u2200{n}{t : n \u22a2} \u2192 Error t \u2192 \u00ac (\u03a3 (n \u22a2) (t \u2014\u2192_))\nerr-red E-error (_ , ())\n\nerrT-redT : \u2200{m n}{ts : Tel m n} \u2192 Any Error ts \u2192 \u00ac (\u03a3 (Tel m n) (ts \u2014\u2192T_))\nerrT-redT (here p) (.(_ \u2237 _) , here q) = err-red p (_ , q)\nerrT-redT (here p) (.(_ \u2237 _) , there v q) = val-err v p\nerrT-redT (there v p) (.(_ \u2237 _) , here q) = val-red v (_ , q)\nerrT-redT (there v p) (.(_ \u2237 _) , there w q) = errT-redT p (_ , q)\n\nvalT-errT : \u2200{m n}{ts : Tel m n} \u2192 VTel m n ts \u2192 \u00ac (Any Error ts)\nvalT-errT {ts = t \u2237 ts} (v , vs) (here p) = val-err v p\nvalT-errT {ts = t \u2237 ts} (v , vs) (there w p) = valT-errT vs p\n\nvalT-redT : \u2200{m n}{ts : Tel m n} \u2192 VTel m n ts \u2192 \u00ac (\u03a3 (Tel m n) (ts \u2014\u2192T_))\nvalT-redT {ts = []} _ (ts' , ())\nvalT-redT {ts = t \u2237 ts} (v , vs) (._ , here p) = val-red v (_ , p)\nvalT-redT {ts = t \u2237 ts} (v , vs) (._ , there v' p) = valT-redT vs (_ , p)\n\nvalUniq : \u2200{n}{t : n \u22a2}(v v' : Value t) \u2192 v \u2261 v'\nvalUniq (V-F (V-\u019b t)) (V-F (V-\u019b .t)) = refl\nvalUniq (V-F (V-builtin b ts p)) (V-F (V-builtin .b .ts .p)) = refl\nvalUniq (V-con tcn) (V-con .tcn) = refl\n\nvalTUniq : \u2200{m n}{ts : Tel m n}(vs vs' : VTel m n ts) \u2192 vs \u2261 vs'\nvalTUniq {ts = []} _ _ = refl\nvalTUniq {ts = x \u2237 ts} (v , vs) (v' , vs') =\n cong\u2082 _,_ (valUniq v v') (valTUniq vs vs')\n\ndetT : \u2200{m n}{ts ts' ts'' : Tel m n}\n \u2192 (p : ts \u2014\u2192T ts')(q : ts \u2014\u2192T ts'') \u2192 ts' \u2261 ts''\n\ndet : \u2200{n}{t t' t'' : n \u22a2}(p : t \u2014\u2192 t')(q : t \u2014\u2192 t'') \u2192 t' \u2261 t''\n\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2081 q) = cong (_\u00b7 _) (det p q)\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2082 v q) = \u22a5-elim (val-red (V-F v) (_ , p))\ndet (\u03be-\u00b7\u2081 p) (E-\u00b7\u2082 v) = \u22a5-elim (val-red (V-F v) (_ , p))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2081 q) = \u22a5-elim (val-red (V-F v) (_ , q))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2082 w q) = cong (_ \u00b7_) (det p q)\ndet (\u03be-\u00b7\u2082 v p) (\u03b2-\u019b w) = \u22a5-elim (val-red w (_ , p))\ndet (\u03be-\u00b7\u2082 v p) (sat-builtin w q) = \u22a5-elim (val-red w (_ , p))\ndet (\u03be-\u00b7\u2082 () p) E-con\ndet (\u03b2-\u019b v) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val-red v (_ , q))\ndet (\u03b2-\u019b v) (\u03b2-\u019b w) = refl\ndet (\u03b2-\u019b (V-F ())) (E-\u00b7\u2082 v)\ndet (E-\u00b7\u2082 v) (\u03b2-\u019b (V-F ()))\ndet (\u03be-builtin b p) (\u03be-builtin .b q) = cong (builtin b \u2264\u2034-refl) (detT p q)\ndet (\u03be-builtin b p) (\u03b2-builtin ts vs) = \u22a5-elim (valT-redT vs (_ , p))\ndet (\u03be-builtin b p) (E-builtin .b e q) = \u22a5-elim (errT-redT q (_ , p))\ndet (\u03b2-builtin ts vs) (\u03be-builtin b q) = \u22a5-elim (valT-redT vs (_ , q))\ndet (\u03b2-builtin ts vs) (\u03b2-builtin .ts ws) = cong (BUILTIN _ ts) (valTUniq vs ws)\ndet (\u03b2-builtin ts vs) (E-builtin b v q) = \u22a5-elim (valT-errT vs q)\ndet (sat-builtin v p) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val-red v (_ , q))\ndet (sat-builtin v p) (sat-builtin w .p) = refl\ndet (sat-builtin (V-F ()) p) (E-\u00b7\u2082 w)\ndet E-\u00b7\u2081 E-\u00b7\u2081 = refl\ndet (E-\u00b7\u2082 v) (\u03be-\u00b7\u2081 q) = \u22a5-elim (val-red (V-F v) (_ , q))\ndet (E-\u00b7\u2082 v) (sat-builtin (V-F ()) p)\ndet (E-\u00b7\u2082 v) (E-\u00b7\u2082 w) = refl\ndet (E-\u00b7\u2082 ()) E-con\ndet (E-builtin b ts p) (\u03be-builtin .b q) = \u22a5-elim (errT-redT p (_ , q))\ndet (E-builtin b ts p) (\u03b2-builtin ts vs) = \u22a5-elim (valT-errT vs p)\ndet (E-builtin b ts p) (E-builtin .b w q) = refl\ndet E-con (\u03be-\u00b7\u2082 () q)\ndet E-con (E-\u00b7\u2082 ())\ndet E-con E-con = refl\n\ndetT (here p) (here q) = cong (_\u2237 _) (det p q)\ndetT (here p) (there v q) = \u22a5-elim (val-red v (_ , p))\ndetT (there v p) (here q) = \u22a5-elim (val-red v (_ , q))\ndetT (there v p) (there w q) = cong (_ \u2237_) (detT p q)\n\n-- auxiliary functions\n\nvTel:< : \u2200{l n}\n \u2192 (ts : Tel l n)\n \u2192 VTel l n ts \u2192 (t : n \u22a2)\n \u2192 Value t\n \u2192 VTel (suc l) n (ts :< t)\nvTel:< [] vs t v = v , tt\nvTel:< (t' \u2237 ts) (v' , vs) t v = v' , (vTel:< ts vs t v)\n\n\nvTel++ : \u2200{l l' n}\n \u2192 (ts : Tel l n)\n \u2192 VTel l n ts\n \u2192 (ts' : Tel l' n)\n \u2192 VTel l' n ts'\n \u2192 VTel (l Data.Nat.+ l') n (ts ++ ts')\nvTel++ [] vs ts' vs' = vs'\nvTel++ (t \u2237 ts) (v' , vs) ts' vs' = v' , vTel++ ts vs ts' vs'\n\nanyErr++ : \u2200{l l' n}{ts : Tel l n} \u2192 Any Error ts \u2192 (ts' : Tel l' n) \u2192 VTel l' n ts' \u2192 Any Error (ts' ++ ts)\nanyErr++ p [] _ = p\nanyErr++ p (t' \u2237 ts') (v' , vs') = there v' (anyErr++ p ts' vs')\n\n\u2014\u2192T++ : \u2200{l l' n}{ts' ts'' : Tel l n} \u2192 ts' \u2014\u2192T ts'' \u2192 (ts : Tel l' n) \u2192 VTel l' n ts \u2192 (ts ++ ts') \u2014\u2192T (ts ++ ts'')\n\u2014\u2192T++ p [] vs = p\n\u2014\u2192T++ p (t \u2237 ts) (v , vs) = there v (\u2014\u2192T++ p ts vs)\n\\end{code}\n","avg_line_length":36.9313984169,"max_line_length":144,"alphanum_fraction":0.5246124169} +{"size":28221,"ext":"lagda","lang":"Literate Agda","max_stars_count":1003.0,"content":"---\ntitle : \"Quantitative: Counting Resources in Types\"\nlayout : page\npermalink : \/Quantitative\/\n---\n\nIn previous chapters, we introduced the lambda calculus, with [a formalisation based on named variables][plfa.Lambda] and [an inherently-typed version][plfa.DeBruijn]. This chapter presents a refinement of those approaches, in which we not only care about the types of variables, but also about *how often they're used*.\n\n\n# Imports & Module declaration\n\n\\begin{code}\nopen import Relation.Nullary using (Dec)\nopen import Relation.Binary.PropositionalEquality as Eq using (_\u2261_)\nopen import Algebra.Structures using (module IsSemiring; IsSemiring)\n\\end{code}\n\n\\begin{code}\nmodule qtt.Quantitative\n {Mult : Set}\n (_+_ _*_ : Mult \u2192 Mult \u2192 Mult)\n (0# 1# : Mult)\n (*-+-isSemiring : IsSemiring _\u2261_ _+_ _*_ 0# 1#)\n where\n\\end{code}\n\n\\begin{code}\nopen import Function using (_\u2218_; _|>_)\nopen Eq using (refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning using (begin_; _\u2261\u27e8_\u27e9_; _\u2261\u27e8\u27e9_; _\u220e)\nopen IsSemiring *-+-isSemiring hiding (refl; sym; trans)\n\\end{code}\n\n\n# Syntax\n\n\\begin{code}\ninfix 1 _\u22a2_\ninfix 1 _\u220b_\ninfixl 5 _,_\ninfixl 5 _,_\u2219_\ninfixr 6 [_\u2219_]\u22b8_\ninfixl 7 _+\u0307_\ninfixl 8 _*\u0307_\ninfixl 8 _*\u27e9_\n\\end{code}\n\n\n# Types\n\n\\begin{code}\ndata Type : Set where\n `0 : Type\n [_\u2219_]\u22b8_ : Mult \u2192 Type \u2192 Type \u2192 Type\n `1 : Type\n _\u2297_ : Type \u2192 Type \u2192 Type\n _\u2295_ : Type \u2192 Type \u2192 Type\n\\end{code}\n\n\\begin{code}\n_\u22b8_ : Type \u2192 Type \u2192 Type\nA \u22b8 B = [ 1# \u2219 A ]\u22b8 B\n\\end{code}\n\n# Precontexts\n\n\\begin{code}\ndata Precontext : Set where\n \u2205 : Precontext\n _,_ : Precontext \u2192 Type \u2192 Precontext\n\nvariable \u03b3 \u03b4 : Precontext\n\\end{code}\n\n\\begin{code}\n_ : Precontext\n_ = \u2205 , `0 \u22b8 `0 , `0\n\\end{code}\n\n\n# Variables and the lookup judgment\n\n\\begin{code}\ndata _\u220b_ : Precontext \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u03b3} {A}\n\n ---------\n \u2192 \u03b3 , A \u220b A\n\n S_ : \u2200 {\u03b3} {A B}\n\n \u2192 \u03b3 \u220b A\n ---------\n \u2192 \u03b3 , B \u220b A\n\\end{code}\n\n\n# Contexts\n\n\\begin{code}\ndata Context : Precontext \u2192 Set where\n \u2205 : Context \u2205\n _,_\u2219_ : \u2200 {\u0393} \u2192 Context \u0393 \u2192 Mult \u2192 (A : Type) \u2192 Context (\u0393 , A)\n\nvariable \u0393 \u0394 \u0398 : Context \u03b3\n\\end{code}\n\n\\begin{code}\n_ : Context (\u2205 , `0 \u22b8 `0 , `0)\n_ = \u2205 , 1# \u2219 `0 \u22b8 `0 , 0# \u2219 `0\n\\end{code}\n\n\n# Resources and linear algebra\n\nScaling.\n\n\\begin{code}\n_*\u0307_ : \u2200 {\u03b3} \u2192 Mult \u2192 Context \u03b3 \u2192 Context \u03b3\n\u03c0 *\u0307 \u2205 = \u2205\n\u03c0 *\u0307 (\u0393 , \u03c1 \u2219 A) = \u03c0 *\u0307 \u0393 , \u03c0 * \u03c1 \u2219 A\n\\end{code}\n\nThe 0-vector.\n\n\\begin{code}\n0s : \u2200 {\u03b3} \u2192 Context \u03b3\n0s {\u2205} = \u2205\n0s {\u03b3 , A} = 0s , 0# \u2219 A\n\\end{code}\n\nVector addition.\n\n\\begin{code}\n_+\u0307_ : \u2200 {\u03b3} \u2192 Context \u03b3 \u2192 Context \u03b3 \u2192 Context \u03b3\n\u2205 +\u0307 \u2205 = \u2205\n(\u0393\u2081 , \u03c0\u2081 \u2219 A) +\u0307 (\u0393\u2082 , \u03c0\u2082 \u2219 .A) = \u0393\u2081 +\u0307 \u0393\u2082 , \u03c0\u2081 + \u03c0\u2082 \u2219 A\n\\end{code}\n\nMatrices.\n\nSee [this sidenote][plfa.Quantitative.LinAlg].\n\n\\begin{code}\nMatrix : Precontext \u2192 Precontext \u2192 Set\nMatrix \u03b3 \u03b4 = \u2200 {A} \u2192 \u03b3 \u220b A \u2192 Context \u03b4\n\nvariable \u039e : Matrix \u03b3 \u03b4\n\\end{code}\n\nThe identity matrix.\n\n \"x\" \"y\" \"z\"\n \"x\" 1 \u2219 A , 0 \u2219 B , 0 \u2219 C\n \"y\" 0 \u2219 A , 1 \u2219 B , 0 \u2219 C\n \"z\" 0 \u2219 A , 0 \u2219 B , 1 \u2219 C\n\n\\begin{code}\nidentity : \u2200 {\u03b3} \u2192 Matrix \u03b3 \u03b3\nidentity {\u03b3 , A} Z = 0s , 1# \u2219 A\nidentity {\u03b3 , B} (S x) = identity x , 0# \u2219 B\n\\end{code}\n\n\n# Terms and the typing judgment\n\n\\begin{code}\ndata _\u22a2_ : \u2200 {\u03b3} (\u0393 : Context \u03b3) (A : Type) \u2192 Set where\n\n `_ : \u2200 {\u03b3} {A}\n\n \u2192 (x : \u03b3 \u220b A)\n --------------\n \u2192 identity x \u22a2 A\n\n \u019b_ : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {\u03c0}\n\n \u2192 \u0393 , \u03c0 \u2219 A \u22a2 B\n ----------------\n \u2192 \u0393 \u22a2 [ \u03c0 \u2219 A ]\u22b8 B\n\n _\u00b7_ : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {\u03c0}\n\n \u2192 \u0393 \u22a2 [ \u03c0 \u2219 A ]\u22b8 B\n \u2192 \u0394 \u22a2 A\n ----------------\n \u2192 \u0393 +\u0307 \u03c0 *\u0307 \u0394 \u22a2 B\n\n \u27e8\u27e9 : \u2200 {\u03b3}\n\n -----------\n \u2192 0s {\u03b3} \u22a2 `1\n\n case1 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A}\n\n \u2192 \u0393 \u22a2 `1\n \u2192 \u0394 \u22a2 A\n ---------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 A\n\n \u27e8_,_\u27e9 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B}\n\n \u2192 \u0393 \u22a2 A\n \u2192 \u0394 \u22a2 B\n -------------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 A \u2297 B\n\n case\u2297 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C}\n\n \u2192 \u0393 \u22a2 A \u2297 B\n \u2192 \u0394 , 1# \u2219 A , 1# \u2219 B \u22a2 C\n -----------------------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 C\n\n inj\u2081 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B}\n\n \u2192 \u0393 \u22a2 A\n ---------\n \u2192 \u0393 \u22a2 A \u2295 B\n\n inj\u2082 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B}\n\n \u2192 \u0393 \u22a2 B\n ---------\n \u2192 \u0393 \u22a2 A \u2295 B\n\n case\u2295 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C}\n\n \u2192 \u0393 \u22a2 A \u2295 B\n \u2192 \u0394 , 1# \u2219 A \u22a2 C\n \u2192 \u0394 , 1# \u2219 B \u22a2 C\n --------------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 C\n\\end{code}\n\n\n# Properties of vector operations\n\nUnit scaling does nothing.\n\n\\begin{code}\n*\u0307-identity\u02e1 : \u2200 {\u03b3} (\u0393 : Context \u03b3)\n\n -----------\n \u2192 1# *\u0307 \u0393 \u2261 \u0393\n\n*\u0307-identity\u02e1 \u2205 = refl\n*\u0307-identity\u02e1 (\u0393 , \u03c0 \u2219 A) =\n begin\n 1# *\u0307 \u0393 , 1# * \u03c0 \u2219 A\n \u2261\u27e8 *-identity\u02e1 \u03c0 |> cong (1# *\u0307 \u0393 ,_\u2219 A) \u27e9\n 1# *\u0307 \u0393 , \u03c0 \u2219 A\n \u2261\u27e8 *\u0307-identity\u02e1 \u0393 |> cong (_, \u03c0 \u2219 A) \u27e9\n \u0393 , \u03c0 \u2219 A\n \u220e\n\\end{code}\n\nScaling by a product is the composition of scalings.\n\n\\begin{code}\n*\u0307-assoc : \u2200 {\u03b3} (\u0393 : Context \u03b3) {\u03c0 \u03c0\u2032}\n\n ------------------------------\n \u2192 (\u03c0 * \u03c0\u2032) *\u0307 \u0393 \u2261 \u03c0 *\u0307 (\u03c0\u2032 *\u0307 \u0393)\n\n*\u0307-assoc \u2205 = refl\n*\u0307-assoc (\u0393 , \u03c0\u2033 \u2219 A) {\u03c0} {\u03c0\u2032} =\n begin\n (\u03c0 * \u03c0\u2032) *\u0307 \u0393 , (\u03c0 * \u03c0\u2032) * \u03c0\u2033 \u2219 A\n \u2261\u27e8 *-assoc \u03c0 \u03c0\u2032 \u03c0\u2033 |> cong ((\u03c0 * \u03c0\u2032) *\u0307 \u0393 ,_\u2219 A) \u27e9\n (\u03c0 * \u03c0\u2032) *\u0307 \u0393 , \u03c0 * (\u03c0\u2032 * \u03c0\u2033) \u2219 A\n \u2261\u27e8 *\u0307-assoc \u0393 |> cong (_, \u03c0 * (\u03c0\u2032 * \u03c0\u2033) \u2219 A) \u27e9\n \u03c0 *\u0307 (\u03c0\u2032 *\u0307 \u0393) , \u03c0 * (\u03c0\u2032 * \u03c0\u2033) \u2219 A\n \u220e\n\\end{code}\n\nScaling the 0-vector gives the 0-vector.\n\n\\begin{code}\n*\u0307-zero\u02b3 : \u2200 {\u03b3} \u03c0\n\n ----------------\n \u2192 0s {\u03b3} \u2261 \u03c0 *\u0307 0s\n\n*\u0307-zero\u02b3 {\u2205} \u03c0 = refl\n*\u0307-zero\u02b3 {\u03b3 , A} \u03c0 =\n begin\n 0s , 0# \u2219 A\n \u2261\u27e8 zero\u02b3 \u03c0 |> sym \u2218 cong (0s ,_\u2219 A) \u27e9\n 0s , \u03c0 * 0# \u2219 A\n \u2261\u27e8 *\u0307-zero\u02b3 \u03c0 |> cong (_, \u03c0 * 0# \u2219 A) \u27e9\n \u03c0 *\u0307 0s , \u03c0 * 0# \u2219 A\n \u220e\n\\end{code}\n\nScaling by 0 gives the 0-vector.\n\n\\begin{code}\n*\u0307-zero\u02e1 : \u2200 {\u03b3} (\u0393 : Context \u03b3)\n\n ------------\n \u2192 0# *\u0307 \u0393 \u2261 0s\n\n*\u0307-zero\u02e1 \u2205 = refl\n*\u0307-zero\u02e1 (\u0393 , \u03c0 \u2219 A) =\n begin\n 0# *\u0307 \u0393 , 0# * \u03c0 \u2219 A\n \u2261\u27e8 zero\u02e1 \u03c0 |> cong (0# *\u0307 \u0393 ,_\u2219 A) \u27e9\n 0# *\u0307 \u0393 , 0# \u2219 A\n \u2261\u27e8 *\u0307-zero\u02e1 \u0393 |> cong (_, 0# \u2219 A) \u27e9\n 0s , 0# \u2219 A\n \u220e\n\\end{code}\n\nAdding the 0-vector does nothing.\n\n\\begin{code}\n+\u0307-identity\u02e1 : \u2200 {\u03b3} (\u0393 : Context \u03b3)\n\n ----------\n \u2192 0s +\u0307 \u0393 \u2261 \u0393\n\n+\u0307-identity\u02e1 \u2205 = refl\n+\u0307-identity\u02e1 (\u0393 , \u03c0 \u2219 A) =\n begin\n 0s +\u0307 \u0393 , 0# + \u03c0 \u2219 A\n \u2261\u27e8 +-identity\u02e1 \u03c0 |> cong (0s +\u0307 \u0393 ,_\u2219 A) \u27e9\n 0s +\u0307 \u0393 , \u03c0 \u2219 A\n \u2261\u27e8 +\u0307-identity\u02e1 \u0393 |> cong (_, \u03c0 \u2219 A) \u27e9\n \u0393 , \u03c0 \u2219 A\n \u220e\n\\end{code}\n\n\\begin{code}\n+\u0307-identity\u02b3 : \u2200 {\u03b3} (\u0393 : Context \u03b3)\n\n ----------\n \u2192 \u0393 +\u0307 0s \u2261 \u0393\n\n+\u0307-identity\u02b3 \u2205 = refl\n+\u0307-identity\u02b3 (\u0393 , \u03c0 \u2219 A) =\n begin\n \u0393 +\u0307 0s , \u03c0 + 0# \u2219 A\n \u2261\u27e8 +-identity\u02b3 \u03c0 |> cong (\u0393 +\u0307 0s ,_\u2219 A) \u27e9\n \u0393 +\u0307 0s , \u03c0 \u2219 A\n \u2261\u27e8 +\u0307-identity\u02b3 \u0393 |> cong (_, \u03c0 \u2219 A) \u27e9\n \u0393 , \u03c0 \u2219 A\n \u220e\n\\end{code}\n\nVector addition is commutative.\n\n\\begin{code}\n+\u0307-comm : \u2200 {\u03b3} (\u0393\u2081 \u0393\u2082 : Context \u03b3)\n\n -----------------\n \u2192 \u0393\u2081 +\u0307 \u0393\u2082 \u2261 \u0393\u2082 +\u0307 \u0393\u2081\n\n+\u0307-comm \u2205 \u2205 = refl\n+\u0307-comm (\u0393\u2081 , \u03c0\u2081 \u2219 A) (\u0393\u2082 , \u03c0\u2082 \u2219 .A) =\n begin\n \u0393\u2081 +\u0307 \u0393\u2082 , \u03c0\u2081 + \u03c0\u2082 \u2219 A\n \u2261\u27e8 +-comm \u03c0\u2081 \u03c0\u2082 |> cong (\u0393\u2081 +\u0307 \u0393\u2082 ,_\u2219 A) \u27e9\n \u0393\u2081 +\u0307 \u0393\u2082 , \u03c0\u2082 + \u03c0\u2081 \u2219 A\n \u2261\u27e8 +\u0307-comm \u0393\u2081 \u0393\u2082 |> cong (_, \u03c0\u2082 + \u03c0\u2081 \u2219 A) \u27e9\n \u0393\u2082 +\u0307 \u0393\u2081 , \u03c0\u2082 + \u03c0\u2081 \u2219 A\n \u220e\n\\end{code}\n\nVector addition is associative.\n\n\\begin{code}\n+\u0307-assoc : \u2200 {\u03b3} (\u0393\u2081 \u0393\u2082 \u0393\u2083 : Context \u03b3)\n\n -------------------------------------\n \u2192 (\u0393\u2081 +\u0307 \u0393\u2082) +\u0307 \u0393\u2083 \u2261 \u0393\u2081 +\u0307 (\u0393\u2082 +\u0307 \u0393\u2083)\n\n+\u0307-assoc \u2205 \u2205 \u2205 = refl\n+\u0307-assoc (\u0393\u2081 , \u03c0\u2081 \u2219 A) (\u0393\u2082 , \u03c0\u2082 \u2219 .A) (\u0393\u2083 , \u03c0\u2083 \u2219 .A) =\n begin\n (\u0393\u2081 +\u0307 \u0393\u2082) +\u0307 \u0393\u2083 , (\u03c0\u2081 + \u03c0\u2082) + \u03c0\u2083 \u2219 A\n \u2261\u27e8 +-assoc \u03c0\u2081 \u03c0\u2082 \u03c0\u2083 |> cong ((\u0393\u2081 +\u0307 \u0393\u2082) +\u0307 \u0393\u2083 ,_\u2219 A) \u27e9\n (\u0393\u2081 +\u0307 \u0393\u2082) +\u0307 \u0393\u2083 , \u03c0\u2081 + (\u03c0\u2082 + \u03c0\u2083) \u2219 A\n \u2261\u27e8 +\u0307-assoc \u0393\u2081 \u0393\u2082 \u0393\u2083 |> cong (_, \u03c0\u2081 + (\u03c0\u2082 + \u03c0\u2083) \u2219 A) \u27e9\n \u0393\u2081 +\u0307 (\u0393\u2082 +\u0307 \u0393\u2083) , \u03c0\u2081 + (\u03c0\u2082 + \u03c0\u2083) \u2219 A\n \u220e\n\\end{code}\n\nScaling by a sum gives the sum of the scalings.\n\n\\begin{code}\n*\u0307-distrib\u02b3-+\u0307 : \u2200 {\u03b3} (\u0393 : Context \u03b3) (\u03c0\u2081 \u03c0\u2082 : Mult)\n\n -----------------------------------\n \u2192 (\u03c0\u2081 + \u03c0\u2082) *\u0307 \u0393 \u2261 \u03c0\u2081 *\u0307 \u0393 +\u0307 \u03c0\u2082 *\u0307 \u0393\n\n*\u0307-distrib\u02b3-+\u0307 \u2205 \u03c0\u2081 \u03c0\u2082 = refl\n*\u0307-distrib\u02b3-+\u0307 (\u0393 , \u03c0 \u2219 A) \u03c0\u2081 \u03c0\u2082 =\n begin\n (\u03c0\u2081 + \u03c0\u2082) *\u0307 \u0393 , (\u03c0\u2081 + \u03c0\u2082) * \u03c0 \u2219 A\n \u2261\u27e8 distrib\u02b3 \u03c0 \u03c0\u2081 \u03c0\u2082 |> cong ((\u03c0\u2081 + \u03c0\u2082) *\u0307 \u0393 ,_\u2219 A) \u27e9\n (\u03c0\u2081 + \u03c0\u2082) *\u0307 \u0393 , (\u03c0\u2081 * \u03c0) + (\u03c0\u2082 * \u03c0) \u2219 A\n \u2261\u27e8 *\u0307-distrib\u02b3-+\u0307 \u0393 \u03c0\u2081 \u03c0\u2082 |> cong (_, (\u03c0\u2081 * \u03c0) + (\u03c0\u2082 * \u03c0) \u2219 A) \u27e9\n \u03c0\u2081 *\u0307 \u0393 +\u0307 \u03c0\u2082 *\u0307 \u0393 , (\u03c0\u2081 * \u03c0) + (\u03c0\u2082 * \u03c0) \u2219 A\n \u220e\n\\end{code}\n\nScaling a sum gives the sum of the scalings.\n\n\\begin{code}\n*\u0307-distrib\u02e1-+\u0307 : \u2200 {\u03b3} (\u0393\u2081 \u0393\u2082 : Context \u03b3) (\u03c0 : Mult)\n\n --------------------------------------\n \u2192 \u03c0 *\u0307 (\u0393\u2081 +\u0307 \u0393\u2082) \u2261 \u03c0 *\u0307 \u0393\u2081 +\u0307 \u03c0 *\u0307 \u0393\u2082\n\n*\u0307-distrib\u02e1-+\u0307 \u2205 \u2205 \u03c0 = refl\n*\u0307-distrib\u02e1-+\u0307 (\u0393\u2081 , \u03c0\u2081 \u2219 A) (\u0393\u2082 , \u03c0\u2082 \u2219 .A) \u03c0 =\n begin\n \u03c0 *\u0307 (\u0393\u2081 +\u0307 \u0393\u2082) , \u03c0 * (\u03c0\u2081 + \u03c0\u2082) \u2219 A\n \u2261\u27e8 distrib\u02e1 \u03c0 \u03c0\u2081 \u03c0\u2082 |> cong (\u03c0 *\u0307 (\u0393\u2081 +\u0307 \u0393\u2082) ,_\u2219 A) \u27e9\n \u03c0 *\u0307 (\u0393\u2081 +\u0307 \u0393\u2082) , (\u03c0 * \u03c0\u2081) + (\u03c0 * \u03c0\u2082) \u2219 A\n \u2261\u27e8 *\u0307-distrib\u02e1-+\u0307 \u0393\u2081 \u0393\u2082 \u03c0 |> cong (_, (\u03c0 * \u03c0\u2081) + (\u03c0 * \u03c0\u2082) \u2219 A) \u27e9\n \u03c0 *\u0307 \u0393\u2081 +\u0307 \u03c0 *\u0307 \u0393\u2082 , (\u03c0 * \u03c0\u2081) + (\u03c0 * \u03c0\u2082) \u2219 A\n \u220e\n\\end{code}\n\n\n\n# Vector-matrix multiplication\n\nVector-matrix multiplication.\n\n\\begin{code}\n_*\u27e9_ : \u2200 {\u03b3 \u03b4} \u2192 Context \u03b3 \u2192 Matrix \u03b3 \u03b4 \u2192 Context \u03b4\n\u2205 *\u27e9 \u039e = 0s\n(\u0393 , \u03c0 \u2219 A) *\u27e9 \u039e = (\u03c0 *\u0307 \u039e Z) +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)\n\\end{code}\n\nLinear maps preserve the 0-vector.\n\n\\begin{code}\n*\u27e9-zero\u02e1 : \u2200 {\u03b3 \u03b4} (\u039e : Matrix \u03b3 \u03b4)\n\n --------------\n \u2192 0s *\u27e9 \u039e \u2261 0s\n\n*\u27e9-zero\u02e1 {\u2205} {\u03b4} \u039e = refl\n*\u27e9-zero\u02e1 {\u03b3 , A} {\u03b4} \u039e =\n begin\n 0s *\u27e9 \u039e\n \u2261\u27e8\u27e9\n 0# *\u0307 \u039e Z +\u0307 0s *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 *\u27e9-zero\u02e1 (\u039e \u2218 S_) |> cong (0# *\u0307 \u039e Z +\u0307_) \u27e9\n 0# *\u0307 \u039e Z +\u0307 0s\n \u2261\u27e8 *\u0307-zero\u02e1 (\u039e Z) |> cong (_+\u0307 0s) \u27e9\n 0s +\u0307 0s\n \u2261\u27e8 +\u0307-identity\u02b3 0s \u27e9\n 0s\n \u220e\n\\end{code}\n\nAdding a row of 0s to the end of the matrix and then multiplying by a vector produces a vector with a 0 at the bottom.\n\n\\begin{code}\n*\u27e9-zero\u02b3 : \u2200 {\u03b3 \u03b4} (\u0393 : Context \u03b3) (\u039e : Matrix \u03b3 \u03b4) {B}\n\n ---------------------------------------------\n \u2192 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 B) \u2261 \u0393 *\u27e9 \u039e , 0# \u2219 B\n\n*\u27e9-zero\u02b3 {\u03b3} {\u03b4} \u2205 \u039e {B} = refl\n*\u27e9-zero\u02b3 {\u03b3} {\u03b4} (\u0393 , \u03c0 \u2219 C) \u039e {B} =\n begin\n (\u03c0 *\u0307 \u039e Z , \u03c0 * 0# \u2219 B) +\u0307 (\u0393 *\u27e9 (\u03bb x \u2192 \u039e (S x) , 0# \u2219 B))\n \u2261\u27e8 *\u27e9-zero\u02b3 \u0393 (\u039e \u2218 S_) |> cong ((\u03c0 *\u0307 \u039e Z , \u03c0 * 0# \u2219 B) +\u0307_) \u27e9\n (\u03c0 *\u0307 \u039e Z , \u03c0 * 0# \u2219 B) +\u0307 (\u0393 *\u27e9 (\u03bb x \u2192 \u039e (S x)) , 0# \u2219 B)\n \u2261\u27e8\u27e9\n (\u03c0 *\u0307 \u039e Z , \u03c0 * 0# \u2219 B) +\u0307 (\u0393 *\u27e9 (\u039e \u2218 S_) , 0# \u2219 B)\n \u2261\u27e8\u27e9\n (\u03c0 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)) , (\u03c0 * 0#) + 0# \u2219 B\n \u2261\u27e8 +-identity\u02b3 (\u03c0 * 0#) |> cong ((\u03c0 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)) ,_\u2219 B) \u27e9\n (\u03c0 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)) , \u03c0 * 0# \u2219 B\n \u2261\u27e8 zero\u02b3 \u03c0 |> cong ((\u03c0 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)) ,_\u2219 B) \u27e9\n (\u03c0 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_)) , 0# \u2219 B\n \u220e\n\\end{code}\n\nLinear maps preserve scaling.\n\n\\begin{code}\n*\u27e9-assoc : \u2200 {\u03b3 \u03b4} (\u0393 : Context \u03b3) (\u039e : Matrix \u03b3 \u03b4) (\u03c0 : Mult)\n\n -----------------------------------\n \u2192 (\u03c0 *\u0307 \u0393) *\u27e9 \u039e \u2261 \u03c0 *\u0307 (\u0393 *\u27e9 \u039e)\n\n*\u27e9-assoc {\u03b3} {\u03b4} \u2205 \u039e \u03c0 =\n begin\n 0s\n \u2261\u27e8 *\u0307-zero\u02b3 \u03c0 \u27e9\n \u03c0 *\u0307 0s\n \u220e\n\n*\u27e9-assoc {\u03b3} {\u03b4} (\u0393 , \u03c0\u2032 \u2219 A) \u039e \u03c0 =\n begin\n ((\u03c0 * \u03c0\u2032) *\u0307 \u039e Z) +\u0307 ((\u03c0 *\u0307 \u0393) *\u27e9 (\u039e \u2218 S_))\n \u2261\u27e8 *\u27e9-assoc \u0393 (\u039e \u2218 S_) \u03c0 |> cong ((\u03c0 * \u03c0\u2032) *\u0307 \u039e Z +\u0307_) \u27e9\n ((\u03c0 * \u03c0\u2032) *\u0307 \u039e Z) +\u0307 (\u03c0 *\u0307 (\u0393 *\u27e9 (\u039e \u2218 S_)))\n \u2261\u27e8 *\u0307-assoc (\u039e Z) |> cong (_+\u0307 (\u03c0 *\u0307 (\u0393 *\u27e9 (\u039e \u2218 S_)))) \u27e9\n (\u03c0 *\u0307 (\u03c0\u2032 *\u0307 \u039e Z)) +\u0307 (\u03c0 *\u0307 (\u0393 *\u27e9 (\u039e \u2218 S_)))\n \u2261\u27e8 *\u0307-distrib\u02e1-+\u0307 (\u03c0\u2032 *\u0307 \u039e Z) (\u0393 *\u27e9 (\u039e \u2218 S_)) \u03c0 |> sym \u27e9\n \u03c0 *\u0307 (\u03c0\u2032 *\u0307 \u039e Z +\u0307 \u0393 *\u27e9 (\u039e \u2218 S_))\n \u220e\n\\end{code}\n\nLinear maps distribute over sums.\n\n\\begin{code}\n*\u27e9-distrib\u02b3-+\u0307 : \u2200 {\u03b3} {\u03b4} (\u0393\u2081 \u0393\u2082 : Context \u03b3) (\u039e : Matrix \u03b3 \u03b4)\n\n ----------------------------------\n \u2192 (\u0393\u2081 +\u0307 \u0393\u2082) *\u27e9 \u039e \u2261 \u0393\u2081 *\u27e9 \u039e +\u0307 \u0393\u2082 *\u27e9 \u039e\n\n*\u27e9-distrib\u02b3-+\u0307 \u2205 \u2205 \u039e =\n begin\n 0s\n \u2261\u27e8 sym (+\u0307-identity\u02b3 0s) \u27e9\n 0s +\u0307 0s\n \u220e\n\n*\u27e9-distrib\u02b3-+\u0307 (\u0393\u2081 , \u03c0\u2081 \u2219 A) (\u0393\u2082 , \u03c0\u2082 \u2219 .A) \u039e =\n begin\n (\u03c0\u2081 + \u03c0\u2082) *\u0307 \u039e Z +\u0307 (\u0393\u2081 +\u0307 \u0393\u2082) *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 *\u27e9-distrib\u02b3-+\u0307 \u0393\u2081 \u0393\u2082 (\u039e \u2218 S_) |> cong ((\u03c0\u2081 + \u03c0\u2082) *\u0307 \u039e Z +\u0307_) \u27e9\n (\u03c0\u2081 + \u03c0\u2082) *\u0307 \u039e Z +\u0307 (\u0393\u2081 *\u27e9 (\u039e \u2218 S_) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_))\n \u2261\u27e8 *\u0307-distrib\u02b3-+\u0307 (\u039e Z) \u03c0\u2081 \u03c0\u2082 |> cong (_+\u0307 (\u0393\u2081 *\u27e9 (\u039e \u2218 S_) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_))) \u27e9\n (\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u03c0\u2082 *\u0307 \u039e Z) +\u0307 (\u0393\u2081 *\u27e9 (\u039e \u2218 S_) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_))\n \u2261\u27e8 +\u0307-assoc (\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u03c0\u2082 *\u0307 \u039e Z) (\u0393\u2081 *\u27e9 (\u039e \u2218 S_)) (\u0393\u2082 *\u27e9 (\u039e \u2218 S_)) |> sym \u27e9\n ((\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u03c0\u2082 *\u0307 \u039e Z) +\u0307 \u0393\u2081 *\u27e9 (\u039e \u2218 S_)) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 +\u0307-assoc (\u03c0\u2081 *\u0307 \u039e Z) (\u03c0\u2082 *\u0307 \u039e Z) (\u0393\u2081 *\u27e9 (\u039e \u2218 S_)) |> cong (_+\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)) \u27e9\n (\u03c0\u2081 *\u0307 \u039e Z +\u0307 (\u03c0\u2082 *\u0307 \u039e Z +\u0307 \u0393\u2081 *\u27e9 (\u039e \u2218 S_))) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 +\u0307-comm (\u03c0\u2082 *\u0307 \u039e Z) (\u0393\u2081 *\u27e9 (\u039e \u2218 S_)) |> cong ((_+\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)) \u2218 (\u03c0\u2081 *\u0307 \u039e Z +\u0307_)) \u27e9\n (\u03c0\u2081 *\u0307 \u039e Z +\u0307 (\u0393\u2081 *\u27e9 (\u039e \u2218 S_) +\u0307 \u03c0\u2082 *\u0307 \u039e Z)) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 +\u0307-assoc (\u03c0\u2081 *\u0307 \u039e Z) (\u0393\u2081 *\u27e9 (\u039e \u2218 S_)) (\u03c0\u2082 *\u0307 \u039e Z) |> sym \u2218 cong (_+\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)) \u27e9\n ((\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u0393\u2081 *\u27e9 (\u039e \u2218 S_)) +\u0307 \u03c0\u2082 *\u0307 \u039e Z) +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 +\u0307-assoc (\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u0393\u2081 *\u27e9 (\u039e \u2218 S_)) (\u03c0\u2082 *\u0307 \u039e Z) (\u0393\u2082 *\u27e9 (\u039e \u2218 S_)) \u27e9\n (\u03c0\u2081 *\u0307 \u039e Z +\u0307 \u0393\u2081 *\u27e9 (\u039e \u2218 S_)) +\u0307 (\u03c0\u2082 *\u0307 \u039e Z +\u0307 \u0393\u2082 *\u27e9 (\u039e \u2218 S_))\n \u220e\n\\end{code}\n\nMultiplying by a standard basis vector projects out the corresponding column of the matrix.\n\n\\begin{code}\n*\u27e9-identity\u02e1 : \u2200 {\u03b3 \u03b4} {A} (\u039e : Matrix \u03b3 \u03b4)\n\n \u2192 (x : \u03b3 \u220b A)\n -----------------------\n \u2192 identity x *\u27e9 \u039e \u2261 \u039e x\n\n*\u27e9-identity\u02e1 {\u03b3 , A} {\u03b4} {A} \u039e Z =\n begin\n identity Z *\u27e9 \u039e\n \u2261\u27e8\u27e9\n 1# *\u0307 \u039e Z +\u0307 0s *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 *\u27e9-zero\u02e1 (\u039e \u2218 S_) |> cong ((1# *\u0307 \u039e Z) +\u0307_) \u27e9\n 1# *\u0307 \u039e Z +\u0307 0s\n \u2261\u27e8 +\u0307-identity\u02b3 (1# *\u0307 \u039e Z) \u27e9\n 1# *\u0307 \u039e Z\n \u2261\u27e8 *\u0307-identity\u02e1 (\u039e Z) \u27e9\n \u039e Z\n \u220e\n\n*\u27e9-identity\u02e1 {\u03b3 , B} {\u03b4} {A} \u039e (S x) =\n begin\n identity (S x) *\u27e9 \u039e\n \u2261\u27e8\u27e9\n 0# *\u0307 \u039e Z +\u0307 identity x *\u27e9 (\u039e \u2218 S_)\n \u2261\u27e8 *\u27e9-identity\u02e1 (\u039e \u2218 S_) x |> cong (0# *\u0307 \u039e Z +\u0307_) \u27e9\n 0# *\u0307 \u039e Z +\u0307 \u039e (S x)\n \u2261\u27e8 *\u0307-zero\u02e1 (\u039e Z) |> cong (_+\u0307 \u039e (S x)) \u27e9\n 0s +\u0307 \u039e (S x)\n \u2261\u27e8 +\u0307-identity\u02e1 (\u039e (S x)) \u27e9\n \u039e (S x)\n \u220e\n\\end{code}\n\nThe standard basis vectors put together give the identity matrix.\n\n\\begin{code}\n*\u27e9-identity\u02b3 : \u2200 {\u03b3} (\u0393 : Context \u03b3)\n\n -------------------\n \u2192 \u0393 *\u27e9 identity \u2261 \u0393\n\n*\u27e9-identity\u02b3 {\u03b3} \u2205 = refl\n*\u27e9-identity\u02b3 {\u03b3 , .A} (\u0393 , \u03c0 \u2219 A) =\n begin\n (\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307 (\u0393 *\u27e9 (\u03bb x \u2192 identity x , 0# \u2219 A))\n \u2261\u27e8 *\u27e9-zero\u02b3 \u0393 identity |> cong ((\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307_) \u27e9\n (\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307 (\u0393 *\u27e9 identity , 0# \u2219 A)\n \u2261\u27e8 *\u27e9-identity\u02b3 \u0393 |> cong (((\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307_) \u2218 (_, 0# \u2219 A)) \u27e9\n (\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307 (\u0393 , 0# \u2219 A)\n \u2261\u27e8\u27e9\n \u03c0 *\u0307 0s +\u0307 \u0393 , (\u03c0 * 1#) + 0# \u2219 A\n \u2261\u27e8 +-identity\u02b3 (\u03c0 * 1#) |> cong ((\u03c0 *\u0307 0s +\u0307 \u0393) ,_\u2219 A) \u27e9\n \u03c0 *\u0307 0s +\u0307 \u0393 , \u03c0 * 1# \u2219 A\n \u2261\u27e8 *-identity\u02b3 \u03c0 |> cong ((\u03c0 *\u0307 0s +\u0307 \u0393) ,_\u2219 A) \u27e9\n \u03c0 *\u0307 0s +\u0307 \u0393 , \u03c0 \u2219 A\n \u2261\u27e8 *\u0307-zero\u02b3 \u03c0 |> cong ((_, \u03c0 \u2219 A) \u2218 (_+\u0307 \u0393)) \u2218 sym \u27e9\n 0s +\u0307 \u0393 , \u03c0 \u2219 A\n \u2261\u27e8 +\u0307-identity\u02e1 \u0393 |> cong (_, \u03c0 \u2219 A) \u27e9\n \u0393 , \u03c0 \u2219 A\n \u220e\n\\end{code}\n\n\n# Renaming\n\n\\begin{code}\next : \u2200 {\u03b3 \u03b4}\n\n \u2192 (\u2200 {A} \u2192 \u03b3 \u220b A \u2192 \u03b4 \u220b A)\n ---------------------------------\n \u2192 (\u2200 {A B} \u2192 \u03b3 , B \u220b A \u2192 \u03b4 , B \u220b A)\n\next \u03c1 Z = Z\next \u03c1 (S x) = S (\u03c1 x)\n\\end{code}\n\n\\begin{code}\nlem-` : \u2200 {\u03b3 \u03b4} {A} {\u039e : Matrix \u03b3 \u03b4} (x : \u03b3 \u220b A) \u2192 _\nlem-` {\u03b3} {\u03b4} {A} {\u039e} x =\n begin\n \u039e x\n \u2261\u27e8 *\u27e9-identity\u02e1 \u039e x |> sym \u27e9\n identity x *\u27e9 \u039e\n \u220e\n\\end{code}\n\n\\begin{code}\nlem-\u019b : \u2200 {\u03b3 \u03b4} (\u0393 : Context \u03b3) {A} {\u03c0} {\u039e : Matrix \u03b3 \u03b4} \u2192 _\nlem-\u019b {\u03b3} {\u03b4} \u0393 {A} {\u03c0} {\u039e} =\n begin\n (\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307 (\u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A))\n \u2261\u27e8 *\u27e9-zero\u02b3 \u0393 \u039e |> cong ((\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307_) \u27e9\n (\u03c0 *\u0307 0s , \u03c0 * 1# \u2219 A) +\u0307 (\u0393 *\u27e9 \u039e , 0# \u2219 A)\n \u2261\u27e8\u27e9\n \u03c0 *\u0307 0s +\u0307 \u0393 *\u27e9 \u039e , (\u03c0 * 1#) + 0# \u2219 A\n \u2261\u27e8 *\u0307-zero\u02b3 \u03c0 |> cong ((_, (\u03c0 * 1#) + 0# \u2219 A) \u2218 (_+\u0307 \u0393 *\u27e9 \u039e)) \u2218 sym \u27e9\n 0s +\u0307 \u0393 *\u27e9 \u039e , (\u03c0 * 1#) + 0# \u2219 A\n \u2261\u27e8 +\u0307-identity\u02e1 (\u0393 *\u27e9 \u039e) |> cong (_, (\u03c0 * 1#) + 0# \u2219 A) \u27e9\n \u0393 *\u27e9 \u039e , (\u03c0 * 1#) + 0# \u2219 A\n \u2261\u27e8 +-identity\u02b3 (\u03c0 * 1#) |> cong (\u0393 *\u27e9 \u039e ,_\u2219 A) \u27e9\n \u0393 *\u27e9 \u039e , \u03c0 * 1# \u2219 A\n \u2261\u27e8 *-identity\u02b3 \u03c0 |> cong (\u0393 *\u27e9 \u039e ,_\u2219 A) \u27e9\n \u0393 *\u27e9 \u039e , \u03c0 \u2219 A\n \u220e\n\\end{code}\n\n\\begin{code}\nlem-\u00b7 : \u2200 {\u03b3 \u03b4} (\u0393 \u0394 : Context \u03b3) {\u03c0} {\u039e : Matrix \u03b3 \u03b4} \u2192 _\nlem-\u00b7 {\u03b3} {\u03b4} \u0393 \u0394 {\u03c0} {\u039e} =\n begin\n \u0393 *\u27e9 \u039e +\u0307 \u03c0 *\u0307 (\u0394 *\u27e9 \u039e)\n \u2261\u27e8 *\u27e9-assoc \u0394 \u039e \u03c0 |> cong (\u0393 *\u27e9 \u039e +\u0307_) \u2218 sym \u27e9\n \u0393 *\u27e9 \u039e +\u0307 (\u03c0 *\u0307 \u0394) *\u27e9 \u039e\n \u2261\u27e8 *\u27e9-distrib\u02b3-+\u0307 \u0393 (\u03c0 *\u0307 \u0394) \u039e |> sym \u27e9\n (\u0393 +\u0307 \u03c0 *\u0307 \u0394) *\u27e9 \u039e \u220e\n\\end{code}\n\n\\begin{code}\nlem-, : \u2200 {\u03b3 \u03b4} (\u0393 \u0394 : Context \u03b3) {\u039e : Matrix \u03b3 \u03b4} \u2192 _\nlem-, {\u03b3} {\u03b4} \u0393 \u0394 {\u039e} =\n begin\n \u0393 *\u27e9 \u039e +\u0307 \u0394 *\u27e9 \u039e\n \u2261\u27e8 *\u27e9-distrib\u02b3-+\u0307 \u0393 \u0394 \u039e |> sym \u27e9\n (\u0393 +\u0307 \u0394) *\u27e9 \u039e\n \u220e\n\\end{code}\n\n\\begin{code}\nlem-case\u2297 : \u2200 {\u03b3 \u03b4} (\u0393 : Context \u03b3) {A B} {\u039e : Matrix \u03b3 \u03b4} \u2192 _\nlem-case\u2297 {\u03b3} {\u03b4} \u0393 {A} {B} {\u039e} =\n let\n lem1 : \u2200 {\u03b3} (\u0393 : Context \u03b3) \u2192 _\n lem1 {\u03b3} \u0393 =\n begin\n (1# *\u0307 0s) +\u0307 \u0393\n \u2261\u27e8 *\u0307-identity\u02e1 0s |> cong (_+\u0307 \u0393) \u27e9\n 0s +\u0307 \u0393\n \u2261\u27e8 +\u0307-identity\u02e1 \u0393 \u27e9\n \u0393\n \u220e\n in\n begin\n\n (1# *\u0307 0s , 1# * 0# \u2219 A , 1# * 1# \u2219 B) +\u0307 ((1# *\u0307 0s , 1# * 1# \u2219 A , 1# * 0# \u2219 B) +\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A , 0# \u2219 B))\n\n -- Step 1. Move the three occurrences of A and B to the top-level.\n \u2261\u27e8 trans (*\u27e9-zero\u02b3 \u0393 (\u03bb x \u2192 \u039e x , 0# \u2219 A)) (*\u27e9-zero\u02b3 \u0393 \u039e |> cong (_, 0# \u2219 B))\n |> cong (((1# *\u0307 0s , 1# * 0# \u2219 A , 1# * 1# \u2219 B) +\u0307_) \u2218 ((1# *\u0307 0s , 1# * 1# \u2219 A , 1# * 0# \u2219 B) +\u0307_)) \u27e9\n\n (1# *\u0307 0s) +\u0307 ((1# *\u0307 0s) +\u0307 (\u0393 *\u27e9 \u039e)) , (1# * 0#) + ((1# * 1#) + 0#) \u2219 A , (1# * 1#) + ((1# * 0#) + 0#) \u2219 B\n\n -- Step 2. Remove the empty environments (1# *\u0307 0s).\n \u2261\u27e8 trans (lem1 (\u0393 *\u27e9 \u039e) |> cong (1# *\u0307 0s +\u0307_)) (lem1 (\u0393 *\u27e9 \u039e))\n |> cong ((_, (1# * 1#) + ((1# * 0#) + 0#) \u2219 B) \u2218 (_, (1# * 0#) + ((1# * 1#) + 0#) \u2219 A)) \u27e9\n\n \u0393 *\u27e9 \u039e , (1# * 0#) + ((1# * 1#) + 0#) \u2219 A , (1# * 1#) + ((1# * 0#) + 0#) \u2219 B\n\n -- Step 3. Reduce the complex sum for the usage of B to 1#.\n \u2261\u27e8 trans (trans (+-identity\u02b3 (1# * 0#)) (*-identity\u02e1 0#) |> cong ((1# * 1#) +_)) (trans (+-identity\u02b3 (1# * 1#)) (*-identity\u02e1 1#))\n |> cong (\u0393 *\u27e9 \u039e , (1# * 0#) + ((1# * 1#) + 0#) \u2219 A ,_\u2219 B) \u27e9\n\n \u0393 *\u27e9 \u039e , (1# * 0#) + ((1# * 1#) + 0#) \u2219 A , 1# \u2219 B\n\n -- Step 4. Reduce the complex sum for the usage of A to 1#.\n \u2261\u27e8 trans (*-identity\u02e1 0# |> cong (_+ ((1# * 1#) + 0#))) (trans (+-identity\u02e1 ((1# * 1#) + 0#)) (trans (+-identity\u02b3 (1# * 1#)) (*-identity\u02b3 1#)))\n |> cong ((_, 1# \u2219 B) \u2218 (\u0393 *\u27e9 \u039e ,_\u2219 A)) \u27e9\n\n \u0393 *\u27e9 \u039e , 1# \u2219 A , 1# \u2219 B\n\n \u220e\n\\end{code}\n\n\\begin{code}\nlem-\u27e8\u27e9 : \u2200 {\u03b3 \u03b4} {\u039e : Matrix \u03b4 \u03b3} \u2192 _\nlem-\u27e8\u27e9 {\u03b3} {\u03b4} {\u039e} =\n begin\n 0s {\u03b3}\n \u2261\u27e8 *\u27e9-zero\u02e1 \u039e |> sym \u27e9\n 0s {\u03b4} *\u27e9 \u039e\n \u220e\n\\end{code}\n\n\\begin{code}\nlem-case\u2295 : \u2200 {\u03b3 \u03b4} (\u0393 : Context \u03b3) {A : Type} {\u039e : Matrix \u03b3 \u03b4} \u2192 _\nlem-case\u2295 {\u03b3} {\u03b4} \u0393 {A} {\u039e} =\n begin\n (1# *\u0307 0s , 1# * 1# \u2219 A) +\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A)\n \u2261\u27e8 *\u0307-identity\u02e1 0s |> cong ((_+\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A)) \u2218 (_, 1# * 1# \u2219 A)) \u27e9\n (0s , 1# * 1# \u2219 A) +\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A)\n \u2261\u27e8 *-identity\u02e1 1# |> cong ((_+\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A)) \u2218 (0s ,_\u2219 A)) \u27e9\n (0s , 1# \u2219 A) +\u0307 \u0393 *\u27e9 (\u03bb x \u2192 \u039e x , 0# \u2219 A)\n \u2261\u27e8 *\u27e9-zero\u02b3 \u0393 \u039e |> cong (( 0s , 1# \u2219 A) +\u0307_) \u27e9\n (0s , 1# \u2219 A) +\u0307 (\u0393 *\u27e9 \u039e , 0# \u2219 A)\n \u2261\u27e8\u27e9\n (0s +\u0307 \u0393 *\u27e9 \u039e) , 1# + 0# \u2219 A\n \u2261\u27e8 +\u0307-identity\u02e1 (\u0393 *\u27e9 \u039e) |> cong (_, 1# + 0# \u2219 A) \u27e9\n \u0393 *\u27e9 \u039e , 1# + 0# \u2219 A\n \u2261\u27e8 +-identity\u02b3 1# |> cong (\u0393 *\u27e9 \u039e ,_\u2219 A) \u27e9\n \u0393 *\u27e9 \u039e , 1# \u2219 A\n \u220e\n\\end{code}\n\n\\begin{code}\nrename : \u2200 {\u03b3 \u03b4} {\u0393 : Context \u03b3} {B}\n\n \u2192 (\u03c1 : \u2200 {A} \u2192 \u03b3 \u220b A \u2192 \u03b4 \u220b A)\n \u2192 \u0393 \u22a2 B\n ---------------------------\n \u2192 \u0393 *\u27e9 (identity \u2218 \u03c1) \u22a2 B\n\nrename \u03c1 (` x) =\n Eq.subst (_\u22a2 _) (lem-` x) (` \u03c1 x)\nrename \u03c1 (\u019b_ {\u0393 = \u0393} N) =\n \u019b (Eq.subst (_\u22a2 _) (lem-\u019b \u0393) (rename (ext \u03c1) N))\nrename \u03c1 (_\u00b7_ {\u0393 = \u0393} {\u0394 = \u0394} L M) =\n Eq.subst (_\u22a2 _) (lem-\u00b7 \u0393 \u0394) (rename \u03c1 L \u00b7 rename \u03c1 M)\nrename \u03c1 \u27e8\u27e9 =\n Eq.subst (_\u22a2 _) (lem-\u27e8\u27e9 {\u039e = identity \u2218 \u03c1}) \u27e8\u27e9\nrename \u03c1 (case1 {\u0393 = \u0393} {\u0394 = \u0394} L N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case1 (rename \u03c1 L) (rename \u03c1 N))\nrename \u03c1 (\u27e8_,_\u27e9 {\u0393 = \u0393} {\u0394 = \u0394} L M) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) \u27e8 rename \u03c1 L , rename \u03c1 M \u27e9\nrename \u03c1 (case\u2297 {\u0393 = \u0393} {\u0394 = \u0394} L N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case\u2297 (rename \u03c1 L) (Eq.subst (_\u22a2 _) (lem-case\u2297 \u0394) (rename (ext (ext \u03c1)) N)))\nrename \u03c1 (inj\u2081 {\u0393 = \u0393} L) =\n inj\u2081 (rename \u03c1 L)\nrename \u03c1 (inj\u2082 {\u0393 = \u0393} L) =\n inj\u2082 (rename \u03c1 L)\nrename \u03c1 (case\u2295 {\u0393 = \u0393} {\u0394 = \u0394} L M N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case\u2295 (rename \u03c1 L) (Eq.subst (_\u22a2 _) (lem-case\u2295 \u0394) (rename (ext \u03c1) M)) (Eq.subst (_\u22a2 _) (lem-case\u2295 \u0394) (rename (ext \u03c1) N)))\n\\end{code}\n\n\n# Simultaneous Substitution\n\nExtend a matrix as the identity matrix -- add a zero to the end of every row, and add a new row with a 1 and the rest 0s.\n\n\\begin{code}\nextm : \u2200 {\u03b3 \u03b4}\n\n \u2192 Matrix \u03b3 \u03b4\n --------------------------------\n \u2192 (\u2200 {B} \u2192 Matrix (\u03b3 , B) (\u03b4 , B))\n\nextm \u039e {B} {A} Z = identity Z\nextm \u039e {B} {A} (S x) = \u039e x , 0# \u2219 B\n\\end{code}\n\n\\begin{code}\nexts : \u2200 {\u03b3 \u03b4} {\u039e : Matrix \u03b3 \u03b4}\n\n \u2192 (\u2200 {A} \u2192 (x : \u03b3 \u220b A) \u2192 \u039e x \u22a2 A)\n ------------------------------------------\n \u2192 (\u2200 {A B} \u2192 (x : \u03b3 , B \u220b A) \u2192 extm \u039e x \u22a2 A)\n\nexts {\u039e = \u039e} \u03c3 {A} {B} Z = ` Z\nexts {\u039e = \u039e} \u03c3 {A} {B} (S x) = Eq.subst (_\u22a2 A) lem (rename S_ (\u03c3 x))\n where\n lem =\n begin\n \u039e x *\u27e9 (identity \u2218 S_)\n \u2261\u27e8\u27e9\n \u039e x *\u27e9 (\u03bb x \u2192 identity x , 0# \u2219 B)\n \u2261\u27e8 *\u27e9-zero\u02b3 (\u039e x) identity \u27e9\n (\u039e x *\u27e9 identity) , 0# \u2219 B\n \u2261\u27e8 *\u27e9-identity\u02b3 (\u039e x) |> cong (_, 0# \u2219 B) \u27e9\n \u039e x , 0# \u2219 B\n \u220e\n\\end{code}\n\n\\begin{code}\nsubst : \u2200 {\u03b3 \u03b4} {\u0393 : Context \u03b3} {\u039e : Matrix \u03b3 \u03b4} {B}\n\n \u2192 (\u03c3 : \u2200 {A} \u2192 (x : \u03b3 \u220b A) \u2192 \u039e x \u22a2 A)\n \u2192 \u0393 \u22a2 B\n --------------------------------------\n \u2192 \u0393 *\u27e9 \u039e \u22a2 B\n\nsubst {\u039e = \u039e} \u03c3 (` x) =\n Eq.subst (_\u22a2 _) (lem-` x) (\u03c3 x)\nsubst {\u039e = \u039e} \u03c3 (\u019b_ {\u0393 = \u0393} N) =\n \u019b (Eq.subst (_\u22a2 _) (lem-\u019b \u0393) (subst (exts \u03c3) N))\nsubst {\u039e = \u039e} \u03c3 (_\u00b7_ {\u0393 = \u0393} {\u0394 = \u0394} L M) =\n Eq.subst (_\u22a2 _) (lem-\u00b7 \u0393 \u0394)(subst \u03c3 L \u00b7 subst \u03c3 M)\nsubst {\u039e = \u039e} \u03c3 \u27e8\u27e9 =\n Eq.subst (_\u22a2 _) (lem-\u27e8\u27e9 {\u039e = \u039e}) \u27e8\u27e9\nsubst {\u039e = \u039e} \u03c3 (case1 {\u0393 = \u0393} {\u0394 = \u0394} L N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case1 (subst \u03c3 L) (subst \u03c3 N))\nsubst {\u039e = \u039e} \u03c3 (\u27e8_,_\u27e9 {\u0393 = \u0393} {\u0394 = \u0394} L M) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) \u27e8 subst \u03c3 L , subst \u03c3 M \u27e9\nsubst {\u039e = \u039e} \u03c3 (case\u2297 {\u0393 = \u0393} {\u0394 = \u0394} L N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case\u2297 (subst \u03c3 L) (Eq.subst (_\u22a2 _) (lem-case\u2297 \u0394) (subst (exts (exts \u03c3)) N)))\nsubst {\u039e = \u039e} \u03c3 (inj\u2081 {\u0393 = \u0393} L) =\n inj\u2081 (subst \u03c3 L)\nsubst {\u039e = \u039e} \u03c3 (inj\u2082 {\u0393 = \u0393} L) =\n inj\u2082 (subst \u03c3 L)\nsubst {\u039e = \u039e} \u03c3 (case\u2295 {\u0393 = \u0393} {\u0394 = \u0394} L M N) =\n Eq.subst (_\u22a2 _) (lem-, \u0393 \u0394) (case\u2295 (subst \u03c3 L) (Eq.subst (_\u22a2 _) (lem-case\u2295 \u0394) (subst (exts \u03c3) M)) (Eq.subst (_\u22a2 _) (lem-case\u2295 \u0394) (subst (exts \u03c3) N)))\n\\end{code}\n\n\n# Values\n\n\\begin{code}\ndata Value : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A} \u2192 \u0393 \u22a2 A \u2192 Set where\n\n V-\u019b : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {\u03c0} {N : \u0393 , \u03c0 \u2219 A \u22a2 B}\n\n -----------\n \u2192 Value (\u019b N)\n\n V-\u27e8\u27e9 : \u2200 {\u03b3}\n\n --------------\n \u2192 Value (\u27e8\u27e9 {\u03b3})\n\n V-, : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {L : \u0393 \u22a2 A} {M : \u0394 \u22a2 B}\n\n \u2192 Value L\n \u2192 Value M\n ---------------\n \u2192 Value \u27e8 L , M \u27e9\n\n V-inj\u2081 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {L : \u0393 \u22a2 A}\n\n \u2192 Value L\n --------------\n \u2192 Value (inj\u2081 {B = B} L)\n\n V-inj\u2082 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {L : \u0393 \u22a2 B}\n\n \u2192 Value L\n --------------\n \u2192 Value (inj\u2082 {A = A} L)\n\\end{code}\n\n\n# Single Substitution\n\n\\begin{code}\n_\u22b8[_] : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {\u03c0}\n\n \u2192 \u0393 , \u03c0 \u2219 B \u22a2 A\n \u2192 \u0394 \u22a2 B\n --------------\n \u2192 \u0393 +\u0307 \u03c0 *\u0307 \u0394 \u22a2 A\n\n_\u22b8[_] {\u03b3} {\u0393} {\u0394} {A} {B} {\u03c0} N M = Eq.subst (_\u22a2 _) lem (subst \u03c3\u2032 N)\n where\n \u039e\u2032 : Matrix (\u03b3 , B) \u03b3\n \u039e\u2032 Z = \u0394\n \u039e\u2032 (S x) = identity x\n \u03c3\u2032 : \u2200 {A} \u2192 (x : \u03b3 , B \u220b A) \u2192 \u039e\u2032 x \u22a2 A\n \u03c3\u2032 Z = M\n \u03c3\u2032 (S x) = ` x\n\n lem =\n begin\n \u03c0 *\u0307 \u0394 +\u0307 \u0393 *\u27e9 identity\n \u2261\u27e8 +\u0307-comm (\u03c0 *\u0307 \u0394) (\u0393 *\u27e9 identity) \u27e9\n \u0393 *\u27e9 identity +\u0307 \u03c0 *\u0307 \u0394\n \u2261\u27e8 *\u27e9-identity\u02b3 \u0393 |> cong (_+\u0307 \u03c0 *\u0307 \u0394) \u27e9\n \u0393 +\u0307 \u03c0 *\u0307 \u0394\n \u220e\n\\end{code}\n\n\\begin{code}\n_1[_] : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A} {V : \u0393 \u22a2 `1}\n\n \u2192 \u0394 \u22a2 A\n \u2192 Value V\n ---------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 A\n\n_1[_] {\u0394 = \u0394} N V-\u27e8\u27e9 = Eq.subst (_\u22a2 _) lem N\n where\n lem =\n begin\n \u0394\n \u2261\u27e8 +\u0307-identity\u02e1 \u0394 |> sym \u27e9\n 0s +\u0307 \u0394\n \u220e\n\\end{code}\n\n\\begin{code}\n_\u2297[_][_] : \u2200 {\u03b3} {\u0393 \u0394 \u0398 : Context \u03b3} {A B C}\n\n \u2192 \u0398 , 1# \u2219 A , 1# \u2219 B \u22a2 C\n \u2192 \u0393 \u22a2 A\n \u2192 \u0394 \u22a2 B\n ---------------\n \u2192 \u0393 +\u0307 \u0394 +\u0307 \u0398 \u22a2 C\n\n_\u2297[_][_] {\u03b3} {\u0393} {\u0394} {\u0398} {A} {B} {C} N L M = Eq.subst (_\u22a2 _) lem (subst \u03c3\u2032 N)\n where\n \u039e\u2032 : Matrix (\u03b3 , A , B) \u03b3\n \u039e\u2032 Z = \u0394\n \u039e\u2032 (S Z) = \u0393\n \u039e\u2032 (S (S x)) = identity x\n \u03c3\u2032 : \u2200 {D} \u2192 (x : \u03b3 , A , B \u220b D) \u2192 \u039e\u2032 x \u22a2 D\n \u03c3\u2032 Z = M\n \u03c3\u2032 (S Z) = L\n \u03c3\u2032 (S (S x)) = ` x\n\n lem =\n begin\n 1# *\u0307 \u0394 +\u0307 (1# *\u0307 \u0393 +\u0307 \u0398 *\u27e9 identity)\n \u2261\u27e8 *\u27e9-identity\u02b3 \u0398 |> cong ((1# *\u0307 \u0394 +\u0307_) \u2218 (1# *\u0307 \u0393 +\u0307_)) \u27e9\n 1# *\u0307 \u0394 +\u0307 (1# *\u0307 \u0393 +\u0307 \u0398)\n \u2261\u27e8 *\u0307-identity\u02e1 \u0393 |> cong ((1# *\u0307 \u0394 +\u0307_) \u2218 (_+\u0307 \u0398)) \u27e9\n 1# *\u0307 \u0394 +\u0307 (\u0393 +\u0307 \u0398)\n \u2261\u27e8 *\u0307-identity\u02e1 \u0394 |> cong (_+\u0307 (\u0393 +\u0307 \u0398)) \u27e9\n \u0394 +\u0307 (\u0393 +\u0307 \u0398)\n \u2261\u27e8 +\u0307-assoc \u0394 \u0393 \u0398 |> sym \u27e9\n (\u0394 +\u0307 \u0393) +\u0307 \u0398\n \u2261\u27e8 +\u0307-comm \u0394 \u0393 |> cong (_+\u0307 \u0398) \u27e9\n (\u0393 +\u0307 \u0394) +\u0307 \u0398\n \u220e\n\\end{code}\n\n\\begin{code}\n_\u2295[_] : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B}\n\n \u2192 \u0394 , 1# \u2219 B \u22a2 A\n \u2192 \u0393 \u22a2 B\n --------------\n \u2192 \u0393 +\u0307 \u0394 \u22a2 A\n\n_\u2295[_] {\u03b3} {\u0393} {\u0394} {A} {B} N M = Eq.subst (_\u22a2 _) lem (N \u22b8[ M ])\n where\n lem =\n begin\n \u0394 +\u0307 1# *\u0307 \u0393\n \u2261\u27e8 *\u0307-identity\u02e1 \u0393 |> cong (\u0394 +\u0307_) \u27e9\n \u0394 +\u0307 \u0393\n \u2261\u27e8 +\u0307-comm \u0394 \u0393 \u27e9\n \u0393 +\u0307 \u0394\n \u220e\n\\end{code}\n\n\n# Reduction\n\n\\begin{code}\ninfix 2 _\u2014\u2192_\n\ndata _\u2014\u2192_ : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {\u03c0} {L L\u2032 : \u0393 \u22a2 [ \u03c0 \u2219 A ]\u22b8 B} {M : \u0394 \u22a2 A}\n\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {\u03c0} {V : \u0393 \u22a2 [ \u03c0 \u2219 A ]\u22b8 B} {M M\u2032 : \u0394 \u22a2 A}\n\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n --------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03be-case1 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A} {L L\u2032 : \u0393 \u22a2 `1} {M : \u0394 \u22a2 A}\n\n \u2192 L \u2014\u2192 L\u2032\n -----------------------\n \u2192 case1 L M \u2014\u2192 case1 L\u2032 M\n\n \u03be-,\u2081 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {L L\u2032 : \u0393 \u22a2 A} {M : \u0394 \u22a2 B}\n\n \u2192 L \u2014\u2192 L\u2032\n -----------------------\n \u2192 \u27e8 L , M \u27e9 \u2014\u2192 \u27e8 L\u2032 , M \u27e9\n\n \u03be-,\u2082 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {V : \u0393 \u22a2 A} {M M\u2032 : \u0394 \u22a2 B}\n\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n -----------------------\n \u2192 \u27e8 V , M \u27e9 \u2014\u2192 \u27e8 V , M\u2032 \u27e9\n\n \u03be-case\u2297 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C} {L L\u2032 : \u0393 \u22a2 A \u2297 B} {N : \u0394 , 1# \u2219 A , 1# \u2219 B \u22a2 C}\n\n \u2192 L \u2014\u2192 L\u2032\n ------------------------\n \u2192 case\u2297 L N \u2014\u2192 case\u2297 L\u2032 N\n\n \u03be-inj\u2081 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {L L\u2032 : \u0393 \u22a2 A}\n\n \u2192 L \u2014\u2192 L\u2032\n ---------------------------------\n \u2192 inj\u2081 {B = B} L \u2014\u2192 inj\u2081 {B = B} L\u2032\n\n \u03be-inj\u2082 : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A B} {L L\u2032 : \u0393 \u22a2 B}\n\n \u2192 L \u2014\u2192 L\u2032\n ---------------------------------\n \u2192 inj\u2082 {A = A} L \u2014\u2192 inj\u2082 {A = A} L\u2032\n\n \u03be-case\u2295 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C} {L L\u2032 : \u0393 \u22a2 A \u2295 B} {M : \u0394 , 1# \u2219 A \u22a2 C} {N : \u0394 , 1# \u2219 B \u22a2 C}\n\n \u2192 L \u2014\u2192 L\u2032\n ---------------------------\n \u2192 case\u2295 L M N \u2014\u2192 case\u2295 L\u2032 M N\n\n \u03b2-\u22b8 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B} {\u03c0} {N : \u0393 , \u03c0 \u2219 A \u22a2 B} {W : \u0394 \u22a2 A}\n\n \u2192 (VW : Value W)\n --------------------\n \u2192 (\u019b N) \u00b7 W \u2014\u2192 N \u22b8[ W ]\n\n \u03b2-1 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A} {V : \u0393 \u22a2 `1} {N : \u0394 \u22a2 A}\n\n \u2192 (VV : Value V)\n ----------------------\n \u2192 case1 V N \u2014\u2192 N 1[ VV ]\n\n \u03b2-\u2297 : \u2200 {\u03b3} {\u0393 \u0394 \u0398 : Context \u03b3} {A B C} {V : \u0393 \u22a2 A} {W : \u0394 \u22a2 B} {N : \u0398 , 1# \u2219 A , 1# \u2219 B \u22a2 C}\n\n \u2192 (VV : Value V)\n \u2192 (VW : Value W)\n ---------------------------------\n \u2192 case\u2297 \u27e8 V , W \u27e9 N \u2014\u2192 N \u2297[ V ][ W ]\n\n \u03b2-\u2295\u2081 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C} {V : \u0393 \u22a2 A} {M : \u0394 , 1# \u2219 A \u22a2 C} {N : \u0394 , 1# \u2219 B \u22a2 C}\n\n \u2192 (VV : Value V)\n -----------------------------\n \u2192 case\u2295 (inj\u2081 V) M N \u2014\u2192 M \u2295[ V ]\n\n\n \u03b2-\u2295\u2082 : \u2200 {\u03b3} {\u0393 \u0394 : Context \u03b3} {A B C} {V : \u0393 \u22a2 B} {M : \u0394 , 1# \u2219 A \u22a2 C} {N : \u0394 , 1# \u2219 B \u22a2 C}\n\n \u2192 (VV : Value V)\n -----------------------------\n \u2192 case\u2295 (inj\u2082 V) M N \u2014\u2192 N \u2295[ V ]\n\\end{code}\n\n\n# Progress\n\n\\begin{code}\ndata Progress {\u03b3} {\u0393 : Context \u03b3} {A} (M : \u0393 \u22a2 A) : Set where\n\n step : {N : \u0393 \u22a2 A}\n\n \u2192 M \u2014\u2192 N\n ----------\n \u2192 Progress M\n\n done :\n\n Value M\n ----------\n \u2192 Progress M\n\\end{code}\n\n\\begin{code}\nprogress : \u2200 {A} \u2192 (M : \u2205 \u22a2 A) \u2192 Progress M\nprogress M = go refl M\n where\n go : \u2200 {\u03b3} {\u0393 : Context \u03b3} {A} \u2192 \u03b3 \u2261 \u2205 \u2192 (M : \u0393 \u22a2 A) \u2192 Progress M\n go refl (` ())\n go refl (\u019b N) = done V-\u019b\n go refl (L \u00b7 M) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-\u00b7\u2081 L\u2014\u2192L\u2032)\n ... | done V-\u019b with go refl M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-\u00b7\u2082 V-\u019b M\u2014\u2192M\u2032)\n ... | done V-M = step (\u03b2-\u22b8 V-M)\n go refl \u27e8\u27e9 = done V-\u27e8\u27e9\n go refl (case1 L N) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case1 L\u2014\u2192L\u2032)\n ... | done V-L = step (\u03b2-1 V-L)\n go refl \u27e8 L , M \u27e9 with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-,\u2081 L\u2014\u2192L\u2032)\n ... | done V-L with go refl M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-,\u2082 V-L M\u2014\u2192M\u2032)\n ... | done V-M = done (V-, V-L V-M)\n go refl (case\u2297 L N) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case\u2297 L\u2014\u2192L\u2032)\n ... | done (V-, V-L V-M) = step (\u03b2-\u2297 V-L V-M)\n go refl (inj\u2081 L) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-inj\u2081 L\u2014\u2192L\u2032)\n ... | done V-L = done (V-inj\u2081 V-L)\n go refl (inj\u2082 L) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-inj\u2082 L\u2014\u2192L\u2032)\n ... | done V-L = done (V-inj\u2082 V-L)\n go refl (case\u2295 L M N) with go refl L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case\u2295 L\u2014\u2192L\u2032)\n ... | done (V-inj\u2081 V-L) = step (\u03b2-\u2295\u2081 V-L)\n ... | done (V-inj\u2082 V-L) = step (\u03b2-\u2295\u2082 V-L)\n\\end{code}\n","avg_line_length":24.6687062937,"max_line_length":321,"alphanum_fraction":0.3751816024} +{"size":4438,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Type where\n\\end{code}\n\n## Fixity declarations\n\nTo begin, we get all our infix declarations out of the way.\n\n\\begin{code}\ninfix 4 _\u220b\u22c6_\ninfix 4 _\u22a2\u22c6_\ninfixl 5 _,\u22c6_\n\ninfix 6 \u03a0\ninfixr 7 _\u21d2_\n\ninfix 5 \u019b\ninfixl 7 _\u00b7_\ninfix 9 S\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Agda.Builtin.Nat\n\nopen import Builtin.Constant.Type\n\\end{code}\n\n## Kinds\n\nThe kind of types is `*`. Plutus core core is based on System F\u03c9 which\nis higher order so we have `\u21d2` for type level functions. We also have\na kind called `#` which is used for sized integers and bytestrings.\n\n\\begin{code}\ndata Kind : Set where\n * : Kind -- type\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind -- function kind\n\n{-# FOREIGN GHC import Scoped #-}\n{-# COMPILE GHC Kind = data ScKind (ScKiStar | ScKiFun) #-}\n\n\\end{code}\n\nLet `J`, `K` range over kinds.\n\n## Type contexts\n\nA type context is either empty or extends a type\ncontext by a type variable of a given kind.\n\n\\begin{code}\ndata Ctx\u22c6 : Set where\n \u2205 : Ctx\u22c6\n _,\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Ctx\u22c6\n\\end{code}\n\nLet `\u03a6`, `\u03a8` range over type contexts.\n\n## Type variables\n\nType variables are not named, they are numbered (de Bruijn indices).\n\nA type variable is indexed by its context and kind. For a given\ncontext, it's impossible to construct a variable that is out of\nscope.\n\n\\begin{code}\ndata _\u220b\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Set where\n\n Z : \u2200 {\u03a6 J}\n -------------\n \u2192 \u03a6 ,\u22c6 J \u220b\u22c6 J\n\n S : \u2200 {\u03a6 J K}\n \u2192 \u03a6 \u220b\u22c6 J\n -------------\n \u2192 \u03a6 ,\u22c6 K \u220b\u22c6 J\n\\end{code}\n\nLet `\u03b1`, `\u03b2` range over type variables.\n\n## Types\n\nA type is indexed by its context and kind. Types are intrinsically\nscoped and kinded: it's impossible to construct an ill-kinded\napplication and it's impossible to refer to a variable that is not in\nscope.\n\nA type is either a type variable, a pi type, a function type, a\nlambda, an application, an iso-recursive type `\u03bc`, a size, or a type\nconstant (base type). Note that recursive types range over an\narbitrary kind `k` which goes beyond standard iso-recursive types.\n\n\\begin{code}\nopen import Data.String\n\ndata _\u22a2\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Set where\n\n ` : \u2200 {\u03a6 J}\n \u2192 \u03a6 \u220b\u22c6 J\n --------\n \u2192 \u03a6 \u22a2\u22c6 J\n\n \u03a0 : \u2200 {\u03a6 K}\n \u2192 String\n \u2192 \u03a6 ,\u22c6 K \u22a2\u22c6 *\n -----------\n \u2192 \u03a6 \u22a2\u22c6 *\n\n _\u21d2_ : \u2200 {\u03a6}\n \u2192 \u03a6 \u22a2\u22c6 *\n \u2192 \u03a6 \u22a2\u22c6 *\n ------\n \u2192 \u03a6 \u22a2\u22c6 *\n\n \u019b : \u2200 {\u03a6 K J}\n \u2192 String\n \u2192 \u03a6 ,\u22c6 K \u22a2\u22c6 J \n -----------\n \u2192 \u03a6 \u22a2\u22c6 K \u21d2 J\n\n _\u00b7_ : \u2200{\u03a6 K J}\n \u2192 \u03a6 \u22a2\u22c6 K \u21d2 J\n \u2192 \u03a6 \u22a2\u22c6 K\n ------\n \u2192 \u03a6 \u22a2\u22c6 J\n\n \u03bc1 : \u2200{\u03c6 K}\n --------------------------------\n \u2192 \u03c6 \u22a2\u22c6 ((K \u21d2 *) \u21d2 K \u21d2 *) \u21d2 K \u21d2 *\n\n con : \u2200{\u03c6}\n \u2192 TyCon\n ------\n \u2192 \u03c6 \u22a2\u22c6 *\n\n\\end{code}\n\nLet `A`, `B`, `C` range over types.\n\n## Type Abbreviations\nTODO: these should be in the stdlib\n\\begin{code}\nunit : \u2200{\u0393} \u2192 \u0393 \u22a2\u22c6 *\nunit = \u03a0 \"\u03b1\" (` Z \u21d2 ` Z)\n\nboolean : \u2200{\u0393} \u2192 \u0393 \u22a2\u22c6 *\nboolean = \u03a0 \"\u03b1\" (` Z \u21d2 ` Z \u21d2 ` Z)\n\\end{code}\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality\n\ndata _\u2261\u03b1_ {\u0393} : \u2200{J} \u2192 \u0393 \u22a2\u22c6 J \u2192 \u0393 \u22a2\u22c6 J \u2192 Set where\n var\u2261\u03b1 : \u2200{J}{\u03b1 \u03b1' : \u0393 \u220b\u22c6 J} \u2192 \u03b1 \u2261 \u03b1' \u2192 ` \u03b1 \u2261\u03b1 ` \u03b1'\n\n \u21d2\u2261\u03b1 : {A A' B B' : \u0393 \u22a2\u22c6 *}\n \u2192 A \u2261\u03b1 A'\n \u2192 B \u2261\u03b1 B'\n ---------------------\n \u2192 (A \u21d2 B) \u2261\u03b1 (A' \u21d2 B')\n \n \u03a0\u2261\u03b1 : \u2200{J}{B B' : \u0393 ,\u22c6 J \u22a2\u22c6 *}{x}{x'}\n \u2192 B \u2261\u03b1 B'\n -------\n \u2192 \u03a0 x B \u2261\u03b1 \u03a0 x' B'\n \n \u019b\u2261\u03b1 : \u2200{K J}{B B' : \u0393 ,\u22c6 J \u22a2\u22c6 K}{x}{x'}\n \u2192 B \u2261\u03b1 B'\n ---------------\n \u2192 \u019b x B \u2261\u03b1 \u019b x' B'\n \n \u00b7\u2261\u03b1 : \u2200{K J}{A A' : \u0393 \u22a2\u22c6 K \u21d2 J}{B B' : \u0393 \u22a2\u22c6 K}\n \u2192 A \u2261\u03b1 A'\n \u2192 B \u2261\u03b1 B'\n --------------------\n \u2192 (A \u00b7 B) \u2261\u03b1 (A' \u00b7 B')\n\n \u03bc\u2261\u03b1 : \u2200{K} \u2192 \u03bc1 {K = K} \u2261\u03b1 \u03bc1\n\n con\u2261\u03b1 : \u2200 {c} \u2192 con c \u2261\u03b1 con c\n\nrefl\u03b1 : \u2200{\u03a6 J}{A : \u03a6 \u22a2\u22c6 J} \u2192 A \u2261\u03b1 A\nrefl\u03b1 {A = ` \u03b1} = var\u2261\u03b1 refl\nrefl\u03b1 {A = \u03a0 x A} = \u03a0\u2261\u03b1 refl\u03b1\nrefl\u03b1 {A = A \u21d2 B} = \u21d2\u2261\u03b1 refl\u03b1 refl\u03b1\nrefl\u03b1 {A = \u019b x A} = \u019b\u2261\u03b1 refl\u03b1\nrefl\u03b1 {A = A \u00b7 B} = \u00b7\u2261\u03b1 refl\u03b1 refl\u03b1\nrefl\u03b1 {A = \u03bc1} = \u03bc\u2261\u03b1\nrefl\u03b1 {A = con c} = con\u2261\u03b1\n\n\nsym\u03b1 : \u2200{\u03a6 J}{A A' : \u03a6 \u22a2\u22c6 J} \u2192 A \u2261\u03b1 A' \u2192 A' \u2261\u03b1 A\nsym\u03b1 (var\u2261\u03b1 p) = var\u2261\u03b1 (sym p)\nsym\u03b1 (\u21d2\u2261\u03b1 p q) = \u21d2\u2261\u03b1 (sym\u03b1 p) (sym\u03b1 q)\nsym\u03b1 (\u03a0\u2261\u03b1 p) = \u03a0\u2261\u03b1 (sym\u03b1 p)\nsym\u03b1 (\u019b\u2261\u03b1 p) = \u019b\u2261\u03b1 (sym\u03b1 p)\nsym\u03b1 (\u00b7\u2261\u03b1 p q) = \u00b7\u2261\u03b1 (sym\u03b1 p) (sym\u03b1 q)\nsym\u03b1 \u03bc\u2261\u03b1 = \u03bc\u2261\u03b1\nsym\u03b1 con\u2261\u03b1 = con\u2261\u03b1\n\ntrans\u03b1 : \u2200{\u03a6 J}{A A' A'' : \u03a6 \u22a2\u22c6 J} \u2192 A \u2261\u03b1 A' \u2192 A' \u2261\u03b1 A'' \u2192 A \u2261\u03b1 A''\ntrans\u03b1 (var\u2261\u03b1 p) (var\u2261\u03b1 p') = var\u2261\u03b1 (trans p p')\ntrans\u03b1 (\u21d2\u2261\u03b1 p q) (\u21d2\u2261\u03b1 p' q') = \u21d2\u2261\u03b1 (trans\u03b1 p p') (trans\u03b1 q q')\ntrans\u03b1 (\u03a0\u2261\u03b1 p) (\u03a0\u2261\u03b1 p') = \u03a0\u2261\u03b1 (trans\u03b1 p p')\ntrans\u03b1 (\u019b\u2261\u03b1 p) (\u019b\u2261\u03b1 p') = \u019b\u2261\u03b1 (trans\u03b1 p p')\ntrans\u03b1 (\u00b7\u2261\u03b1 p q) (\u00b7\u2261\u03b1 p' q') = \u00b7\u2261\u03b1 (trans\u03b1 p p') (trans\u03b1 q q')\ntrans\u03b1 \u03bc\u2261\u03b1 \u03bc\u2261\u03b1 = \u03bc\u2261\u03b1\ntrans\u03b1 con\u2261\u03b1 con\u2261\u03b1 = con\u2261\u03b1\n\\end{code}\n","avg_line_length":20.8356807512,"max_line_length":70,"alphanum_fraction":0.4957187922} +{"size":271,"ext":"lagda","lang":"Literate Agda","max_stars_count":23.0,"content":"%!TeX jobNames = job-1, job-2\n%!TeX job-2:outputDirectory = output\n%!TeX check = lhs2TeX -V\n\\documentclass{article}\n\n\\def\\textmu{}\n\n%include agda.fmt\n\n\\begin{document}\n\nThe identity function:\n\n\\begin{code}\nid : {S : Set} -> S -> S\nid {S} x = x\n\\end{code}\n\n\\end{document}\n","avg_line_length":13.55,"max_line_length":36,"alphanum_fraction":0.6568265683} +{"size":9363,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule AlgorithmicRed.Term.RenamingSubstitution where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Function using (id; _\u2218_)\nopen import Relation.Binary.PropositionalEquality\n renaming (subst to substEq) using (_\u2261_; refl; cong; cong\u2082; trans; sym)\nopen import Data.Unit\n\nopen import Type\nopen import Type.BetaNormal\nopen import Type.Reduction\nimport Type.RenamingSubstitution as \u22c6\nopen import Type.BetaNBE.RenamingSubstitution\nopen import Type.Equality\nopen import Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con booleanNf\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con\nopen import AlgorithmicRed.Term\n\\end{code}\n\n\n## Renaming\n\\begin{code}\nRen : \u2200 \u0393 \u0394 \u2192 \u22c6.Ren \u2225 \u0393 \u2225 \u2225 \u0394 \u2225 \u2192 Set\nRen \u0393 \u0394 \u03c1 = \u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b renNf \u03c1 A\n\\end{code}\n\n\n\\begin{code}\next : \u2200 {\u0393 \u0394}\n \u2192 (\u03c1\u22c6 : \u22c6.Ren \u2225 \u0393 \u2225 \u2225 \u0394 \u2225)\n \u2192 Ren \u0393 \u0394 \u03c1\u22c6\n ------------------------------------------------------------\n \u2192 (\u2200 {K }{B : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K} \u2192 Ren (\u0393 , B) (\u0394 , renNf \u03c1\u22c6 B) \u03c1\u22c6)\next \u03c1\u22c6 \u03c1 Z = Z\next \u03c1\u22c6 \u03c1 (S x) = S (\u03c1 x)\n\\end{code}\n\n\\begin{code}\next\u22c6 : \u2200 {\u0393 \u0394}\n \u2192 (\u03c1\u22c6 : \u22c6.Ren \u2225 \u0393 \u2225 \u2225 \u0394 \u2225)\n \u2192 Ren \u0393 \u0394 \u03c1\u22c6\n ----------------------------------------\n \u2192 \u2200 {K} \u2192 Ren (\u0393 ,\u22c6 K) (\u0394 ,\u22c6 K) (\u22c6.ext \u03c1\u22c6)\next\u22c6 {\u0393}{\u0394} \u03c1\u22c6 \u03c1 {K}{A} (T x) =\n substEq (\u03bb A \u2192 \u0394 ,\u22c6 K \u220b A)\n (trans (sym (renNf-comp _)) (renNf-comp _))\n (T (\u03c1 x))\n\\end{code}\n\n\\begin{code}\nrenTermCon : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c1\u22c6 : \u2200 {J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u03a8 \u220b\u22c6 J)\n -----------------------------------------------------\n \u2192 ({A : \u03a6 \u22a2Nf\u22c6 *} \u2192 TermCon A \u2192 TermCon (renNf \u03c1\u22c6 A ))\nrenTermCon \u03c1\u22c6 (integer i) = integer i\nrenTermCon \u03c1\u22c6 (bytestring b) = bytestring b\n\\end{code}\n\n\\begin{code}\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List\n\nren : \u2200 {\u0393 \u0394}\n \u2192 (\u03c1\u22c6 : \u22c6.Ren \u2225 \u0393 \u2225 \u2225 \u0394 \u2225)\n \u2192 Ren \u0393 \u0394 \u03c1\u22c6\n ------------------------\n \u2192 (\u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u22a2 A \u2192 \u0394 \u22a2 renNf \u03c1\u22c6 A )\n\nrenTel : \u2200 {\u0393 \u0393' \u0394}\n \u2192 (\u03c1\u22c6 : \u22c6.Ren \u2225 \u0393 \u2225 \u2225 \u0393' \u2225)\n \u2192 Ren \u0393 \u0393' \u03c1\u22c6\n \u2192 {\u03c3 : \u2200 {J} \u2192 \u0394 \u220b\u22c6 J \u2192 \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J}\n \u2192 {As : List (\u0394 \u22a2Nf\u22c6 *)}\n \u2192 Tel \u0393 \u0394 \u03c3 As\n \u2192 Tel \u0393' \u0394 (renNf \u03c1\u22c6 \u2218 \u03c3) As\n\nrenTel \u03c1\u22c6 \u03c1 {As = []} _ = _\nrenTel \u03c1\u22c6 \u03c1 {\u03c3 = \u03c3}{As = A \u2237 As} (R ,, (p ,, q ,, r ,, s) ,, M ,, Ms) =\n renNf \u03c1\u22c6 R\n ,,\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n substEq\n (_\u2014\u2192\u22c6 \u22c6.ren \u03c1\u22c6 p)\n (trans\n (sym (\u22c6.ren-subst (embNf A)))\n (\u22c6.subst-cong (\u03bb x \u2192 sym (ren-embNf \u03c1\u22c6 (\u03c3 x))) (embNf A)))\n (ren\u2014\u2192\u22c6 \u03c1\u22c6 q)\n ,,\n renValue\u22c6 \u03c1\u22c6 r\n ,,\n trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\n ,,\n ren \u03c1\u22c6 \u03c1 M \n ,,\n renTel \u03c1\u22c6 \u03c1 Ms\n \nren \u03c1\u22c6 \u03c1 (` x) = ` (\u03c1 x)\nren \u03c1\u22c6 \u03c1 (\u019b {A' = A'} (A ,, p ,, q ,, r) N) = \u019b (\n \u22c6.ren \u03c1\u22c6 A\n ,,\n ren\u2014\u2192\u22c6 \u03c1\u22c6 p\n ,,\n renValue\u22c6 \u03c1\u22c6 q\n ,,\n trans (ren-embNf \u03c1\u22c6 A') (cong (\u22c6.ren \u03c1\u22c6) r)) (ren \u03c1\u22c6 (ext \u03c1\u22c6 \u03c1) N)\nren \u03c1\u22c6 \u03c1 (L \u00b7 M) = ren \u03c1\u22c6 \u03c1 L \u00b7 ren \u03c1\u22c6 \u03c1 M \nren \u03c1\u22c6 \u03c1 (\u039b N) = \u039b (ren (\u22c6.ext \u03c1\u22c6) (ext\u22c6 \u03c1\u22c6 \u03c1) N )\nren {\u0393}{\u0394} \u03c1\u22c6 \u03c1 (_\u00b7\u22c6_ {B = B} t A {R} (p ,, q ,, r ,, s)) = _\u00b7\u22c6_\n (ren \u03c1\u22c6 \u03c1 t)\n (renNf \u03c1\u22c6 A)\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n substEq\n (_\u2014\u2192\u22c6 \u22c6.ren \u03c1\u22c6 p)\n (trans\n (sym (\u22c6.ren-subst (embNf B)))\n (trans\n (trans\n (\u22c6.subst-cong (sym \u2218 \u22c6.ren-subst-cons \u03c1\u22c6 (embNf A)) (embNf B))\n (\u22c6.subst-ren (embNf B)))\n (cong\u2082\n \u22c6._[_]\n (sym (ren-embNf (\u22c6.ext \u03c1\u22c6) B))\n (sym (ren-embNf \u03c1\u22c6 A)))))\n (ren\u2014\u2192\u22c6 \u03c1\u22c6 q)\n ,, renValue\u22c6 \u03c1\u22c6 r\n ,, trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\nren \u03c1\u22c6 \u03c1 (wrap1 pat arg {R} (p ,, q ,, r ,, s) t) = wrap1\n _\n _\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n substEq\n (_\u2014\u2192\u22c6 \u22c6.ren \u03c1\u22c6 p)\n (cong\u2082\n (\u03bb t u \u2192 t \u00b7 (\u03bc1 \u00b7 t) \u00b7 u)\n (sym (ren-embNf \u03c1\u22c6 pat))\n (sym (ren-embNf \u03c1\u22c6 arg)))\n (ren\u2014\u2192\u22c6 \u03c1\u22c6 q)\n ,,\n renValue\u22c6 \u03c1\u22c6 r\n ,,\n trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\n (ren \u03c1\u22c6 \u03c1 t)\nren \u03c1\u22c6 \u03c1 (unwrap1 {pat = pat}{arg = arg} t {R = R} (p ,, q ,, r ,, s)) = unwrap1\n (ren \u03c1\u22c6 \u03c1 t)\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n substEq\n (_\u2014\u2192\u22c6 \u22c6.ren \u03c1\u22c6 p)\n (cong\u2082\n (\u03bb t u \u2192 t \u00b7 (\u03bc1 \u00b7 t) \u00b7 u)\n (sym (ren-embNf \u03c1\u22c6 pat))\n (sym (ren-embNf \u03c1\u22c6 arg)))\n (ren\u2014\u2192\u22c6 \u03c1\u22c6 q)\n ,,\n renValue\u22c6 \u03c1\u22c6 r\n ,,\n trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\nren \u03c1\u22c6 \u03c1 (con cn) = con (renTermCon \u03c1\u22c6 cn)\nren {\u0393} {\u0394} \u03c1\u22c6 \u03c1 (builtin bn \u03c3 X {R = R} (p ,, q ,, r ,, s)) = builtin\n bn\n (renNf \u03c1\u22c6 \u2218 \u03c3)\n (renTel \u03c1\u22c6 \u03c1 X)\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n substEq\n (_\u2014\u2192\u22c6 \u22c6.ren \u03c1\u22c6 p)\n (trans\n (sym (\u22c6.ren-subst (embNf (proj\u2082 (proj\u2082 (SIG bn))))))\n (\u22c6.subst-cong\n (\u03bb x \u2192 sym (ren-embNf \u03c1\u22c6 (\u03c3 x)))\n (embNf (proj\u2082 (proj\u2082 (SIG bn))))))\n (ren\u2014\u2192\u22c6 \u03c1\u22c6 q)\n ,,\n renValue\u22c6 \u03c1\u22c6 r\n ,,\n trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\nren \u03c1\u22c6 \u03c1 (error A {R = R} (p ,, q ,, r ,, s)) = error\n (\u22c6.ren \u03c1\u22c6 A)\n (\u22c6.ren \u03c1\u22c6 p\n ,,\n ren\u2014\u2192\u22c6 \u03c1\u22c6 q\n ,,\n renValue\u22c6 \u03c1\u22c6 r\n ,,\n trans (ren-embNf \u03c1\u22c6 R) (cong (\u22c6.ren \u03c1\u22c6) s))\n\\end{code}\n\n\\begin{code}\nweaken : \u2200 {\u03a6 J}{A : \u2225 \u03a6 \u2225 \u22a2Nf\u22c6 J}{K}{B : \u2225 \u03a6 \u2225 \u22a2Nf\u22c6 K}\n \u2192 \u03a6 \u22a2 A\n -------------\n \u2192 \u03a6 , B \u22a2 A\nweaken {\u03a6}{J}{A}{K}{B} x =\n substEq (\u03bb x \u2192 \u03a6 , B \u22a2 x)\n (renNf-id A)\n (ren id\n (\u03bb x \u2192 substEq (\u03bb A \u2192 \u03a6 , B \u220b A) (sym (renNf-id _)) (S x))\n x)\n\\end{code}\n\n\\begin{code}\nweaken\u22c6 : \u2200 {\u03a6 J}{A : \u2225 \u03a6 \u2225 \u22a2Nf\u22c6 J}{K}\n \u2192 \u03a6 \u22a2 A\n ------------------\n \u2192 \u03a6 ,\u22c6 K \u22a2 weakenNf A\nweaken\u22c6 x = ren _\u220b\u22c6_.S _\u220b_.T x\n\\end{code}\n\n## Substitution\n\\begin{code}\nSub : \u2200 \u0393 \u0394 \u2192 (\u2200{J} \u2192 \u2225 \u0393 \u2225 \u220b\u22c6 J \u2192 \u2225 \u0394 \u2225 \u22a2Nf\u22c6 J) \u2192 Set\nSub \u0393 \u0394 \u03c3 = \u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 substNf \u03c3 A\n\\end{code}\n\n\n\\begin{code}\nexts : \u2200 {\u0393 \u0394}\n \u2192 (\u03c3\u22c6 : \u2200 {K} \u2192 \u2225 \u0393 \u2225 \u220b\u22c6 K \u2192 \u2225 \u0394 \u2225 \u22a2Nf\u22c6 K)\n \u2192 (\u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 substNf \u03c3\u22c6 A)\n ---------------------------------------------------\n \u2192 (\u2200 {J} {K} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} {B : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 K}\n \u2192 \u0393 , B \u220b A\n -------------------------------\n \u2192 \u0394 , substNf \u03c3\u22c6 B \u22a2 substNf \u03c3\u22c6 A)\nexts \u03c3\u22c6 \u03c3 Z = ` Z\nexts \u03c3\u22c6 \u03c3 (S x) = weaken (\u03c3 x)\n\\end{code}\n\n\\begin{code}\n{-\nexts\u22c6 : \u2200 {\u0393 \u0394}\n \u2192 (\u03c3\u22c6 : \u2200 {K} \u2192 \u2225 \u0393 \u2225 \u220b\u22c6 K \u2192 \u2225 \u0394 \u2225 \u22a2Nf\u22c6 K)\n \u2192 (\u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 substNf \u03c3\u22c6 A)\n ---------------------------------------------------\n \u2192 (\u2200 {J K}{A : \u2225 \u0393 ,\u22c6 K \u2225 \u22a2Nf\u22c6 J}\n \u2192 \u0393 ,\u22c6 K \u220b A \n -------------------------------\n \u2192 \u0394 ,\u22c6 K \u22a2 substNf (extsNf \u03c3\u22c6) A )\nexts\u22c6 {\u0393}{\u0394} \u03c3\u22c6 \u03c3 {J}{K}(T {A = A} x) =\n substEq (\u03bb x \u2192 \u0394 ,\u22c6 K \u22a2 x)\n {! trans (sym (\u22c6.ren-subst A))\n (\u22c6.subst-ren A) !}\n (weaken\u22c6 (\u03c3 x))\n-}\n\\end{code}\n\n\\begin{code}\n{-\nsubstTermCon : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c3\u22c6 : \u2200 {J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u03a8 \u22a2\u22c6 J)\n ------------------------\n \u2192 ({A : \u03a6 \u22a2\u22c6 *} \u2192 TermCon A \u2192 TermCon (\u22c6.subst \u03c3\u22c6 A ))\nsubstTermCon \u03c3\u22c6 (integer s i p) = integer s i p\nsubstTermCon \u03c3\u22c6 (bytestring s b p) = bytestring s b p\nsubstTermCon \u03c3\u22c6 (size s) = size s\n-}\n\\end{code}\n\n\n\\begin{code}\n{-\nsubst : \u2200 {\u0393 \u0394}\n \u2192 (\u03c3\u22c6 : \u2200 {K} \u2192 \u2225 \u0393 \u2225 \u220b\u22c6 K \u2192 \u2225 \u0394 \u2225 \u22a2\u22c6 K)\n \u2192 (\u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 \u22c6.subst \u03c3\u22c6 A)\n ---------------------------------------------------\n \u2192 (\u2200 {J} {A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} \u2192 \u0393 \u22a2 A \u2192 \u0394 \u22a2 \u22c6.subst \u03c3\u22c6 A)\n\nsubstTel : \u2200 {\u0393 \u0393' \u0394}\n \u2192 (\u03c3\u22c6 : \u22c6.Sub \u2225 \u0393 \u2225 \u2225 \u0393' \u2225)\n \u2192 Sub \u0393 \u0393' \u03c3\u22c6\n \u2192 {\u03c3' : \u22c6.Sub \u0394 \u2225 \u0393 \u2225}\n \u2192 {As : List (\u0394 \u22a2\u22c6 *)}\n \u2192 Tel \u0393 \u0394 \u03c3' As\n \u2192 Tel \u0393' \u0394 (\u22c6.subst \u03c3\u22c6 \u2218 \u03c3') As\nsubstTel \u03c3\u22c6 \u03c3 {As = []} _ = _\nsubstTel \u03c3\u22c6 \u03c3 {As = A \u2237 As} (M ,, Ms) =\n substEq (_ \u22a2_) (sym (\u22c6.subst-comp A)) (subst \u03c3\u22c6 \u03c3 M) ,, substTel \u03c3\u22c6 \u03c3 Ms\n\nsubst \u03c3\u22c6 \u03c3 (` k) = \u03c3 k\nsubst \u03c3\u22c6 \u03c3 (\u019b N) = \u019b (subst \u03c3\u22c6 (exts \u03c3\u22c6 \u03c3) N)\nsubst \u03c3\u22c6 \u03c3 (L \u00b7 M) = subst \u03c3\u22c6 \u03c3 L \u00b7 subst \u03c3\u22c6 \u03c3 M\nsubst \u03c3\u22c6 \u03c3 (\u039b N) = \u039b (subst (\u22c6.exts \u03c3\u22c6) (exts\u22c6 \u03c3\u22c6 \u03c3) N)\nsubst {\u0393}{\u0394} \u03c3\u22c6 \u03c3 (_\u00b7\u22c6_ {B = B} L M) =\n substEq (\u03bb A \u2192 \u0394 \u22a2 A)\n (trans (sym (\u22c6.subst-comp B))\n (trans (\u22c6.subst-cong (\u22c6.subst-subst-cons \u03c3\u22c6 M)\n B)\n (\u22c6.subst-comp B)))\n (subst \u03c3\u22c6 \u03c3 L \u00b7\u22c6 \u22c6.subst \u03c3\u22c6 M)\nsubst \u03c3\u22c6 \u03c3 (wrap1 pat arg t) = wrap1 _ _ (subst \u03c3\u22c6 \u03c3 t)\nsubst \u03c3\u22c6 \u03c3 (unwrap1 t) = unwrap1 (subst \u03c3\u22c6 \u03c3 t)\nsubst \u03c3\u22c6 \u03c3 (con cn) = con (substTermCon \u03c3\u22c6 cn)\nsubst {\u0393}{\u0393'} \u03c3\u22c6 \u03c3 (builtin bn \u03c3' tel ) = substEq\n (\u0393' \u22a2_)\n (\u22c6.subst-comp (proj\u2082 (proj\u2082 (SIG bn))))\n (builtin bn (\u22c6.subst \u03c3\u22c6 \u2218 \u03c3') (substTel \u03c3\u22c6 \u03c3 tel))\nsubst \u03c3\u22c6 \u03c3 (error A) = error (\u22c6.subst \u03c3\u22c6 A)\n-}\n\\end{code}\n\n\\begin{code}\nsubstcons : \u2200{\u0393 \u0394} \u2192\n (\u03c3\u22c6 : \u2200{K} \u2192 \u2225 \u0393 \u2225 \u220b\u22c6 K \u2192 \u2225 \u0394 \u2225 \u22a2Nf\u22c6 K)\n \u2192 (\u2200 {J}{A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 substNf \u03c3\u22c6 A)\n \u2192 \u2200{J}{A : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J}\n \u2192 (t : \u0394 \u22a2 substNf \u03c3\u22c6 A)\n ---------------------\n \u2192 (\u2200 {J} {B : \u2225 \u0393 \u2225 \u22a2Nf\u22c6 J} \u2192 \u0393 , A \u220b B \u2192 \u0394 \u22a2 substNf \u03c3\u22c6 B)\nsubstcons \u03c3\u22c6 \u03c3 t Z = t\nsubstcons \u03c3\u22c6 \u03c3 t (S x) = \u03c3 x\n\\end{code}\n\n\\begin{code}\n{-\n_[_] : \u2200 {J \u0393} {A B : \u2225 \u0393 \u2225 \u22a2\u22c6 J}\n \u2192 \u0393 , B \u22a2 A\n \u2192 \u0393 \u22a2 B \n ---------\n \u2192 \u0393 \u22a2 A\n_[_] {J} {\u0393}{A}{B} t s =\n substEq (\u03bb A \u2192 \u0393 \u22a2 A)\n (\u22c6.subst-id A)\n (subst `\n (substcons `\n (\u03bb x \u2192 substEq (\u03bb A \u2192 \u0393 \u22a2 A)\n (sym (\u22c6.subst-id _))\n (` x))\n (substEq (\u03bb A \u2192 \u0393 \u22a2 A) (sym (\u22c6.subst-id B)) s))\n t) \n-}\n\\end{code}\n\n\\begin{code}\n{-\n_[_]\u22c6 : \u2200 {J \u0393 K} {B : \u2225 \u0393 ,\u22c6 K \u2225 \u22a2\u22c6 J}\n \u2192 \u0393 ,\u22c6 K \u22a2 B\n \u2192 (A : \u2225 \u0393 \u2225 \u22a2\u22c6 K)\n ---------\n \u2192 \u0393 \u22a2 B \u22c6.[ A ]\n_[_]\u22c6 {J}{\u0393}{K}{B} t A =\n subst (\u22c6.subst-cons ` A)\n (\u03bb{(T {A = A'} x) \u2192 substEq (\u03bb A \u2192 \u0393 \u22a2 A)\n (trans (sym (\u22c6.subst-id A'))\n (\u22c6.subst-ren A'))\n (` x)})\n t\n-}\n\\end{code}\n","avg_line_length":26.2268907563,"max_line_length":80,"alphanum_fraction":0.387055431} +{"size":1751,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\nmodule Ledger where\n\nopen import Prelude\nopen import Utils\nopen import Crypto\nopen import Transactions\nopen import RawTransactions\nopen import TXTree\nopen import RawTXTree\n\\end{code}\n\n%<*ledgernoid>\n\\begin{code}\nledgerOutNoId : \u2200 (outputs : List TXField) (addr : Address)\n \u2192 Amount\nledgerOutNoId [] addr = zero\nledgerOutNoId (output \u2237 outputs) addr with TXField.address output == addr\n... | yes _ = TXField.amount output + ledgerOutNoId outputs addr\n... | no _ = ledgerOutNoId outputs addr\n\\end{code}\n%<\/ledgernoid>\n\n%<*ledgerout>\n\\begin{code}\nledgerOut : \u2200 (outputs : List TXFieldWithId) (addr : Address)\n \u2192 Amount\nledgerOut [] addr = zero\nledgerOut (output \u2237 outputs) addr with TXFieldWithId.address output == addr\n... | yes _ = TXFieldWithId.amount output + ledgerOut outputs addr\n... | no _ = ledgerOut outputs addr\n\\end{code}\n%<\/ledgerout>\n\n%<*ledgertree>\n\\begin{code}\nledgerTree : (rawTXTree : RawTXTree) (addr : Address) \u2192 Amount\nledgerTree txTree = ledgerOut outputs\n where open RawTXTree.RawTXTree txTree\n\\end{code}\n%<\/ledgertree>\n\n%<*deltarawtx>\n\\begin{code}\ndeltaRawTX : (tx : RawTX) (addr : Address) \u2192 Int\ndeltaRawTX (coinbase record { outputs = outputs }) = pos \u2218 ledgerOut outputs\ndeltaRawTX (normalTX record { inputs = [] ; outputs = outputs }) addr =\n pos $ ledgerOutNoId outputs addr\ndeltaRawTX (normalTX record { inputs = (record { time = _ ; position = _ ; amount = amount ; msg = _ ;\n signature = _ ; publicKey = pk } \u2237 inputs) ; outputs = outputs }) addr\n with addr == publicKey2Address pk\n... | yes _ = deltaRawTX (normalTX (record { inputs = inputs ; outputs = outputs })) addr - pos amount\n... | no _ = deltaRawTX (normalTX (record { inputs = inputs ; outputs = outputs })) addr\n\\end{code}\n%<\/deltarawtx>\n","avg_line_length":31.2678571429,"max_line_length":102,"alphanum_fraction":0.7081667619} +{"size":151,"ext":"lagda","lang":"Literate Agda","max_stars_count":1989.0,"content":"\\documentclass{article}\n\n\\usepackage{agda}\n\n\\begin{document}\n\n\\begin{code} \nmodule AccidentalSpacesAfterBeginCode where\n\\end{code}\n\n\\end{document}\n","avg_line_length":12.5833333333,"max_line_length":43,"alphanum_fraction":0.761589404} +{"size":28156,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"---\ntitle : \"Assignment4: TSPL Assignment 4\"\nlayout : page\npermalink : \/Assignment4\/\n---\n\n\\begin{code}\nmodule Assignment4 where\n\\end{code}\n\n## YOUR NAME AND EMAIL GOES HERE\n\n\n## Introduction\n\n\n\nYou must do _all_ the exercises labelled \"(recommended)\".\n\nExercises labelled \"(stretch)\" are there to provide an extra challenge.\nYou don't need to do all of these, but should attempt at least a few.\n\nExercises without a label are optional, and may be done if you want\nsome extra practice.\n\n\nPlease ensure your files execute correctly under Agda!\n\n**IMPORTANT** For ease of marking, when modifying the given code please write\n\n -- begin\n -- end\n\nbefore and after code you add, to indicate your changes.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_)\nopen import Data.Product using (_\u00d7_; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.String using (String)\nopen import Data.String.Unsafe using (_\u225f_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\\end{code}\n\n\n## DeBruijn\n\n\n\\begin{code}\nmodule DeBruijn where\n\\end{code}\n\nRemember to indent all code by two spaces.\n\n\\begin{code}\n open import plfa.DeBruijn\n\\end{code}\n\n\n#### Exercise (`mul`) (recommended)\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers, now adapted to the inherently typed\nDeBruijn representation.\n\n\n#### Exercise `V\u00ac\u2014\u2192`\n\nFollowing the previous development, show values do\nnot reduce, and its corollary, terms that reduce are not\nvalues.\n\n\n#### Exercise `mul-example` (recommended)\n\nUsing the evaluator, confirm that two times two is four.\n\n\n## More\n\n\\begin{code}\nmodule More where\n\\end{code}\n\nRemember to indent all code by two spaces.\n\n\n### Syntax\n\n\\begin{code}\n infix 4 _\u22a2_\n infix 4 _\u220b_\n infixl 5 _,_\n\n infixr 7 _\u21d2_\n infixr 8 _`\u228e_\n infixr 9 _`\u00d7_\n\n infix 5 \u019b_\n infix 5 \u03bc_\n infixl 7 _\u00b7_\n infixl 8 _`*_\n infix 8 `suc_\n infix 9 `_\n infix 9 S_\n infix 9 #_\n\\end{code}\n\n### Types\n\n\\begin{code}\n data Type : Set where\n `\u2115 : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n Nat : Type\n _`\u00d7_ : Type \u2192 Type \u2192 Type\n _`\u228e_ : Type \u2192 Type \u2192 Type\n `\u22a4 : Type\n `\u22a5 : Type\n `List : Type \u2192 Type\n\\end{code}\n\n### Contexts\n\n\\begin{code}\n data Context : Set where\n \u2205 : Context\n _,_ : Context \u2192 Type \u2192 Context\n\\end{code}\n\n### Variables and the lookup judgment\n\n\\begin{code}\n data _\u220b_ : Context \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 A}\n ---------\n \u2192 \u0393 , A \u220b A\n\n S_ : \u2200 {\u0393 A B}\n \u2192 \u0393 \u220b B\n ---------\n \u2192 \u0393 , A \u220b B\n\\end{code} \n\n### Terms and the typing judgment\n\n\\begin{code}\n data _\u22a2_ : Context \u2192 Type \u2192 Set where\n\n -- variables\n\n `_ : \u2200 {\u0393 A}\n \u2192 \u0393 \u220b A\n -----\n \u2192 \u0393 \u22a2 A\n\n -- functions\n\n \u019b_ : \u2200 {\u0393 A B}\n \u2192 \u0393 , A \u22a2 B\n ---------\n \u2192 \u0393 \u22a2 A \u21d2 B\n\n _\u00b7_ : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A \u21d2 B\n \u2192 \u0393 \u22a2 A\n ---------\n \u2192 \u0393 \u22a2 B\n\n -- naturals\n\n `zero : \u2200 {\u0393}\n ------\n \u2192 \u0393 \u22a2 `\u2115\n\n `suc_ : \u2200 {\u0393}\n \u2192 \u0393 \u22a2 `\u2115\n ------\n \u2192 \u0393 \u22a2 `\u2115\n\n case : \u2200 {\u0393 A}\n \u2192 \u0393 \u22a2 `\u2115\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 , `\u2115 \u22a2 A\n -----\n \u2192 \u0393 \u22a2 A\n\n -- fixpoint\n\n \u03bc_ : \u2200 {\u0393 A}\n \u2192 \u0393 , A \u22a2 A\n ----------\n \u2192 \u0393 \u22a2 A\n\n -- primitive numbers\n\n con : \u2200 {\u0393}\n \u2192 \u2115\n -------\n \u2192 \u0393 \u22a2 Nat\n\n _`*_ : \u2200 {\u0393}\n \u2192 \u0393 \u22a2 Nat\n \u2192 \u0393 \u22a2 Nat\n -------\n \u2192 \u0393 \u22a2 Nat\n\n -- let\n\n `let : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 , A \u22a2 B\n ----------\n \u2192 \u0393 \u22a2 B\n\n -- products\n\n `\u27e8_,_\u27e9 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 \u22a2 B\n -----------\n \u2192 \u0393 \u22a2 A `\u00d7 B\n\n `proj\u2081 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n -----------\n \u2192 \u0393 \u22a2 A\n\n `proj\u2082 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n -----------\n \u2192 \u0393 \u22a2 B\n\n -- alternative formulation of products\n\n case\u00d7 : \u2200 {\u0393 A B C}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n \u2192 \u0393 , A , B \u22a2 C\n --------------\n \u2192 \u0393 \u22a2 C\n\n\\end{code}\n\n### Abbreviating de Bruijn indices\n\n\\begin{code}\n lookup : Context \u2192 \u2115 \u2192 Type\n lookup (\u0393 , A) zero = A\n lookup (\u0393 , _) (suc n) = lookup \u0393 n\n lookup \u2205 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n count : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 \u0393 \u220b lookup \u0393 n\n count {\u0393 , _} zero = Z\n count {\u0393 , _} (suc n) = S (count n)\n count {\u2205} _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n #_ : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 \u0393 \u22a2 lookup \u0393 n\n # n = ` count n\n\\end{code}\n\n## Renaming\n\n\\begin{code}\n ext : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A) \u2192 (\u2200 {A B} \u2192 \u0393 , A \u220b B \u2192 \u0394 , A \u220b B)\n ext \u03c1 Z = Z\n ext \u03c1 (S x) = S (\u03c1 x)\n\n rename : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A) \u2192 (\u2200 {A} \u2192 \u0393 \u22a2 A \u2192 \u0394 \u22a2 A)\n rename \u03c1 (` x) = ` (\u03c1 x)\n rename \u03c1 (\u019b N) = \u019b (rename (ext \u03c1) N)\n rename \u03c1 (L \u00b7 M) = (rename \u03c1 L) \u00b7 (rename \u03c1 M)\n rename \u03c1 (`zero) = `zero\n rename \u03c1 (`suc M) = `suc (rename \u03c1 M)\n rename \u03c1 (case L M N) = case (rename \u03c1 L) (rename \u03c1 M) (rename (ext \u03c1) N)\n rename \u03c1 (\u03bc N) = \u03bc (rename (ext \u03c1) N)\n rename \u03c1 (con n) = con n\n rename \u03c1 (M `* N) = rename \u03c1 M `* rename \u03c1 N\n rename \u03c1 (`let M N) = `let (rename \u03c1 M) (rename (ext \u03c1) N)\n rename \u03c1 `\u27e8 M , N \u27e9 = `\u27e8 rename \u03c1 M , rename \u03c1 N \u27e9\n rename \u03c1 (`proj\u2081 L) = `proj\u2081 (rename \u03c1 L)\n rename \u03c1 (`proj\u2082 L) = `proj\u2082 (rename \u03c1 L)\n rename \u03c1 (case\u00d7 L M) = case\u00d7 (rename \u03c1 L) (rename (ext (ext \u03c1)) M)\n\\end{code}\n\n## Simultaneous Substitution\n\n\\begin{code}\n exts : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A) \u2192 (\u2200 {A B} \u2192 \u0393 , A \u220b B \u2192 \u0394 , A \u22a2 B)\n exts \u03c3 Z = ` Z\n exts \u03c3 (S x) = rename S_ (\u03c3 x)\n\n subst : \u2200 {\u0393 \u0394} \u2192 (\u2200 {C} \u2192 \u0393 \u220b C \u2192 \u0394 \u22a2 C) \u2192 (\u2200 {C} \u2192 \u0393 \u22a2 C \u2192 \u0394 \u22a2 C)\n subst \u03c3 (` k) = \u03c3 k\n subst \u03c3 (\u019b N) = \u019b (subst (exts \u03c3) N)\n subst \u03c3 (L \u00b7 M) = (subst \u03c3 L) \u00b7 (subst \u03c3 M)\n subst \u03c3 (`zero) = `zero\n subst \u03c3 (`suc M) = `suc (subst \u03c3 M)\n subst \u03c3 (case L M N) = case (subst \u03c3 L) (subst \u03c3 M) (subst (exts \u03c3) N)\n subst \u03c3 (\u03bc N) = \u03bc (subst (exts \u03c3) N)\n subst \u03c3 (con n) = con n\n subst \u03c3 (M `* N) = subst \u03c3 M `* subst \u03c3 N\n subst \u03c3 (`let M N) = `let (subst \u03c3 M) (subst (exts \u03c3) N)\n subst \u03c3 `\u27e8 M , N \u27e9 = `\u27e8 subst \u03c3 M , subst \u03c3 N \u27e9\n subst \u03c3 (`proj\u2081 L) = `proj\u2081 (subst \u03c3 L)\n subst \u03c3 (`proj\u2082 L) = `proj\u2082 (subst \u03c3 L)\n subst \u03c3 (case\u00d7 L M) = case\u00d7 (subst \u03c3 L) (subst (exts (exts \u03c3)) M)\n\\end{code}\n\n## Single and double substitution\n\n\\begin{code}\n _[_] : \u2200 {\u0393 A B}\n \u2192 \u0393 , A \u22a2 B\n \u2192 \u0393 \u22a2 A\n ------------\n \u2192 \u0393 \u22a2 B\n _[_] {\u0393} {A} N V = subst {\u0393 , A} {\u0393} \u03c3 N\n where\n \u03c3 : \u2200 {B} \u2192 \u0393 , A \u220b B \u2192 \u0393 \u22a2 B\n \u03c3 Z = V\n \u03c3 (S x) = ` x\n\n _[_][_] : \u2200 {\u0393 A B C}\n \u2192 \u0393 , A , B \u22a2 C\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 \u22a2 B\n ---------------\n \u2192 \u0393 \u22a2 C\n _[_][_] {\u0393} {A} {B} N V W = subst {\u0393 , A , B} {\u0393} \u03c3 N\n where\n \u03c3 : \u2200 {C} \u2192 \u0393 , A , B \u220b C \u2192 \u0393 \u22a2 C\n \u03c3 Z = W\n \u03c3 (S Z) = V\n \u03c3 (S (S x)) = ` x\n\\end{code}\n\n## Values\n\n\\begin{code}\n data Value : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 A \u2192 Set where\n\n -- functions\n\n V-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B}\n ---------------------------\n \u2192 Value (\u019b N)\n\n -- naturals\n\n V-zero : \u2200 {\u0393} \u2192\n -----------------\n Value (`zero {\u0393})\n\n V-suc_ : \u2200 {\u0393} {V : \u0393 \u22a2 `\u2115}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n\n -- primitives\n\n V-con : \u2200 {\u0393 n}\n ---------------------\n \u2192 Value {\u0393 = \u0393} (con n)\n\n -- products\n\n V-\u27e8_,_\u27e9 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------\n \u2192 Value `\u27e8 V , W \u27e9\n\\end{code}\n\nImplicit arguments need to be supplied when they are\nnot fixed by the given arguments.\n\n## Reduction\n\n\\begin{code}\n infix 2 _\u2014\u2192_\n\n data _\u2014\u2192_ : \u2200 {\u0393 A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n -- functions\n\n \u03be-\u00b7\u2081 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A \u21d2 B} {M : \u0393 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n ---------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A \u21d2 B} {M M\u2032 : \u0393 \u22a2 A}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n ---------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03b2-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B} {V : \u0393 \u22a2 A}\n \u2192 Value V\n --------------------\n \u2192 (\u019b N) \u00b7 V \u2014\u2192 N [ V ]\n\n -- naturals\n\n \u03be-suc : \u2200 {\u0393} {M M\u2032 : \u0393 \u22a2 `\u2115}\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 `suc M \u2014\u2192 `suc M\u2032\n\n \u03be-case : \u2200 {\u0393 A} {L L\u2032 : \u0393 \u22a2 `\u2115} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n -------------------------\n \u2192 case L M N \u2014\u2192 case L\u2032 M N\n\n \u03b2-zero : \u2200 {\u0393 A} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n -------------------\n \u2192 case `zero M N \u2014\u2192 M\n\n \u03b2-suc : \u2200 {\u0393 A} {V : \u0393 \u22a2 `\u2115} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n \u2192 Value V\n ----------------------------\n \u2192 case (`suc V) M N \u2014\u2192 N [ V ]\n\n -- fixpoint\n\n \u03b2-\u03bc : \u2200 {\u0393 A} {N : \u0393 , A \u22a2 A}\n ----------------\n \u2192 \u03bc N \u2014\u2192 N [ \u03bc N ]\n\n -- primitive numbers\n\n \u03be-*\u2081 : \u2200 {\u0393} {L L\u2032 M : \u0393 \u22a2 Nat}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L `* M \u2014\u2192 L\u2032 `* M\n\n \u03be-*\u2082 : \u2200 {\u0393} {V M M\u2032 : \u0393 \u22a2 Nat}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 V `* M \u2014\u2192 V `* M\u2032\n\n \u03b4-* : \u2200 {\u0393 c d}\n -------------------------------------\n \u2192 con {\u0393 = \u0393} c `* con d \u2014\u2192 con (c * d)\n\n -- let\n\n \u03be-let : \u2200 {\u0393 A B} {M M\u2032 : \u0393 \u22a2 A} {N : \u0393 , A \u22a2 B}\n \u2192 M \u2014\u2192 M\u2032\n ---------------------\n \u2192 `let M N \u2014\u2192 `let M\u2032 N\n\n \u03b2-let : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {N : \u0393 , A \u22a2 B}\n \u2192 Value V\n -------------------\n \u2192 `let V N \u2014\u2192 N [ V ]\n\n -- products\n\n \u03be-\u27e8,\u27e9\u2081 : \u2200 {\u0393 A B} {M M\u2032 : \u0393 \u22a2 A} {N : \u0393 \u22a2 B}\n \u2192 M \u2014\u2192 M\u2032\n -------------------------\n \u2192 `\u27e8 M , N \u27e9 \u2014\u2192 `\u27e8 M\u2032 , N \u27e9\n\n \u03be-\u27e8,\u27e9\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {N N\u2032 : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 N \u2014\u2192 N\u2032\n -------------------------\n \u2192 `\u27e8 V , N \u27e9 \u2014\u2192 `\u27e8 V , N\u2032 \u27e9\n\n \u03be-proj\u2081 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B}\n \u2192 L \u2014\u2192 L\u2032\n ---------------------\n \u2192 `proj\u2081 L \u2014\u2192 `proj\u2081 L\u2032\n\n \u03be-proj\u2082 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B}\n \u2192 L \u2014\u2192 L\u2032\n ---------------------\n \u2192 `proj\u2082 L \u2014\u2192 `proj\u2082 L\u2032\n\n \u03b2-proj\u2081 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------------\n \u2192 `proj\u2081 `\u27e8 V , W \u27e9 \u2014\u2192 V\n\n \u03b2-proj\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------------\n \u2192 `proj\u2082 `\u27e8 V , W \u27e9 \u2014\u2192 W\n\n -- alternative formulation of products\n\n \u03be-case\u00d7 : \u2200 {\u0393 A B C} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B} {M : \u0393 , A , B \u22a2 C}\n \u2192 L \u2014\u2192 L\u2032\n -----------------------\n \u2192 case\u00d7 L M \u2014\u2192 case\u00d7 L\u2032 M\n\n \u03b2-case\u00d7 : \u2200 {\u0393 A B C} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B} {M : \u0393 , A , B \u22a2 C}\n \u2192 Value V\n \u2192 Value W\n ----------------------------------\n \u2192 case\u00d7 `\u27e8 V , W \u27e9 M \u2014\u2192 M [ V ][ W ]\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\n infix 2 _\u2014\u21a0_\n infix 1 begin_\n infixr 2 _\u2014\u2192\u27e8_\u27e9_\n infix 3 _\u220e\n\n data _\u2014\u21a0_ : \u2200 {\u0393 A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n _\u220e : \u2200 {\u0393 A} (M : \u0393 \u22a2 A)\n --------\n \u2192 M \u2014\u21a0 M\n\n _\u2014\u2192\u27e8_\u27e9_ : \u2200 {\u0393 A} (L : \u0393 \u22a2 A) {M N : \u0393 \u22a2 A}\n \u2192 L \u2014\u2192 M\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 L \u2014\u21a0 N\n\n begin_ : \u2200 {\u0393} {A} {M N : \u0393 \u22a2 A}\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 M \u2014\u21a0 N\n begin M\u2014\u21a0N = M\u2014\u21a0N\n\\end{code}\n\n\n## Values do not reduce\n\n\\begin{code}\n V\u00ac\u2014\u2192 : \u2200 {\u0393 A} {M N : \u0393 \u22a2 A}\n \u2192 Value M\n ----------\n \u2192 \u00ac (M \u2014\u2192 N)\n V\u00ac\u2014\u2192 V-\u019b ()\n V\u00ac\u2014\u2192 V-zero ()\n V\u00ac\u2014\u2192 (V-suc VM) (\u03be-suc M\u2014\u2192M\u2032) = V\u00ac\u2014\u2192 VM M\u2014\u2192M\u2032\n V\u00ac\u2014\u2192 V-con ()\n V\u00ac\u2014\u2192 V-\u27e8 VM , _ \u27e9 (\u03be-\u27e8,\u27e9\u2081 M\u2014\u2192M\u2032) = V\u00ac\u2014\u2192 VM M\u2014\u2192M\u2032\n V\u00ac\u2014\u2192 V-\u27e8 _ , VN \u27e9 (\u03be-\u27e8,\u27e9\u2082 _ N\u2014\u2192N\u2032) = V\u00ac\u2014\u2192 VN N\u2014\u2192N\u2032\n\\end{code}\n\n\n## Progress\n\n\\begin{code}\n data Progress {A} (M : \u2205 \u22a2 A) : Set where\n\n step : \u2200 {N : \u2205 \u22a2 A}\n \u2192 M \u2014\u2192 N\n ----------\n \u2192 Progress M\n\n done :\n Value M\n ----------\n \u2192 Progress M\n\n progress : \u2200 {A}\n \u2192 (M : \u2205 \u22a2 A)\n -----------\n \u2192 Progress M\n progress (` ())\n progress (\u019b N) = done V-\u019b\n progress (L \u00b7 M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-\u00b7\u2081 L\u2014\u2192L\u2032)\n ... | done V-\u019b with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-\u00b7\u2082 V-\u019b M\u2014\u2192M\u2032)\n ... | done VM = step (\u03b2-\u019b VM)\n progress (`zero) = done V-zero\n progress (`suc M) with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-suc M\u2014\u2192M\u2032)\n ... | done VM = done (V-suc VM)\n progress (case L M N) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case L\u2014\u2192L\u2032)\n ... | done V-zero = step \u03b2-zero\n ... | done (V-suc VL) = step (\u03b2-suc VL)\n progress (\u03bc N) = step \u03b2-\u03bc\n progress (con n) = done V-con\n progress (L `* M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-*\u2081 L\u2014\u2192L\u2032)\n ... | done V-con with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-*\u2082 V-con M\u2014\u2192M\u2032)\n ... | done V-con = step \u03b4-*\n progress (`let M N) with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-let M\u2014\u2192M\u2032)\n ... | done VM = step (\u03b2-let VM)\n progress `\u27e8 M , N \u27e9 with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-\u27e8,\u27e9\u2081 M\u2014\u2192M\u2032)\n ... | done VM with progress N\n ... | step N\u2014\u2192N\u2032 = step (\u03be-\u27e8,\u27e9\u2082 VM N\u2014\u2192N\u2032)\n ... | done VN = done (V-\u27e8 VM , VN \u27e9)\n progress (`proj\u2081 L) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-proj\u2081 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-proj\u2081 VM VN)\n progress (`proj\u2082 L) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-proj\u2082 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-proj\u2082 VM VN)\n progress (case\u00d7 L M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case\u00d7 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-case\u00d7 VM VN)\n\\end{code}\n\n\n## Evaluation\n\n\\begin{code}\n data Gas : Set where\n gas : \u2115 \u2192 Gas\n\n data Finished {\u0393 A} (N : \u0393 \u22a2 A) : Set where\n\n done :\n Value N\n ----------\n \u2192 Finished N\n\n out-of-gas :\n ----------\n Finished N\n\n data Steps : \u2200 {A} \u2192 \u2205 \u22a2 A \u2192 Set where\n\n steps : \u2200 {A} {L N : \u2205 \u22a2 A}\n \u2192 L \u2014\u21a0 N\n \u2192 Finished N\n ----------\n \u2192 Steps L\n\n eval : \u2200 {A}\n \u2192 Gas\n \u2192 (L : \u2205 \u22a2 A)\n -----------\n \u2192 Steps L\n eval (gas zero) L = steps (L \u220e) out-of-gas\n eval (gas (suc m)) L with progress L\n ... | done VL = steps (L \u220e) (done VL)\n ... | step {M} L\u2014\u2192M with eval (gas m) M\n ... | steps M\u2014\u21a0N fin = steps (L \u2014\u2192\u27e8 L\u2014\u2192M \u27e9 M\u2014\u21a0N) fin\n\\end{code}\n\n## Examples\n\n\\begin{code}\n cube : \u2205 \u22a2 Nat \u21d2 Nat\n cube = \u019b (# 0 `* # 0 `* # 0)\n\n _ : cube \u00b7 con 2 \u2014\u21a0 con 8\n _ = \n begin\n cube \u00b7 con 2\n \u2014\u2192\u27e8 \u03b2-\u019b V-con \u27e9\n con 2 `* con 2 `* con 2\n \u2014\u2192\u27e8 \u03be-*\u2081 \u03b4-* \u27e9\n con 4 `* con 2\n \u2014\u2192\u27e8 \u03b4-* \u27e9\n con 8\n \u220e\n\n exp10 : \u2205 \u22a2 Nat \u21d2 Nat\n exp10 = \u019b (`let (# 0 `* # 0)\n (`let (# 0 `* # 0)\n (`let (# 0 `* # 2)\n (# 0 `* # 0))))\n\n _ : exp10 \u00b7 con 2 \u2014\u21a0 con 1024\n _ =\n begin\n exp10 \u00b7 con 2\n \u2014\u2192\u27e8 \u03b2-\u019b V-con \u27e9\n `let (con 2 `* con 2) (`let (# 0 `* # 0) (`let (# 0 `* con 2) (# 0 `* # 0)))\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 4) (`let (# 0 `* # 0) (`let (# 0 `* con 2) (# 0 `* # 0)))\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n `let (con 4 `* con 4) (`let (# 0 `* con 2) (# 0 `* # 0))\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 16) (`let (# 0 `* con 2) (# 0 `* # 0))\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n `let (con 16 `* con 2) (# 0 `* # 0)\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 32) (# 0 `* # 0)\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n con 32 `* con 32\n \u2014\u2192\u27e8 \u03b4-* \u27e9\n con 1024\n \u220e\n\n swap\u00d7 : \u2200 {A B} \u2192 \u2205 \u22a2 A `\u00d7 B \u21d2 B `\u00d7 A\n swap\u00d7 = \u019b `\u27e8 `proj\u2082 (# 0) , `proj\u2081 (# 0) \u27e9\n\n _ : swap\u00d7 \u00b7 `\u27e8 con 42 , `zero \u27e9 \u2014\u21a0 `\u27e8 `zero , con 42 \u27e9\n _ =\n begin\n swap\u00d7 \u00b7 `\u27e8 con 42 , `zero \u27e9\n \u2014\u2192\u27e8 \u03b2-\u019b V-\u27e8 V-con , V-zero \u27e9 \u27e9\n `\u27e8 `proj\u2082 `\u27e8 con 42 , `zero \u27e9 , `proj\u2081 `\u27e8 con 42 , `zero \u27e9 \u27e9\n \u2014\u2192\u27e8 \u03be-\u27e8,\u27e9\u2081 (\u03b2-proj\u2082 V-con V-zero) \u27e9\n `\u27e8 `zero , `proj\u2081 `\u27e8 con 42 , `zero \u27e9 \u27e9\n \u2014\u2192\u27e8 \u03be-\u27e8,\u27e9\u2082 V-zero (\u03b2-proj\u2081 V-con V-zero) \u27e9\n `\u27e8 `zero , con 42 \u27e9\n \u220e\n\n swap\u00d7-case : \u2200 {A B} \u2192 \u2205 \u22a2 A `\u00d7 B \u21d2 B `\u00d7 A\n swap\u00d7-case = \u019b case\u00d7 (# 0) `\u27e8 # 0 , # 1 \u27e9\n\n _ : swap\u00d7-case \u00b7 `\u27e8 con 42 , `zero \u27e9 \u2014\u21a0 `\u27e8 `zero , con 42 \u27e9\n _ =\n begin\n swap\u00d7-case \u00b7 `\u27e8 con 42 , `zero \u27e9\n \u2014\u2192\u27e8 \u03b2-\u019b V-\u27e8 V-con , V-zero \u27e9 \u27e9\n case\u00d7 `\u27e8 con 42 , `zero \u27e9 `\u27e8 # 0 , # 1 \u27e9\n \u2014\u2192\u27e8 \u03b2-case\u00d7 V-con V-zero \u27e9\n `\u27e8 `zero , con 42 \u27e9\n \u220e\n\\end{code}\n\n\n#### Exercise `More` (recommended in part)\n\nFormalise the remaining constructs defined in this chapter.\nEvaluate each example, applied to data as needed,\nto confirm it returns the expected answer.\n\n * sums (recommended)\n * unit type\n * an alternative formulation of unit type\n * empty type (recommended)\n * lists\n\n\n## Bisimulation\n\n(No recommended exercises for this chapter.)\n\n#### Exercise `sim\u207b\u00b9`\n\nShow that we also have a simulation in the other direction, and hence that we have\na bisimulation.\n\n#### Exercise `products`\n\nShow that the two formulations of products in\nChapter [More][plfa.More]\nare in bisimulation. The only constructs you need to include are\nvariables, and those connected to functions and products.\nIn this case, the simulation is _not_ lock-step.\n\n## Inference\n\n\\begin{code}\nmodule Inference where\n\\end{code}\n\nRemember to indent all code by two spaces.\n\n### Imports\n\n\\begin{code}\n import plfa.More as DB\n\\end{code}\n\n### Syntax\n\n\\begin{code}\n infix 4 _\u220b_\u2982_\n infix 4 _\u22a2_\u2191_\n infix 4 _\u22a2_\u2193_\n infixl 5 _,_\u2982_\n\n infixr 7 _\u21d2_\n\n infix 5 \u019b_\u21d2_\n infix 5 \u03bc_\u21d2_\n infix 6 _\u2191\n infix 6 _\u2193_\n infixl 7 _\u00b7_\n infix 8 `suc_\n infix 9 `_\n\\end{code}\n\n### Identifiers, types, and contexts\n\n\\begin{code}\n Id : Set\n Id = String\n\n data Type : Set where\n `\u2115 : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n\n data Context : Set where\n \u2205 : Context\n _,_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Context\n\\end{code}\n\n### Terms\n\n\\begin{code}\n data Term\u207a : Set\n data Term\u207b : Set\n\n data Term\u207a where\n `_ : Id \u2192 Term\u207a\n _\u00b7_ : Term\u207a \u2192 Term\u207b \u2192 Term\u207a\n _\u2193_ : Term\u207b \u2192 Type \u2192 Term\u207a\n\n data Term\u207b where\n \u019b_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n `zero : Term\u207b\n `suc_ : Term\u207b \u2192 Term\u207b\n `case_[zero\u21d2_|suc_\u21d2_] : Term\u207a \u2192 Term\u207b \u2192 Id \u2192 Term\u207b \u2192 Term\u207b\n \u03bc_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n _\u2191 : Term\u207a \u2192 Term\u207b\n\\end{code}\n\n### Sample terms\n\n\\begin{code}\n two : Term\u207b\n two = `suc (`suc `zero)\n\n plus : Term\u207a\n plus = (\u03bc \"p\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n `case (` \"m\") [zero\u21d2 ` \"n\" \u2191\n |suc \"m\" \u21d2 `suc (` \"p\" \u00b7 (` \"m\" \u2191) \u00b7 (` \"n\" \u2191) \u2191) ])\n \u2193 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\n\n 2+2 : Term\u207a\n 2+2 = plus \u00b7 two \u00b7 two\n\\end{code}\n\n### Lookup \n\n\\begin{code}\n data _\u220b_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 x A}\n --------------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 x y A B}\n \u2192 x \u2262 y\n \u2192 \u0393 \u220b x \u2982 A\n -----------------\n \u2192 \u0393 , y \u2982 B \u220b x \u2982 A\n\\end{code}\n\n### Bidirectional type checking\n\n\\begin{code}\n data _\u22a2_\u2191_ : Context \u2192 Term\u207a \u2192 Type \u2192 Set\n data _\u22a2_\u2193_ : Context \u2192 Term\u207b \u2192 Type \u2192 Set\n\n data _\u22a2_\u2191_ where\n\n \u22a2` : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x \u2982 A\n -----------\n \u2192 \u0393 \u22a2 ` x \u2191 A\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2191 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2193 A\n -------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2191 B\n\n \u22a2\u2193 : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M \u2193 A\n ---------------\n \u2192 \u0393 \u22a2 (M \u2193 A) \u2191 A\n\n data _\u22a2_\u2193_ where\n\n \u22a2\u019b : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 B\n -------------------\n \u2192 \u0393 \u22a2 \u019b x \u21d2 N \u2193 A \u21d2 B\n\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2193 `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2193 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2193 `\u2115\n\n \u22a2case : \u2200 {\u0393 L M x N A}\n \u2192 \u0393 \u22a2 L \u2191 `\u2115\n \u2192 \u0393 \u22a2 M \u2193 A\n \u2192 \u0393 , x \u2982 `\u2115 \u22a2 N \u2193 A\n -------------------------------------\n \u2192 \u0393 \u22a2 `case L [zero\u21d2 M |suc x \u21d2 N ] \u2193 A\n\n \u22a2\u03bc : \u2200 {\u0393 x N A}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 A\n -----------------\n \u2192 \u0393 \u22a2 \u03bc x \u21d2 N \u2193 A\n\n \u22a2\u2191 : \u2200 {\u0393 M A B}\n \u2192 \u0393 \u22a2 M \u2191 A\n \u2192 A \u2261 B\n -------------\n \u2192 \u0393 \u22a2 (M \u2191) \u2193 B\n\\end{code}\n\n\n### Type equality\n\n\\begin{code}\n _\u225fTp_ : (A B : Type) \u2192 Dec (A \u2261 B)\n `\u2115 \u225fTp `\u2115 = yes refl\n `\u2115 \u225fTp (A \u21d2 B) = no \u03bb()\n (A \u21d2 B) \u225fTp `\u2115 = no \u03bb()\n (A \u21d2 B) \u225fTp (A\u2032 \u21d2 B\u2032)\n with A \u225fTp A\u2032 | B \u225fTp B\u2032\n ... | no A\u2262 | _ = no \u03bb{refl \u2192 A\u2262 refl}\n ... | yes _ | no B\u2262 = no \u03bb{refl \u2192 B\u2262 refl}\n ... | yes refl | yes refl = yes refl\n\\end{code}\n\n### Prerequisites\n\n\\begin{code}\n dom\u2261 : \u2200 {A A\u2032 B B\u2032} \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032 \u2192 A \u2261 A\u2032\n dom\u2261 refl = refl\n\n rng\u2261 : \u2200 {A A\u2032 B B\u2032} \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032 \u2192 B \u2261 B\u2032\n rng\u2261 refl = refl\n\n \u2115\u2262\u21d2 : \u2200 {A B} \u2192 `\u2115 \u2262 A \u21d2 B\n \u2115\u2262\u21d2 ()\n\\end{code}\n\n\n### Unique lookup\n\n\\begin{code}\n uniq-\u220b : \u2200 {\u0393 x A B} \u2192 \u0393 \u220b x \u2982 A \u2192 \u0393 \u220b x \u2982 B \u2192 A \u2261 B\n uniq-\u220b Z Z = refl\n uniq-\u220b Z (S x\u2262y _) = \u22a5-elim (x\u2262y refl)\n uniq-\u220b (S x\u2262y _) Z = \u22a5-elim (x\u2262y refl)\n uniq-\u220b (S _ \u220bx) (S _ \u220bx\u2032) = uniq-\u220b \u220bx \u220bx\u2032\n\\end{code}\n\n### Unique synthesis\n\n\\begin{code}\n uniq-\u2191 : \u2200 {\u0393 M A B} \u2192 \u0393 \u22a2 M \u2191 A \u2192 \u0393 \u22a2 M \u2191 B \u2192 A \u2261 B\n uniq-\u2191 (\u22a2` \u220bx) (\u22a2` \u220bx\u2032) = uniq-\u220b \u220bx \u220bx\u2032\n uniq-\u2191 (\u22a2L \u00b7 \u22a2M) (\u22a2L\u2032 \u00b7 \u22a2M\u2032) = rng\u2261 (uniq-\u2191 \u22a2L \u22a2L\u2032)\n uniq-\u2191 (\u22a2\u2193 \u22a2M) (\u22a2\u2193 \u22a2M\u2032) = refl \n\\end{code}\n\n## Lookup type of a variable in the context\n\n\\begin{code}\n ext\u220b : \u2200 {\u0393 B x y}\n \u2192 x \u2262 y\n \u2192 \u00ac \u2203[ A ]( \u0393 \u220b x \u2982 A )\n -----------------------------\n \u2192 \u00ac \u2203[ A ]( \u0393 , y \u2982 B \u220b x \u2982 A )\n ext\u220b x\u2262y _ \u27e8 A , Z \u27e9 = x\u2262y refl\n ext\u220b _ \u00ac\u2203 \u27e8 A , S _ \u22a2x \u27e9 = \u00ac\u2203 \u27e8 A , \u22a2x \u27e9\n\n lookup : \u2200 (\u0393 : Context) (x : Id)\n -----------------------\n \u2192 Dec (\u2203[ A ](\u0393 \u220b x \u2982 A))\n lookup \u2205 x = no (\u03bb ())\n lookup (\u0393 , y \u2982 B) x with x \u225f y\n ... | yes refl = yes \u27e8 B , Z \u27e9\n ... | no x\u2262y with lookup \u0393 x\n ... | no \u00ac\u2203 = no (ext\u220b x\u2262y \u00ac\u2203)\n ... | yes \u27e8 A , \u22a2x \u27e9 = yes \u27e8 A , S x\u2262y \u22a2x \u27e9\n\\end{code}\n\n### Promoting negations\n\n\\begin{code}\n \u00acarg : \u2200 {\u0393 A B L M}\n \u2192 \u0393 \u22a2 L \u2191 A \u21d2 B\n \u2192 \u00ac \u0393 \u22a2 M \u2193 A\n -------------------------\n \u2192 \u00ac \u2203[ B\u2032 ](\u0393 \u22a2 L \u00b7 M \u2191 B\u2032)\n \u00acarg \u22a2L \u00ac\u22a2M \u27e8 B\u2032 , \u22a2L\u2032 \u00b7 \u22a2M\u2032 \u27e9 rewrite dom\u2261 (uniq-\u2191 \u22a2L \u22a2L\u2032) = \u00ac\u22a2M \u22a2M\u2032\n\n \u00acswitch : \u2200 {\u0393 M A B}\n \u2192 \u0393 \u22a2 M \u2191 A\n \u2192 A \u2262 B\n ---------------\n \u2192 \u00ac \u0393 \u22a2 (M \u2191) \u2193 B\n \u00acswitch \u22a2M A\u2262B (\u22a2\u2191 \u22a2M\u2032 A\u2032\u2261B) rewrite uniq-\u2191 \u22a2M \u22a2M\u2032 = A\u2262B A\u2032\u2261B\n\\end{code}\n\n\n## Synthesize and inherit types\n\n\\begin{code}\n synthesize : \u2200 (\u0393 : Context) (M : Term\u207a)\n -----------------------\n \u2192 Dec (\u2203[ A ](\u0393 \u22a2 M \u2191 A))\n\n inherit : \u2200 (\u0393 : Context) (M : Term\u207b) (A : Type)\n ---------------\n \u2192 Dec (\u0393 \u22a2 M \u2193 A)\n\n synthesize \u0393 (` x) with lookup \u0393 x\n ... | no \u00ac\u2203 = no (\u03bb{ \u27e8 A , \u22a2` \u220bx \u27e9 \u2192 \u00ac\u2203 \u27e8 A , \u220bx \u27e9 })\n ... | yes \u27e8 A , \u220bx \u27e9 = yes \u27e8 A , \u22a2` \u220bx \u27e9\n synthesize \u0393 (L \u00b7 M) with synthesize \u0393 L\n ... | no \u00ac\u2203 = no (\u03bb{ \u27e8 _ , \u22a2L \u00b7 _ \u27e9 \u2192 \u00ac\u2203 \u27e8 _ , \u22a2L \u27e9 })\n ... | yes \u27e8 `\u2115 , \u22a2L \u27e9 = no (\u03bb{ \u27e8 _ , \u22a2L\u2032 \u00b7 _ \u27e9 \u2192 \u2115\u2262\u21d2 (uniq-\u2191 \u22a2L \u22a2L\u2032) })\n ... | yes \u27e8 A \u21d2 B , \u22a2L \u27e9 with inherit \u0393 M A\n ... | no \u00ac\u22a2M = no (\u00acarg \u22a2L \u00ac\u22a2M)\n ... | yes \u22a2M = yes \u27e8 B , \u22a2L \u00b7 \u22a2M \u27e9 \n synthesize \u0393 (M \u2193 A) with inherit \u0393 M A\n ... | no \u00ac\u22a2M = no (\u03bb{ \u27e8 _ , \u22a2\u2193 \u22a2M \u27e9 \u2192 \u00ac\u22a2M \u22a2M })\n ... | yes \u22a2M = yes \u27e8 A , \u22a2\u2193 \u22a2M \u27e9\n\n inherit \u0393 (\u019b x \u21d2 N) `\u2115 = no (\u03bb())\n inherit \u0393 (\u019b x \u21d2 N) (A \u21d2 B) with inherit (\u0393 , x \u2982 A) N B\n ... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2\u019b \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n ... | yes \u22a2N = yes (\u22a2\u019b \u22a2N)\n inherit \u0393 `zero `\u2115 = yes \u22a2zero\n inherit \u0393 `zero (A \u21d2 B) = no (\u03bb())\n inherit \u0393 (`suc M) `\u2115 with inherit \u0393 M `\u2115\n ... | no \u00ac\u22a2M = no (\u03bb{ (\u22a2suc \u22a2M) \u2192 \u00ac\u22a2M \u22a2M })\n ... | yes \u22a2M = yes (\u22a2suc \u22a2M)\n inherit \u0393 (`suc M) (A \u21d2 B) = no (\u03bb())\n inherit \u0393 (`case L [zero\u21d2 M |suc x \u21d2 N ]) A with synthesize \u0393 L\n ... | no \u00ac\u2203 = no (\u03bb{ (\u22a2case \u22a2L _ _) \u2192 \u00ac\u2203 \u27e8 `\u2115 , \u22a2L \u27e9})\n ... | yes \u27e8 _ \u21d2 _ , \u22a2L \u27e9 = no (\u03bb{ (\u22a2case \u22a2L\u2032 _ _) \u2192 \u2115\u2262\u21d2 (uniq-\u2191 \u22a2L\u2032 \u22a2L) }) \n ... | yes \u27e8 `\u2115 , \u22a2L \u27e9 with inherit \u0393 M A\n ... | no \u00ac\u22a2M = no (\u03bb{ (\u22a2case _ \u22a2M _) \u2192 \u00ac\u22a2M \u22a2M })\n ... | yes \u22a2M with inherit (\u0393 , x \u2982 `\u2115) N A\n ... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2case _ _ \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n ... | yes \u22a2N = yes (\u22a2case \u22a2L \u22a2M \u22a2N)\n inherit \u0393 (\u03bc x \u21d2 N) A with inherit (\u0393 , x \u2982 A) N A\n ... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2\u03bc \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n ... | yes \u22a2N = yes (\u22a2\u03bc \u22a2N)\n inherit \u0393 (M \u2191) B with synthesize \u0393 M\n ... | no \u00ac\u2203 = no (\u03bb{ (\u22a2\u2191 \u22a2M _) \u2192 \u00ac\u2203 \u27e8 _ , \u22a2M \u27e9 })\n ... | yes \u27e8 A , \u22a2M \u27e9 with A \u225fTp B\n ... | no A\u2262B = no (\u00acswitch \u22a2M A\u2262B)\n ... | yes A\u2261B = yes (\u22a2\u2191 \u22a2M A\u2261B)\n\\end{code}\n\n### Erasure\n\n\\begin{code}\n \u2225_\u2225Tp : Type \u2192 DB.Type\n \u2225 `\u2115 \u2225Tp = DB.`\u2115\n \u2225 A \u21d2 B \u2225Tp = \u2225 A \u2225Tp DB.\u21d2 \u2225 B \u2225Tp\n\n \u2225_\u2225Cx : Context \u2192 DB.Context\n \u2225 \u2205 \u2225Cx = DB.\u2205\n \u2225 \u0393 , x \u2982 A \u2225Cx = \u2225 \u0393 \u2225Cx DB., \u2225 A \u2225Tp\n\n \u2225_\u2225\u220b : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x \u2982 A \u2192 \u2225 \u0393 \u2225Cx DB.\u220b \u2225 A \u2225Tp\n \u2225 Z \u2225\u220b = DB.Z\n \u2225 S x\u2262 \u22a2x \u2225\u220b = DB.S \u2225 \u22a2x \u2225\u220b\n\n \u2225_\u2225\u207a : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2191 A \u2192 \u2225 \u0393 \u2225Cx DB.\u22a2 \u2225 A \u2225Tp\n \u2225_\u2225\u207b : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2193 A \u2192 \u2225 \u0393 \u2225Cx DB.\u22a2 \u2225 A \u2225Tp\n\n \u2225 \u22a2` \u22a2x \u2225\u207a = DB.` \u2225 \u22a2x \u2225\u220b\n \u2225 \u22a2L \u00b7 \u22a2M \u2225\u207a = \u2225 \u22a2L \u2225\u207a DB.\u00b7 \u2225 \u22a2M \u2225\u207b\n \u2225 \u22a2\u2193 \u22a2M \u2225\u207a = \u2225 \u22a2M \u2225\u207b\n\n \u2225 \u22a2\u019b \u22a2N \u2225\u207b = DB.\u019b \u2225 \u22a2N \u2225\u207b\n \u2225 \u22a2zero \u2225\u207b = DB.`zero\n \u2225 \u22a2suc \u22a2M \u2225\u207b = DB.`suc \u2225 \u22a2M \u2225\u207b\n \u2225 \u22a2case \u22a2L \u22a2M \u22a2N \u2225\u207b = DB.case \u2225 \u22a2L \u2225\u207a \u2225 \u22a2M \u2225\u207b \u2225 \u22a2N \u2225\u207b\n \u2225 \u22a2\u03bc \u22a2M \u2225\u207b = DB.\u03bc \u2225 \u22a2M \u2225\u207b\n \u2225 \u22a2\u2191 \u22a2M refl \u2225\u207b = \u2225 \u22a2M \u2225\u207a\n\\end{code}\n\n#### Exercise `bidirectional-mul` (recommended) {#bidirectional-mul}\n\nRewrite your definition of multiplication from\nChapter [Lambda][plfa.Lambda], decorated to support inference.\n\n\n#### Exercise `bidirectional-products` (recommended) {#bidirectional-products}\n\nExtend the bidirectional type rules to include products from\nChapter [More][plfa.More].\n\n\n#### Exercise `bidirectional-rest` (stretch)\n\nExtend the bidirectional type rules to include the rest of the constructs from\nChapter [More][plfa.More].\n\n\n#### Exercise `inference-mul` (recommended)\n\nRewrite your definition of multiplication from\nChapter [Lambda][plfa.Lambda] decorated to support inference, and show\nthat erasure of the inferred typing yields your definition of\nmultiplication from Chapter [DeBruijn][plfa.DeBruijn].\n\n\n#### Exercise `inference-products` (recommended)\n\nExtend bidirectional inference to include products from\nChapter [More][plfa.More].\n\n\n#### Exercise `inference-rest` (stretch)\n\nExtend bidirectional inference to include the rest of the constructs from\nChapter [More][plfa.More].\n\n## Untyped\n\n#### Exercise (`Type\u2243\u22a4`)\n\nShow that `Type` is isomorphic to `\u22a4`, the unit type.\n\n#### Exercise (`Context\u2243\u2115`)\n\nShow that `Context` is isomorphic to `\u2115`.\n\n#### Exercise (`variant-1`)\n\nHow would the rules change if we want call-by-value where terms\nnormalise completely? Assume that `\u03b2` should not permit reduction\nunless both terms are in normal form.\n\n#### Exercise (`variant-2`)\n\nHow would the rules change if we want call-by-value where terms\ndo not reduce underneath lambda? Assume that `\u03b2`\npermits reduction when both terms are values (that is, lambda\nabstractions). What would `2+2\u1d9c` reduce to in this case?\n\n#### Exercise `2+2\u2261four`\n\nUse the evaluator to confirm that `2+2` and `four` normalise to\nthe same term.\n\n#### Exercise `multiplication-untyped` (recommended)\n\nUse the encodings above to translate your definition of\nmultiplication from previous chapters with the Scott\nrepresentation and the encoding of the fixpoint operator.\nConfirm that two times two is four.\n\n#### Exercise `encode-more` (stretch)\n\nAlong the lines above, encode all of the constructs of\nChapter [More][plfa.More],\nsave for primitive numbers, in the untyped lambda calculus.\n","avg_line_length":24.064957265,"max_line_length":84,"alphanum_fraction":0.4037860492} +{"size":1623,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\nonstopmode\n\\documentclass{article}\n\\usepackage{pifont}\n\\usepackage[utf8x]{inputenc}\n\n\\usepackage{latex\/agda}\n\\DeclareUnicodeCharacter{738}{$^\\mathsf{s}$}\n\\DeclareUnicodeCharacter{8759}{::}\n\n\\setlength{\\mathindent}{0pt}\n\n\\begin{document}\n\\begin{code}\n\n{-# OPTIONS --copatterns --sized-types #-}\n\npostulate\n Size : Set\n Size<_ : Size \u2192 Set\n \u2191_ : Size \u2192 Size\n \u221e : Size\n\n{-# BUILTIN SIZE Size #-}\n{-# BUILTIN SIZELT Size<_ #-}\n{-# BUILTIN SIZESUC \u2191_ #-}\n{-# BUILTIN SIZEINF \u221e #-}\n\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : (x : A) (xs : List A) -> List A\n\nrecord _\u00d7_ (A B : Set) : Set where\n constructor _,_\n field\n fst : A\n snd : B\nopen _\u00d7_\n\n-- Sized streams via head\/tail.\n\nrecord Stream {i : Size} (A : Set) : Set where\n coinductive\n constructor _\u2237_\n field\n head : A\n tail : \u2200 {j : Size< i} \u2192 Stream {j} A\nopen Stream public\n\n-- Streams and lists.\n\n-- Prepending a list to a stream.\n\n_++\u02e2_ : \u2200 {i A} \u2192 List A \u2192 Stream {i} A \u2192 Stream {i} A\n[] ++\u02e2 s = s\n(a \u2237 as) ++\u02e2 s = a \u2237 (as ++\u02e2 s)\n\n-- Unfold which produces several outputs at one step\n\nrecord List1 (A : Set) : Set where\n constructor _\u2237_\n field\n first : A\n rest : List A\nopen List1 public\n\nunfold\u207a : \u2200 {S : Size \u2192 Set} {A : Set}\n\n (step : \u2200 {i} \u2192 S i \u2192 List1 A \u00d7 (\u2200 {j : Size< i} \u2192 S j)) \u2192\n\n \u2200 {i} \u2192 (s : S i) \u2192 Stream {i} A\n\nhead (unfold\u207a step s) = first (fst (step s))\ntail (unfold\u207a step s) = let ((_ \u2237 l) , s\u2032) = step s\n in l ++\u02e2 unfold\u207a step s\u2032\n\\end{code}\nProblem: The \u2237 in the last let statement is not colored with constructor color.\n\\end{document}\n","avg_line_length":20.8076923077,"max_line_length":79,"alphanum_fraction":0.5723967961} +{"size":42239,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Lambda: Introduction to Lambda Calculus\"\nlayout : page\nprev : \/Lists\/\npermalink : \/Lambda\/\nnext : \/Properties\/\n---\n\n\\begin{code}\nmodule plfa.Lambda where\n\\end{code}\n\nThe _lambda-calculus_, first published by the logician Alonzo Church in\n1932, is a core calculus with only three syntactic constructs:\nvariables, abstraction, and application. It captures the key concept of\n_functional abstraction_, which appears in pretty much every programming\nlanguage, in the form of either functions, procedures, or methods.\nThe _simply-typed lambda calculus_ (or STLC) is a variant of the\nlambda calculus published by Church in 1940. It has the three\nconstructs above for function types, plus whatever else is required\nfor base types. Church had a minimal base type with no operations.\nWe will instead echo Plotkin's _Programmable Computable\nFunctions_ (PCF), and add operations on natural numbers and\nrecursive function definitions.\n\nThis chapter formalises the simply-typed lambda calculus, giving its\nsyntax, small-step semantics, and typing rules. The next chapter\n[Properties][plfa.Properties]\nproves its main properties, including\nprogress and preservation. Following chapters will look at a number\nof variants of lambda calculus.\n\nBe aware that the approach we take here is _not_ our recommended\napproach to formalisation. Using De Bruijn indices and\ninherently-typed terms, as we will do in\nChapter [DeBruijn][plfa.DeBruijn],\nleads to a more compact formulation. Nonetheless, we begin with named\nvariables, partly because such terms are easier to read and partly\nbecause the development is more traditional.\n\nThe development in this chapter was inspired by the corresponding\ndevelopment in Chapter _Stlc_ of _Software Foundations_ \n(_Programming Language Foundations_). We differ by\nrepresenting contexts explicitly (as lists pairing identifiers with\ntypes) rather than as partial maps (which take identifiers to types),\nwhich will corresponds better to our subsequent development of DeBruin\nnotation. We also differ by taking natural numbers as the base type\nrather than booleans, allowing more sophisticated examples. In\nparticular, we will be able to show (twice!) that two plus two is\nfour.\n\n## Imports\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality using (_\u2261_; _\u2262_; refl)\nopen import Data.String using (String)\nopen import Data.String.Unsafe using (_\u225f_)\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Relation.Nullary using (Dec; yes; no; \u00ac_)\nopen import Relation.Nullary.Negation using (\u00ac?)\n\\end{code}\n\n## Syntax of terms\n\nTerms have seven constructs. Three are for the core lambda calculus:\n\n * Variables `` ` x ``\n * Abstractions `\u019b x \u21d2 N`\n * Applications `L \u00b7 M`\n\nThree are for the naturals:\n\n * Zero `` `zero ``\n * Successor `` `suc ``\n * Case `` case L [zero\u21d2 M |suc x \u21d2 N ] ``\n\nAnd one is for recursion:\n\n * Fixpoint `\u03bc x \u21d2 M`\n\nAbstraction is also called _lambda abstraction_, and is the construct\nfrom which the calculus takes its name.\n\nWith the exception of variables and fixpoints, each term\nform either constructs a value of a given type (abstractions yield functions,\nzero and successor yield booleans) or deconstructs it (applications use functions,\ncase terms use naturals). We will see this again when we come\nto the rules for assigning types to terms, where constructors\ncorrespond to introduction rules and deconstructors to eliminators.\n\nHere is the syntax of terms in BNF.\n\n L, M, N ::=\n ` x | \u019b x \u21d2 N | L \u00b7 M |\n `zero | `suc M | case L [zero\u21d2 M |suc x \u21d2 N] |\n \u03bc x \u21d2 M\n\nAnd here it is formalised in Agda.\n\\begin{code}\nId : Set\nId = String\n\ninfix 5 \u019b_\u21d2_\ninfix 5 \u03bc_\u21d2_\ninfixl 7 _\u00b7_\ninfix 8 `suc_\ninfix 9 `_\n\ndata Term : Set where\n `_ : Id \u2192 Term\n \u019b_\u21d2_ : Id \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n `zero : Term\n `suc_ : Term \u2192 Term\n case_[zero\u21d2_|suc_\u21d2_] : Term \u2192 Term \u2192 Id \u2192 Term \u2192 Term\n \u03bc_\u21d2_ : Id \u2192 Term \u2192 Term\n\\end{code}\nWe represent identifiers by strings. We choose precedence so that\nlambda abstraction and fixpoint bind least tightly, then application,\nthen successor, and tightest of all is the constructor for variables.\nCase expressions are self-bracketing.\n\n\n### Example terms\n\nHere are some example terms: the natural number two,\na function that adds naturals,\nand a term that computes two plus two.\n\\begin{code}\ntwo : Term\ntwo = `suc `suc `zero\n\nplus : Term\nplus = \u03bc \"+\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\"\n [zero\u21d2 ` \"n\"\n |suc \"m\" \u21d2 `suc (` \"+\" \u00b7 ` \"m\" \u00b7 ` \"n\") ]\n\n2+2 : Term\n2+2 = plus \u00b7 two \u00b7 two\n\\end{code}\nThe recursive definition of addition is similar to our original\ndefinition of `_+_` for naturals, as given in\nChapter [Naturals][plfa.Naturals#plus].\nHere variable \"m\" is bound twice, once in a lambda abstraction and once in\nthe successor branch of the case; the first use of \"m\" refers to\nthe former and the second to the latter. Any use of \"m\" in the successor branch\nmust refer to the latter binding, and so we say that the latter binding _shadows_\nthe former. Later we will confirm that two plus two is four, in other words that\nthe term\n\n plus \u00b7 two \u00b7 two\n\nreduces to `` `suc `suc `suc `suc `zero ``.\n\nAs a second example, we use higher-order functions to represent\nnatural numbers. In particular, the number _n_ is represented by a\nfunction that accepts two arguments and applies the first _n_ times to the\nsecond. This is called the _Church representation_ of the\nnaturals. Here are some example terms: the Church numeral two, a\nfunction that adds Church numerals, a function to compute successor,\nand a term that computes two plus two.\n\\begin{code}\ntwo\u1d9c : Term\ntwo\u1d9c = \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")\n\nplus\u1d9c : Term\nplus\u1d9c = \u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2\n ` \"m\" \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\")\n\nsuc\u1d9c : Term\nsuc\u1d9c = \u019b \"n\" \u21d2 `suc (` \"n\")\n\nfour\u1d9c : Term\nfour\u1d9c = plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n\\end{code}\nThe Church numeral for two takes two arguments `s` and `z`\nand applies `s` twice to `z`.\nAddition takes two numerals `m` and `n`, a\nfunction `s` and an argument `z`, and it uses `m` to apply `s` to the\nresult of using `n` to apply `s` to `z`; hence `s` is applied `m` plus\n`n` times to `z`, yielding the Church numeral for the sum of `m` and\n`n`. For convenience, we define a function that computes successor;\nto convert a Church numeral to the corresponding natural, we apply\nit to this function and the natural number zero.\nAgain, later we will confirm that two plus two is four,\nin other words that the term\n\n plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n\nreduces to `` `suc `suc `suc `suc `zero ``.\n\n\n#### Exercise `mul` (recommended)\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers. Your definition may use `plus` as\ndefined earlier.\n\n\n#### Exercise `mul\u1d9c`\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers represented as Church numerals. Your\ndefinition may use `plus\u1d9c` as defined earlier (or may not\n\u2014 there are nice definitions both ways).\n\n\n#### Exercise `primed` (stretch)\n\nWe can make examples with lambda terms slighly easier to write\nby adding the following definitions.\n\\begin{code}\n\u019b\u2032_\u21d2_ : Term \u2192 Term \u2192 Term\n\u019b\u2032 (` x) \u21d2 N = \u019b x \u21d2 N\n\u019b\u2032 _ \u21d2 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\ncase\u2032_[zero\u21d2_|suc_\u21d2_] : Term \u2192 Term \u2192 Term \u2192 Term \u2192 Term\ncase\u2032 L [zero\u21d2 M |suc (` x) \u21d2 N ] = case L [zero\u21d2 M |suc x \u21d2 N ]\ncase\u2032 _ [zero\u21d2 _ |suc _ \u21d2 _ ] = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n\u03bc\u2032_\u21d2_ : Term \u2192 Term \u2192 Term\n\u03bc\u2032 (` x) \u21d2 N = \u03bc x \u21d2 N\n\u03bc\u2032 _ \u21d2 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\nThe definition of `plus` can now be written as follows.\n\\begin{code}\nplus\u2032 : Term\nplus\u2032 = \u03bc\u2032 + \u21d2 \u019b\u2032 m \u21d2 \u019b\u2032 n \u21d2\n case\u2032 m\n [zero\u21d2 n\n |suc m \u21d2 `suc (+ \u00b7 m \u00b7 n) ]\n where\n + = ` \"+\"\n m = ` \"m\"\n n = ` \"n\"\n\\end{code}\nWrite out the definition of multiplication in the same style.\n\n\n### Formal vs informal\n\nIn informal presentation of formal semantics, one uses choice of\nvariable name to disambiguate and writes `x` rather than `` ` x ``\nfor a term that is a variable. Agda requires we distinguish.\n\nSimilarly, informal presentation often use the same notation for\nfunction types, lambda abstraction, and function application in both\nthe object language (the language one is describing) and the\nmeta-language (the language in which the description is written),\ntrusting readers can use context to distinguish the two. Agda is\nnot quite so forgiving, so here we use `\u019b x \u21d2 N` and `L \u00b7 M` for the\nobject language, as compared to `\u03bb x \u2192 N` and `L M` in our\nmeta-language, Agda.\n\n\n### Bound and free variables\n\nIn an abstraction `\u019b x \u21d2 N` we call `x` the _bound_ variable\nand `N` the _body_ of the abstraction. A central feature\nof lambda calculus is that consistent renaming of bound variables\nleaves the meaning of a term unchanged. Thus the five terms\n\n* `` \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n* `` \u019b \"f\" \u21d2 \u019b \"x\" \u21d2 ` \"f\" \u00b7 (` \"f\" \u00b7 ` \"x\") ``\n* `` \u019b \"sam\" \u21d2 \u019b \"zelda\" \u21d2 ` \"sam\" \u00b7 (` \"sam\" \u00b7 ` \"zelda\") ``\n* `` \u019b \"z\" \u21d2 \u019b \"s\" \u21d2 ` \"z\" \u00b7 (` \"z\" \u00b7 ` \"s\") ``\n* `` \u019b \"\ud83d\ude07\" \u21d2 \u019b \"\ud83d\ude08\" \u21d2 ` \"\ud83d\ude07\" \u00b7 (` \"\ud83d\ude07\" \u00b7 ` \"\ud83d\ude08\" ) ``\n\nare all considered equivalent. Following the convention introduced\nby Haskell Curry, who used the Greek letter `\u03b1` (_alpha_) to label such rules,\nthis equivalence relation is called _alpha renaming_.\n\nAs we descend from a term into its subterms, variables\nthat are bound may become free. Consider the following terms.\n\n* `` \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has both `s` and `z` as bound variables.\n\n* `` \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has `z` bound and `s` free.\n\n* `` ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") ``\n has both `s` and `z` as free variables.\n\nWe say that a term with no free variables is _closed_; otherwise it is\n_open_. Of the three terms above, the first is closed and the other\ntwo are open. We will focus on reduction of closed terms.\n\nDifferent occurrences of a variable may be bound and free.\nIn the term\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 ` \"x\"\n\nthe inner occurrence of `x` is bound while the outer occurrence is free.\nBy alpha renaming, the term above is equivalent to\n\n (\u019b \"y\" \u21d2 ` \"y\") \u00b7 ` \"x\"\n\nin which `y` is bound and `x` is free. A common convention, called the\n_Barendregt convention_, is to use alpha renaming to ensure that the bound\nvariables in a term are distinct from the free variables, which can\navoid confusions that may arise if bound and free variables have the\nsame names.\n\nCase and recursion also introduce bound variables, which are also subject\nto alpha renaming. In the term\n\n \u03bc \"+\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\"\n [zero\u21d2 ` \"n\"\n |suc \"m\" \u21d2 `suc (` \"+\" \u00b7 ` \"m\" \u00b7 ` \"n\") ]\n\nnotice that there are two binding occurrences of `m`, one in the first\nline and one in the last line. It is equivalent to the following term,\n\n \u03bc \"plus\" \u21d2 \u019b \"x\" \u21d2 \u019b \"y\" \u21d2\n case ` \"x\"\n [zero\u21d2 ` \"y\"\n |suc \"x\u2032\" \u21d2 `suc (` \"plus\" \u00b7 ` \"x\u2032\" \u00b7 ` \"y\") ]\n\nwhere the two binding occurrences corresponding to `m` now have distinct\nnames, `x` and `x\u2032`.\n\n\n## Values\n\nA _value_ is a term that corresponds to an answer.\nThus, `` `suc `suc `suc `suc `zero` `` is a value,\nwhile `` plus \u00b7 two \u00b7 two `` is not.\nFollowing convention, we treat all function abstractions\nas values; thus, `` plus `` by itself is considered a value.\n\nThe predicate `Value M` holds if term `M` is a value.\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n\n V-\u019b : \u2200 {x N}\n ---------------\n \u2192 Value (\u019b x \u21d2 N)\n\n V-zero :\n -----------\n Value `zero\n\n V-suc : \u2200 {V}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n\\end{code}\n\nWe let `V` and `W` range over values.\n\n\n### Formal vs informal\n\nIn informal presentations of formal semantics, using\n`V` as the name of a metavariable is sufficient to\nindicate that it is a value. In Agda, we must explicitly\ninvoke the `Value` predicate.\n\n### Other approaches\n\nAn alternative is not to focus on closed terms,\nto treat variables as values, and to treat\n`\u019b x \u21d2 N` as a value only if `N` is a value.\nIndeed, this is how Agda normalises terms.\nWe consider this approach in\nChapter [Untyped][plfa.Untyped].\n\n\n## Substitution\n\nThe heart of lambda calculus is the operation of\nsubstituting one term for a variable in another term.\nSubstitution plays a key role in defining the\noperational semantics of function application.\nFor instance, we have\n\n (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 \"z\")) \u00b7 `zero\n \u2014\u2192\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n\nwhere we substitute `suc\u1d9c` for `` ` \"s\" `` and `` `zero `` for `` ` \"z\" ``\nin the body of the function abstraction.\n\nWe write substitution as `N [ x := V ]`, meaning\n\"substitute term `V` for free occurrences of variable `x` in term `N`\",\nor, more compactly, \"substitute `V` for `x` in `N`\".\nSubstitution works if `V` is any closed term;\nit need not be a value, but we use `V` since in fact we\nusually substitute values.\n\nHere are some examples:\n\n* `` (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) [ \"z\" := `zero ] `` yields\n `` suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero) ``\n* `` (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) [ \"s\" := suc\u1d9c ] `` yields\n `` \u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\") ``\n* `` (\u019b \"x\" \u21d2 ` \"y\") [ \"y\" := `zero ] `` yields `` \u019b \"x\" \u21d2 `zero ``\n* `` (\u019b \"x\" \u21d2 ` \"x\") [ \"x\" := `zero ] `` yields `` \u019b \"x\" \u21d2 ` \"x\" ``\n* `` (\u019b \"y\" \u21d2 ` \"y\") [ \"x\" := `zero ] `` yields `` \u019b \"y\" \u21d2 ` \"y\" ``\n\nIn the last but one example, substituting `` `zero `` for `x` in\n`` \u019b \"x\" \u21d2 ` \"x\" `` does _not_ yield `` \u019b \"x\" \u21d2 `zero ``,\nsince `x` is bound in the lambda abstraction.\nThe choice of bound names is irrelevant: both\n`` \u019b \"x\" \u21d2 ` \"x\" `` and `` \u019b \"y\" \u21d2 ` \"y\" `` stand for the\nidentity function. One way to think of this is that `x` within\nthe body of the abstraction stands for a _different_ variable than\n`x` outside the abstraction, they just happen to have the same name.\n\nWe will give a definition of substitution that is only valid\nwhen term substituted for the variable is closed. This is because\nsubstitution by terms that are _not_ closed may require renaming\nof bound variables. For example:\n\n* `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"y\") [ \"y\" := ` \"x\" \u00b7 `zero] `` should not yield \n `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 (` \"x\" \u00b7 ` `zero)) ``\n\nInstead, we should rename the bound variable to avoid capture.\n\n* `` (\u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"y\") [ \"y\" := ` \"x\" \u00b7 `zero ] `` should yield \n `` \u019b \"x\u2032\" \u21d2 ` \"x\u2032\" \u00b7 (` \"x\" \u00b7 `zero) ``\n\nHere `x\u2032` is a fresh variable distinct from `x`.\nFormal definition of substitution with suitable renaming is considerably\nmore complex, so we avoid it by restricting to substitution by closed terms,\nwhich will be adequate for our purposes.\n\nHere is the formal definition of substitution by closed terms in Agda.\n\n\\begin{code}\ninfix 9 _[_:=_]\n\n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\n(` x) [ y := V ] with x \u225f y\n... | yes _ = V\n... | no _ = ` x\n(\u019b x \u21d2 N) [ y := V ] with x \u225f y\n... | yes _ = \u019b x \u21d2 N\n... | no _ = \u019b x \u21d2 N [ y := V ]\n(L \u00b7 M) [ y := V ] = L [ y := V ] \u00b7 M [ y := V ]\n(`zero) [ y := V ] = `zero\n(`suc M) [ y := V ] = `suc M [ y := V ]\n(case L [zero\u21d2 M |suc x \u21d2 N ]) [ y := V ] with x \u225f y\n... | yes _ = case L [ y := V ] [zero\u21d2 M [ y := V ] |suc x \u21d2 N ]\n... | no _ = case L [ y := V ] [zero\u21d2 M [ y := V ] |suc x \u21d2 N [ y := V ] ]\n(\u03bc x \u21d2 N) [ y := V ] with x \u225f y\n... | yes _ = \u03bc x \u21d2 N\n... | no _ = \u03bc x \u21d2 N [ y := V ]\n\\end{code}\n\nLet's unpack the first three cases.\n\n* For variables, we compare `y`, the substituted variable,\nwith `x`, the variable in the term. If they are the same,\nwe yield `V`, otherwise we yield `x` unchanged.\n\n* For abstractions, we compare `w`, the variable we are substituting for,\nwith `x`, the variable bound in the abstraction. If they are the same,\nwe yield the abstraction unchanged, otherwise we subsititute inside the body.\n\n* For application, we recursively substitute in the function\nand the argument.\n\nCase expressions and recursion also have bound variables that are\ntreated similarly to those in lambda abstractions. Otherwise we\nsimply push substitution recursively into the subterms.\n\n\n### Examples\n\nHere is confirmation that the examples above are correct.\n\n\\begin{code}\n_ : (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) [ \"z\" := `zero ] \u2261 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n_ = refl\n\n_ : (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) [ \"s\" := suc\u1d9c ] \u2261 \u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")\n_ = refl\n\n_ : (\u019b \"x\" \u21d2 ` \"y\") [ \"y\" := `zero ] \u2261 \u019b \"x\" \u21d2 `zero\n_ = refl\n\n_ : (\u019b \"x\" \u21d2 ` \"x\") [ \"x\" := `zero ] \u2261 \u019b \"x\" \u21d2 ` \"x\"\n_ = refl\n\n_ : (\u019b \"y\" \u21d2 ` \"y\") [ \"x\" := `zero ] \u2261 \u019b \"y\" \u21d2 ` \"y\"\n_ = refl\n\\end{code}\n\n\n#### Quiz\n\nWhat is the result of the following substitution?\n\n (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) [ \"x\" := `zero ]\n\n1. `` (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) ``\n2. `` (\u019b \"y\" \u21d2 ` \"x\" \u00b7 (\u019b \"x\" \u21d2 `zero)) ``\n3. `` (\u019b \"y\" \u21d2 `zero \u00b7 (\u019b \"x\" \u21d2 ` \"x\")) ``\n4. `` (\u019b \"y\" \u21d2 `zero \u00b7 (\u019b \"x\" \u21d2 `zero)) ``\n\n\n#### Exercise `_[_:=_]\u2032` (stretch)\n\nThe definition of substitution above has three clauses (`\u019b`, `case`,\nand `\u03bc`) that invoke a with clause to deal with bound variables.\nRewrite the definition to factor the common part of these three\nclauses into a single function, defined by mutual recursion with\nsubstitution.\n\n\n## Reduction\n\nWe give the reduction rules for call-by-value lambda calculus. To\nreduce an application, first we reduce the left-hand side until it\nbecomes a value (which must be an abstraction); then we reduce the\nright-hand side until it becomes a value; and finally we substitute\nthe argument for the variable in the abstraction.\n\nIn an informal presentation of the operational semantics,\nthe rules for reduction of applications are written as follows.\n\n L \u2014\u2192 L\u2032\n -------------- \u03be-\u00b7\u2081\n L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n M \u2014\u2192 M\u2032\n -------------- \u03be-\u00b7\u2082\n V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n ---------------------------- \u03b2-\u019b\n (\u019b x \u21d2 N) \u00b7 V \u2014\u2192 N [ x := V ] \n\nThe Agda version of the rules below will be similar, except that universal\nquantifications are made explicit, and so are the predicates that indicate\nwhich terms are values.\n\nThe rules break into two sorts. Compatibility rules direct us to\nreduce some part of a term. We give them names starting with the\nGreek letter `\u03be` (_xi_). Once a term is sufficiently reduced, it will\nconsist of a constructor and a deconstructor, in our case `\u03bb` and `\u00b7`,\nwhich reduces directly. We give them names starting with the Greek\nletter `\u03b2` (_beta_) and such rules are traditionally called _beta rules_.\n\nIf a term is a value, then no reduction applies; conversely,\nif a reduction applies to a term then it is not a value.\nWe will show in the next chapter that for well-typed terms\nthis exhausts the possibilities: for every well-typed term\neither a reduction applies or it is a value.\n\nFor numbers, zero does not reduce and successor reduces the subterm.\nA case expression reduces its argument to a number, and then chooses\nthe zero or successor branch as appropriate. A fixpoint replaces\nthe bound variable by the entire fixpoint term; this is the one\ncase where we substitute by a term that is not a value.\n\nHere are the rules formalised in Agda.\n\n\\begin{code}\ninfix 4 _\u2014\u2192_\n\ndata _\u2014\u2192_ : Term \u2192 Term \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03b2-\u019b : \u2200 {x N V}\n \u2192 Value V\n ------------------------------\n \u2192 (\u019b x \u21d2 N) \u00b7 V \u2014\u2192 N [ x := V ]\n\n \u03be-suc : \u2200 {M M\u2032}\n \u2192 M \u2014\u2192 M\u2032\n ------------------\n \u2192 `suc M \u2014\u2192 `suc M\u2032\n\n \u03be-case : \u2200 {x L L\u2032 M N}\n \u2192 L \u2014\u2192 L\u2032\n -----------------------------------------------------------------\n \u2192 case L [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 case L\u2032 [zero\u21d2 M |suc x \u21d2 N ]\n\n \u03b2-zero : \u2200 {x M N}\n ----------------------------------------\n \u2192 case `zero [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 M\n\n \u03b2-suc : \u2200 {x V M N}\n \u2192 Value V\n ---------------------------------------------------\n \u2192 case `suc V [zero\u21d2 M |suc x \u21d2 N ] \u2014\u2192 N [ x := V ]\n\n \u03b2-\u03bc : \u2200 {x M}\n ------------------------------\n \u2192 \u03bc x \u21d2 M \u2014\u2192 M [ x := \u03bc x \u21d2 M ]\n\\end{code}\n\nThe reduction rules are carefully designed to ensure that subterms\nof a term are reduced to values before the whole term is reduced.\nThis is referred to as _call by value_ reduction.\n\nFurther, we have arranged that subterms are reduced in a\nleft-to-right order. This means that reduction is _deterministic_:\nfor any term, there is at most one other term to which it reduces.\nPut another way, our reduction relation `\u2014\u2192` is in fact a function.\n\n\n#### Quiz\n\nWhat does the following term step to?\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u2014\u2192 ???\n\n1. `` (\u019b \"x\" \u21d2 ` \"x\") ``\n2. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n3. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n\nWhat does the following term step to?\n\n (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u2014\u2192 ???\n\n1. `` (\u019b \"x\" \u21d2 ` \"x\") ``\n2. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n3. `` (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") \u00b7 (\u019b \"x\" \u21d2 ` \"x\") ``\n\nWhat does the following term step to? (Where `two` and `suc\u1d9c` are as defined above.)\n\n two \u00b7 suc\u1d9c \u00b7 `zero \u2014\u2192 ???\n\n1. `` suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero) ``\n2. `` (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero ``\n3. `` `zero ``\n\n\n## Reflexive and transitive closure\n\nA single step is only part of the story. In general, we wish to repeatedly\nstep a closed term until it reduces to a value. We do this by defining\nthe reflexive and transitive closure `\u2014\u21a0` of the step relation `\u2014\u2192`.\n\nWe define reflexive and transitive closure as a sequence of zero or\nmore steps of the underlying relation, along lines similar to that for\nreasoning about chains of equalities\nChapter [Equality][plfa.Equality].\n\\begin{code}\ninfix 2 _\u2014\u21a0_\ninfix 1 begin_\ninfixr 2 _\u2014\u2192\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u2014\u21a0_ : Term \u2192 Term \u2192 Set where\n _\u220e : \u2200 M\n ---------\n \u2192 M \u2014\u21a0 M\n\n _\u2014\u2192\u27e8_\u27e9_ : \u2200 L {M N}\n \u2192 L \u2014\u2192 M\n \u2192 M \u2014\u21a0 N\n ---------\n \u2192 L \u2014\u21a0 N\n\nbegin_ : \u2200 {M N}\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 M \u2014\u21a0 N\nbegin M\u2014\u21a0N = M\u2014\u21a0N\n\\end{code}\nWe can read this as follows.\n\n* From term `M`, we can take no steps, giving a step of type `M \u2014\u21a0 M`.\n It is written `M \u220e`.\n\n* From term `L` we can take a single of type `L \u2014\u2192 M` followed by zero\n or more steps of type `M \u2014\u21a0 N`, giving a step of type `L \u2014\u21a0 N`. It is\n written `L \u2014\u2192\u27e8 L\u2014\u2192M \u27e9 M\u2014\u21a0N`, where `L\u2014\u2192M` and `M\u2014\u21a0N` are steps of the\n appropriate type.\n\nThe notation is chosen to allow us to lay out example reductions in an\nappealing way, as we will see in the next section.\n\nAs alternative is to define reflexive and transitive closure directly,\nas the smallest relation that includes `\u2014\u2192` and is also reflexive\nand transitive. We could do so as follows.\n\\begin{code}\ndata _\u2014\u21a0\u2032_ : Term \u2192 Term \u2192 Set where\n\n step\u2032 : \u2200 {M N}\n \u2192 M \u2014\u2192 N\n -------\n \u2192 M \u2014\u21a0\u2032 N\n\n refl\u2032 : \u2200 {M}\n -------\n \u2192 M \u2014\u21a0\u2032 M\n\n trans\u2032 : \u2200 {L M N}\n \u2192 L \u2014\u21a0\u2032 M\n \u2192 M \u2014\u21a0\u2032 N\n -------\n \u2192 L \u2014\u21a0\u2032 N\n\\end{code}\nThe three constructors specify, respectively, that `\u2014\u21a0\u2032` includes `\u2014\u2192`\nand is reflexive and transitive. A good exercise is to show that\nthe two definitions are equivalent (indeed, one embeds in the other).\n\n#### Exercise `\u2014\u21a0\u2272\u2014\u21a0\u2032`\n\nShow that the first notion of reflexive and transitive closure\nabove embeds into the second. Why are they not isomorphic?\n\n## Confluence\n\nOne important property a reduction relation might satisfy is\nto be _confluent_. If term `L` reduces to two other terms,\n`M` and `N`, then both of these reduce to a common term `P`.\nIt can be illustrated as follows.\n\n L\n \/ \\\n \/ \\\n \/ \\\n M N\n \\ \/\n \\ \/\n \\ \/\n P\n\nHere `L`, `M`, `N` are universally quantified while `P`\nis existentially quantified. If each line stand for zero\nor more reduction steps, this is called confluence,\nwhile if the top two lines stand for a single reduction\nstep and the bottom two stand for zero or more reduction\nsteps it is called the diamond property. In symbols:\n\n confluence : \u2200 {L M N} \u2192 \u2203[ P ]\n ( ((L \u2014\u21a0 M) \u00d7 (L \u2014\u21a0 N))\n --------------------\n \u2192 ((M \u2014\u21a0 P) \u00d7 (N \u2014\u21a0 P)) )\n\n diamond : \u2200 {L M N} \u2192 \u2203[ P ]\n ( ((L \u2014\u2192 M) \u00d7 (L \u2014\u2192 N))\n --------------------\n \u2192 ((M \u2014\u21a0 P) \u00d7 (N \u2014\u21a0 P)) )\n\nAll of the reduction systems studied in this text are determistic.\nIn symbols:\n\n deterministic : \u2200 {L M N}\n \u2192 L \u2014\u2192 M\n \u2192 L \u2014\u2192 N\n ------\n \u2192 M \u2261 N\n\nIt is easy to show that every deterministic relation satisfies\nthe diamond property, and that every relation that satisfies\nthe diamond property is confluent. Hence, all the reduction\nsystems studied in this text are trivially confluent.\n\n\n## Examples\n\nWe start with a simple example. The Church numeral two applied to the\nsuccessor function and zero yields the natural number two.\n\\begin{code}\n_ : two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2014\u21a0 `suc `suc `zero\n_ =\n begin\n two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero\n \u2014\u2192\u27e8 \u03b2-\u019b V-zero \u27e9\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero) \u27e9\n suc\u1d9c \u00b7 `suc `zero\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc V-zero) \u27e9\n `suc (`suc `zero)\n \u220e\n\\end{code}\n\nHere is a sample reduction demonstrating that two plus two is four.\n\\begin{code}\n_ : plus \u00b7 two \u00b7 two \u2014\u21a0 `suc `suc `suc `suc `zero\n_ =\n begin\n plus \u00b7 two \u00b7 two\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc) \u27e9\n (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two \u00b7 two\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n (\u019b \"n\" \u21d2\n case two [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc V-zero)) \u27e9\n case two [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ]\n \u2014\u2192\u27e8 \u03b2-suc (V-suc V-zero) \u27e9\n `suc (plus \u00b7 `suc `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc)) \u27e9\n `suc ((\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 `suc `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u019b (V-suc V-zero))) \u27e9\n `suc ((\u019b \"n\" \u21d2\n case `suc `zero [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n `suc (case `suc `zero [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ])\n \u2014\u2192\u27e8 \u03be-suc (\u03b2-suc V-zero) \u27e9\n `suc `suc (plus \u00b7 `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 \u03b2-\u03bc))) \u27e9\n `suc `suc ((\u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n case ` \"m\" [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 `zero \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u019b V-zero))) \u27e9\n `suc `suc ((\u019b \"n\" \u21d2\n case `zero [zero\u21d2 ` \"n\" |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 ` \"n\") ])\n \u00b7 two)\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u019b (V-suc (V-suc V-zero)))) \u27e9\n `suc `suc (case `zero [zero\u21d2 two |suc \"m\" \u21d2 `suc (plus \u00b7 ` \"m\" \u00b7 two) ])\n \u2014\u2192\u27e8 \u03be-suc (\u03be-suc \u03b2-zero) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\nAnd here is a similar sample reduction for Church numerals.\n\\begin{code}\n_ : four\u1d9c \u2014\u21a0 `suc `suc `suc `suc `zero\n_ =\n begin\n (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"m\" \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\"))\n \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b))) \u27e9\n (\u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 two\u1d9c \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\"))\n \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b)) \u27e9\n (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 two\u1d9c \u00b7 ` \"s\" \u00b7 (two\u1d9c \u00b7 ` \"s\" \u00b7 ` \"z\")) \u00b7 suc\u1d9c \u00b7 `zero\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 two\u1d9c \u00b7 suc\u1d9c \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero\n \u2014\u2192\u27e8 \u03b2-\u019b V-zero \u27e9\n two\u1d9c \u00b7 suc\u1d9c \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03be-\u00b7\u2081 (\u03b2-\u019b V-\u019b)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 ((\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `zero))\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b V-zero)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (suc\u1d9c \u00b7 (`suc `zero))\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b (V-suc V-zero)) \u27e9\n (\u019b \"z\" \u21d2 suc\u1d9c \u00b7 (suc\u1d9c \u00b7 ` \"z\")) \u00b7 (`suc `suc `zero)\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc V-zero)) \u27e9\n suc\u1d9c \u00b7 (suc\u1d9c \u00b7 `suc `suc `zero)\n \u2014\u2192\u27e8 \u03be-\u00b7\u2082 V-\u019b (\u03b2-\u019b (V-suc (V-suc V-zero))) \u27e9\n suc\u1d9c \u00b7 (`suc `suc `suc `zero)\n \u2014\u2192\u27e8 \u03b2-\u019b (V-suc (V-suc (V-suc V-zero))) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\nIn the next chapter, we will see how to compute such reduction sequences.\n\n\n#### Exercise `plus-example`\n\nWrite out the reduction sequence demonstrating that one plus one is two.\n\n\n## Syntax of types\n\nWe have just two types:\n\n * Functions, `A \u21d2 B`\n * Naturals, `` `\u2115 ``\n\nAs before, to avoid overlap we use variants of the names used by Agda.\n\nHere is the syntax of types in BNF.\n\n A, B, C ::= A \u21d2 B | `\u2115\n\nAnd here it is formalised in Agda.\n\n\\begin{code}\ninfixr 7 _\u21d2_\n\ndata Type : Set where\n _\u21d2_ : Type \u2192 Type \u2192 Type\n `\u2115 : Type\n\\end{code}\n\n### Precedence\n\nAs in Agda, functions of two or more arguments are represented via\ncurrying. This is made more convenient by declaring `_\u21d2_` to\nassociate to the right and `_\u00b7_` to associate to the left.\nThus,\n\n* ``(`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115`` stands for ``((`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115))``\n* `plus \u00b7 two \u00b7 two` stands for `(plus \u00b7 two) \u00b7 two`.\n\n### Quiz\n\n* What is the type of the following term?\n\n \u019b \"s\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 `zero)\n\n 1. `` (`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 2. `` (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 ``\n 3. `` `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 4. `` `\u2115 \u21d2 `\u2115 \u21d2 `\u2115 ``\n 5. `` `\u2115 \u21d2 `\u2115 ``\n 6. `` `\u2115 ``\n\n Give more than one answer if appropriate.\n\n* What is the type of the following term?\n\n (\u019b \"s\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 `zero)) \u00b7 suc\u1d50\n\n 1. `` (`\u2115 \u21d2 `\u2115) \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 2. `` (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 ``\n 3. `` `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115) ``\n 4. `` `\u2115 \u21d2 `\u2115 \u21d2 `\u2115 ``\n 5. `` `\u2115 \u21d2 `\u2115 ``\n 6. `` `\u2115 ``\n\n Give more than one answer if appropriate.\n\n\n## Typing\n\n### Contexts\n\nWhile reduction considers only closed terms, typing must\nconsider terms with free variables. To type a term,\nwe must first type its subterms, and in particular in the\nbody of an abstraction its bound variable may appear free.\n\nA _context_ associates variables with types. We let `\u0393` and `\u0394` range\nover contexts. We write `\u2205` for the empty context, and `\u0393 , x \u2982 A`\nfor the context that extends `\u0393` by mapping variable `x` to type `A`.\nFor example,\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 ``\n\nis the context that associates variable ` \"s\" ` with type `` `\u2115 \u21d2 `\u2115 ``,\nand variable ` \"z\" ` with type `` `\u2115 ``.\n\nContexts are formalised as follows.\n\n\\begin{code}\ninfixl 5 _,_\u2982_\n\ndata Context : Set where\n \u2205 : Context\n _,_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Context\n\\end{code}\n\n### Lookup judgement\n\nWe have two forms of _judgement_. The first is written\n\n \u0393 \u220b x \u2982 A\n\nand indicates in context `\u0393` that variable `x` has type `A`.\nIt is called _lookup_.\nFor example\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u220b \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u220b \"s\" \u2982 `\u2115 \u21d2 `\u2115 ``\n\ngive us the types associated with variables `` \"z\" `` and `` \"s\" ``,\nrespectively. The symbol `\u220b` (pronounced \"ni\", for \"in\"\nbackwards) is chosen because checking that `\u0393 \u220b x \u2982 A` is anologous to\nchecking whether `x \u2982 A` appears in a list corresponding to `\u0393`.\n\nIf two variables in a context have the same name, then lookup\nshould return the most recently bound variable, which _shadows_\nthe other variables. For example,\n\n* `` \u2205 , \"x\" : `\u2115 \u21d2 `\u2115 , \"x\" : `\u2115 \u220b \"x\" \u2982 `\u2115 ``\n\nHere `` \"x\" \u2982 `\u2115 \u21d2 `\u2115 `` is shadowed by `` \"x\" \u2982 `\u2115 ``.\n\nLookup is formalised as follows.\n\\begin{code}\ninfix 4 _\u220b_\u2982_\n\ndata _\u220b_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 x A}\n ------------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 x y A B}\n \u2192 x \u2262 y\n \u2192 \u0393 \u220b x \u2982 A\n ------------------\n \u2192 \u0393 , y \u2982 B \u220b x \u2982 A\n\\end{code}\n\nThe constructors `Z` and `S` correspond roughly to the constructors\n`here` and `there` for the element-of relation `_\u2208_` on lists.\nConstructor `S` takes an additional parameter, which ensures that\nwhen we look up a variable that it is not _shadowed_ by another\nvariable with the same name earlier in the list.\n\n### Typing judgement\n\nThe second judgement is written\n\n \u0393 \u22a2 M \u2982 A\n\nand indicates in context `\u0393` that term `M` has type `A`.\nContext `\u0393` provides types for all the free variables in `M`.\nFor example\n\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"s\" \u2982 `\u2115 \u21d2 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` ` \"s\" \u00b7 ` \"z\" \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 , \"z\" \u2982 `\u2115 \u22a2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 `\u2115 ``\n* `` \u2205 , \"s\" \u2982 `\u2115 \u21d2 `\u2115 \u22a2 (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u2982 `\u2115 \u21d2 `\u2115 ``\n* `` \u2205 \u22a2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\")) \u2982 (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115 ``\n\nTyping is formalised as follows.\n\\begin{code}\ninfix 4 _\u22a2_\u2982_\n\ndata _\u22a2_\u2982_ : Context \u2192 Term \u2192 Type \u2192 Set where\n\n -- Axiom\n \u22a2` : \u2200 {\u0393 x A}\n \u2192 \u0393 \u220b x \u2982 A\n -------------\n \u2192 \u0393 \u22a2 ` x \u2982 A\n\n -- \u21d2-I\n \u22a2\u019b : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2982 B\n -------------------\n \u2192 \u0393 \u22a2 \u019b x \u21d2 N \u2982 A \u21d2 B\n\n -- \u21d2-E\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2982 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2982 A\n -------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2982 B\n\n -- \u2115-I\u2081\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2982 `\u2115\n\n -- \u2115-I\u2082\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2982 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2982 `\u2115\n\n -- \u2115-E\n \u22a2case : \u2200 {\u0393 L M x N A}\n \u2192 \u0393 \u22a2 L \u2982 `\u2115\n \u2192 \u0393 \u22a2 M \u2982 A\n \u2192 \u0393 , x \u2982 `\u2115 \u22a2 N \u2982 A\n -------------------------------------\n \u2192 \u0393 \u22a2 case L [zero\u21d2 M |suc x \u21d2 N ] \u2982 A\n\n \u22a2\u03bc : \u2200 {\u0393 x M A}\n \u2192 \u0393 , x \u2982 A \u22a2 M \u2982 A\n -----------------\n \u2192 \u0393 \u22a2 \u03bc x \u21d2 M \u2982 A\n\\end{code}\n\nEach type rule is named after the constructor for the\ncorresponding term.\n\nMost of the rules have a second name,\nderived from a convention in logic, whereby the rule is\nnamed after the type connective that it concerns;\nrules to introduce and to\neliminate each connective are labeled `-I` and `-E`, respectively. As we\nread the rules from top to bottom, introduction and elimination rules\ndo what they say on the tin: the first _introduces_ a formula for the\nconnective, which appears in the conclusion but not in the premises;\nwhile the second _eliminates_ a formula for the connective, which appears in\na premise but not in the conclusion. An introduction rule describes\nhow to construct a value of the type (abstractions yield functions,\n`` `suc `` and `` `zero `` yield naturals), while an elimination rule describes\nhow to deconstruct a value of the given type (applications use\nfunctions, case expressions use naturals).\n\nThe rules are deterministic, in that at most one rule applies to every term.\n\n\n### Checking inequality and postulating the impossible {#impossible}\n\nThe following function makes it convenient to assert an inequality.\n\\begin{code}\n_\u2260_ : \u2200 (x y : Id) \u2192 x \u2262 y\nx \u2260 y with x \u225f y\n... | no x\u2262y = x\u2262y\n... | yes _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\nHere `_\u225f_` is the function that tests two identifiers for equality.\nWe intend to apply the function only when the\ntwo arguments are indeed unequal, and indicate that the second\ncase should never arise by postulating a term `impossible` of\nwith the empty type `\u22a5`. If we use `C `N to normalise the term\n\n \"a\" \u2260 \"a\"\n\nAgda will return an answer warning us that the impossible has occurred:\n\n \u22a5-elim (.plfa.Lambda.impossible \"a\" \"a\" refl)\n\nWhile postulating the impossible is a useful technique, it must be\nused with care, since such postulation could allow us to provide\nevidence of _any_ proposition whatsoever, regardless of its truth.\n\n\n### Example type derivations {#derivation}\n\nType derivations correspond to trees. In informal notation, here\nis a type derivation for the Church numberal two:\n\n \u220bs \u220bz\n ------------------ \u22a2` -------------- \u22a2`\n \u220bs \u0393\u2082 \u22a2 ` \"s\" \u2982 A \u21d2 A \u0393\u2082 \u22a2 ` \"z\" \u2982 A\n ------------------ \u22a2` ------------------------------------- _\u00b7_\n \u0393\u2082 \u22a2 ` \"s\" \u2982 A \u21d2 A \u0393\u2082 \u22a2 ` \"s\" \u00b7 ` \"z\" \u2982 A\n ---------------------------------------------- _\u00b7_\n \u0393\u2082 \u22a2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 A\n -------------------------------------------- \u22a2\u019b\n \u0393\u2081 \u22a2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 A \u21d2 A\n ------------------------------------------------------------- \u22a2\u019b\n \u0393 \u22a2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\") \u2982 (A \u21d2 A) \u21d2 A \u21d2 A\n\nwhere `\u220bs` and `\u220bz` abbreviate the two derivations:\n\n ---------------- Z\n \"s\" \u2262 \"z\" \u0393\u2081 \u220b \"s\" \u2982 A \u21d2 A\n ----------------------------- S ------------- Z\n \u0393\u2082 \u220b \"s\" \u2982 A \u21d2 A \u0393\u2082 \u220b \"z\" \u2982 A\n\nand where `\u0393\u2081 = \u0393 , s \u2982 A \u21d2 A` and `\u0393\u2082 = \u0393 , s \u2982 A \u21d2 A , z \u2982 A`.\nThe typing derivation is valid for any `\u0393` and `A`, for instance,\nwe might take `\u0393` to be `\u2205` and `A` to be `` `\u2115 ``.\n\nHere is the above typing derivation formalised in Agda.\n\\begin{code}\nCh : Type \u2192 Type\nCh A = (A \u21d2 A) \u21d2 A \u21d2 A\n\n\u22a2two\u1d9c : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 two\u1d9c \u2982 Ch A\n\u22a2two\u1d9c = \u22a2\u019b (\u22a2\u019b (\u22a2` \u220bs \u00b7 (\u22a2` \u220bs \u00b7 \u22a2` \u220bz)))\n where\n \u220bs = S (\"s\" \u2260 \"z\") Z\n \u220bz = Z\n\\end{code}\n\nHere are the typings corresponding to computing two plus two.\n\\begin{code}\n\u22a2two : \u2200 {\u0393} \u2192 \u0393 \u22a2 two \u2982 `\u2115\n\u22a2two = \u22a2suc (\u22a2suc \u22a2zero)\n\n\u22a2plus : \u2200 {\u0393} \u2192 \u0393 \u22a2 plus \u2982 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\n\u22a2plus = \u22a2\u03bc (\u22a2\u019b (\u22a2\u019b (\u22a2case (\u22a2` \u220bm) (\u22a2` \u220bn)\n (\u22a2suc (\u22a2` \u220b+ \u00b7 \u22a2` \u220bm\u2032 \u00b7 \u22a2` \u220bn\u2032)))))\n where\n \u220b+ = (S (\"+\" \u2260 \"m\") (S (\"+\" \u2260 \"n\") (S (\"+\" \u2260 \"m\") Z)))\n \u220bm = (S (\"m\" \u2260 \"n\") Z)\n \u220bn = Z\n \u220bm\u2032 = Z\n \u220bn\u2032 = (S (\"n\" \u2260 \"m\") Z)\n\n\u22a22+2 : \u2205 \u22a2 plus \u00b7 two \u00b7 two \u2982 `\u2115\n\u22a22+2 = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\n\\end{code}\nIn contrast to our earlier examples, here we have typed `two` and `plus`\nin an arbitrary context rather than the empty context; this makes it easy\nto use them inside other binding contexts as well as at the top level.\nHere the two lookup judgements `\u220bm` and `\u220bm\u2032` refer to two different\nbindings of variables named `\"m\"`. In contrast, the two judgements `\u220bn` and\n`\u220bn\u2032` both refer to the same binding of `\"n\"` but accessed in different\ncontexts, the first where \"n\" is the last binding in the context, and\nthe second after \"m\" is bound in the successor branch of the case.\n\nAnd here are typings for the remainder of the Church example.\n\\begin{code}\n\u22a2plus\u1d9c : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 plus\u1d9c \u2982 Ch A \u21d2 Ch A \u21d2 Ch A\n\u22a2plus\u1d9c = \u22a2\u019b (\u22a2\u019b (\u22a2\u019b (\u22a2\u019b (\u22a2` \u220bm \u00b7 \u22a2` \u220bs \u00b7 (\u22a2` \u220bn \u00b7 \u22a2` \u220bs \u00b7 \u22a2` \u220bz)))))\n where\n \u220bm = S (\"m\" \u2260 \"z\") (S (\"m\" \u2260 \"s\") (S (\"m\" \u2260 \"n\") Z))\n \u220bn = S (\"n\" \u2260 \"z\") (S (\"n\" \u2260 \"s\") Z)\n \u220bs = S (\"s\" \u2260 \"z\") Z\n \u220bz = Z\n\n\u22a2suc\u1d9c : \u2200 {\u0393} \u2192 \u0393 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n\u22a2suc\u1d9c = \u22a2\u019b (\u22a2suc (\u22a2` \u220bn))\n where\n \u220bn = Z\n\n\u22a22+2\u1d9c : \u2205 \u22a2 plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero \u2982 `\u2115\n\u22a22+2\u1d9c = \u22a2plus\u1d9c \u00b7 \u22a2two\u1d9c \u00b7 \u22a2two\u1d9c \u00b7 \u22a2suc\u1d9c \u00b7 \u22a2zero\n\\end{code}\n\n### Interaction with Agda\n\nConstruction of a type derivation may be done interactively.\nStart with the declaration:\n\n \u22a2suc\u1d9c : \u2205 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n \u22a2suc\u1d9c = ?\n\nTyping C-l causes Agda to create a hole and tell us its expected type.\n\n \u22a2suc\u1d9c = { }0\n ?0 : \u2205 \u22a2 suc\u1d9c \u2982 `\u2115 \u21d2 `\u2115\n\nNow we fill in the hole by typing C-c C-r. Agda observes that\nthe outermost term in `suc\u1d9c` in `\u22a2\u019b`, which is typed using `\u019b`. The\n`\u019b` rule in turn takes one argument, which Agda leaves as a hole.\n\n \u22a2suc\u1d9c = \u22a2\u019b { }1\n ?1 : \u2205 , \"n\" \u2982 `\u2115 \u22a2 `suc ` \"n\" \u2982 `\u2115\n\nWe can fill in the hole by type C-c C-r again.\n\n \u22a2suc\u1d9c = \u22a2\u019b (\u22a2suc { }2)\n ?2 : \u2205 , \"n\" \u2982 `\u2115 \u22a2 ` \"n\" \u2982 `\u2115\n\nAnd again.\n\n \u22a2suc\u2032 = \u22a2\u019b (\u22a2suc (\u22a2` { }3))\n ?3 : \u2205 , \"n\" \u2982 `\u2115 \u220b \"n\" \u2982 `\u2115\n\nA further attempt with C-c C-r yields the message:\n\n Don't know which constructor to introduce of Z or S\n\nWe can fill in `Z` by hand. If we type C-c C-space, Agda will confirm we are done.\n\n \u22a2suc\u2032 = \u22a2\u019b (\u22a2suc (\u22a2` Z))\n\nThe entire process can be automated using Agsy, invoked with C-c C-a.\n\nChapter [Inference][plfa.DeBruijn]\nwill show how to use Agda to compute type derivations directly.\n\n\n### Lookup is injective\n\nThe lookup relation `\u0393 \u220b x \u2982 A` is injective, in that for each `\u0393` and `x`\nthere is at most one `A` such that the judgement holds.\n\\begin{code}\n\u220b-injective : \u2200 {\u0393 x A B} \u2192 \u0393 \u220b x \u2982 A \u2192 \u0393 \u220b x \u2982 B \u2192 A \u2261 B\n\u220b-injective Z Z = refl\n\u220b-injective Z (S x\u2262 _) = \u22a5-elim (x\u2262 refl)\n\u220b-injective (S x\u2262 _) Z = \u22a5-elim (x\u2262 refl)\n\u220b-injective (S _ \u220bx) (S _ \u220bx\u2032) = \u220b-injective \u220bx \u220bx\u2032\n\\end{code}\n\nThe typing relation `\u0393 \u22a2 M \u2982 A` is not injective. For example, in any `\u0393`\nthe term `\u019b \"x\" \u21d2 \"x\"` has type `A \u21d2 A` for any type `A`.\n\n### Non-examples\n\nWe can also show that terms are _not_ typeable. For example, here is\na formal proof that it is not possible to type the term\n`` `zero \u00b7 `suc `zero ``. It cannot be typed, because doing so\nrequires that the first term in the application is both a natural and\na function.\n\n\\begin{code}\nnope\u2081 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 `zero \u00b7 `suc `zero \u2982 A)\nnope\u2081 (() \u00b7 _)\n\\end{code}\n\nAs a second example, here is a formal proof that it is not possible to\ntype `` \u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"x\" `` It cannot be typed, because\ndoing so requires types `A` and `B` such that `A \u21d2 B \u2261 A`.\n\n\\begin{code}\nnope\u2082 : \u2200 {A} \u2192 \u00ac (\u2205 \u22a2 \u019b \"x\" \u21d2 ` \"x\" \u00b7 ` \"x\" \u2982 A)\nnope\u2082 (\u22a2\u019b (\u22a2` \u220bx \u00b7 \u22a2` \u220bx\u2032)) = contradiction (\u220b-injective \u220bx \u220bx\u2032)\n where\n contradiction : \u2200 {A B} \u2192 \u00ac (A \u21d2 B \u2261 A)\n contradiction ()\n\\end{code}\n\n\n#### Quiz\n\nFor each of the following, given a type `A` for which it is derivable,\nor explain why there is no such `A`.\n\n1. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 , \"x\" \u2982 `\u2115 \u22a2 ` \"y\" \u00b7 ` \"x\" \u2982 A ``\n2. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 , \"x\" \u2982 `\u2115 \u22a2 ` \"x\" \u00b7 ` \"y\" \u2982 A ``\n3. `` \u2205 , \"y\" \u2982 `\u2115 \u21d2 `\u2115 \u22a2 \u019b \"x\" \u21d2 ` \"y\" \u00b7 ` \"x\" \u2982 A ``\n\nFor each of the following, give type `A`, `B`, and `C` for which it is derivable,\nor explain why there are no such types.\n\n1. `` \u2205 , \"x\" \u2982 A \u22a2 ` \"x\" \u00b7 ` \"x\" \u2982 B ``\n2. `` \u2205 , \"x\" \u2982 A , \"y\" \u2982 B \u22a2 \u019b \"z\" \u21d2 ` \"x\" \u00b7 (` \"y\" \u00b7 ` \"z\") \u2982 C ``\n\n\n#### Exercise `mul-type` (recommended)\n\nUsing the term `mul` you defined earlier, write out the derivation\nshowing that it is well-typed.\n\n\n#### Exercise `mul\u1d9c-type`\n\nUsing the term `mul\u1d9c` you defined earlier, write out the derivation\nshowing that it is well-typed.\n\n\n## Unicode\n\nThis chapter uses the following unicode.\n\n \u21d2 U+21D2 RIGHTWARDS DOUBLE ARROW (\\=>)\n \u019b U+019B LATIN SMALL LETTER LAMBDA WITH STROKE (\\Gl-)\n \u00b7 U+00B7 MIDDLE DOT (\\cdot)\n \u2014 U+2014 EM DASH (\\em)\n \u21a0 U+21A0 RIGHTWARDS TWO HEADED ARROW (\\rr-)\n \u03be U+03BE GREEK SMALL LETTER XI (\\Gx or \\xi)\n \u03b2 U+03B2 GREEK SMALL LETTER BETA (\\Gb or \\beta)\n \u220b U+220B CONTAINS AS MEMBER (\\ni)\n \u22a2 U+22A2 RIGHT TACK (\\vdash or \\|-)\n \u2982 U+2982 Z NOTATION TYPE COLON (\\:)\n \ud83d\ude07 U+1F607 SMILING FACE WITH HALO\n \ud83d\ude08 U+1F608 SMILING FACE WITH HORNS\n\nWe compose reduction `\u2014\u2192` from an em dash `\u2014` and an arrow `\u2192`.\nSimilarly for reflexive and transitive closure `\u2014\u21a0`.\n","avg_line_length":31.1267501842,"max_line_length":86,"alphanum_fraction":0.5758422311} +{"size":14597,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"---\ntitle : \"Bisimulation: Relating reduction systems\"\nlayout : page\nprev : \/More\/\npermalink : \/Bisimulation\/\nnext : \/Inference\/\n---\n\n\\begin{code}\nmodule plfa.Bisimulation where\n\\end{code}\n\nSome constructs can be defined in terms of other constructs. In the\nprevious chapter, we saw how _let_ terms can be rewritten as an\napplication of an abstraction, and how two alternative formulations of\nproducts \u2014 one with projections and one with case \u2014 can be formulated\nin terms of each other. In this chapter, we look at how to formalise\nsuch claims. \n\nGiven two different systems, with different terms and reduction rules,\nwe define what it means to claim that one _simulates_ the other.\nLet's call our two systems _source_ and _target_. Let `M`, `N` range\nover terms of the source, and `M\u2020`, `N\u2020` range over terms of the\ntarget. We define a relation\n\n M ~ M\u2020\n\nbetween corresponding terms of the two systems. We have a\n_simulation_ of the source by the target if every reduction\nin the source has a corresponding reduction sequence\nin the target:\n\n_Simulation_: For every `M`, `M\u2020`, and `N`:\nIf `M ~ M\u2020` and `M \u2014\u2192 N`\nthen `M\u2020 \u2014\u21a0 N\u2020` and `N ~ N\u2020`\nfor some `N\u2020`.\n \nOr, in a diagram:\n\n M --- \u2014\u2192 --- N\n | |\n | |\n ~ ~\n | |\n | |\n M\u2020 --- \u2014\u21a0 --- N\u2020\n\nSometimes we will have a stronger condition, where each reduction in the source\ncorresponds to a reduction (rather than a reduction sequence)\nin the target:\n\n M --- \u2014\u2192 --- N\n | |\n | |\n ~ ~\n | |\n | |\n M\u2020 --- \u2014\u2192 --- N\u2020\n\nThis stronger condition is known as _lock-step_ or _on the nose_ simulation.\n\nWe are particularly interested in the situation where there is also\na simulation from the target to the source: every reduction in the\ntarget has a corresponding reduction sequence in the source. This\nsituation is called a _bisimulation_.\n\nSimulation is established by case analysis over all possible\nreductions and all possible terms to which they are related. For each\nreduction step in the source we must show a corresponding reduction\nsequence in the target.\n\nFor instance, the source might be lambda calculus with _let_\nadded, and the target the same system with `let` translated out. \nThe key rule defining our relation will be:\n\n M ~ M\u2020\n N ~ N\u2020\n --------------------------------\n let x = M in N ~ (\u019b x \u21d2 N\u2020) \u00b7 M\u2020\n\nAll the other rules are congruences: variables relate to\nthemselves, and abstractions and applications relate if their\ncomponents relate:\n\n -----\n x ~ x\n\n N ~ N\u2020\n ------------------\n \u019b x \u21d2 N ~ \u019b x \u21d2 N\u2020\n\n L ~ L\u2020\n M ~ M\u2020\n ---------------\n L \u00b7 M ~ L\u2020 \u00b7 M\u2020\n\nCovering the other constructs of our language \u2014 naturals,\nfixpoints, products, and so on \u2014 would add little save length.\n\nIn this case, our relation can be specified by a function\nfrom source to target:\n\n (x) \u2020 = x\n (\u019b x \u21d2 N) \u2020 = \u019b x \u21d2 (N \u2020)\n (L \u00b7 M) \u2020 = (L \u2020) \u00b7 (M \u2020)\n (let x = M in N) \u2020 = (\u019b x \u21d2 (N \u2020)) \u00b7 (M \u2020)\n\nAnd we have\n\n M \u2020 \u2261 N\n -------\n M ~ N\n\nand conversely. But in general we may have a relation without any\ncorresponding function.\n\nThis chapter formalises establishing that `~` as defined\nabove is a simulation from source to target. We leave\nestablishing it in the reverse direction as an exercise.\nAnother exercise is to show the alternative formulations\nof products in\nChapter [More][plfa.More]\nare in bisimulation.\n \n \n## Imports\n\nWe import our source language from\nChapter [More][plfa.More]:\n\\begin{code}\nopen import plfa.More\n\\end{code}\n\n\n## Simulation\n\nThe simulation is a straightforward formalisation of the rules\nin the introduction:\n\\begin{code}\ninfix 4 _~_\ninfix 5 ~\u019b_\ninfix 7 _~\u00b7_\n\ndata _~_ : \u2200 {\u0393 A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n ~` : \u2200 {\u0393 A} {x : \u0393 \u220b A}\n ---------\n \u2192 ` x ~ ` x\n\n ~\u019b_ : \u2200 {\u0393 A B} {N N\u2020 : \u0393 , A \u22a2 B}\n \u2192 N ~ N\u2020\n ----------\n \u2192 \u019b N ~ \u019b N\u2020\n\n _~\u00b7_ : \u2200 {\u0393 A B} {L L\u2020 : \u0393 \u22a2 A \u21d2 B} {M M\u2020 : \u0393 \u22a2 A}\n \u2192 L ~ L\u2020\n \u2192 M ~ M\u2020\n ---------------\n \u2192 L \u00b7 M ~ L\u2020 \u00b7 M\u2020\n\n ~let : \u2200 {\u0393 A B} {M M\u2020 : \u0393 \u22a2 A} {N N\u2020 : \u0393 , A \u22a2 B}\n \u2192 M ~ M\u2020\n \u2192 N ~ N\u2020\n ----------------------\n \u2192 `let M N ~ (\u019b N\u2020) \u00b7 M\u2020\n\\end{code}\nThe language in Chapter [More][plfa.More] has more constructs, which we could easily add.\nHowever, leaving the simulation small let's us focus on the essence.\nIt's a handy technical trick that we can have a large source language,\nbut only bother to include in the simulation the terms of interest.\n\n#### Exercise `_\u2020`\n\nFormalise the translation from source to target given in the introduction.\nShow that `M \u2020 \u2261 N` implies `M ~ N`, and conversely.\n\n\n## Simulation commutes with values\n\nWe need a number of technical results. The first is that simulation\ncommutes with values. That is, if `M ~ M\u2020` and `M` is a value then\n`M\u2020` is also a value:\n\\begin{code}\n~val : \u2200 {\u0393 A} {M M\u2020 : \u0393 \u22a2 A} \n \u2192 M ~ M\u2020\n \u2192 Value M\n --------\n \u2192 Value M\u2020\n~val ~` ()\n~val (~\u019b ~N) V-\u019b = V-\u019b\n~val (~L ~\u00b7 ~M) ()\n~val (~let ~M ~N) ()\n\\end{code}\nIt is a straightforward case analysis, where here the only value\nof interest is a lambda abstraction.\n\n#### Exercise `~val\u207b\u00b9`\n\nShow that this also holds in the reverse direction: if `M ~ M\u2020`\nand `Value M\u2020` then `Value M`.\n\n\n## Simulation commutes with renaming\n\nThe next technical result is that simulation commutes with renaming.\nThat is, if `\u03c1` maps any judgment `\u0393 \u220b A` to a judgment `\u0394 \u220b A`,\nand if `M ~ M\u2020` then `rename \u03c1 M ~ rename \u03c1 M\u2020`:\n\n\\begin{code}\n~rename : \u2200 {\u0393 \u0394}\n \u2192 (\u03c1 : \u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A)\n ----------------------------------------------------------\n \u2192 (\u2200 {A} {M M\u2020 : \u0393 \u22a2 A} \u2192 M ~ M\u2020 \u2192 rename \u03c1 M ~ rename \u03c1 M\u2020)\n~rename \u03c1 (~`) = ~`\n~rename \u03c1 (~\u019b ~N) = ~\u019b (~rename (ext \u03c1) ~N)\n~rename \u03c1 (~L ~\u00b7 ~M) = (~rename \u03c1 ~L) ~\u00b7 (~rename \u03c1 ~M)\n~rename \u03c1 (~let ~M ~N) = ~let (~rename \u03c1 ~M) (~rename (ext \u03c1) ~N)\n\\end{code}\nThe structure of the proof is similar to the structure of renaming itself:\nreconstruct each term with recursive invocation, extending the environment\nwhere appropriate (in this case, only for the body of an abstraction).\n\n\n## Simulation commutes with substitution\n\nThe third technical result is that simulation commutes with substitution.\nIt is more complex than substitution, because where we had one renaming map\n`\u03c1` here we need two substitution maps, `\u03c3` and `\u03c3\u2020`.\n\nThe proof first requires we establish an analogue of extension.\nIf `\u03c3` and `\u03c3\u2020` both map any judgment `\u0393 \u220b A` to a judgment `\u0394 \u22a2 A`,\nsuch that for every `x` in `\u0393 \u220b A` we have `\u03c3 x ~ \u03c3\u2020 x`,\nthen for any `x` in `\u0393 , B \u220b A` we have `exts \u03c3 x ~ exts \u03c3\u2020 x`:\n\\begin{code}\n~exts : \u2200 {\u0393 \u0394}\n \u2192 {\u03c3 : \u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A}\n \u2192 {\u03c3\u2020 : \u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A}\n \u2192 (\u2200 {A} \u2192 (x : \u0393 \u220b A) \u2192 \u03c3 x ~ \u03c3\u2020 x)\n --------------------------------------------------\n \u2192 (\u2200 {A B} \u2192 (x : \u0393 , B \u220b A) \u2192 exts \u03c3 x ~ exts \u03c3\u2020 x)\n~exts ~\u03c3 Z = ~`\n~exts ~\u03c3 (S x) = ~rename S_ (~\u03c3 x)\n\\end{code}\nThe structure of the proof is similar to the structure of extension itself.\nThe newly introduced variable trivially relates to itself, and otherwise\nwe apply renaming to the hypothesis.\n\nWith extension under our belts, it is straightforward to show\nsubstitution commutes. If `\u03c3` and `\u03c3\u2020` both map any judgment `\u0393 \u220b A`\nto a judgment `\u0394 \u22a2 A`, such that for every `x` in `\u0393 \u220b A` we have `\u03c3\nx ~ \u03c3\u2020 x`, and if `M ~ M\u2020`, then `subst \u03c3 M ~ subst \u03c3\u2020 M\u2020`:\n\\begin{code}\n~subst : \u2200 {\u0393 \u0394}\n \u2192 {\u03c3 : \u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A}\n \u2192 {\u03c3\u2020 : \u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A}\n \u2192 (\u2200 {A} \u2192 (x : \u0393 \u220b A) \u2192 \u03c3 x ~ \u03c3\u2020 x)\n ---------------------------------------------------------\n \u2192 (\u2200 {A} {M M\u2020 : \u0393 \u22a2 A} \u2192 M ~ M\u2020 \u2192 subst \u03c3 M ~ subst \u03c3\u2020 M\u2020)\n~subst ~\u03c3 (~` {x = x}) = ~\u03c3 x\n~subst ~\u03c3 (~\u019b ~N) = ~\u019b (~subst (~exts ~\u03c3) ~N)\n~subst ~\u03c3 (~L ~\u00b7 ~M) = (~subst ~\u03c3 ~L) ~\u00b7 (~subst ~\u03c3 ~M)\n~subst ~\u03c3 (~let ~M ~N) = ~let (~subst ~\u03c3 ~M) (~subst (~exts ~\u03c3) ~N)\n\\end{code}\nAgain, the structure of the proof is similar to the structure of\nsubstitution itself: reconstruct each term with recursive invocation,\nextending the environment where appropriate (in this case, only for\nthe body of an abstraction).\n\nFrom the general case of substitution, it is also easy to derive\nthe required special case. If `N ~ N\u2020` and `M ~ M\u2020`, then\n`N [ M ] ~ N\u2020 [ M\u2020 ]`:\n\\begin{code}\n~sub : \u2200 {\u0393 A B} {N N\u2020 : \u0393 , B \u22a2 A} {M M\u2020 : \u0393 \u22a2 B} \n \u2192 N ~ N\u2020\n \u2192 M ~ M\u2020\n -----------------------\n \u2192 (N [ M ]) ~ (N\u2020 [ M\u2020 ])\n~sub {\u0393} {A} {B} ~N ~M = ~subst {\u0393 , B} {\u0393} ~\u03c3 {A} ~N\n where\n ~\u03c3 : \u2200 {A} \u2192 (x : \u0393 , B \u220b A) \u2192 _ ~ _\n ~\u03c3 Z = ~M\n ~\u03c3 (S x) = ~`\n\\end{code}\nOnce more, the structure of the proof resembles the original.\n\n\n## The relation is a simulation\n\nFinally, we can show that the relation actually is a simulation.\nIn fact, we will show the stronger condition of a lock-step simulation.\nWhat we wish to show is:\n\n_Lock-step simulation_: For every `M`, `M\u2020`, and `N`:\nIf `M ~ M\u2020` and `M \u2014\u2192 N`\nthen `M\u2020 \u2014\u2192 N\u2020` and `N ~ N\u2020`\nfor some `N\u2020`.\n \nOr, in a diagram:\n\n M --- \u2014\u2192 --- N\n | |\n | |\n ~ ~\n | |\n | |\n M\u2020 --- \u2014\u2192 --- N\u2020\n\nWe first formulate a concept corresponding to the lower leg\nof the diagram, that is, its right and bottom edges:\n\\begin{code}\ndata Leg {\u0393 A} (M\u2020 N : \u0393 \u22a2 A) : Set where\n\n leg : \u2200 {N\u2020 : \u0393 \u22a2 A}\n \u2192 N ~ N\u2020\n \u2192 M\u2020 \u2014\u2192 N\u2020\n --------\n \u2192 Leg M\u2020 N\n\\end{code}\nFor our formalisation, in this case, we can use a stronger\nrelation than `\u2014\u21a0`, replacing it by `\u2014\u2192`.\n\nWe can now state and prove that the relation is a simulation.\nAgain, in this case, we can use a stronger relation than\n`\u2014\u21a0`, replacing it by `\u2014\u2192`:\n\\begin{code}\nsim : \u2200 {\u0393 A} {M M\u2020 N : \u0393 \u22a2 A}\n \u2192 M ~ M\u2020\n \u2192 M \u2014\u2192 N\n ---------\n \u2192 Leg M\u2020 N\nsim ~` ()\nsim (~\u019b ~N) ()\nsim (~L ~\u00b7 ~M) (\u03be-\u00b7\u2081 L\u2014\u2192)\n with sim ~L L\u2014\u2192\n... | leg ~L\u2032 L\u2020\u2014\u2192 = leg (~L\u2032 ~\u00b7 ~M) (\u03be-\u00b7\u2081 L\u2020\u2014\u2192)\nsim (~V ~\u00b7 ~M) (\u03be-\u00b7\u2082 VV M\u2014\u2192)\n with sim ~M M\u2014\u2192\n... | leg ~M\u2032 M\u2020\u2014\u2192 = leg (~V ~\u00b7 ~M\u2032) (\u03be-\u00b7\u2082 (~val ~V VV) M\u2020\u2014\u2192)\nsim ((~\u019b ~N) ~\u00b7 ~V) (\u03b2-\u019b VV) = leg (~sub ~N ~V) (\u03b2-\u019b (~val ~V VV))\nsim (~let ~M ~N) (\u03be-let M\u2014\u2192)\n with sim ~M M\u2014\u2192\n... | leg ~M\u2032 M\u2020\u2014\u2192 = leg (~let ~M\u2032 ~N) (\u03be-\u00b7\u2082 V-\u019b M\u2020\u2014\u2192)\nsim (~let ~V ~N) (\u03b2-let VV) = leg (~sub ~N ~V) (\u03b2-\u019b (~val ~V VV))\n\\end{code}\nThe proof is by case analysis, examining each possible instance of `M ~ M\u2020`\nand each possible instance of `M \u2014\u2192 M\u2020`, using recursive invocation whenever\nthe reduction is by a `\u03be` rule, and hence contains another reduction.\nIn its structure, it looks a little bit like a proof of progress:\n\n* If the related terms are variables, no reduction applies.\n* If the related terms are abstractions, no reduction applies.\n* If the related terms are applications, there are three subcases:\n - The source term reduces via `\u03be-\u00b7\u2081`, in which case the target term does as well.\n Recursive invocation gives us\n\n L --- \u2014\u2192 --- L\u2032\n | |\n | |\n ~ ~\n | |\n | |\n L\u2020 --- \u2014\u2192 --- L\u2032\u2020\n\n from which follows:\n\n L \u00b7 M --- \u2014\u2192 --- L\u2032 \u00b7 M\n | | \n | |\n ~ ~\n | |\n | |\n L\u2020 \u00b7 M\u2020 --- \u2014\u2192 --- L\u2032\u2020 \u00b7 M\u2020\n \n - The source term reduces via `\u03be-\u00b7\u2082`, in which case the target term does as well.\n Recursive invocation gives us\n\n M --- \u2014\u2192 --- M\u2032\n | |\n | |\n ~ ~\n | |\n | |\n M\u2020 --- \u2014\u2192 --- M\u2032\u2020\n\n from which follows:\n\n V \u00b7 M --- \u2014\u2192 --- V \u00b7 M\u2032\n | | \n | |\n ~ ~\n | |\n | |\n V\u2020 \u00b7 M\u2020 --- \u2014\u2192 --- V\u2020 \u00b7 M\u2032\u2020\n\n Since simulation commutes with values and `V` is a value, `V\u2020` is also a value.\n\n - The source term reduces via `\u03b2-\u019b`, in which case the target term does as well:\n\n (\u019b x \u21d2 N) \u00b7 V --- \u2014\u2192 --- N [ x := V ] \n | | \n | |\n ~ ~\n | |\n | | \n (\u019b x \u21d2 N\u2020) \u00b7 V\u2020 --- \u2014\u2192 --- N\u2020 [ x := V\u2020 ]\n\n Since simulation commutes with values and `V` is a value, `V\u2020` is also a value.\n Since simulation commutes with substitution and `N ~ N\u2020` and `V ~ V\u2020`,\n we have `N [ x := V] ~ N\u2020 [ x := V\u2020 ]`.\n\n* If the related terms are a let and an application of an abstraction,\n there are two subcases:\n\n - The source term reduces via `\u03be-let`, in which case the target term\n reduces via `\u03be-\u00b7\u2082`. Recursive invocation gives us\n\n M --- \u2014\u2192 --- M\u2032\n | |\n | |\n ~ ~\n | |\n | |\n M\u2020 --- \u2014\u2192 --- M\u2032\u2020\n\n from which follows:\n\n let x = M in N --- \u2014\u2192 --- let x = M\u2032 in N\n | |\n | |\n ~ ~\n | |\n | |\n (\u019b x \u21d2 N) \u00b7 M --- \u2014\u2192 --- (\u019b x \u21d2 N) \u00b7 M\u2032\n\n - The source term reduces via `\u03b2-let`, in which case the target\n term reduces via `\u03b2-\u019b`:\n\n let x = V in N --- \u2014\u2192 --- N [ x := V ]\n | |\n | |\n ~ ~\n | |\n | |\n (\u019b x \u21d2 N\u2020) \u00b7 V\u2020 --- \u2014\u2192 --- N\u2020 [ x := V ]\n\n Since simulation commutes with values and `V` is a value, `V\u2020` is also a value.\n Since simulation commutes with substitution and `N ~ N\u2020` and `V ~ V\u2020`,\n we have `N [ x := V] ~ N\u2020 [ x := V\u2020 ]`.\n\n\n#### Exercise `sim\u207b\u00b9`\n\nShow that we also have a simulation in the other direction, and hence that we have\na bisimulation.\n\n#### Exercise `products`\n\nShow that the two formulations of products in\nChapter [More][plfa.More]\nare in bisimulation. The only constructs you need to include are\nvariables, and those connected to functions and products.\nIn this case, the simulation is _not_ lock-step.\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u2020 U+2020 DAGGER (\\dag)\n \u207b U+207B SUPERSCRIPT MINUS (\\^-)\n \u00b9 U+00B9 SUPERSCRIPT ONE (\\^1)\n","avg_line_length":30.991507431,"max_line_length":89,"alphanum_fraction":0.5097622799} +{"size":1392,"ext":"lagda","lang":"Literate Agda","max_stars_count":22.0,"content":"\\documentclass[preprint]{llncs}\n\n%include agda.fmt\n%include main.fmt\n\\include{preamble}\n\n\\begin{document}\n\n\\title{Auto in Agda}\n\\subtitle{Programming proof search using reflection}\n\n\\author{Pepijn Kokke \\and Wouter Swierstra}\n\n\\institute{Universiteit Utrecht\\\\ \\email{pepijn.kokke@@gmail \\quad w.s.swierstra@@uu.nl}}\n\n\\maketitle\n\n\\begin{abstract}\n\n As proofs in type theory become increasingly complex, there is a\n growing need to provide better proof automation. This paper shows\n how to implement a Prolog-style resolution procedure in the\n dependently typed programming language Agda. Connecting this\n resolution procedure to Agda's reflection mechanism provides a\n first-class proof search tactic for first-order Agda\n terms. As a result, writing proof automation tactics need not be\n different from writing any other program.\n\n\\end{abstract}\n\n%include intro.lagda\n%include motivation.lagda\n%include prolog.lagda\n%include reflection.lagda\n%include extensible.lagda\n%include discussion.lagda\n\n\\paragraph{Acknowledgements}\nWe would like to thank the Software Technology Reading Club at the\nUniversiteit Utrecht, and all our anonymous reviewers for their helpful\nfeedback -- we hope we have done their feedback justice.\n\n\\bibliographystyle{plainnat}\n\\bibliography{main}\n\n\\end{document}\n\n%%% Local Variables:\n%%% mode: latex\n%%% TeX-master: t\n%%% TeX-command-default: \"rake\"\n%%% End:\n","avg_line_length":26.2641509434,"max_line_length":89,"alphanum_fraction":0.7837643678} +{"size":10683,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Main where\nopen import Agda.Builtin.IO\nopen import Agda.Builtin.Unit\nopen import Agda.Builtin.String\n\npostulate\n putStrLn : String \u2192 IO \u22a4\n\n{-# FOREIGN GHC import qualified Data.Text.IO as Text #-}\n{-# FOREIGN GHC import qualified Data.Text as T #-}\n{-# COMPILE GHC putStrLn = Text.putStrLn #-}\n\nopen import Type\nopen import Builtin\nopen import Builtin.Constant.Type\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\nopen import Builtin.Signature\n\nopen import Agda.Builtin.TrustMe\nopen import Relation.Binary.PropositionalEquality\nopen import Agda.Builtin.Nat\nopen import Data.Nat\nopen import Agda.Builtin.Int\nopen import Data.Integer\nimport Data.Maybe as M\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Bool\n\nopen import Check hiding (_>>=_; return)\nopen import Scoped.Extrication\nopen import Type.BetaNBE\n\n\nopen Agda.Builtin.IO\nopen import Data.String\npostulate\n return : \u2200 {a} {A : Set a} \u2192 A \u2192 IO A\n _>>=_ : \u2200 {a b} {A : Set a} {B : Set b} \u2192 IO A \u2192 (A \u2192 IO B) \u2192 IO B\n\n{-# COMPILE GHC return = \\_ _ -> return #-}\n{-# COMPILE GHC _>>=_ = \\_ _ _ _ -> (>>=) #-}\n\n_>>_ : \u2200 {a b} {A : Set a} {B : Set b} \u2192 IO A \u2192 IO B \u2192 IO B\nx >> y = x >>= \u03bb _ \u2192 y\n\nopen import Raw\nopen import Scoped\nopen import Utils\n\npostulate\n imap : \u2200{A B : Set} \u2192 (A \u2192 B) \u2192 IO A \u2192 IO B\n mmap : \u2200{A B : Set} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\n mbind : \u2200{A B : Set} \u2192 (A \u2192 Maybe B) \u2192 Maybe A \u2192 Maybe B\n TermN : Set\n Term : Set\n TypeN : Set\n Type : Set\n ProgramN : Set\n Program : Set\n convTm : Term \u2192 RawTm\n convTy : Type \u2192 RawTy\n convP : Program \u2192 RawTm\n readFile : String \u2192 IO ByteString\n parse : ByteString \u2192 Maybe ProgramN\n parseTm : ByteString \u2192 Maybe TermN\n parseTy : ByteString \u2192 Maybe TypeN\n showTerm : RawTm \u2192 String\n getContents : IO ByteString\n exitFailure : IO \u22a4\n exitSuccess : IO \u22a4\n deBruijnify : ProgramN \u2192 Maybe Program\n deBruijnifyTm : TermN \u2192 Maybe Term\n deBruijnifyTy : TypeN \u2192 Maybe Type\n \n{-# FOREIGN GHC import Language.PlutusCore.Name #-}\n{-# FOREIGN GHC import Language.PlutusCore.Lexer #-}\n{-# FOREIGN GHC import Language.PlutusCore.Parser #-}\n{-# FOREIGN GHC import Language.PlutusCore.Pretty #-}\n{-# FOREIGN GHC import Language.PlutusCore.DeBruijn #-}\n{-# FOREIGN GHC import Data.Either #-}\n{-# FOREIGN GHC import System.Exit #-}\n{-# COMPILE GHC exitSuccess = exitSuccess #-}\n{-# COMPILE GHC exitFailure = exitFailure #-}\n{-# FOREIGN GHC import Control.Monad.Trans.Except #-}\n\n\n{-# FOREIGN GHC import Raw #-}\n{-# COMPILE GHC convP = convP #-}\n{-# COMPILE GHC convTm = conv #-}\n{-# COMPILE GHC convTy = convT #-}\n\n{-# FOREIGN GHC import qualified Data.ByteString.Lazy as BSL #-}\n{-# COMPILE GHC getContents = BSL.getContents #-}\n{-# COMPILE GHC imap = \\_ _ -> fmap #-}\n{-# COMPILE GHC mmap = \\_ _ -> fmap #-}\n{-# COMPILE GHC mbind = \\_ _ f a -> f =<< a #-}\n{-# FOREIGN GHC import Data.Either #-}\n{-# COMPILE GHC parse = either (\\_ -> Nothing) Just . parse #-}\n{-# COMPILE GHC parseTm = either (\\_ -> Nothing) Just . parseTm #-}\n{-# COMPILE GHC parseTy = either (\\_ -> Nothing) Just . parseTy #-}\n\n{-# COMPILE GHC deBruijnify = either (\\_ -> Nothing) Just . runExcept . deBruijnProgram #-}\n{-# COMPILE GHC deBruijnifyTm = either (\\_ -> Nothing) Just . runExcept . deBruijnTerm #-}\n{-# COMPILE GHC deBruijnifyTy = either (\\_ -> Nothing) Just . runExcept . deBruijnTy #-}\n{-# FOREIGN GHC import Language.PlutusCore #-}\n{-# COMPILE GHC ProgramN = type Language.PlutusCore.Program TyName Name Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Program = type Language.PlutusCore.Program TyDeBruijn DeBruijn Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC TermN = type Language.PlutusCore.Term TyName Name Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Term = type Language.PlutusCore.Term TyDeBruijn DeBruijn Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC TypeN = type Language.PlutusCore.Type TyName Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Type = type Language.PlutusCore.Type TyDeBruijn Language.PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC readFile = \\ s -> BSL.readFile (T.unpack s) #-}\n{-# COMPILE GHC showTerm = T.pack . show #-}\nopen import Function\n\nopen import Untyped as U\nimport Untyped.Reduction as U\nimport Scoped as S\nimport Scoped.Reduction as S\n\nopen import Data.Sum\n\nmapper : {A B : Set} \u2192 (A \u2192 B) \u2192 Maybe A \u2192 Maybe B\nmapper f nothing = nothing\nmapper f (just a) = just (f a)\n\nopen import Untyped\n\n-- untyped evaluation\n--utestPLC : ByteString \u2192 Maybe String\n--utestplc plc = mmap (U.ugly \u2218 (\u03bb (t : 0 \u22a2) \u2192 proj\u2081 (U.run t 100)) \u2218 erase\u22a2) (mbind (deBruijnifyTm nil) (mmap convP (parse plc)))\n\nopen import Data.Fin\n\npostulate\n prettyPrintTm : RawTm \u2192 String\n prettyPrintTy : RawTy \u2192 String\n\n{-# COMPILE GHC prettyPrintTm = prettyText . unconv (-1) (-1) #-}\n{-# COMPILE GHC prettyPrintTy = prettyText . unconvT (-1) #-}\n\nopen import Data.Vec hiding (_>>=_;_++_)\n\nopen import Scoped.CK\nopen import Algorithmic.CK\n\ndata EvalMode : Set where\n TCK CK L : EvalMode\n\n-- extrinsically typed evaluation\nevalPLC : EvalMode \u2192 ByteString \u2192 String \u228e String\nevalPLC m plc with parse plc\nevalPLC m plc | nothing = inj\u2082 \"parse error\"\nevalPLC m plc | just nt with deBruijnify nt\nevalPLC m plc | just nt | nothing = inj\u2082 \"(Haskell) Scope Error\"\nevalPLC m plc | just nt | just t with scopeCheckTm {0}{Z} (shifter 0 Z (convP t))\nevalPLC m plc | just nt | just t | nothing = inj\u2082 $ \"(Agda) Scope Error\"\n ++ \"\\n\" ++ rawPrinter (shifter 0 Z (convP t))\nevalPLC L plc | just nt | just t | just t' with S.run (saturate t') 1000000\nevalPLC L plc | just nt | just t | just t' | t'' ,, _ ,, inj\u2081 (just _) = inj\u2081 $\n prettyPrintTm (extricateScope (unsaturate t'')) \nevalPLC L plc | just nt | just t | just t' | t'' ,, p ,, inj\u2081 nothing =\n inj\u2082 \"out of fuel\"\nevalPLC L plc | just nt | just t | just t' | t'' ,, p ,, inj\u2082 e = inj\u2082\n (\"runtime error\" Data.String.++\n prettyPrintTm (extricateScope (unsaturate t'')))\nevalPLC CK plc | just nt | just t | just t' with Scoped.CK.stepper 1000000000 _ (\u03b5 \u25bb saturate t')\nevalPLC CK plc | just nt | just t | just t' | n ,, i ,, _ ,, just (\u25a1 {t = t''} V) =\n inj\u2081 (prettyPrintTm (extricateScope (unsaturate t'')))\nevalPLC CK plc | just nt | just t | just t' | _ ,, _ ,, _ ,, just _ =\n inj\u2082 (\"this shouldn't happen\")\nevalPLC CK plc | just nt | just t | just t' | _ ,, _ ,, _ ,, nothing = inj\u2082 \"out of fuel\"\nevalPLC TCK plc | just nt | just t | just t' with inferType _ t'\n... | inj\u2082 e = inj\u2082 \"typechecking error\"\n... | inj\u2081 (A ,, t'') with Algorithmic.CK.stepper 1000000000 _ (\u03b5 \u25bb t'')\n... | _ ,, _ ,, _ ,, _ ,, M.just (\u25a1 {t = t'''} V) =\n inj\u2081 (prettyPrintTm (extricateScope (extricate t''')))\n... | _ ,, _ ,, _ ,, _ ,, M.just _ = inj\u2082 \"this shouldn't happen\"\n... | _ ,, _ ,, _ ,, _ ,, M.nothing = inj\u2082 \"out of fuel\"\n\n\njunk : \u2200{n} \u2192 Vec String n\njunk {zero} = []\njunk {Nat.suc n} = Data.Integer.show (pos n) \u2237 junk\n\ntcPLC : ByteString \u2192 String \u228e String\ntcPLC plc with parse plc\n... | nothing = inj\u2082 \"Parse Error\"\n... | just nt with deBruijnify nt\n... | nothing = inj\u2082 \"(Haskell) Scope Error\"\n... | just t with scopeCheckTm {0}{Z} (shifter 0 Z (convP t))\n... | nothing = inj\u2082 \"(Agda) scope error\"\n... | just t' with inferType _ t'\n... | inj\u2081 (A ,, t'') =\n inj\u2081 (prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A)))\n... | inj\u2082 typeError = inj\u2082 \"typeError\"\n... | inj\u2082 kindEqError = inj\u2082 \"kindEqError\"\n... | inj\u2082 notTypeError = inj\u2082 \"notTypeError\"\n... | inj\u2082 notFunction = inj\u2082 \"notFunction\"\n... | inj\u2082 notPiError = inj\u2082 \"notPiError\"\n... | inj\u2082 notPat = inj\u2082 \"notPat\"\n... | inj\u2082 (nameError x x') = inj\u2082 (x Data.String.++ \" != \" Data.String.++ x')\n... | inj\u2082 (typeEqError n n') = inj\u2082 (\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 n))\n Data.String.++\n \"\\n != \\n\"\n Data.String.++\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 n')))\n \n... | inj\u2082 typeVarEqError = inj\u2082 \"typeVarEqError\"\n... | inj\u2082 tyConError = inj\u2082 \"tyConError\"\n... | inj\u2082 builtinError = inj\u2082 \"builtinError\"\n... | inj\u2082 unwrapError = inj\u2082 \"unwrapError\"\n\nalphaTm : ByteString \u2192 ByteString \u2192 Bool\nalphaTm plc1 plc2 with parseTm plc1 | parseTm plc2\nalphaTm plc1 plc2 | just plc1' | just plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nalphaTm plc1 plc2 | just plc1' | just plc2' | just plc1'' | just plc2'' = decRTm (convTm plc1'') (convTm plc2'')\nalphaTm plc1 plc2 | just plc1' | just plc2' | _ | _ = Bool.false\nalphaTm plc1 plc2 | _ | _ = Bool.false\n\n\n{-# COMPILE GHC alphaTm as alphaTm #-}\nprintTy : ByteString \u2192 String\nprintTy b with parseTy b\n... | nothing = \"parseTy error\"\n... | just A with deBruijnifyTy A\n... | nothing = \"deBruinjifyTy error\"\n... | just A' = rawTyPrinter (convTy A')\n\n{-# COMPILE GHC printTy as printTy #-}\n\n\nalphaTy : ByteString \u2192 ByteString \u2192 Bool\nalphaTy plc1 plc2 with parseTy plc1 | parseTy plc2\nalphaTy plc1 plc2 | just plc1' | just plc2' with deBruijnifyTy plc1' | deBruijnifyTy plc2'\nalphaTy plc1 plc2 | just plc1' | just plc2' | just plc1'' | just plc2'' = decRTy (convTy plc1'') (convTy plc2'')\nalphaTy plc1 plc2 | just plc1' | just plc2' | _ | _ = Bool.false\nalphaTy plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTy as alphaTy #-}\n\n\n{-# FOREIGN GHC import System.Environment #-}\n\nopen import Data.List\n\npostulate getArgs : IO (List String)\n\n{-# COMPILE GHC getArgs = (fmap . fmap) T.pack $ getArgs #-}\n\n{-# FOREIGN GHC import Opts #-}\n\ndata Input : Set where\n FileInput : String \u2192 Input\n StdInput : Input\n\n{-# COMPILE GHC Input = data Input (FileInput | StdInput) #-}\n\ndata EvalOptions : Set where\n EvalOpts : Input \u2192 EvalMode \u2192 EvalOptions\n\ndata TCOptions : Set where\n TCOpts : Input \u2192 TCOptions\n \ndata Command : Set where\n Evaluate : EvalOptions \u2192 Command\n TypeCheck : TCOptions \u2192 Command\n\npostulate execP : IO Command\n\n{-# COMPILE GHC EvalOptions = data EvalOptions (EvalOpts) #-}\n{-# COMPILE GHC TCOptions = data TCOptions (TCOpts) #-}\n{-# COMPILE GHC Command = data Command (Evaluate | TypeCheck) #-}\n{-# COMPILE GHC EvalMode = data EvalMode (TCK | CK | L ) #-}\n{-# COMPILE GHC execP = execP #-}\n\nevalInput : EvalMode \u2192 Input \u2192 IO (String \u228e String)\nevalInput m (FileInput fn) = imap (evalPLC m) (readFile fn)\nevalInput m StdInput = imap (evalPLC m) getContents \n\ntcInput : Input \u2192 IO (String \u228e String)\ntcInput (FileInput fn) = imap tcPLC (readFile fn)\ntcInput StdInput = imap tcPLC getContents\n\nmain' : Command \u2192 IO \u22a4\nmain' (Evaluate (EvalOpts i m)) =\n evalInput m i\n >>=\n Data.Sum.[ (\u03bb s \u2192 putStrLn s >> exitSuccess)\n , (\u03bb e \u2192 putStrLn e >> exitFailure)\n ] \nmain' (TypeCheck (TCOpts i)) =\n (tcInput i)\n >>= \n Data.Sum.[ (\u03bb s \u2192 putStrLn s >> exitSuccess)\n , (\u03bb e \u2192 putStrLn e >> exitFailure)\n ] \n\nmain : IO \u22a4\nmain = execP >>= main'\n\\end{code}\n","avg_line_length":35.1414473684,"max_line_length":130,"alphanum_fraction":0.6607694468} +{"size":1803,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\n\nmodule Scoped.Erasure where\n\\end{code}\n\n\\begin{code}\nopen import Scoped\nopen import Untyped\nopen import Untyped.RenamingSubstitution\nopen import Builtin\nopen import Utils\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin using (Fin;zero;suc)\nopen import Data.Vec\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Sum\nopen import Data.Product\n\\end{code}\n\n\\begin{code}\nlen : \u2200{n} \u2192 Weird\u2115 n \u2192 \u2115\nlen Z = 0\nlen (S i) = suc (len i)\nlen (T i) = len i\n\\end{code}\n\n\\begin{code}\neraseVar : \u2200{n}{i : Weird\u2115 n} \u2192 WeirdFin i \u2192 Fin (len i)\neraseVar Z = zero\neraseVar (S x) = suc (eraseVar x)\neraseVar (T x) = eraseVar x\n\neraseTC : Scoped.TermCon \u2192 Untyped.TermCon\neraseTC (integer i) = integer i\neraseTC (bytestring b) = bytestring b\neraseTC (string s) = string s\neraseTC (bool b) = bool b\neraseTC (char c) = char c\neraseTC unit = unit\n\neraseTm : \u2200{n}{i : Weird\u2115 n} \u2192 ScopedTm i \u2192 len i \u22a2\n\n{-\neraseTel\u22c6 : \u2200{m n}(i : Weird\u2115 n) \u2192 Vec (ScopedTy n) m \u2192 Vec (len i \u22a2) m\neraseTel\u22c6 i [] = []\neraseTel\u22c6 i (A \u2237 As) = plc_dummy \u2237 eraseTel\u22c6 i As\n-}\neraseTel : \u2200{m n}{i : Weird\u2115 n} \u2192 Vec (ScopedTm i) m \u2192 Vec (len i \u22a2) m\n\neraseTel [] = []\neraseTel (t \u2237 ts) = eraseTm t \u2237 eraseTel ts\n\neraseTm (` x) = ` (eraseVar x)\neraseTm (\u039b K t) = delay (eraseTm t)\neraseTm (t \u00b7\u22c6 A) = force (eraseTm t)\neraseTm (\u019b A t) = \u019b (eraseTm t)\neraseTm (t \u00b7 u) = eraseTm t \u00b7 eraseTm u\neraseTm (con c) = con (eraseTC c)\neraseTm (error A) = error\neraseTm (wrap pat arg t) = eraseTm t\neraseTm (unwrap t) = eraseTm t\neraseTm (ibuiltin b) = error -- builtin b (\u2264\u2033\u21d2\u2264\u2034 (\u2264\u21d2\u2264\u2033 z\u2264n)) []\n\\end{code}\n","avg_line_length":26.9104477612,"max_line_length":73,"alphanum_fraction":0.5951192457} +{"size":8120,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Untyped.RenamingSubstitution where\n\\end{code}\n\n\\begin{code}\nopen import Untyped\n\nopen import Data.Nat\nopen import Data.Fin hiding (lift)\nopen import Data.Vec\nopen import Relation.Binary.PropositionalEquality\nopen import Function\nopen import Utils\n\\end{code}\n\n\\begin{code}\nRen : \u2115 \u2192 \u2115 \u2192 Set\nRen m n = Fin m \u2192 Fin n\n\nlift : \u2200{m n} \u2192 Ren m n \u2192 Ren (suc m) (suc n)\nlift \u03c1 zero = zero\nlift \u03c1 (suc x) = suc (\u03c1 x)\n\nren : \u2200{m n} \u2192 Ren m n \u2192 m \u22a2 \u2192 n \u22a2\nrenTel : \u2200{l m n} \u2192 Ren m n \u2192 Tel l m \u2192 Tel l n\n\nren \u03c1 (` x) = ` (\u03c1 x)\nren \u03c1 (\u019b t) = \u019b (ren (lift \u03c1) t)\nren \u03c1 (t \u00b7 u) = ren \u03c1 t \u00b7 ren \u03c1 u\nren \u03c1 (con tcn) = con tcn\nren \u03c1 (builtin b p ts) = builtin b p (renTel \u03c1 ts)\nren \u03c1 error = error\n\nrenTel \u03c1 [] = []\nrenTel \u03c1 (t \u2237 ts) = ren \u03c1 t \u2237 renTel \u03c1 ts\n\nweaken : \u2200{n} \u2192 n \u22a2 \u2192 suc n \u22a2\nweaken t = ren suc t\n\nlift-cong : \u2200{m n}{\u03c1 \u03c1' : Ren m n}\n \u2192 (\u2200 x \u2192 \u03c1 x \u2261 \u03c1' x)\n \u2192 (x : Fin (suc m))\n \u2192 lift \u03c1 x \u2261 lift \u03c1' x\nlift-cong p zero = refl\nlift-cong p (suc x) = cong suc (p x)\n\nren-cong : \u2200{m n}{\u03c1 \u03c1' : Ren m n}\n \u2192 (\u2200 x \u2192 \u03c1 x \u2261 \u03c1' x)\n \u2192 (t : m \u22a2)\n \u2192 ren \u03c1 t \u2261 ren \u03c1' t\n\nrenTel-cong : \u2200{l m n}{\u03c1 \u03c1' : Ren m n}\n \u2192 (\u2200 x \u2192 \u03c1 x \u2261 \u03c1' x)\n \u2192 (ts : Tel l m)\n \u2192 renTel \u03c1 ts \u2261 renTel \u03c1' ts\n \nrenTel-cong p [] = refl\nrenTel-cong p (t \u2237 ts) = cong\u2082 _\u2237_ (ren-cong p t) (renTel-cong p ts)\n\nren-cong p (` x) = cong ` (p x)\nren-cong p (\u019b t) = cong \u019b (ren-cong (lift-cong p) t)\nren-cong p (t \u00b7 u) = cong\u2082 _\u00b7_ (ren-cong p t) (ren-cong p u)\nren-cong p (con c) = refl\nren-cong p (builtin b q ts) = cong (builtin b q) (renTel-cong p ts)\nren-cong p error = refl\n\nlift-id : \u2200{n} \u2192 (x : Fin (suc n)) \u2192 id x \u2261 lift id x\nlift-id zero = refl\nlift-id (suc x) = refl\n\nlift-comp : \u2200{m n o}(g : Ren m n)(f : Ren n o)(x : Fin (suc m))\n \u2192 lift (f \u2218 g) x \u2261 lift f (lift g x)\nlift-comp g f zero = refl\nlift-comp g f (suc x) = refl\n\n\nren-id : \u2200{n} \u2192 (t : n \u22a2) \u2192 t \u2261 ren id t\n\nrenTel-id : \u2200{l n} \u2192 (ts : Tel l n) \u2192 ts \u2261 renTel id ts\n\nrenTel-id [] = refl\nrenTel-id (t \u2237 ts) = cong\u2082 _\u2237_ (ren-id t) (renTel-id ts)\n\nren-id (` x) = refl\nren-id (\u019b t) = cong \u019b (trans (ren-id t) (ren-cong lift-id t)) \nren-id (t \u00b7 u) = cong\u2082 _\u00b7_ (ren-id t) (ren-id u)\nren-id (con c) = refl\nren-id (builtin bn p ts) = cong (builtin bn p) (renTel-id ts)\nren-id error = refl\n\nren-comp : \u2200{m n o}(g : Ren m n)(f : Ren n o)(t : m \u22a2)\n \u2192 ren (f \u2218 g) t \u2261 ren f (ren g t)\n\nrenTel-comp : \u2200{l m n o}(g : Ren m n)(f : Ren n o)(ts : Tel l m)\n \u2192 renTel (f \u2218 g) ts \u2261 renTel f (renTel g ts)\n\nrenTel-comp g f [] = refl\nrenTel-comp g f (t \u2237 ts) = cong\u2082 _\u2237_ (ren-comp g f t) (renTel-comp g f ts)\n\nren-comp \u03c1 \u03c1' (` x) = refl\nren-comp \u03c1 \u03c1' (\u019b t) = cong \u019b (trans\n (ren-cong (lift-comp \u03c1 \u03c1') t)\n (ren-comp (lift \u03c1) (lift \u03c1') t))\nren-comp \u03c1 \u03c1' (t \u00b7 u) = cong\u2082 _\u00b7_ (ren-comp \u03c1 \u03c1' t) (ren-comp \u03c1 \u03c1' u)\nren-comp \u03c1 \u03c1' (con c) = refl\nren-comp \u03c1 \u03c1' (builtin b p ts) = cong (builtin b p) (renTel-comp \u03c1 \u03c1' ts)\nren-comp \u03c1 \u03c1' error = refl \n--\n\nSub : \u2115 \u2192 \u2115 \u2192 Set\nSub m n = Fin m \u2192 n \u22a2\n\nlifts : \u2200{m n} \u2192 Sub m n \u2192 Sub (suc m) (suc n)\nlifts \u03c1 zero = ` zero\nlifts \u03c1 (suc x) = ren suc (\u03c1 x)\n\nsub : \u2200{m n} \u2192 Sub m n \u2192 m \u22a2 \u2192 n \u22a2\nsubTel : \u2200{l m n} \u2192 Sub m n \u2192 Tel l m \u2192 Tel l n\n\nsub \u03c3 (` x) = \u03c3 x\nsub \u03c3 (\u019b t) = \u019b (sub (lifts \u03c3) t) \nsub \u03c3 (t \u00b7 u) = sub \u03c3 t \u00b7 sub \u03c3 u\nsub \u03c3 (con tcn) = con tcn\nsub \u03c3 (builtin b p ts) = builtin b p (subTel \u03c3 ts)\nsub \u03c3 error = error\n\nsubTel \u03c3 [] = []\nsubTel \u03c3 (t \u2237 ts) = sub \u03c3 t \u2237 subTel \u03c3 ts\n\nextend : \u2200{m n} \u2192 Sub m n \u2192 n \u22a2 \u2192 Sub (suc m) n\nextend \u03c3 t zero = t\nextend \u03c3 t (suc x) = \u03c3 x\n\n_[_] : \u2200{n} \u2192 suc n \u22a2 \u2192 n \u22a2 \u2192 n \u22a2\nt [ u ] = sub (extend ` u) t\n\nlifts-cong : \u2200{m n}{\u03c3 \u03c3' : Sub m n}\n \u2192 (\u2200 x \u2192 \u03c3 x \u2261 \u03c3' x)\n \u2192 (x : Fin (suc m))\n \u2192 lifts \u03c3 x \u2261 lifts \u03c3' x\nlifts-cong p zero = refl\nlifts-cong p (suc x) = cong (ren suc) (p x) \n\nsub-cong : \u2200{m n}{\u03c3 \u03c3' : Sub m n}\n \u2192 (\u2200 x \u2192 \u03c3 x \u2261 \u03c3' x)\n \u2192 (t : m \u22a2)\n \u2192 sub \u03c3 t \u2261 sub \u03c3' t\n\nsubTel-cong : \u2200{l m n}{\u03c3 \u03c3' : Sub m n}\n \u2192 (\u2200 x \u2192 \u03c3 x \u2261 \u03c3' x)\n \u2192 (ts : Tel l m)\n \u2192 subTel \u03c3 ts \u2261 subTel \u03c3' ts\n\nsubTel-cong p [] = refl\nsubTel-cong p (t \u2237 ts) = cong\u2082 _\u2237_ (sub-cong p t) (subTel-cong p ts)\n\nsub-cong p (` x) = p x\nsub-cong p (\u019b t) = cong \u019b (sub-cong (lifts-cong p) t)\nsub-cong p (t \u00b7 u) = cong\u2082 _\u00b7_ (sub-cong p t) (sub-cong p u)\nsub-cong p (con c) = refl\nsub-cong p (builtin bn q ts) = cong (builtin bn q) (subTel-cong p ts)\nsub-cong p error = refl\n\nlifts-id : \u2200{n} \u2192 (x : Fin (suc n)) \u2192 ` x \u2261 lifts ` x\nlifts-id zero = refl\nlifts-id (suc x) = refl\n\nsub-id : \u2200{n} \u2192 (t : n \u22a2) \u2192 t \u2261 sub ` t\n\nsubTel-id : \u2200{l n} \u2192 (ts : Tel l n) \u2192 ts \u2261 subTel ` ts\nsubTel-id [] = refl\nsubTel-id (t \u2237 ts) = cong\u2082 _\u2237_ (sub-id t) (subTel-id ts)\n\nsub-id (` x) = refl\nsub-id (\u019b t) = cong \u019b (trans (sub-id t) (sub-cong lifts-id t))\nsub-id (t \u00b7 u) = cong\u2082 _\u00b7_ (sub-id t) (sub-id u)\nsub-id (con c) = refl\nsub-id (builtin b p ts) = cong (builtin b p) (subTel-id ts)\nsub-id error = refl\n\nlifts-lift : \u2200{m n o}(g : Ren m n)(f : Sub n o)(x : Fin (suc m))\n \u2192 lifts (f \u2218 g) x \u2261 lifts f (lift g x)\nlifts-lift g f zero = refl\nlifts-lift g f (suc x) = refl\n\nsub-ren : \u2200{m n o}(\u03c1 : Ren m n)(\u03c3 : Sub n o)(t : m \u22a2)\n \u2192 sub (\u03c3 \u2218 \u03c1) t \u2261 sub \u03c3 (ren \u03c1 t)\nsubTel-renTel : \u2200{l m n o}(g : Ren m n)(f : Sub n o)(ts : Tel l m)\n \u2192 subTel (f \u2218 g) ts \u2261 subTel f (renTel g ts)\nsubTel-renTel g f [] = refl\nsubTel-renTel g f (t \u2237 ts) =\n cong\u2082 _\u2237_ (sub-ren g f t) (subTel-renTel g f ts)\n\nsub-ren \u03c1 \u03c3 (` x) = refl\nsub-ren \u03c1 \u03c3 (\u019b t) = cong \u019b (trans\n (sub-cong (lifts-lift \u03c1 \u03c3) t)\n (sub-ren (lift \u03c1) (lifts \u03c3) t))\nsub-ren \u03c1 \u03c3 (t \u00b7 u) = cong\u2082 _\u00b7_ (sub-ren \u03c1 \u03c3 t) (sub-ren \u03c1 \u03c3 u) \nsub-ren \u03c1 \u03c3 (con c) = refl\nsub-ren \u03c1 \u03c3 (builtin b p ts) = cong (builtin b p) (subTel-renTel \u03c1 \u03c3 ts)\nsub-ren \u03c1 \u03c3 error = refl\n\nren-lift-lifts : \u2200{m n o}(g : Sub m n)(f : Ren n o)(x : Fin (suc m))\n \u2192 lifts (ren f \u2218 g) x \u2261 ren (lift f) (lifts g x)\nren-lift-lifts g f zero = refl\nren-lift-lifts g f (suc x) = trans\n (sym (ren-comp f suc (g x)))\n (ren-comp suc (lift f) (g x))\n\nren-sub : \u2200{m n o}(\u03c3 : Sub m n)(\u03c1 : Ren n o)(t : m \u22a2)\n \u2192 sub (ren \u03c1 \u2218 \u03c3) t \u2261 ren \u03c1 (sub \u03c3 t)\nrenTel-subTel : \u2200{l m n o}(g : Sub m n)(f : Ren n o)(ts : Tel l m)\n \u2192 subTel (ren f \u2218 g) ts \u2261 renTel f (subTel g ts)\n\nren-sub \u03c3 \u03c1 (` x) = refl\nren-sub \u03c3 \u03c1 (\u019b t) = cong \u019b (trans\n (sub-cong (ren-lift-lifts \u03c3 \u03c1) t)\n (ren-sub (lifts \u03c3) (lift \u03c1) t))\nren-sub \u03c3 \u03c1 (t \u00b7 u) = cong\u2082 _\u00b7_ (ren-sub \u03c3 \u03c1 t) (ren-sub \u03c3 \u03c1 u) \nren-sub \u03c3 \u03c1 (con c) = refl\nren-sub \u03c3 \u03c1 (builtin b p ts) = cong (builtin b p) (renTel-subTel \u03c3 \u03c1 ts)\nren-sub \u03c3 \u03c1 error = refl\n\nrenTel-subTel g f [] = refl\nrenTel-subTel g f (t \u2237 ts) =\n cong\u2082 _\u2237_ (ren-sub g f t) (renTel-subTel g f ts)\n\nlifts-comp : \u2200{m n o}(g : Sub m n)(f : Sub n o)(x : Fin (suc m))\n \u2192 lifts (sub f \u2218 g) x \u2261 sub (lifts f) (lifts g x)\nlifts-comp g f zero = refl\nlifts-comp g f (suc x) = trans\n (sym (ren-sub f suc (g x)))\n (sub-ren suc (lifts f) (g x))\n\n-- some auxiliary properties\nrenTel++ : {l l' m n : \u2115}(\u03c1 : Ren m n)\n \u2192 (ts : Tel l m)(ts' : Tel l' m)\n \u2192 renTel \u03c1 (ts ++ ts') \u2261 renTel \u03c1 ts ++ renTel \u03c1 ts'\nrenTel++ \u03c1 [] ts' = refl\nrenTel++ \u03c1 (t \u2237 ts) ts' = cong (_ \u2237_) (renTel++ \u03c1 ts ts')\n\nrenTel:< : {l m n : \u2115}(\u03c1 : Ren m n)\n \u2192 (ts : Tel l m)(t : m \u22a2)\n \u2192 renTel \u03c1 (ts :< t) \u2261 renTel \u03c1 ts :< ren \u03c1 t\nrenTel:< \u03c1 [] t = refl\nrenTel:< \u03c1 (_ \u2237 ts) t = cong (_ \u2237_) (renTel:< \u03c1 ts t)\n\nsubTel++ : {l l' m n : \u2115}(\u03c3 : Sub m n)\n \u2192 (ts : Tel l m)(ts' : Tel l' m)\n \u2192 subTel \u03c3 (ts ++ ts') \u2261 subTel \u03c3 ts ++ subTel \u03c3 ts'\nsubTel++ \u03c3 [] ts' = refl\nsubTel++ \u03c3 (t \u2237 ts) ts' = cong (_ \u2237_) (subTel++ \u03c3 ts ts')\n\nsubTel:< : {l m n : \u2115}(\u03c3 : Sub m n)\n \u2192 (ts : Tel l m)(t : m \u22a2)\n \u2192 subTel \u03c3 (ts :< t) \u2261 subTel \u03c3 ts :< sub \u03c3 t\nsubTel:< \u03c3 [] t = refl\nsubTel:< \u03c3 (_ \u2237 ts) t = cong (_ \u2237_) (subTel:< \u03c3 ts t)\n\\end{code}\n","avg_line_length":31.2307692308,"max_line_length":78,"alphanum_fraction":0.5007389163} +{"size":468,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Declarative.StdLib.Bool where\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Declarative\n\\end{code}\n\n# Term Abbreviations\n\\begin{code}\ntrue : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 boolean\ntrue = \u039b \"\u03b1\" (\u019b \"t\" (\u019b \"f\" (` (S Z'))))\n\nfalse : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 boolean\nfalse = \u039b \"\u03b1\" (\u019b \"t\" (\u019b \"f\" (` Z')))\n\nif : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 \u03a0 \"\u03b1\" (boolean \u21d2 ` Z \u21d2 ` Z \u21d2 ` Z)\nif = \u039b \"\u03b1\" (\u019b \"x\" (\u019b \"y\" (\u019b \"z\" ((` (S (S Z')) \u00b7\u22c6' ` Z) \u00b7 ` (S Z') \u00b7 ` Z'))))\n\\end{code}\n\n","avg_line_length":21.2727272727,"max_line_length":77,"alphanum_fraction":0.4935897436} +{"size":219,"ext":"lagda","lang":"Literate Agda","max_stars_count":4.0,"content":"\\begin{code}\n{-# OPTIONS --cubical --safe #-}\n\nmodule Snippets.Circle where\n\nopen import Prelude\n\\end{code}\n%<*circle-def>\n\\begin{code}\ndata S\u00b9 : Type\u2080 where\n base : S\u00b9\n loop : base \u2261 base\n\\end{code}\n%<\/circle-def>\n","avg_line_length":14.6,"max_line_length":32,"alphanum_fraction":0.6575342466} +{"size":20882,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"%format Zero = \"\\D{Zero}\"\n%format One = \"\\D{One}\"\n%format Two = \"\\D{Two}\"\n%format == = \"\\D{\\texttt{==}}\"\n%format _==_ = \"\\_\" == \"\\_\"\n%format refl = \"\\C{refl}\"\n%format Set = \"\\D{Set}\"\n\n\\chapter{|Two|, |One|, |Zero|, Blast Off!}\n\n%if False\n\\begin{code}\nmodule BlastOff where\n\ndata _==_ {l}{X : Set l}(x : X) : X -> Set l where\n refl : x == x\ninfix 1 _==_\n{-# BUILTIN EQUALITY _==_ #-}\n{-# BUILTIN REFL refl #-}\n\\end{code}\n%endif\n\n\nThe repository file {\\tt CS410-Prelude.agda} contains quite a lot of\nstuff that we'll need as we go along. Let us visit it selectively.\nIn this chapter, we'll look at the types |Two|, |One| and |Zero|,\nwhich are each named after the number of values they contain.\n\n\n\\section{|Two|, the type of Boolean values}\n\nThe type |Two| represents a choice between exactly two things, i.e.,\none bit of information.\nIt is declared as follows.\\nudge{Agda's |:| means\n`is in', just like Haskell's |::|, but shorter, except that space\naround |:| is essential.}\n%format tt = \"\\C{tt}\"\n%format ff = \"\\C{ff}\"\n\\begin{code}\ndata Two : Set where\n tt : Two\n ff : Two\n\\end{code}\n\nIn Agda, we declare a data type with the keyword `|data|', followed by\nan assertion which brings a \\emph{type constructor} into existence,\nin this case, |Two|. Specifically, we say |Two : Set| to mean `|Two|\nis in |Set|'. |Set|\\nudge{|Set| is not an ordinary type. |Set| also has a type.}\nis a built-in type in Agda: it is the type of\n`ordinary' types.\nThe keyword `|where|' introduces an indented\nblock of further declarations, this time of \\emph{data constructors},\nexplaining which values exist in |Two|, namely |tt| and |ff|.\nIn Haskell, this type is called Bool and has values True and False. I call\nthe type |Two| to remind you how big it is, and I use ancient abbreviations\nfor the constructors. We can see, clearly stated, what each of the new things\nis called and what types they have.\n\nWe can give more than one name the same type by listing them left of |:|,\nseparated by spaces, so the whole declaration can fit on one line:\n\\begin{spec}\ndata Two : Set where tt ff : Two\n\\end{spec}\n\nAgda's cunning `mixfix' syntax is not just for binary operators: it lets you\nrebuild familiar notations. We can write\n%format if = \"\\F{if}\"\n%format then = \"\\F{then}\"\n%format else = \"\\F{else}\"\n%format if_then_else_ = if _ then _ else _\n\\begin{spec}\nif_then_else_ : {X : Set} -> Two -> X -> X -> X\nif b then t else f = ?\n\\end{spec}\nWhat have we done? We have declared the type and layout of the\nif-then-else construct, then given an incomplete definition of it. As\nin Haskell, |->| associates to the right. Correspondingly, the type\nsays that we expect to receive four inputs, an invisible |Set| called\n|X|, a visible element of |Two|, then two visible elements of |X|,\nbefore returning a value of type |X|. The invisibility of |X| is\nindicated by the \\emph{braces} in the type.\nThe underscores in the name of the operator show us the places where\nthe \\emph{visible} arguments go, and sure enough, there are three of\nthem in our incomplete definition. Naming |X| allows us to\nrefer to |X| later in the type: we say that the rest of the type\n\\emph{depends} on |X|. We could have named everything, writing\n\\begin{spec}\nif_then_else_ : {X : Set} -> (b : Two) -> (t f : X) -> X\n\\end{spec}\nbut it is often tidier to name only what is depended on.\n\n\nNow hit {\\tt C-c C-l} to load the file.\nWe see that |?| turns into a pair of\nhighlighted braces (a `hole'\\nudge{Sometimes I call a hole a `shed' because\nit's a private workspace.}) labelled 0,\n\\begin{spec}\nif b then t else f = (HOLE 0)\n\\end{spec}\nand that the other window shows the \\emph{goal}.\n\\[\n |?0 : .X|\n\\]\nIt's telling us that we must explain how to fill the braces with a value\nof type |X|: the \\emph{dot} is to remind us that |X| is not in scope. If you\nclick between the braces and hit {\\tt C-c C-comma}, you will get information specific\nto that goal:\n\\[\\begin{array}{l@@{\\;:\\;}l}\n\\multicolumn{2}{l}{\\mbox{Goal: |.X|}}\\\\\n\\hline\n|f| & |.X|\\\\\n|t| & |.X|\\\\\n|b| & |Two|\\\\\n|.X| & |Set|\\\\\n\\end{array}\\]\nWe see the \\emph{goal} above the line, and the \\emph{context} below. The variables\nin scope, |b|, |f| and |t|, are given their types in the context. Meanwhile, we cannot\nsee |X| on the left, so it is not in scope, hence its dot, but it is in the context,\nso types can refer to it. That tallies with the visibility information we gave in the\ntype.\n\n%format MINUSL = \"\\mbox{\\tt -l}\"\n%format MINUSS = \"\\mbox{\\tt -s}\"\n%format MINUSC = \"\\mbox{\\tt -c}\"\nSo, we need a value of type |X| and we have two to choose from: |t| and |f|. We can\njust guess. Try typing |t| in the hole\n\\begin{spec}\nif b then t else f = (HOLEC t 0)\n\\end{spec}\nand hit {\\tt C-c C-space} to say `give the answer'. We get a completed program\n\\begin{spec}\nif b then t else f = t\n\\end{spec}\nbut it probably doesn't do what we expect of if-then-else. Retreat by turning\nthat answer back into |?| and let's think again.\n\nThe role of a value in |Two| is to inform choices, so let us consider the\nvalue of |b| case by case. To inspect |b|, type |b| in the goal\n\\begin{spec}\nif b then t else f = (HOLEC b 0)\n\\end{spec}\nand hit {\\tt C-c C-c}.\nNow we have\n\\begin{spec}\nif tt then t else f = (HOLE 0)\nif ff then t else f = (HOLE 1)\n\\end{spec}\nOne line has become two, and in each, |b| has been replaced by one of its possible values.\nIf you click in the lower hole, you can try another trick: hit {\\tt C-c C-a}. Suddenly,\nAgda writes some code for you.\n\\begin{spec}\nif tt then t else f = (HOLE 0)\nif ff then t else f = f\n\\end{spec}\nThat keystroke is a bit like `I feel lucky' on Google: it gives you the first well typed\nthing the system can find. Sadly, if you do the same thing in the top hole, you also get |f|.\nFortunately, the {\\tt C-c C-a} technology can be persuaded to work a little\nharder~(\\cite{DBLP:conf\/types\/LindbladB04}). Try\ntyping {\\tt -l}\\nudge{{\\tt l} for `list'} in the hole\n\\begin{spec}\nif tt then t else f = (HOLEC (MINUSL) 0)\n\\end{spec}\nand hitting {\\tt C-c C-a}. The other window shows a list of solutions. Now, if you\ntype {\\tt -s1}\\nudge{{\\tt s} for `skip', 1 for how many to skip} in the hole\n\\begin{spec}\nif tt then t else f = (HOLEC (MINUSS 1) 0)\n\\end{spec}\nand hit {\\tt C-c C-a}, you get the finished definition:\n\\begin{code}\nif_then_else_ : {X : Set} -> Two -> X -> X -> X\nif tt then t else f = t\nif ff then t else f = f\n\\end{code}\n\nOf course, you could have given the answer directly, just by typing\n\\begin{spec}\nif tt then t else f = (HOLEC (t) 0)\n\\end{spec}\nand hitting {\\tt C-c C-space}, but in more\ncomplex situations, type-based search can really cut down effort. Moreover,\nthe more precise a type, the more likely it is that type-based search will\nchoose good solutions.\n\nLet's see that for real, by defining the \\emph{case analysis} principle\nfor |Two|.\n%format caseTwo = \"\\F{caseTwo}\"\n\\begin{spec}\ncaseTwo : {P : Two -> Set} -> P tt -> P ff -> (b : Two) -> P b\ncaseTwo t f b = (HOLE 0)\n\\end{spec}\nThe invisible argument is a \\emph{function}, |P|, from |Two| to |Set|, which means that\neach of |P tt| and |P ff| is a |Set|, so they can be used as the types of the visible\narguments |t| and |f|. The rest of the function type says that whichever |b : Two| we\nget, we can deliver a value in |P b|. Now, if we try\n\\begin{spec}\ncaseTwo t f b = (HOLEC (t) 0)\n\\end{spec}\nand {\\tt C-c C-space}, it does not work! To give |t|, we would need to know that |b|\nis |tt|, and we do not. It seems there is no choice but to look at |b|, and no choice\nabout the solutions in each case when we do. So, try typing {\\tt -c}\\nudge{{\\tt c} for `case analysis} in the hole\n\\begin{spec}\ncaseTwo t f b = (HOLEC (MINUSC) 0)\n\\end{spec}\nand hit {\\tt C-c C-a}. Literally, Agda writes the program at a stroke.\n\\begin{code}\ncaseTwo : {P : Two -> Set} -> P tt -> P ff -> (b : Two) -> P b\ncaseTwo t f tt = t\ncaseTwo t f ff = f\n\\end{code}\n\nThink for a moment about what just happened. The type of |if_then_else_|\ndid not distinguish the type of `what to do with |tt|' from `what to do\nwith |ff|', but the whole point of |tt| and |ff| is to be different from\neach other. Moreover, the types of the branches are the same as the types\nof the whole application, when the point of a conditional construct is\nto learn something useful. It is a type preserving transformation to swap over\nthe `then' and `else' branches of every conditional in a program, or to\nreplace the whole conditional by one of its branches: type preserving but\nmeaning destroying! By contrast, the type of |caseTwo| says `if we have a\nproblem involving some |b : Two|, it is enough to consider two special\ncases where \\emph{we know what |b| is}'.\n\n\n\\section{|One|, the dullest type in the universe}\n\nThe type which Haskell calls |()| is what we will call |One|. Let us declare it:\n%format <> = \"\\C{\\langle\\rangle}\"\n%format One = \"\\D{One}\"\n\\begin{code}\nrecord One : Set where\n constructor <>\n\\end{code}\n\nAgda has a special syntax to declare a data type with \\emph{exactly one} constructor:\nit is not compulsory to name the |constructor|, but here I have done so.\nWe think of such a type as a |record|. |One| is the degenerate case of records where\nthere are \\emph{no fields}. We shall have more interesting records later. The syntax\n|record {}| is also permitted for the value in |One|.\n\nNow, you might well be wondering why we don't use this variant:\n%format OneD = \"\\D{OneD}\"\n\\begin{code}\ndata OneD : Set where <> : OneD\n\\end{code}\nand the reason is a little bit subtle.\n\nWe are permitted to write\n%format caseOne = \"\\F{caseOne}\"\n\\begin{code}\ncaseOne : {P : One -> Set} -> P <> -> (x : One) -> P x\ncaseOne p x = p\n\\end{code}\nbut it is forbidden to write\n%format caseOneD = \"\\F{caseOneD}\"\n\\begin{spec}\ncaseOneD : {P : OneD -> Set} -> P <> -> (x : OneD) -> P x\ncaseOneD p x = p -- error |<>| != |x| of type |OneD|\n\\end{spec}\nWhen the typechecker tests equality between expressions in a |data| type, it compares\ntheir normal forms: above in |OneD|, both |<>| and |x| cannot compute any further, and\nthey are different, so we have a mismatch. But when the typechecker tests equality\nbetween expressions in a |record| type, it compares the normal forms of each field\nseparately: |One| has no fields, so all the fields match! That is, we choose a |record|\ntype over a |data| type whenever we can, because the typechecker is more generous when\ntesting that records match. It is essential that |Two| is a |data| type, because the whole\npoint is that its values may vary, but for |One|, we can treat all \\emph{expressions} as\nequal because we know there is only one \\emph{value} they can take.\n\n\n\\section{|Zero|, the type of the impossible}\n\n%format Zero = \"\\D{Zero}\"\nYou have seen a |record| type with no fields, where\nit is easy to give a value to each field: your work is over as soon as\nit starts. You might wonder whether it makes sense to define a |data|\ntype with no constructors, like this:\n\\begin{code}\ndata Zero : Set where\n\\end{code}\nThat definition is accepted, provided you remember to write the `|where|',\neven though you put nothing there. We have given no constructors, so it is\n\\emph{impossible} to construct a value in |Zero|. Why is this useful?\n\nIf you're given a value in the |Zero| type, you are a very lucky person.\nYou can trade it in for a value in any type you want. Let's try it.\n%format magic = \"\\F{magic}\"\n\\begin{spec}\nmagic : {X : Set} -> Zero -> X\nmagic z = (HOLE 0)\n\\end{spec}\nNow what happens if we do case analysis on |z|. Type |z| in the hole\n\\begin{spec}\nmagic z = (HOLEC (z) 0)\n\\end{spec}\nand hit {\\tt C-c C-c}. When we did case analysis on |Two|, one line turned\ninto two, so we might perhaps expect one line to vanish, but that would\nmake the program invisible. What actually happens is this:\\nudge{In Haskell,\n() is the one value in the one-valued type, also (). Agda's |()| is rather the opposite.}\n\\begin{code}\nmagic : {X : Set} -> Zero -> X\nmagic ()\n\\end{code}\nThe program has no right-hand side! Instead, the symbol |()|, which you can\npronounce `impossible'\\nudge{Say `impossible' or blow a raspberry!}, points out why there is \\emph{no problem to solve}.\nInstead of explaining how to make an output, we explain why the function will\nnever need to: nobody can produce its input!\n\nYou will find |Zero| useful in Exercise 1, to get out of tricky situations\nunscathed. You just say `This can't be happening to me!', and all of a sudden,\nit isn't.\n\n\\textbf{Rant} ~ If the typechecker can identify every expression in |One| because there's\nonly one thing they can be, can it identify every expression in |Zero|? Well, not if |Zero|\nis treated like an ordinary |data| type. However, it is certainly possible to construct\ntype systems with an empty type whose inhabiting expressions are all\nequal~(\\cite{DBLP:conf\/sfp\/ChapmanAM05}). Sadly, Agda does not offer this feature.\n\n\n\\section{Equality and unit testing}\n\nWhen you have two expressions, |a : X| and |b : X| in the same type,\nyou can form the type |a == b| of \\emph{evidence that |a| and |b| are\nthe same}. We'll see its declaration later, but we can start using it\nbefore then. For the time being, the key is that |a == b| has \\emph{at\nmost one} constructor.\n\nIf the typechecker can see why |a| and |b| are\nthe same, then |refl| is the constructor.\n\n%format testIf = \"\\F{testIf}\"\n\\begin{code}\ntestIf : if tt then ff else tt == ff\ntestIf = refl\n\\end{code}\nComputing by the rules we have given,\nthe typechecker gets |ff| for both sides of the equation, so |refl| is\naccepted as a value of the given equality type---a \\emph{proof} of the\nequation. This method allows us to embed unit tests directly into our\ncode. The unit tests must pass for the code to typecheck.\n\nIf the typechecker can see why\n|a| and |b| are definitely different, then there is definitely no\nconstructor, so we can use `impossible'.\n%format trueIsn'tFalse = \"\\F{trueIsn'tFalse}\"\n\\begin{code}\ntrueIsn'tFalse : tt == ff -> Zero\ntrueIsn'tFalse ()\n\\end{code}\n\nOf course, some equations might be too weird for the typechecker either\nto rule them in or to rule them out. Fortunately, we can write programs\nwhich compute evidence, just as we can write programs which compute\nvalues. E.g., we might like to check that\n\\[\n |if b then tt else ff == b|\n\\]\nbut the pattern matching rules we gave for |if_then_else_| don't make that\nso, just by computing. Correspondingly, we can try to implement this:\n%format ifTrueFalse = \"\\F{ifTrueFalse}\"\n\\begin{spec}\nifTrueFalse : (b : Two) -> if b then tt else ff == b\nifTrueFalse b = (HOLEC (refl) 0)\n\\end{spec}\nbut |refl| will not typecheck. Instead, however, we can use case analysis\nto split |b| into its two possibilities. Once we know |b|'s \\emph{value}\nin each case, |if_then_else_| computes and we can complete the proof.\n\\begin{code}\nifTrueFalse : (b : Two) -> if b then tt else ff == b\nifTrueFalse tt = refl\nifTrueFalse ff = refl\n\\end{code}\n\nProving things and functional programming turn out to be remarkably similar!\n\n\n\\section{|Two| |with| a view}\n\n%format not = \"\\F{not}\"\n\nLet us finish this section by introducing a key Agda construct, `|with|', and\nits typical use in dependently typed programming---the construction of a\n`view'~(\\cite{DBLP:journals\/jfp\/McBrideM04}).\n\nTwo bits are either the \\emph{same} or \\emph{different}: sometimes, that\ndistinction is more important than whether either is |tt| or |ff|.\nIn more detail, given some |b : Two|, any other |x : Two| is either |b| or\n|not b|, where |not| is given as follows:\n\\begin{code}\nnot : Two -> Two\nnot tt = ff\nnot ff = tt\n\\end{code}\n\nWe could write an equality testing function of type |Two -> Two -> Two|\nand apply it to |b| and |x|, but all that does is compute a \\emph{meaningless}\nbit. The output type |Two| doesn't say what the bit it about. \\emph{A Boolean\nis a bit uninformative.}\n\nThe `view' method gives us a way to generate an \\emph{informative} bit, making\nessential use of the way types can talk about values. A type depending on\n|b| and |x| can contain values which teach us about |b| and |x|. Moreover, a\n`view' is a way of seeing: pattern matching will let us \\emph{see} whether\n|x| is |tt| or |ff|, but we can similarly learn to \\emph{see} whether |x| is\n|b| or |not b|. The first step is to say what we would like to be able to\nsee.\n\n%format TwoTestable = \"\\D{TwoTestable}\"\n%format same = \"\\C{same}\"\n%format diff = \"\\C{diff}\"\n\\begin{code}\ndata TwoTestable (b : Two) : (x : Two) -> Set where\n same : TwoTestable b b\n diff : TwoTestable b (not b)\n\\end{code}\n\nWe have some new syntax here. The declaration of |TwoTestable| has |(b : Two)|\nleft of the |:|, which means |b| scopes over not only the type to the right\nof |:|, so\n\\[\n |TwoTestable : (b : Two)(x : Two) -> Set| \\qquad \\mbox{i.e.,} \\qquad\n |TwoTestable : Two -> Two -> Set|\n\\]\nbut also over the whole declaration, and you can see that |b| has been\nused in the types of |same| and |diff|. In effect, we are giving the constructors\nfor |TwoTestable b|, and |b| must be the first argument of |TwoTestable| in each\nconstructor's return type. Meanwhile, right of the |:|, we have not |Set| but |Two -> Set|,\nmeaning that we are giving a collection of sets indexed over an element |x : Two|.\nThe |x| doesn \\emph{not} scope over the rest of the declaration, and we are free\nto choose specific values for it in the return type of each constructor. So what we\nare saying is that the constructor |same| is available when |x = b| and the constructor\n|diff| is available when |x = not b|. That is, if we have some value |v : TwoTestable b x|,\nwe can find out whether |x| is |b| or |not b| by testing whether |v| is |same| or |diff|:\nwe can implement a nonstandard pattern match by turning into a pattern match on something\nelse, with a dependent type.\n\n%format twoTest = \"\\F{twoTest}\"\nWe have said how we wish to see |x|, but we have not yet shown that wish can come true.\nFor that, we must prove that for every |b| and |x|, we can construct the value in\n|TwoTestable b x| that will allow us to see |x| in terms of |b|. We need a function\n\\begin{spec}\ntwoTest : (b x : Two) -> TwoTestable b x\ntwoTest b x = (HOLEC (MINUSC) 0)\n\\end{spec}\nbut we can ask Agda to write it for us with |MINUS C| and {\\tt C-c C-a}. There is only\none way it can possibly work.\n\\begin{code}\ntwoTest : (b x : Two) -> TwoTestable b x\ntwoTest tt tt = same\ntwoTest tt ff = diff\ntwoTest ff tt = diff\ntwoTest ff ff = same\n\\end{code}\n\n%format xor = \"\\F{xor}\"\nNow that we've established our `view', how do we deploy it? Suppose, e.g., that\nwe want to implement the |xor| function\n\\begin{spec}\nxor : Two -> Two -> Two\nxor b c = (HOLE 0)\n\\end{spec}\nby seeing whether |c| is the same as |b|. We need some extra information, namely\nthe result of |twoTest b c|. Here's how to get it. Click just left of |=| and make\nthis insertion and reload the file.\n\\begin{spec}\nxor : Two -> Two -> Two\nxor b c with twoTest b c\n... | v = (HOLE 0)\n\\end{spec}\nThe |with| keyword introduces the extra information we want, and on the next line,\n|...| means `same left-hand side as before', but the vertical bar adds an extra\ncolumn to the pattern match, with a new variable, |v|\\nudge{Choosing the name |v|\nis not compulsory.} standing for the value of the\nextra information. \nIf you click in the hole and do {\\tt C-c C-comma}, you will see that we know more than\nwe did.\n\\[\\begin{array}{l@@{\\;:\\;}l}\n\\multicolumn{2}{l}{\\mbox{Goal: |Two|}}\\\\\n\\hline\n|v| & |TwoTestable b c|\\\\\n|c| & |Two|\\\\\n|b| & |Two|\\\\\n\\end{array}\\]\nNow pattern match on |v|, using {\\tt C-c C-c},\n\\begin{spec}\nxor : Two -> Two -> Two\nxor b c with twoTest b c\nxor b .b | same = (HOLE 0)\nxor b .(not b) | diff = (HOLE 1)\n\\end{spec}\nand you will see the whole picture. Not only do we get patterns of |same| and |diff|\nfor |v|, but at the same time, we learn what |c| is. The \\emph{dotted patterns}\nare a thing you don't get in Haskell: they say `I don't need to match here to tell\nyou what this is!'. Operationally, the actual matching is done on the output of\n|twoTest b c|: learning whether |c| is |b| or |not b| is the bonus we paid for\nwhen we established the view. We can finish the job directly.\n\\begin{code}\nxor : Two -> Two -> Two\nxor b c with twoTest b c\nxor b .b | same = ff\nxor b .(not b) | diff = tt\n\\end{code}\nLook at the undotted parts of the pattern: they are just given by constructors and\nvariables used at most once, like pattern matching in Haskell. We have defined\nfunctions and repeated uses of variables only under dot. Operationally, dotted patterns\ntreated as `don't care' patterns, |_|. There is nothing new here about how pattern\nmatching programs compute. What's new is what pattern matching can \\emph{mean}.\n\n%format majority = \"\\F{majority}\"\n\\begin{puzz}[|majority|]\nDefine the function which computes which value occurs the more often in three\nbits. Use one |with|, one pattern match, and only variables right of |=|.\n\\begin{spec}\nmajority : Two -> Two -> Two -> Two\nmajority a b c = (HOLE 0)\n\\end{spec}\n\\end{puzz}\n","avg_line_length":40.3907156673,"max_line_length":120,"alphanum_fraction":0.7021836989} +{"size":4754,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule Scoped.Extrication where\n\\end{code}\n\n\\begin{code}\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin\nopen import Data.Vec\nopen import Function using (_\u2218_)\nopen import Data.Sum using (inj\u2081;inj\u2082)\nopen import Data.Product renaming (_,_ to _,,_)\n\nopen import Type\nopen import Type.BetaNormal\nopen import Algorithmic as A\nopen import Scoped\nopen import Builtin\nopen import Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con \nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con as B\nopen import Type.BetaNormal\nopen import Type.RenamingSubstitution as T\n\\end{code}\n\ntype level\n\n\\begin{code}\nlen\u22c6 : Ctx\u22c6 \u2192 \u2115\nlen\u22c6 \u2205 = zero\nlen\u22c6 (\u0393 ,\u22c6 K) = suc (len\u22c6 \u0393)\n\nextricateVar\u22c6 : \u2200{\u0393 K}(A : \u0393 \u220b\u22c6 K) \u2192 Fin (len\u22c6 \u0393)\nextricateVar\u22c6 Z = zero\nextricateVar\u22c6 (S \u03b1) = suc (extricateVar\u22c6 \u03b1)\n\nextricateNf\u22c6 : \u2200{\u0393 K}(A : \u0393 \u22a2Nf\u22c6 K) \u2192 ScopedTy (len\u22c6 \u0393)\nextricateNe\u22c6 : \u2200{\u0393 K}(A : \u0393 \u22a2Ne\u22c6 K) \u2192 ScopedTy (len\u22c6 \u0393)\n\n-- intrinsically typed terms should also carry user chosen names as\n-- instructions to the pretty printer\n\nextricateNf\u22c6 (\u03a0 {K = K} A) = \u03a0 K (extricateNf\u22c6 A)\nextricateNf\u22c6 (A \u21d2 B) = extricateNf\u22c6 A \u21d2 extricateNf\u22c6 B\nextricateNf\u22c6 (\u019b {K = K} A) = \u019b K (extricateNf\u22c6 A)\nextricateNf\u22c6 (ne n) = extricateNe\u22c6 n\nextricateNf\u22c6 (con c) = con c\n\nextricateNe\u22c6 (` \u03b1) = ` (extricateVar\u22c6 \u03b1)\nextricateNe\u22c6 (n \u00b7 n') = extricateNe\u22c6 n \u00b7 extricateNf\u22c6 n'\n-- ((K \u21d2 *) \u21d2 K \u21d2 *) \u21d2 K \u21d2 *\nextricateNe\u22c6 (\u03bc1 {K = K}) = \u019b \n ((K \u21d2 *) \u21d2 K \u21d2 *)\n (\u019b (K) (\u03bc (` (suc zero)) (` zero)))\n\\end{code}\n\n\n\\begin{code}\nlen : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 Weird\u2115 (len\u22c6 \u03a6)\nlen \u2205 = Z\nlen (\u0393 ,\u22c6 K) = T (len \u0393)\nlen (\u0393 , A) = S (len \u0393)\n\nopen import Relation.Binary.PropositionalEquality as Eq\n\nextricateVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u220b A \u2192 WeirdFin (len \u0393)\nextricateVar Z = Z\nextricateVar (S x) = S (extricateVar x)\nextricateVar (T x) = T (extricateVar x)\n\nextricateC : \u2200{\u0393}{A : \u0393 \u22a2Nf\u22c6 *} \u2192 B.TermCon A \u2192 Scoped.TermCon\nextricateC (integer i) = integer i\nextricateC (bytestring b) = bytestring b\nextricateC (string s) = string s\nextricateC (bool b) = bool b\nextricateC (char c) = char c\nextricateC unit = unit\n\nopen import Data.Product as P\nopen import Function hiding (_\u220b_)\n\nextricateSub : \u2200 {\u0393 \u0394} \u2192 (\u2200 {J} \u2192 \u0394 \u220b\u22c6 J \u2192 \u0393 \u22a2Nf\u22c6 J)\n \u2192 Scoped.Tel\u22c6 (len\u22c6 \u0393) (len\u22c6 \u0394)\nextricateSub {\u0394 = \u2205} \u03c3 = []\nextricateSub {\u0393}{\u0394 ,\u22c6 K} \u03c3 =\n Eq.subst (Scoped.Tel\u22c6 (len\u22c6 \u0393))\n (+-comm (len\u22c6 \u0394) 1)\n (extricateSub {\u0394 = \u0394} (\u03c3 \u2218 S) ++ Data.Vec.[ extricateNf\u22c6 (\u03c3 Z) ]) \n\nopen import Data.List\n\nlemma\u22c6 : \u2200 b \u2192 len\u22c6 (proj\u2081 (SIG b)) \u2261 arity\u22c6 b\nlemma\u22c6 addInteger = refl\nlemma\u22c6 subtractInteger = refl\nlemma\u22c6 multiplyInteger = refl\nlemma\u22c6 divideInteger = refl\nlemma\u22c6 quotientInteger = refl\nlemma\u22c6 remainderInteger = refl\nlemma\u22c6 modInteger = refl\nlemma\u22c6 lessThanInteger = refl\nlemma\u22c6 lessThanEqualsInteger = refl\nlemma\u22c6 greaterThanInteger = refl\nlemma\u22c6 greaterThanEqualsInteger = refl\nlemma\u22c6 equalsInteger = refl\nlemma\u22c6 concatenate = refl\nlemma\u22c6 takeByteString = refl\nlemma\u22c6 dropByteString = refl\nlemma\u22c6 sha2-256 = refl\nlemma\u22c6 sha3-256 = refl\nlemma\u22c6 verifySignature = refl\nlemma\u22c6 equalsByteString = refl\nlemma\u22c6 ifThenElse = refl\n\nlemma : \u2200 b \u2192 Data.List.length (proj\u2081 (proj\u2082 (SIG b))) \u2261 arity b\nlemma addInteger = refl\nlemma subtractInteger = refl\nlemma multiplyInteger = refl\nlemma divideInteger = refl\nlemma quotientInteger = refl\nlemma remainderInteger = refl\nlemma modInteger = refl\nlemma lessThanInteger = refl\nlemma lessThanEqualsInteger = refl\nlemma greaterThanInteger = refl\nlemma greaterThanEqualsInteger = refl\nlemma equalsInteger = refl\nlemma concatenate = refl\nlemma takeByteString = refl\nlemma dropByteString = refl\nlemma sha2-256 = refl\nlemma sha3-256 = refl\nlemma verifySignature = refl\nlemma equalsByteString = refl\nlemma ifThenElse = refl\n\n\u22612\u2264\u2034 : \u2200{m n} \u2192 m \u2261 n \u2192 m \u2264\u2034 n\n\u22612\u2264\u2034 refl = \u2264\u2034-refl\n\nextricateTel : \u2200 {\u03a6 \u0393 \u0394}(\u03c3 : \u2200 {J} \u2192 \u0394 \u220b\u22c6 J \u2192 \u03a6 \u22a2Nf\u22c6 J)(As : List (\u0394 \u22a2Nf\u22c6 *))\n \u2192 A.Tel \u0393 \u0394 \u03c3 As\n \u2192 Vec (ScopedTm (len \u0393)) (Data.List.length As)\n\nextricate : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 ScopedTm (len \u0393)\n\nextricateTel \u03c3 [] x = []\nextricateTel \u03c3 (A \u2237 As) (t \u2237 ts) = extricate t \u2237 extricateTel \u03c3 As ts\n\nextricate (` x) = ` (extricateVar x)\nextricate {\u03a6}{\u0393} (\u019b {A = A} t) = \u019b (extricateNf\u22c6 A) (extricate t)\nextricate (t \u00b7 u) = extricate t \u00b7 extricate u\nextricate (\u039b {K = K} t) = \u039b K (extricate t)\nextricate {\u03a6}{\u0393} (_\u00b7\u22c6_ t A) = extricate t ScopedTm.\u00b7\u22c6 extricateNf\u22c6 A\nextricate {\u03a6}{\u0393} (wrap1 pat arg t) = wrap (extricateNf\u22c6 pat) (extricateNf\u22c6 arg)\n (extricate t)\nextricate (unwrap1 t) = unwrap (extricate t)\nextricate (con c) = con (extricateC c)\nextricate {\u03a6}{\u0393} (builtin b \u03c3 ts) =\n builtin\n b\n (inj\u2082 ((lemma\u22c6 b) ,, (\u22612\u2264\u2034 (lemma b))))\n (extricateSub \u03c3)\n (extricateTel \u03c3 _ ts)\nextricate {\u03a6}{\u0393} (error A) = error (extricateNf\u22c6 A)\n\\end{code}\n","avg_line_length":28.987804878,"max_line_length":79,"alphanum_fraction":0.6573411864} +{"size":20130,"ext":"lagda","lang":"Literate Agda","max_stars_count":1299.0,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\nmodule Main where\nopen import Agda.Builtin.IO\nimport IO.Primitive as IO using (return;_>>=_)\nopen import Agda.Builtin.Unit\nopen import Agda.Builtin.String\nopen import Function\nopen import Data.Sum\nopen import Data.String\nopen import Agda.Builtin.TrustMe\nopen import Relation.Binary.PropositionalEquality\nopen import Agda.Builtin.Nat\nopen import Data.Nat\nopen import Agda.Builtin.Int\nopen import Data.Integer\nopen import Data.Integer.Show\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Bool\nopen import Data.Fin\nopen import Data.Vec hiding (_>>=_;_++_)\nopen import Data.List hiding (_++_)\nimport Debug.Trace as D\n\nopen import Type\nopen import Builtin\nopen import Check\nopen import Scoped.Extrication\nopen import Type.BetaNBE\nopen import Type.BetaNormal\nopen import Untyped as U\nimport Untyped.Reduction as U\nimport Scoped as S\nopen import Raw\nopen import Scoped\nopen import Utils hiding (ByteString)\nopen import Untyped\nopen import Algorithmic hiding (Term;Type)\nopen import Algorithmic.ReductionEC\nopen import Algorithmic.Reduction\nopen import Algorithmic.CK\nopen import Algorithmic.CEKV\nopen import Algorithmic.Erasure\n\n-- There's a long prelude here that could go in a different file but\n-- currently it's only used here\n\n-- Text Stuff\n\npostulate\n putStrLn : String \u2192 IO \u22a4\n\n{-# FOREIGN GHC import qualified Data.Text as T #-}\n{-# FOREIGN GHC import qualified Data.Text.IO as TextIO #-}\n{-# COMPILE GHC putStrLn = TextIO.putStrLn #-}\n\n-- IO Stuff\n\ninstance\n IOMonad : Monad IO\n IOMonad = record { return = IO.return; _>>=_ = IO._>>=_ }\n\n-- Bytestring stuff\n\npostulate\n ByteString : Set\n getContents : IO ByteString\n readFile : String \u2192 IO ByteString\n\n{-# FOREIGN GHC import qualified Data.ByteString.Lazy as BSL #-}\n{-# COMPILE GHC ByteString = type BSL.ByteString #-}\n{-# COMPILE GHC readFile = \\ s -> BSL.readFile (T.unpack s) #-}\n{-# COMPILE GHC getContents = BSL.getContents #-}\n\n-- System.Exit stuff\n\npostulate\n exitFailure : IO \u22a4\n exitSuccess : IO \u22a4\n\n{-# FOREIGN GHC import System.Exit #-}\n{-# COMPILE GHC exitSuccess = exitSuccess #-}\n{-# COMPILE GHC exitFailure = exitFailure #-}\n\n-- System.Environment stuff\n\npostulate\n getArgs : IO (List String)\n\n{-# FOREIGN GHC import System.Environment #-}\n{-# COMPILE GHC getArgs = (fmap . fmap) T.pack $ getArgs #-}\n\n-- Misc stuff\n\n{-# FOREIGN GHC import Data.Either #-}\n{-# FOREIGN GHC import Control.Monad.Trans.Except #-}\n\npostulate\n TermN : Set -- term with names\n Term : Set -- DeBruijn term\n TypeN : Set\n Type : Set\n ProgramN : Set\n Program : Set\n convTm : Term \u2192 RawTm\n convTy : Type \u2192 RawTy\n unconvTy : RawTy \u2192 Type\n unconvTm : RawTm \u2192 Term\n convP : Program \u2192 RawTm\n ParseError : Set\n parse : ByteString \u2192 Either ParseError ProgramN\n parseTm : ByteString \u2192 Either ParseError TermN\n parseTy : ByteString \u2192 Either ParseError TypeN\n showTerm : RawTm \u2192 String\n deBruijnify : ProgramN \u2192 Either FreeVariableError Program\n deBruijnifyTm : TermN \u2192 Either FreeVariableError Term\n deBruijnifyTy : TypeN \u2192 Either FreeVariableError Type\n\n ProgramNU : Set\n ProgramU : Set\n TermNU : Set\n TermU : Set\n deBruijnifyU : ProgramNU \u2192 Either FreeVariableError ProgramU\n deBruijnifyTmU : TermNU \u2192 Either FreeVariableError TermU\n parseU : ByteString \u2192 Either ParseError ProgramNU\n parseTmU : ByteString \u2192 Either ParseError TermNU\n convPU : ProgramU \u2192 Untyped\n convTmU : TermU \u2192 Untyped\n unconvTmU : Untyped \u2192 TermU\n \n\n{-# FOREIGN GHC import PlutusCore.Name #-}\n{-# FOREIGN GHC import PlutusCore.Lexer #-}\n{-# FOREIGN GHC import PlutusCore.Parser #-}\n{-# FOREIGN GHC import PlutusCore.Pretty #-}\n{-# FOREIGN GHC import PlutusCore.DeBruijn #-}\n{-# FOREIGN GHC import qualified UntypedPlutusCore as U #-}\n{-# FOREIGN GHC import qualified UntypedPlutusCore.Parser as U #-}\n\n{-# FOREIGN GHC import Raw #-}\n{-# COMPILE GHC convP = convP #-}\n{-# COMPILE GHC convPU = U.convP #-}\n{-# COMPILE GHC convTm = conv #-}\n{-# COMPILE GHC convTy = convT #-}\n{-# COMPILE GHC unconvTy = unconvT 0 #-}\n{-# COMPILE GHC unconvTm = unconv 0 #-}\n{-# FOREIGN GHC import Data.Bifunctor #-}\n\n{-# COMPILE GHC ParseError = type ParseError () #-}\n{-# COMPILE GHC parse = first (() <$) . runQuote . runExceptT . parseProgram #-}\n{-# COMPILE GHC parseU = first (() <$) . runQuote . runExceptT . U.parseProgram #-}\n{-# COMPILE GHC parseTm = first (() <$) . runQuote. runExceptT . parseTerm #-}\n{-# COMPILE GHC parseTy = first (() <$) . runQuote . runExceptT . parseType #-}\n{-# COMPILE GHC deBruijnify = second (() <$) . runExcept . deBruijnProgram #-}\n{-# COMPILE GHC deBruijnifyTm = second (() <$) . runExcept . deBruijnTerm #-}\n{-# COMPILE GHC deBruijnifyTy = second (() <$) . runExcept . deBruijnTy #-}\n{-# FOREIGN GHC import PlutusCore #-}\n{-# COMPILE GHC ProgramN = type PlutusCore.Program TyName Name DefaultUni DefaultFun PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Program = type PlutusCore.Program NamedTyDeBruijn NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TermN = type PlutusCore.Term TyName Name DefaultUni DefaultFun PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Term = type PlutusCore.Term NamedTyDeBruijn NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TypeN = type PlutusCore.Type TyName DefaultUni PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC Type = type PlutusCore.Type NamedTyDeBruijn DefaultUni () #-}\n{-# COMPILE GHC showTerm = T.pack . show #-}\n\n{-# COMPILE GHC ProgramNU = type U.Program Name DefaultUni DefaultFun PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC ProgramU = type U.Program NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TermNU = type U.Term Name DefaultUni DefaultFun PlutusCore.Lexer.AlexPosn #-}\n{-# COMPILE GHC TermU = type U.Term NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC deBruijnifyU = second (() <$) . runExcept . U.deBruijnProgram #-}\n{-# COMPILE GHC deBruijnifyTmU = second (() <$) . runExcept . U.deBruijnTerm #-}\n{-# COMPILE GHC parseTmU = first (() <$) . runQuote. runExceptT . U.parseTerm #-}\n{-# COMPILE GHC convTmU = U.conv #-}\n{-# COMPILE GHC unconvTmU = U.uconv 0 #-}\n\npostulate\n prettyPrintTm : RawTm \u2192 String\n prettyPrintTy : RawTy \u2192 String\n\n prettyPrintUTm : Untyped \u2192 String\n\n{-# FOREIGN GHC {-# LANGUAGE TypeApplications #-} #-}\n{-# COMPILE GHC prettyPrintTm = display @T.Text . unconv 0 #-}\n{-# COMPILE GHC prettyPrintTy = display @T.Text . unconvT 0 #-}\n\n{-# FOREIGN GHC import qualified Untyped as U #-}\n{-# COMPILE GHC prettyPrintUTm = display @T.Text . U.uconv 0 #-}\n\ndata EvalMode : Set where\n U TL L TCK CK TCEK : EvalMode\n\n{-# COMPILE GHC EvalMode = data EvalMode (U | TL | L | TCK | CK | TCEK) #-}\n\n-- the Error's returned by `plc-agda` and the haskell interface to `metatheory`.\n\ndata ERROR : Set where\n typeError : String \u2192 ERROR\n parseError : ParseError \u2192 ERROR\n scopeError : ScopeError \u2192 ERROR\n runtimeError : RuntimeError \u2192 ERROR\n\n\nuglyTypeError : TypeError \u2192 String\nuglyTypeError (kindMismatch K K' x) = \"kindMismatch\"\nuglyTypeError (notStar K x) = \"notStar\"\nuglyTypeError (notFunKind K x) = \"NotFunKind\"\nuglyTypeError (notPat K x) = \"notPat\"\nuglyTypeError UnknownType = \"UnknownType\"\nuglyTypeError (notPi A x) = \"notPi\"\nuglyTypeError (notMu A x) = \"notMu\"\nuglyTypeError (notFunType A x) = \"notFunType\"\nuglyTypeError (typeMismatch A A' x) =\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A))\n ++\n \" doesn't match \"\n ++\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A'))\nuglyTypeError builtinError = \"builtinError\"\n\n-- the haskell version of Error is defined in Raw\n{-# FOREIGN GHC import Raw #-}\n\n{-# COMPILE GHC ERROR = data ERROR (TypeError | ParseError | ScopeError | RuntimeError) #-}\n\nparsePLC : ByteString \u2192 Either ERROR (ScopedTm Z)\nparsePLC plc = do\n namedprog \u2190 withE parseError $ parse plc\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnify namedprog\n withE scopeError $ scopeCheckTm {0}{Z} (shifter Z (convP prog))\n -- ^ FIXME: this should have an interface that guarantees that the\n -- shifter is run\n\nparseUPLC : ByteString \u2192 Either ERROR (0 \u22a2)\nparseUPLC plc = do\n namedprog \u2190 withE parseError $ parseU plc\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnifyU namedprog\n withE scopeError $ U.scopeCheckU {0} (convPU prog)\n\ntypeCheckPLC : ScopedTm Z \u2192 Either TypeError (\u03a3 (\u2205 \u22a2Nf\u22c6 *) (\u2205 \u22a2_))\ntypeCheckPLC t = inferType _ t\n\n\nmaxsteps = 10000000000\n\nopen import Data.String\n\nreportError : ERROR \u2192 String\nreportError (parseError _) = \"parseError\"\nreportError (typeError s) = \"typeError: \" ++ s\nreportError (scopeError _) = \"scopeError\"\nreportError (runtimeError gasError) = \"gasError\"\nreportError (runtimeError userError) = \"userError\"\nreportError (runtimeError runtimeTypeError) = \"runtimeTypeError\"\n\n\nexecutePLC : EvalMode \u2192 ScopedTm Z \u2192 Either ERROR String\nexecutePLC U t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n just t' \u2190 withE runtimeError $ U.progressor 10000000 (erase t)\n where nothing \u2192 inj\u2081 (runtimeError userError)\n return $ prettyPrintUTm (extricateU t')\nexecutePLC TL t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n just t' \u2190 withE runtimeError $ Algorithmic.Reduction.progressor maxsteps t\n where nothing \u2192 inj\u2081 (runtimeError userError)\n return (prettyPrintTm (unshifter Z (extricateScope (extricate t'))))\nexecutePLC TCK t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n \u25a1 {t = t} v \u2190 withE runtimeError $ Algorithmic.CK.stepper maxsteps (\u03b5 \u25bb t)\n where \u25c6 _ \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return (prettyPrintTm (unshifter Z (extricateScope (extricate t))))\nexecutePLC TCEKV t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CEKV.stepper maxsteps (\u03b5 \u037e [] \u25bb t)\n where \u25c6 _ \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return (prettyPrintTm (unshifter Z (extricateScope (extricate (Algorithmic.CEKV.discharge V)))))\n\nexecuteUPLC : 0 \u22a2 \u2192 Either ERROR String\nexecuteUPLC t = do\n just t' \u2190 withE runtimeError $ U.progressor 10000000 t\n where nothing \u2192 inj\u2081 (runtimeError userError)\n return $ prettyPrintUTm (extricateU t')\n\nevalByteString : EvalMode \u2192 ByteString \u2192 Either ERROR String\nevalByteString U b = do\n t \u2190 parseUPLC b\n executeUPLC t\nevalByteString m b = do\n{-\n -- some debugging code\n namedprog \u2190 withE parseError $ parse b\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnify namedprog\n let shiftedprog = shifter Z (convP prog)\n scopedprog \u2190 withE scopeError $ scopeCheckTm {0}{Z} shiftedprog\n let extricatedprog = extricateScope scopedprog\n let unshiftedprog = unshifter Z extricatedprog\n return (\"orginal: \" ++ rawPrinter (convP prog) ++ \"\\n\" ++\n \"shifted: \" ++ rawPrinter shiftedprog ++ \"\\n\" ++\n \"instrinsically scoped: \" ++ Scoped.ugly scopedprog ++ \"\\n\" ++\n \"extricated: \" ++ rawPrinter extricatedprog ++ \"\\n\" ++\n \"unshifted: \" ++ rawPrinter unshiftedprog ++ \"\\n\" ++\n \"unconved: \" ++ prettyPrintTm unshiftedprog ++ \"\\n\")\n-}\n t \u2190 parsePLC b\n executePLC m t\n\ntypeCheckByteString : ByteString \u2192 Either ERROR String\ntypeCheckByteString b = do\n t \u2190 parsePLC b\n (A ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e) ) $ typeCheckPLC t\n{-\n -- some debugging code\n let extricatedtype = extricateScopeTy (extricateNf\u22c6 A)\n let unshiftedtype = unshifterTy Z extricatedtype\n return (\"original: \" ++ \"???\" ++ \"\\n\" ++\n \"extricated: \" ++ rawTyPrinter extricatedtype ++ \"\\n\" ++\n \"unshifted: \" ++ rawTyPrinter unshiftedtype ++ \"\\n\" ++\n \"unconved: \" ++ prettyPrintTy unshiftedtype ++ \"\\n\")\n-}\n return (prettyPrintTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 A))))\n\njunk : \u2200{n} \u2192 Vec String n\njunk {zero} = []\njunk {Nat.suc n} = Data.Integer.Show.show (pos n) \u2237 junk\n\nalphaTm : ByteString \u2192 ByteString \u2192 Bool\nalphaTm plc1 plc2 with parseTm plc1 | parseTm plc2\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = decRTm (convTm plc1'') (convTm plc2'')\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaTm plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTm as alphaTm #-}\n\nalphaU : ByteString \u2192 ByteString \u2192 Bool\nalphaU plc1 plc2 with parseTmU plc1 | parseTmU plc2\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTmU plc1' | deBruijnifyTmU plc2'\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = decUTm (convTmU plc1'') (convTmU plc2'')\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaU plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaU as alphaU #-}\n\n\nblah : ByteString \u2192 ByteString \u2192 String\nblah plc1 plc2 with parseTm plc1 | parseTm plc2\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = rawPrinter (convTm plc1'') ++ \" || \" ++ rawPrinter (convTm plc2'')\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = \"deBruijnifying failed\"\nblah plc1 plc2 | _ | _ = \"parsing failed\"\n\n{-# COMPILE GHC blah as blah #-}\n\nprintTy : ByteString \u2192 String\nprintTy b with parseTy b\n... | inj\u2081 _ = \"parseTy error\"\n... | inj\u2082 A with deBruijnifyTy A\n... | inj\u2081 _ = \"deBruinjifyTy error\"\n... | inj\u2082 A' = rawTyPrinter (convTy A')\n\n{-# COMPILE GHC printTy as printTy #-}\n\nalphaTy : ByteString \u2192 ByteString \u2192 Bool\nalphaTy plc1 plc2 with parseTy plc1 | parseTy plc2\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTy plc1' | deBruijnifyTy plc2'\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = decRTy (convTy plc1'') (convTy plc2'')\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaTy plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTy as alphaTy #-}\n\n-- Opt stuff\n\n{-# FOREIGN GHC import Opts #-}\n\ndata Input : Set where\n FileInput : String \u2192 Input\n StdInput : Input\n\n{-# COMPILE GHC Input = data Input (FileInput | StdInput) #-}\n\ndata EvalOptions : Set where\n EvalOpts : Input \u2192 EvalMode \u2192 EvalOptions\n\n{-# COMPILE GHC EvalOptions = data EvalOptions (EvalOpts) #-}\n\ndata TCOptions : Set where\n TCOpts : Input \u2192 TCOptions\n\n{-# COMPILE GHC TCOptions = data TCOptions (TCOpts) #-}\n\ndata Command : Set where\n Evaluate : EvalOptions \u2192 Command\n TypeCheck : TCOptions \u2192 Command\n\n{-# COMPILE GHC Command = data Command (Evaluate | TypeCheck) #-}\n\npostulate execP : IO Command\n\n{-# COMPILE GHC execP = execP #-}\n\nevalInput : EvalMode \u2192 Input \u2192 IO (Either ERROR String)\nevalInput m (FileInput fn) = fmap (evalByteString m) (readFile fn)\nevalInput m StdInput = fmap (evalByteString m) getContents\n\ntcInput : Input \u2192 IO (Either ERROR String)\ntcInput (FileInput fn) = fmap typeCheckByteString (readFile fn)\ntcInput StdInput = fmap typeCheckByteString getContents\n\n\nmain' : Command \u2192 IO \u22a4\nmain' (Evaluate (EvalOpts i m)) = do\n inj\u2082 s \u2190 evalInput m i\n where\n inj\u2081 e \u2192 putStrLn (reportError e) >> exitFailure\n putStrLn s >> exitSuccess\nmain' (TypeCheck (TCOpts i)) = do\n inj\u2082 s \u2190 tcInput i\n where\n inj\u2081 e \u2192 putStrLn (reportError e) >> exitFailure\n putStrLn s >> exitSuccess\n\nmain : IO \u22a4\nmain = execP >>= main'\n\nliftSum : {A : Set} \u2192 Either ERROR A \u2192 Maybe A\nliftSum (inj\u2082 a) = just a\nliftSum (inj\u2081 e) = nothing\n\n\n-- a Haskell interface to the kindchecker:\ncheckKindX : Type \u2192 Kind \u2192 Either ERROR \u22a4\ncheckKindX ty k = do\n ty \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n (k' ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty)\n _ \u2190 withE ((\u03bb e \u2192 ERROR.typeError (uglyTypeError e)) \u2218 kindMismatch _ _) (meqKind k k')\n return tt\n\n{-# COMPILE GHC checkKindX as checkKindAgda #-}\n\n\n-- a Haskell interface to kind inference:\n\ninferKind\u2205 : Type \u2192 Either ERROR Kind\ninferKind\u2205 ty = do\n ty \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n (k ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty)\n return k\n\n{-# COMPILE GHC inferKind\u2205 as inferKindAgda #-}\n\nopen import Type.BetaNormal\n\n-- a Haskell interface to the type normalizer:\nnormalizeType : Type \u2192 Either ERROR Type\nnormalizeType ty = do\n ty' \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n _ ,, n \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty')\n return (unconvTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 n))))\n\n{-# COMPILE GHC normalizeType as normalizeTypeAgda #-}\n\n-- Haskell interface to type checker:\ninferType\u2205 : Term \u2192 Either ERROR Type\ninferType\u2205 t = do\n t' \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, _ \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 t')\n return (unconvTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 ty))))\n\n{-# COMPILE GHC inferType\u2205 as inferTypeAgda #-}\n\n\n-- FIXME: we have a checkType function now...\ncheckType\u2205 : Type \u2192 Term \u2192 Either ERROR \u22a4\ncheckType\u2205 ty t = do\n ty' \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n tyN \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (checkKind \u2205 ty' *)\n t' \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n withE (\u03bb e \u2192 typeError (uglyTypeError e)) (checkType \u2205 t' tyN)\n{-\n tyN' ,, tmC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 t')\n refl \u2190 withE ((\u03bb e \u2192 typeError (uglyTypeError e)) \u2218 kindMismatch _ _) (meqKind k *)\n refl \u2190 withE ((\u03bb e \u2192 typeError (uglyTypeError e)) \u2218 typeMismatch _ _) (meqNfTy tyN tyN')\n-}\n return _\n\n-- Haskell interface to type normalizer (for terms)\n-- the type checker\/inferer could also return such a term\nnormalizeTypeTerm : Term \u2192 Either ERROR Term\nnormalizeTypeTerm t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n _ ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n return (unconvTm (unshifter Z (extricateScope (extricate tC))))\n\n{-# COMPILE GHC normalizeTypeTerm as normalizeTypeTermAgda #-}\n\n\n{-# COMPILE GHC checkType\u2205 as checkTypeAgda #-}\n\n-- Haskell interface to (typechecked and proven correct) reduction\n\nimport Algorithmic.Evaluation as L\n\nrunTL : Term \u2192 Either ERROR Term\nrunTL t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n _ ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n t \u2190 withE runtimeError $ L.stepper tC maxsteps\n return (unconvTm (unshifter Z (extricateScope (extricate t))))\n\n{-# COMPILE GHC runTL as runTLAgda #-}\n\n-- Haskell interface to (typechecked) CK\nrunTCK : Term \u2192 Either ERROR Term\nrunTCK t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CK.stepper maxsteps (\u03b5 \u25bb tC)\n where (_ \u25bb _) \u2192 inj\u2081 (runtimeError gasError)\n (_ \u25c5 _) \u2192 inj\u2081 (runtimeError gasError)\n \u25c6 A \u2192 return (unconvTm (unshifter Z (extricateScope {0}{Z} (extricate (error ty)))))\n return (unconvTm (unshifter Z (extricateScope (extricate (Algorithmic.CK.discharge V)))))\n\n{-# COMPILE GHC runTCK as runTCKAgda #-}\n\n-- Haskell interface to (typechecked) CEKV\nrunTCEK : Term \u2192 Either ERROR Term\nrunTCEK t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CEKV.stepper maxsteps (\u03b5 \u037e [] \u25bb tC)\n where (_ \u037e _ \u25bb _) \u2192 inj\u2081 (runtimeError gasError)\n (_ \u25c5 _) \u2192 inj\u2081 (runtimeError gasError)\n \u25c6 A \u2192 return (unconvTm (unshifter Z (extricateScope {0}{Z} (extricate (error ty)))))\n return (unconvTm (unshifter Z (extricateScope (extricate (Algorithmic.CEKV.discharge V)))))\n\n{-# COMPILE GHC runTCEK as runTCEKAgda #-}\n\npostulate showU : TermU -> String\n\n{-# COMPILE GHC showU = T.pack . show #-}\n\n\nrunU : TermU \u2192 Either ERROR TermU\nrunU t = do\n tDB \u2190 withE scopeError $ U.scopeCheckU {0} (convTmU t)\n just tR \u2190 withE runtimeError $ U.progressor maxsteps tDB\n where nothing \u2192 inj\u2082 (unconvTmU UError)\n return (unconvTmU (extricateU tR))\n\n{-# COMPILE GHC runU as runUAgda #-}\n\\end{code}\n","avg_line_length":36.2702702703,"max_line_length":137,"alphanum_fraction":0.6963735718} +{"size":46,"ext":"lagda","lang":"Literate Agda","max_stars_count":1989.0,"content":"\n\\begin{code}\nmodule Issue97 where\n\\end{code}\n","avg_line_length":9.2,"max_line_length":20,"alphanum_fraction":0.7391304348} +{"size":20210,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Decidable: Booleans and decision procedures\"\nlayout : page\nprev : \/Quantifiers\/\npermalink : \/Decidable\/\nnext : \/Lists\/\n---\n\n\\begin{code}\nmodule plfa.Decidable where\n\\end{code}\n\nWe have a choice as to how to represent relations:\nas an inductive data type of _evidence_ that the relation holds,\nor as a function that _computes_ whether the relation holds.\nHere we explore the relation between these choices.\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Nat using (\u2115; zero; suc)\nopen import Data.Product using (_\u00d7_) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Nullary.Negation using (contraposition)\n renaming (contradiction to \u00ac\u00ac-intro)\nopen import Data.Unit using (\u22a4; tt)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.List using (List; []; _\u2237_; foldr; map)\nopen import Function using (_\u2218_)\nopen import plfa.Relations using (_<_; z\n\nImplication binds less tightly than any other operator. Thus, `A \u228e B \u2192\nB \u228e A` parses as `(A \u228e B) \u2192 (B \u228e A)`.\n\nGiven two types `A` and `B`, we refer to `A \u2192 B` as the *function*\nfrom `A` to `B`. It is also sometimes called the *exponential*,\nwith `B` raised to the `A` power. Among other reasons for calling\nit the exponential, note that if type `A` has `m` distinct\nmemebers, and type `B` has `n` distinct members, then the type\n`A \u2192 B` has `n ^ m` distinct members. For instance, consider a\ntype `Bool` with two members and a type `Tri` with three members,\nas defined earlier. The the type `Bool \u2192 Tri` has nine (that is,\nthree squared) members:\n\n {true \u2192 aa; false \u2192 aa} {true \u2192 aa; false \u2192 bb} {true \u2192 aa; false \u2192 cc}\n {true \u2192 bb; false \u2192 aa} {true \u2192 bb; false \u2192 bb} {true \u2192 bb; false \u2192 cc}\n {true \u2192 cc; false \u2192 aa} {true \u2192 cc; false \u2192 bb} {true \u2192 cc; false \u2192 cc}\n\nFor example, the following function enumerates all possible\narguments of the type `Bool \u2192 Tri`:\n\\begin{code}\n\u2192-count : (Bool \u2192 Tri) \u2192 \u2115\n\u2192-count f with f true | f false\n... | aa | aa = 1\n... | aa | bb = 2 \n... | aa | cc = 3\n... | bb | aa = 4\n... | bb | bb = 5\n... | bb | cc = 6\n... | cc | aa = 7\n... | cc | bb = 8\n... | cc | cc = 9 \n\\end{code}\n\nExponential on types also share a property with exponential on\nnumbers in that many of the standard identities for numbers carry\nover to the types.\n\nCorresponding to the law\n\n (p ^ n) ^ m = p ^ (n * m)\n\nwe have that the types `A \u2192 B \u2192 C` and `A \u00d7 B \u2192 C` are isomorphic.\nBoth types can be viewed as functions that given evidence that `A` holds\nand evidence that `B` holds can return evidence that `C` holds.\nThis isomorphism sometimes goes by the name *currying*.\nThe proof of the right inverse requires extensionality.\n\\begin{code}\npostulate\n extensionality : \u2200 {A B : Set} \u2192 {f g : A \u2192 B} \u2192 (\u2200 (x : A) \u2192 f x \u2261 g x) \u2192 f \u2261 g\n \ncurrying : \u2200 {A B C : Set} \u2192 (A \u2192 B \u2192 C) \u2243 (A \u00d7 B \u2192 C)\ncurrying =\n record\n { to = \u03bb f \u2192 \u03bb { (x , y) \u2192 f x y }\n ; fro = \u03bb g \u2192 \u03bb x \u2192 \u03bb y \u2192 g (x , y)\n ; inv\u02e1 = \u03bb f \u2192 refl\n ; inv\u02b3 = \u03bb g \u2192 extensionality (\u03bb { (x , y) \u2192 refl })\n }\n\\end{code}\n\nCurrying tells us that instead of a function that takes a pair of arguments,\nwe can have a function that takes the first argument and returns a function that\nexpects the second argument. Thus, for instance, instead of\n\n _+\u2032_ : (\u2115 \u00d7 \u2115) \u2192 \u2115\n\nwe have\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n\nThe syntax of Agda, like that of other functional languages such as ML and\nHaskell, is designed to make currying easy to use. Function arrows associate\nto the left and application associates to the right. Thus\n\n A \u2192 B \u2192 C *stands for* A \u2192 (B \u2192 C)\n\nand\n\n f x y *stands for* (f x) y\n\nCurrying is named for Haskell Curry, after whom the programming language Haskell\nis also named. However, Currying was introduced earlier by both Gotlob Frege\nand Moses Sch\u00f6nfinkel. When I first learned about currying, I was told a joke:\n\"It should be called sch\u00f6nfinkeling, but currying is tastier\". Only later did I\nlearn that the idea actually goes all the way back to Frege!\n\nCorresponding to the law\n\n p ^ (n + m) = (p ^ n) * (p ^ m)\n\nwe have that the types `A \u228e B \u2192 C` and `(A \u2192 C) \u00d7 (B \u2192 C)` are isomorphic.\nThat is, the assertion that if either `A` holds or `B` holds then `C` holds\nis the same as the assertion that if `A` holds then `C` holds and if\n`B` holds then `C` holds.\n\\begin{code}\n\u2192-distributes-\u228e : \u2200 {A B C : Set} \u2192 (A \u228e B \u2192 C) \u2243 ((A \u2192 C) \u00d7 (B \u2192 C))\n\u2192-distributes-\u228e =\n record\n { to = \u03bb f \u2192 ( (\u03bb x \u2192 f (inj\u2081 x)) , (\u03bb y \u2192 f (inj\u2082 y)) ) \n ; fro = \u03bb {(g , h) \u2192 \u03bb { (inj\u2081 x) \u2192 g x ; (inj\u2082 y) \u2192 h y } }\n ; inv\u02e1 = \u03bb f \u2192 extensionality (\u03bb { (inj\u2081 x) \u2192 refl ; (inj\u2082 y) \u2192 refl })\n ; inv\u02b3 = \u03bb {(g , h) \u2192 refl}\n }\n\\end{code}\n\nCorresponding to the law\n\n (p * n) ^ m = (p ^ m) * (n ^ m)\n\nwe have that the types `A \u2192 B \u00d7 C` and `(A \u2192 B) \u00d7 (A \u2192 C)` are isomorphic.\nThat is, the assertion that if either `A` holds then `B` holds and `C` holds\nis the same as the assertion that if `A` holds then `B` holds and if\n`A` holds then `C` holds. The proof requires that we recognise the eta\nrule for products, which states that `(fst w , snd w) \u2261 w` for any product `w`.\n\\begin{code}\npostulate\n \u03b7-\u00d7 : \u2200 {A B : Set} \u2192 \u2200 (w : A \u00d7 B) \u2192 (fst w , snd w) \u2261 w\n\n\u2192-distributes-\u00d7 : \u2200 {A B C : Set} \u2192 (A \u2192 B \u00d7 C) \u2243 ((A \u2192 B) \u00d7 (A \u2192 C))\n\u2192-distributes-\u00d7 =\n record\n { to = \u03bb f \u2192 ( (\u03bb x \u2192 fst (f x)) , (\u03bb y \u2192 snd (f y)) ) \n ; fro = \u03bb {(g , h) \u2192 (\u03bb x \u2192 (g x , h x))}\n ; inv\u02e1 = \u03bb f \u2192 extensionality (\u03bb x \u2192 \u03b7-\u00d7 (f x))\n ; inv\u02b3 = \u03bb {(g , h) \u2192 refl}\n }\n\\end{code}\n\n\n## Distribution\n\nProducts distributes over sum, up to isomorphism. The code to validate\nthis fact is similar in structure to our previous results.\n\\begin{code}\n\u00d7-distributes-\u228e : \u2200 {A B C : Set} \u2192 ((A \u228e B) \u00d7 C) \u2243 ((A \u00d7 C) \u228e (B \u00d7 C))\n\u00d7-distributes-\u228e =\n record\n { to = \u03bb { ((inj\u2081 x) , z) \u2192 (inj\u2081 (x , z)) \n ; ((inj\u2082 y) , z) \u2192 (inj\u2082 (y , z))\n }\n ; fro = \u03bb { (inj\u2081 (x , z)) \u2192 ((inj\u2081 x) , z)\n ; (inj\u2082 (y , z)) \u2192 ((inj\u2082 y) , z)\n }\n ; inv\u02e1 = \u03bb { ((inj\u2081 x) , z) \u2192 refl\n ; ((inj\u2082 y) , z) \u2192 refl\n }\n ; inv\u02b3 = \u03bb { (inj\u2081 (x , z)) \u2192 refl\n ; (inj\u2082 (y , z)) \u2192 refl\n } \n }\n\\end{code}\n\nSums do not distribute over products up to isomorphism, but it is an embedding.\n\\begin{code}\n\u228e-distributes-\u00d7 : \u2200 {A B C : Set} \u2192 ((A \u00d7 B) \u228e C) \u2272 ((A \u228e C) \u00d7 (B \u228e C))\n\u228e-distributes-\u00d7 =\n record\n { to = \u03bb { (inj\u2081 (x , y)) \u2192 (inj\u2081 x , inj\u2081 y)\n ; (inj\u2082 z) \u2192 (inj\u2082 z , inj\u2082 z)\n }\n ; fro = \u03bb { (inj\u2081 x , inj\u2081 y) \u2192 (inj\u2081 (x , y))\n ; (inj\u2081 x , inj\u2082 z) \u2192 (inj\u2082 z)\n ; (inj\u2082 z , _ ) \u2192 (inj\u2082 z)\n }\n ; inv\u02e1 = \u03bb { (inj\u2081 (x , y)) \u2192 refl\n ; (inj\u2082 z) \u2192 refl\n }\n }\n\\end{code}\nNote that there is a choice in how we write the `fro` function.\nAs given, it takes `(inj\u2082 z , inj\u2082 z\u2032)` to `(inj\u2082 z)`, but it is\neasy to write a variant that instead returns `(inj\u2082 z\u2032)`. We have\nan embedding rather than an isomorphism because the\n`fro` function must discard either `z` or `z\u2032` in this case.\n\nIn the usual approach to logic, both of the distribution laws\nare given as equivalences, where each side implies the other:\n\n A & (B \u2228 C) \u21d4 (A & B) \u2228 (A & C)\n A \u2228 (B & C) \u21d4 (A \u2228 B) & (A \u2228 C)\n\nBut when we consider the two laws in terms of evidence, where `_&_`\ncorresponds to `_\u00d7_` and `_\u2228_` corresponds to `_\u228e_`, then the first\ngives rise to an isomorphism, while the second only gives rise to an\nembedding, revealing a sense in which one of these laws is \"more\ntrue\" than the other.\n\n\n## Negation\n\nGiven a proposition `A`, the negation `\u00ac A` holds if `A` cannot hold.\nWe formalise this idea by declaring negation to be the same\nas implication of false. \n\\begin{code}\n\u00ac_ : Set \u2192 Set\n\u00ac A = A \u2192 \u22a5\n\\end{code}\nThis is a form of *proof by contradiction*: if assuming `A` leads\nto the conclusion `\u22a5` (a contradiction), then we must have `\u00ac A`.\n\nEvidence that `\u00ac A` holds is of the form\n\n \u03bb (x : A) \u2192 N\n\nwhere `N` is a term of type `\u22a5` containing as a free variable `x` of type `A`.\nIn other words, evidence that `\u00ac A` holds is a function that converts evidence\nthat `A` holds into evidence that `\u22a5` holds.\n\nGiven evidence that both `\u00ac A` and `A` hold, we can conclude that `\u22a5` holds.\nIn other words, if both `\u00ac A` and `A` hold, then we have a contradiction.\n\\begin{code}\n\u00ac-elim : \u2200 {A : Set} \u2192 \u00ac A \u2192 A \u2192 \u22a5\n\u00ac-elim \u00aca a = \u00aca a\n\\end{code}\nHere we write `\u00aca` for evidence of `\u00ac A` and `a` for evidence of `A`. This\nmeans that `\u00aca` must be a function of type `A \u2192 \u22a5`, and hence the application\n`\u00aca a` must be of type `\u22a5`. Note that this rule is just a special case of `\u2192-elim`.\n\nWe set the precedence of negation so that it binds more tightly\nthan disjunction and conjunction, but more tightly than anything else.\n\\begin{code}\ninfix 3 \u00ac_\n\\end{code}\nThus, `\u00ac A \u00d7 \u00ac B` parses as `(\u00ac A) \u00d7 (\u00ac B)`.\n\nIn *classical* logic, we have that `A` is equivalent to `\u00ac \u00ac A`.\nAs we discuss below, in Agda we use *intuitionistic* logic, wher\nwe have only half of this equivalence, namely that `A` implies `\u00ac \u00ac A`.\n\\begin{code}\n\u00ac\u00ac-intro : \u2200 {A : Set} \u2192 A \u2192 \u00ac \u00ac A\n\u00ac\u00ac-intro a \u00aca = \u00aca a\n\\end{code}\nLet `a` be evidence of `A`. We will show that assuming\n`\u00ac A` leads to a contradiction, and hence `\u00ac \u00ac A` must hold.\nLet `\u00aca` be evidence of `\u00ac A`. Then from `A` and `\u00ac A`\nwe have a contradiction (evidenced by `\u00aca a`). Hence, we have\nshown `\u00ac \u00ac A`.\n\nWe cannot show that `\u00ac \u00ac A` implies `A`, but we can show that\n`\u00ac \u00ac \u00ac A` implies `\u00ac A`.\n\\begin{code}\n\u00ac\u00ac\u00ac-elim : \u2200 {A : Set} \u2192 \u00ac \u00ac \u00ac A \u2192 \u00ac A\n\u00ac\u00ac\u00ac-elim \u00ac\u00ac\u00aca a = \u00ac\u00ac\u00aca (\u00ac\u00ac-intro a)\n\\end{code}\nLet `\u00ac\u00ac\u00aca` be evidence of `\u00ac \u00ac \u00ac A`. We will show that assuming\n`A` leads to a contradiction, and hence `\u00ac A` must hold.\nLet `a` be evidence of `A`. Then by the previous result, we\ncan conclude `\u00ac \u00ac A` (evidenced by `\u00ac\u00ac-intro a`). Then from\n`\u00ac \u00ac \u00ac A` and `\u00ac \u00ac A` we have a contradiction (evidenced by\n`\u00ac\u00ac\u00aca (\u00ac\u00ac-intro a)`. Hence we have shown `\u00ac A`.\n\nAnother law of logic is the *contrapositive*,\nstating that if `A` implies `B`, then `\u00ac B` implies `\u00ac A`.\n\\begin{code}\ncontrapositive : \u2200 {A B : Set} \u2192 (A \u2192 B) \u2192 (\u00ac B \u2192 \u00ac A)\ncontrapositive a\u2192b \u00acb a = \u00acb (a\u2192b a)\n\\end{code}\nLet `a\u2192b` be evidence of `A \u2192 B` and let `\u00acb` be evidence of `\u00ac B`. We\nwill show that assuming `A` leads to a contradiction, and hence\n`\u00ac A` must hold. Let `a` be evidence of `A`. Then from `A \u2192 B` and\n`A` we may conclude `B` (evidenced by `a\u2192b a`), and from `B` and `\u00ac B`\nwe may conclude `\u22a5` (evidenced by `\u00acb (a\u2192b a)`). Hence, we have shown\n`\u00ac A`.\n\nGiven the correspondence of implication to exponentiation and\nfalse to the type with no members, we can view negation as\nraising to the zero power. This indeed corresponds to what\nwe know for arithmetic, where\n\n 0 ^ n = 1, if n = 0\n = 0, if n \u2260 0\n\nIndeed, there is exactly one proof of `\u22a5 \u2192 \u22a5`.\n\\begin{code}\nid : \u22a5 \u2192 \u22a5\nid x = x\n\\end{code}\nHowever, there are no possible values of type `Bool \u2192 \u22a5`\nor `\u2115 \u2192 bot`.\n\n[TODO?: Give the proof that one cannot falsify law of the excluded middle,\nincluding a variant of the story from Call-by-Value is Dual to Call-by-Name.]\n\nThe law of the excluded middle is irrefutable.\n\\begin{code}\nexcluded-middle-irrefutable : \u2200 {A : Set} \u2192 \u00ac \u00ac (A \u228e \u00ac A)\nexcluded-middle-irrefutable k = k (inj\u2082 (\u03bb a \u2192 k (inj\u2081 a)))\n\\end{code}\n\n*Exercise* (`\u00ac\u228e\u2243\u00d7\u00ac`)\n\nShow that conjunction, disjunction, and negation are related by a\nversion of De Morgan's Law.\n\\begin{code}\npostulate\n \u00ac\u228e\u2243\u00d7\u00ac : \u2200 {A B : Set} \u2192 \u00ac (A \u228e B) \u2243 (\u00ac A) \u00d7 (\u00ac B)\n\\end{code}\nThis result is an easy consequence of something we've proved previously.\n\nDo we also have the following?\n\n \u00ac (A \u00d7 B) \u2243 (\u00ac A) \u228e (\u00ac B)\n\nIf so, prove; if not, explain why.\n\n\n## Intuitive and Classical logic\n\nIn Gilbert and Sullivan's *The Gondoliers*, Casilda is told that\nas an infant she was married to the heir of the King of Batavia, but\nthat due to a mix-up no one knows which of two individuals, Marco or\nGiuseppe, is the heir. Alarmed, she wails ``Then do you mean to say\nthat I am married to one of two gondoliers, but it is impossible to\nsay which?'' To which the response is ``Without any doubt of any kind\nwhatever.''\n\nLogic comes in many varieties, and one distinction is between\n*classical* and *intuitionistic*. Intuitionists, concerned\nby cavalier assumptions made by some logicians about the nature of\ninfinity, insist upon a constructionist notion of truth. In\nparticular, they insist that a proof of `A \u228e B` must show\n*which* of `A` or `B` holds, and hence they would reject the\nclaim that Casilda is married to Marco or Giuseppe until one of the\ntwo was identified as her husband. Perhaps Gilbert and Sullivan\nanticipated intuitionism, for their story's outcome is that the heir\nturns out to be a third individual, Luiz, with whom Casilda is,\nconveniently, already in love.\n\nIntuitionists also reject the law of the excluded\nmiddle, which asserts `A \u228e \u00ac A` for every `A`, since the law\ngives no clue as to *which* of `A` or `\u00ac A` holds. Heyting\nformalised a variant of Hilbert's classical logic that captures the\nintuitionistic notion of provability. In particular, the law of the\nexcluded middle is provable in Hilbert's logic, but not in Heyting's.\nFurther, if the law of the excluded middle is added as an axiom to\nHeyting's logic, then it becomes equivalent to Hilbert's.\nKolmogorov\nshowed the two logics were closely related: he gave a double-negation\ntranslation, such that a formula is provable in classical logic if and\nonly if its translation is provable in intuitionistic logic.\n\nPropositions as Types was first formulated for intuitionistic logic.\nIt is a perfect fit, because in the intuitionist interpretation the\nformula `A \u228e B` is provable exactly when one exhibits either a proof\nof `A` or a proof of `B`, so the type corresponding to disjunction is\na disjoint sum.\n\n(The passage above is taken from \"Propositions as Types\", Philip Wadler,\n*Communications of the ACM*, December 2015.)\n\n**Exercise** Prove the following five formulas are equivalent.\n\n\\begin{code}\nlone ltwo : Level\nlone = lsuc lzero\nltwo = lsuc lone\n\n\u00ac\u00ac-elim excluded-middle peirce implication de-morgan : Set lone\n\u00ac\u00ac-elim = \u2200 {A : Set} \u2192 \u00ac \u00ac A \u2192 A\nexcluded-middle = \u2200 {A : Set} \u2192 A \u228e \u00ac A\npeirce = \u2200 {A B : Set} \u2192 (((A \u2192 B) \u2192 A) \u2192 A)\nimplication = \u2200 {A B : Set} \u2192 (A \u2192 B) \u2192 \u00ac A \u228e B\nde-morgan = \u2200 {A B : Set} \u2192 \u00ac (A \u00d7 B) \u2192 \u00ac A \u228e \u00ac B\n\\end{code}\n\nIt turns out that an assertion such as `Set : Set` is paradoxical.\nTherefore, there are an infinite number of different levels of sets,\nwhere `Set lzero : Set lone` and `Set lone : Set ltwo`, and so on,\nwhere `lone` is `lsuc lzero`, and `ltwo` is `lsuc lone`, analogous to\nthe way we represent the natural nuambers. So far, we have only used\nthe type `Set`, Here is the demonstration that the reverse direction of one of the five\nformulas equivalent to which is equivalent to `Set lzero`. Here, since each\nof `double-negation` and the others defines a type, we need to use\n`Set lone` as the \"type of types\".\n\n\n## Universals\n\nGiven a variable `x` of type `A` and a proposition `B[x]` which\ncontains `x` as a free variable, the universally quantified\nproposition `\u2200 (x : A) \u2192 B[x]` holds if for every term `M` of type\n`A` the proposition `B[M]` holds. Here `B[M]` stands for\nthe proposition `B[x]` with each free occurrence of `x` replaced by\n`M`. The variable `x` appears free in `B[x]` but bound in\n`\u2200 (x : A) \u2192 B[x]`. We formalise universal quantification using the\ndependent function type, which has appeared throughout this book.\n\nEvidence that `\u2200 (x : A) \u2192 B[x]` holds is of the form\n\n \u03bb (x : A) \u2192 N[x]\n\nwhere `N[x]` is a term of type `B[x]`; here `N[x]` is a term and `B[x]`\nis a proposition (or type) both containing as a free variable `x` of\ntype `A`. Given a term `L` providing evidence that `\u2200 (x : A) \u2192 B[x]`\nholds and a term `M` of type `A`, the term `L M` provides evidence\nthat `B[M]` holds. In other words, evidence that `\u2200 (x : A) \u2192 B[x]`\nholds is a function that converts a term `M` of type `A` into evidence\nthat `B[M]` holds.\n\nPut another way, if we know that `\u2200 (x : A) \u2192 B[x]` holds and that `M`\nis a term of type `A` then we may conclude that `B[M]` holds. In\nmedieval times, this rule was known by the name *dictum de omni*.\n\nIf we introduce a universal and the immediately eliminate it, we can\nalways simplify the resulting term. Thus\n\n (\u03bb (x : A) \u2192 N[x]) M\n\nsimplifies to `N[M]` of type `B[M]`, where `N[M]` stands for the term\n`N[x]` with each free occurrence of `x` replaced by `M` of type `A`.\n\nUnlike with conjunction, disjunction, and implication, there is no simple\nanalogy between universals and arithmetic.\n\n\n## Existentials\n\nGiven a variable `x` of type `A` and a proposition `B[x]` which\ncontains `x` as a free variable, the existentially quantified\nproposition `\u2203 (\u03bb (x : A) \u2192 B[x])` holds if for some term `M` of type\n`A` the proposition `B[M]` holds. Here `B[M]` stands for\nthe proposition `B[x]` with each free occurrence of `x` replaced by\n`M`. The variable `x` appears free in `B[x]` but bound in\n`\u2203 (\u03bb (x : A) \u2192 B[x])`.\n\nIt is common to adopt a notation such as `\u2203[ x : A ]\u2192 B[x]`,\nwhich introduces `x` as a bound variable of type `A` that appears\nfree in `B[x]` and bound in `\u2203[ x : A ]\u2192 B[x]`. We won't do\nthat here, but instead use the lambda notation built-in to Agda\nto introduce `x` as a bound variable.\n\nWe formalise existential quantification by declaring a suitable\ninductive type.\n\\begin{code}\ndata \u2203 {A : Set} (B : A \u2192 Set) : Set where\n _,_ : (x : A) \u2192 B x \u2192 \u2203 B\n\\end{code}\nEvidence that `\u2203 (\u03bb (x : A) \u2192 B[x])` holds is of the form\n`(M , N)` where `M` is a term of type `A`, and `N` is evidence\nthat `B[M]` holds.\n\nGiven evidence that `\u2203 (\u03bb (x : A) \u2192 B[x])` holds, and\ngiven evidence that `\u2200 (x : A) \u2192 B[x] \u2192 C` holds, where `C` does\nnot contain `x` as a free variable, we may conclude that `C` holds.\n\\begin{code}\n\u2203-elim : \u2200 {A : Set} {B : A \u2192 Set} {C : Set} \u2192 \u2203 B \u2192 (\u2200 (x : A) \u2192 B x \u2192 C) \u2192 C\n\u2203-elim (M , N) P = P M N\n\\end{code}\nIn other words, if we know for every `x` of type `A` that `B[x]`\nimplies `C`, and we know for some `x` of type `A` that `B[x]` holds,\nthen we may conclude that `C` holds. This is because we may\ninstantiate that proof that `\u2200 (x : A) \u2192 B[x] \u2192 C` to any `x`, and we\nmay choose the particular `x` provided by the evidence that `\u2203 (\u03bb (x :\nA) \u2192 B[x])`.\n\n\n[It would be better to have even and odd as an exercise. Is there\na simpler example that I could start with?]\n\nAs an example, recall the definitions of `even` from\nChapter [Relations](Relations). \n\\begin{code}\ndata even : \u2115 \u2192 Set where\n ev0 : even zero\n ev+2 : \u2200 {n : \u2115} \u2192 even n \u2192 even (suc (suc n))\n\\end{code}\nA number is even if it is zero, or if it is two\ngreater than an even number.\n\nWe will show that a number is even if and only if it is twice some\nother number. That is, number `n` is even if and only if there exists\na number `m` such that twice `m` is `n`.\n\nFirst, we need a lemma, which allows us to\nsimplify twice the successor of `m` to two\nmore than twice `m`.\n\\begin{code}\nlemma : \u2200 (m : \u2115) \u2192 2 * suc m \u2261 suc (suc (2 * m))\nlemma m =\n begin\n 2 * suc m\n \u2261\u27e8\u27e9\n suc m + (suc m + zero)\n \u2261\u27e8\u27e9\n suc (m + (suc (m + zero)))\n \u2261\u27e8 cong suc (+-suc m (m + zero)) \u27e9\n suc (suc (m + (m + zero)))\n \u2261\u27e8\u27e9\n suc (suc (2 * m))\n \u220e\n\\end{code}\nThe lemma is straightforward, and uses the lemma\n`+-suc` from Chapter [Properties](Properties), which\nallows us to simplify `m + suc n` to `suc (m + n)`.\n\nHere is the proof in the forward direction.\n\\begin{code} \nev-ex : \u2200 {n : \u2115} \u2192 even n \u2192 \u2203(\u03bb (m : \u2115) \u2192 2 * m \u2261 n)\nev-ex ev0 = (zero , refl)\nev-ex (ev+2 ev) with ev-ex ev\n... | (m , refl) = (suc m , lemma m)\n\\end{code}\nGiven an even number, we must show it is twice some\nother number. The proof is a function, which\ngiven a proof that `n` is even\nreturns a pair consisting of `m` and a proof\nthat twice `m` is `n`. The proof is by induction over\nthe evidence that `n` is even.\n\n- If the number is even because it is zero, then we return a pair\nconsisting of zero and the (trivial) proof that twice zero is zero.\n\n- If the number is even because it is two more than another even\nnumber `n`, then we apply the induction hypothesis, giving us a number\n`m` and a proof that `2 * m \u2261 n`, which we match against `refl`. We\nreturn a pair consisting of `suc m` and a proof that `2 * suc m \u2261 suc\n(suc n)`, which follows from `2 * m \u2261 n` and the lemma.\n\nHere is the proof in the reverse direction.\n\\begin{code}\nex-ev : \u2200 {n : \u2115} \u2192 \u2203(\u03bb (m : \u2115) \u2192 2 * m \u2261 n) \u2192 even n\nex-ev (zero , refl) = ev0\nex-ev (suc m , refl) rewrite lemma m = ev+2 (ex-ev (m , refl))\n\\end{code}\nGiven a number that is twice some other number,\nwe must show that it is even. The proof is a function,\nwhich given a number `m` and a proof that `n` is twice `m`,\nreturns a proof that `n` is even. The proof is by induction\nover the number `m`.\n\n- If it is zero, then we must show that twice zero is even,\nwhich follows by rule `ev0`. \n\n- If it is `suc m`, then we must show that `2 * suc m` is\neven. After rewriting with our lemma, we must show that\n`suc (suc (2 * m))` is even. The inductive hypothesis tells\nus `2 * m` is even, from which the desired result follows\nby rule `ev+2`.\n\nNegation of an existential is isomorphic to universal\nof a negation. Considering that existentials are generalised\ndisjuntion and universals are generalised conjunction, this\nresult is analogous to the one which tells us that negation\nof a disjuntion is isomorphic to a conjunction of negations.\n\\begin{code}\n\u00ac\u2203\u2200 : \u2200 {A : Set} {B : A \u2192 Set} \u2192 (\u00ac \u2203 (\u03bb (x : A) \u2192 B x)) \u2243 \u2200 (x : A) \u2192 \u00ac B x\n\u00ac\u2203\u2200 =\n record\n { to = \u03bb { \u00ac\u2203bx x bx \u2192 \u00ac\u2203bx (x , bx) }\n ; fro = \u03bb { \u2200\u00acbx (x , bx) \u2192 \u2200\u00acbx x bx }\n ; inv\u02e1 = \u03bb { \u00ac\u2203bx \u2192 extensionality (\u03bb { (x , bx) \u2192 refl }) } \n ; inv\u02b3 = \u03bb { \u2200\u00acbx \u2192 refl } \n }\n\\end{code}\nIn the `to` direction, we are given a value `\u00ac\u2203bx` of type\n`\u00ac \u2203 (\u03bb (x : A) \u2192 B x)`, and need to show that given a value\n`x` of type `A` that `\u00ac B x` follows, in other words, from\na value `bx` of type `B x` we can derive false. Combining\n`x` and `bx` gives us a value `(x , bx)` of type `\u2203 (\u03bb (x : A) \u2192 B x)`,\nand applying `\u00ac\u2203bx` to that yields a contradiction.\n\nIn the `fro` direction, we are given a value `\u2200\u00acbx` of type\n`\u2200 (x : A) \u2192 \u00ac B x`, and need to show that from a value `(x , bx)`\nof type `\u2203 (\u03bb (x : A) \u2192 B x)` we can derive false. Applying `\u2200\u00acbx`\nto `x` gives a value of type `\u00ac B x`, and applying that to `bx` yields\na contradiction.\n\nThe two inverse proofs are straightforward, where one direction\nrequires extensionality.\n\n*Exercise* Show `\u2203 (\u03bb (x : A) \u2192 \u00ac B x) \u2192 \u00ac (\u2200 (x : A) \u2192 B x)`.\n\n\n\n## Decidability\n\n\\begin{code}\ndata Dec : Set \u2192 Set where\n yes : \u2200 {A : Set} \u2192 A \u2192 Dec A\n no : \u2200 {A : Set} \u2192 (\u00ac A) \u2192 Dec A\n\\end{code}\n\n## Unicode\n\nThis chapter introduces the following unicode.\n\n \u2264 U+2264 LESS-THAN OR EQUAL TO (\\<=, \\le)\n\n\n[NOTES]\n\nTwo halves of de Morgan's laws hold intuitionistically. The other two\nhalves are each equivalent to the law of double negation.\n\n\\begin{code}\ndem1 : \u2200 {A B : Set} \u2192 A \u00d7 B \u2192 \u00ac (\u00ac A \u228e \u00ac B)\ndem1 (a , b) (inj\u2081 \u00aca) = \u00aca a\ndem1 (a , b) (inj\u2082 \u00acb) = \u00acb b\n\ndem2 : \u2200 {A B : Set} \u2192 A \u228e B \u2192 \u00ac (\u00ac A \u00d7 \u00ac B)\ndem2 (inj\u2081 a) (\u00aca , \u00acb) = \u00aca a\ndem2 (inj\u2082 b) (\u00aca , \u00acb) = \u00acb b\n\\end{code}\n\nFor the other variant of De Morgan's law, one way is an isomorphism.\n\\begin{code}\ndem-\u2243 : \u2200 {A B : Set} \u2192 (\u00ac (A \u228e B)) \u2243 (\u00ac A \u00d7 \u00ac B)\ndem-\u2243 = \u2192-distributes-\u228e\n\\end{code}\n\nThe other holds in only one direction.\n\\begin{code}\ndem-half : \u2200 {A B : Set} \u2192 \u00ac A \u228e \u00ac B \u2192 \u00ac (A \u00d7 B)\ndem-half (inj\u2081 \u00aca) (a , b) = \u00aca a\ndem-half (inj\u2082 \u00acb) (a , b) = \u00acb b\n\\end{code}\n\nThe other variant does not appear to be equivalent to classical logic.\nSo that undermines my idea that basic propositions are either true\nintuitionistically or equivalent to classical logic.\n\nFor several of the laws equivalent to classical logic, the reverse\ndirection holds in intuitionistic long.\n\\begin{code}\nimplication-inv : \u2200 {A B : Set} \u2192 (\u00ac A \u228e B) \u2192 A \u2192 B\nimplication-inv (inj\u2081 \u00aca) a = \u22a5-elim (\u00aca a)\nimplication-inv (inj\u2082 b) a = b\n\ndemorgan-inv : \u2200 {A B : Set} \u2192 A \u228e B \u2192 \u00ac (\u00ac A \u00d7 \u00ac B)\ndemorgan-inv (inj\u2081 a) (\u00aca , \u00acb) = \u00aca a\ndemorgan-inv (inj\u2082 b) (\u00aca , \u00acb) = \u00acb b\n\\end{code}\n\n\n\n \n","avg_line_length":34.6111580727,"max_line_length":87,"alphanum_fraction":0.630260105} +{"size":17526,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\n{-# OPTIONS --without-K --rewriting #-}\n\nmodule Rewriting where\n\n{- Universe levels -}\n\nopen import Agda.Primitive renaming (_\u2294_ to lmax) public\n\nType : (i : Level) \u2192 Set (lsuc i)\nType i = Set i\n\nType\u2080 = Type lzero\n\nofType : {i : Level} (A : Type i) \u2192 A \u2192 A\nofType A x = x\n\nsyntax ofType A x = x :> A\n\ninfixr 3 ofType\n\n\n{- Rewriting relation -}\n\npostulate\n _\u21a6_ : \u2200 {i} {A : Type i} \u2192 A \u2192 A \u2192 Type i\n idr : \u2200 {i} {A : Type i} {a : A} \u2192 a \u21a6 a\n\n{-# BUILTIN REWRITE _\u21a6_ #-}\n\npostulate\n ap\u21a6 : \u2200 {i j} {A : Type i} {B : Type j} (f : A \u2192 B) {a b : A} (p : a \u21a6 b) \u2192 f a \u21a6 f b\n\n{- Identity type -}\n\ninfixr 3 _==_\n\npostulate\n _==_ : \u2200 {i} {A : Type i} \u2192 A \u2192 A \u2192 Type i\n idp : \u2200 {i} {A : Type i} {a : A} \u2192 a == a\n\n\npostulate\n \u21a6== : \u2200 {i} {A : Type i} {a b : A} \u2192 a \u21a6 b \u2192 a == b\n\n{- Heterogeneous equality, dependent equality and ap -}\n\npostulate\n HetEq : \u2200 {i} {A B : Type i} (e : A == B) (a : A) (b : B) \u2192 Type i\n HetEq\u21a6 : \u2200 {i} {A : Type i} (a b : A) \u2192 HetEq idp a b \u21a6 (a == b)\n {-# REWRITE HetEq\u21a6 #-}\n PathOver : \u2200 {i j} {A : Type i} (B : A \u2192 Type j)\n {x y : A} (p : x == y) (u : B x) (v : B y) \u2192 Type j\n PathOver\u21a6idp : \u2200 {i j} {A : Type i} (B : A \u2192 Type j) {x : A} (u v : B x)\n \u2192 PathOver B idp u v \u21a6 (u == v)\n {-# REWRITE PathOver\u21a6idp #-}\n\n\nsyntax PathOver B p u v =\n u == v [ B \u2193 p ]\n\npostulate\n PathOver-cst : \u2200 {i j} {A : Type i} {B : Type j} {x y : A} (p : x == y) (u v : B) \u2192\n (PathOver (\u03bb _ \u2192 B) p u v) \u21a6 (u == v)\n {-# REWRITE PathOver-cst #-}\n\n-- Note that this is both [ap] and [apd], as a [PathOver] in a constant family\n-- reduces to the usual identity type.\npostulate\n ap : \u2200 {i j} {A : Type i} {B : A \u2192 Type j} (f : (a : A) \u2192 B a) {x y : A}\n \u2192 (p : x == y) \u2192 PathOver B p (f x) (f y)\n\n ap\u21a6idp : \u2200 {i j} {A : Type i} {B : A \u2192 Type j} (f : (a : A) \u2192 B a) {x : A}\n \u2192 ap f (idp {a = x}) \u21a6 idp\n {-# REWRITE ap\u21a6idp #-}\n\npostulate\n ap-cst : \u2200 {i j} {A : Type i} {B : Type j} (b : B) {x y : A} (p : x == y) \u2192\n ap (\u03bb _ \u2192 b) p \u21a6 idp\n {-# REWRITE ap-cst #-}\n\n{-\nIntuitively, [PathOver] should be defined using [HetEq], except that for that\nwe need [ap] for which we need [PathOver]. So we have to define first [PathOver]\nand [ap], and then declare a rewrite rule rewriting any [PathOver] to [HetEq].\n-}\n\npostulate\n PathOver\u21a6 : \u2200 {i j} {A : Type i} (B : A \u2192 Type j)\n {x y : A} (p : x == y) (u : B x) (v : B y) \u2192 PathOver B p u v \u21a6 HetEq (ap B p) u v\n {-# REWRITE PathOver\u21a6 #-}\n\n-- [ap] for dependent paths. It might be possible to merge it with [ap] with the\n-- appropriate reductions on types but I haven\u2019t tried yet -}\npostulate\n ap\u2193 : \u2200 {i j k} {A : Type i} {B : A \u2192 Type j} {C : A \u2192 Type k}\n (g : {a : A} \u2192 B a \u2192 C a) {x y : A} {p : x == y}\n {u : B x} {v : B y}\n \u2192 (u == v [ B \u2193 p ] \u2192 g u == g v [ C \u2193 p ])\n\n{- Composition of functions -}\n\n_\u2218_ : \u2200 {i j k} {A : Type i} {B : Type j} {C : Type k} (f : B \u2192 C) (g : A \u2192 B) \u2192 A \u2192 C\nf \u2218 g = \u03bb x \u2192 f (g x)\n\n{- Squares -}\n\npostulate\n Square : \u2200 {i} {A : Type i} {w : A} {x y z : A} \u2192 w == y \u2192 x == z \u2192 w == x \u2192 y == z \u2192 Type i\n ids : \u2200 {i} {A : Type i} {w : A} \u2192 Square {w = w} idp idp idp idp\n\n Square-degen : \u2200 {i} {A : Type i} {a b : A} {p q : a == b} \u2192 p == q \u2192 Square idp idp p q\n Square-degen2 : \u2200 {i} {A : Type i} {a b : A} {p q : a == b} \u2192 p == q \u2192 Square p q idp idp\n Square-degen2' : \u2200 {i} {A : Type i} {a b : A} {p q : a == b} \u2192 Square p q idp idp \u2192 p == q\n\npostulate\n HetSquare : \u2200 {i} {W X Y Z : Type i} {L : W == Y} {R : X == Z} {T : W == X} {B : Y == Z} (SQ : Square L R T B)\n {w : W} {x : X} {y : Y} {z : Z} (l : HetEq L w y) (r : HetEq R x z) (t : HetEq T w x) (b : HetEq B y z)\n \u2192 Type i\n\n HetSquare\u21a6 : \u2200 {i} {A : Type i} {w x y z : A} (l : w == y) (r : x == z) (t : w == x) (b : y == z) \u2192 HetSquare ids l r t b \u21a6 Square l r t b\n {-# REWRITE HetSquare\u21a6 #-}\n\npostulate\n SquareOver : \u2200 {i j} {A : Type i} (B : A \u2192 Type j) {w x y z : A}\n {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b)\n {w' : B w} {x' : B x} {y' : B y} {z' : B z}\n (l' : w' == y' [ B \u2193 l ]) (r' : x' == z' [ B \u2193 r ])\n (t' : w' == x' [ B \u2193 t ]) (b' : y' == z' [ B \u2193 b ])\n \u2192 Type j\n SquareOver\u21a6Square : \u2200 {i j} {A : Type i} (B : A \u2192 Type j) {w : A}\n {w' x y z : B w} {l : w' == y} {r : x == z} {t : w' == x} {b : y == z}\n \u2192 SquareOver B (ids {w = w}) l r t b \u21a6 Square l r t b\n {-# REWRITE SquareOver\u21a6Square #-}\n\npostulate\n ap\u25a1 : \u2200 {i j} {A : Type i} {B : A \u2192 Type j} (f : (x : A) \u2192 B x)\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b)\n \u2192 SquareOver B sq (ap f l) (ap f r) (ap f t) (ap f b)\n\n SquareOver-cst : \u2200 {i j} {A : Type i} {B : Type j} {w x y z : A}\n {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b)\n {w' x' y' z' : B}\n (l' : w' == y') (r' : x' == z')\n (t' : w' == x') (b' : y' == z') \u2192\n SquareOver (\u03bb _ \u2192 B) sq l' r' t' b' \u21a6 Square l' r' t' b'\n {-# REWRITE SquareOver-cst #-}\n\n ap\u25a1-cst : \u2200 {i j} {A : Type i} {B : Type j} {b\u2080 : B}\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b)\n \u2192 ap\u25a1 (\u03bb _ \u2192 b\u2080) sq \u21a6 ids\n {-# REWRITE ap\u25a1-cst #-}\n\n SquareOver-rewr : \u2200 {i j} {A : Type i} (B : A \u2192 Type j) {w x y z : A}\n {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b)\n {w' : B w} {x' : B x} {y' : B y} {z' : B z}\n (l' : w' == y' [ B \u2193 l ]) (r' : x' == z' [ B \u2193 r ])\n (t' : w' == x' [ B \u2193 t ]) (b' : y' == z' [ B \u2193 b ])\n \u2192 (SquareOver B sq l' r' t' b' \u21a6 HetSquare (ap\u25a1 B sq) l' r' t' b')\n {-# REWRITE SquareOver-rewr #-}\n\n idv : \u2200 {i} {A : Type i} {a a' : A} {p : a == a'} \u2192 Square idp idp p p\n idv-idp : \u2200 {i} {A : Type i} {a : A} \u2192 idv {p = idp {a = a}} \u21a6 ids\n {-# REWRITE idv-idp #-}\n\n ap\u25a1-idv : \u2200 {i j} {A : Type i} {B : Type j} (f : A \u2192 B) {a a' : A} (p : a == a') \u2192\n ap\u25a1 f (idv {p = p}) \u21a6 idv\n {-# REWRITE ap\u25a1-idv #-}\n\n idh : \u2200 {i} {A : Type i} {a a' : A} {p : a == a'} \u2192 Square p p idp idp\n\n Square-degen2-idp : \u2200 {i} {A : Type i} {a b : A} {p : a == b} \u2192 Square-degen2 (idp {a = p}) \u21a6 idh\n {-# REWRITE Square-degen2-idp #-}\n\n diag : \u2200 {i} {A : Type i} {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b) \u2192 w == z\n\n diag-idh : \u2200 {i} {A : Type i} {a a' : A} {p : a == a'} \u2192 diag (idh {p = p}) \u21a6 p\n {-# REWRITE diag-idh #-}\n\n\npostulate\n sym-square : \u2200 {i} {A : Type i}\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n \u2192 Square l r t b \u2192 Square t b l r\n\nmodule _ {i} {A : Type i} where\n\n postulate\n ap-idf : {x y : A} (p : x == y) \u2192 ap (\u03bb x \u2192 x) p \u21a6 p\n {-# REWRITE ap-idf #-}\n\n postulate\n ap\u25a1-idf : {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b) \u2192\n ap\u25a1 (\u03bb x \u2192 x) sq \u21a6 sq\n {-# REWRITE ap\u25a1-idf #-}\n\n{- Dependent identity type in an identity type -}\n\npostulate\n ==-eq : \u2200 {i} {A B : Type i} (e : A == B) {a b : A} {c d : B} (p : HetEq e a c) (q : HetEq e b d) \u2192 (a == b) == (c == d)\n ==-eq-idp : \u2200 {i} {A : Type i} {a b : A} \u2192 ==-eq idp {a = a} {b = b} idp idp \u21a6 idp\n {-# REWRITE ==-eq-idp #-}\n\nmodule _ {i} {A : Type i} {B : A \u2192 Type i} (f g : (a : A) \u2192 B a)\n {x y : A} (p : x == y) where\n\n postulate\n ap-== : ap (\u03bb z \u2192 f z == g z) p \u21a6 ==-eq (ap B p) (ap f p) (ap g p)\n {-# REWRITE ap-== #-}\n\nmodule _ {i} {A B : Type i} (e : A == B) {a b : A} {c d : B} {p : HetEq e a c} {q : HetEq e b d} {u : a == b} {v : c == d} where\n\n postulate\n \u2193-==-eq-in : HetSquare idv u v p q \u2192 HetEq (==-eq e p q) u v\n \u2193-==-eq-out : HetEq (==-eq e p q) u v \u2192 HetSquare idv u v p q\n \u2193-==-eq-\u03b2 : (sq : HetSquare idv u v p q) \u2192 \u2193-==-eq-out (\u2193-==-eq-in sq) \u21a6 sq\n {-# REWRITE \u2193-==-eq-\u03b2 #-}\n\npostulate\n \u2193-==-eq-out-idp : \u2200 {i} {A : Type i} {a b : A} {u v : a == b} \u2192 \u2193-==-eq-out idp {p = idp} {q = idp} {u = u} {v = v} \u21a6 Square-degen2\n {-# REWRITE \u2193-==-eq-out-idp #-}\n\nmodule _ {i} {A : Type i} {B : A \u2192 Type i} (f g : (a : A) \u2192 B a)\n {x y : A} (p : x == y) {u : f x == g x} {v : f y == g y} where\n\n \u2193-='-in : HetSquare idv u v (ap f p) (ap g p)\n \u2192 u == v [ (\u03bb z \u2192 f z == g z) \u2193 p ]\n \u2193-='-in = \u2193-==-eq-in (ap B p)\n\n \u2193-='-out : u == v [ (\u03bb z \u2192 f z == g z) \u2193 p ]\n \u2192 HetSquare idv u v (ap f p) (ap g p)\n \u2193-='-out = \u2193-==-eq-out (ap B p)\n\n{-\nCubes, in order to deal with dependent squares in the identity type and\ndependent paths in the square type.\n-}\n\npostulate\n Cube : \u2200 {i} {A : Type i} {w : A} {x y z w' x' y' z' : A}\n {ll : w == y} {lr : x == z} {lt : w == x} {lb : y == z}\n (l : Square ll lr lt lb)\n {rl : w' == y'} {rr : x' == z'} {rt : w' == x'} {rb : y' == z'}\n (r : Square rl rr rt rb)\n {w= : w == w'} {x= : x == x'} {y= : y == y'} {z= : z == z'}\n (t : Square w= y= ll rl)\n (b : Square x= z= lr rr)\n (f : Square w= x= lt rt)\n (k : Square y= z= lb rb)\n \u2192 Type i\n idc : \u2200 {i} {A : Type i} {w : A} \u2192 Cube {w = w} ids ids ids ids ids ids\n\n idhc : \u2200 {i} {A : Type i}\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n {sq : Square l r t b}\n \u2192 Cube sq sq idv idv idv idv\n\n coh0 : \u2200 {i} {A : Type i} {w x y z : A} {l : w == y} {r : x == z} {t t' : w == x} {b b' : y == z}\n (pt : t == t') (pb : b == b') (\u03b1 : Square l r t b) \u2192 Square l r t' b'\n\n Cube-degen : \u2200 {i} {A : Type i} {w x y z : A} {l : w == y} {r : x == z} {t t' : w == x} {b b' : y == z}\n {pt : t == t'} {pb : b == b'} {\u03b1 : Square l r t b} {\u03b2 : Square l r t' b'} \u2192 coh0 pt pb \u03b1 == \u03b2 \u2192 Cube \u03b1 \u03b2 idv idv (Square-degen pt) (Square-degen pb)\n\n\n\nmodule _ {i} {A : Type i} {B : Type i} (f g : A \u2192 B)\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z} (sq : Square l r t b)\n {w= : f w == g w} {x= : f x == g x} {y= : f y == g y} {z= : f z == g z}\n {l= : Square w= y= (ap f l) (ap g l)} {r= : Square x= z= (ap f r) (ap g r)}\n {t= : Square w= x= (ap f t) (ap g t)} {b= : Square y= z= (ap f b) (ap g b)} where\n\n postulate\n \u2193-\u25a1'-in : Cube (ap\u25a1 f sq) (ap\u25a1 g sq) l= r= t= b=\n \u2192 SquareOver (\u03bb z \u2192 f z == g z) sq (\u2193-='-in f g l l=) (\u2193-='-in f g r r=) (\u2193-='-in f g t t=) (\u2193-='-in f g b b=)\n\nmodule _ {i} {A : Type i} {B : Type i} {f g : A \u2192 B}\n {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z} {sq : Square l r t b}\n {w= : f w == g w} {x= : f x == g x} {y= : f y == g y} {z= : f z == g z}\n {l= : HetEq (ap (\u03bb z\u2081 \u2192 f z\u2081 == g z\u2081) l) w= y=} {r= : HetEq (ap (\u03bb z\u2081 \u2192 f z\u2081 == g z\u2081) r) x= z=}\n {t= : HetEq (ap (\u03bb z\u2081 \u2192 f z\u2081 == g z\u2081) t) w= x=} {b= : HetEq (ap (\u03bb z\u2081 \u2192 f z\u2081 == g z\u2081) b) y= z=} where\n postulate\n \u2193-\u25a1'-in2 : Cube (ap\u25a1 f sq) (ap\u25a1 g sq) (\u2193-='-out f g l l=) (\u2193-='-out f g r r=) (\u2193-='-out f g t t=) (\u2193-='-out f g b b=)\n \u2192 SquareOver (\u03bb z \u2192 f z == g z) sq l= r= t= b=\n\nmodule _ {i} {A : Type i} {B : Type i}\n {w x y z : A \u2192 B} (l : (e : A) \u2192 w e == y e) (r : (e : A) \u2192 x e == z e) (t : (e : A) \u2192 w e == x e) (b : (e : A) \u2192 y e == z e)\n {a a' : A} (p : a == a')\n {sq : Square (l a) (r a) (t a) (b a)} {sq' : Square (l a') (r a') (t a') (b a')} where\n postulate\n \u2193-\u25a1='-in : Cube (\u2193-='-out w x p (ap t p)) (\u2193-='-out y z p (ap b p)) sq sq' (\u2193-='-out w y p (ap l p)) (\u2193-='-out x z p (ap r p))\n \u2192 PathOver (\u03bb z \u2192 Square (l z) (r z) (t z) (b z)) p sq sq'\n\npostulate\n ap\u25a1-idh : \u2200 {i j} {A : Type i} {B : Type j} (f : A \u2192 B) {a a' : A} (p : a == a') \u2192\n ap\u25a1 f (idh {p = p}) \u21a6 idh {p = ap f p}\n{-# REWRITE ap\u25a1-idh #-}\n\n\nmodule _ {i j k} {A : Type i} {B : Type j} {C : Type k} (g : B \u2192 C) (f : A \u2192 B) where\n\n {-\n The following two rewrite rules are inverse to each other, so they can\u2019t\n both be defined as rewrite rules or there would be an infinite loop\n (and [ap\u2218] can\u2019t possibly be a rewrite rule anyway, as there would be no way\n to guess [f] and [g]).\n We need them both, though, for instance we need [\u2218ap] when [f] is a\n constructor-with-argument of a (higher-)inductive type (like [inl]) and [g]\n the elimination rule, and we need [ap\u2218] when [p] is a path-constructor of a\n higher-inductive type and [f] the eliminator.\n\n We handle both of these cases as follows:\n - [\u2218ap] is declared as a rewrite rule so that the first case is automatic\n - [ap\u2218] has the type of a rewrite rule but you should define a special case\n of it everytime you need it. In order to avoid the infinite loop, you need\n to do that only when the [ap f p] term already reduces.\n\n This pattern is repeated for the other variants below, and also in\n [Sigma] for the application of a function of two arguments to two paths.\n -}\n\n postulate\n \u2218ap : {x y : A} (p : x == y) \u2192\n ap g (ap f p) \u21a6 ap (\u03bb x \u2192 g (f x)) p\n {-# REWRITE \u2218ap #-}\n\n -- [ap f p] has to reduce before you declare this as a rewrite rule!\n ap\u2218 : {x y : A} (p : x == y) \u2192\n ap (\u03bb x \u2192 g (f x)) p \u21a6 ap g (ap f p)\n ap\u2218 p = idr\n\n postulate\n \u2218ap\u25a1 : {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b) \u2192\n ap\u25a1 g (ap\u25a1 f sq) \u21a6 ap\u25a1 (\u03bb x \u2192 g (f x)) sq\n {-# REWRITE \u2218ap\u25a1 #-}\n\n ap\u25a1\u2218 : {w x y z : A} {l : w == y} {r : x == z} {t : w == x} {b : y == z}\n (sq : Square l r t b) \u2192\n ap\u25a1 (\u03bb x \u2192 g (f x)) sq \u21a6 ap\u25a1 g (ap\u25a1 f sq)\n ap\u25a1\u2218 sq = idr\n\nmodule _ {i j k} {A : Type i} {B : A \u2192 Type j} {C : A \u2192 Type k} (g : {a : A} \u2192 B a \u2192 C a) (f : (a : A) \u2192 B a) where\n\n postulate\n \u2218ap\u2193 : {x y : A} (p : x == y) \u2192 ap\u2193 g {p = p} (ap f p) \u21a6 ap (\u03bb x \u2192 g (f x)) p\n {-# REWRITE \u2218ap\u2193 #-}\n\n ap\u2193\u2218 : {x y : A} (p : x == y) \u2192 ap (\u03bb x \u2192 g (f x)) p \u21a6 ap\u2193 g {p = p} (ap f p)\n ap\u2193\u2218 p = idr\n\nmodule _ {i j k} {A : Type i} {B : Type j} {C : B \u2192 Type k} (g : (b : B) \u2192 C b) (f : A \u2192 B) where\n\n postulate\n ap\u2191\u2218 : {x y : A} (p : x == y) \u2192 ap (\u03bb x \u2192 g (f x)) p \u21a6 ap g (ap f p)\n\npostulate\n ap\u2193-ap-\u2193-='-in : \u2200 {i} {A B C : Type i}\n {f g : A \u2192 B} (h : B \u2192 C) {x y : A} {p : x == y} {u : f x == g x} {v : f y == g y}\n (sq : Square u v (ap f p) (ap g p)) \u2192\n ap\u2193 (ap h) {p = p} (\u2193-='-in f g p sq) \u21a6 \u2193-==-eq-in idp (ap\u25a1 h sq) --\u2193-='-in (h \u2218 f) (h \u2218 g) p (ap\u25a1 h sq)\n{-# REWRITE ap\u2193-ap-\u2193-='-in #-}\n\npostulate\n ap\u25a1-\u2193-='-out : \u2200 {i} {A B C : Type i} (h : B \u2192 C)\n {f g : A \u2192 B} {x y : A} {p : x == y} {u : f x == g x} {v : f y == g y}\n (eq : u == v [ (\u03bb z \u2192 f z == g z) \u2193 p ]) \u2192\n ap\u25a1 h (\u2193-='-out f g p eq) \u21a6 \u2193-='-out (h \u2218 f) (h \u2218 g) p (ap\u2193 (ap h) {p = p} eq)\n{-# REWRITE ap\u25a1-\u2193-='-out #-}\n\n\n{- Function extensionality -}\n\nmodule _ {i} {A : Type i} {B : Type i} {f g : A \u2192 B} where\n\n postulate\n -- Intro\n funext3 : ({x y : A} (p : x == y) \u2192 f x == g y) \u2192 f == g\n -- Elim\n app= : f == g \u2192 {x y : A} \u2192 x == y \u2192 f x == g y\n -- \u03b2\n app=\u21a6 : (p : f == g) {x : A} \u2192 app= p idp \u21a6 ap (\u03bb h \u2192 h x) p\n {-# REWRITE app=\u21a6 #-}\n funext3-\u03b2 : (h : {x y : A} (p : x == y) \u2192 f x == g y) {x y : A} (p : x == y) \u2192 app= (funext3 h) p \u21a6 h p\n {-# REWRITE funext3-\u03b2 #-}\n funext3-\u03b2' : (h : {x y : A} (p : x == y) \u2192 f x == g y) (a : A) \u2192 ap (\u03bb h \u2192 h a) (funext3 h) \u21a6 h idp\n {-# REWRITE funext3-\u03b2' #-}\n \n\n funext-thing : (h : (x : A) \u2192 f x == g x) {x y : A} (p : x == y) \u2192 f x == g y\n funext-thing h p = diag (\u2193-='-out f g p (ap h p) :> Square (h _) (h _) (ap f p) (ap g p))\n\n funext : ((x : A) \u2192 f x == g x) \u2192 f == g\n funext h = funext3 (funext-thing h)\n \npostulate\n app\u25a1 : \u2200 {i} {A : Type i} {B : Type i}\n {w x y z : A \u2192 B} {l : w == y} {r : x == z} {t : w == x} {b : y == z} (sq : Square l r t b)\n {w' x' y' z' : A} {l' : w' == y'} {r' : x' == z'} {t' : w' == x'} {b' : y' == z'} (sq' : Square l' r' t' b')\n \u2192 Square (app= l l') (app= r r') (app= t t') (app= b b')\n\n app=idp : \u2200 {i} {A : Type i} {B : Type i} {f : A \u2192 B} {x y : A} (p : x == y) \u2192\n app= (idp {a = f}) p \u21a6 ap f p\n {-# REWRITE app=idp #-}\n\n app\u25a1-idh-idv : \u2200 {i} {A : Type i} {B : Type i} {f g : A \u2192 B} (q : f == g)\n {x y : A} (p : x == y) \u2192\n app\u25a1 (idh {p = q}) (idv {p = p}) \u21a6 \u2193-='-out f g p (ap (\u03bb z \u2192 app= q (idp {a = z})) p)\n {-# REWRITE app\u25a1-idh-idv #-}\n\n\n\n\n{- Natural numbers -}\n\ndata \u2115 : Type\u2080 where\n O : \u2115\n S : \u2115 \u2192 \u2115\n{-# BUILTIN NATURAL \u2115 #-}\n\n\n{- Booleans -}\n\ndata Bool : Type\u2080 where\n true false : Bool\n\nnot : Bool \u2192 Bool\nnot true = false\nnot false = true\n\nnotNot : (x : Bool) \u2192 not (not x) \u21a6 x\nnotNot true = idr\nnotNot false = idr\n{-# REWRITE notNot #-}\n\n\n{- Unit -}\n\nrecord Unit : Type\u2080 where\n constructor tt\n\n-- {- Function types -}\n\nmodule _ {i j k} {A : Type i} {B : A \u2192 Type j} {C : Type k} where\n\n postulate\n \u03bb\u2193 : {x y : A} (p : x == y) {u : B x \u2192 C} {v : B y \u2192 C} \u2192\n ({a : B x} {b : B y} (q : a == b [ B \u2193 p ]) \u2192 u a == v b) \u2192 u == v [ (\u03bb z \u2192 B z \u2192 C) \u2193 p ]\n\n app\u2193= : {x y : A} (p : x == y) {u : B x \u2192 C} {v : B y \u2192 C} \u2192\n u == v [ (\u03bb z \u2192 B z \u2192 C) \u2193 p ] \u2192 {b : B x} {b' : B y} (q : b == b' [ B \u2193 p ]) \u2192 u b == v b'\n\n postulate\n app\u2193\u25a1 : {wb xb yb zb : A} {lb : wb == yb} {rb : xb == zb} {tb : wb == xb} {bb : yb == zb} (sqb : Square lb rb tb bb)\n {w : B wb \u2192 C} {x : B xb \u2192 C} {y : B yb \u2192 C} {z : B zb \u2192 C} {l : w == y [ (\u03bb x \u2192 B x \u2192 C) \u2193 lb ]} {r : x == z [ (\u03bb x \u2192 B x \u2192 C) \u2193 rb ]} {t : w == x [ (\u03bb x \u2192 B x \u2192 C) \u2193 tb ]} {b : y == z [ (\u03bb x \u2192 B x \u2192 C) \u2193 bb ]} (sq : SquareOver (\u03bb x \u2192 B x \u2192 C) sqb l r t b)\n {w' : B wb} {x' : B xb} {y' : B yb} {z' : B zb} {l' : w' == y' [ B \u2193 lb ]} {r' : x' == z' [ B \u2193 rb ]} {t' : w' == x' [ B \u2193 tb ]} {b' : y' == z' [ B \u2193 bb ]} (sq' : SquareOver B sqb l' r' t' b')\n \u2192 Square (app\u2193= lb l l') (app\u2193= rb r r') (app\u2193= tb t t') (app\u2193= bb b b')\n\n app\u2193=-\u03bb\u2193 : {x y : A} (p : x == y) {u : B x \u2192 C} {v : B y \u2192 C} (h : {a : B x} {b : B y} \u2192 (a == b [ B \u2193 p ]) \u2192 u a == v b)\n {b : B x} {b' : B y} (r : b == b' [ B \u2193 p ])\n \u2192 app\u2193= p (\u03bb\u2193 p {v = v} h) r \u21a6 h r\n{-# REWRITE app\u2193=-\u03bb\u2193 #-}\n\\end{code}\n","avg_line_length":37.2893617021,"max_line_length":267,"alphanum_fraction":0.4258244893} +{"size":16985,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"PUC-Assignment4: PUC Assignment 4\"\nlayout : page\npermalink : \/PUC-Assignment4\/\n---\n\n\\begin{code}\nmodule PUC-Assignment4 where\n\\end{code}\n\n## YOUR NAME AND EMAIL GOES HERE\n\n\n## Introduction\n\nYou must do _all_ the exercises labelled \"(recommended)\".\n\nExercises labelled \"(stretch)\" are there to provide an extra challenge.\nYou don't need to do all of these, but should attempt at least a few.\n\nExercises without a label are optional, and may be done if you want\nsome extra practice.\n\nPlease ensure your files execute correctly under Agda!\n\n**IMPORTANT** For ease of marking, when modifying the given code please write\n\n -- begin\n -- end\n\nbefore and after code you add, to indicate your changes.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_)\nopen import Data.Product using (_\u00d7_; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.String using (String)\nopen import Data.String.Unsafe using (_\u225f_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\\end{code}\n\n\n## DeBruijn\n\n\n\\begin{code}\nmodule DeBruijn where\n\\end{code}\n\nRemember to indent all code by two spaces.\n\n\\begin{code}\n open import plfa.DeBruijn\n\\end{code}\n\n\n#### Exercise (`mul`) (recommended)\n\nWrite out the definition of a lambda term that multiplies\ntwo natural numbers, now adapted to the inherently typed\nDeBruijn representation.\n\n\n#### Exercise `V\u00ac\u2014\u2192`\n\nFollowing the previous development, show values do\nnot reduce, and its corollary, terms that reduce are not\nvalues.\n\n\n#### Exercise `mul-example` (recommended)\n\nUsing the evaluator, confirm that two times two is four.\n\n\n## More\n\n\\begin{code}\nmodule More where\n\\end{code}\n\nRemember to indent all code by two spaces.\n\n\n### Syntax\n\n\\begin{code}\n infix 4 _\u22a2_\n infix 4 _\u220b_\n infixl 5 _,_\n\n infixr 7 _\u21d2_\n infixr 8 _`\u228e_\n infixr 9 _`\u00d7_\n\n infix 5 \u019b_\n infix 5 \u03bc_\n infixl 7 _\u00b7_\n infixl 8 _`*_\n infix 8 `suc_\n infix 9 `_\n infix 9 S_\n infix 9 #_\n\\end{code}\n\n### Types\n\n\\begin{code}\n data Type : Set where\n `\u2115 : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n Nat : Type\n _`\u00d7_ : Type \u2192 Type \u2192 Type\n _`\u228e_ : Type \u2192 Type \u2192 Type\n `\u22a4 : Type\n `\u22a5 : Type\n `List : Type \u2192 Type\n\\end{code}\n\n### Contexts\n\n\\begin{code}\n data Context : Set where\n \u2205 : Context\n _,_ : Context \u2192 Type \u2192 Context\n\\end{code}\n\n### Variables and the lookup judgment\n\n\\begin{code}\n data _\u220b_ : Context \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 A}\n ---------\n \u2192 \u0393 , A \u220b A\n\n S_ : \u2200 {\u0393 A B}\n \u2192 \u0393 \u220b B\n ---------\n \u2192 \u0393 , A \u220b B\n\\end{code} \n\n### Terms and the typing judgment\n\n\\begin{code}\n data _\u22a2_ : Context \u2192 Type \u2192 Set where\n\n -- variables\n\n `_ : \u2200 {\u0393 A}\n \u2192 \u0393 \u220b A\n -----\n \u2192 \u0393 \u22a2 A\n\n -- functions\n\n \u019b_ : \u2200 {\u0393 A B}\n \u2192 \u0393 , A \u22a2 B\n ---------\n \u2192 \u0393 \u22a2 A \u21d2 B\n\n _\u00b7_ : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A \u21d2 B\n \u2192 \u0393 \u22a2 A\n ---------\n \u2192 \u0393 \u22a2 B\n\n -- naturals\n\n `zero : \u2200 {\u0393}\n ------\n \u2192 \u0393 \u22a2 `\u2115\n\n `suc_ : \u2200 {\u0393}\n \u2192 \u0393 \u22a2 `\u2115\n ------\n \u2192 \u0393 \u22a2 `\u2115\n\n case : \u2200 {\u0393 A}\n \u2192 \u0393 \u22a2 `\u2115\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 , `\u2115 \u22a2 A\n -----\n \u2192 \u0393 \u22a2 A\n\n -- fixpoint\n\n \u03bc_ : \u2200 {\u0393 A}\n \u2192 \u0393 , A \u22a2 A\n ----------\n \u2192 \u0393 \u22a2 A\n\n -- primitive numbers\n\n con : \u2200 {\u0393}\n \u2192 \u2115\n -------\n \u2192 \u0393 \u22a2 Nat\n\n _`*_ : \u2200 {\u0393}\n \u2192 \u0393 \u22a2 Nat\n \u2192 \u0393 \u22a2 Nat\n -------\n \u2192 \u0393 \u22a2 Nat\n\n -- let\n\n `let : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 , A \u22a2 B\n ----------\n \u2192 \u0393 \u22a2 B\n\n -- products\n\n `\u27e8_,_\u27e9 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 \u22a2 B\n -----------\n \u2192 \u0393 \u22a2 A `\u00d7 B\n\n `proj\u2081 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n -----------\n \u2192 \u0393 \u22a2 A\n\n `proj\u2082 : \u2200 {\u0393 A B}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n -----------\n \u2192 \u0393 \u22a2 B\n\n -- alternative formulation of products\n\n case\u00d7 : \u2200 {\u0393 A B C}\n \u2192 \u0393 \u22a2 A `\u00d7 B\n \u2192 \u0393 , A , B \u22a2 C\n --------------\n \u2192 \u0393 \u22a2 C\n\n\\end{code}\n\n### Abbreviating de Bruijn indices\n\n\\begin{code}\n lookup : Context \u2192 \u2115 \u2192 Type\n lookup (\u0393 , A) zero = A\n lookup (\u0393 , _) (suc n) = lookup \u0393 n\n lookup \u2205 _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n count : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 \u0393 \u220b lookup \u0393 n\n count {\u0393 , _} zero = Z\n count {\u0393 , _} (suc n) = S (count n)\n count {\u2205} _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\n #_ : \u2200 {\u0393} \u2192 (n : \u2115) \u2192 \u0393 \u22a2 lookup \u0393 n\n # n = ` count n\n\\end{code}\n\n## Renaming\n\n\\begin{code}\n ext : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A) \u2192 (\u2200 {A B} \u2192 \u0393 , A \u220b B \u2192 \u0394 , A \u220b B)\n ext \u03c1 Z = Z\n ext \u03c1 (S x) = S (\u03c1 x)\n\n rename : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A) \u2192 (\u2200 {A} \u2192 \u0393 \u22a2 A \u2192 \u0394 \u22a2 A)\n rename \u03c1 (` x) = ` (\u03c1 x)\n rename \u03c1 (\u019b N) = \u019b (rename (ext \u03c1) N)\n rename \u03c1 (L \u00b7 M) = (rename \u03c1 L) \u00b7 (rename \u03c1 M)\n rename \u03c1 (`zero) = `zero\n rename \u03c1 (`suc M) = `suc (rename \u03c1 M)\n rename \u03c1 (case L M N) = case (rename \u03c1 L) (rename \u03c1 M) (rename (ext \u03c1) N)\n rename \u03c1 (\u03bc N) = \u03bc (rename (ext \u03c1) N)\n rename \u03c1 (con n) = con n\n rename \u03c1 (M `* N) = rename \u03c1 M `* rename \u03c1 N\n rename \u03c1 (`let M N) = `let (rename \u03c1 M) (rename (ext \u03c1) N)\n rename \u03c1 `\u27e8 M , N \u27e9 = `\u27e8 rename \u03c1 M , rename \u03c1 N \u27e9\n rename \u03c1 (`proj\u2081 L) = `proj\u2081 (rename \u03c1 L)\n rename \u03c1 (`proj\u2082 L) = `proj\u2082 (rename \u03c1 L)\n rename \u03c1 (case\u00d7 L M) = case\u00d7 (rename \u03c1 L) (rename (ext (ext \u03c1)) M)\n\\end{code}\n\n## Simultaneous Substitution\n\n\\begin{code}\n exts : \u2200 {\u0393 \u0394} \u2192 (\u2200 {A} \u2192 \u0393 \u220b A \u2192 \u0394 \u22a2 A) \u2192 (\u2200 {A B} \u2192 \u0393 , A \u220b B \u2192 \u0394 , A \u22a2 B)\n exts \u03c3 Z = ` Z\n exts \u03c3 (S x) = rename S_ (\u03c3 x)\n\n subst : \u2200 {\u0393 \u0394} \u2192 (\u2200 {C} \u2192 \u0393 \u220b C \u2192 \u0394 \u22a2 C) \u2192 (\u2200 {C} \u2192 \u0393 \u22a2 C \u2192 \u0394 \u22a2 C)\n subst \u03c3 (` k) = \u03c3 k\n subst \u03c3 (\u019b N) = \u019b (subst (exts \u03c3) N)\n subst \u03c3 (L \u00b7 M) = (subst \u03c3 L) \u00b7 (subst \u03c3 M)\n subst \u03c3 (`zero) = `zero\n subst \u03c3 (`suc M) = `suc (subst \u03c3 M)\n subst \u03c3 (case L M N) = case (subst \u03c3 L) (subst \u03c3 M) (subst (exts \u03c3) N)\n subst \u03c3 (\u03bc N) = \u03bc (subst (exts \u03c3) N)\n subst \u03c3 (con n) = con n\n subst \u03c3 (M `* N) = subst \u03c3 M `* subst \u03c3 N\n subst \u03c3 (`let M N) = `let (subst \u03c3 M) (subst (exts \u03c3) N)\n subst \u03c3 `\u27e8 M , N \u27e9 = `\u27e8 subst \u03c3 M , subst \u03c3 N \u27e9\n subst \u03c3 (`proj\u2081 L) = `proj\u2081 (subst \u03c3 L)\n subst \u03c3 (`proj\u2082 L) = `proj\u2082 (subst \u03c3 L)\n subst \u03c3 (case\u00d7 L M) = case\u00d7 (subst \u03c3 L) (subst (exts (exts \u03c3)) M)\n\\end{code}\n\n## Single and double substitution\n\n\\begin{code}\n _[_] : \u2200 {\u0393 A B}\n \u2192 \u0393 , A \u22a2 B\n \u2192 \u0393 \u22a2 A\n ------------\n \u2192 \u0393 \u22a2 B\n _[_] {\u0393} {A} N V = subst {\u0393 , A} {\u0393} \u03c3 N\n where\n \u03c3 : \u2200 {B} \u2192 \u0393 , A \u220b B \u2192 \u0393 \u22a2 B\n \u03c3 Z = V\n \u03c3 (S x) = ` x\n\n _[_][_] : \u2200 {\u0393 A B C}\n \u2192 \u0393 , A , B \u22a2 C\n \u2192 \u0393 \u22a2 A\n \u2192 \u0393 \u22a2 B\n ---------------\n \u2192 \u0393 \u22a2 C\n _[_][_] {\u0393} {A} {B} N V W = subst {\u0393 , A , B} {\u0393} \u03c3 N\n where\n \u03c3 : \u2200 {C} \u2192 \u0393 , A , B \u220b C \u2192 \u0393 \u22a2 C\n \u03c3 Z = W\n \u03c3 (S Z) = V\n \u03c3 (S (S x)) = ` x\n\\end{code}\n\n## Values\n\n\\begin{code}\n data Value : \u2200 {\u0393 A} \u2192 \u0393 \u22a2 A \u2192 Set where\n\n -- functions\n\n V-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B}\n ---------------------------\n \u2192 Value (\u019b N)\n\n -- naturals\n\n V-zero : \u2200 {\u0393} \u2192\n -----------------\n Value (`zero {\u0393})\n\n V-suc_ : \u2200 {\u0393} {V : \u0393 \u22a2 `\u2115}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n\n -- primitives\n\n V-con : \u2200 {\u0393 n}\n ---------------------\n \u2192 Value {\u0393 = \u0393} (con n)\n\n -- products\n\n V-\u27e8_,_\u27e9 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------\n \u2192 Value `\u27e8 V , W \u27e9\n\\end{code}\n\nImplicit arguments need to be supplied when they are\nnot fixed by the given arguments.\n\n## Reduction\n\n\\begin{code}\n infix 2 _\u2014\u2192_\n\n data _\u2014\u2192_ : \u2200 {\u0393 A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n -- functions\n\n \u03be-\u00b7\u2081 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A \u21d2 B} {M : \u0393 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n ---------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A \u21d2 B} {M M\u2032 : \u0393 \u22a2 A}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n ---------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03b2-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B} {V : \u0393 \u22a2 A}\n \u2192 Value V\n --------------------\n \u2192 (\u019b N) \u00b7 V \u2014\u2192 N [ V ]\n\n -- naturals\n\n \u03be-suc : \u2200 {\u0393} {M M\u2032 : \u0393 \u22a2 `\u2115}\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 `suc M \u2014\u2192 `suc M\u2032\n\n \u03be-case : \u2200 {\u0393 A} {L L\u2032 : \u0393 \u22a2 `\u2115} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n -------------------------\n \u2192 case L M N \u2014\u2192 case L\u2032 M N\n\n \u03b2-zero : \u2200 {\u0393 A} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n -------------------\n \u2192 case `zero M N \u2014\u2192 M\n\n \u03b2-suc : \u2200 {\u0393 A} {V : \u0393 \u22a2 `\u2115} {M : \u0393 \u22a2 A} {N : \u0393 , `\u2115 \u22a2 A}\n \u2192 Value V\n ----------------------------\n \u2192 case (`suc V) M N \u2014\u2192 N [ V ]\n\n -- fixpoint\n\n \u03b2-\u03bc : \u2200 {\u0393 A} {N : \u0393 , A \u22a2 A}\n ----------------\n \u2192 \u03bc N \u2014\u2192 N [ \u03bc N ]\n\n -- primitive numbers\n\n \u03be-*\u2081 : \u2200 {\u0393} {L L\u2032 M : \u0393 \u22a2 Nat}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L `* M \u2014\u2192 L\u2032 `* M\n\n \u03be-*\u2082 : \u2200 {\u0393} {V M M\u2032 : \u0393 \u22a2 Nat}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n -----------------\n \u2192 V `* M \u2014\u2192 V `* M\u2032\n\n \u03b4-* : \u2200 {\u0393 c d}\n -------------------------------------\n \u2192 con {\u0393 = \u0393} c `* con d \u2014\u2192 con (c * d)\n\n -- let\n\n \u03be-let : \u2200 {\u0393 A B} {M M\u2032 : \u0393 \u22a2 A} {N : \u0393 , A \u22a2 B}\n \u2192 M \u2014\u2192 M\u2032\n ---------------------\n \u2192 `let M N \u2014\u2192 `let M\u2032 N\n\n \u03b2-let : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {N : \u0393 , A \u22a2 B}\n \u2192 Value V\n -------------------\n \u2192 `let V N \u2014\u2192 N [ V ]\n\n -- products\n\n \u03be-\u27e8,\u27e9\u2081 : \u2200 {\u0393 A B} {M M\u2032 : \u0393 \u22a2 A} {N : \u0393 \u22a2 B}\n \u2192 M \u2014\u2192 M\u2032\n -------------------------\n \u2192 `\u27e8 M , N \u27e9 \u2014\u2192 `\u27e8 M\u2032 , N \u27e9\n\n \u03be-\u27e8,\u27e9\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {N N\u2032 : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 N \u2014\u2192 N\u2032\n -------------------------\n \u2192 `\u27e8 V , N \u27e9 \u2014\u2192 `\u27e8 V , N\u2032 \u27e9\n\n \u03be-proj\u2081 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B}\n \u2192 L \u2014\u2192 L\u2032\n ---------------------\n \u2192 `proj\u2081 L \u2014\u2192 `proj\u2081 L\u2032\n\n \u03be-proj\u2082 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B}\n \u2192 L \u2014\u2192 L\u2032\n ---------------------\n \u2192 `proj\u2082 L \u2014\u2192 `proj\u2082 L\u2032\n\n \u03b2-proj\u2081 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------------\n \u2192 `proj\u2081 `\u27e8 V , W \u27e9 \u2014\u2192 V\n\n \u03b2-proj\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B}\n \u2192 Value V\n \u2192 Value W\n ----------------------\n \u2192 `proj\u2082 `\u27e8 V , W \u27e9 \u2014\u2192 W\n\n -- alternative formulation of products\n\n \u03be-case\u00d7 : \u2200 {\u0393 A B C} {L L\u2032 : \u0393 \u22a2 A `\u00d7 B} {M : \u0393 , A , B \u22a2 C}\n \u2192 L \u2014\u2192 L\u2032\n -----------------------\n \u2192 case\u00d7 L M \u2014\u2192 case\u00d7 L\u2032 M\n\n \u03b2-case\u00d7 : \u2200 {\u0393 A B C} {V : \u0393 \u22a2 A} {W : \u0393 \u22a2 B} {M : \u0393 , A , B \u22a2 C}\n \u2192 Value V\n \u2192 Value W\n ----------------------------------\n \u2192 case\u00d7 `\u27e8 V , W \u27e9 M \u2014\u2192 M [ V ][ W ]\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\n infix 2 _\u2014\u21a0_\n infix 1 begin_\n infixr 2 _\u2014\u2192\u27e8_\u27e9_\n infix 3 _\u220e\n\n data _\u2014\u21a0_ : \u2200 {\u0393 A} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n _\u220e : \u2200 {\u0393 A} (M : \u0393 \u22a2 A)\n --------\n \u2192 M \u2014\u21a0 M\n\n _\u2014\u2192\u27e8_\u27e9_ : \u2200 {\u0393 A} (L : \u0393 \u22a2 A) {M N : \u0393 \u22a2 A}\n \u2192 L \u2014\u2192 M\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 L \u2014\u21a0 N\n\n begin_ : \u2200 {\u0393} {A} {M N : \u0393 \u22a2 A}\n \u2192 M \u2014\u21a0 N\n ------\n \u2192 M \u2014\u21a0 N\n begin M\u2014\u21a0N = M\u2014\u21a0N\n\\end{code}\n\n\n## Values do not reduce\n\n\\begin{code}\n V\u00ac\u2014\u2192 : \u2200 {\u0393 A} {M N : \u0393 \u22a2 A}\n \u2192 Value M\n ----------\n \u2192 \u00ac (M \u2014\u2192 N)\n V\u00ac\u2014\u2192 V-\u019b ()\n V\u00ac\u2014\u2192 V-zero ()\n V\u00ac\u2014\u2192 (V-suc VM) (\u03be-suc M\u2014\u2192M\u2032) = V\u00ac\u2014\u2192 VM M\u2014\u2192M\u2032\n V\u00ac\u2014\u2192 V-con ()\n V\u00ac\u2014\u2192 V-\u27e8 VM , _ \u27e9 (\u03be-\u27e8,\u27e9\u2081 M\u2014\u2192M\u2032) = V\u00ac\u2014\u2192 VM M\u2014\u2192M\u2032\n V\u00ac\u2014\u2192 V-\u27e8 _ , VN \u27e9 (\u03be-\u27e8,\u27e9\u2082 _ N\u2014\u2192N\u2032) = V\u00ac\u2014\u2192 VN N\u2014\u2192N\u2032\n\\end{code}\n\n\n## Progress\n\n\\begin{code}\n data Progress {A} (M : \u2205 \u22a2 A) : Set where\n\n step : \u2200 {N : \u2205 \u22a2 A}\n \u2192 M \u2014\u2192 N\n ----------\n \u2192 Progress M\n\n done :\n Value M\n ----------\n \u2192 Progress M\n\n progress : \u2200 {A}\n \u2192 (M : \u2205 \u22a2 A)\n -----------\n \u2192 Progress M\n progress (` ())\n progress (\u019b N) = done V-\u019b\n progress (L \u00b7 M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-\u00b7\u2081 L\u2014\u2192L\u2032)\n ... | done V-\u019b with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-\u00b7\u2082 V-\u019b M\u2014\u2192M\u2032)\n ... | done VM = step (\u03b2-\u019b VM)\n progress (`zero) = done V-zero\n progress (`suc M) with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-suc M\u2014\u2192M\u2032)\n ... | done VM = done (V-suc VM)\n progress (case L M N) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case L\u2014\u2192L\u2032)\n ... | done V-zero = step \u03b2-zero\n ... | done (V-suc VL) = step (\u03b2-suc VL)\n progress (\u03bc N) = step \u03b2-\u03bc\n progress (con n) = done V-con\n progress (L `* M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-*\u2081 L\u2014\u2192L\u2032)\n ... | done V-con with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-*\u2082 V-con M\u2014\u2192M\u2032)\n ... | done V-con = step \u03b4-*\n progress (`let M N) with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-let M\u2014\u2192M\u2032)\n ... | done VM = step (\u03b2-let VM)\n progress `\u27e8 M , N \u27e9 with progress M\n ... | step M\u2014\u2192M\u2032 = step (\u03be-\u27e8,\u27e9\u2081 M\u2014\u2192M\u2032)\n ... | done VM with progress N\n ... | step N\u2014\u2192N\u2032 = step (\u03be-\u27e8,\u27e9\u2082 VM N\u2014\u2192N\u2032)\n ... | done VN = done (V-\u27e8 VM , VN \u27e9)\n progress (`proj\u2081 L) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-proj\u2081 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-proj\u2081 VM VN)\n progress (`proj\u2082 L) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-proj\u2082 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-proj\u2082 VM VN)\n progress (case\u00d7 L M) with progress L\n ... | step L\u2014\u2192L\u2032 = step (\u03be-case\u00d7 L\u2014\u2192L\u2032)\n ... | done (V-\u27e8 VM , VN \u27e9) = step (\u03b2-case\u00d7 VM VN)\n\\end{code}\n\n\n## Evaluation\n\n\\begin{code}\n data Gas : Set where\n gas : \u2115 \u2192 Gas\n\n data Finished {\u0393 A} (N : \u0393 \u22a2 A) : Set where\n\n done :\n Value N\n ----------\n \u2192 Finished N\n\n out-of-gas :\n ----------\n Finished N\n\n data Steps : \u2200 {A} \u2192 \u2205 \u22a2 A \u2192 Set where\n\n steps : \u2200 {A} {L N : \u2205 \u22a2 A}\n \u2192 L \u2014\u21a0 N\n \u2192 Finished N\n ----------\n \u2192 Steps L\n\n eval : \u2200 {A}\n \u2192 Gas\n \u2192 (L : \u2205 \u22a2 A)\n -----------\n \u2192 Steps L\n eval (gas zero) L = steps (L \u220e) out-of-gas\n eval (gas (suc m)) L with progress L\n ... | done VL = steps (L \u220e) (done VL)\n ... | step {M} L\u2014\u2192M with eval (gas m) M\n ... | steps M\u2014\u21a0N fin = steps (L \u2014\u2192\u27e8 L\u2014\u2192M \u27e9 M\u2014\u21a0N) fin\n\\end{code}\n\n## Examples\n\n\\begin{code}\n cube : \u2205 \u22a2 Nat \u21d2 Nat\n cube = \u019b (# 0 `* # 0 `* # 0)\n\n _ : cube \u00b7 con 2 \u2014\u21a0 con 8\n _ = \n begin\n cube \u00b7 con 2\n \u2014\u2192\u27e8 \u03b2-\u019b V-con \u27e9\n con 2 `* con 2 `* con 2\n \u2014\u2192\u27e8 \u03be-*\u2081 \u03b4-* \u27e9\n con 4 `* con 2\n \u2014\u2192\u27e8 \u03b4-* \u27e9\n con 8\n \u220e\n\n exp10 : \u2205 \u22a2 Nat \u21d2 Nat\n exp10 = \u019b (`let (# 0 `* # 0)\n (`let (# 0 `* # 0)\n (`let (# 0 `* # 2)\n (# 0 `* # 0))))\n\n _ : exp10 \u00b7 con 2 \u2014\u21a0 con 1024\n _ =\n begin\n exp10 \u00b7 con 2\n \u2014\u2192\u27e8 \u03b2-\u019b V-con \u27e9\n `let (con 2 `* con 2) (`let (# 0 `* # 0) (`let (# 0 `* con 2) (# 0 `* # 0)))\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 4) (`let (# 0 `* # 0) (`let (# 0 `* con 2) (# 0 `* # 0)))\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n `let (con 4 `* con 4) (`let (# 0 `* con 2) (# 0 `* # 0))\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 16) (`let (# 0 `* con 2) (# 0 `* # 0))\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n `let (con 16 `* con 2) (# 0 `* # 0)\n \u2014\u2192\u27e8 \u03be-let \u03b4-* \u27e9\n `let (con 32) (# 0 `* # 0)\n \u2014\u2192\u27e8 \u03b2-let V-con \u27e9\n con 32 `* con 32\n \u2014\u2192\u27e8 \u03b4-* \u27e9\n con 1024\n \u220e\n\n swap\u00d7 : \u2200 {A B} \u2192 \u2205 \u22a2 A `\u00d7 B \u21d2 B `\u00d7 A\n swap\u00d7 = \u019b `\u27e8 `proj\u2082 (# 0) , `proj\u2081 (# 0) \u27e9\n\n _ : swap\u00d7 \u00b7 `\u27e8 con 42 , `zero \u27e9 \u2014\u21a0 `\u27e8 `zero , con 42 \u27e9\n _ =\n begin\n swap\u00d7 \u00b7 `\u27e8 con 42 , `zero \u27e9\n \u2014\u2192\u27e8 \u03b2-\u019b V-\u27e8 V-con , V-zero \u27e9 \u27e9\n `\u27e8 `proj\u2082 `\u27e8 con 42 , `zero \u27e9 , `proj\u2081 `\u27e8 con 42 , `zero \u27e9 \u27e9\n \u2014\u2192\u27e8 \u03be-\u27e8,\u27e9\u2081 (\u03b2-proj\u2082 V-con V-zero) \u27e9\n `\u27e8 `zero , `proj\u2081 `\u27e8 con 42 , `zero \u27e9 \u27e9\n \u2014\u2192\u27e8 \u03be-\u27e8,\u27e9\u2082 V-zero (\u03b2-proj\u2081 V-con V-zero) \u27e9\n `\u27e8 `zero , con 42 \u27e9\n \u220e\n\n swap\u00d7-case : \u2200 {A B} \u2192 \u2205 \u22a2 A `\u00d7 B \u21d2 B `\u00d7 A\n swap\u00d7-case = \u019b case\u00d7 (# 0) `\u27e8 # 0 , # 1 \u27e9\n\n _ : swap\u00d7-case \u00b7 `\u27e8 con 42 , `zero \u27e9 \u2014\u21a0 `\u27e8 `zero , con 42 \u27e9\n _ =\n begin\n swap\u00d7-case \u00b7 `\u27e8 con 42 , `zero \u27e9\n \u2014\u2192\u27e8 \u03b2-\u019b V-\u27e8 V-con , V-zero \u27e9 \u27e9\n case\u00d7 `\u27e8 con 42 , `zero \u27e9 `\u27e8 # 0 , # 1 \u27e9\n \u2014\u2192\u27e8 \u03b2-case\u00d7 V-con V-zero \u27e9\n `\u27e8 `zero , con 42 \u27e9\n \u220e\n\\end{code}\n\n\n#### Exercise `More` (recommended in part)\n\nFormalise the remaining constructs defined in this chapter.\nEvaluate each example, applied to data as needed,\nto confirm it returns the expected answer.\n\n * sums (recommended)\n * unit type\n * an alternative formulation of unit type\n * empty type (recommended)\n * lists\n\n\n","avg_line_length":22.8600269179,"max_line_length":82,"alphanum_fraction":0.3846335001} +{"size":22786,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Typed: Raw terms with types (broken)\"\npermalink : \/Typed\n---\n\nThis version uses raw terms. Substitution presumes that no\ngeneration of fresh names is required.\n\nThe substitution algorithm is based on one by McBride.\nIt is given a map from names to terms. Say the mapping of a\nname is trivial if it takes a name to a term consisting of\njust the variable with that name. No fresh names are required\nif the mapping on each variable is either trivial or to a\nclosed term. However, the proof of correctness currently\ncontains a hole, and may be difficult to repair.\n\n## Imports\n\n\\begin{code}\nmodule Typed where\n\\end{code}\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr; filter)\nopen import Data.Nat using (\u2115; zero; suc; _+_)\nopen import Data.String using (String; _\u225f_)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Function using (_\u2218_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Negation using (\u00ac?)\nimport Collections\n\npattern [_] x = x \u2237 []\npattern [_,_] x y = x \u2237 y \u2237 []\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\n\\end{code}\n\n\n## Syntax\n\n\\begin{code}\ninfix 4 _\u220b_`:_\ninfix 4 _\u22a2_`:_\ninfixl 5 _,_`:_\ninfixr 6 _`\u2192_\ninfix 6 `\u03bb_`\u2192_\ninfixl 7 `if0_then_else_\ninfix 8 `suc_ `pred_ `Y_\ninfixl 9 _\u00b7_\n\nId : Set\nId = String\n\ndata Type : Set where\n `\u2115 : Type\n _`\u2192_ : Type \u2192 Type \u2192 Type\n\ndata Env : Set where\n \u03b5 : Env\n _,_`:_ : Env \u2192 Id \u2192 Type \u2192 Env\n\ndata Term : Set where\n `_ : Id \u2192 Term\n `\u03bb_`\u2192_ : Id \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n `zero : Term\n `suc_ : Term \u2192 Term\n `pred_ : Term \u2192 Term\n `if0_then_else_ : Term \u2192 Term \u2192 Term \u2192 Term\n `Y_ : Term \u2192 Term\n\ndata _\u220b_`:_ : Env \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 A x}\n --------------------\n \u2192 \u0393 , x `: A \u220b x `: A\n\n S : \u2200 {\u0393 A B x w}\n \u2192 w \u2262 x\n \u2192 \u0393 \u220b w `: B\n --------------------\n \u2192 \u0393 , x `: A \u220b w `: B\n\ndata _\u22a2_`:_ : Env \u2192 Term \u2192 Type \u2192 Set where\n\n Ax : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x `: A\n --------------\n \u2192 \u0393 \u22a2 ` x `: A\n\n \u22a2\u03bb : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x `: A \u22a2 N `: B\n --------------------------\n \u2192 \u0393 \u22a2 (`\u03bb x `\u2192 N) `: A `\u2192 B\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L `: A `\u2192 B\n \u2192 \u0393 \u22a2 M `: A\n ----------------\n \u2192 \u0393 \u22a2 L \u00b7 M `: B\n\n \u22a2zero : \u2200 {\u0393}\n ----------------\n \u2192 \u0393 \u22a2 `zero `: `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M `: `\u2115\n -----------------\n \u2192 \u0393 \u22a2 `suc M `: `\u2115\n\n \u22a2pred : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M `: `\u2115\n ------------------\n \u2192 \u0393 \u22a2 `pred M `: `\u2115\n\n \u22a2if0 : \u2200 {\u0393 L M N A}\n \u2192 \u0393 \u22a2 L `: `\u2115\n \u2192 \u0393 \u22a2 M `: A\n \u2192 \u0393 \u22a2 N `: A\n ------------------------------\n \u2192 \u0393 \u22a2 `if0 L then M else N `: A\n\n \u22a2Y : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M `: A `\u2192 A\n ----------------\n \u2192 \u0393 \u22a2 `Y M `: A\n\\end{code}\n\n## Test examples\n\n\\begin{code}\ns\u2262z : \"s\" \u2262 \"z\"\ns\u2262z ()\n\nn\u2262z : \"n\" \u2262 \"z\"\nn\u2262z ()\n\nn\u2262s : \"n\" \u2262 \"s\"\nn\u2262s ()\n\nm\u2262z : \"m\" \u2262 \"z\"\nm\u2262z ()\n\nm\u2262s : \"m\" \u2262 \"s\"\nm\u2262s ()\n\nm\u2262n : \"m\" \u2262 \"n\"\nm\u2262n ()\n\np\u2262n : \"p\" \u2262 \"n\"\np\u2262n ()\n\np\u2262m : \"p\" \u2262 \"m\"\np\u2262m ()\n\ntwo : Term\ntwo = `suc `suc `zero\n\n\u22a2two : \u03b5 \u22a2 two `: `\u2115\n\u22a2two = \u22a2suc (\u22a2suc \u22a2zero)\n\nplus : Term\nplus = `Y (`\u03bb \"p\" `\u2192 `\u03bb \"m\" `\u2192 `\u03bb \"n\" `\u2192\n `if0 ` \"m\" then ` \"n\" else `suc (` \"p\" \u00b7 (`pred (` \"m\")) \u00b7 ` \"n\"))\n\n\u22a2plus : \u03b5 \u22a2 plus `: `\u2115 `\u2192 `\u2115 `\u2192 `\u2115\n\u22a2plus = (\u22a2Y (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2if0 (Ax \u22a2m) (Ax \u22a2n) (\u22a2suc (Ax \u22a2p \u00b7 (\u22a2pred (Ax \u22a2m)) \u00b7 Ax \u22a2n)))))))\n where\n \u22a2p = S p\u2262n (S p\u2262m Z)\n \u22a2m = S m\u2262n Z\n \u22a2n = Z\n\nfour : Term\nfour = plus \u00b7 two \u00b7 two\n\n\u22a2four : \u03b5 \u22a2 four `: `\u2115\n\u22a2four = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\n\nCh : Type\nCh = (`\u2115 `\u2192 `\u2115) `\u2192 `\u2115 `\u2192 `\u2115\n\ntwoCh : Term\ntwoCh = `\u03bb \"s\" `\u2192 `\u03bb \"z\" `\u2192 (` \"s\" \u00b7 (` \"s\" \u00b7 ` \"z\"))\n\n\u22a2twoCh : \u03b5 \u22a2 twoCh `: Ch\n\u22a2twoCh = (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2s \u00b7 (Ax \u22a2s \u00b7 Ax \u22a2z))))\n where\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nplusCh : Term\nplusCh = `\u03bb \"m\" `\u2192 `\u03bb \"n\" `\u2192 `\u03bb \"s\" `\u2192 `\u03bb \"z\" `\u2192\n ` \"m\" \u00b7 ` \"s\" \u00b7 (` \"n\" \u00b7 ` \"s\" \u00b7 ` \"z\")\n\n\u22a2plusCh : \u03b5 \u22a2 plusCh `: Ch `\u2192 Ch `\u2192 Ch\n\u22a2plusCh = (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2m \u00b7 Ax \u22a2s \u00b7 (Ax \u22a2n \u00b7 Ax \u22a2s \u00b7 Ax \u22a2z))))))\n where\n \u22a2m = S m\u2262z (S m\u2262s (S m\u2262n Z))\n \u22a2n = S n\u2262z (S n\u2262s Z)\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nfromCh : Term\nfromCh = `\u03bb \"m\" `\u2192 ` \"m\" \u00b7 (`\u03bb \"s\" `\u2192 `suc ` \"s\") \u00b7 `zero\n\n\u22a2fromCh : \u03b5 \u22a2 fromCh `: Ch `\u2192 `\u2115\n\u22a2fromCh = (\u22a2\u03bb (Ax \u22a2m \u00b7 (\u22a2\u03bb (\u22a2suc (Ax \u22a2s))) \u00b7 \u22a2zero))\n where\n \u22a2m = Z\n \u22a2s = Z\n\nfourCh : Term\nfourCh = fromCh \u00b7 (plusCh \u00b7 twoCh \u00b7 twoCh)\n\n\u22a2fourCh : \u03b5 \u22a2 fourCh `: `\u2115\n\u22a2fourCh = \u22a2fromCh \u00b7 (\u22a2plusCh \u00b7 \u22a2twoCh \u00b7 \u22a2twoCh)\n\\end{code}\n\n\n## Erasure\n\n\\begin{code}\nlookup : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x `: A \u2192 Id\nlookup {\u0393 , x `: A} Z = x\nlookup {\u0393 , x `: A} (S w\u2262 \u22a2w) = lookup {\u0393} \u22a2w\n\nerase : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M `: A \u2192 Term\nerase (Ax \u22a2w) = ` lookup \u22a2w\nerase (\u22a2\u03bb {x = x} \u22a2N) = `\u03bb x `\u2192 erase \u22a2N\nerase (\u22a2L \u00b7 \u22a2M) = erase \u22a2L \u00b7 erase \u22a2M\nerase (\u22a2zero) = `zero\nerase (\u22a2suc \u22a2M) = `suc (erase \u22a2M)\nerase (\u22a2pred \u22a2M) = `pred (erase \u22a2M)\nerase (\u22a2if0 \u22a2L \u22a2M \u22a2N) = `if0 (erase \u22a2L) then (erase \u22a2M) else (erase \u22a2N)\nerase (\u22a2Y \u22a2M) = `Y (erase \u22a2M)\n\\end{code}\n\n### Properties of erasure\n\n\\begin{code}\ncong\u2083 : \u2200 {A B C D : Set} (f : A \u2192 B \u2192 C \u2192 D) {s t u v x y} \u2192\n s \u2261 t \u2192 u \u2261 v \u2192 x \u2261 y \u2192 f s u x \u2261 f t v y\ncong\u2083 f refl refl refl = refl\n\nlookup-lemma : \u2200 {\u0393 x A} \u2192 (\u22a2x : \u0393 \u220b x `: A) \u2192 lookup \u22a2x \u2261 x\nlookup-lemma Z = refl\nlookup-lemma (S w\u2262 \u22a2w) = lookup-lemma \u22a2w\n\nerase-lemma : \u2200 {\u0393 M A} \u2192 (\u22a2M : \u0393 \u22a2 M `: A) \u2192 erase \u22a2M \u2261 M\nerase-lemma (Ax \u22a2x) = cong `_ (lookup-lemma \u22a2x)\nerase-lemma (\u22a2\u03bb {x = x} \u22a2N) = cong (`\u03bb x `\u2192_) (erase-lemma \u22a2N)\nerase-lemma (\u22a2L \u00b7 \u22a2M) = cong\u2082 _\u00b7_ (erase-lemma \u22a2L) (erase-lemma \u22a2M)\nerase-lemma (\u22a2zero) = refl\nerase-lemma (\u22a2suc \u22a2M) = cong `suc_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2pred \u22a2M) = cong `pred_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) = cong\u2083 `if0_then_else_\n (erase-lemma \u22a2L) (erase-lemma \u22a2M) (erase-lemma \u22a2N)\nerase-lemma (\u22a2Y \u22a2M) = cong `Y_ (erase-lemma \u22a2M)\n\\end{code}\n\n\n## Substitution\n\n### Lists as sets\n\n\\begin{code}\nopen Collections (Id) (_\u225f_)\n\\end{code}\n\n### Free variables\n\n\\begin{code}\nfree : Term \u2192 List Id\nfree (` x) = [ x ]\nfree (`\u03bb x `\u2192 N) = free N \\\\ x\nfree (L \u00b7 M) = free L ++ free M\nfree (`zero) = []\nfree (`suc M) = free M\nfree (`pred M) = free M\nfree (`if0 L then M else N) = free L ++ free M ++ free N\nfree (`Y M) = free M\n\\end{code}\n\n### Identifier maps\n\n\\begin{code}\n\u2205 : Id \u2192 Term\n\u2205 x = ` x\n\ninfixl 5 _,_\u21a6_\n\n_,_\u21a6_ : (Id \u2192 Term) \u2192 Id \u2192 Term \u2192 (Id \u2192 Term)\n(\u03c1 , x \u21a6 M) w with w \u225f x\n... | yes _ = M\n... | no _ = \u03c1 w\n\\end{code}\n\n### Substitution\n\n\\begin{code}\nsubst : (Id \u2192 Term) \u2192 Term \u2192 Term\nsubst \u03c1 (` x) = \u03c1 x\nsubst \u03c1 (`\u03bb x `\u2192 N) = `\u03bb x `\u2192 subst (\u03c1 , x \u21a6 ` x) N\nsubst \u03c1 (L \u00b7 M) = subst \u03c1 L \u00b7 subst \u03c1 M\nsubst \u03c1 (`zero) = `zero\nsubst \u03c1 (`suc M) = `suc (subst \u03c1 M)\nsubst \u03c1 (`pred M) = `pred (subst \u03c1 M)\nsubst \u03c1 (`if0 L then M else N)\n = `if0 (subst \u03c1 L) then (subst \u03c1 M) else (subst \u03c1 N)\nsubst \u03c1 (`Y M) = `Y (subst \u03c1 M)\n\n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\nN [ x := M ] = subst (\u2205 , x \u21a6 M) N\n\\end{code}\n\n### Testing substitution\n\n\\begin{code}\n_ : (` \"s\" \u00b7 ` \"s\" \u00b7 ` \"z\") [ \"z\" := `zero ] \u2261 (` \"s\" \u00b7 ` \"s\" \u00b7 `zero)\n_ = refl\n\n_ : (` \"s\" \u00b7 ` \"s\" \u00b7 ` \"z\") [ \"s\" := (`\u03bb \"m\" `\u2192 `suc ` \"m\") ] [ \"z\" := `zero ]\n \u2261 (`\u03bb \"m\" `\u2192 `suc ` \"m\") \u00b7 (`\u03bb \"m\" `\u2192 `suc ` \"m\") \u00b7 `zero\n_ = refl\n\n_ : (`\u03bb \"m\" `\u2192 ` \"m\" \u00b7 ` \"n\") [ \"n\" := ` \"p\" \u00b7 ` \"q\" ]\n \u2261 `\u03bb \"m\" `\u2192 ` \"m\" \u00b7 (` \"p\" \u00b7 ` \"q\")\n_ = refl\n\n_ : subst (\u2205 , \"m\" \u21a6 two , \"n\" \u21a6 four) (` \"m\" \u00b7 ` \"n\") \u2261 (two \u00b7 four)\n_ = refl\n\\end{code}\n\n\n## Values\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n\n Zero :\n ----------\n Value `zero\n\n Suc : \u2200 {V}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n\n Fun : \u2200 {x N}\n ---------------\n \u2192 Value (`\u03bb x `\u2192 N)\n\\end{code}\n\n## Reduction\n\n\\begin{code}\ninfix 4 _\u27f6_\n\ndata _\u27f6_ : Term \u2192 Term \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\n \u2192 L \u27f6 L\u2032\n -----------------\n \u2192 L \u00b7 M \u27f6 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\n \u2192 Value V\n \u2192 M \u27f6 M\u2032\n -----------------\n \u2192 V \u00b7 M \u27f6 V \u00b7 M\u2032\n\n \u03b2-\u2192 : \u2200 {x N V}\n \u2192 Value V\n ---------------------------------\n \u2192 (`\u03bb x `\u2192 N) \u00b7 V \u27f6 N [ x := V ]\n\n \u03be-suc : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n -------------------\n \u2192 `suc M \u27f6 `suc M\u2032\n\n \u03be-pred : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n ---------------------\n \u2192 `pred M \u27f6 `pred M\u2032\n\n \u03b2-pred-zero :\n ----------------------\n `pred `zero \u27f6 `zero\n\n \u03b2-pred-suc : \u2200 {V}\n \u2192 Value V\n ---------------------\n \u2192 `pred (`suc V) \u27f6 V\n\n \u03be-if0 : \u2200 {L L\u2032 M N}\n \u2192 L \u27f6 L\u2032\n -----------------------------------------------\n \u2192 `if0 L then M else N \u27f6 `if0 L\u2032 then M else N\n\n \u03b2-if0-zero : \u2200 {M N}\n -------------------------------\n \u2192 `if0 `zero then M else N \u27f6 M\n\n \u03b2-if0-suc : \u2200 {V M N}\n \u2192 Value V\n ----------------------------------\n \u2192 `if0 (`suc V) then M else N \u27f6 N\n\n \u03be-Y : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n ---------------\n \u2192 `Y M \u27f6 `Y M\u2032\n\n \u03b2-Y : \u2200 {F x N}\n \u2192 F \u2261 `\u03bb x `\u2192 N\n -------------------------\n \u2192 `Y F \u27f6 N [ x := `Y F ]\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\ninfix 2 _\u27f6*_\ninfix 1 begin_\ninfixr 2 _\u27f6\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u27f6*_ : Term \u2192 Term \u2192 Set where\n\n _\u220e : \u2200 (M : Term)\n -------------\n \u2192 M \u27f6* M\n\n _\u27f6\u27e8_\u27e9_ : \u2200 (L : Term) {M N}\n \u2192 L \u27f6 M\n \u2192 M \u27f6* N\n ---------\n \u2192 L \u27f6* N\n\nbegin_ : \u2200 {M N} \u2192 (M \u27f6* N) \u2192 (M \u27f6* N)\nbegin M\u27f6*N = M\u27f6*N\n\\end{code}\n\n## Sample execution\n\n\\begin{code}\n_ : plus \u00b7 two \u00b7 two \u27f6* (`suc (`suc (`suc (`suc `zero))))\n_ =\n begin\n plus \u00b7 two \u00b7 two\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl)) \u27e9\n (`\u03bb \"m\" `\u2192 (`\u03bb \"n\" `\u2192 `if0 ` \"m\" then ` \"n\" else\n `suc (plus \u00b7 (`pred (` \"m\")) \u00b7 (` \"n\")))) \u00b7 two \u00b7 two\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u2192 (Suc (Suc Zero))) \u27e9\n (`\u03bb \"n\" `\u2192 `if0 two then ` \"n\" else\n `suc (plus \u00b7 (`pred two) \u00b7 (` \"n\"))) \u00b7 two\n \u27f6\u27e8 \u03b2-\u2192 (Suc (Suc Zero)) \u27e9\n `if0 two then two else\n `suc (plus \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03b2-if0-suc (Suc Zero) \u27e9\n `suc (plus \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl))) \u27e9\n `suc ((`\u03bb \"m\" `\u2192 (`\u03bb \"n\" `\u2192 `if0 ` \"m\" then ` \"n\" else\n `suc (plus \u00b7 (`pred (` \"m\")) \u00b7 (` \"n\")))) \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc (Suc Zero)))) \u27e9\n `suc ((`\u03bb \"m\" `\u2192 (`\u03bb \"n\" `\u2192 `if0 ` \"m\" then ` \"n\" else\n `suc (plus \u00b7 (`pred (` \"m\")) \u00b7 (` \"n\")))) \u00b7 (`suc `zero) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u2192 (Suc Zero))) \u27e9\n `suc ((`\u03bb \"n\" `\u2192 `if0 `suc `zero then ` \"n\" else\n `suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 (` \"n\")))) \u00b7 two\n \u27f6\u27e8 \u03be-suc (\u03b2-\u2192 (Suc (Suc Zero))) \u27e9\n `suc (`if0 `suc `zero then two else\n `suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03b2-if0-suc Zero) \u27e9\n `suc (`suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl)))) \u27e9\n `suc (`suc ((`\u03bb \"m\" `\u2192 (`\u03bb \"n\" `\u2192 `if0 ` \"m\" then ` \"n\" else\n `suc (plus \u00b7 (`pred (` \"m\")) \u00b7 (` \"n\")))) \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc Zero)))) \u27e9\n `suc (`suc ((`\u03bb \"m\" `\u2192 (`\u03bb \"n\" `\u2192 `if0 ` \"m\" then ` \"n\" else\n `suc (plus \u00b7 (`pred (` \"m\")) \u00b7 (` \"n\")))) \u00b7 `zero \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u2192 Zero))) \u27e9\n `suc (`suc ((`\u03bb \"n\" `\u2192 `if0 `zero then ` \"n\" else\n `suc (plus \u00b7 (`pred `zero) \u00b7 (` \"n\"))) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u2192 (Suc (Suc Zero)))) \u27e9\n `suc (`suc (`if0 `zero then two else\n `suc (plus \u00b7 (`pred `zero) \u00b7 two)))\n \u27f6\u27e8 \u03be-suc (\u03be-suc \u03b2-if0-zero) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\n## Values do not reduce\n\nValues do not reduce.\n\\begin{code}\nVal-\u27f6 : \u2200 {M N} \u2192 Value M \u2192 \u00ac (M \u27f6 N)\nVal-\u27f6 Fun ()\nVal-\u27f6 Zero ()\nVal-\u27f6 (Suc VM) (\u03be-suc M\u27f6N) = Val-\u27f6 VM M\u27f6N\n\\end{code}\n\nAs a corollary, terms that reduce are not values.\n\\begin{code}\n\u27f6-Val : \u2200 {M N} \u2192 (M \u27f6 N) \u2192 \u00ac Value M\n\u27f6-Val M\u27f6N VM = Val-\u27f6 VM M\u27f6N\n\\end{code}\n\n## Reduction is deterministic\n\n\\begin{code}\ndet : \u2200 {M M\u2032 M\u2033}\n \u2192 (M \u27f6 M\u2032)\n \u2192 (M \u27f6 M\u2033)\n ----------\n \u2192 M\u2032 \u2261 M\u2033\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = cong\u2082 _\u00b7_ (det L\u27f6L\u2032 L\u27f6L\u2033) refl\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2082 VL _) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2032)\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03b2-\u2192 _) = \u22a5-elim (Val-\u27f6 Fun L\u27f6L\u2032)\ndet (\u03be-\u00b7\u2082 VL _) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2033)\ndet (\u03be-\u00b7\u2082 _ M\u27f6M\u2032) (\u03be-\u00b7\u2082 _ M\u27f6M\u2033) = cong\u2082 _\u00b7_ refl (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-\u00b7\u2082 _ M\u27f6M\u2032) (\u03b2-\u2192 VM) = \u22a5-elim (Val-\u27f6 VM M\u27f6M\u2032)\ndet (\u03b2-\u2192 VM) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 Fun L\u27f6L\u2033)\ndet (\u03b2-\u2192 VM) (\u03be-\u00b7\u2082 _ M\u27f6M\u2033) = \u22a5-elim (Val-\u27f6 VM M\u27f6M\u2033)\ndet (\u03b2-\u2192 _) (\u03b2-\u2192 _) = refl\ndet (\u03be-suc M\u27f6M\u2032) (\u03be-suc M\u27f6M\u2033) = cong `suc_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-pred M\u27f6M\u2032) (\u03be-pred M\u27f6M\u2033) = cong `pred_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-pred M\u27f6M\u2032) \u03b2-pred-zero = \u22a5-elim (Val-\u27f6 Zero M\u27f6M\u2032)\ndet (\u03be-pred M\u27f6M\u2032) (\u03b2-pred-suc VM) = \u22a5-elim (Val-\u27f6 (Suc VM) M\u27f6M\u2032)\ndet \u03b2-pred-zero (\u03be-pred M\u27f6M\u2032) = \u22a5-elim (Val-\u27f6 Zero M\u27f6M\u2032)\ndet \u03b2-pred-zero \u03b2-pred-zero = refl\ndet (\u03b2-pred-suc VM) (\u03be-pred M\u27f6M\u2032) = \u22a5-elim (Val-\u27f6 (Suc VM) M\u27f6M\u2032)\ndet (\u03b2-pred-suc _) (\u03b2-pred-suc _) = refl\ndet (\u03be-if0 L\u27f6L\u2032) (\u03be-if0 L\u27f6L\u2033) = cong\u2083 `if0_then_else_ (det L\u27f6L\u2032 L\u27f6L\u2033) refl refl\ndet (\u03be-if0 L\u27f6L\u2032) \u03b2-if0-zero = \u22a5-elim (Val-\u27f6 Zero L\u27f6L\u2032)\ndet (\u03be-if0 L\u27f6L\u2032) (\u03b2-if0-suc VL) = \u22a5-elim (Val-\u27f6 (Suc VL) L\u27f6L\u2032)\ndet \u03b2-if0-zero (\u03be-if0 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 Zero L\u27f6L\u2033)\ndet \u03b2-if0-zero \u03b2-if0-zero = refl\ndet (\u03b2-if0-suc VL) (\u03be-if0 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 (Suc VL) L\u27f6L\u2033)\ndet (\u03b2-if0-suc _) (\u03b2-if0-suc _) = refl\ndet (\u03be-Y M\u27f6M\u2032) (\u03be-Y M\u27f6M\u2033) = cong `Y_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-Y M\u27f6M\u2032) (\u03b2-Y refl) = \u22a5-elim (Val-\u27f6 Fun M\u27f6M\u2032)\ndet (\u03b2-Y refl) (\u03be-Y M\u27f6M\u2033) = \u22a5-elim (Val-\u27f6 Fun M\u27f6M\u2033)\ndet (\u03b2-Y refl) (\u03b2-Y refl) = refl\n\\end{code}\n\nAlmost half the lines in the above proof are redundant, for example\n\n det (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2082 VL _) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2032)\n det (\u03be-\u00b7\u2082 VL _) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2033)\n\nare essentially identical. What we might like to do is delete the\nredundant lines and add\n\n det M\u27f6M\u2032 M\u27f6M\u2033 = sym (det M\u27f6M\u2033 M\u27f6M\u2032)\n\nto the bottom of the proof. But this does not work. The termination\nchecker complains, because the arguments have merely switched order\nand neither is smaller.\n\n## Canonical forms\n\n\\begin{code}\ndata Canonical : Term \u2192 Type \u2192 Set where\n\n Zero :\n -------------------\n Canonical `zero `\u2115\n\n Suc : \u2200 {V}\n \u2192 Canonical V `\u2115\n ----------------------\n \u2192 Canonical (`suc V) `\u2115\n\n Fun : \u2200 {x N A B}\n \u2192 \u03b5 , x `: A \u22a2 N `: B\n -------------------------------\n \u2192 Canonical (`\u03bb x `\u2192 N) (A `\u2192 B)\n\\end{code}\n\n## Canonical forms lemma\n\nEvery typed value is canonical.\n\n\\begin{code}\ncanonical : \u2200 {V A}\n \u2192 \u03b5 \u22a2 V `: A\n \u2192 Value V\n -------------\n \u2192 Canonical V A\ncanonical \u22a2zero Zero = Zero\ncanonical (\u22a2suc \u22a2V) (Suc VV) = Suc (canonical \u22a2V VV)\ncanonical (\u22a2\u03bb \u22a2N) Fun = Fun \u22a2N\n\\end{code}\n\nEvery canonical form has a type and a value.\n\n\\begin{code}\ntype : \u2200 {V A}\n \u2192 Canonical V A\n --------------\n \u2192 \u03b5 \u22a2 V `: A\ntype Zero = \u22a2zero\ntype (Suc CV) = \u22a2suc (type CV)\ntype (Fun {x = x} \u22a2N) = \u22a2\u03bb \u22a2N\n\nvalue : \u2200 {V A}\n \u2192 Canonical V A\n -------------\n \u2192 Value V\nvalue Zero = Zero\nvalue (Suc CV) = Suc (value CV)\nvalue (Fun \u22a2N) = Fun\n\\end{code}\n\n## Progress\n\n\\begin{code}\ndata Progress (M : Term) (A : Type) : Set where\n step : \u2200 {N}\n \u2192 M \u27f6 N\n ----------\n \u2192 Progress M A\n done :\n Canonical M A\n -------------\n \u2192 Progress M A\n\nprogress : \u2200 {M A} \u2192 \u03b5 \u22a2 M `: A \u2192 Progress M A\nprogress (Ax ())\nprogress (\u22a2\u03bb \u22a2N) = done (Fun \u22a2N)\nprogress (\u22a2L \u00b7 \u22a2M) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-\u00b7\u2081 L\u27f6L\u2032)\n... | done (Fun _) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-\u00b7\u2082 Fun M\u27f6M\u2032)\n... | done CM = step (\u03b2-\u2192 (value CM))\nprogress \u22a2zero = done Zero\nprogress (\u22a2suc \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-suc M\u27f6M\u2032)\n... | done CM = done (Suc CM)\nprogress (\u22a2pred \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-pred M\u27f6M\u2032)\n... | done Zero = step \u03b2-pred-zero\n... | done (Suc CM) = step (\u03b2-pred-suc (value CM))\nprogress (\u22a2if0 \u22a2L \u22a2M \u22a2N) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-if0 L\u27f6L\u2032)\n... | done Zero = step \u03b2-if0-zero\n... | done (Suc CM) = step (\u03b2-if0-suc (value CM))\nprogress (\u22a2Y \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-Y M\u27f6M\u2032)\n... | done (Fun _) = step (\u03b2-Y refl)\n\\end{code}\n\n\n## Preservation\n\n### Domain of an environment\n\n\\begin{code}\n{-\ndom : Env \u2192 List Id\ndom \u03b5 = []\ndom (\u0393 , x `: A) = x \u2237 dom \u0393\n\ndom-lemma : \u2200 {\u0393 y B} \u2192 \u0393 \u220b y `: B \u2192 y \u2208 dom \u0393\ndom-lemma Z = here\ndom-lemma (S x\u2262y \u22a2y) = there (dom-lemma \u22a2y)\n\nfree-lemma : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M `: A \u2192 free M \u2286 dom \u0393\nfree-lemma (Ax \u22a2x) w\u2208 with w\u2208\n... | here = dom-lemma \u22a2x\n... | there ()\nfree-lemma {\u0393} (\u22a2\u03bb {N = N} \u22a2N) = \u2237-to-\\\\ (free-lemma \u22a2N)\nfree-lemma (\u22a2L \u00b7 \u22a2M) w\u2208 with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208M = free-lemma \u22a2M \u2208M\nfree-lemma \u22a2zero ()\nfree-lemma (\u22a2suc \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2pred \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) w\u2208\n with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208MN with ++-to-\u228e \u2208MN\n... | inj\u2081 \u2208M = free-lemma \u22a2M \u2208M\n... | inj\u2082 \u2208N = free-lemma \u22a2N \u2208N\nfree-lemma (\u22a2Y \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\n-}\n\\end{code}\n\n### Renaming\n\n\\begin{code}\n\u22a2rename : \u2200 {\u0393 \u0394}\n \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 \u0394 \u220b x `: A)\n --------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 \u0393 \u22a2 M `: A \u2192 \u0394 \u22a2 M `: A)\n\u22a2rename \u22a2\u03c3 (Ax \u22a2x) = Ax (\u22a2\u03c3 \u22a2x)\n\u22a2rename {\u0393} {\u0394} \u22a2\u03c3 (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb (\u22a2rename {\u0393\u2032} {\u0394\u2032} \u22a2\u03c3\u2032 \u22a2N)\n where\n \u0393\u2032 = \u0393 , x `: A\n \u0394\u2032 = \u0394 , x `: A\n\n \u22a2\u03c3\u2032 : \u2200 {w B} \u2192 \u0393\u2032 \u220b w `: B \u2192 \u0394\u2032 \u220b w `: B\n \u22a2\u03c3\u2032 Z = Z\n \u22a2\u03c3\u2032 (S w\u2262 \u22a2w) = S w\u2262 (\u22a2\u03c3 \u22a2w)\n\n\u22a2rename \u22a2\u03c3 (\u22a2L \u00b7 \u22a2M) = \u22a2rename \u22a2\u03c3 \u22a2L \u00b7 \u22a2rename \u22a2\u03c3 \u22a2M\n\u22a2rename \u22a2\u03c3 (\u22a2zero) = \u22a2zero\n\u22a2rename \u22a2\u03c3 (\u22a2suc \u22a2M) = \u22a2suc (\u22a2rename \u22a2\u03c3 \u22a2M)\n\u22a2rename \u22a2\u03c3 (\u22a2pred \u22a2M) = \u22a2pred (\u22a2rename \u22a2\u03c3 \u22a2M)\n\u22a2rename \u22a2\u03c3 (\u22a2if0 \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2rename \u22a2\u03c3 \u22a2L) (\u22a2rename \u22a2\u03c3 \u22a2M) (\u22a2rename \u22a2\u03c3 \u22a2N)\n\u22a2rename \u22a2\u03c3 (\u22a2Y \u22a2M) = \u22a2Y (\u22a2rename \u22a2\u03c3 \u22a2M)\n\\end{code}\n\n\n### Substitution preserves types\n\n\\begin{code}\n-- trivial : Set\n-- trivial = \u2200 \u03c1 x \u2192 \u03c1 x \u2261 ` x \u228e closed (\u03c1 x)\n\n\u22a2subst : \u2200 {\u0393 \u0394 \u03c1}\n-- \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 trivial \u03c1 x)\n \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 \u0394 \u22a2 \u03c1 x `: A)\n -------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 \u0393 \u22a2 M `: A \u2192 \u0394 \u22a2 subst \u03c1 M `: A)\n\u22a2subst \u22a2\u03c1 (Ax \u22a2x) = \u22a2\u03c1 \u22a2x\n\u22a2subst {\u0393} {\u0394} {\u03c1} \u22a2\u03c1 (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb {x = x} {A = A} (\u22a2subst {\u0393\u2032} {\u0394\u2032} {\u03c1\u2032} \u22a2\u03c1\u2032 \u22a2N)\n where\n \u0393\u2032 = \u0393 , x `: A\n \u0394\u2032 = \u0394 , x `: A\n \u03c1\u2032 = \u03c1 , x \u21a6 ` x\n\n \u22a2\u03c3 : \u2200 {w C} \u2192 \u0394 \u220b w `: C \u2192 \u0394\u2032 \u220b w `: C\n \u22a2\u03c3 \u22a2w = S {!!} \u22a2w\n\n \u22a2\u03c1\u2032 : \u2200 {w C} \u2192 \u0393\u2032 \u220b w `: C \u2192 \u0394\u2032 \u22a2 \u03c1\u2032 w `: C\n \u22a2\u03c1\u2032 {w} Z with w \u225f x\n ... | yes _ = Ax Z\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1\u2032 {w} (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = \u22a2rename {\u0394} {\u0394\u2032} \u22a2\u03c3 (\u22a2\u03c1 \u22a2w)\n\n\u22a2subst \u22a2\u03c1 (\u22a2L \u00b7 \u22a2M) = \u22a2subst \u22a2\u03c1 \u22a2L \u00b7 \u22a2subst \u22a2\u03c1 \u22a2M\n\u22a2subst \u22a2\u03c1 \u22a2zero = \u22a2zero\n\u22a2subst \u22a2\u03c1 (\u22a2suc \u22a2M) = \u22a2suc (\u22a2subst \u22a2\u03c1 \u22a2M)\n\u22a2subst \u22a2\u03c1 (\u22a2pred \u22a2M) = \u22a2pred (\u22a2subst \u22a2\u03c1 \u22a2M)\n\u22a2subst \u22a2\u03c1 (\u22a2if0 \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2subst \u22a2\u03c1 \u22a2L) (\u22a2subst \u22a2\u03c1 \u22a2M) (\u22a2subst \u22a2\u03c1 \u22a2N)\n\u22a2subst \u22a2\u03c1 (\u22a2Y \u22a2M) = \u22a2Y (\u22a2subst \u22a2\u03c1 \u22a2M)\n\\end{code}\n\nLet's look at examples. Assume `M` is closed. Example 1.\n\n subst (\u2205 , \"x\" \u21a6 M) (`\u03bb \"y\" `\u2192 ` \"x\") \u2261 `\u03bb \"y\" `\u2192 M\n\nExample 2.\n\n subst (\u2205 , \"y\" \u21a6 N , \"x\" \u21a6 M) (`\u03bb \"y\" `\u2192 ` \"x\" \u00b7 ` \"y\")\n \u2261\n `\u03bb \"y\" `\u2192 subst (\u2205 , \"y\" \u21a6 ` N , \"x\" \u21a6 M , \"y\" \u21a6 ` \"y\") (` \"x\" \u00b7 ` \"y\")\n \u2261\n `\u03bb \"y\" `\u2192 (M \u00b7 ` \"y\")\n\nBefore I wrote: \"The hypotheses of the theorem appear to be violated. Drat!\"\nBut let's assume that ``M `: A``, ``N `: B``, and the lambda bound `y` has type `C`.\nThen ``\u0393 \u220b y `: B`` will not hold for the extended `\u03c1` because of interference\nby the earlier `y`. So I'm not sure the hypothesis is violated.\n\n\n\n\\begin{code}\n\u22a2substitution : \u2200 {\u0393 x A N B M}\n \u2192 \u0393 , x `: A \u22a2 N `: B\n \u2192 \u0393 \u22a2 M `: A\n ----------------------\n \u2192 \u0393 \u22a2 N [ x := M ] `: B\n\u22a2substitution {\u0393} {x} {A} {N} {B} {M} \u22a2N \u22a2M =\n \u22a2subst {\u0393\u2032} {\u0393} {\u03c1} \u22a2\u03c1 {N} {B} \u22a2N\n where\n \u0393\u2032 = \u0393 , x `: A\n \u03c1 = \u2205 , x \u21a6 M\n \u22a2\u03c1 : \u2200 {w B} \u2192 \u0393\u2032 \u220b w `: B \u2192 \u0393 \u22a2 \u03c1 w `: B\n \u22a2\u03c1 {w} Z with w \u225f x\n ... | yes _ = \u22a2M\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1 {w} (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = Ax \u22a2w\n\\end{code}\n\n### Preservation\n\n\\begin{code}\npreservation : \u2200 {\u0393 M N A}\n \u2192 \u0393 \u22a2 M `: A\n \u2192 M \u27f6 N\n ---------\n \u2192 \u0393 \u22a2 N `: A\npreservation (Ax \u22a2x) ()\npreservation (\u22a2\u03bb \u22a2N) ()\npreservation (\u22a2L \u00b7 \u22a2M) (\u03be-\u00b7\u2081 L\u27f6) = preservation \u22a2L L\u27f6 \u00b7 \u22a2M\npreservation (\u22a2V \u00b7 \u22a2M) (\u03be-\u00b7\u2082 _ M\u27f6) = \u22a2V \u00b7 preservation \u22a2M M\u27f6\npreservation ((\u22a2\u03bb \u22a2N) \u00b7 \u22a2W) (\u03b2-\u2192 _) = \u22a2substitution \u22a2N \u22a2W\npreservation (\u22a2zero) ()\npreservation (\u22a2suc \u22a2M) (\u03be-suc M\u27f6) = \u22a2suc (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2M) (\u03be-pred M\u27f6) = \u22a2pred (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2zero) (\u03b2-pred-zero) = \u22a2zero\npreservation (\u22a2pred (\u22a2suc \u22a2M)) (\u03b2-pred-suc _) = \u22a2M\npreservation (\u22a2if0 \u22a2L \u22a2M \u22a2N) (\u03be-if0 L\u27f6) = \u22a2if0 (preservation \u22a2L L\u27f6) \u22a2M \u22a2N\npreservation (\u22a2if0 \u22a2zero \u22a2M \u22a2N) \u03b2-if0-zero = \u22a2M\npreservation (\u22a2if0 (\u22a2suc \u22a2V) \u22a2M \u22a2N) (\u03b2-if0-suc _) = \u22a2N\npreservation (\u22a2Y \u22a2M) (\u03be-Y M\u27f6) = \u22a2Y (preservation \u22a2M M\u27f6)\npreservation (\u22a2Y (\u22a2\u03bb \u22a2N)) (\u03b2-Y refl) = \u22a2substitution \u22a2N (\u22a2Y (\u22a2\u03bb \u22a2N))\n\\end{code}\n\n## Normalise\n\n\\begin{code}\ndata Normalise {M A} (\u22a2M : \u03b5 \u22a2 M `: A) : Set where\n out-of-gas : \u2200 {N} \u2192 M \u27f6* N \u2192 \u03b5 \u22a2 N `: A \u2192 Normalise \u22a2M\n normal : \u2200 {V} \u2192 \u2115 \u2192 Canonical V A \u2192 M \u27f6* V \u2192 Normalise \u22a2M\n\nnormalise : \u2200 {L A} \u2192 \u2115 \u2192 (\u22a2L : \u03b5 \u22a2 L `: A) \u2192 Normalise \u22a2L\nnormalise {L} zero \u22a2L = out-of-gas (L \u220e) \u22a2L\nnormalise {L} (suc m) \u22a2L with progress \u22a2L\n... | done CL = normal (suc m) CL (L \u220e)\n... | step L\u27f6M with preservation \u22a2L L\u27f6M\n... | \u22a2M with normalise m \u22a2M\n... | out-of-gas M\u27f6*N \u22a2N = out-of-gas (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*N) \u22a2N\n... | normal n CV M\u27f6*V = normal n CV (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*V)\n\\end{code}\n\n","avg_line_length":27.0296559905,"max_line_length":91,"alphanum_fraction":0.4200386202} +{"size":47536,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"---\ntitle : \"Denotational: semantics of the untyped lambda calculus\"\nlayout : page\nprev : \/Untyped\/\npermalink : \/Denotational\/\nnext : \/Compositional\/\n---\n\n\\begin{code}\nmodule plfa.Denotational where\n\\end{code}\n\nThe lambda calculus is a language about _functions_, that is, mappings\nfrom input to output. In computing we often think of such\nmappings as being carried out by a sequence of\noperations that transform an input into an output. But \nfunctions can also be represented as data. For example, one\ncan tabulate a function, that is, create a table where each row has\ntwo entries, an input and the corresponding output for the function.\nFunction application is then the process of looking up the row for\na given input and reading off the output.\n\nWe shall create a semantics for the untyped lambda calculus based on\nthis idea of functions-as-tables. However, there are two difficulties\nthat arise. First, functions often have an infinite domain, so it\nwould seem that we would need infinitely long tables to represent\nfunctions. Second, in the lambda calculus, functions can be applied to\nfunctions. They can even be applied to themselves! So it would seem\nthat the tables would contain cycles. One might start to worry that\nadvanced techniques are necessary to to address these issues, but\nfortunately this is not the case!\n\nThe first problem, of functions with infinite domains, is solved by\nobserving that in the execution of a terminating program, each lambda\nabstraction will only be applied to a finite number of distinct\narguments. (We come back later to discuss diverging programs.) This\nobservation is another way of looking at Dana Scott's insight that\nonly continuous functions are needed to model the lambda calculus.\n\nThe second problem, that of self-application, is solved by relaxing\nthe way in which we lookup an argument in a function's table.\nNaively, one would look in the table for a row in which the input\nentry exactly matches the argument. In the case of self-application,\nthis would require the table to contain a copy of itself. Impossible!\n(At least, it is impossible if we want to build tables using inductive\ndata type definitions, which indeed we do.) Instead it is sufficient\nto find an input such that every row of the input appears as a row of\nthe argument (that is, the input is a subset of the argument). In the\ncase of self-application, the table only needs to contain a smaller\ncopy of itself, which is fine.\n\nWith these two observations in hand, it is straightforward to write\ndown a denotational semantics of the lambda calculus.\n\n## Imports\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality\n using (_\u2261_; _\u2262_; refl; sym; cong; cong\u2082; cong-app)\nopen import Data.Product using (_\u00d7_; \u03a3; \u03a3-syntax; \u2203; \u2203-syntax; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum\nopen import Agda.Primitive using (lzero)\nopen import plfa.Untyped\nopen import Relation.Nullary using (\u00ac_)\nopen import Relation.Nullary.Negation using (contradiction)\nopen import Data.Empty using (\u22a5-elim)\nopen import Data.Unit\nopen import Relation.Nullary using (Dec; yes; no)\nopen import Function using (_\u2218_)\n-- open import plfa.Isomorphism using (extensionality) -- causes a bug!\n\\end{code}\n\n\\begin{code}\npostulate\n extensionality : \u2200 {A B : Set} {f g : A \u2192 B}\n \u2192 (\u2200 (x : A) \u2192 f x \u2261 g x)\n -----------------------\n \u2192 f \u2261 g\n\\end{code}\n\n## Values\n\nThe `Value` data type represents a finite portion of a function. We\nthink of a value as a finite set of pairs that represent input-output\nmappings. The `Value` data type represents the set as a binary tree\nwhose internal nodes are the union operator and whose leaves represent\neither a single mapping or the empty set.\n\n * The \u22a5 value provides no information about the computation.\n\n * A value of the form `v \u21a6 w` is a single input-output mapping, from\n input `v` to output `w`.\n\n * A value of the form `v \u2294 w` is a function that maps inputs to\n outputs according to both `v` and `w`. Think of it as taking the\n union of the two sets.\n\n\\begin{code}\ninfixr 7 _\u21a6_\ninfixl 5 _\u2294_\n\ndata Value : Set where\n \u22a5 : Value\n _\u21a6_ : Value \u2192 Value \u2192 Value\n _\u2294_ : Value \u2192 Value \u2192 Value\n\\end{code}\n\nThe `\u2291` relation adapts the familiar notion of subset to the Value data\ntype. This relation plays the key role in enabling self-application.\nThere are two rules that are specific to functions, `Fun\u2291` and `Dist\u2291`,\nwhich we discuss below.\n\n\\begin{code}\ninfix 4 _\u2291_\n\ndata _\u2291_ : Value \u2192 Value \u2192 Set where\n\n Bot\u2291 : \u2200 {v} \u2192 \u22a5 \u2291 v\n\n ConjL\u2291 : \u2200 {u v w}\n \u2192 v \u2291 u\n \u2192 w \u2291 u\n -----------\n \u2192 (v \u2294 w) \u2291 u\n\n ConjR1\u2291 : \u2200 {u v w}\n \u2192 u \u2291 v\n -----------\n \u2192 u \u2291 (v \u2294 w)\n\n ConjR2\u2291 : \u2200 {u v w}\n \u2192 u \u2291 w\n -----------\n \u2192 u \u2291 (v \u2294 w)\n\n Trans\u2291 : \u2200 {u v w}\n \u2192 u \u2291 v\n \u2192 v \u2291 w\n -----\n \u2192 u \u2291 w\n\n Fun\u2291 : \u2200 {v w v' w'}\n \u2192 v' \u2291 v\n \u2192 w \u2291 w'\n -------------------\n \u2192 (v \u21a6 w) \u2291 (v' \u21a6 w')\n\n Dist\u2291 : \u2200{v w w'}\n ---------------------------------\n \u2192 v \u21a6 (w \u2294 w') \u2291 (v \u21a6 w) \u2294 (v \u21a6 w')\n\\end{code}\n\n\nThe first five rules are straightforward.\nThe rule `Fun\u2291` captures when it is OK to match a higher-order argument\n`v' \u21a6 w'` to a table entry whose input is `v \u21a6 w`. Considering a\ncall to the higher-order argument. It is OK to pass a larger argument\nthan expected, so `v` can be larger than `v'`. Also, it is OK to\ndisregard some of the output, so `w` can be smaller than `w'`.\nThe rule `Dist\u2291` says that if you have two entries for the same input,\nthen you can combine them into a single entry and joins the two\noutputs.\n\nThe `\u2291` relation is reflexive.\n\n\\begin{code}\nRefl\u2291 : \u2200 {v} \u2192 v \u2291 v\nRefl\u2291 {\u22a5} = Bot\u2291\nRefl\u2291 {v \u21a6 v'} = Fun\u2291 Refl\u2291 Refl\u2291\nRefl\u2291 {v\u2081 \u2294 v\u2082} = ConjL\u2291 (ConjR1\u2291 Refl\u2291) (ConjR2\u2291 Refl\u2291)\n\\end{code}\n\nThe `\u2294` operation is monotonic with respect to `\u2291`, that is, given two\nlarger values it produces a larger value.\n\n\\begin{code}\n\u2294\u2291\u2294 : \u2200 {v w v' w'}\n \u2192 v \u2291 v' \u2192 w \u2291 w'\n -----------------------\n \u2192 (v \u2294 w) \u2291 (v' \u2294 w')\n\u2294\u2291\u2294 d\u2081 d\u2082 = ConjL\u2291 (ConjR1\u2291 d\u2081) (ConjR2\u2291 d\u2082)\n\\end{code}\n\nThe `Dist\u2291` rule can be used to combine two entries even when the\ninput values are not identical. One can first combine the two inputs\nusing \u2294 and then apply the `Dist\u2291` rule to obtain the following\nproperty.\n\n\\begin{code}\nDist\u2294\u21a6\u2294 : \u2200{v v' w w' : Value}\n \u2192 (v \u2294 v') \u21a6 (w \u2294 w') \u2291 (v \u21a6 w) \u2294 (v' \u21a6 w')\nDist\u2294\u21a6\u2294 = Trans\u2291 Dist\u2291 (\u2294\u2291\u2294 (Fun\u2291 (ConjR1\u2291 Refl\u2291) Refl\u2291)\n (Fun\u2291 (ConjR2\u2291 Refl\u2291) Refl\u2291))\n\\end{code}\n\n\n\nIf the join `u \u2294 v` is less than another value `w`,\nthen both `u` and `v` are less than `w`.\n\n\\begin{code}\n\u2294\u2291-invL : \u2200{u v w : Value}\n \u2192 u \u2294 v \u2291 w\n ---------\n \u2192 u \u2291 w\n\u2294\u2291-invL (ConjL\u2291 lt1 lt2) = lt1\n\u2294\u2291-invL (ConjR1\u2291 lt) = ConjR1\u2291 (\u2294\u2291-invL lt)\n\u2294\u2291-invL (ConjR2\u2291 lt) = ConjR2\u2291 (\u2294\u2291-invL lt)\n\u2294\u2291-invL (Trans\u2291 lt1 lt2) = Trans\u2291 (\u2294\u2291-invL lt1) lt2\n\n\u2294\u2291-invR : \u2200{u v w : Value}\n \u2192 u \u2294 v \u2291 w\n ---------\n \u2192 v \u2291 w\n\u2294\u2291-invR (ConjL\u2291 lt1 lt2) = lt2\n\u2294\u2291-invR (ConjR1\u2291 lt) = ConjR1\u2291 (\u2294\u2291-invR lt)\n\u2294\u2291-invR (ConjR2\u2291 lt) = ConjR2\u2291 (\u2294\u2291-invR lt)\n\u2294\u2291-invR (Trans\u2291 lt1 lt2) = Trans\u2291 (\u2294\u2291-invR lt1) lt2\n\\end{code}\n\n\n## Environments\n\nAn environment gives meaning to the free variables in a term by\nmapping variables to values.\n\n\\begin{code}\nEnv : Context \u2192 Set\nEnv \u0393 = \u2200 (x : \u0393 \u220b \u2605) \u2192 Value\n\\end{code}\n\nWe have the empty environment, and we can extend an environment.\n\\begin{code}\n`\u2205 : Env \u2205\n`\u2205 ()\n\ninfixl 5 _`,_\n\n_`,_ : \u2200 {\u0393} \u2192 Env \u0393 \u2192 Value \u2192 Env (\u0393 , \u2605)\n(\u03b3 `, v) Z = v\n(\u03b3 `, v) (S x) = \u03b3 x\n\\end{code}\n\nWe can recover the initial environment from an extended environment,\nand the last value. Putting them back together again takes us where we started.\n\\begin{code}\ninit : \u2200 {\u0393} \u2192 Env (\u0393 , \u2605) \u2192 Env \u0393\ninit \u03b3 x = \u03b3 (S x)\n\nlast : \u2200 {\u0393} \u2192 Env (\u0393 , \u2605) \u2192 Value\nlast \u03b3 = \u03b3 Z\n\ninit-last : \u2200 {\u0393} \u2192 (\u03b3 : Env (\u0393 , \u2605)) \u2192 \u03b3 \u2261 (init \u03b3 `, last \u03b3)\ninit-last {\u0393} \u03b3 = extensionality lemma\n where\n lemma : \u2200 (x : \u0393 , \u2605 \u220b \u2605) \u2192 \u03b3 x \u2261 (init \u03b3 `, last \u03b3) x\n lemma Z = refl\n lemma (S x) = refl\n\\end{code}\n\nThe nth function takes a De Bruijn index and finds the corresponding\nvalue in the environment.\n\n\\begin{code}\nnth : \u2200{\u0393} \u2192 (\u0393 \u220b \u2605) \u2192 Env \u0393 \u2192 Value\nnth x \u03c1 = \u03c1 x\n\\end{code}\n\nWe extend the `\u2291` relation point-wise to environments with the\nfollowing definition.\n\n\\begin{code}\n_`\u2291_ : \u2200 {\u0393} \u2192 Env \u0393 \u2192 Env \u0393 \u2192 Set\n_`\u2291_ {\u0393} \u03b3 \u03b4 = \u2200 (x : \u0393 \u220b \u2605) \u2192 \u03b3 x \u2291 \u03b4 x\n\\end{code}\n\nWe define a bottom environment and a join operator on environments,\nwhich takes the point-wise join of their values.\n\n\\begin{code}\n`\u22a5 : \u2200 {\u0393} \u2192 Env \u0393\n`\u22a5 x = \u22a5\n\n_`\u2294_ : \u2200 {\u0393} \u2192 Env \u0393 \u2192 Env \u0393 \u2192 Env \u0393\n(\u03b3 `\u2294 \u03b4) x = \u03b3 x \u2294 \u03b4 x\n\\end{code}\n\nThe `Refl\u2291`, `ConjR1\u2291`, and `ConjR2\u2291` rules lift to environments. So\nthe join of two environments `\u03b3` and `\u03b4` is greater than the first\nenvironment `\u03b3` or the second environment `\u03b4`.\n\n\\begin{code}\n`Refl\u2291 : \u2200 {\u0393} {\u03b3 : Env \u0393} \u2192 \u03b3 `\u2291 \u03b3\n`Refl\u2291 {\u0393} {\u03b3} x = Refl\u2291 {\u03b3 x}\n\nEnvConjR1\u2291 : \u2200 {\u0393} \u2192 (\u03b3 : Env \u0393) \u2192 (\u03b4 : Env \u0393) \u2192 \u03b3 `\u2291 (\u03b3 `\u2294 \u03b4)\nEnvConjR1\u2291 \u03b3 \u03b4 x = ConjR1\u2291 Refl\u2291\n\nEnvConjR2\u2291 : \u2200 {\u0393} \u2192 (\u03b3 : Env \u0393) \u2192 (\u03b4 : Env \u0393) \u2192 \u03b4 `\u2291 (\u03b3 `\u2294 \u03b4)\nEnvConjR2\u2291 \u03b3 \u03b4 x = ConjR2\u2291 Refl\u2291\n\\end{code}\n\n## Denotational Semantics\n\nWe define the semantics with a judgment of the form `\u03c1 \u22a2 M \u2193 v`,\nwhere `\u03c1` is the environment, `M` the program, and `v` is a result value.\nFor readers familiar with big-step semantics, this notation will feel\nquite natural, but don't let the similarity fool you. There are\nsubtle but important differences! So here is the definition of the\nsemantics, which we discuss in detail in the following paragraphs.\n\n[PLW: PLFA doesn't mention big-step semantics. But perhaps it should!]\n[JGS: It does now in the chapter on Adequacy! Though perhaps\n the big-step semantics should be introduced in an earlier chapter\n and proved equivalent to the reduction semantics.]\n\n\\begin{code}\ninfix 3 _\u22a2_\u2193_\n\ndata _\u22a2_\u2193_ : \u2200{\u0393} \u2192 Env \u0393 \u2192 (\u0393 \u22a2 \u2605) \u2192 Value \u2192 Set where\n\n var : \u2200 {\u0393} {\u03b3 : Env \u0393} {x}\n ---------------\n \u2192 \u03b3 \u22a2 (` x) \u2193 \u03b3 x\n\n \u21a6-elim : \u2200 {\u0393} {\u03b3 : Env \u0393} {L M v w}\n \u2192 \u03b3 \u22a2 L \u2193 (v \u21a6 w)\n \u2192 \u03b3 \u22a2 M \u2193 v\n ---------------\n \u2192 \u03b3 \u22a2 (L \u00b7 M) \u2193 w\n\n \u21a6-intro : \u2200 {\u0393} {\u03b3 : Env \u0393} {N v w}\n \u2192 \u03b3 `, v \u22a2 N \u2193 w\n -------------------\n \u2192 \u03b3 \u22a2 (\u019b N) \u2193 (v \u21a6 w)\n\n \u22a5-intro : \u2200 {\u0393} {\u03b3 : Env \u0393} {M}\n ---------\n \u2192 \u03b3 \u22a2 M \u2193 \u22a5\n\n \u2294-intro : \u2200 {\u0393} {\u03b3 : Env \u0393} {M v w}\n \u2192 \u03b3 \u22a2 M \u2193 v\n \u2192 \u03b3 \u22a2 M \u2193 w\n ---------------\n \u2192 \u03b3 \u22a2 M \u2193 (v \u2294 w)\n \n sub : \u2200 {\u0393} {\u03b3 : Env \u0393} {M v w}\n \u2192 \u03b3 \u22a2 M \u2193 v\n \u2192 w \u2291 v\n ---------\n \u2192 \u03b3 \u22a2 M \u2193 w\n\\end{code}\n\n[PLW: Say we redefine:\n var : \u2200 {\u0393} {\u03b3 : Env \u0393} {x}\n \u2192 v \u2291 \u03b3 x\n -------------\n \u2192 \u03b3 \u22a2 (` x) \u2193 v\nThen does sub (downward closure) follow from the other rules?]\n[JGS: Good question. I'll look into it. In the past I've\n used the above var rule in addition to using \u2291 in \u21a6-elim.\n But perhaps it is now possible to only use it in var.]\n\nConsider the rule for lambda abstractions, `\u21a6-intro`. It says that a\nlambda abstraction results in a single-entry table that maps the input\n`v` to the output `w`, provided that evaluating the body in an\nenvironment with `v` bound to its parameter produces the output `w`.\nAs a simple example of this rule, we can see that the identity function\nmaps `\u22a5` to `\u22a5`. \n\n\\begin{code}\nid : \u2205 \u22a2 \u2605\nid = \u019b # 0\n\\end{code}\n\n\\begin{code}\ndenot-id : \u2200 {\u03b3 v} \u2192 \u03b3 \u22a2 id \u2193 v \u21a6 v\ndenot-id = \u21a6-intro var\n\ndenot-id-two : \u2200 {\u03b3 v w} \u2192 \u03b3 \u22a2 id \u2193 (v \u21a6 v) \u2294 (w \u21a6 w)\ndenot-id-two = \u2294-intro denot-id denot-id\n\\end{code}\n\nOf course, we will need tables with many rows for our lambda\nabstractions. These can be constructed using the `\u2294-intro` rule. If\nterm M (typically a lambda abstraction) can produce both tables `v` and\n`w`, then it produces the combined table `v \u2294 w`. One can take an\noperational view of the rules `\u21a6-intro` and `\u2294-intro` by \nimagining that when an interpreter first comes to a lambda\nabstraction, it pre-evaluates the function on a bunch of randomly\nchosen arguments, using many instances of the rule `\u21a6-intro`, and then\njoins them into one table using many instances of the rule `\u2294-intro`.\nIn the following we show that the identity function produces a table\ncontaining both of the previous results, `\u22a5 \u21a6 \u22a5` and `(\u22a5 \u21a6 \u22a5) \u21a6 (\u22a5 \u21a6 \u22a5)`.\n\n\\begin{code}\ndenot-id3 : `\u2205 \u22a2 id \u2193 (\u22a5 \u21a6 \u22a5) \u2294 (\u22a5 \u21a6 \u22a5) \u21a6 (\u22a5 \u21a6 \u22a5)\ndenot-id3 = denot-id-two\n\\end{code}\n\nWe most often think of the judgment `\u03b3 \u22a2 M \u2193 v` as taking the\nenvironment `\u03b3` and term `M` as input, producing the result `v`. However,\nit is worth emphasizing that the semantics is a _relation_. The above\nresults for the identity function show that the same environment and\nterm can be mapped to different results. However, the results for a\ngiven `\u03b3` and `M` are not _too_ different, they are all finite\napproximations of the same function. Perhaps a better way of thinking\nabout the judgment `\u03b3 \u22a2 M \u2193 v` is that the `\u03b3`, `M`, and `v` are all inputs\nand the semantics either confirms or denies whether `v` is an accurate\npartial description of the result of `M` in environment `\u03b3`.\n\nNext we consider the meaning of function application as given by the\n`\u21a6-elim` rule. In the premise of the rule we have that `L` maps `v` to\n`w`. So if `M` produces `v`, then the application of `L` to `M`\nproduces `w`.\n\nAs an example of function application and the `\u21a6-elim` rule, we apply\nthe identity function to itself. Indeed, we have both that\n`\u2205 \u22a2 id \u2193 (u \u21a6 u) \u21a6 (u \u21a6 u)` and also `\u2205 \u22a2 id \u2193 (u \u21a6 u)`, so we can\napply the rule `\u21a6-elim`.\n\n\\begin{code}\nid-app-id : \u2200 {u : Value} \u2192 `\u2205 \u22a2 id \u00b7 id \u2193 (u \u21a6 u)\nid-app-id {u} = \u21a6-elim (\u21a6-intro var) (\u21a6-intro var)\n\\end{code}\n\nNext we revisit the Church numeral two. This function has two\nparameters: a function and an arbitrary value `u`, and it applies the\nfunction twice. So the function must map `u` to some value, which\nwe'll name `v`. Then for the second application, it must map `v` to\nsome value. Let's name it `w`. So the parameter's table must contain\ntwo entries, both `u \u21a6 v` and `v \u21a6 w`. For each application of the\ntable, we extract the appropriate entry from it using the `sub` rule.\nIn particular, we use the ConjR1\u2291 and ConjR2\u2291 to select `u \u21a6 v` and `v\n\u21a6 w`, respectively, from the table `u \u21a6 v \u2294 v \u21a6 w`. So the meaning of\ntwo\u1d9c is that it takes this table and parameter `u`, and it returns `w`.\nIndeed we derive this as follows.\n\n\\begin{code}\ndenot-two\u1d9c : \u2200{u v w : Value} \u2192 `\u2205 \u22a2 two\u1d9c \u2193 ((u \u21a6 v \u2294 v \u21a6 w) \u21a6 (u \u21a6 w))\ndenot-two\u1d9c {u}{v}{w} =\n \u21a6-intro (\u21a6-intro (\u21a6-elim (sub var lt1) (\u21a6-elim (sub var lt2) var)))\n where lt1 : v \u21a6 w \u2291 u \u21a6 v \u2294 v \u21a6 w\n lt1 = ConjR2\u2291 (Fun\u2291 Refl\u2291 Refl\u2291)\n \n lt2 : u \u21a6 v \u2291 u \u21a6 v \u2294 v \u21a6 w\n lt2 = (ConjR1\u2291 (Fun\u2291 Refl\u2291 Refl\u2291))\n\\end{code}\n\n\nNext we have a classic example of self application: `\u0394 = \u03bbx. (x x)`.\nThe input value for `x` needs to be a table, and it needs to have an\nentry that maps a smaller version of itself, call it `v`, to some value\n`w`. So the input value looks like `v \u21a6 w \u2294 v`. Of course, then the\noutput of `\u0394` is `w`. The derivation is given below. The first occurrences\nof `x` evaluates to `v \u21a6 w`, the second occurrence of `x` evaluates to `v`,\nand then the result of the application is `w`.\n\n\\begin{code}\n\u0394 : \u2205 \u22a2 \u2605\n\u0394 = (\u019b (# 0) \u00b7 (# 0))\n\ndenot-\u0394 : \u2200 {v w} \u2192 `\u2205 \u22a2 \u0394 \u2193 ((v \u21a6 w \u2294 v) \u21a6 w)\ndenot-\u0394 = \u21a6-intro (\u21a6-elim (sub var (ConjR1\u2291 Refl\u2291))\n (sub var (ConjR2\u2291 Refl\u2291)))\n\\end{code}\n\nOne might worry whether this semantics can deal with diverging\nprograms. The `\u22a5` value and the `\u22a5-intro` rule provide a way to handle\nthem. (The `\u22a5-intro` rule is also what enables \u03b2 reduction on\nnon-terminating arguments.) The classic `\u03a9` program is a particularly\nsimple program that diverges. It applies `\u0394` to itself. The semantics\nassigns to `\u03a9` the meaning `\u22a5`. There are several ways to derive this, we\nshall start with one that makes use of the `\u2294-intro` rule. First,\n`denot-\u0394` tells us that `\u0394` evaluates to `((\u22a5 \u21a6 \u22a5) \u2294 \u22a5) \u21a6 \u22a5`\n(choose `v\u2081 = v\u2082 = \u22a5`). Next, `\u0394` also evaluates to `\u22a5 \u21a6 \u22a5` by use of\n`\u21a6-intro` and `\u22a5-intro` and to `\u22a5` by `\u22a5-intro`. As we saw previously,\nwhenever we can show that a program evaluates to two values, we can apply\n`\u2294-intro` to join them together, so `\u0394` evaluates to `(\u22a5 \u21a6 \u22a5) \u2294 \u22a5`. This\nmatches the input of the first occurrence of `\u0394`, so we can conclude that\nthe result of the application is `\u22a5`.\n\n\\begin{code}\n\u03a9 : \u2205 \u22a2 \u2605\n\u03a9 = \u0394 \u00b7 \u0394\n\ndenot-\u03a9 : `\u2205 \u22a2 \u03a9 \u2193 \u22a5\ndenot-\u03a9 = \u21a6-elim denot-\u0394 (\u2294-intro (\u21a6-intro \u22a5-intro) \u22a5-intro) \n\\end{code}\n\nA shorter derivation of the same result is by just one use of the\n`\u22a5-intro` rule.\n\n\\begin{code}\ndenot-\u03a9' : `\u2205 \u22a2 \u03a9 \u2193 \u22a5\ndenot-\u03a9' = \u22a5-intro\n\\end{code}\n\nJust because one can derive `\u2205 \u22a2 M \u2193 \u22a5` for some closed term `M` doesn't mean\nthat `M` necessarily diverges. There may be other derivations that\nconclude with `M` producing some more informative value. However, if\nthe only thing that a term evaluates to is `\u22a5`, then it indeed it\ndiverges.\n\nAn attentive reader may have noticed a disconnect earlier in the way\nwe planned to solve the self-application problem and the actual\n`\u21a6-elim` rule for application. We said at the beginning that we would\nrelax the notion of table lookup, allowing an argument to match an\ninput entry if the argument is equal or greater than the input entry.\nInstead, the `\u21a6-elim` rule seems to require an exact match. However,\nbecause of the `sub` rule, application really does allow larger\narguments.\n\n\\begin{code}\n\u21a6-elim2 : \u2200 {\u0393} {\u03b3 : Env \u0393} {M\u2081 M\u2082 v\u2081 v\u2082 v\u2083}\n \u2192 \u03b3 \u22a2 M\u2081 \u2193 (v\u2081 \u21a6 v\u2083)\n \u2192 \u03b3 \u22a2 M\u2082 \u2193 v\u2082\n \u2192 v\u2081 \u2291 v\u2082\n ------------------\n \u2192 \u03b3 \u22a2 (M\u2081 \u00b7 M\u2082) \u2193 v\u2083\n\u21a6-elim2 d\u2081 d\u2082 lt = \u21a6-elim d\u2081 (sub d\u2082 lt)\n\\end{code}\n\n\n## Denotations and denotational equality\n\nNext we define a notion of denotational equality based on the above\nsemantics. Its statement makes use of an if-and-only-if, which we\ndefine as follows.\n\n\\begin{code}\n_iff_ : Set \u2192 Set \u2192 Set\nP iff Q = (P \u2192 Q) \u00d7 (Q \u2192 P)\n\\end{code}\n\nAnother way to view the denotational semantics is as a function that\nmaps a term to a relation from environments to values. That is, the\n_denotation_ of a term is a relation from environments to values.\n\n\\begin{code}\nDenotation : Context \u2192 Set\u2081\nDenotation \u0393 = (Env \u0393 \u2192 Value \u2192 Set)\n\\end{code}\n\nThe following function \u2130 gives this alternative view of the semantics,\nwhich really just amounts to changing the order of the parameters.\n\n\\begin{code}\n\u2130 : \u2200{\u0393} \u2192 (M : \u0393 \u22a2 \u2605) \u2192 Denotation \u0393\n\u2130 M = \u03bb \u03b3 v \u2192 \u03b3 \u22a2 M \u2193 v\n\\end{code}\n\nIn general, two denotations are equal when they produce the same\nvalues in the same environment.\n\n\\begin{code}\ninfix 3 _\u2243_\n\n_\u2243_ : \u2200 {\u0393} \u2192 (Denotation \u0393) \u2192 (Denotation \u0393) \u2192 Set\n(_\u2243_ {\u0393} D\u2081 D\u2082) = (\u03b3 : Env \u0393) \u2192 (v : Value) \u2192 D\u2081 \u03b3 v iff D\u2082 \u03b3 v\n\\end{code}\n\nDenotational equality is an equivalence relation.\n\n\\begin{code}\n\u2243-refl : \u2200 {\u0393 : Context} \u2192 {M : Denotation \u0393}\n \u2192 M \u2243 M\n\u2243-refl \u03b3 v = \u27e8 (\u03bb x \u2192 x) , (\u03bb x \u2192 x) \u27e9\n\n\u2243-sym : \u2200 {\u0393 : Context} \u2192 {M N : Denotation \u0393}\n \u2192 M \u2243 N\n -----\n \u2192 N \u2243 M\n\u2243-sym eq \u03b3 v = \u27e8 (proj\u2082 (eq \u03b3 v)) , (proj\u2081 (eq \u03b3 v)) \u27e9\n\n\u2243-trans : \u2200 {\u0393 : Context} \u2192 {M\u2081 M\u2082 M\u2083 : Denotation \u0393}\n \u2192 M\u2081 \u2243 M\u2082\n \u2192 M\u2082 \u2243 M\u2083\n -------\n \u2192 M\u2081 \u2243 M\u2083\n\u2243-trans eq1 eq2 \u03b3 v = \u27e8 (\u03bb z \u2192 proj\u2081 (eq2 \u03b3 v) (proj\u2081 (eq1 \u03b3 v) z)) ,\n (\u03bb z \u2192 proj\u2082 (eq1 \u03b3 v) (proj\u2082 (eq2 \u03b3 v) z)) \u27e9\n\\end{code}\n\nTwo terms `M` and `N` are denotational equal when their denotations are\nequal, that is, `\u2130 M \u2243 \u2130 N`.\n\nThe following submodule introduces equational reasoning for the `\u2243`\nrelation.\n\n\\begin{code}\nmodule \u2243-Reasoning {\u0393 : Context} where\n\n infix 1 start_\n infixr 2 _\u2243\u27e8\u27e9_ _\u2243\u27e8_\u27e9_\n infix 3 _\u2610\n\n start_ : \u2200 {x y : Denotation \u0393}\n \u2192 x \u2243 y\n -----\n \u2192 x \u2243 y\n start x\u2243y = x\u2243y\n\n _\u2243\u27e8\u27e9_ : \u2200 (x : Denotation \u0393) {y : Denotation \u0393}\n \u2192 x \u2243 y\n -----\n \u2192 x \u2243 y\n x \u2243\u27e8\u27e9 x\u2243y = x\u2243y\n\n _\u2243\u27e8_\u27e9_ : \u2200 (x : Denotation \u0393) {y z : Denotation \u0393}\n \u2192 x \u2243 y\n \u2192 y \u2243 z\n -----\n \u2192 x \u2243 z\n (x \u2243\u27e8 x\u2243y \u27e9 y\u2243z) = \u2243-trans x\u2243y y\u2243z\n\n _\u2610 : \u2200 (x : Denotation \u0393)\n -----\n \u2192 x \u2243 x\n (x \u2610) = \u2243-refl\n\\end{code}\n\n## Road map for the following chapters\n\nThe subsequent chapters prove that the denotational semantics has\nseveral desirable properties. First, we prove that the semantics is\ncompositional, i.e., that the denotation of a term is a function of\nthe denotations of its subterms. To do this we shall prove equations\nof the following shape.\n\n \u2130 (\u019b M) \u2243 ... \u2130 M ...\n \u2130 (M \u00b7 N) \u2243 ... \u2130 M ... \u2130 N ...\n\nThe compositionality property is not trivial because the semantics we\nhave defined includes three rules that are not syntax directed:\n`\u22a5-intro`, `\u2294-intro`, and `sub`.\n\nNext we investigate whether the denotational semantics and the\nreduction semantics are equivalent. Recall that the job of a language\nsemantics is to describe the observable behavior of a given program\n`M`. For the lambda calculus there are several choices that one can\nmake, but they usually boil down to a single bit of information:\n\n * divergence: the program `M` executes forever.\n * termination: the program `M` halts.\n\nWe can characterize divergence and termination in terms of reduction.\n\n * divergence: `\u00ac (M \u2014\u21a0 \u019b N)` for any term `N`.\n * termination: `M \u2014\u21a0 \u019b N` for some term `N`.\n\nWe can also characterize divergence and termination using denotations.\n\n * divergence: `\u00ac (\u2205 \u22a2 M \u2193 v \u21a6 w)` for any `v` and `w`.\n * termination: `\u2205 \u22a2 M \u2193 v \u21a6 w` for some `v` and `w`.\n\nAlternatively, we can use the denotation function `\u2130`. \n\n * divergence: `\u00ac (\u2130 M \u2243 \u2130 (\u019b N))` for any term `N`.\n * termination: `\u2130 M \u2243 \u2130 (\u019b N)` for some term `N`.\n\nSo the question is whether the reduction semantics and denotational\nsemantics are equivalent.\n\n (\u2203 N. M \u2014\u21a0 \u019b N) iff (\u2203 N. \u2130 M \u2243 \u2130 (\u019b N))\n\nWe address each direction of the equivalence in the second and third\nchapters. In the second chapter we prove that reduction to a lambda\nabstraction implies denotational equality to a lambda\nabstraction. This property is called the _soundness_ in the\nliterature.\n\n M \u2014\u21a0 \u019b N implies \u2130 M \u2243 \u2130 (\u019b N)\n\nIn the third chapter we prove that denotational equality to a lambda\nabstraction implies reduction to a lambda abstraction. This property\nis called _adequacy_ in the literature.\n\n \u2130 M \u2243 \u2130 (\u019b N) implies M \u2014\u21a0 \u019b N' for some N'\n\nThe proofs of these properties rely on some basic results about the\ndenotational semantics, which we establish in the rest of this\nchapter. We start with some lemmas about renaming, which are quite\nsimilar to the renaming lemmas that we have seen in previous chapters.\nWe conclude with a proof of an important inversion lemma for the\nless-than relation regarding function values.\n\n\n## Renaming preserves denotations\n\nWe shall prove that renaming variables, and changing the environment\naccordingly, preserves the meaning of a term. We generalize the\nrenaming lemma to allow the values in the new environment to be the\nsame or larger than the original values. This generalization is useful\nin proving that reduction implies denotational equality.\n\nAs before, we need an extension lemma to handle the case where we\nproceed underneath a lambda abstraction. Here, the nth function\nperforms lookup in the environment, analogous to `\u0393 \u220b A`. Now suppose\nthat `\u03c1` is a renaming that maps variables in `\u03b3` into variables with\nequal or larger values in `\u03b4`. This lemmas says that extending the\nrenaming producing a renaming `ext r` that maps `\u03b3 , v` to `\u03b4 , v`.\n\n\\begin{code}\nRename : Context \u2192 Context \u2192 Set\nRename \u0393 \u0394 = \u2200{A} \u2192 \u0393 \u220b A \u2192 \u0394 \u220b A\n\\end{code}\n\n\n\\begin{code}\next-nth : \u2200 {\u0393 \u0394 v} {\u03b3 : Env \u0393} {\u03b4 : Env \u0394}\n \u2192 (\u03c1 : Rename \u0393 \u0394)\n \u2192 \u03b3 `\u2291 (\u03b4 \u2218 \u03c1)\n ------------------------------\n \u2192 (\u03b3 `, v) `\u2291 ((\u03b4 `, v) \u2218 ext \u03c1)\next-nth \u03c1 lt Z = Refl\u2291\next-nth \u03c1 lt (S x) = lt x\n\\end{code}\n\nWe proceed by cases on the de Bruijn index `n`.\n\n* If it is `Z`, then we just need to show that `v \u2261 v`, which we have by `refl`.\n\n* If it is `S n'`, then the goal simplifies to `nth n' \u03b3 \u2261 nth (\u03c1 n') \u03b4`,\n which is an instance of the premise.\n\nNow for the renaming lemma. Suppose we have a renaming that maps\nvariables in `\u03b3` into variables with the same values in `\u03b4`. If `M`\nresults in `v` when evaluated in environment `\u03b3`, then applying the\nrenaming to `M` produces a program that results in the same value `v` when\nevaluated in `\u03b4`.\n\n\\begin{code}\nrename-pres : \u2200 {\u0393 \u0394 v} {\u03b3 : Env \u0393} {\u03b4 : Env \u0394} {M : \u0393 \u22a2 \u2605}\n \u2192 (\u03c1 : Rename \u0393 \u0394)\n \u2192 \u03b3 `\u2291 (\u03b4 \u2218 \u03c1)\n \u2192 \u03b3 \u22a2 M \u2193 v\n ---------------------\n \u2192 \u03b4 \u22a2 (rename \u03c1 M) \u2193 v\nrename-pres \u03c1 lt (var {x = x}) = sub var (lt x)\nrename-pres \u03c1 lt (\u21a6-elim d d\u2081) =\n \u21a6-elim (rename-pres \u03c1 lt d) (rename-pres \u03c1 lt d\u2081) \nrename-pres \u03c1 lt (\u21a6-intro d) =\n \u21a6-intro (rename-pres (ext \u03c1) (ext-nth \u03c1 lt) d)\nrename-pres \u03c1 lt \u22a5-intro = \u22a5-intro\nrename-pres \u03c1 lt (\u2294-intro d d\u2081) =\n \u2294-intro (rename-pres \u03c1 lt d) (rename-pres \u03c1 lt d\u2081)\nrename-pres \u03c1 lt (sub d lt\u2032) =\n sub (rename-pres \u03c1 lt d) lt\u2032\n\\end{code}\n\nThe proof is by induction on the semantics of `M`. As you can see, all\nof the cases are trivial except the cases for variables and lambda.\n\n* For a variable `x`, we make use of the premise to\n show that `nth x \u03b3 \u2261 nth (\u03c1 x) \u03b4`.\n\n* For a lambda abstraction, the induction hypothesis requires us to\n extend the renaming. We do so, and use the `ext-nth` lemma to show\n that the extended renaming maps variables to ones with equivalent\n values.\n\n\n## Identity renamings and environment strengthening\n\nWe shall need a corollary of the renaming lemma that says that if `M`\nresults in `v`, then we can replace a value in the environment with a\nlarger one (a stronger one), and `M` still results in `v`. In particular,\nif `\u03b3 \u22a2 M \u2193 v` and `\u03b3 \u2291 \u03b4`, then `\u03b4 \u22a2 M \u2193 v`. What does this have to do\nwith renaming? It's renaming with the identity function.\n\nThe next lemma shows that renaming with an identity function is indeed\nan identity function on terms. In the case of lambda abstraction, the\nidentity function gets extended, becoming another identity function,\nbut not the same one (Agda lacks extensionality). To work around this\nissue, we parameterize the proof over any function that is an\nidentity.\n\n[JGS: todo: use extensionality]\n\n\\begin{code}\nis-identity : \u2200 {\u0393} (id : \u2200{A} \u2192 \u0393 \u220b A \u2192 \u0393 \u220b A) \u2192 Set\nis-identity {\u0393} id = (\u2200 {x : \u0393 \u220b \u2605} \u2192 id {\u2605} x \u2261 x)\n\\end{code}\n\n\\begin{code}\nrename-id : \u2200 {\u0393} {M : \u0393 \u22a2 \u2605} {id : \u2200{A} \u2192 \u0393 \u220b A \u2192 \u0393 \u220b A}\n \u2192 is-identity id\n ---------------\n \u2192 rename id M \u2261 M\nrename-id {M = ` x} eq = cong `_ (eq {x})\nrename-id {M = \u019b N}{id = id} eq = cong \u019b_ (rename-id {M = N} ext-id)\n where\n ext-id : is-identity (ext id)\n ext-id {Z} = refl\n ext-id {S x} = cong S_ eq\nrename-id {M = L \u00b7 M} eq = cong\u2082 _\u00b7_ (rename-id eq) (rename-id eq)\n\\end{code}\n\nThe identity function on variables, `var-id`, is an identity function.\n\n\\begin{code}\nvar-id : \u2200 {\u0393 A} \u2192 (\u0393 \u220b A) \u2192 (\u0393 \u220b A)\nvar-id {A} x = x\n\nvar-id-id : \u2200 {\u0393} \u2192 is-identity {\u0393} var-id\nvar-id-id = refl\n\\end{code}\n\nWe can now prove environment strengthening by applying the renaming\nlemma with the identity renaming, which gives us\n`\u03b4 \u22a2 rename var-id M \u2193 v`, and then we apply the `rename-id` lemma\nto obtain `\u03b4 \u22a2 M \u2193 v`.\n\n\\begin{code}\nEnv\u2291 : \u2200 {\u0393} {\u03b3 : Env \u0393} {\u03b4 : Env \u0393} {M v}\n \u2192 \u03b3 \u22a2 M \u2193 v\n \u2192 \u03b3 `\u2291 \u03b4\n ----------\n \u2192 \u03b4 \u22a2 M \u2193 v\nEnv\u2291{\u0393}{\u03b3}{\u03b4}{M}{v} d lt\n with rename-pres var-id lt d\n... | d' rewrite rename-id {\u0393}{M}{var-id} (var-id-id {\u0393}) = d'\n\\end{code}\n\nIn the proof that substitution reflects denotations, in the case for\nlambda abstraction, we use a minor variation of `Env\u2291`, in which just\nthe last element of the environment gets larger.\n\n\\begin{code}\nup-env : \u2200 {\u0393} {\u03b3 : Env \u0393} {M v u\u2081 u\u2082}\n \u2192 (\u03b3 `, u\u2081) \u22a2 M \u2193 v\n \u2192 u\u2081 \u2291 u\u2082\n -----------------\n \u2192 (\u03b3 `, u\u2082) \u22a2 M \u2193 v\nup-env d lt = Env\u2291 d (nth-le lt)\n where\n nth-le : \u2200 {\u03b3 u\u2081 u\u2082} \u2192 u\u2081 \u2291 u\u2082 \u2192 (\u03b3 `, u\u2081) `\u2291 (\u03b3 `, u\u2082)\n nth-le lt Z = lt\n nth-le lt (S n) = Refl\u2291\n\\end{code}\n\n\n## Inversion of the less-than relation for functions\n\nWhat can we deduce from knowing that a function `v \u21a6 w` is less than\nsome value `u`? What can we deduce about `u`? The answer to this\nquestion is called the inversion property of less-than for functions.\nThis question is not easy to answer because of the `Dist\u2291` rule, which\nrelates a function on the left to a pair of functions on the right.\nSo `u` may include several functions that, as a group, relate to\n`v \u21a6 w`. Furthermore, because of the rules `ConjR1\u2291` and `ConjR2\u2291`,\nthere may be other values inside `u`, such as `\u22a5`, that have nothing\nto do with `v \u21a6 w`. But in general, we can deduce that `u` includes\na collection of functions where the join of their domains is less\nthan `v` and the join of their codomains is greater than `w`.\n\nTo precisely state and prove this inversion property, we need to\ndefine what it means for a value to _include_ a collection of values.\nWe also need to define how to compute the join of their domains and\ncodomains.\n\n\n### Value membership and inclusion\n\nRecall that we think of a value as a set of entries with the join\noperator `v \u2294 w` acting like set union. The function value `v \u21a6 w` and\nbottom value `\u22a5` constitute the two kinds of elements of the set. (In\nother contexts one can instead think of `\u22a5` as the empty set, but here\nwe must think of it as an element.) We write `u \u2208 v` to say that `u` is\nan element of `v`, as defined below.\n\n\\begin{code}\ninfix 5 _\u2208_\n\n_\u2208_ : Value \u2192 Value \u2192 Set\nu \u2208 \u22a5 = u \u2261 \u22a5\nu \u2208 v \u21a6 w = u \u2261 v \u21a6 w\nu \u2208 (v \u2294 w) = u \u2208 v \u228e u \u2208 w\n\\end{code}\n\nSo we can represent a collection of values simply as a value. We\nwrite `v \u2286 w` to say that all the elements of `v` are also in `w`.\n\n\\begin{code}\ninfix 5 _\u2286_\n\n_\u2286_ : Value \u2192 Value \u2192 Set\nv \u2286 w = \u2200{u} \u2192 u \u2208 v \u2192 u \u2208 w\n\\end{code}\n\nThe notions of membership and inclusion for values are closely related\nto the less-than relation. They are narrower relations in that they\nimply the less-than relation but not the other way around.\n\n\\begin{code}\n\u2208\u2192\u2291 : \u2200{u v : Value}\n \u2192 u \u2208 v\n -----\n \u2192 u \u2291 v\n\u2208\u2192\u2291 {.\u22a5} {\u22a5} refl = Bot\u2291\n\u2208\u2192\u2291 {v \u21a6 w} {v \u21a6 w} refl = Refl\u2291\n\u2208\u2192\u2291 {u} {v \u2294 w} (inj\u2081 x) = ConjR1\u2291 (\u2208\u2192\u2291 x)\n\u2208\u2192\u2291 {u} {v \u2294 w} (inj\u2082 y) = ConjR2\u2291 (\u2208\u2192\u2291 y)\n\n\u2286\u2192\u2291 : \u2200{u v : Value}\n \u2192 u \u2286 v\n -----\n \u2192 u \u2291 v\n\u2286\u2192\u2291 {\u22a5} s with s {\u22a5} refl\n... | x = Bot\u2291\n\u2286\u2192\u2291 {u \u21a6 u'} s with s {u \u21a6 u'} refl\n... | x = \u2208\u2192\u2291 x\n\u2286\u2192\u2291 {u \u2294 u'} s = ConjL\u2291 (\u2286\u2192\u2291 (\u03bb z \u2192 s (inj\u2081 z))) (\u2286\u2192\u2291 (\u03bb z \u2192 s (inj\u2082 z)))\n\\end{code}\n\nWe shall also need some inversion principles for value inclusion. If\nthe union of `u` and `v` is included in `w`, then of course both `u` and\n`v` are each included in `w`.\n\n\\begin{code}\n\u2294\u2286-inv : \u2200{u v w : Value}\n \u2192 (u \u2294 v) \u2286 w\n ---------------\n \u2192 u \u2286 w \u00d7 v \u2286 w\n\u2294\u2286-inv uvw = \u27e8 (\u03bb x \u2192 uvw (inj\u2081 x)) , (\u03bb x \u2192 uvw (inj\u2082 x)) \u27e9\n\\end{code}\n\nIn our value representation, the function value `v \u21a6 w` is both an\nelement and also a singleton set. So if `v \u21a6 w` is a subset of `u`,\nthen `v \u21a6 w` must be a member of `u`.\n\n\\begin{code}\n\u21a6\u2286\u2192\u2208 : \u2200{v w u : Value}\n \u2192 v \u21a6 w \u2286 u\n ---------\n \u2192 v \u21a6 w \u2208 u\n\u21a6\u2286\u2192\u2208 incl = incl refl \n\\end{code}\n\n\n### Function values\n\nTo identify collections of functions, we define the following two\npredicates. We write `Fun u` if `u` is a function value, that is, if\n`u \u2261 v \u21a6 w` for some values `v` and `w`. We write `Funs v` if all the elements\nof `v` are functions.\n\n\\begin{code}\ndata Fun : Value \u2192 Set where\n fun : \u2200{u v w} \u2192 u \u2261 (v \u21a6 w) \u2192 Fun u\n\nFuns : Value \u2192 Set\nFuns v = \u2200{u} \u2192 u \u2208 v \u2192 Fun u\n\\end{code}\n\nThe value `\u22a5` is not a function.\n\n\\begin{code}\n\u00acFun\u22a5 : \u00ac (Fun \u22a5)\n\u00acFun\u22a5 (fun ())\n\\end{code}\n\nIn our values-as-sets representation, our sets always include at least\none element. Thus, if all the elements are functions, there is at\nleast one that is a function.\n\n\\begin{code}\nFuns\u2208 : \u2200{u}\n \u2192 Funs u\n \u2192 \u03a3[ v \u2208 Value ] \u03a3[ w \u2208 Value ] v \u21a6 w \u2208 u\nFuns\u2208 {\u22a5} f with f {\u22a5} refl\n... | fun ()\nFuns\u2208 {v \u21a6 w} f = \u27e8 v , \u27e8 w , refl \u27e9 \u27e9\nFuns\u2208 {u \u2294 u'} f\n with Funs\u2208 \u03bb z \u2192 f (inj\u2081 z)\n... | \u27e8 v , \u27e8 w , m \u27e9 \u27e9 = \u27e8 v , \u27e8 w , (inj\u2081 m) \u27e9 \u27e9\n\\end{code}\n\n\n### Domains and codomains\n\nReturning to our goal, the inversion principle for less-than a\nfunction, we want to show that `v \u21a6 w \u2291 u` implies that `u` includes\na set of function values such that the join of their domains is less\nthan `v` and the join of their codomains is greater than `w`.\n\nTo this end we define the following dom and cod functions. Given some\nvalue `u` (that represents a set of entries), `dom u` returns the join of\ntheir domains and `cod u` returns the join of their codomains.\n\n\\begin{code}\ndom : (u : Value) \u2192 Value\ndom \u22a5 = \u22a5\ndom (v \u21a6 w) = v\ndom (u \u2294 u') = dom u \u2294 dom u'\n\ncod : (u : Value) \u2192 Value\ncod \u22a5 = \u22a5\ncod (v \u21a6 w) = w\ncod (u \u2294 u') = cod u \u2294 cod u'\n\\end{code}\n\nWe need just one property each for `dom` and `cod`. Given a collection of\nfunctions represented by value `u`, and an entry `v \u21a6 w \u2208 u`, we know\nthat `v` is included in the domain of `v`.\n\n\\begin{code}\n\u21a6\u2208\u2192\u2286dom : \u2200{u v w : Value}\n \u2192 Funs u \u2192 (v \u21a6 w) \u2208 u\n ----------------------\n \u2192 v \u2286 dom u\n\u21a6\u2208\u2192\u2286dom {\u22a5} fg () u\u2208v\n\u21a6\u2208\u2192\u2286dom {v \u21a6 w} fg refl u\u2208v = u\u2208v\n\u21a6\u2208\u2192\u2286dom {u \u2294 u'} fg (inj\u2081 v\u21a6w\u2208u) u\u2208v =\n let ih = \u21a6\u2208\u2192\u2286dom (\u03bb z \u2192 fg (inj\u2081 z)) v\u21a6w\u2208u in\n inj\u2081 (ih u\u2208v)\n\u21a6\u2208\u2192\u2286dom {u \u2294 u'} fg (inj\u2082 v\u21a6w\u2208u') u\u2208v =\n let ih = \u21a6\u2208\u2192\u2286dom (\u03bb z \u2192 fg (inj\u2082 z)) v\u21a6w\u2208u' in\n inj\u2082 (ih u\u2208v)\n\\end{code}\n\nRegarding `cod`, suppose we have a collection of functions represented\nby `u`, but all of them are just copies of `v \u21a6 w`. Then the `cod u` is\nincluded in `w`.\n\n\\begin{code}\n\u2286\u21a6\u2192cod\u2286 : \u2200{u v w : Value}\n \u2192 u \u2286 v \u21a6 w\n ---------\n \u2192 cod u \u2286 w\n\u2286\u21a6\u2192cod\u2286 {\u22a5} s refl with s {\u22a5} refl\n... | ()\n\u2286\u21a6\u2192cod\u2286 {C \u21a6 C'} s m with s {C \u21a6 C'} refl\n... | refl = m\n\u2286\u21a6\u2192cod\u2286 {u \u2294 u'} s (inj\u2081 x) = \u2286\u21a6\u2192cod\u2286 (\u03bb {C} z \u2192 s (inj\u2081 z)) x\n\u2286\u21a6\u2192cod\u2286 {u \u2294 u'} s (inj\u2082 y) = \u2286\u21a6\u2192cod\u2286 (\u03bb {C} z \u2192 s (inj\u2082 z)) y\n\\end{code}\n\nWith the `dom` and `cod` functions in hand, we can make precise the\nconclusion of the inversion principle for functions, which we package\ninto the following predicate named `factor`. We say that `v \u21a6 w`\n_factors_ `u` into `u'` if `u'` is a included in `u`, if `u'` contains only\nfunctions, its domain is less than `v`, and its codomain is greater\nthan `w`.\n\n\\begin{code}\nfactor : (u : Value) \u2192 (u' : Value) \u2192 (v : Value) \u2192 (w : Value) \u2192 Set\nfactor u u' v w = Funs u' \u00d7 u' \u2286 u \u00d7 dom u' \u2291 v \u00d7 w \u2291 cod u'\n\\end{code}\n\nWe prove the inversion principle for functions by induction on the\nderivation of the less-than relation. To make the induction hypothesis\nstronger, we broaden the premise to `u\u2081 \u2291 u\u2082` (instead of `v \u21a6 w \u2291\nu`), and strengthen the conclusion to say that for _every_ function\nvalue `v \u21a6 w \u2208 u\u2081`, we have that `v \u21a6 w` factors `u\u2082` into some\nvalue `u\u2083`.\n\n### Inversion of less-than for functions, the case for Trans\u2291\n\nThe crux of the proof is the case for `Trans\u2291`.\n\n u\u2081 \u2291 u u \u2291 u\u2082\n --------------- (Trans\u2291)\n u\u2081 \u2291 u\u2082\n\nBy the induction hypothesis for `u\u2081 \u2291 u`, we know\nthat `v \u21a6 w factors u into u'`, for some value `u'`,\nso we have `Funs u'` and `u' \u2286 u`.\nBy the induction hypothesis for `u \u2291 u\u2082`, we know\nthat for any `v' \u21a6 w' \u2208 u`, `v' \u21a6 w'` factors `u\u2082` into `u\u2083`.\nWith these facts in hand, we proceed by induction on `u'`\nto prove that `(dom u') \u21a6 (cod u')` factors `u\u2082` into `u\u2083`.\nWe discuss each case of the proof in the text below.\n\n\\begin{code}\nsub-inv-trans : \u2200{u' u\u2082 u : Value}\n \u2192 Funs u' \u2192 u' \u2286 u\n \u2192 (\u2200{v' w'} \u2192 v' \u21a6 w' \u2208 u \u2192 \u03a3[ u\u2083 \u2208 Value ] factor u\u2082 u\u2083 v' w')\n ---------------------------------------------------------------\n \u2192 \u03a3[ u\u2083 \u2208 Value ] factor u\u2082 u\u2083 (dom u') (cod u')\nsub-inv-trans {\u22a5} {u\u2082} {u} fu' u'\u2286u IH =\n \u22a5-elim (contradiction (fu' refl) \u00acFun\u22a5)\nsub-inv-trans {u\u2081' \u21a6 u\u2082'} {u\u2082} {u} fg u'\u2286u IH = IH (\u21a6\u2286\u2192\u2208 u'\u2286u)\nsub-inv-trans {u\u2081' \u2294 u\u2082'} {u\u2082} {u} fg u'\u2286u IH\n with \u2294\u2286-inv u'\u2286u\n... | \u27e8 u\u2081'\u2286u , u\u2082'\u2286u \u27e9\n with sub-inv-trans {u\u2081'} {u\u2082} {u} (\u03bb {v'} z \u2192 fg (inj\u2081 z)) u\u2081'\u2286u IH\n | sub-inv-trans {u\u2082'} {u\u2082} {u} (\u03bb {v'} z \u2192 fg (inj\u2082 z)) u\u2082'\u2286u IH\n... | \u27e8 u\u2083\u2081 , \u27e8 fu21' , \u27e8 u\u2083\u2081\u2286u\u2082 , \u27e8 du\u2083\u2081\u2291du\u2081' , cu\u2081'\u2291cu\u2083\u2081 \u27e9 \u27e9 \u27e9 \u27e9\n | \u27e8 u\u2083\u2082 , \u27e8 fu22' , \u27e8 u\u2083\u2082\u2286u\u2082 , \u27e8 du\u2083\u2082\u2291du\u2082' , cu\u2081'\u2291cu\u2083\u2082 \u27e9 \u27e9 \u27e9 \u27e9 =\n \u27e8 (u\u2083\u2081 \u2294 u\u2083\u2082) , \u27e8 fu\u2082' , \u27e8 u\u2082'\u2286u\u2082 ,\n \u27e8 \u2294\u2291\u2294 du\u2083\u2081\u2291du\u2081' du\u2083\u2082\u2291du\u2082' ,\n \u2294\u2291\u2294 cu\u2081'\u2291cu\u2083\u2081 cu\u2081'\u2291cu\u2083\u2082 \u27e9 \u27e9 \u27e9 \u27e9\n where fu\u2082' : {v' : Value} \u2192 v' \u2208 u\u2083\u2081 \u228e v' \u2208 u\u2083\u2082 \u2192 Fun v'\n fu\u2082' {v'} (inj\u2081 x) = fu21' x\n fu\u2082' {v'} (inj\u2082 y) = fu22' y\n u\u2082'\u2286u\u2082 : {C : Value} \u2192 C \u2208 u\u2083\u2081 \u228e C \u2208 u\u2083\u2082 \u2192 C \u2208 u\u2082\n u\u2082'\u2286u\u2082 {C} (inj\u2081 x) = u\u2083\u2081\u2286u\u2082 x\n u\u2082'\u2286u\u2082 {C} (inj\u2082 y) = u\u2083\u2082\u2286u\u2082 y\n\\end{code}\n\n* Suppose `u' \u2261 \u22a5`. Then we have a contradiction because\n it is not the case that `Fun \u22a5`.\n\n* Suppose `u' \u2261 u\u2081' \u21a6 u\u2082'`. Then `u\u2081' \u21a6 u\u2082' \u2208 u` and we can apply the\n premise (the induction hypothesis from `u \u2291 u\u2082`) to obtain that\n `u\u2081' \u21a6 u\u2082'` factors of `u\u2082 into u\u2082'`. This case is complete because\n `dom u' \u2261 u\u2081'` and `cod u' \u2261 u\u2082'`.\n \n* Suppose `u' \u2261 u\u2081' \u2294 u\u2082'`. Then we have `u\u2081' \u2286 u` and `u\u2082' \u2286 u`. We also \n have `Funs u\u2081'` and `Funs u\u2082'`, so we can apply the induction hypothesis\n for both `u\u2081'` and `u\u2082'`. So there exists values `u\u2083\u2081` and `u\u2083\u2082` such that\n `(dom u\u2081') \u21a6 (cod u\u2081')` factors `u` into `u\u2083\u2081` and\n `(dom u\u2082') \u21a6 (cod u\u2082')` factors `u` into `u\u2083\u2082`.\n We will show that `(dom u) \u21a6 (cod u)` factors `u` into `u\u2083\u2081 \u2294 u\u2083\u2082`.\n So we need to show that\n \n dom (u\u2083\u2081 \u2294 u\u2083\u2082) \u2291 dom (u\u2081' \u2294 u\u2082')\n cod (u\u2081' \u2294 u\u2082') \u2291 cod (u\u2083\u2081 \u2294 u\u2083\u2082)\n \n But those both follow directly from the factoring of\n `u` into `u\u2083\u2081` and `u\u2083\u2082`, using the monotonicity of `\u2294` with respect to `\u2291`.\n \n\n### Inversion of less-than for functions\n\nWe come to the proof of the main lemma concerning the inversion of\nless-than for functions. We show that if `u\u2081 \u2291 u\u2082`, then for any\n`v \u21a6 w \u2208 u\u2081`, we can factor `u\u2082` into `u\u2083` according to `v \u21a6 w`. We proceed\nby induction on the derivation of `u\u2081 \u2291 u\u2082`, and describe each case in\nthe text after the Agda proof.\n\n\\begin{code}\nsub-inv : \u2200{u\u2081 u\u2082 : Value}\n \u2192 u\u2081 \u2291 u\u2082\n \u2192 \u2200{v w} \u2192 v \u21a6 w \u2208 u\u2081\n -------------------------------------\n \u2192 \u03a3[ u\u2083 \u2208 Value ] factor u\u2082 u\u2083 v w\nsub-inv {\u22a5} {u\u2082} Bot\u2291 {v} {w} ()\nsub-inv {u\u2081\u2081 \u2294 u\u2081\u2082} {u\u2082} (ConjL\u2291 lt1 lt2) {v} {w} (inj\u2081 x) = sub-inv lt1 x\nsub-inv {u\u2081\u2081 \u2294 u\u2081\u2082} {u\u2082} (ConjL\u2291 lt1 lt2) {v} {w} (inj\u2082 y) = sub-inv lt2 y\nsub-inv {u\u2081} {u\u2082\u2081 \u2294 u\u2082\u2082} (ConjR1\u2291 lt) {v} {w} m\n with sub-inv lt m \n... | \u27e8 u\u2083\u2081 , \u27e8 fu\u2083\u2081 , \u27e8 u\u2083\u2081\u2286u\u2082\u2081 , \u27e8 domu\u2083\u2081\u2291v , w\u2291codu\u2083\u2081 \u27e9 \u27e9 \u27e9 \u27e9 =\n \u27e8 u\u2083\u2081 , \u27e8 fu\u2083\u2081 , \u27e8 (\u03bb {w} z \u2192 inj\u2081 (u\u2083\u2081\u2286u\u2082\u2081 z)) ,\n \u27e8 domu\u2083\u2081\u2291v , w\u2291codu\u2083\u2081 \u27e9 \u27e9 \u27e9 \u27e9\nsub-inv {u\u2081} {u\u2082\u2081 \u2294 u\u2082\u2082} (ConjR2\u2291 lt) {v} {w} m\n with sub-inv lt m \n... | \u27e8 u\u2083\u2082 , \u27e8 fu\u2083\u2082 , \u27e8 u\u2083\u2082\u2286u\u2082\u2082 , \u27e8 domu\u2083\u2082\u2291v , w\u2291codu\u2083\u2082 \u27e9 \u27e9 \u27e9 \u27e9 =\n \u27e8 u\u2083\u2082 , \u27e8 fu\u2083\u2082 , \u27e8 (\u03bb {C} z \u2192 inj\u2082 (u\u2083\u2082\u2286u\u2082\u2082 z)) ,\n \u27e8 domu\u2083\u2082\u2291v , w\u2291codu\u2083\u2082 \u27e9 \u27e9 \u27e9 \u27e9\nsub-inv {u\u2081} {u\u2082} (Trans\u2291{v = u} u\u2081\u2291u u\u2291u\u2082) {v} {w} v\u21a6w\u2208u\u2081\n with sub-inv u\u2081\u2291u v\u21a6w\u2208u\u2081\n... | \u27e8 u' , \u27e8 fu' , \u27e8 u'\u2286u , \u27e8 domu'\u2291v , w\u2291codu' \u27e9 \u27e9 \u27e9 \u27e9 \n with sub-inv-trans {u'} fu' u'\u2286u (sub-inv u\u2291u\u2082) \n... | \u27e8 u\u2083 , \u27e8 fu\u2083 , \u27e8 u\u2083\u2286u\u2082 , \u27e8 domu\u2083\u2291domu' , codu'\u2291codu\u2083 \u27e9 \u27e9 \u27e9 \u27e9 =\n \u27e8 u\u2083 , \u27e8 fu\u2083 , \u27e8 u\u2083\u2286u\u2082 , \u27e8 Trans\u2291 domu\u2083\u2291domu' domu'\u2291v ,\n Trans\u2291 w\u2291codu' codu'\u2291codu\u2083 \u27e9 \u27e9 \u27e9 \u27e9\nsub-inv {u\u2081\u2081 \u21a6 u\u2081\u2082} {u\u2082\u2081 \u21a6 u\u2082\u2082} (Fun\u2291 lt1 lt2) refl =\n \u27e8 u\u2082\u2081 \u21a6 u\u2082\u2082 , \u27e8 (\u03bb {w} \u2192 fun) , \u27e8 (\u03bb {C} z \u2192 z) , \u27e8 lt1 , lt2 \u27e9 \u27e9 \u27e9 \u27e9\nsub-inv {u\u2082\u2081 \u21a6 (u\u2082\u2082 \u2294 u\u2082\u2083)} {u\u2082\u2081 \u21a6 u\u2082\u2082 \u2294 u\u2082\u2081 \u21a6 u\u2082\u2083} Dist\u2291\n {.u\u2082\u2081} {.(u\u2082\u2082 \u2294 u\u2082\u2083)} refl =\n \u27e8 u\u2082\u2081 \u21a6 u\u2082\u2082 \u2294 u\u2082\u2081 \u21a6 u\u2082\u2083 , \u27e8 f , \u27e8 g , \u27e8 ConjL\u2291 Refl\u2291 Refl\u2291 , Refl\u2291 \u27e9 \u27e9 \u27e9 \u27e9\n where f : Funs (u\u2082\u2081 \u21a6 u\u2082\u2082 \u2294 u\u2082\u2081 \u21a6 u\u2082\u2083)\n f (inj\u2081 x) = fun x\n f (inj\u2082 y) = fun y\n g : (u\u2082\u2081 \u21a6 u\u2082\u2082 \u2294 u\u2082\u2081 \u21a6 u\u2082\u2083) \u2286 (u\u2082\u2081 \u21a6 u\u2082\u2082 \u2294 u\u2082\u2081 \u21a6 u\u2082\u2083)\n g (inj\u2081 x) = inj\u2081 x\n g (inj\u2082 y) = inj\u2082 y\n\\end{code}\n\nLet `v` and `w` be arbitrary values.\n\n* Case `Bot\u2291`. So `u\u2081 \u2261 \u22a5`. We have `v \u21a6 w \u2208 \u22a5`, but that is impossible.\n\n* Case `ConjL\u2291`.\n\n u\u2081\u2081 \u2291 u\u2082 u\u2081\u2082 \u2291 u\u2082\n -------------------\n u\u2081\u2081 \u2294 u\u2081\u2082 \u2291 u\u2082\n\n Given that `v \u21a6 w \u2208 u\u2081\u2081 \u2294 u\u2081\u2082`, there are two subcases to consider.\n\n * Subcase `v \u21a6 w \u2208 u\u2081\u2081`. We conclude by the induction\n hypothesis for `u\u2081\u2081 \u2291 u\u2082`.\n \n * Subcase `v \u21a6 w \u2208 u\u2081\u2082`. We conclude by the induction hypothesis\n for `u\u2081\u2082 \u2291 u\u2082`.\n\n* Case `ConjR1\u2291`.\n\n u\u2081 \u2291 u\u2082\u2081\n --------------\n u\u2081 \u2291 u\u2082\u2081 \u2294 u\u2082\u2082\n\n Given that `v \u21a6 w \u2208 u\u2081`, the induction hypothesis for `u\u2081 \u2291 u\u2082\u2081`\n gives us that `v \u21a6 w` factors `u\u2082\u2081` into `u\u2083\u2081` for some `u\u2083\u2081`.\n To show that `v \u21a6 w` also factors `u\u2082\u2081 \u2294 u\u2082\u2082` into `u\u2083\u2081`,\n we just need to show that `u\u2083\u2081 \u2286 u\u2082\u2081 \u2294 u\u2082\u2082`, but that follows\n directly from `u\u2083\u2081 \u2286 u\u2082\u2081`.\n\n* Case `ConjR2\u2291`. This case follows by reasoning similar to\n the case for `ConjR1\u2291`.\n\n* Case `Trans\u2291`. \n\n u\u2081 \u2291 u u \u2291 u\u2082\n ---------------\n u\u2081 \u2291 u\u2082\n \n By the induction hypothesis for `u\u2081 \u2291 u`, we know\n that `v \u21a6 w` factors `u` into `u'`, for some value `u'`,\n so we have `Funs u'` and `u' \u2286 u`.\n By the induction hypothesis for `u \u2291 u\u2082`, we know\n that for any `v' \u21a6 w' \u2208 u`, `v' \u21a6 w'` factors `u\u2082`.\n Now we apply the lemma sub-inv-trans, which gives us\n some `u\u2083` such that `(dom u') \u21a6 (cod u')` factors `u\u2082` into `u\u2083`.\n We show that `v \u21a6 w` also factors `u\u2082` into `u\u2083`.\n From `dom u\u2083 \u2291 dom u'` and `dom u' \u2291 v`, we have `dom u\u2083 \u2291 v`.\n From `w \u2291 cod u'` and `cod u' \u2291 cod u\u2083`, we have `w \u2291 cod u\u2083`,\n and this case is complete.\n\n* Case `Fun\u2291`.\n\n u\u2082\u2081 \u2291 u\u2081\u2081 u\u2081\u2082 \u2291 u\u2082\u2082\n ---------------------\n u\u2081\u2081 \u21a6 u\u2081\u2082 \u2291 u\u2082\u2081 \u21a6 u\u2082\u2082\n\n Given that `v \u21a6 w \u2208 u\u2081\u2081 \u21a6 u\u2081\u2082`, we have `v \u2261 u\u2081\u2081` and `w \u2261 u\u2081\u2082`.\n We show that `u\u2081\u2081 \u21a6 u\u2081\u2082` factors `u\u2082\u2081 \u21a6 u\u2082\u2082` into itself.\n We need to show that `dom (u\u2082\u2081 \u21a6 u\u2082\u2082) \u2291 u\u2081\u2081` and `u\u2081\u2082 \u2291 cod (u\u2082\u2081 \u21a6 u\u2082\u2082)`,\n but that is equivalent to our premises `u\u2082\u2081 \u2291 u\u2081\u2081` and `u\u2081\u2082 \u2291 u\u2082\u2082`.\n\n* Case `Dist\u2291`.\n\n ---------------------------------------------\n u\u2082\u2081 \u21a6 (u\u2082\u2082 \u2294 u\u2082\u2083) \u2291 (u\u2082\u2081 \u21a6 u\u2082\u2082) \u2294 (u\u2082\u2081 \u21a6 u\u2082\u2083)\n\n Given that `v \u21a6 w \u2208 u\u2082\u2081 \u21a6 (u\u2082\u2082 \u2294 u\u2082\u2083)`, we have `v \u2261 u\u2082\u2081`\n and `w \u2261 u\u2082\u2082 \u2294 u\u2082\u2083`.\n We show that `u\u2082\u2081 \u21a6 (u\u2082\u2082 \u2294 u\u2082\u2083)` factors `(u\u2082\u2081 \u21a6 u\u2082\u2082) \u2294 (u\u2082\u2081 \u21a6 u\u2082\u2083)`\n into itself. We have `u\u2082\u2081 \u2294 u\u2082\u2081 \u2291 u\u2082\u2081`, and also\n `u\u2082\u2082 \u2294 u\u2082\u2083 \u2291 u\u2082\u2082 \u2294 u\u2082\u2083`, so the proof is complete.\n\n\nWe conclude this section with two corollaries of the sub-inv lemma.\nFirst, we have the following property that is convenient to use in\nlater proofs. We specialize the premise to just `v \u21a6 w \u2291 u\u2081`\nand we modify the conclusion to say that for every\n`v' \u21a6 w' \u2208 u\u2082`, we have `v' \u2291 v`.\n\n\\begin{code}\nsub-inv-fun : \u2200{v w u\u2081 : Value}\n \u2192 (v \u21a6 w) \u2291 u\u2081\n -----------------------------------------------------\n \u2192 \u03a3[ u\u2082 \u2208 Value ] Funs u\u2082 \u00d7 u\u2082 \u2286 u\u2081\n \u00d7 (\u2200{v' w'} \u2192 (v' \u21a6 w') \u2208 u\u2082 \u2192 v' \u2291 v) \u00d7 w \u2291 cod u\u2082\nsub-inv-fun{v}{w}{u\u2081} abc\n with sub-inv abc {v}{w} refl\n... | \u27e8 u\u2082 , \u27e8 f , \u27e8 u\u2082\u2286u\u2081 , \u27e8 db , cc \u27e9 \u27e9 \u27e9 \u27e9 =\n \u27e8 u\u2082 , \u27e8 f , \u27e8 u\u2082\u2286u\u2081 , \u27e8 G , cc \u27e9 \u27e9 \u27e9 \u27e9\n where G : \u2200{D E} \u2192 (D \u21a6 E) \u2208 u\u2082 \u2192 D \u2291 v\n G{D}{E} m = Trans\u2291 (\u2286\u2192\u2291 (\u21a6\u2208\u2192\u2286dom f m)) db\n\\end{code}\n\nThe second corollary is the inversion rule that one would expect for\nless-than with functions on the left and right-hand sides.\n\n\\begin{code}\n\u21a6\u2291\u21a6-inv : \u2200{v w v' w'}\n \u2192 v \u21a6 w \u2291 v' \u21a6 w'\n -----------------\n \u2192 v' \u2291 v \u00d7 w \u2291 w'\n\u21a6\u2291\u21a6-inv{v}{w}{v'}{w'} lt\n with sub-inv-fun lt \n... | \u27e8 \u0393 , \u27e8 f , \u27e8 \u0393\u2286v34 , \u27e8 lt1 , lt2 \u27e9 \u27e9 \u27e9 \u27e9\n with Funs\u2208 f\n... | \u27e8 u , \u27e8 u' , u\u21a6u'\u2208\u0393 \u27e9 \u27e9\n with \u0393\u2286v34 u\u21a6u'\u2208\u0393\n... | refl = \n let cod\u0393\u2286w' = \u2286\u21a6\u2192cod\u2286 \u0393\u2286v34 in\n \u27e8 lt1 u\u21a6u'\u2208\u0393 , Trans\u2291 lt2 (\u2286\u2192\u2291 cod\u0393\u2286w') \u27e9\n\\end{code}\n\n\n## Notes\n\nThe denotational semantics presented in this chapter is an example of\na _filter model_ (Barendregt, Coppo, Dezani-Ciancaglini, 1983). Filter\nmodels use type systems with intersection types to precisely\ncharacterize runtime behavior (Coppo, Dezani-Ciancaglini, and Salle,\n1979). The notation that we use in this chapter is not that of type\nsystems and intersection types, but the `Value` data type is isomorphic\nto types (`\u21a6` is `\u2192`, `\u2294` is `\u2227`, `\u22a5` is `\u22a4`), the `\u2291` relation is the\ninverse of subtyping `<:`, and the evaluation relation `\u03c1 \u22a2 M \u2193 v` is\nisomorphic to a type system. Write `\u0393` instead of `\u03c1`, `A` instead of `v`, and\nreplace `\u2193` with `:` and one has a typing judgement `\u0393 \u22a2 M : A`.\nBy varying the definition of subtyping and using different choices of\ntype atoms, intersection type systems provide semantics for many different\nuntyped \u03bb calculi, from full beta to the lazy and call-by-value calculi\n(Alessi, Barbanera, and Dezani-Ciancaglini, 2006) (Rocca and Paolini, 2004).\nThe denotational semantics in this chapter corresponds to the BCD\nsystem (Barendregt, Coppo, Dezani-Ciancaglini, 1983). Part 3 of the\nbook _Lambda Calculus with Types_ describes a framework for\nintersection type systems that enables results similar to the ones in\nthis chapter, but for the entire family of intersection type systems\n(Barendregt, Dekkers, and Statman, 2013).\n\nThe two ideas of using finite tables to represent functions and of\nrelaxing table lookup to enable self application first appeared in a\ntechnical report by Gordon Plotkin (1972) and are later described in\nan article in Theoretical Computer Science (Plotkin 1993). In that\nwork, the inductive definition of `Value` is a bit different than the\none we use:\n \n Value = C + \u2118f(Value) \u00d7 \u2118f(Value)\n \nwhere `C` is a set of constants and `\u2118f` means finite powerset. The pairs\nin `\u2118f(Value) \u00d7 \u2118f(Value)` represent input-output mappings, just as in\nthis chapter. The finite powersets are used to enable a function table\nto appear in the input and in the output. These differences amount to\nchanging where the recursion appears in the definition of `Value`.\nPlotkin's model is an example of a _graph model_ of the untyped lambda\ncalculus (Barendregt, 1984). In a graph model, the semantics is\npresented as a function from programs and environments to (possibly\ninfinite) sets of values. The semantics in this chapter is instead\ndefined as a relation, but set-valued functions are isomorphic to\nrelations. We choose to present the semantics as a relation because\nthe functional approach requires a kind of existential quantifier that\nis not present in Agda.\n\n[PLW: What kind of existential is required?]\n\nDana Scott's \u2118(\u03c9) (1976) and Engeler's B(A) (1981) are two more\nexamples of graph models. Both use the following inductive definition\nof `Value`.\n \n Value = C + \u2118f(Value) \u00d7 Value\n \nThe use of `Value` instead of `\u2118f(Value)` in the output does not restrict\nexpressiveness compared to Plotkin's model because the semantics use\nsets of values and a pair of sets `(V, V')` can be represented as a set\nof pairs `{ (V, v') | v' \u2208 V' }`. In Scott's \u2118(\u03c9), the above values are\nmapped to and from the natural numbers using a kind of Godel encoding.\n\n\n## References\n\n* Intersection Types and Lambda Models. Fabio Alessi, Franco\n Barbanera, and Mariangiola Dezani-Ciancaglini, Theoretical\n Compututer Science, vol. 355, pages 108-126, 2006.\n\n* The Lambda Calculus. H.P. Barendregt, 1984.\n\n* A filter lambda model and the completeness of type assignment. Henk\n Barendregt, Mario Coppo, and Mariangiola Dezani-Ciancaglini, Journal\n of Symbolic Logic, vol. 48, pages 931-940, 1983.\n\n* Lambda Calculus with Types. Henk Barendregt, Wil Dekkers, and\n Richard Statman, Cambridge University Press, Perspectives in Logic,\n 2013.\n\n* Functional characterization of some semantic equalities inside\n \u03bb-calculus. Mario Coppo, Mariangiola Dezani-Ciancaglini, and Patrick\n Salle, in Sixth Colloquium on Automata, Languages and Programming.\n Springer, pages 133--146, 1979.\n\n* Algebras and combinators. Erwin Engeler, Algebra Universalis,\n vol. 13, pages 389-392, 1981.\n\n* A Set-Theoretical Definition of Application. Gordon D. Plotkin,\n University of Edinburgh, Technical Report MIP-R-95, 1972.\n\n* Set-theoretical and other elementary models of the \u03bb-calculus.\n Gordon D. Plotkin, Theoretical Computer Science, vol. 121,\n pages 351-409, 1993.\n\n* The Parametric Lambda Calculus. Simona Ronchi Della Rocca and Luca\n Paolini, Springer, 2004.\n\n* Data Types as Lattices. Dana Scott, SIAM Journal on Computing,\n vol. 5, pages 522-587, 1976.\n\n","avg_line_length":34.3220216606,"max_line_length":100,"alphanum_fraction":0.6193411309} +{"size":37455,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Inference: Bidirectional type inference\"\nlayout : page\nprev : \/Bisimulation\/\npermalink : \/Inference\/\nnext : \/Untyped\/\n---\n\n\\begin{code}\nmodule plfa.Inference where\n\\end{code}\n\nSo far in our development, type derivations for the corresponding\nterm have been provided by fiat.\nIn Chapter [Lambda][plfa.Lambda]\ntype derivations were given separately from the term, while\nin Chapter [DeBruijn][plfa.DeBruijn]\nthe type derivation was inherently part of the term.\n\nIn practice, one often writes down a term with a few decorations and\napplies an algorithm to _infer_ the corresponding type derivation.\nIndeed, this is exactly what happens in Agda: we specify the types for\ntop-level function declarations, and type information for everything\nelse is inferred from what has been given. The style of inference\nused is based on a technique called _bidirectional_ type\ninference, which will be presented in this chapter.\n\nThis chapter ties our previous developments together. We begin with\na term with some type annotations, quite close to the raw terms of\nChapter [Lambda][plfa.Lambda],\nand from it we compute a term with inherent types, in the style of\nChapter [DeBruijn][plfa.DeBruijn].\n\n## Introduction: Inference rules as algorithms {#algorithms}\n\nIn the calculus we have considered so far, a term may have more than\none type. For example,\n\n (\u019b x \u21d2 x) \u2982 (A \u21d2 A)\n\nholds for _every_ type `A`. We start by considering a small language for\nlambda terms where every term has a unique type. All we need do\nis decorate each abstraction term with the type of its argument.\nThis gives us the grammar:\n\n L, M, N ::= decorated terms\n x variable\n \u019b x \u2982 A \u21d2 N abstraction (decorated)\n L \u00b7 M application\n\nEach of the associated type rules can be read as an algorithm for\ntype checking. For each typing judgment, we label each position\nas either an _input_ or an _output_.\n\nFor the judgment\n\n \u0393 \u220b x \u2982 A\n\nwe take the context `\u0393` and the variable `x` as inputs, and the\ntype `A` as output. Consider the rules:\n\n ----------------- Z\n \u0393 , x \u2982 A \u220b x \u2982 A\n\n \u0393 \u220b x \u2982 A\n ----------------- S\n \u0393 , y \u2982 B \u220b x \u2982 A\n\nFrom the inputs we can determine which rule applies: if the last\nvariable in the context matches the given variable then the first\nrule applies, else the second. (For de Bruijn indices, it is even\neasier: zero matches the first rule and successor the second.)\nFor the first rule, the output type can be read off as the last\ntype in the input context. For the second rule, the inputs of the\nconclusion determine the inputs of the hypothesis, and the output\nof the hypothesis determines the output of the conclusion.\n\nFor the judgment\n\n \u0393 \u22a2 M \u2982 A\n\nwe take the context `\u0393` and term `M` as inputs, and the type `A`\nas output. Consider the rules:\n\n \u0393 \u220b x \u2982 A\n -----------\n \u0393 \u22a2 ` x \u2982 A\n\n \u0393 , x \u2982 A \u22a2 N \u2982 B\n ---------------------------\n \u0393 \u22a2 (\u019b x \u2982 A \u21d2 N) \u2982 (A \u21d2 B)\n\n \u0393 \u22a2 L \u2982 A \u21d2 B\n \u0393 \u22a2 M \u2982 A\u2032\n A \u2261 A\u2032\n -------------\n \u0393 \u22a2 L \u00b7 M \u2982 B\n\nThe input term determines which rule applies: variables use the first\nrule, abstractions the second, and applications the third. We say\nsuch rules are _syntax directed_. For the variable rule, the inputs\nof the conclusion determine the inputs of the hypothesis, and the\noutput of the hypothesis determines the output of the conclusion.\nSame for the abstraction rule \u2014 the bound variable and argument are\ncarried from the term of the conclusion into the context of the\nhypothesis; this works because we added the argument type to the\nabstraction. For the application rule, we add a third hypothesis to\ncheck whether the domain of the function matches the type of the\nargument; this judgment is decidable when both types are given as\ninputs. The inputs of the conclusion determine the inputs of the first\ntwo hypotheses, the outputs of the first two hypotheses determine the\ninputs of the third hypothesis, and the output of the first hypothesis\ndetermines the output of the conclusion.\n\nConverting the above to an algorithm is straightforward, as is adding\nnaturals and fixpoint. We omit the details. Instead, we consider a\ndetailed description of an approach that requires less obtrusive\ndecoration. The idea is to break the normal typing judgment into two\njudgments, one that produces the type as an output (as above), and\nanother that takes it as an input.\n\n\n## Synthesising and inheriting types\n\nIn addition to the lookup judgment for variables, which will remain\nas before, we now have two judgments for the type of the term:\n\n \u0393 \u22a2 M \u2191 A\n \u0393 \u22a2 M \u2193 A\n\nThe first of these _synthesises_ the type of a term, as before,\nwhile the second _inherits_ the type. In the first, the context\nand term are inputs and the type is an output; while in the\nsecond, all three of the context, term, and type are inputs.\n\nWhich terms use synthesis and which inheritance? Our approach will be\nthat the main term in a _deconstructor_ is typed via synthesis while\n_constructors_ are typed via inheritance. For instance, the function in\nan application is typed via synthesis, but an abstraction is typed via\ninheritance. The inherited type in an abstraction term serves the\nsame purpose as the argument type decoration of the previous section.\n\nTerms that deconstruct a value of a type always have a main term\n(supplying an argument of the required type) and often have\nside-terms. For application, the main term supplies the function and\nthe side term supplies the argument. For case terms, the main term\nsupplies a natural and the side terms are the two branches. In a\ndeconstructor, the main term will be typed using synthesis but the\nside terms will be typed using inheritance. As we will see, this\nleads naturally to an application as a whole being typed by synthesis,\nwhile a case term as a whole will be typed by inheritance.\nVariables are naturally typed by synthesis, since we can look up\nthe type in the input context. Fixed points will be naturally\ntyped by inheritance.\n\nIn order to get a syntax-directed type system we break terms into two\nkinds, `Term\u207a` and `Term\u207b`, which are typed by synthesis and\ninheritance, respectively. A subterm that is typed\nby synthesis may appear in a context where it is typed by inheritance,\nor vice-versa, and this gives rise to two new term forms.\n\nFor instance, we said above that the argument of an application is\ntyped by inheritance and that variables are typed by synthesis, giving\na mismatch if the argument of an application is a variable. Hence, we\nneed a way to treat a synthesized term as if it is inherited. We\nintroduce a new term form, `M \u2191` for this purpose. The typing judgment\nchecks that the inherited and synthesised types match.\n\nSimilarly, we said above that the function of an application is typed\nby synthesis and that abstractions are typed by inheritance, giving a\nmismatch if the function of an application is a variable. Hence, we\nneed a way to treat an inherited term as if it is synthesised. We\nintroduce a new term form `M \u2193 A` for this purpose. The typing\njudgment returns `A` as the synthesized type of the term as a whole,\nas well as using it as the inherited type for `M`.\n\nThe term form `M \u2193 A` represents the only place terms need to be\ndecorated with types. It only appears when switching from synthesis\nto inheritance, that is, when a term that _deconstructs_ a value of a\ntype contains as its main term a term that _constructs_ a value of a\ntype, in other words, a place where a `\u03b2`-reduction will occur.\nTypically, we will find that decorations are only required on top\nlevel declarations.\n\nWe can extract the grammar for terms from the above:\n\n L\u207a, M\u207a, N\u207a ::= terms with synthesized type\n x variable\n L\u207a \u00b7 M\u207b application\n M\u207b \u2193 A switch to inherited\n\n L\u207b, M\u207b, N\u207b ::= terms with inherited type\n \u019b x \u21d2 N abstraction\n `zero zero\n `suc M\u207b successor\n case L\u207a [zero\u21d2 M\u207b |suc x \u21d2 N\u207b ] case\n \u03bc x \u21d2 N fixpoint\n M \u2191 switch to synthesized\n\nWe will formalise the above shortly.\n\n\n## Soundness and completeness\n\nWhat we intend to show is that the typing judgments are\n_decidable_:\n\n synthesize : \u2200 (\u0393 : Context) (M : Term\u207a)\n -----------------------\n \u2192 Dec (\u2203[ A ](\u0393 \u22a2 M \u2191 A))\n\n inherit : \u2200 (\u0393 : Context) (M : Term\u207b) (A : Type)\n ---------------\n \u2192 Dec (\u0393 \u22a2 M \u2193 A)\n\nGiven context `\u0393` and synthesised term `M`, we must decide whether\nthere exists a type `A` such that `\u0393 \u22a2 M \u2191 A` holds, or its negation.\nSimilarly, given context `\u0393`, inherited term `M`, and type `A`, we\nmust decide whether `\u0393 \u22a2 M \u2193 A` holds, or its negation.\n\nOur proof is constructive. In the synthesised case, it will either\ndeliver a pair of a type `A` and evidence that `\u0393 \u22a2 M \u2193 A`, or a function\nthat given such a pair produces evidence of a contradiction. In the inherited\ncase, it will either deliver evidence that `\u0393 \u22a2 M \u2191 A`, or a function\nthat given such evidence produces evidence of a contradiction.\nThe positive case is referred to as _soundness_ --- synthesis and inheritance\nsucceed only if the corresponding relation holds. The negative case is\nreferred to as _completeness_ --- synthesis and inheritance fail only when\nthey cannot possibly succeed.\n\nAnother approach might be to return a derivation if synthesis or\ninheritance succeeds, and an error message otherwise --- for instance,\nsee the section of the Agda user manual discussing\n[syntactic sugar](https:\/\/agda.readthedocs.io\/en\/latest\/language\/syntactic-sugar.html#example).\nSuch an approach demonstrates soundness, but not completeness. If it\nreturns a derivation, we know it is correct; but there is nothing to\nprevent us from writing a function that _always_ returns an error,\neven when there exists a correct derivation. Demonstrating both\nsoundness and completeness is significantly stronger than\ndemonstrating soundness alone. The negative proof can be thought of\nas a semantically verified error message, although in practice it\nmay be less readable than a well-crafted error message.\n\nWe are now ready to begin the formal development.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_)\nopen import Data.String using (String)\nopen import Data.String.Unsafe using (_\u225f_)\nopen import Data.Product using (_\u00d7_; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\n\\end{code}\n\nOnce we have a type derivation, it will be easy to construct\nfrom it the inherently typed representation. In order that we\ncan compare with our previous development, we import\nmodule `pfla.DeBruijn`:\n\n\\begin{code}\nimport plfa.DeBruijn as DB\n\\end{code}\n\nThe phrase `as DB` allows us to refer to definitions\nfrom that module as, for instance, `DB._\u22a2_`, which is\ninvoked as `\u0393 DB.\u22a2 A`, where `\u0393` has type\n`DB.Context` and `A` has type `DB.Type`. We also import\n`Type` and its constructors directly, so the latter may\nalso be referred to as just `Type`.\n\n\n## Syntax\n\nFirst, we get all our infix declarations out of the way.\nWe list separately operators for judgments and terms:\n\n\\begin{code}\ninfix 4 _\u220b_\u2982_\ninfix 4 _\u22a2_\u2191_\ninfix 4 _\u22a2_\u2193_\ninfixl 5 _,_\u2982_\n\ninfixr 7 _\u21d2_\n\ninfix 5 \u019b_\u21d2_\ninfix 5 \u03bc_\u21d2_\ninfix 6 _\u2191\ninfix 6 _\u2193_\ninfixl 7 _\u00b7_\ninfix 8 `suc_\ninfix 9 `_\n\\end{code}\n\nIdentifiers, types, and contexts are as before:\n\\begin{code}\nId : Set\nId = String\n\ndata Type : Set where\n `\u2115 : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n\ndata Context : Set where\n \u2205 : Context\n _,_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Context\n\\end{code}\n\nThe syntax of terms is defined by mutual recursion.\nWe use `Term\u207a` and `Term\u207b`\nfor terms with synthesized and inherited types, respectively.\nNote the inclusion of the switching forms,\n`M \u2193 A` and `M \u2191`:\n\\begin{code}\ndata Term\u207a : Set\ndata Term\u207b : Set\n\ndata Term\u207a where\n `_ : Id \u2192 Term\u207a\n _\u00b7_ : Term\u207a \u2192 Term\u207b \u2192 Term\u207a\n _\u2193_ : Term\u207b \u2192 Type \u2192 Term\u207a\n\ndata Term\u207b where\n \u019b_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n `zero : Term\u207b\n `suc_ : Term\u207b \u2192 Term\u207b\n `case_[zero\u21d2_|suc_\u21d2_] : Term\u207a \u2192 Term\u207b \u2192 Id \u2192 Term\u207b \u2192 Term\u207b\n \u03bc_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n _\u2191 : Term\u207a \u2192 Term\u207b\n\\end{code}\nThe choice as to whether each term is synthesized or\ninherited follows the discussion above, and can be read\noff from the informal grammar presented earlier. Main terms in\ndeconstructors synthesise, constructors and side terms\nin deconstructors inherit.\n\n## Example terms\n\nWe can recreate the examples from preceding chapters.\nFirst, computing two plus two on naturals:\n\\begin{code}\ntwo : Term\u207b\ntwo = `suc (`suc `zero)\n\nplus : Term\u207a\nplus = (\u03bc \"p\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n `case (` \"m\") [zero\u21d2 ` \"n\" \u2191\n |suc \"m\" \u21d2 `suc (` \"p\" \u00b7 (` \"m\" \u2191) \u00b7 (` \"n\" \u2191) \u2191) ])\n \u2193 (`\u2115 \u21d2 `\u2115 \u21d2 `\u2115)\n\n2+2 : Term\u207a\n2+2 = plus \u00b7 two \u00b7 two\n\\end{code}\nThe only change is to decorate with down and up arrows as required.\nThe only type decoration required is for `plus`.\n\nNext, computing two plus two with Church numerals:\n\\begin{code}\nCh : Type\nCh = (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115\n\ntwo\u1d9c : Term\u207b\ntwo\u1d9c = (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 (` \"z\" \u2191) \u2191) \u2191)\n\nplus\u1d9c : Term\u207a\nplus\u1d9c = (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2\n ` \"m\" \u00b7 (` \"s\" \u2191) \u00b7 (` \"n\" \u00b7 (` \"s\" \u2191) \u00b7 (` \"z\" \u2191) \u2191) \u2191)\n \u2193 (Ch \u21d2 Ch \u21d2 Ch)\n\nsuc\u1d9c : Term\u207b\nsuc\u1d9c = \u019b \"x\" \u21d2 `suc (` \"x\" \u2191)\n\n2+2\u1d9c : Term\u207a\n2+2\u1d9c = plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n\\end{code}\nThe only type decoration required is for `plus\u1d9c`. One is not even\nrequired for `suc\u1d9c`, which inherits its type as an argument of `plus\u1d9c`.\n\n## Bidirectional type checking\n\nThe typing rules for variables are as in\n[Lambda][plfa.Lambda]:\n\\begin{code}\ndata _\u220b_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 x A}\n --------------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 x y A B}\n \u2192 x \u2262 y\n \u2192 \u0393 \u220b x \u2982 A\n -----------------\n \u2192 \u0393 , y \u2982 B \u220b x \u2982 A\n\\end{code}\n\nAs with syntax, the judgments for synthesizing\nand inheriting types are mutually recursive:\n\\begin{code}\ndata _\u22a2_\u2191_ : Context \u2192 Term\u207a \u2192 Type \u2192 Set\ndata _\u22a2_\u2193_ : Context \u2192 Term\u207b \u2192 Type \u2192 Set\n\ndata _\u22a2_\u2191_ where\n\n \u22a2` : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x \u2982 A\n -----------\n \u2192 \u0393 \u22a2 ` x \u2191 A\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2191 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2193 A\n -------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2191 B\n\n \u22a2\u2193 : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M \u2193 A\n ---------------\n \u2192 \u0393 \u22a2 (M \u2193 A) \u2191 A\n\ndata _\u22a2_\u2193_ where\n\n \u22a2\u019b : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 B\n -------------------\n \u2192 \u0393 \u22a2 \u019b x \u21d2 N \u2193 A \u21d2 B\n\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2193 `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2193 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2193 `\u2115\n\n \u22a2case : \u2200 {\u0393 L M x N A}\n \u2192 \u0393 \u22a2 L \u2191 `\u2115\n \u2192 \u0393 \u22a2 M \u2193 A\n \u2192 \u0393 , x \u2982 `\u2115 \u22a2 N \u2193 A\n -------------------------------------\n \u2192 \u0393 \u22a2 `case L [zero\u21d2 M |suc x \u21d2 N ] \u2193 A\n\n \u22a2\u03bc : \u2200 {\u0393 x N A}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 A\n -----------------\n \u2192 \u0393 \u22a2 \u03bc x \u21d2 N \u2193 A\n\n \u22a2\u2191 : \u2200 {\u0393 M A B}\n \u2192 \u0393 \u22a2 M \u2191 A\n \u2192 A \u2261 B\n -------------\n \u2192 \u0393 \u22a2 (M \u2191) \u2193 B\n\\end{code}\nWe follow the same convention as\nChapter [Lambda][plfa.Lambda],\nprefacing the constructor with `\u22a2` to derive the name of the\ncorresponding type rule.\n\nThe rules are similar to those in\nChapter [Lambda][plfa.Lambda],\nmodified to support synthesised and inherited types.\nThe two new rules are those for `\u22a2\u2191` and `\u22a2\u2193`.\nThe former both passes the type decoration as the inherited type and returns\nit as the synthesised type. The latter takes the synthesised type and the\ninherited type and confirms they are identical --- it should remind you of\nthe equality test in the application rule in the first\n[section][plfa.Inference#algorithms].\n\n\n#### Exercise `bidirectional-mul` (recommended) {#bidirectional-mul}\n\nRewrite your definition of multiplication from\nChapter [Lambda][plfa.Lambda], decorated to support inference.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `bidirectional-products` (recommended) {#bidirectional-products}\n\nExtend the bidirectional type rules to include products from\nChapter [More][plfa.More].\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n#### Exercise `bidirectional-rest` (stretch)\n\nExtend the bidirectional type rules to include the rest of the constructs from\nChapter [More][plfa.More].\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n## Prerequisites\n\nThe rule for `M \u2191` requires the ability to decide whether two types\nare equal. It is straightforward to code:\n\\begin{code}\n_\u225fTp_ : (A B : Type) \u2192 Dec (A \u2261 B)\n`\u2115 \u225fTp `\u2115 = yes refl\n`\u2115 \u225fTp (A \u21d2 B) = no \u03bb()\n(A \u21d2 B) \u225fTp `\u2115 = no \u03bb()\n(A \u21d2 B) \u225fTp (A\u2032 \u21d2 B\u2032)\n with A \u225fTp A\u2032 | B \u225fTp B\u2032\n... | no A\u2262 | _ = no \u03bb{refl \u2192 A\u2262 refl}\n... | yes _ | no B\u2262 = no \u03bb{refl \u2192 B\u2262 refl}\n... | yes refl | yes refl = yes refl\n\\end{code}\n\nWe will also need a couple of obvious lemmas; the domain\nand range of equal function types are equal:\n\\begin{code}\ndom\u2261 : \u2200 {A A\u2032 B B\u2032} \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032 \u2192 A \u2261 A\u2032\ndom\u2261 refl = refl\n\nrng\u2261 : \u2200 {A A\u2032 B B\u2032} \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032 \u2192 B \u2261 B\u2032\nrng\u2261 refl = refl\n\\end{code}\n\nWe will also need to know that the types `` `\u2115 ``\nand `A \u21d2 B` are not equal:\n\\begin{code}\n\u2115\u2262\u21d2 : \u2200 {A B} \u2192 `\u2115 \u2262 A \u21d2 B\n\u2115\u2262\u21d2 ()\n\\end{code}\n\n\n## Unique types\n\nLooking up a type in the context is unique. Given two derivations,\none showing `\u0393 \u220b x \u2982 A` and one showing `\u0393 \u220b x \u2982 B`, it follows that\n`A` and `B` must be identical:\n\\begin{code}\nuniq-\u220b : \u2200 {\u0393 x A B} \u2192 \u0393 \u220b x \u2982 A \u2192 \u0393 \u220b x \u2982 B \u2192 A \u2261 B\nuniq-\u220b Z Z = refl\nuniq-\u220b Z (S x\u2262y _) = \u22a5-elim (x\u2262y refl)\nuniq-\u220b (S x\u2262y _) Z = \u22a5-elim (x\u2262y refl)\nuniq-\u220b (S _ \u220bx) (S _ \u220bx\u2032) = uniq-\u220b \u220bx \u220bx\u2032\n\\end{code}\nIf both derivations are by rule `Z` then uniqueness\nfollows immediately, while if both derivations are\nby rule `S` then uniqueness follows by induction.\nIt is a contradiction if one derivation is by\nrule `Z` and one by rule `S`, since rule `Z`\nrequires the variable we are looking for is the\nfinal one in the context, while rule `S` requires\nit is not.\n\nSynthesizing a type is also unique. Given two derivations,\none showing `\u0393 \u22a2 M \u2191 A` and one showing `\u0393 \u22a2 M \u2191 B`, it follows\nthat `A` and `B` must be identical:\n\\begin{code}\nuniq-\u2191 : \u2200 {\u0393 M A B} \u2192 \u0393 \u22a2 M \u2191 A \u2192 \u0393 \u22a2 M \u2191 B \u2192 A \u2261 B\nuniq-\u2191 (\u22a2` \u220bx) (\u22a2` \u220bx\u2032) = uniq-\u220b \u220bx \u220bx\u2032\nuniq-\u2191 (\u22a2L \u00b7 \u22a2M) (\u22a2L\u2032 \u00b7 \u22a2M\u2032) = rng\u2261 (uniq-\u2191 \u22a2L \u22a2L\u2032)\nuniq-\u2191 (\u22a2\u2193 \u22a2M) (\u22a2\u2193 \u22a2M\u2032) = refl\n\\end{code}\nThere are three possibilities for the term. If it is a variable,\nuniqueness of synthesis follows from uniqueness of lookup.\nIf it is an application, uniqueness follows by induction on\nthe function in the application, since the range of equal\ntypes are equal. If it is a switch expression, uniqueness\nfollows since both terms are decorated with the same type.\n\n\n## Lookup type of a variable in the context\n\nGiven `\u0393` and two distinct variables `x` and `y`, if there is no type `A`\nsuch that `\u0393 \u220b x \u2982 A` holds, then there is also no type `A` such that\n`\u0393 , y \u2982 B \u220b x \u2982 A` holds:\n\\begin{code}\next\u220b : \u2200 {\u0393 B x y}\n \u2192 x \u2262 y\n \u2192 \u00ac \u2203[ A ]( \u0393 \u220b x \u2982 A )\n -----------------------------\n \u2192 \u00ac \u2203[ A ]( \u0393 , y \u2982 B \u220b x \u2982 A )\next\u220b x\u2262y _ \u27e8 A , Z \u27e9 = x\u2262y refl\next\u220b _ \u00ac\u2203 \u27e8 A , S _ \u22a2x \u27e9 = \u00ac\u2203 \u27e8 A , \u22a2x \u27e9\n\\end{code}\nGiven a type `A` and evidence that `\u0393 , y \u2982 B \u220b x \u2982 A` holds, we must\ndemonstrate a contradiction. If the judgment holds by `Z`, then we\nmust have that `x` and `y` are the same, which contradicts the first\nassumption. If the judgment holds by `S _ \u22a2x` then `\u22a2x` provides\nevidence that `\u0393 \u220b x \u2982 A`, which contradicts the second assumption.\n\nGiven a context `\u0393` and a variable `x`, we decide whether\nthere exists a type `A` such that `\u0393 \u220b x \u2982 A` holds, or its\nnegation:\n\\begin{code}\nlookup : \u2200 (\u0393 : Context) (x : Id)\n -----------------------\n \u2192 Dec (\u2203[ A ](\u0393 \u220b x \u2982 A))\nlookup \u2205 x = no (\u03bb ())\nlookup (\u0393 , y \u2982 B) x with x \u225f y\n... | yes refl = yes \u27e8 B , Z \u27e9\n... | no x\u2262y with lookup \u0393 x\n... | no \u00ac\u2203 = no (ext\u220b x\u2262y \u00ac\u2203)\n... | yes \u27e8 A , \u22a2x \u27e9 = yes \u27e8 A , S x\u2262y \u22a2x \u27e9\n\\end{code}\nConsider the context:\n\n* If it is empty, then trivially there is no possible derivation.\n\n* If it is non-empty, compare the given variable to the most\n recent binding:\n\n + If they are identical, we have succeeded, with `Z` as\n the appropriate derivation.\n\n + If they differ, we recurse:\n\n - If lookup fails, we apply `ext\u220b` to conver the proof\n there is no derivation from the contained context\n to the extended context.\n\n - If lookup succeeds, we extend the derivation with `S`.\n\n\n## Promoting negations\n\nFor each possible term form, we need to show that if one of its\ncomponents fails to type, then the whole fails to type. Most of\nthese results are easy to demonstrate inline, but we provide\nauxiliary functions for a couple of the trickier cases.\n\nIf `\u0393 \u22a2 L \u2191 A \u21d2 B` holds but `\u0393 \u22a2 M \u2193 A` does not hold, then\nthere is no term `B\u2032` such that `\u0393 \u22a2 L \u00b7 M \u2191 B\u2032` holds:\n\\begin{code}\n\u00acarg : \u2200 {\u0393 A B L M}\n \u2192 \u0393 \u22a2 L \u2191 A \u21d2 B\n \u2192 \u00ac \u0393 \u22a2 M \u2193 A\n -------------------------\n \u2192 \u00ac \u2203[ B\u2032 ](\u0393 \u22a2 L \u00b7 M \u2191 B\u2032)\n\u00acarg \u22a2L \u00ac\u22a2M \u27e8 B\u2032 , \u22a2L\u2032 \u00b7 \u22a2M\u2032 \u27e9 rewrite dom\u2261 (uniq-\u2191 \u22a2L \u22a2L\u2032) = \u00ac\u22a2M \u22a2M\u2032\n\\end{code}\nLet `\u22a2L` be evidence that `\u0393 \u22a2 L \u2191 A \u21d2 B` holds and `\u00ac\u22a2M` be evidence\nthat `\u0393 \u22a2 M \u2193 A` does not hold. Given a type `B\u2032` and evidence that\n`\u0393 \u22a2 L \u00b7 M \u2191 B\u2032` holds, we must demonstrate a contradiction. The\nevidence must take the form `\u22a2L\u2032 \u00b7 \u22a2M\u2032`, where `\u22a2L\u2032` is evidence that\n`\u0393 \u22a2 L \u2191 A\u2032 \u21d2 B\u2032` and `\u22a2M\u2032` is evidence that `\u0393 \u22a2 M \u2193 A\u2032`. By\n`uniq-\u2191` applied to `\u22a2L` and `\u22a2L\u2032`, we know that `A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032`,\nand hence that `A \u2261 A\u2032`, which means that `\u00ac\u22a2M` and `\u22a2M\u2032` yield a\ncontradiction. Without the `rewrite` clause, Agda would not allow us\nto derive a contradiction between `\u00ac\u22a2M` and `\u22a2M\u2032`, since one concerns\ntype `A` and the other type `A\u2032`.\n\n\nIf `\u0393 \u22a2 M \u2191 A` holds and `A \u2262 B`, then `\u0393 \u22a2 (M \u2191) \u2193 B` does not hold:\n\\begin{code}\n\u00acswitch : \u2200 {\u0393 M A B}\n \u2192 \u0393 \u22a2 M \u2191 A\n \u2192 A \u2262 B\n ---------------\n \u2192 \u00ac \u0393 \u22a2 (M \u2191) \u2193 B\n\u00acswitch \u22a2M A\u2262B (\u22a2\u2191 \u22a2M\u2032 A\u2032\u2261B) rewrite uniq-\u2191 \u22a2M \u22a2M\u2032 = A\u2262B A\u2032\u2261B\n\\end{code}\nLet `\u22a2M` be evidence that `\u0393 \u22a2 M \u2191 A` holds, and `A\u2262B` be evidence\nthat `A \u2262 B`. Given evidence that `\u0393 \u22a2 (M \u2191) \u2193 B` holds, we must\ndemonstrate a contradiction. The evidence must take the form `\u22a2\u2191 \u22a2M\u2032\nA\u2032\u2261B`, where `\u22a2M\u2032` is evidence that `\u0393 \u22a2 M \u2191 A\u2032` and `A\u2032\u2261B` is\nevidence that `A\u2032\u2261B`. By `uniq-\u2191` applied to `\u22a2M` and `\u22a2M\u2032` we know\nthat `A \u2261 A\u2032`, which means that `A\u2262B` and `A\u2032\u2261B` yield a\ncontradiction. Without the `rewrite` clause, Agda would not allow us\nto derive a contradiction between `A\u2262B` and `A\u2032\u2261B`, since one concerns\ntype `A` and the other type `A\u2032`.\n\n\n## Synthesize and inherit types\n\nThe table has been set and we are ready for the main course.\nWe define two mutually recursive functions,\none for synthesis and one for inheritance. Synthesis is given\na context `\u0393` and a synthesis term `M` and either\nreturns a type `A` and evidence that `\u0393 \u22a2 M \u2191 A`, or its negation.\nInheritance is given a context `\u0393`, an inheritance term `M`,\nand a type `A` and either returns evidence that `\u0393 \u22a2 M \u2193 A`,\nor its negation:\n\\begin{code}\nsynthesize : \u2200 (\u0393 : Context) (M : Term\u207a)\n -----------------------\n \u2192 Dec (\u2203[ A ](\u0393 \u22a2 M \u2191 A))\n\ninherit : \u2200 (\u0393 : Context) (M : Term\u207b) (A : Type)\n ---------------\n \u2192 Dec (\u0393 \u22a2 M \u2193 A)\n\\end{code}\n\nWe first consider the code for synthesis:\n\\begin{code}\nsynthesize \u0393 (` x) with lookup \u0393 x\n... | no \u00ac\u2203 = no (\u03bb{ \u27e8 A , \u22a2` \u220bx \u27e9 \u2192 \u00ac\u2203 \u27e8 A , \u220bx \u27e9 })\n... | yes \u27e8 A , \u220bx \u27e9 = yes \u27e8 A , \u22a2` \u220bx \u27e9\nsynthesize \u0393 (L \u00b7 M) with synthesize \u0393 L\n... | no \u00ac\u2203 = no (\u03bb{ \u27e8 _ , \u22a2L \u00b7 _ \u27e9 \u2192 \u00ac\u2203 \u27e8 _ , \u22a2L \u27e9 })\n... | yes \u27e8 `\u2115 , \u22a2L \u27e9 = no (\u03bb{ \u27e8 _ , \u22a2L\u2032 \u00b7 _ \u27e9 \u2192 \u2115\u2262\u21d2 (uniq-\u2191 \u22a2L \u22a2L\u2032) })\n... | yes \u27e8 A \u21d2 B , \u22a2L \u27e9 with inherit \u0393 M A\n... | no \u00ac\u22a2M = no (\u00acarg \u22a2L \u00ac\u22a2M)\n... | yes \u22a2M = yes \u27e8 B , \u22a2L \u00b7 \u22a2M \u27e9\nsynthesize \u0393 (M \u2193 A) with inherit \u0393 M A\n... | no \u00ac\u22a2M = no (\u03bb{ \u27e8 _ , \u22a2\u2193 \u22a2M \u27e9 \u2192 \u00ac\u22a2M \u22a2M })\n... | yes \u22a2M = yes \u27e8 A , \u22a2\u2193 \u22a2M \u27e9\n\\end{code}\nThere are three cases:\n\n* If the term is a variable `` ` x ``, we use lookup as defined above:\n\n + If it fails, then `\u00ac\u2203` is evidence that there is no `A` such\n that `\u0393 \u220b x \u2982 A` holds. Evidence that `` \u0393 \u22a2 ` x \u2191 A `` holds must\n have the form `` \u22a2` \u220bx ``, where `\u220bx` is evidence that `\u0393 \u220b x \u2982 A`,\n which yields a contradiction.\n\n + If it succeeds, then `\u220bx` is evidence that `\u0393 \u220b x \u2982 A`, and\n hence `` \u22a2\u2032 \u220bx `` is evidence that `` \u0393 \u22a2 ` x \u2191 A ``.\n\n* If the term is an application `L \u00b7 M`, we recurse on the function `L`:\n\n + If it fails, then `\u00ac\u2203` is evidence that there is no type such\n that `\u0393 \u22a2 L \u2191 _` holds. Evidence that `\u0393 \u22a2 L \u00b7 M \u2191 _` holds\n must have the form `\u22a2L \u00b7 _`, where `\u22a2L` is evidence that\n `\u0393 \u22a2 L \u2191 _`, which yields a contradiction.\n\n + If it succeeds, there are two possibilities:\n\n - One is that `\u22a2L` is evidence that `` \u0393 \u22a2 L \u2982 `\u2115 ``. Evidence\n that `\u0393 \u22a2 L \u00b7 M \u2191 _` holds must have the form `\u22a2L\u2032 \u00b7 _` where\n `\u22a2L\u2032` is evidence that `\u0393 \u22a2 L \u2191 A \u21d2 B` for some types `A` and\n `B`. Applying `uniq-\u2191` to `\u22a2L` and `\u22a2L\u2032` yields a\n contradiction, since `` `\u2115 `` cannot equal `A \u21d2 B`.\n\n - The other is that `\u22a2L` is evidence that `\u0393 \u22a2 L \u2191 A \u21d2 B`, in\n which case we recurse on the argument `M`:\n\n * If it fails, then `\u00ac\u22a2M` is evidence that `\u0393 \u22a2 M \u2193 A` does\n not hold. By `\u00acarg` applied to `\u22a2L` and `\u00ac\u22a2M`, it follows\n that `\u0393 \u22a2 L \u00b7 M \u2191 B` cannot hold.\n\n * If it succeeds, then `\u22a2M` is evidence that `\u0393 \u22a2 M \u2193 A`,\n and `\u22a2L \u00b7 \u22a2M` provides evidence that `\u0393 \u22a2 L \u00b7 M \u2191 B`.\n\n* If the term is a switch `M \u2193 A` from synthesised to inherited,\n we recurse on the subterm `M`, supplying type `A` by inheritance:\n\n + If it fails, then `\u00ac\u22a2M` is evidence that `\u0393 \u22a2 M \u2193 A` does not\n hold. Evidence that `\u0393 \u22a2 (M \u2193 A) \u2191 A` holds must have the\n form `\u22a2\u2193 \u22a2M` where `\u22a2M` is evidence that `\u0393 \u22a2 M \u2193 A` holds,\n which yields a contradiction.\n\n + If it succeeds, then `\u22a2M` is evidence that `\u0393 \u22a2 M \u2193 A`,\n and `\u22a2\u2193 \u22a2M` provides evidence that `\u0393 \u22a2 (M \u2193 A) \u2191 A`.\n\nWe next consider the code for inheritance:\n\\begin{code}\ninherit \u0393 (\u019b x \u21d2 N) `\u2115 = no (\u03bb())\ninherit \u0393 (\u019b x \u21d2 N) (A \u21d2 B) with inherit (\u0393 , x \u2982 A) N B\n... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2\u019b \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n... | yes \u22a2N = yes (\u22a2\u019b \u22a2N)\ninherit \u0393 `zero `\u2115 = yes \u22a2zero\ninherit \u0393 `zero (A \u21d2 B) = no (\u03bb())\ninherit \u0393 (`suc M) `\u2115 with inherit \u0393 M `\u2115\n... | no \u00ac\u22a2M = no (\u03bb{ (\u22a2suc \u22a2M) \u2192 \u00ac\u22a2M \u22a2M })\n... | yes \u22a2M = yes (\u22a2suc \u22a2M)\ninherit \u0393 (`suc M) (A \u21d2 B) = no (\u03bb())\ninherit \u0393 (`case L [zero\u21d2 M |suc x \u21d2 N ]) A with synthesize \u0393 L\n... | no \u00ac\u2203 = no (\u03bb{ (\u22a2case \u22a2L _ _) \u2192 \u00ac\u2203 \u27e8 `\u2115 , \u22a2L \u27e9})\n... | yes \u27e8 _ \u21d2 _ , \u22a2L \u27e9 = no (\u03bb{ (\u22a2case \u22a2L\u2032 _ _) \u2192 \u2115\u2262\u21d2 (uniq-\u2191 \u22a2L\u2032 \u22a2L) })\n... | yes \u27e8 `\u2115 , \u22a2L \u27e9 with inherit \u0393 M A\n... | no \u00ac\u22a2M = no (\u03bb{ (\u22a2case _ \u22a2M _) \u2192 \u00ac\u22a2M \u22a2M })\n... | yes \u22a2M with inherit (\u0393 , x \u2982 `\u2115) N A\n... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2case _ _ \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n... | yes \u22a2N = yes (\u22a2case \u22a2L \u22a2M \u22a2N)\ninherit \u0393 (\u03bc x \u21d2 N) A with inherit (\u0393 , x \u2982 A) N A\n... | no \u00ac\u22a2N = no (\u03bb{ (\u22a2\u03bc \u22a2N) \u2192 \u00ac\u22a2N \u22a2N })\n... | yes \u22a2N = yes (\u22a2\u03bc \u22a2N)\ninherit \u0393 (M \u2191) B with synthesize \u0393 M\n... | no \u00ac\u2203 = no (\u03bb{ (\u22a2\u2191 \u22a2M _) \u2192 \u00ac\u2203 \u27e8 _ , \u22a2M \u27e9 })\n... | yes \u27e8 A , \u22a2M \u27e9 with A \u225fTp B\n... | no A\u2262B = no (\u00acswitch \u22a2M A\u2262B)\n... | yes A\u2261B = yes (\u22a2\u2191 \u22a2M A\u2261B)\n\\end{code}\nWe consider only the cases for abstraction and\nand for switching from inherited to synthesized:\n\n* If the term is an abstraction `\u019b x \u21d2 N` and the inherited type\n is `` `\u2115 ``, then it is trivial that `` \u0393 \u22a2 (\u019b x \u21d2 N) \u2193 `\u2115 ``\n cannot hold.\n\n* If the term is an abstraction `\u019b x \u21d2 N` and the inherited type\n is `A \u21d2 B`, then we recurse with context `\u0393 , x \u2982 A` on subterm\n `N` inheriting type `B`:\n\n + If it fails, then `\u00ac\u22a2N` is evidence that `\u0393 , x \u2982 A \u22a2 N \u2193 B`\n does not hold. Evidence that `\u0393 \u22a2 (\u019b x \u21d2 N) \u2193 A \u21d2 B` holds\n must have the form `\u22a2\u019b \u22a2N` where `\u22a2N` is evidence that\n `\u0393 , x \u2982 A \u22a2 N \u2193 B`, which yields a contradiction.\n\n + If it succeeds, then `\u22a2N` is evidence that `\u0393 , x \u2982 A \u22a2 N \u2193 B`\n holds, and `\u22a2\u019b \u22a2N` provides evidence that `\u0393 \u22a2 (\u019b x \u21d2 N) \u2193 A \u21d2 B`.\n\n* If the term is a switch `M \u2191` from inherited to synthesised,\n we recurse on the subterm `M`:\n\n + If it fails, then `\u00ac\u2203` is evidence there is no `A` such\n that `\u0393 \u22a2 M \u2191 A` holds. Evidence that `\u0393 \u22a2 (M \u2191) \u2193 B` holds\n must have the form `\u22a2\u2191 \u22a2M _` where `\u22a2M` is evidence that\n `\u0393 \u22a2 M \u2191 _`, which yields a contradiction.\n\n + If it succeeds, then `\u22a2M` is evidence that `\u0393 \u22a2 M \u2191 A` holds.\n We apply `_\u225fTp_` do decide whether `A` and `B` are equal:\n\n - If it fails, then `A\u2262B` is evidence that `A \u2262 B`.\n By `\u00acswitch` applied to `\u22a2M` and `A\u2262B` it follow that\n `\u0393 \u22a2 (M \u2191) \u2193 B` cannot hold.\n\n - If it succeeds, then `A\u2261B` is evidence that `A \u2261 B`,\n and `\u22a2\u2191 \u22a2M A\u2261B` provides evidence that `\u0393 \u22a2 (M \u2191) \u2193 B`.\n\nThe remaining cases are similar, and their code can pretty much be\nread directly from the corresponding typing rules.\n\n## Testing the example terms\n\nFirst, we copy a function introduced earlier that makes it easy to\ncompute the evidence that two variable names are distinct:\n\\begin{code}\n_\u2260_ : \u2200 (x y : Id) \u2192 x \u2262 y\nx \u2260 y with x \u225f y\n... | no x\u2262y = x\u2262y\n... | yes _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\n\nHere is the result of typing two plus two on naturals:\n\\begin{code}\n\u22a22+2 : \u2205 \u22a2 2+2 \u2191 `\u2115\n\u22a22+2 =\n (\u22a2\u2193\n (\u22a2\u03bc\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2case (\u22a2` (S (\"m\" \u2260 \"n\") Z)) (\u22a2\u2191 (\u22a2` Z) refl)\n (\u22a2suc\n (\u22a2\u2191\n (\u22a2`\n (S (\"p\" \u2260 \"m\")\n (S (\"p\" \u2260 \"n\")\n (S (\"p\" \u2260 \"m\") Z)))\n \u00b7 \u22a2\u2191 (\u22a2` Z) refl\n \u00b7 \u22a2\u2191 (\u22a2` (S (\"n\" \u2260 \"m\") Z)) refl)\n refl))))))\n \u00b7 \u22a2suc (\u22a2suc \u22a2zero)\n \u00b7 \u22a2suc (\u22a2suc \u22a2zero))\n\\end{code}\nWe confirm that synthesis on the relevant term returns\nnatural as the type and the above derivation:\n\\begin{code}\n_ : synthesize \u2205 2+2 \u2261 yes \u27e8 `\u2115 , \u22a22+2 \u27e9\n_ = refl\n\\end{code}\nIndeed, the above derivation was computed by evaluating the term on\nthe left, with minor editing of the result. The only editing required\nwas to replace Agda's representation of the evidence that two strings\nare unequal (which it cannot print nor read) by equivalent calls to\n`_\u2260_`.\n\nHere is the result of typing two plus two with Church numerals:\n\\begin{code}\n\u22a22+2\u1d9c : \u2205 \u22a2 2+2\u1d9c \u2191 `\u2115\n\u22a22+2\u1d9c =\n \u22a2\u2193\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (\u22a2`\n (S (\"m\" \u2260 \"z\")\n (S (\"m\" \u2260 \"s\")\n (S (\"m\" \u2260 \"n\") Z)))\n \u00b7 \u22a2\u2191 (\u22a2` (S (\"s\" \u2260 \"z\") Z)) refl\n \u00b7\n \u22a2\u2191\n (\u22a2`\n (S (\"n\" \u2260 \"z\")\n (S (\"n\" \u2260 \"s\") Z))\n \u00b7 \u22a2\u2191 (\u22a2` (S (\"s\" \u2260 \"z\") Z)) refl\n \u00b7 \u22a2\u2191 (\u22a2` Z) refl)\n refl)\n refl)))))\n \u00b7\n \u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (\u22a2` (S (\"s\" \u2260 \"z\") Z) \u00b7\n \u22a2\u2191 (\u22a2` (S (\"s\" \u2260 \"z\") Z) \u00b7 \u22a2\u2191 (\u22a2` Z) refl)\n refl)\n refl))\n \u00b7\n \u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (\u22a2` (S (\"s\" \u2260 \"z\") Z) \u00b7\n \u22a2\u2191 (\u22a2` (S (\"s\" \u2260 \"z\") Z) \u00b7 \u22a2\u2191 (\u22a2` Z) refl)\n refl)\n refl))\n \u00b7 \u22a2\u019b (\u22a2suc (\u22a2\u2191 (\u22a2` Z) refl))\n \u00b7 \u22a2zero\n\\end{code}\nWe confirm that synthesis on the relevant term returns\nnatural as the type and the above derivation:\n\\begin{code}\n_ : synthesize \u2205 2+2\u1d9c \u2261 yes \u27e8 `\u2115 , \u22a22+2\u1d9c \u27e9\n_ = refl\n\\end{code}\nAgain, the above derivation was computed by evaluating the\nterm on the left and editing.\n\n## Testing the error cases\n\nIt is important not just to check that code works as intended,\nbut also that it fails as intended. Here are checks for\nseveral possible errors:\n\nUnbound variable:\n\\begin{code}\n_ : synthesize \u2205 ((\u019b \"x\" \u21d2 ` \"y\" \u2191) \u2193 (`\u2115 \u21d2 `\u2115)) \u2261 no _\n_ = refl\n\\end{code}\n\nArgument in application is ill-typed:\n\\begin{code}\n_ : synthesize \u2205 (plus \u00b7 suc\u1d9c) \u2261 no _\n_ = refl\n\\end{code}\n\nFunction in application is ill-typed:\n\\begin{code}\n_ : synthesize \u2205 (plus \u00b7 suc\u1d9c \u00b7 two) \u2261 no _\n_ = refl\n\\end{code}\n\nFunction in application has type natural:\n\\begin{code}\n_ : synthesize \u2205 ((two \u2193 `\u2115) \u00b7 two) \u2261 no _\n_ = refl\n\\end{code}\n\nAbstraction inherits type natural:\n\\begin{code}\n_ : synthesize \u2205 (two\u1d9c \u2193 `\u2115) \u2261 no _\n_ = refl\n\\end{code}\n\nZero inherits a function type:\n\\begin{code}\n_ : synthesize \u2205 (`zero \u2193 `\u2115 \u21d2 `\u2115) \u2261 no _\n_ = refl\n\\end{code}\n\nSuccessor inherits a function type:\n\\begin{code}\n_ : synthesize \u2205 (two \u2193 `\u2115 \u21d2 `\u2115) \u2261 no _\n_ = refl\n\\end{code}\n\nSuccessor of an ill-typed term:\n\\begin{code}\n_ : synthesize \u2205 (`suc two\u1d9c \u2193 `\u2115) \u2261 no _\n_ = refl\n\\end{code}\n\nCase of a term with a function type:\n\\begin{code}\n_ : synthesize \u2205\n ((`case (two\u1d9c \u2193 Ch) [zero\u21d2 `zero |suc \"x\" \u21d2 ` \"x\" \u2191 ] \u2193 `\u2115) ) \u2261 no _\n_ = refl\n\\end{code}\n\nCase of an ill-typed term:\n\\begin{code}\n_ : synthesize \u2205\n ((`case (two\u1d9c \u2193 `\u2115) [zero\u21d2 `zero |suc \"x\" \u21d2 ` \"x\" \u2191 ] \u2193 `\u2115) ) \u2261 no _\n_ = refl\n\\end{code}\n\nInherited and synthesised types disagree in a switch:\n\\begin{code}\n_ : synthesize \u2205 (((\u019b \"x\" \u21d2 ` \"x\" \u2191) \u2193 `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115))) \u2261 no _\n_ = refl\n\\end{code}\n\n\n## Erasure\n\nFrom the evidence that a decorated term has the correct type it is\neasy to extract the corresponding inherently typed term. We use the\nname `DB` to refer to the code in\nChapter [DeBruijn][plfa.DeBruijn].\nIt is easy to define an _erasure_ function that takes evidence of a\ntype judgment into the corresponding inherently typed term.\n\nFirst, we give code to erase a type:\n\\begin{code}\n\u2225_\u2225Tp : Type \u2192 DB.Type\n\u2225 `\u2115 \u2225Tp = DB.`\u2115\n\u2225 A \u21d2 B \u2225Tp = \u2225 A \u2225Tp DB.\u21d2 \u2225 B \u2225Tp\n\\end{code}\nIt simply renames to the corresponding constructors in module `DB`.\n\nNext, we give the code to erase a context:\n\\begin{code}\n\u2225_\u2225Cx : Context \u2192 DB.Context\n\u2225 \u2205 \u2225Cx = DB.\u2205\n\u2225 \u0393 , x \u2982 A \u2225Cx = \u2225 \u0393 \u2225Cx DB., \u2225 A \u2225Tp\n\\end{code}\nIt simply drops the variable names.\n\nNext, we give the code to erase a lookup judgment:\n\\begin{code}\n\u2225_\u2225\u220b : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x \u2982 A \u2192 \u2225 \u0393 \u2225Cx DB.\u220b \u2225 A \u2225Tp\n\u2225 Z \u2225\u220b = DB.Z\n\u2225 S x\u2262 \u22a2x \u2225\u220b = DB.S \u2225 \u22a2x \u2225\u220b\n\\end{code}\nIt simply drops the evidence that variable names are distinct.\n\nFinally, we give the code to erase a typing judgment.\nJust as there are two mutually recursive typing judgments,\nthere are two mutually recursive erasure functions:\n\\begin{code}\n\u2225_\u2225\u207a : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2191 A \u2192 \u2225 \u0393 \u2225Cx DB.\u22a2 \u2225 A \u2225Tp\n\u2225_\u2225\u207b : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2193 A \u2192 \u2225 \u0393 \u2225Cx DB.\u22a2 \u2225 A \u2225Tp\n\n\u2225 \u22a2` \u22a2x \u2225\u207a = DB.` \u2225 \u22a2x \u2225\u220b\n\u2225 \u22a2L \u00b7 \u22a2M \u2225\u207a = \u2225 \u22a2L \u2225\u207a DB.\u00b7 \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2\u2193 \u22a2M \u2225\u207a = \u2225 \u22a2M \u2225\u207b\n\n\u2225 \u22a2\u019b \u22a2N \u2225\u207b = DB.\u019b \u2225 \u22a2N \u2225\u207b\n\u2225 \u22a2zero \u2225\u207b = DB.`zero\n\u2225 \u22a2suc \u22a2M \u2225\u207b = DB.`suc \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2case \u22a2L \u22a2M \u22a2N \u2225\u207b = DB.case \u2225 \u22a2L \u2225\u207a \u2225 \u22a2M \u2225\u207b \u2225 \u22a2N \u2225\u207b\n\u2225 \u22a2\u03bc \u22a2M \u2225\u207b = DB.\u03bc \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2\u2191 \u22a2M refl \u2225\u207b = \u2225 \u22a2M \u2225\u207a\n\\end{code}\nErasure replaces constructors for each typing judgment\nby the corresponding term constructor from `DB`. The\nconstructors that correspond to switching from synthesized\nto inherited or vice versa are dropped.\n\nWe confirm that the erasure of the type derivations in\nthis chapter yield the corresponding inherently typed terms\nfrom the earlier chapter:\n\\begin{code}\n_ : \u2225 \u22a22+2 \u2225\u207a \u2261 DB.2+2\n_ = refl\n\n_ : \u2225 \u22a22+2\u1d9c \u2225\u207a \u2261 DB.2+2\u1d9c\n_ = refl\n\\end{code}\nThus, we have confirmed that bidirectional type inference\nconverts decorated versions of the lambda terms from\nChapter [Lambda][plfa.Lambda]\nto the inherently typed terms of\nChapter [DeBruijn][plfa.DeBruijn].\n\n\n#### Exercise `inference-multiplication` (recommended)\n\nApply inference to your decorated definition of multiplication from\nexercise [`bidirectional-mul`][plfa.Inference#bidirectional-mul], and show that\nerasure of the inferred typing yields your definition of\nmultiplication from Chapter [DeBruijn][plfa.DeBruijn].\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n#### Exercise `inference-products` (recommended)\n\nUsing your rules from exercise\n[`bidirectional-products`][plfa.Inference#bidirectional-products], extend\nbidirectional inference to include products.\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n#### Exercise `inference-rest` (stretch)\n\nExtend the bidirectional type rules to include the rest of the constructs from\nChapter [More][plfa.More].\n\n\\begin{code}\n-- Your code goes here\n\\end{code}\n\n\n## Bidirectional inference in Agda\n\nAgda itself uses bidirectional inference. This explains why\nconstructors can be overloaded while other defined names cannot ---\nhere by _overloaded_ we mean that the same name can be used for\nconstructors of different types. Constructors are typed by\ninheritance, and so the name is available when resolving the\nconstructor, whereas variables are typed by synthesis, and so each\nvariable must have a unique type.\n\nMost top-level definitions in Agda are of functions, which are typed\nby inheritance, which is why Agda requires a type declaration for\nthose definitions. A definition with a right-hand side that is a term\ntyped by synthesis, such as an application, does not require a type\ndeclaration.\n\\begin{code}\nanswer = 6 * 7\n\\end{code}\n\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u2193 U+2193: DOWNWARDS ARROW (\\d)\n \u2191 U+2191: UPWARDS ARROW (\\u)\n \u2225 U+2225: PARALLEL TO (\\||)\n","avg_line_length":33.0291005291,"max_line_length":95,"alphanum_fraction":0.6074489387} +{"size":168713,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\documentclass{llncs}\n\\usepackage{hyperref}\n\\bibliographystyle{splncs04}\n%\\usepackage{ucs}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb}\n\\usepackage{amsmath}\n\\usepackage{txfonts}\n%\\usepackage{bbm}\n%\\usepackage[greek,english]{babel}\n\n% agda.sty wants to use the deprecated utf8x option, which\n% many publishers don't like. So we do it ourselves\n\n\\usepackage{agda}\n\n\\usepackage{proof}\n\n% this stuff is related to the Agda latex backend with inputenc\/utf8\n% not having heard of many characters...\n\n\\usepackage{newunicodechar}\n\\newunicodechar{\u220b}{$\\ni$}\n\\newunicodechar{\u00b7}{$\\cdot$}\n\\newunicodechar{\u22a2}{$\\vdash$}\n\\newunicodechar{\u22c6}{${}^\\star$}\n\\newunicodechar{\u03a0}{$\\Pi$}\n\\newunicodechar{\u21d2}{$\\Rightarrow$}\n\\newunicodechar{\u019b}{$\\lambdabar$}\n\\newunicodechar{\u2205}{$\\emptyset$}\n\\newunicodechar{\u2200}{$\\forall$}\n\\newunicodechar{\u03d5}{$\\Phi$}\n\\newunicodechar{\u03c8}{$\\Psi$}\n\\newunicodechar{\u03c1}{$\\rho$}\n\\newunicodechar{\u03b1}{$\\alpha$}\n\\newunicodechar{\u03b2}{$\\beta$}\n\\newunicodechar{\u03bc}{$\\mu$}\n\\newunicodechar{\u03c3}{$\\sigma$}\n\\newunicodechar{\u2261}{$\\equiv$}\n\\newunicodechar{\u0393}{$\\Gamma$}\n\\newunicodechar{\u2225}{$\\parallel$}\n\\newunicodechar{\u039b}{$\\Lambda$}\n\\newunicodechar{\u2082}{$~_2$}\n\\newunicodechar{\u03b8}{$\\theta$}\n\\newunicodechar{\u0398}{$\\Theta$}\n\\newunicodechar{\u2218}{$\\circ$}\n\\newunicodechar{\u0394}{$\\Delta$}\n\\newunicodechar{\u03bb}{$\\lambda$}\n\\newunicodechar{\u22a7}{$\\models$}\n\\newunicodechar{\u228e}{$\\uplus$}\n\\newunicodechar{\u03b7}{$\\eta$}\n\\newunicodechar{\u22a5}{$\\bot$}\n\\newunicodechar{\u03a3}{$\\Sigma$}\n\\newunicodechar{\u03be}{$\\xi$}\n\\newunicodechar{\u2081}{$1$}\n\\newunicodechar{\u2115}{$\\mathbb{N}$}\n\\newunicodechar{\u1d9c}{${}^c$}\n\\newunicodechar{\u03a6}{$\\Phi$}\n\\newunicodechar{\u03a8}{$\\Psi$}\n\\newunicodechar{\u22a4}{$\\top$}\n\n\\newunicodechar{\u2192}{$\\rightarrow$}\n\\newunicodechar{\u00d7}{$\\times$}\n\n\n\\usepackage{hyperref}\n\\usepackage{cleveref}\n\n\\author{James Chapman\\inst{1}\\orcidID{0000-0001-9036-8252}\n \\and Roman Kireev\\inst{1}\\orcidID{0000-0003-4687-2739}\n \\and Chad Nester\\inst{2}\n \\and Philip Wadler\\inst{2}\\orcidID{0000-0001-7619-6378}}\n \\institute{IOHK, Hong Kong\n\\email{\\{james.chapman,roman.kireev\\}@iohk.io}\n\\and\nUniversity of Edinburgh, UK\n\\email{\\{cnester,wadler\\}@inf.ed.ac.uk}}\n\\title{System~$F$ in Agda, for fun and profit}\n\n\\begin{document}\n\\maketitle\n\n\\begin{abstract}\n\nSystem~$F$, also known as the polymorphic \u03bb-calculus, is a typed\n\u03bb-calculus independently discovered by the logician Jean-Yves Girard\nand the computer scientist John Reynolds. We consider $F_{\\omega\\mu}$,\nwhich adds higher-order kinds and iso-recursive types. We present the\nfirst complete, intrinsically typed, executable, formalisation of\nSystem~$F_{\\omega\\mu}$ that we are aware of. The work is motivated by\nverifying the core language of a smart contract system based on\nSystem~$F_{\\omega\\mu}$. The paper is a literate Agda script\n\\cite{formalisation}.\n\n\\end{abstract}\n\n\n\\section{Introduction}\n\nSystem~$F$, also known as the polymorphic $\\lambda$-calculus, is a\ntyped $\\lambda$-calculus independently discovered by the logician\nJean-Yves Girard and the computer scientist John Reynolds. System~$F$\nextends the simply-typed $\\lambda$-calculus (STLC). Under the\nprinciple of Propositions as Types, the $\\to$ type of STLC corresponds\nto implication; to this System~F adds a $\\forall$ type that\ncorresponds to universal quantification over propositions.\nFormalisation of System~$F$ is tricky: it, when extended with\nsubtyping, formed the basis for the POPLmark challenge\n\\cite{POPLmark}, a set of formalisation problems widely attempted as a\nbasis for comparing different systems.\n\nSystem~$F$ is small but powerful. By a standard technique known as\nChurch encoding, it can represent a wide variety of datatypes,\nincluding natural numbers, lists, and trees. However, while\nSystem~$F$ can encode the type ``list of $A$'' for any type $A$ that\ncan also be encoded, it cannot encode ``list'' as a function from\ntypes to types. For that one requires System~$F$ with higher-kinded\ntypes, known as System~$F_\\omega$. Girard's original work also\nconsidered this variant, though Reynolds did not.\n\nThe basic idea of System~$F_\\omega$ is simple. Not only does each\n\\emph{term} have a \\emph{type}, but also each \\emph{type} level object\nhas a \\emph{kind}. Notably, type \\emph{families} are classified by\nhigher kinds. The first level, relating terms and types, includes an\nembedding of STLC (plus quantification); while the second level,\nrelating types and kinds, is an isomorphic image of STLC.\n\nChurch encodings can represent any algebraic datatype recursive only in\npositive positions; though extracting a component of a structure, such\nas finding the tail of a list, takes time proportional to the size of\nthe structure. Another standard technique, known as Scott encoding,\ncan represent any algebraic type whatsoever; and extracting a\ncomponent now takes constant time. However, Scott encoding requires\na second extension to System~$F$, to represent arbitrary recursive\ntypes, known as System~$F_\\mu$. The system with both extensions\nis known as System~$F_{\\omega\\mu}$, and will be the subject of our\nformalisation.\n\nTerms in Systems~$F$ and $F_\\omega$ are strongly normalising.\nRecursive types with recursion in a negative position permit encoding\narbitrary recursive functions, so normalisation of terms in\nSystems~$F_\\mu$ and $F_{\\omega\\mu}$ may not terminate. However,\nconstructs at the type level of Systems~$F_\\omega$ and $F_{\\omega\\mu}$\nare also strongly normalising.\n\nThere are two approaches to recursive types, \\emph{equi-recursive} and\n\\emph{iso-recursive} \\cite{pierce:tapl}. In an equi-recursive\nformulation, the types $\\mu \\alpha. A[\\alpha]$ and $A[\\mu\n\\alpha. A[\\alpha]]$ are considered equal, while in an iso-recursive\nformulation they are considered isomorphic, with an \\emph{unfold} term\nto convert the former to the latter, and a \\emph{fold} term to convert\nthe other way. Equi-recursive formulation makes coding easier, as it\ndoesn't require extra term forms. But it makes type checking more\ndifficult, and it is not known whether equi-recursive types for\nSystem~$F_{\\omega\\mu}$ are decidable\n\\cite{dreyer:recursive-modules,cai.giarrusso.ostermann}. Accordingly,\nwe use iso-recursive types, which are also used by Dreyer\n\\cite{dreyer:thesis} and Brown and Palsberg \\cite{brown.palsberg}.\n\nThere are also two approaches to formalising a typed calculus,\n\\emph{extrinsic} and \\emph{intrinsic}\n\\cite{reynolds:intrinsic-extrinsic}. In an extrinsic formulation,\nterms come first and are assigned types later, while in an intrinsic\nformulation, types come first and a term can be formed only at a given\ntype. The two approaches are sometimes associated with Curry and\nChurch, respectively \\cite{hindley.seldin}. There is also the\ndichotomy between named variables and de Bruijn indices. De Bruijn\nindices ease formalisation, but require error-prone arithmetic to move\na term underneath a lambda expression. An intrinsic formulation\ncatches such errors, because they would lead to incorrect types.\nAccordingly, we use an intrinsic formulation with de Bruijn indices.\nThe approach we follow was introduced by Altenkirch and Reus\n\\cite{altenkirch.reus:welltyped}, and used by Chapman\n\\cite{chapman:thesis} and Allais et\nal. \\cite{allais.chapman.mcbride.mckinna} among others.\n\n\\subsection{For Fun and Profit}\n\nOur interest in System~$F_{\\omega\\mu}$ is far from merely theoretical.\nInput Output HK Ltd. (IOHK) is developing the Cardano blockchain,\nwhich features a smart contract language known as Plutus\n\\cite{platform}. The part of the contract that runs off-chain is\nwritten in Haskell with an appropriate library, while the part of the\ncontract that runs on-chain is written using Template Haskell and\ncompiled to a language called Plutus Core. Any change to the core\nlanguage would require all participants of the blockchain to update their\nsoftware, an event referred to as a \\emph{hard fork}. Hard forks are\nbest avoided, so the goal with Plutus Core was to make it so simple\nthat it is unlikely to need revision. The design settled on is\nSystem~$F_{\\omega\\mu}$ with suitable primitives, using Scott encoding\nto represent data structures. Supported primitives include integers,\nbytestrings, and a few cryptographic and blockchain-specific\noperations.\n\nThe blockchain community puts a high premium on rigorous specification\nof smart contract languages. Simplicity, a proposed smart contract\nlanguage for Bitcoin, has been formalised in Coq\n\\cite{simplicity}. The smart contract language Michelson, used by\nTezos, has also been formalised in Coq \\cite{michelson-coq}. EVM, the\nvirtual machine of Ethereum, has been formalised in K \\cite{kevm}, in\nIsabelle\/HOL \\cite{hirai,amani.begel.bortin.staples}, and in $F^*$\n\\cite{grishchenko.maffei.schneidewind}. For a more complete account\nof blockchain projects involving formal methods see\n\\cite{verification-survey}.\n\nIOHK funded the development of our formalisation of\nSystem~$F_{\\omega\\mu}$ because of the correspondence to Plutus Core.\nThe formal model in Agda and associated proofs give us high assurance\nthat our specification is correct. Further, we plan to use the\nevaluator that falls out from our proof of progress for testing\nagainst the evaluator for Plutus Core that is used in Cardano.\n\n\\subsection{Contributions}\n\nThis paper represents the first complete intrinsically typed,\nexecutable, formalisation of System~$F_{\\omega\\mu}$ that we are aware\nof. There are other intrinsically typed formalisations of fragments of\nSystem $F_{\\omega\\mu}$. But, as far as we are aware none are\ncomplete. Andr\u00e1s Kov\u00e1cs has formalised System $F_\\omega$\\cite{kovacs}\nusing hereditary substitutions \\cite{watkins} at the type\nlevel. Kov\u00e1cs' formalisation does not cover iso-recursive types and\nalso does not have the two different presentations of the syntax and\nthe metatheory relating them that are present here.\n\nIntrinsically typed formalisations of arguably more challenging\nlanguages exist such as those of Chapman \\cite{chapman:thesis} and\nDanielsson \\cite{danielsson} for dependently typed languages. However,\nthey are not complete and do not consider features such as recursive\ntypes. This paper represents a more complete treatment of a different\npoint in the design space which is interesting in its own right and\nhas computation at the type level but stops short of allowing\ndependent types. We believe that that techniques described here will\nbe useful when scaling up to greater degrees of dependency.\n\nA key challenge with the intrinsically typed approach for System\n$F_\\omega$ is that due to computation at the type level, it is\nnecessary to make use of the implementations of type level operations\nand even proofs of their correctness properties when defining the term\nlevel syntax and term level operations. Also, if we want to run term\nlevel programs, rather than just formalise them, it is vital that\nthese proofs of type level operations compute, which means that we\ncannot assume any properties or rely on axioms in the metatheory such\nas functional extensionality. Achieving this level of completeness is\na contribution of this paper as is the fact that this formalisation is\nexecutable. We do not need extensionality despite using higher order\nrepresentations of renamings, substitutions, and (the semantics of)\ntype functions. First order variants of these concepts are more\ncumbersome and long winded to work with. As the type level language is\na strongly normalising extension of the simply-typed\n$\\lambda$-calculus we were able to leverage work about renaming,\nsubstitution and normalisation from simply-typed\n$\\lambda$-calculus. Albeit with the greater emphasis that proofs must\ncompute. We learnt how to avoid using extensionality when reasoning\nabout higher order\/functional representations of renamings and\nsubstitutions from Conor McBride. The normalisation algorithm is\nderived from work by Allais et al. and McBride\n\\cite{allias.mcbride.boutillier:neutral,mcbride:data}. The\nnormalisation proof also builds on their work, and in our opinion,\nsimplifies and improves it as the uniformity property in the\ncompleteness proof becomes simply a type synonym required only at\nfunction type (kind in our case) rather than needing to be mutually\ndefined with the logical relation at every type (kind), simplifying\nthe construction and the proofs considerably. In addition we work with\n\u03b2-equality not \u03b2\u03b7-equality which, in the context of NBE makes things a\nlittle more challenging. The reason for this choice is that our smart\ncontract core language Plutus Core has only \u03b2-equality.\n\nAnother challenge with the intrinsically typed approach for System\n$F_\\omega$, where typing derivations and syntax coincide, is that the\npresence of the conversion rule in the syntax makes computation\nproblematic as it can block $\\beta$-reduction. Solving\/avoiding this\nproblem is a contribution of this paper.\n\nThe approach to the term level and the notation borrow heavily from\nPLFA \\cite{wadler.koke} where the chapters on STLC form essentially a\nblueprint for and a very relevant introduction to this work. The idea\nof deriving an evaluator from a proof of progress appears in PLFA, and\nappears to be not widely known \\cite{wadler:sbmf}.\n \n\\subsection{Overview}\n\nThis paper is a literate Agda program that is machine checked and\nexecutable either via Agda's interpreter or compiled to Haskell. The\ncode (i.e. the source code of the paper) is available as a supporting\nartefact. In addition the complete formalisation of the extended\nsystem (Plutus Core) on which this paper is based is also available as\na supporting artefact.\n\nIn the paper we aim to show the highlights of the formalisation: we\nshow as much code as possible and the statements of significant lemmas\nand theorems. We hide many proofs and minor auxiliary lemmas.\n\nDealing with the computation in types and the conversion rule was the\nmain challenge in this work for us. The approaches taken to variable\nbinding, renaming\/substitution and normalisation lifted\nrelatively easily to this setting. In addition to the two versions of\nsyntax where types are (1) not normalised and (2) completely\nnormalised we also experimented with a version where types are in weak\nhead normal form (3). In (1) the conversion rule takes an inductive\nwitness of type equality relation as an argument. In (2) conversion is\nderivable as type equality is replaced by identity. In (3), the type\nequality relation in conversion can be replaced by a witness of a logical\nrelation that computes, indeed it is the same logical relation as\ndescribed in the completeness of type normalisation proof. We did not\npursue this further in this work so far as this approach is not\nused in Plutus Core but this is something that we would like to\ninvestigate further in future.\n\nIn \\cref{sec:intrinsically-typed} we introduce intrinsically typed\nsyntax (kinds, types and terms) and the dynamics of types (type\nequality). We also introduce the necessary syntactic operations for\nthese definitions: type weakening and substitution (and their\ncorrectness properties) are necessary to define terms. In\n\\cref{sec:algorithmic} we introduce an alternative version of the\nsyntax where the types are $\\beta$-normal forms. We also introduce the\ntype level normalisation algorithm, its correctness proof and a\nnormalising substitution operation on normal types. In\n\\cref{sec:correspondence} we reconcile the two versions of the syntax,\nprove soundness and completeness results and also demonstrate that\nnormalising the types preserves the \\emph{semantics} of terms where\nsemantics refers to corresponding untyped terms. In\n\\cref{sec:operational-semantics} we introduce the dynamics of the\nalgorithmic system (type preserving small-step reduction) and we prove\nprogress in \\cref{sec:algorithmic}. Preservation holds\nintrinsically. In \\cref{sec:execution} we provide a step-indexed\nevaluator that we can use to execute programs for a given number of\nreduction steps. In \\cref{sec:examples} we show examples of Church and\nScott Numerals. In \\cref{sec:real-world} we discuss extensions of the\nformalisation to higher kinded recursive types and intrinsically sized\nintegers and bytestrings.\n\n\\section{Intrinsically typed syntax of System $F_{\\omega\\mu}$}\n\\label{sec:intrinsically-typed}\n\nWe take the view that when writing a program such as an interpreter we\nwant to specify very precisely how the program behaves on meaningful\ninput and we want to rule out meaningless input as early and as\nconclusively as possible. Many of the operations we define in this\npaper, including substitution, evaluation, and normalisation, are only\nintended to work on well-typed input. In a programming language with a\nless precise type system we might need to work under the informal\nassumption that we will only ever feed meaningful inputs to our\nprograms and otherwise their behaviour is unspecified, and all bets\nare off. Working in Agda we can guarantee that our programs will only\naccept meaningful input by narrowing the definition of valid\ninput. This is the motivation for using intrinsic syntax as the\nmeaningful inputs are those that are guaranteed to be type correct and\nin Agda we can build this property right into the definition of the\nsyntax.\n\nIn practice, in our setting, before receiving the input (some source\ncode in a file) it would have been run through a lexing, parsing,\nscope checking and most importantly \\emph{type checking} phase before\nreaching our starting point in this paper: intrinsically typed\nsyntax. Formalising the type checker is future work.\n\nOne can say that in intrinsically typed syntax, terms carry their\ntypes. But, we can go further, the terms are actually typing\nderivations. Hence, the definition of the syntax and the type system,\nas we present it, coincide: each syntactic constructor corresponds to\none typing rule and vice versa. As such we dispense with presenting\nthem separately and instead present them in one go.\n\nThere are three levels in this syntax:\n\\begin{enumerate}\n\\item kinds, which classify types;\n\\item types, which classify terms;\n\\item terms, the level of ordinary programs.\n\\end{enumerate}\n\n\\noindent The kind level is needed as there are functions at the type\nlevel. Types appear in terms, but terms do not appear in types.\n\n\\subsection{Kinds}\n\nThe kinds consist of a base kind \\AgdaInductiveConstructor{*}, which is\nthe kind of types, and a function kind.\\footnote{The code in this paper\nis typeset in \\AgdaComment{colour}.}\n\n\\AgdaHide{\n\\begin{code}\nopen import Function hiding (_\u220b_)\nopen import Relation.Binary.PropositionalEquality\n using (subst; _\u2261_; refl; cong; cong\u2082; trans; sym)\nopen import Data.Sum as Sum renaming (inj\u2081 to inl; inj\u2082 to inr) \nopen import Data.Product renaming (_,_ to _,,_) hiding (map)\nopen import Data.Empty\nopen import Data.Unit\nopen import Data.Nat\nopen import Data.Fin\n\ninfix 4 _\u220b\u22c6_\ninfix 4 _\u22a2\u22c6_\ninfixl 5 _,\u22c6_\n\ninfix 6 \u03a0\ninfixr 7 _\u21d2_\n\ninfix 5 \u019b\ninfixl 7 _\u00b7_\ninfix 9 S\n\\end{code}\n}\n\\begin{code}\ndata Kind : Set where\n * : Kind -- type\n _\u21d2_ : Kind \u2192 Kind \u2192 Kind -- function kind\n\\end{code}\n\nLet \\AgdaBound{K} and \\AgdaBound{J} range over kinds.\n\n\\subsection{Type Contexts}\n\nTo manage the types of variables and their scopes we introduce\ncontexts. Our choice of how to deal with variables is already visible\nin the representation of contexts. We will use de Bruijn indices to\nrepresent variables. While this makes terms harder to write, it makes\nthe syntactic properties of the language clear and any potential\noff-by-one errors etc. are mitigated by working with intrinsically\nscoped terms and the fact that syntactic properties are proven\ncorrect. We intend to use the language as a compilation target so ease\nof manually writing programs in this language is not a high priority.\n\nWe refer to type contexts as \\AgdaDatatype{Ctx${}^\\star$} and reserve\nthe name \\AgdaDatatype{Ctx} for term level contexts. Indeed, when a\nconcept occurs at both type and term level we often suffix the name of\nthe type level version with ${}^\\star$.\n\nType contexts are essentially lists of types written in reverse. No\nnames are required.\n\n\\begin{code}\ndata Ctx\u22c6 : Set where\n \u2205 : Ctx\u22c6 -- empty context\n _,\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Ctx\u22c6 -- context extension\n\\end{code}\n\n\\noindent Let \\AgdaBound{$\\Phi$} and \\AgdaBound{$\\Psi$} range over\ncontexts.\n\n\\subsection{Type Variables}\n\nWe use de Bruijn indices for variables. They are natural numbers\naugmented with additional kind and context information. The kind index\ntells us the kind of the variable and the context index ensures that\nthe variable is in scope. It is impossible to write a variable that\nisn't in the context. \\AgdaInductiveConstructor{Z} refers to the last\nvariable introduced on the right hand end of the context. Adding one\nto a variable via \\AgdaInductiveConstructor{S} moves one position to\nthe left in the context. Note that there is no way to construct a\nvariable in the empty context as it would be out of scope. Indeed,\nthere is no way at all to construct a variable that is out of scope.\n\n\\begin{code}\ndata _\u220b\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Set where\n Z : \u2200 {\u03d5 J} \u2192 \u03d5 ,\u22c6 J \u220b\u22c6 J\n S : \u2200 {\u03d5 J K} \u2192 \u03d5 \u220b\u22c6 J \u2192 \u03d5 ,\u22c6 K \u220b\u22c6 J\n\\end{code}\n\n\\noindent Let \\AgdaBound{$\\alpha$} and \\AgdaBound{$\\beta$} range over type\nvariables.\n\n\\subsection{Types}\n\nTypes, like type variables, are indexed by context and kind, ensuring\nwell-scopedness and well-kindedness. The first three constructors\n\\AgdaInductiveConstructor{`} \\AgdaInductiveConstructor{$\\lambdabar$}\nand \\AgdaInductiveConstructor{$\\cdot$} are analogous to the terms of\nSTLC. This is extended with the \\AgdaInductiveConstructor{$\\Pi$} type\nto classify type abstractions at the type level, function type\n\\AgdaInductiveConstructor{$\\Rightarrow$} to classify functions, and\n\\AgdaInductiveConstructor{$\\mu$} to classify recursive terms. Note\nthat \\AgdaInductiveConstructor{$\\Pi$},\n\\AgdaInductiveConstructor{$\\Rightarrow$}, and\n\\AgdaInductiveConstructor{$\\mu$} are effectively base types as they\nlive at kind \\AgdaInductiveConstructor{*}.\n\n\\begin{code}\ndata _\u22a2\u22c6_ \u03a6 : Kind \u2192 Set where\n ` : \u2200{J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u03a6 \u22a2\u22c6 J -- type variable\n \u019b : \u2200{K J} \u2192 \u03a6 ,\u22c6 K \u22a2\u22c6 J \u2192 \u03a6 \u22a2\u22c6 K \u21d2 J -- type lambda\n _\u00b7_ : \u2200{K J} \u2192 \u03a6 \u22a2\u22c6 K \u21d2 J \u2192 \u03a6 \u22a2\u22c6 K \u2192 \u03a6 \u22a2\u22c6 J -- type application\n _\u21d2_ : \u03a6 \u22a2\u22c6 * \u2192 \u03a6 \u22a2\u22c6 * \u2192 \u03a6 \u22a2\u22c6 * -- function type\n \u03a0 : \u2200{K} \u2192 \u03a6 ,\u22c6 K \u22a2\u22c6 * \u2192 \u03a6 \u22a2\u22c6 * -- Pi\/forall type\n \u03bc : \u03a6 ,\u22c6 * \u22a2\u22c6 * \u2192 \u03a6 \u22a2\u22c6 * -- recursive type\n\\end{code}\n\n\\noindent Let \\AgdaBound{A} and \\AgdaBound{B} range over types.\n\n\\subsection{Type Renaming}\n\nTypes can contain functions and as such are subject to a nontrivial\nequality relation. To explain the computation equation (the\n$\\beta$-rule) we need to define substitution for a single type\nvariable in a type. Later, when we define terms that are indexed by\ntheir type we will need to be able to weaken types by an extra kind\n(section \\ref{sec:term-var}) and also, again, substitute for a\nsingle type variable in a type (section \\ref{sec:term}). There are\nvarious different ways to define the required weakening and\nsubstitution operations. We choose to define so-called parallel\nrenaming and substitution i.e. renaming\/substitution of several\nvariables at once. Weakening and single variable substitution are\nspecial cases of these operations.\n\nWe follow Altenkirch and Reus \\cite{altenkirch.reus:welltyped} and\nimplement renaming first and then substitution using renaming. In our\nopinion the biggest advantage of this approach is that it has a very\nclear mathematical theory. The necessary correctness properties of\nrenaming are identified with the notion of a functor and the\ncorrectness properties of substitution are identified with the notion\nof a relative monad. For the purposes of reading this paper it is not\nnecessary to understand relative monads in detail. The important thing\nis that, like ordinary monads, they have a return and bind and the\nrules that govern them are the same. It is only the types of the\noperations involved that are different. The interested reader may\nconsult \\cite{relmon} for a detailed investigation of relative monads\nand \\cite{relmonformalized} for a directly applicable investigation of\nsubstitution of STLC as a relative monad.\n\nA type renaming is a function from type variables in one context to\ntype variables in another. This is much more flexibility than we\nneed. We only need the ability to introduce new variable on the right\nhand side of the context. The simplicity of the definition makes it\neasy to work with and we get some properties for free that we would\nhave to pay for with a first order representation, such as not needing\nto define a lookup function, and we inherit the properties of functions\nprovided by $\\eta$-equality, such as associativity of composition, for\nfree. Note that even though renamings are functions we do not require\nour metatheory (Agda's type system) to support functional\nextensionality. As pointed out to us by Conor McBride we only ever\nneed to make use of an equation between renamings on a point (a\nvariable) and therefore need only a pointwise version of equality on\nfunctions to work with equality of renamings and substitutions.\n\n\\begin{code}\nRen\u22c6 : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nRen\u22c6 \u03a6 \u03c8 = \u2200 {J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u03c8 \u220b\u22c6 J\n\\end{code}\n\n\\noindent\nLet \\AgdaBound{\u03c1} range over type renamings.\n\nAs we are going to push renamings through types we need to be able to\npush them under a binder. To do this safely the newly bound variable\nshould remain untouched and other renamings should be shifted by one\nto accommodate this. This is exactly what the\n\\AgdaFunction{lift\u22c6} function does and it is defined by\nrecursion on variables:\n\n\\begin{code}\nlift\u22c6 : \u2200 {\u03d5 \u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u2200 {K} \u2192 Ren\u22c6 (\u03d5 ,\u22c6 K) (\u03c8 ,\u22c6 K)\nlift\u22c6 \u03c1 Z = Z -- leave newly bound variable untouched\nlift\u22c6 \u03c1 (S \u03b1) = S (\u03c1 \u03b1) -- apply renaming to other variables and add 1\n\\end{code}\n\n\\noindent\nNext we define the action of renaming on types. This is defined\nby recursion on the type. Observe that we lift the renaming when we\ngo under a binder and actually apply the renaming when we hit a\nvariable:\n\n\\begin{code}\nren\u22c6 : \u2200 {\u03d5 \u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u2200 {J} \u2192 \u03d5 \u22a2\u22c6 J \u2192 \u03c8 \u22a2\u22c6 J\nren\u22c6 \u03c1 (` \u03b1) = ` (\u03c1 \u03b1)\nren\u22c6 \u03c1 (\u019b B) = \u019b (ren\u22c6 (lift\u22c6 \u03c1) B)\nren\u22c6 \u03c1 (A \u00b7 B) = ren\u22c6 \u03c1 A \u00b7 ren\u22c6 \u03c1 B\nren\u22c6 \u03c1 (A \u21d2 B) = ren\u22c6 \u03c1 A \u21d2 ren\u22c6 \u03c1 B\nren\u22c6 \u03c1 (\u03a0 B) = \u03a0 (ren\u22c6 (lift\u22c6 \u03c1) B)\nren\u22c6 \u03c1 (\u03bc B) = \u03bc (ren\u22c6 (lift\u22c6 \u03c1) B)\n\\end{code}\n\n\\noindent Weakening is a special case of renaming. We apply the\nrenaming \\AgdaInductiveConstructor{S} which does double duty as the\nvariable constructor, if we check the type of\n\\AgdaInductiveConstructor{S} we see that it is a renaming.\n\nWeakening shifts all the existing variables one place to the left in\nthe context:\n\n\\begin{code}\nweaken\u22c6 : \u2200 {\u03d5 J K} \u2192 \u03d5 \u22a2\u22c6 J \u2192 \u03d5 ,\u22c6 K \u22a2\u22c6 J\nweaken\u22c6 = ren\u22c6 S\n\\end{code}\n\n\\subsection{Type Substitution}\n\\label{sec:type-substitution}\n\nHaving defined renaming we are now ready to define substitution for\ntypes. Substitutions are defined as functions from type variables to\ntypes:\n\n\\begin{code}\nSub\u22c6 : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nSub\u22c6 \u03d5 \u03c8 = \u2200 {J} \u2192 \u03d5 \u220b\u22c6 J \u2192 \u03c8 \u22a2\u22c6 J\n\\end{code}\n\n\\noindent\nLet \\AgdaBound{$\\sigma$} range over substitutions.\n\nWe must be able to lift substitutions when we push them under\nbinders. Notice that we leave the newly bound variable intact and make\nuse of \\AgdaFunction{weaken\u22c6} to weaken a term that is substituted.\n\n\\begin{code}\nlifts\u22c6 : \u2200 {\u03d5 \u03c8} \u2192 Sub\u22c6 \u03d5 \u03c8 \u2192 \u2200 {K} \u2192 Sub\u22c6 (\u03d5 ,\u22c6 K) (\u03c8 ,\u22c6 K)\nlifts\u22c6 \u03c3 Z = ` Z -- leave newly bound variable untouched\nlifts\u22c6 \u03c3 (S \u03b1) = weaken\u22c6 (\u03c3 \u03b1) -- apply substitution and weaken\n\\end{code}\n\n\\noindent\nAnalogously to renaming, we define the action of substitutions on types:\n\n\\begin{code}\nsub\u22c6 : \u2200 {\u03d5 \u03c8} \u2192 Sub\u22c6 \u03d5 \u03c8 \u2192 \u2200 {J} \u2192 \u03d5 \u22a2\u22c6 J \u2192 \u03c8 \u22a2\u22c6 J\nsub\u22c6 \u03c3 (` \u03b1) = \u03c3 \u03b1\nsub\u22c6 \u03c3 (\u019b B) = \u019b (sub\u22c6 (lifts\u22c6 \u03c3) B)\nsub\u22c6 \u03c3 (A \u00b7 B) = sub\u22c6 \u03c3 A \u00b7 sub\u22c6 \u03c3 B\nsub\u22c6 \u03c3 (A \u21d2 B) = sub\u22c6 \u03c3 A \u21d2 sub\u22c6 \u03c3 B\nsub\u22c6 \u03c3 (\u03a0 B) = \u03a0 (sub\u22c6 (lifts\u22c6 \u03c3) B)\nsub\u22c6 \u03c3 (\u03bc B) = \u03bc (sub\u22c6 (lifts\u22c6 \u03c3) B)\n\\end{code}\n\n\\noindent Substitutions could be implemented as lists of types and\nthen the \\emph{cons} constructor would extend a substitution by an\nadditional term. Using our functional representation for substitutions\nit is convenient to define an operation for this. This effectively\ndefines a new function that, if it is applied to the \\AgdaBound{Z}\nvariable, returns our additional terms and otherwise invokes the\noriginal substitution.\n\n\\begin{code}\nextend\u22c6 : \u2200{\u03d5 \u03c8} \u2192 Sub\u22c6 \u03d5 \u03c8 \u2192 \u2200{J}(A : \u03c8 \u22a2\u22c6 J) \u2192 Sub\u22c6 (\u03d5 ,\u22c6 J) \u03c8\nextend\u22c6 \u03c3 A Z = A -- project out additional term\nextend\u22c6 \u03c3 A (S \u03b1) = \u03c3 \u03b1 -- apply original substitution\n\\end{code}\n\n\\noindent Substitution of a single type variable is a special case of\nparallel substitution \\AgdaFunction{sub\u22c6}. Note we make use of\n\\AgdaFunction{extend\u22c6} to define the appropriate substitution by\nextending the substitution \\AgdaInductiveConstructor{`} with the type\n\\AgdaBound{A}. Notice that the variable constructor\n\\AgdaInductiveConstructor{`} serves double duty as the identity\nsubstitution:\n\n\\begin{code}\n_[_]\u22c6 : \u2200 {\u03d5 J K} \u2192 \u03d5 ,\u22c6 K \u22a2\u22c6 J \u2192 \u03d5 \u22a2\u22c6 K \u2192 \u03d5 \u22a2\u22c6 J\nB [ A ]\u22c6 = sub\u22c6 (extend\u22c6 ` A) B\n\\end{code}\n\n\\noindent At this point the reader may well ask how we know that our\nsubstitution and renaming operations are the right ones. One\nindication that we have the right definitions is that renaming defines\na functor, and that substitution forms a relative monad. Further,\nevaluation (\\AgdaFunction{eval} defined in section\n\\ref{sec:type-normalisation}) can be seen as an algebra of this\nrelative monad. This categorical structure results in clean proofs.\n\nAdditionally, without some sort of compositional structure to our\nrenaming and substitution, we would be unable to define coherent type\nlevel operations. For example, we must have that performing two\nsubstitutions in sequence results in the same type as performing the\ncomposite of the two substitutions. We assert that these are necessary\nfunctional correctness properties and structure our proofs\naccordingly.\n\nBack in our development we show that lifting a renaming and the action\nof renaming satisfy the functor laws where \\AgdaFunction{lift\u22c6} and\n\\AgdaFunction{ren\u22c6} are both functorial actions.\n\n\\begin{code}\nlift\u22c6-id : \u2200 {\u03d5 J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J) \u2192 lift\u22c6 id \u03b1 \u2261 \u03b1\nlift\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c1' : Ren\u22c6 \u03c8 \u0398}{J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J)\n \u2192 lift\u22c6 (\u03c1' \u2218 \u03c1) \u03b1 \u2261 lift\u22c6 \u03c1' (lift\u22c6 \u03c1 \u03b1)\n \nren\u22c6-id : \u2200{\u03d5 J}(A : \u03d5 \u22a2\u22c6 J) \u2192 ren\u22c6 id A \u2261 A\nren\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c1' : Ren\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2\u22c6 J)\n \u2192 ren\u22c6 (\u03c1' \u2218 \u03c1) A \u2261 ren\u22c6 \u03c1' (ren\u22c6 \u03c1 A)\n\\end{code}\n\\begin{code}[hide]\n-- 1st functor law for lift\u22c6\nlift\u22c6-id Z = refl\nlift\u22c6-id (S \u03b1) = refl\n\n-- congruence for lift\u22c6, pointwise equality of renamings\nlift\u22c6-cong : \u2200 {\u03d5 \u03c8}{\u03c3 \u03c3' : Ren\u22c6 \u03d5 \u03c8} \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 \u2200{J K}(\u03b1 : \u03d5 ,\u22c6 J \u220b\u22c6 K) \u2192 lift\u22c6 \u03c3 \u03b1 \u2261 lift\u22c6 \u03c3' \u03b1\nlift\u22c6-cong p Z = refl\nlift\u22c6-cong p (S \u03b1) = cong S (p \u03b1)\n\n-- congruence for ren\u22c6, pointwise equality of renamings\nren\u22c6-cong : \u2200 {\u03d5 \u03c8}{\u03c3 \u03c3' : Ren\u22c6 \u03d5 \u03c8} \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 \u2200{K}(A : \u03d5 \u22a2\u22c6 K) \u2192 ren\u22c6 \u03c3 A \u2261 ren\u22c6 \u03c3' A\nren\u22c6-cong p (` \u03b1) = cong ` (p \u03b1)\nren\u22c6-cong p (\u019b A) = cong \u019b (ren\u22c6-cong (lift\u22c6-cong p) A)\nren\u22c6-cong p (A \u00b7 B) = cong\u2082 _\u00b7_ (ren\u22c6-cong p A) (ren\u22c6-cong p B)\nren\u22c6-cong p (A \u21d2 B) = cong\u2082 _\u21d2_ (ren\u22c6-cong p A) (ren\u22c6-cong p B)\nren\u22c6-cong p (\u03a0 A) = cong \u03a0 (ren\u22c6-cong (lift\u22c6-cong p) A)\nren\u22c6-cong p (\u03bc A) = cong \u03bc (ren\u22c6-cong (lift\u22c6-cong p) A)\n\n-- 1st functor law for ren\u22c6\nren\u22c6-id (` \u03b1) = refl\nren\u22c6-id (\u019b A) = cong \u019b (trans (ren\u22c6-cong lift\u22c6-id A) (ren\u22c6-id A))\nren\u22c6-id (A \u00b7 B) = cong\u2082 _\u00b7_ (ren\u22c6-id A) (ren\u22c6-id B)\nren\u22c6-id (A \u21d2 B) = cong\u2082 _\u21d2_ (ren\u22c6-id A) (ren\u22c6-id B)\nren\u22c6-id (\u03a0 A) = cong \u03a0 (trans (ren\u22c6-cong lift\u22c6-id A) (ren\u22c6-id A))\nren\u22c6-id (\u03bc A) = cong \u03bc (trans (ren\u22c6-cong lift\u22c6-id A) (ren\u22c6-id A))\n\n-- 2nd functor law for lift\u22c6\nlift\u22c6-comp Z = refl\nlift\u22c6-comp (S x) = refl\n\n-- 2nd functor law for ren\u22c6\nren\u22c6-comp (` \u03b1) = refl\nren\u22c6-comp (\u019b A) =\n cong \u019b (trans (ren\u22c6-cong lift\u22c6-comp A) (ren\u22c6-comp A))\nren\u22c6-comp (A \u00b7 B) = cong\u2082 _\u00b7_ (ren\u22c6-comp A) (ren\u22c6-comp B)\nren\u22c6-comp (A \u21d2 B) = cong\u2082 _\u21d2_ (ren\u22c6-comp A) (ren\u22c6-comp B)\nren\u22c6-comp (\u03a0 A) =\n cong \u03a0 (trans (ren\u22c6-cong lift\u22c6-comp A) (ren\u22c6-comp A))\nren\u22c6-comp (\u03bc A) =\n cong \u03bc (trans (ren\u22c6-cong lift\u22c6-comp A) (ren\u22c6-comp A))\n\\end{code}\n\n\\noindent Lifting a substitution satisfies the functor laws where\n\\AgdaFunction{lift\u22c6} is a functorial action:\n\n\\begin{code}\nlifts\u22c6-id : \u2200 {\u03d5 J K}(x : \u03d5 ,\u22c6 K \u220b\u22c6 J) \u2192 lifts\u22c6 ` x \u2261 ` x \nlifts\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03c3' : Sub\u22c6 \u03c8 \u0398}{J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J)\n \u2192 lifts\u22c6 (sub\u22c6 \u03c3' \u2218 \u03c3) \u03b1 \u2261 sub\u22c6 (lifts\u22c6 \u03c3') (lifts\u22c6 \u03c3 \u03b1)\n\\end{code}\n\n%\\noindent\n%Various fusion laws about renaming and substitution are needed\n%to prove the required properties about substitution:\n\n\\begin{code}[hide]\nlifts\u22c6-lift\u22c6 : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c3 : Sub\u22c6 \u03c8 \u0398}{J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J)\n \u2192 lifts\u22c6 (\u03c3 \u2218 \u03c1) \u03b1 \u2261 lifts\u22c6 \u03c3 (lift\u22c6 \u03c1 \u03b1)\n \nsub\u22c6-ren\u22c6 : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c3 : Sub\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2\u22c6 J)\n \u2192 sub\u22c6 (\u03c3 \u2218 \u03c1) A \u2261 sub\u22c6 \u03c3 (ren\u22c6 \u03c1 A)\n \nren\u22c6-lift\u22c6-lifts\u22c6 : \u2200{\u03d5 \u03c8 \u0398}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03c1 : Ren\u22c6 \u03c8 \u0398}{J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J)\n \u2192 lifts\u22c6 (ren\u22c6 \u03c1 \u2218 \u03c3) \u03b1 \u2261 ren\u22c6 (lift\u22c6 \u03c1) (lifts\u22c6 \u03c3 \u03b1)\n \nren\u22c6-sub\u22c6 : \u2200{\u03d5 \u03c8 \u0398}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03c1 : Ren\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2\u22c6 J)\n \u2192 sub\u22c6 (ren\u22c6 \u03c1 \u2218 \u03c3) A \u2261 ren\u22c6 \u03c1 (sub\u22c6 \u03c3 A)\n\\end{code}\n\n\\noindent The action of substitution satisfies the relative monad laws\nwhere \\AgdaInductiveConstructor{`} is return and \\AgdaFunction{sub\u22c6} is bind:\n\n\\begin{code}\nsub\u22c6-id : \u2200 {\u03d5 J}(A : \u03d5 \u22a2\u22c6 J) \u2192 sub\u22c6 ` A \u2261 A\nsub\u22c6-var : \u2200 {\u03d5 \u03c8}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 sub\u22c6 \u03c3 (` \u03b1) \u2261 \u03c3 \u03b1\nsub\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03c3' : Sub\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2\u22c6 J)\n \u2192 sub\u22c6 (sub\u22c6 \u03c3' \u2218 \u03c3) A \u2261 sub\u22c6 \u03c3' (sub\u22c6 \u03c3 A)\n\\end{code}\n\n \\noindent Note that the second law holds definitionally, it is the\nfirst line of the definition of \\AgdaFunction{sub\u22c6}.\n\n\\begin{code}[hide]\n-- we can push a renaming into an extend\u22c6\nren\u22c6-extend\u22c6 : \u2200{\u0393 \u0394}{J K}(\u03c1 : Ren\u22c6 \u0393 \u0394)(A : \u0393 \u22a2\u22c6 K)(\u03b1 : \u0393 ,\u22c6 K \u220b\u22c6 J)\n \u2192 extend\u22c6 ` (ren\u22c6 \u03c1 A) (lift\u22c6 \u03c1 \u03b1) \u2261 ren\u22c6 \u03c1 (extend\u22c6 ` A \u03b1)\n \n-- we can push a substitution into an extend\u22c6\nsub\u22c6-extend\u22c6 : \u2200{\u0393 \u0394}{J K}(\u03c3 : Sub\u22c6 \u0393 \u0394)(A : \u0393 \u22a2\u22c6 K)(\u03b1 : \u0393 ,\u22c6 K \u220b\u22c6 J)\n \u2192 sub\u22c6 (extend\u22c6 ` (sub\u22c6 \u03c3 A)) (lifts\u22c6 \u03c3 \u03b1) \u2261 sub\u22c6 \u03c3 (extend\u22c6 ` A \u03b1)\n\n-- 1st functor law for lifts\u22c6\nlifts\u22c6-id Z = refl\nlifts\u22c6-id (S x) = refl\n\n-- congruence for lifts\u22c6 lifts\u22c6, pointwise equality on functions\nlifts\u22c6-cong : \u2200 {\u03d5 \u03c8}\n \u2192 {\u03c3 \u03c3' : Sub\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 \u2200{J K}(\u03b1 : \u03d5 ,\u22c6 K \u220b\u22c6 J)\n \u2192 lifts\u22c6 \u03c3 \u03b1 \u2261 lifts\u22c6 \u03c3' \u03b1\nlifts\u22c6-cong p Z = refl\nlifts\u22c6-cong p (S \u03b1) = cong weaken\u22c6 (p \u03b1)\n\n-- congruence for sub\u22c6, pointwise equality on substitutions\nsub\u22c6-cong : \u2200 {\u03d5 \u03c8}\n \u2192 {\u03c3 \u03c3' : Sub\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 \u2200{K}(A : \u03d5 \u22a2\u22c6 K)\n \u2192 sub\u22c6 \u03c3 A \u2261 sub\u22c6 \u03c3' A\nsub\u22c6-cong p (` \u03b1) = p \u03b1\nsub\u22c6-cong p (\u019b A) = cong \u019b (sub\u22c6-cong (lifts\u22c6-cong p) A)\nsub\u22c6-cong p (A \u00b7 B) = cong\u2082 _\u00b7_ (sub\u22c6-cong p A) (sub\u22c6-cong p B)\nsub\u22c6-cong p (A \u21d2 B) = cong\u2082 _\u21d2_ (sub\u22c6-cong p A) (sub\u22c6-cong p B)\nsub\u22c6-cong p (\u03a0 A) = cong \u03a0 (sub\u22c6-cong (lifts\u22c6-cong p) A)\nsub\u22c6-cong p (\u03bc A) = cong \u03bc (sub\u22c6-cong (lifts\u22c6-cong p) A)\n\n-- 1st monad law for sub\u22c6 \nsub\u22c6-id (` x) = refl\nsub\u22c6-id (\u019b A) = cong \u019b (trans (sub\u22c6-cong lifts\u22c6-id A) (sub\u22c6-id A))\nsub\u22c6-id (A \u00b7 B) = cong\u2082 _\u00b7_ (sub\u22c6-id A) (sub\u22c6-id B)\nsub\u22c6-id (A \u21d2 B) = cong\u2082 _\u21d2_ (sub\u22c6-id A) (sub\u22c6-id B)\nsub\u22c6-id (\u03a0 A) = cong \u03a0 (trans (sub\u22c6-cong lifts\u22c6-id A) (sub\u22c6-id A))\nsub\u22c6-id (\u03bc A) = cong \u03bc (trans (sub\u22c6-cong lifts\u22c6-id A) (sub\u22c6-id A))\n\n-- 2nd rel. monad law for sub\u22c6 (holds definitionally)\nsub\u22c6-var \u03b1 = refl\n\n-- fusion for lifts\u22c6 and lift\u22c6\nlifts\u22c6-lift\u22c6 Z = refl\nlifts\u22c6-lift\u22c6 (S x) = refl\n\n-- fusion for sub\u22c6 and ren\u22c6\nsub\u22c6-ren\u22c6 (` \u03b1) = refl\nsub\u22c6-ren\u22c6 (\u019b A) =\n cong \u019b (trans (sub\u22c6-cong lifts\u22c6-lift\u22c6 A) (sub\u22c6-ren\u22c6 A))\nsub\u22c6-ren\u22c6 (A \u00b7 B) = cong\u2082 _\u00b7_ (sub\u22c6-ren\u22c6 A) (sub\u22c6-ren\u22c6 B)\nsub\u22c6-ren\u22c6 (A \u21d2 B) = cong\u2082 _\u21d2_ (sub\u22c6-ren\u22c6 A) (sub\u22c6-ren\u22c6 B)\nsub\u22c6-ren\u22c6 (\u03a0 A) =\n cong \u03a0 (trans (sub\u22c6-cong lifts\u22c6-lift\u22c6 A) (sub\u22c6-ren\u22c6 A))\nsub\u22c6-ren\u22c6 (\u03bc A) =\n cong \u03bc (trans (sub\u22c6-cong lifts\u22c6-lift\u22c6 A) (sub\u22c6-ren\u22c6 A))\n\nren\u22c6-lift\u22c6-lifts\u22c6 Z = refl\nren\u22c6-lift\u22c6-lifts\u22c6 (S x) = trans (sym (ren\u22c6-comp _)) (ren\u22c6-comp _)\n\n-- fusion for ren\u22c6 and sub\u22c6\nren\u22c6-sub\u22c6 (` \u03b1) = refl\nren\u22c6-sub\u22c6 (\u019b A) =\n cong \u019b (trans (sub\u22c6-cong ren\u22c6-lift\u22c6-lifts\u22c6 A) (ren\u22c6-sub\u22c6 A))\nren\u22c6-sub\u22c6 (A \u00b7 B) = cong\u2082 _\u00b7_ (ren\u22c6-sub\u22c6 A) (ren\u22c6-sub\u22c6 B)\nren\u22c6-sub\u22c6 (A \u21d2 B) = cong\u2082 _\u21d2_ (ren\u22c6-sub\u22c6 A) (ren\u22c6-sub\u22c6 B)\nren\u22c6-sub\u22c6 (\u03a0 A) =\n cong \u03a0 (trans (sub\u22c6-cong ren\u22c6-lift\u22c6-lifts\u22c6 A) (ren\u22c6-sub\u22c6 A))\nren\u22c6-sub\u22c6 (\u03bc A) =\n cong \u03bc (trans (sub\u22c6-cong ren\u22c6-lift\u22c6-lifts\u22c6 A) (ren\u22c6-sub\u22c6 A))\n\n-- 2nd functor law for lifts\u22c6\nlifts\u22c6-comp Z = refl\nlifts\u22c6-comp {\u03c3 = \u03c3} (S \u03b1) = trans (sym (ren\u22c6-sub\u22c6 (\u03c3 \u03b1))) (sub\u22c6-ren\u22c6 (\u03c3 \u03b1))\n\n-- 2nd functor law for sub\u22c6\nsub\u22c6-comp (` \u03b1) = refl\nsub\u22c6-comp (\u019b A) = cong \u019b (trans (sub\u22c6-cong lifts\u22c6-comp A) (sub\u22c6-comp A))\nsub\u22c6-comp (A \u00b7 B) = cong\u2082 _\u00b7_ (sub\u22c6-comp A) (sub\u22c6-comp B)\nsub\u22c6-comp (A \u21d2 B) = cong\u2082 _\u21d2_ (sub\u22c6-comp A) (sub\u22c6-comp B)\nsub\u22c6-comp (\u03a0 A) = cong \u03a0 (trans (sub\u22c6-cong lifts\u22c6-comp A) (sub\u22c6-comp A))\nsub\u22c6-comp (\u03bc A) = cong \u03bc (trans (sub\u22c6-cong lifts\u22c6-comp A) (sub\u22c6-comp A))\n\n-- pushing a renaming through an extend\u22c6\nren\u22c6-extend\u22c6 \u03c1 A Z = refl\nren\u22c6-extend\u22c6 \u03c1 A (S \u03b1) = refl\n-- pushing a substitution through an extend\u22c6\nsub\u22c6-extend\u22c6 \u03c3 A Z = refl\nsub\u22c6-extend\u22c6 \u03c3 A (S \u03b1) = trans (sym (sub\u22c6-ren\u22c6 (\u03c3 \u03b1))) (sub\u22c6-id (\u03c3 \u03b1))\n\\end{code}\n\n\\subsection{Type Equality}\n\n\\begin{code}[hide]\ninfix 1 _\u2261\u03b2_\n\\end{code}\n\nWe define type equality as an intrinsically scoped and kinded\nrelation. In particular, this means it is impossible to state an\nequation between types in different contexts, or of different\nkinds. The only interesting rule is the $\\beta$-rule from the lambda\ncalculus. We omit the $\\eta$-rule as Plutus Core does not have it. The\nformalisation could be easily modified to include it and it would\nslightly simplify the type normalisation proof. The additional types\n(\\AgdaInductiveConstructor{$\\Rightarrow$},\n\\AgdaInductiveConstructor{$\\forall$}, and\n\\AgdaInductiveConstructor{$\\mu$}) do not have any computational\nbehaviour, and are essentially inert. In particular, the fixed point\noperator \\AgdaInductiveConstructor{$\\mu$} does not complicate the\nequational theory.\n\n\\begin{code}\ndata _\u2261\u03b2_ {\u03a6} : \u2200{J} \u2192 \u03a6 \u22a2\u22c6 J \u2192 \u03a6 \u22a2\u22c6 J \u2192 Set where\n \u03b2\u2261\u03b2 : \u2200{K J}(B : \u03a6 ,\u22c6 J \u22a2\u22c6 K)(A : \u03a6 \u22a2\u22c6 J) \u2192 \u019b B \u00b7 A \u2261\u03b2 B [ A ]\u22c6\n -- remaining rules hidden\n\\end{code}\n\\begin{code}[hide]\n -- structural rules\n\n refl\u2261\u03b2 : \u2200{J}\n \u2192 (A : \u03a6 \u22a2\u22c6 J)\n ------------\n \u2192 A \u2261\u03b2 A\n \n sym\u2261\u03b2 : \u2200{J}{A B : \u03a6 \u22a2\u22c6 J}\n \u2192 A \u2261\u03b2 B\n ------\n \u2192 B \u2261\u03b2 A\n trans\u2261\u03b2 : \u2200{J}{A B C : \u03a6 \u22a2\u22c6 J}\n \u2192 A \u2261\u03b2 B\n \u2192 B \u2261\u03b2 C\n ------\n \u2192 A \u2261\u03b2 C\n\n -- congruence rules\n\n -- (no variable rule is needed)\n \n \u21d2\u2261\u03b2 : {A A' B B' : \u03a6 \u22a2\u22c6 *}\n \u2192 A \u2261\u03b2 A'\n \u2192 B \u2261\u03b2 B'\n ---------------------\n \u2192 (A \u21d2 B) \u2261\u03b2 (A' \u21d2 B')\n \n \u03a0\u2261\u03b2 : \u2200{J}{B B' : \u03a6 ,\u22c6 J \u22a2\u22c6 *}\n \u2192 B \u2261\u03b2 B'\n -------\n \u2192 \u03a0 B \u2261\u03b2 \u03a0 B'\n \n \u03bc\u2261\u03b2 : \u2200{B B' : \u03a6 ,\u22c6 * \u22a2\u22c6 *}\n \u2192 B \u2261\u03b2 B'\n -------\n \u2192 \u03bc B \u2261\u03b2 \u03bc B'\n\n \u019b\u2261\u03b2 : \u2200{K J}{B B' : \u03a6 ,\u22c6 J \u22a2\u22c6 K}\n \u2192 B \u2261\u03b2 B'\n ---------------\n \u2192 \u019b B \u2261\u03b2 \u019b B'\n \n \u00b7\u2261\u03b2 : \u2200{K J}{A A' : \u03a6 \u22a2\u22c6 K \u21d2 J}{B B' : \u03a6 \u22a2\u22c6 K}\n \u2192 A \u2261\u03b2 A'\n \u2192 B \u2261\u03b2 B'\n --------------------\n \u2192 A \u00b7 B \u2261\u03b2 A' \u00b7 B' \n\\end{code}\n\n\\noindent We omit the rules for reflexivity, symmetry,\ntransitivity, and congruence rules for type constructors.\n\n\n%Note that renaming and substitution preserve type equality:\n\\begin{code}[hide]\nren\u22c6\u2261\u03b2 : \u2200{\u03d5 \u03c8 J}{A B : \u03d5 \u22a2\u22c6 J}(\u03c1 : Ren\u22c6 \u03d5 \u03c8)\n \u2192 A \u2261\u03b2 B \u2192 ren\u22c6 \u03c1 A \u2261\u03b2 ren\u22c6 \u03c1 B\nsub\u22c6\u2261\u03b2 : \u2200{\u03d5 \u03c8 J}{A B : \u03d5 \u22a2\u22c6 J}(\u03c3 : Sub\u22c6 \u03d5 \u03c8)\n \u2192 A \u2261\u03b2 B \u2192 sub\u22c6 \u03c3 A \u2261\u03b2 sub\u22c6 \u03c3 B\n\\end{code}\n\\begin{code}[hide]\nren\u22c6\u2261\u03b2 \u03c1 (refl\u2261\u03b2 A) = refl\u2261\u03b2 (ren\u22c6 \u03c1 A)\nren\u22c6\u2261\u03b2 \u03c1 (sym\u2261\u03b2 p) = sym\u2261\u03b2 (ren\u22c6\u2261\u03b2 \u03c1 p)\nren\u22c6\u2261\u03b2 \u03c1 (trans\u2261\u03b2 p q) = trans\u2261\u03b2 (ren\u22c6\u2261\u03b2 \u03c1 p) (ren\u22c6\u2261\u03b2 \u03c1 q)\nren\u22c6\u2261\u03b2 \u03c1 (\u21d2\u2261\u03b2 p q) = \u21d2\u2261\u03b2 (ren\u22c6\u2261\u03b2 \u03c1 p) (ren\u22c6\u2261\u03b2 \u03c1 q)\nren\u22c6\u2261\u03b2 \u03c1 (\u03a0\u2261\u03b2 p) = \u03a0\u2261\u03b2 (ren\u22c6\u2261\u03b2 (lift\u22c6 \u03c1) p)\nren\u22c6\u2261\u03b2 \u03c1 (\u03bc\u2261\u03b2 p) = \u03bc\u2261\u03b2 (ren\u22c6\u2261\u03b2 (lift\u22c6 \u03c1) p)\nren\u22c6\u2261\u03b2 \u03c1 (\u019b\u2261\u03b2 p) = \u019b\u2261\u03b2 (ren\u22c6\u2261\u03b2 (lift\u22c6 \u03c1) p)\nren\u22c6\u2261\u03b2 \u03c1 (\u00b7\u2261\u03b2 p q) = \u00b7\u2261\u03b2 (ren\u22c6\u2261\u03b2 \u03c1 p) (ren\u22c6\u2261\u03b2 \u03c1 q)\nren\u22c6\u2261\u03b2 \u03c1 (\u03b2\u2261\u03b2 B A) =\n subst (ren\u22c6 \u03c1 ((\u019b B) \u00b7 A) \u2261\u03b2_)\n (trans (sym (sub\u22c6-ren\u22c6 B))\n (trans (sub\u22c6-cong (ren\u22c6-extend\u22c6 \u03c1 A) B)\n (ren\u22c6-sub\u22c6 B)))\n (\u03b2\u2261\u03b2 _ _)\n\nsub\u22c6\u2261\u03b2 \u03c3 (refl\u2261\u03b2 A) = refl\u2261\u03b2 (sub\u22c6 \u03c3 A)\nsub\u22c6\u2261\u03b2 \u03c3 (sym\u2261\u03b2 p) = sym\u2261\u03b2 (sub\u22c6\u2261\u03b2 \u03c3 p)\nsub\u22c6\u2261\u03b2 \u03c3 (trans\u2261\u03b2 p q) = trans\u2261\u03b2 (sub\u22c6\u2261\u03b2 \u03c3 p) (sub\u22c6\u2261\u03b2 \u03c3 q) \nsub\u22c6\u2261\u03b2 \u03c3 (\u21d2\u2261\u03b2 p q) = \u21d2\u2261\u03b2 (sub\u22c6\u2261\u03b2 \u03c3 p) (sub\u22c6\u2261\u03b2 \u03c3 q)\nsub\u22c6\u2261\u03b2 \u03c3 (\u03a0\u2261\u03b2 p) = \u03a0\u2261\u03b2 (sub\u22c6\u2261\u03b2 (lifts\u22c6 \u03c3) p)\nsub\u22c6\u2261\u03b2 \u03c3 (\u03bc\u2261\u03b2 p) = \u03bc\u2261\u03b2 (sub\u22c6\u2261\u03b2 (lifts\u22c6 \u03c3) p)\nsub\u22c6\u2261\u03b2 \u03c3 (\u019b\u2261\u03b2 p) = \u019b\u2261\u03b2 (sub\u22c6\u2261\u03b2 (lifts\u22c6 \u03c3) p)\nsub\u22c6\u2261\u03b2 \u03c3 (\u00b7\u2261\u03b2 p q) = \u00b7\u2261\u03b2 (sub\u22c6\u2261\u03b2 \u03c3 p) (sub\u22c6\u2261\u03b2 \u03c3 q)\nsub\u22c6\u2261\u03b2 \u03c3 (\u03b2\u2261\u03b2 B A) =\n subst (sub\u22c6 \u03c3 ((\u019b B) \u00b7 A) \u2261\u03b2_)\n (trans (trans (sym (sub\u22c6-comp B))\n (sub\u22c6-cong (sub\u22c6-extend\u22c6 \u03c3 A) B))\n (sub\u22c6-comp B))\n (\u03b2\u2261\u03b2 _ _)\n\\end{code}\n\n\\subsection{Term contexts}\n\nHaving dealt with the type level, we turn our attention to the term level.\n\n\\begin{code}[hide]\ninfix 4 _\u220bNf_\ninfix 3 _\u22a2Nf_\n\\end{code}\n\nTerms may contain types, and so the term level contexts must also\ntrack information about type variables in addition to term\nvariables. We would like to avoid having the extra syntactic baggage\nof multiple contexts. We do so by defining term contexts which contain\nboth (the kinds of) type variables and (the types of) term\nvariables. Term contexts are indexed over type contexts. In an earlier\nversion of this formalisation instead of indexing by type contexts we\ndefined inductive term contexts simultaneously with a recursive\nerasure operation that converts a term level context to a type level\ncontext by dropping the term variables but keeping the type\nvariables. Defining an inductive data type simultaneously with a\nrecursive function is referred to as \\emph{induction recursion}\n\\cite{induction-recursion}. This proved to be too cumbersome in later\nproofs as it can introduce a situation where there can be multiple\nprovably equal ways to recover the same type context and expressions\nbecome cluttered with proofs of such equations. In addition to the\ndifficulty of working with this version, it also made type checking\nthe examples in our formalisation much slower. In the version\npresented here neither of these problems arise.\n\n\\noindent A context is either empty, or it extends an existing context\nby a type variable of a given kind, or by a term variable of a given\ntype.\n\n\\begin{code}\ndata Ctx : Ctx\u22c6 \u2192 Set where\n \u2205 : Ctx \u2205\n -- empty term context\n _,\u22c6_ : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u2200 J \u2192 Ctx (\u03a6 ,\u22c6 J)\n -- extension by (the kind of) a type variable\n _,_ : \u2200 {\u03a6} \u2192 Ctx \u03a6 \u2192 \u03a6 \u22a2\u22c6 * \u2192 Ctx \u03a6\n -- extension by (the type of) a term variable\n\\end{code}\n\n\\noindent Let \\AgdaBound{\u0393}, \\AgdaBound{\u0394}, range over\ncontexts. Note that in the last rule\n\\AgdaInductiveConstructor{$\\_,\\_$}, the type we are extending by may\nonly refer to variables in the type context, a term that inhabits that\ntype may refer to any variable in its context.\n\n\\subsection{Term variables}\n\\label{sec:term-var}\nA variable is indexed by its context and type. While type variables\ncan appear in types, and those types can appear in terms, the\nvariables defined here are term level variables only.\n\nNotice that there is only one base constructor\n\\AgdaInductiveConstructor{Z}. This gives us exactly what we want: we\ncan only construct term variables. We have two ways to shift these\nvariables to the left, we use \\AgdaInductiveConstructor{S} to shift\nover a type and \\AgdaInductiveConstructor{T} to shift over a kind in\nthe context.\n\n\\begin{code}\ndata _\u220b_ : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u03a6 \u22a2\u22c6 * \u2192 Set where\n Z : \u2200{\u03a6 \u0393} {A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 , A \u220b A\n S : \u2200{\u03a6 \u0393} {A : \u03a6 \u22a2\u22c6 *} {B : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u220b A \u2192 \u0393 , B \u220b A \n T : \u2200{\u03a6 \u0393} {A : \u03a6 \u22a2\u22c6 *} {K} \u2192 \u0393 \u220b A \u2192 \u0393 ,\u22c6 K \u220b weaken\u22c6 A \n\\end{code}\n\n\\noindent Let \\AgdaBound{x}, \\AgdaBound{y} range over\nvariables. Notice that we need weakening of (System $F$) types in the\n(Agda) type of \\AgdaInductiveConstructor{T}. We must weaken\n\\AgdaBound{A} to shift it from context \\AgdaBound{$\\Gamma$} to context\n\\AgdaBound{$\\Gamma$} \\AgdaInductiveConstructor{,\u22c6}\n\\AgdaBound{K}. Indeed, \\AgdaFunction{weaken\u22c6} is a function and it\nappears in a type. This is possible due to the rich support for\ndependent types and in particular inductive families in Agda. It is\nhowever a feature that must be used with care and while it often seems\nto be the most natural option it can be more trouble than it is\nworth. We have learnt from experience, for example, that it is easier\nto work with renamings (morphisms between contexts) \\AgdaBound{\u03c1} :\n\\AgdaFunction{Ren} \\AgdaBound{\u0393} \\AgdaBound{\u0394} rather than context\nextensions \\AgdaBound{\u0393} \\AgdaFunction{+} \\AgdaBound{\u0394} where the\ncontexts are built from concatenation. The function \\AgdaFunction{+},\nwhose associativity holds only propositionally, is awkward to work\nwith when it appears in type indices. Renamings do not suffer from\nthis problem as no additional operations on contexts are needed as we\ncommonly refer to a renaming into an \\emph{arbitrary} new context\n(e.g., \\AgdaBound{\u0394}) rather than, precisely, an extension of an\nexisting one (e.g., \\AgdaBound{\u0393} \\AgdaFunction{+} \\AgdaBound{\u0394}). In\nthis formalisation we could have chosen to work with explicit\nrenamings and substitutions turning operations like\n\\AgdaFunction{weaken\u22c6} into more benign constructors but this would\nhave been overall more cumbersome and in this case we are able to work\nwith executable renaming and substitution cleanly. Doing so cleanly is\na contribution of this work.\n\n\\subsection{Terms}\n\\label{sec:term}\n\nA term is indexed by its context and type. A term is a variable, an\nabstraction, an application, a type abstraction, a type application, a\nwrapped term, an unwrapped term, or a term whose type is cast to another \nequal type.\n\n\\begin{code}\ndata _\u22a2_ {\u03a6} \u0393 : \u03a6 \u22a2\u22c6 * \u2192 Set where\n ` : \u2200{A} \u2192 \u0393 \u220b A \u2192 \u0393 \u22a2 A -- variable\n \u019b : \u2200{A B} \u2192 \u0393 , A \u22a2 B \u2192 \u0393 \u22a2 A \u21d2 B -- term \u03bb\n _\u00b7_ : \u2200{A B} \u2192 \u0393 \u22a2 A \u21d2 B \u2192 \u0393 \u22a2 A \u2192 \u0393 \u22a2 B -- term app\n \u039b : \u2200{K B} \u2192 \u0393 ,\u22c6 K \u22a2 B \u2192 \u0393 \u22a2 \u03a0 B -- type \u03bb\n _\u00b7\u22c6_ : \u2200{K B} \u2192 \u0393 \u22a2 \u03a0 B \u2192 (A : \u03a6 \u22a2\u22c6 K) \u2192 \u0393 \u22a2 B [ A ]\u22c6 -- type app\n wrap : \u2200 A \u2192 \u0393 \u22a2 A [ \u03bc A ]\u22c6 \u2192 \u0393 \u22a2 \u03bc A -- wrap\n unwrap : \u2200{A} \u2192 \u0393 \u22a2 \u03bc A \u2192 \u0393 \u22a2 A [ \u03bc A ]\u22c6 -- unwrap\n conv : \u2200{A B} \u2192 A \u2261\u03b2 B \u2192 \u0393 \u22a2 A \u2192 \u0393 \u22a2 B -- type cast\n\\end{code}\n\n\\noindent Let \\AgdaBound{L}, \\AgdaBound{M} range over terms. The\nlast rule \\AgdaInductiveConstructor{conv} is required as we have\ncomputation in types. So, a type which has a $\\beta$-redex in it is\nequal, via type equality, to the type where that redex is reduced. We\nwant a term which is typed by the original unreduced type to also be\ntyped by the reduced type. This is a standard typing rule but it looks\nstrange as a syntactic constructor. See \\cite{PTSpaper} for a\ndiscussion of syntax with explicit conversions.\n\nWe could give a dynamics for this syntax as a small-step reduction\nrelation but the \\AgdaInductiveConstructor{conv} case is\nproblematic. It is not enough to say that a conversion reduces if the\nunderlying term reduces. If a conversion is in the function position\n(also called head position) in an application it would block\n$\\beta$-reduction. We cannot prove progress directly for such a\nrelation. One could try to construct a dynamics for this system where\nduring reduction both terms and also types can make reduction steps\nand we could modify progress and explicitly prove preservation. We do\nnot pursue this here. In the system we present here we have the\nadvantage that the type level language is strongly normalising. In\nsection \\ref{sec:algorithmic} we are able to make use of this\nadvantage quite directly to solve the conversion problem in a\ndifferent way. An additional motivation for us to choose the\nnormalisation oriented approach is that in Plutus, contracts are\nstored and executed on chain with types normalised and this mode of\noperation is therefore needed anyway.\n\nIf we forget intrinsically typed syntax for a moment and consider\nthese rules as a type system then we observe that it is not syntax\ndirected, we cannot use it as the algorithmic specification of a type\nchecker as we can apply the conversion rule at any point. This is why\nwe refer to this version of the rules as \\emph{declarative} and the\nversion presented in section \\ref{sec:algorithmic}, which is (in\nthis specific sense) syntax directed, as \\emph{algorithmic}.\n\n\\section{Algorithmic Rules}\n\\label{sec:algorithmic}\n\nIn this section we remove the conversion rule from our system. Two\npromising approaches to achieving this are (1) to push traces of the\nconversion rule into the other rules which is difficult to prove\ncomplete \\cite{pollack} and (2) to normalise the types which collapses\nall the conversion proofs to reflexivity. In this paper we will pursue\nthe latter.\n\nIn the pursuit of (2) we have another important design decision to\nmake: which approach to take to normalisation. Indeed, another\nadditional aspect to this is that we need not only a normaliser but a\nnormal form respecting substitution operation. We choose to implement\na Normalisation-by-Evaluation (NBE) style normaliser and use that to\nimplement a substitution operation on normal forms.\n\nWe chose NBE as we are experienced with it and it has a clear\nmathematical structure (e.g., evaluation is a relative algebra for the\nrelative monad given by substitution) which gave us confidence that we\ncould construct a well structured normalisation proof that would\ncompute. The NBE approach is also centred around a normalisation\n\\emph{algorithm}: something that we want to use. Other approaches\nwould also work we expect. One option would be to try hereditary\nsubstitutions where the substitution operation is primary and use that\nto define a normaliser.\n\n\\Cref{sec:normal-types}--\\cref{sec:normal-substitution} describe the normal types, the normalisation\nalgorithm, its correctness proof, and a normalising substitution\noperation. Readers not interested in these details may skip to \n\\cref{sec:terms-normal-types}.\n\n\\subsection{Normal types}\n\\label{sec:normal-types}\n\nWe define a data type of $\\beta$-normal types which are either in\nconstructor form or neutral. Neutral types, which are defined mutually\nwith normal types, are either variables or (possibly nested)\napplications that are stuck on a variable in a function position, so\ncannot reduce. In this syntax, it is impossible to define an\nexpression containing a $\\beta$-redex.\n\n\\begin{code}[hide]\ninfix 4 _\u22a2Nf\u22c6_\ninfix 4 _\u22a2Ne\u22c6_\n\\end{code}\n\\begin{code}\ndata _\u22a2Nf\u22c6_ \u03a6 : Kind \u2192 Set\n\ndata _\u22a2Ne\u22c6_ \u03a6 J : Set where\n ` : \u03a6 \u220b\u22c6 J \u2192 \u03a6 \u22a2Ne\u22c6 J -- type var\n _\u00b7_ : \u2200{K} \u2192 \u03a6 \u22a2Ne\u22c6 (K \u21d2 J) \u2192 \u03a6 \u22a2Nf\u22c6 K \u2192 \u03a6 \u22a2Ne\u22c6 J -- neutral app\n\ndata _\u22a2Nf\u22c6_ \u03a6 where\n \u019b : \u2200{K J} \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J \u2192 \u03a6 \u22a2Nf\u22c6 (K \u21d2 J) -- type lambda\n ne : \u2200{K} \u2192 \u03a6 \u22a2Ne\u22c6 K \u2192 \u03a6 \u22a2Nf\u22c6 K -- neutral type\n _\u21d2_ : \u03a6 \u22a2Nf\u22c6 * \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 \u03a6 \u22a2Nf\u22c6 * -- function type\n \u03a0 : \u2200{K} \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 * \u2192 \u03a6 \u22a2Nf\u22c6 * -- pi\/forall type\n \u03bc : \u03a6 ,\u22c6 * \u22a2Nf\u22c6 * \u2192 \u03a6 \u22a2Nf\u22c6 * -- recursive type\n\\end{code}\n\n\\noindent Let \\AgdaBound{A}, \\AgdaBound{B} range over neutral and\nnormal types.\n\nAs before, we need weakening at the type level in the definition of\nterm level variables. As before, we define it as a special case of\nrenaming whose correctness we verify by proving the functor laws.\n\n\\begin{code}\nrenNf\u22c6 : \u2200{\u03d5 \u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u2200 {J} \u2192 \u03d5 \u22a2Nf\u22c6 J \u2192 \u03c8 \u22a2Nf\u22c6 J\nrenNe\u22c6 : \u2200{\u03d5 \u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u2200 {J} \u2192 \u03d5 \u22a2Ne\u22c6 J \u2192 \u03c8 \u22a2Ne\u22c6 J\nweakenNf\u22c6 : \u2200{\u03d5 J K} \u2192 \u03d5 \u22a2Nf\u22c6 J \u2192 \u03d5 ,\u22c6 K \u22a2Nf\u22c6 J\n\\end{code}\n\n\\noindent Renaming of normal and neutral types satisfies the functor\nlaws where \\AgdaFunction{renNf\u22c6} and \\AgdaFunction{renNe\u22c6} are both\nfunctorial actions:\n\n\\begin{code}\nrenNf\u22c6-id : \u2200{\u03d5 J}(A : \u03d5 \u22a2Nf\u22c6 J) \u2192 renNf\u22c6 id A \u2261 A\nrenNf\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c1' : Ren\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2Nf\u22c6 J)\n \u2192 renNf\u22c6 (\u03c1' \u2218 \u03c1) A \u2261 renNf\u22c6 \u03c1' (renNf\u22c6 \u03c1 A)\n\nrenNe\u22c6-id : \u2200{\u03d5 J}(A : \u03d5 \u22a2Ne\u22c6 J) \u2192 renNe\u22c6 id A \u2261 A\nrenNe\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}{\u03c1 : Ren\u22c6 \u03d5 \u03c8}{\u03c1' : Ren\u22c6 \u03c8 \u0398}{J}(A : \u03d5 \u22a2Ne\u22c6 J)\n \u2192 renNe\u22c6 (\u03c1' \u2218 \u03c1) A \u2261 renNe\u22c6 \u03c1' (renNe\u22c6 \u03c1 A)\n\\end{code}\n\n\\begin{code}[hide]\nrenNf\u22c6 \u03c1 (\u019b B) = \u019b (renNf\u22c6 (lift\u22c6 \u03c1) B)\nrenNf\u22c6 \u03c1 (ne A) = ne (renNe\u22c6 \u03c1 A)\nrenNf\u22c6 \u03c1 (A \u21d2 B) = renNf\u22c6 \u03c1 A \u21d2 renNf\u22c6 \u03c1 B\nrenNf\u22c6 \u03c1 (\u03a0 A) = \u03a0 (renNf\u22c6 (lift\u22c6 \u03c1) A)\nrenNf\u22c6 \u03c1 (\u03bc A) = \u03bc (renNf\u22c6 (lift\u22c6 \u03c1) A)\n\nrenNe\u22c6 \u03c1 (` \u03b1) = ` (\u03c1 \u03b1)\nrenNe\u22c6 \u03c1 (A \u00b7 B) = renNe\u22c6 \u03c1 A \u00b7 renNf\u22c6 \u03c1 B\n\nweakenNf\u22c6 = renNf\u22c6 S\n\n-- congruence for renNe\u22c6, pointwise equality of renamings\nrenNe\u22c6-cong : \u2200 {\u03d5 \u03c8}\n \u2192 {\u03c1 \u03c1' : Ren\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c1 \u03b1 \u2261 \u03c1' \u03b1)\n \u2192 \u2200{K}(A : \u03d5 \u22a2Ne\u22c6 K)\n -------------------------\n \u2192 renNe\u22c6 \u03c1 A \u2261 renNe\u22c6 \u03c1' A\n\n-- congruence for renNf\u22c6, pointwise equality of renamings\nrenNf\u22c6-cong : \u2200 {\u03d5 \u03c8}\n \u2192 {\u03c1 \u03c1' : Ren\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c1 \u03b1 \u2261 \u03c1' \u03b1)\n \u2192 \u2200{K}(A : \u03d5 \u22a2Nf\u22c6 K)\n ---------------------------\n \u2192 renNf\u22c6 \u03c1 A \u2261 renNf\u22c6 \u03c1' A\nrenNf\u22c6-cong p (\u019b A) = cong \u019b (renNf\u22c6-cong (lift\u22c6-cong p) A)\nrenNf\u22c6-cong p (ne A) = cong ne (renNe\u22c6-cong p A)\nrenNf\u22c6-cong p (A \u21d2 B) = cong\u2082 _\u21d2_ (renNf\u22c6-cong p A) (renNf\u22c6-cong p B)\nrenNf\u22c6-cong p (\u03a0 A) = cong \u03a0 (renNf\u22c6-cong (lift\u22c6-cong p) A)\nrenNf\u22c6-cong p (\u03bc A) = cong \u03bc (renNf\u22c6-cong (lift\u22c6-cong p) A)\n\n-- congruence for renNe\u22c6, pointwise equality of renamings\nrenNe\u22c6-cong p (` \u03b1) = cong ` (p \u03b1)\nrenNe\u22c6-cong p (A \u00b7 B) = cong\u2082 _\u00b7_ (renNe\u22c6-cong p A) (renNf\u22c6-cong p B)\n\n-- 1st functor law for renNf\u22c6\nrenNf\u22c6-id (\u019b A) =\n cong \u019b (trans (renNf\u22c6-cong lift\u22c6-id A) (renNf\u22c6-id A))\nrenNf\u22c6-id (ne A) = cong ne (renNe\u22c6-id A)\nrenNf\u22c6-id (A \u21d2 B) = cong\u2082 _\u21d2_ (renNf\u22c6-id A) (renNf\u22c6-id B)\nrenNf\u22c6-id (\u03a0 A) =\n cong \u03a0 (trans (renNf\u22c6-cong lift\u22c6-id A) (renNf\u22c6-id A))\nrenNf\u22c6-id (\u03bc A) =\n cong \u03bc (trans (renNf\u22c6-cong lift\u22c6-id A) (renNf\u22c6-id A))\n\n-- 1st functor law for renNe\u22c6\nrenNe\u22c6-id (` \u03b1) = refl\nrenNe\u22c6-id (A \u00b7 B) = cong\u2082 _\u00b7_ (renNe\u22c6-id A) (renNf\u22c6-id B)\n\n-- 2nd functor law for renNf\u22c6\nrenNf\u22c6-comp (\u019b A) = \n cong \u019b (trans (renNf\u22c6-cong lift\u22c6-comp A) (renNf\u22c6-comp A))\nrenNf\u22c6-comp (ne A) = cong ne (renNe\u22c6-comp A)\nrenNf\u22c6-comp (A \u21d2 B) = cong\u2082 _\u21d2_ (renNf\u22c6-comp A) (renNf\u22c6-comp B)\nrenNf\u22c6-comp (\u03a0 A) =\n cong \u03a0 (trans (renNf\u22c6-cong lift\u22c6-comp A) (renNf\u22c6-comp A))\nrenNf\u22c6-comp (\u03bc A) =\n cong \u03bc (trans (renNf\u22c6-cong lift\u22c6-comp A) (renNf\u22c6-comp A))\n\n-- 2nd functor law for renNe\u22c6\nrenNe\u22c6-comp (` \u03b1) = cong ` refl\nrenNe\u22c6-comp (A \u00b7 B) = cong\u2082 _\u00b7_ (renNe\u22c6-comp A) (renNf\u22c6-comp B)\n\\end{code}\n\n\\subsection{Type Normalisation algorithm}\n\\label{sec:type-normalisation}\n\nWe use the NBE approach introduced by \\cite{nbe}. This is a two stage\nprocess, first we evaluate into a semantic domain that supports open\nterms, then we reify these semantic terms back into normal forms.\n\nThe semantic domain \\AgdaFunction{\u22a7}, our notion of semantic value is\ndefined below. Like syntactic types and normal types it is indexed by\ncontext and kind. However, it is not a type defined as an inductive\ndata type. Instead, it is function that returns a type. More\nprecisely, it is a function that takes a context and, by recursion on\nkinds, defines a new type. At base kind it is defined to be the type\nof normal types. At function kind it is either a neutral type at\nfunction kind or a semantic function. If it is a semantic function\nthen we are essentially interpreting object level (type) functions as\nmeta level (Agda) functions. The additional renaming argument means we\nhave a so-called \\emph{Kripke function space} (\\cite{KripkeLR}). This\nis essential for our purposes as it allows us to introduce new free\nvariables into the context and then apply functions to them. Without\nthis feature we would not be able to reify from semantic values to\nnormal forms.\n\n\\begin{code}\n_\u22a7_ : Ctx\u22c6 \u2192 Kind \u2192 Set\n\u03d5 \u22a7 * = \u03d5 \u22a2Nf\u22c6 *\n\u03d5 \u22a7 (K \u21d2 J) = \u03d5 \u22a2Ne\u22c6 (K \u21d2 J) \u228e \u2200 {\u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u03c8 \u22a7 K \u2192 \u03c8 \u22a7 J\n\\end{code}\n\n\\noindent Let \\AgdaBound{V}, \\AgdaBound{W} range over values. Let\n\\AgdaBound{F}, \\AgdaBound{G} range over meta-level (Agda)\nfunctions. The definition \\AgdaFunction{$\\models$} is a Kripke Logical\nPredicate. It is also a so-called large elimination, as it is a\nfunction which returns a new type (a \\AgdaDatatype{Set} in Agda\nterminology). This definition is inspired by Allais et al.\n\\cite{allias.mcbride.boutillier:neutral}. Their normalisation proof,\nwhich we also took inspiration from, is, in turn, based on the work of\nC. Coquand \\cite{c.coquand}. The coproduct at the function kind is\npresent in McBride \\cite{mcbride:data}. Our motivation for following\nthese three approaches was to be careful not to perturb neutral terms\nwhere possible as we want to use our normaliser in substitution and we\nwant the identity substitution for example not to modify variables. We\nalso learned from \\cite{allias.mcbride.boutillier:neutral} how to move\nthe uniformity condition out of the definition of values into the\ncompleteness relation.\n\nWe will define an evaluator to interpret syntactic types into this\nsemantic domain but first we need to explain how to reify from\nsemantics to normal forms. This is needed first as, at base type, our\nsemantic values are normal forms, so we need a way to convert from\nvalues to normal forms during evaluation. Note that usual NBE\noperations of \\AgdaFunction{reify} and \\AgdaFunction{reflect} are not\nmutually defined here as they commonly are in $\\beta\\eta$-NBE. This is\na characteristic of the coproduct style definition above.\n\nReflection takes a neutral type and embeds it into a semantic\ntype. How we do this depends on what kind we are at. At base kind\n\\AgdaInductiveConstructor{*}, semantic values are normal forms, so we\nembed our neutral term using the \\AgdaInductiveConstructor{ne}\nconstructor. At function kind, semantic values are a coproduct of\neither a neutral term or a function, so we embed our neutral term\nusing the \\AgdaInductiveConstructor{inl} constructor.\n\n\\begin{code}\nreflect : \u2200{K \u03d5} \u2192 \u03d5 \u22a2Ne\u22c6 K \u2192 \u03d5 \u22a7 K\nreflect {*} A = ne A\nreflect {K \u21d2 J} A = inl A\n\\end{code}\n\n\\noindent Reification is the process of converting from a semantic\ntype to a normal syntactic type. At base kind and for neutral\nfunctions it is trivial, either we already have a normal form or we\nhave a neutral term which can be embedded. The last line, where we\nhave a semantic function is where the action happens. We create a\nfresh variable of kind \\AgdaBound{K} using \\AgdaFunction{reflect} and\napply \\AgdaBound{f} to it making use of the Kripke function space by\nsupplying \\AgdaBound{f} with the weakening renaming\n\\AgdaInductiveConstructor{S}. This creates a semantic value of kind\n\\AgdaBound{J} in context \\AgdaBound{$\\Phi$}\n\\AgdaInductiveConstructor{,} \\AgdaBound{K} which we can call\n\\AgdaFunction{reify} recursively on. This, in turn, gives us a normal\nform in \\AgdaBound{$\\Phi$} \\AgdaInductiveConstructor{,} \\AgdaBound{K}\n\\AgdaDatatype{\u22a2Nf\u22c6} \\AgdaBound{J}. We can then wrap this normal form\nin a \\AgdaInductiveConstructor{$\\lambdabar$}.\n\n\\begin{code}\nreify : \u2200 {K \u03d5} \u2192 \u03d5 \u22a7 K \u2192 \u03d5 \u22a2Nf\u22c6 K\nreify {*} A = A\nreify {K \u21d2 J} (inl A) = ne A\nreify {K \u21d2 J} (inr F) = \u019b (reify (F S (reflect (` Z))))\n\\end{code}\n\n\\noindent We define renaming for semantic values. In the semantic\nfunction case, the new renaming is composed with the existing one.\n\n\\begin{code}\nren\u22a7 : \u2200 {\u03c3 \u03d5 \u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u03d5 \u22a7 \u03c3 \u2192 \u03c8 \u22a7 \u03c3\nren\u22a7 {*} \u03c1 A = renNf\u22c6 \u03c1 A\nren\u22a7 {K \u21d2 J} \u03c1 (inl A) = inl (renNe\u22c6 \u03c1 A)\nren\u22a7 {K \u21d2 J} \u03c1 (inr F) = inr (\u03bb \u03c1' \u2192 F (\u03c1' \u2218 \u03c1))\n\\end{code}\n\n\\noindent Weakening for semantic values is a special case of renaming:\n\n\\begin{code}\nweaken\u22a7 : \u2200 {\u03c3 \u03d5 K} \u2192 \u03d5 \u22a7 \u03c3 \u2192 (\u03d5 ,\u22c6 K) \u22a7 \u03c3\nweaken\u22a7 = ren\u22a7 S\n\\end{code}\n\n\\noindent Our evaluator will take an environment giving semantic\nvalues to syntactic variables, which we represent as a function from\nvariables to values:\n\n\\begin{code}\nEnv : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nEnv \u03c8 \u03d5 = \u2200{J} \u2192 \u03c8 \u220b\u22c6 J \u2192 \u03d5 \u22a7 J\n\\end{code}\n\n\\noindent Let \\AgdaBound{\u03b7}, \\AgdaBound{\u03b7'} range over\nenvironments.\n\nIt is convenient to extend an environment with an additional semantic\ntype:\n\n\\begin{code}\nextende : \u2200{\u03c8 \u03d5} \u2192 (\u03b7 : Env \u03d5 \u03c8) \u2192 \u2200{K}(A : \u03c8 \u22a7 K) \u2192 Env (\u03d5 ,\u22c6 K) \u03c8\nextende \u03b7 V Z = V\nextende \u03b7 V (S \u03b1) = \u03b7 \u03b1\n\\end{code}\n\n\\noindent Lifting of environments to push them under binders can be\ndefined as follows. One could also define it analogously to the\nlifting of renamings and substitutions defined in\n\\cref{sec:intrinsically-typed}.\n\n\\begin{code}\nlifte : \u2200 {\u03d5 \u03c8} \u2192 Env \u03d5 \u03c8 \u2192 \u2200 {K} \u2192 Env (\u03d5 ,\u22c6 K) (\u03c8 ,\u22c6 K)\nlifte \u03b7 = extende (weaken\u22a7 \u2218 \u03b7) (reflect (` Z))\n\\end{code}\n\n\\noindent We define a semantic version of application called\n\\AgdaFunction{\u00b7V} which applies semantic functions to semantic\narguments. As semantic values at function kind can either be neutral\nterms or genuine semantic functions we need to pattern match on them\nto see how to apply them. Notice that the identity renaming\n\\AgdaFunction{id} is used in the case of a semantic function. This is\nbecause, as we can read of from the type of \\AgdaFunction{$\\cdot V$},\nthe function and the argument are in the same context.\n\n\\begin{code}\n_\u00b7V_ : \u2200{\u03d5 K J} \u2192 \u03d5 \u22a7 (K \u21d2 J) \u2192 \u03d5 \u22a7 K \u2192 \u03d5 \u22a7 J\ninl A \u00b7V V = reflect (A \u00b7 reify V)\ninr F \u00b7V V = F id V\n\\end{code}\n\n\\noindent\nEvaluation is defined by recursion on types:\n\n\\begin{code}\neval : \u2200{\u03d5 \u03c8 K} \u2192 \u03c8 \u22a2\u22c6 K \u2192 Env \u03c8 \u03d5 \u2192 \u03d5 \u22a7 K\neval (` \u03b1) \u03b7 = \u03b7 \u03b1\neval (\u019b B) \u03b7 = inr \u03bb \u03c1 v \u2192 eval B (extende (ren\u22a7 \u03c1 \u2218 \u03b7) v)\neval (A \u00b7 B) \u03b7 = eval A \u03b7 \u00b7V eval B \u03b7\neval (A \u21d2 B) \u03b7 = reify (eval A \u03b7) \u21d2 reify (eval B \u03b7)\neval (\u03a0 B) \u03b7 = \u03a0 (reify (eval B (lifte \u03b7)))\neval (\u03bc B) \u03b7 = \u03bc (reify (eval B (lifte \u03b7)))\n\\end{code}\n\n\\noindent We can define the identity environment as a function that\nembeds variables into neutral terms with \\AgdaInductiveConstructor{`}\nand then reflects them into values:\n\n\\begin{code}\nidEnv : \u2200 \u03d5 \u2192 Env \u03d5 \u03d5\nidEnv \u03d5 = reflect \u2218 `\n\\end{code}\n\n\\noindent We combine \\AgdaFunction{reify} with \\AgdaFunction{eval} in\nthe identity environment \\AgdaFunction{idEnv} to yield a normalisation\nfunction that takes types in a given context and kind and returns\nnormal forms in the same context and kind:\n\n\\begin{code}\nnf : \u2200{\u03d5 K} \u2192 \u03d5 \u22a2\u22c6 K \u2192 \u03d5 \u22a2Nf\u22c6 K\nnf A = reify (eval A (idEnv _))\n\\end{code}\n\n\\noindent In the next three sections we prove the three correctness\nproperties about this normalisation algorithm: completeness;\nsoundness; and stability.\n\n\\subsection{Completeness of Type Normalisation} \n\nCompleteness states that normalising two $\\beta$-equal types yields\nthe same normal form. This is an important correctness property for\nnormalisation: it ensures that normalisation picks out unique\nrepresentatives for normal forms. In a similar way to how we defined\nthe semantic domain by recursion on kinds, we define a Kripke Logical\nRelation on kinds which is a sort of equality on values. At different\nkinds and for different semantic values it means different things: at\nbase type and for neutral functions it means equality of normal forms;\nfor semantic functions it means that in a new context and given a\nsuitable renaming into that context, we take related arguments to\nrelated results. We also require an additional condition on semantic\nfunctions, which we call uniformity, following Allais et\nal.\\cite{allias.mcbride.boutillier:neutral}. However, our definition\nis, we believe, simpler as uniformity is just a type synonym (rather\nthan being mutually defined with the logical relation) and we do not\nneed to prove any auxiliary lemmas about it throughout the\ncompleteness proof. Uniformity states that if we receive a renaming\nand related arguments in the target context of the renaming, and then\na further renaming, we can apply the function at the same context as\nthe arguments and then rename the result or rename the arguments first\nand then apply the function in the later context.\n\nIt should not be possible that a semantic function can become equal to\na neutral term so we rule out these cases by defining them to be\n\\AgdaDatatype{\u22a5}. This would not be necessary if we were doing\n$\\beta\\eta$-normalisation.\n\n\\begin{code}\nCR : \u2200{\u03d5} K \u2192 \u03d5 \u22a7 K \u2192 \u03d5 \u22a7 K \u2192 Set\nCR * A A' = A \u2261 A'\nCR (K \u21d2 J) (inl A) (inl A') = A \u2261 A'\nCR (K \u21d2 J) (inr F) (inl A') = \u22a5\nCR (K \u21d2 J) (inl A) (inr F') = \u22a5\nCR (K \u21d2 J) (inr F) (inr F') = Unif F \u00d7 Unif F' \u00d7\n \u2200 {\u03c8}(\u03c1 : Ren\u22c6 _ \u03c8){V V' : \u03c8 \u22a7 K} \u2192 CR K V V' \u2192 CR J (F \u03c1 V) (F' \u03c1 V')\n where\n -- Uniformity\n Unif : \u2200{\u03d5 K J} \u2192 (\u2200 {\u03c8} \u2192 Ren\u22c6 \u03d5 \u03c8 \u2192 \u03c8 \u22a7 K \u2192 \u03c8 \u22a7 J) \u2192 Set\n Unif {\u03d5}{K}{J} F = \u2200{\u03c8 \u03c8'}(\u03c1 : Ren\u22c6 \u03d5 \u03c8)(\u03c1' : Ren\u22c6 \u03c8 \u03c8')(V V' : \u03c8 \u22a7 K)\n \u2192 CR K V V' \u2192 CR J (ren\u22a7 \u03c1' (F \u03c1 V)) (F (\u03c1' \u2218 \u03c1) (ren\u22a7 \u03c1' V'))\n\\end{code}\n\n\\noindent The relation \\AgdaFunction{CR} is not an equivalence\nrelation, it is only a partial equivalence relation (PER) as\nreflexivity does not hold. However, as is always the case for PERs\nthere is a limited version of reflexivity for elements that are\nrelated to some other element.\n\n\\begin{code}\nsymCR : \u2200{\u03d5 K}{V V' : \u03d5 \u22a7 K} \u2192 CR K V V' \u2192 CR K V' V\ntransCR : \u2200{\u03d5 K}{V V' V'' : \u03d5 \u22a7 K} \u2192 CR K V V' \u2192 CR K V' V'' \u2192 CR K V V''\nreflCR : \u2200{\u03d5 K}{V V' : \u03d5 \u22a7 K} \u2192 CR K V V' \u2192 CR K V V\n\\end{code}\n\\begin{code}[hide]\nsymCR {K = *} p = sym p\nsymCR {K = K \u21d2 J} {inl A} {inl A'} p = sym p\nsymCR {K = K \u21d2 J} {inl A} {inr F'} ()\nsymCR {K = K \u21d2 J} {inr F} {inl A'} ()\nsymCR {K = K \u21d2 J} {inr F} {inr F'} (p ,, p' ,, p'') =\n p' ,, p ,, (\u03bb \u03c1 q \u2192 symCR (p'' \u03c1 (symCR q)))\n\ntransCR {K = *} p q\n = trans p q \ntransCR {K = K \u21d2 J} {inl A} {inl A'} {inl A''} p q\n = trans p q \ntransCR {K = K \u21d2 J} {inl A} {inl A'} {inr A''} p ()\ntransCR {K = K \u21d2 J} {inl A} {inr F'} () q\ntransCR {K = K \u21d2 J} {inr F} {inl A'} () q\ntransCR {K = K \u21d2 J} {inr F} {inr F'} {inl A''} p ()\ntransCR {K = K \u21d2 J} {inr F} {inr F'} {inr F''} (p ,, p' ,, p'') (q ,, q' ,, q'') = p ,, q' ,, \u03bb \u03c1 r \u2192 transCR (p'' \u03c1 r) (q'' \u03c1 (transCR (symCR r) r))\n\nreflCR p = transCR p (symCR p)\n\\end{code}\n\n\\noindent We think of \\AgdaFunction{CR} as equality of semantic\nvalues. Renaming of semantic values \\AgdaFunction{ren\u22a7} (defined in\nthe section \\ref{sec:type-normalisation}) is a functorial action and\nwe can prove the functor laws. The laws hold up to \\AgdaFunction{CR}\nnot up to propositional equality \\AgdaDatatype{\u2261}:\n\n\\begin{code}\nren\u22a7-id : \u2200{K \u03d5}{V V' : \u03d5 \u22a7 K} \u2192 CR K V V' \u2192 CR K (ren\u22a7 id V) V'\nren\u22a7-comp : \u2200{K \u03d5 \u03c8 \u0398}(\u03c1 : Ren\u22c6 \u03d5 \u03c8)(\u03c1' : Ren\u22c6 \u03c8 \u0398){V V' : \u03d5 \u22a7 K}\n \u2192 CR K V V' \u2192 CR K (ren\u22a7 (\u03c1' \u2218 \u03c1) V) (ren\u22a7 \u03c1' (ren\u22a7 \u03c1 V'))\n\\end{code}\n\\begin{code}[hide]\nren\u22a7-id {*} refl = renNf\u22c6-id _\nren\u22a7-id {K \u21d2 J} {V = inl A} {inl A'} refl = renNe\u22c6-id _\nren\u22a7-id {K \u21d2 J} {V = inl A} {inr F'} ()\nren\u22a7-id {K \u21d2 J} {V = inr F} {inl A'} () \nren\u22a7-id {K \u21d2 J} {V = inr F} {inr F'} p = p\n\nren\u22a7-comp {*} \u03c1 \u03c1' refl =\n renNf\u22c6-comp _\nren\u22a7-comp {K \u21d2 J} \u03c1 \u03c1' {inl A} {inl .A} refl =\n renNe\u22c6-comp _\nren\u22a7-comp {K \u21d2 J} \u03c1 \u03c1' {inl A} {inr F'} ()\nren\u22a7-comp {K \u21d2 J} \u03c1 \u03c1' {inr F} {inl A'} ()\nren\u22a7-comp {K \u21d2 J} \u03c1 \u03c1' {inr F} {inr F'} (p ,, p' ,, p'') =\n (\u03bb \u03c1'' \u03c1''' v \u2192 p (\u03c1'' \u2218 \u03c1' \u2218 \u03c1) \u03c1''' v)\n ,,\n (\u03bb \u03c1'' \u03c1''' v \u2192 p' (\u03c1'' \u2218 \u03c1' \u2218 \u03c1) \u03c1''' v)\n ,,\n \u03bb \u03c1'' q \u2192 p'' (\u03c1'' \u2218 \u03c1' \u2218 \u03c1) q\n\\end{code}\n\n\\noindent The completeness proof follows a similar structure as the\nnormalisation algorithm. We define \\AgdaFunction{reflectCR} and\n\\AgdaFunction{reifyCR} analogously to the \\AgdaFunction{reflect} and\n\\AgdaFunction{reify} of the algorithm.\n\n\\begin{code}\nreflectCR : \u2200{\u03d5 K}{A A' : \u03d5 \u22a2Ne\u22c6 K} \u2192 A \u2261 A' \u2192 CR K (reflect A) (reflect A')\nreifyCR : \u2200{\u03d5 K}{V V' : \u03d5 \u22a7 K} \u2192 CR K V V' \u2192 reify V \u2261 reify V'\n\\end{code}\n\n\\begin{code}[hide]\nreflectCR {K = *} p = cong ne p\nreflectCR {K = K \u21d2 J} p = p\n\nreifyCR {K = * } p = p\nreifyCR {K = K \u21d2 J} {inl A} {inl A'} p = cong ne p\nreifyCR {K = K \u21d2 J} {inl A} {inr F'} () \nreifyCR {K = K \u21d2 J} {inr F} {inl A'} () \nreifyCR {K = K \u21d2 J} {inr F} {inr F'} (p ,, p' ,, p'') =\n cong \u019b (reifyCR (p'' S (reflectCR refl)))\n\\end{code}\n\n\\noindent We define a pointwise partial equivalence for environments\nanalogously to the definition of environments themselves:\n\n\\begin{code}\nEnvCR : \u2200 {\u03d5 \u03c8} \u2192 (\u03b7 \u03b7' : Env \u03d5 \u03c8) \u2192 Set\nEnvCR \u03b7 \u03b7' = \u2200{K}(\u03b1 : _ \u220b\u22c6 K) \u2192 CR K (\u03b7 \u03b1) (\u03b7' \u03b1) \n\\end{code}\n\n\\begin{code}[hide]\nextendCR : \u2200{\u03d5 \u03c8 K}{\u03b7 \u03b7' : Env \u03d5 \u03c8}\n \u2192 EnvCR \u03b7 \u03b7'\n \u2192 {V V' : \u03c8 \u22a7 K}\n \u2192 CR K V V'\n ----------------------------\n \u2192 EnvCR (extende \u03b7 V) (extende \u03b7' V')\nextendCR p q Z = q\nextendCR p q (S \u03b1) = p \u03b1\n\nAppCR : \u2200{\u03d5 K J}\n \u2192 {V V' : \u03d5 \u22a7 (K \u21d2 J)}\n \u2192 CR (K \u21d2 J) V V'\n \u2192 {W W' : \u03d5 \u22a7 K}\n \u2192 CR K W W'\n \u2192 CR J (V \u00b7V W) (V' \u00b7V W')\nAppCR {V = inl A} {inl .A} refl q =\n reflectCR (cong (A \u00b7_) (reifyCR q))\nAppCR {V = inl A} {inr F'} () q\nAppCR {V = inr F} {inl A'} () q\nAppCR {V = inr F} {inr F'} (p ,, p' ,, p'') q = p'' id q\n\\end{code}\n\n%\\noindent We need to be able to push renamings through the\n%\\AgdaFunction{CR} relation.\n\n\\begin{code}[hide]\nrenCR : \u2200{\u03d5 \u03c8 K}{v v' : \u03d5 \u22a7 K}(\u03c1 : Ren\u22c6 \u03d5 \u03c8)\n \u2192 CR K v v' \u2192 CR K (ren\u22a7 \u03c1 v) (ren\u22a7 \u03c1 v')\n\\end{code}\n\\begin{code}[hide]\nrenCR {K = *} \u03c1 p = cong (renNf\u22c6 \u03c1) p\nrenCR {K = K \u21d2 J} {inl A} {inl .A} \u03c1 refl = refl\nrenCR {K = K \u21d2 J} {inl A} {inr F'} \u03c1 ()\nrenCR {K = K \u21d2 J} {inr F} {inl A'} \u03c1 ()\nrenCR {K = K \u21d2 J} {inr F} {inr F'} \u03c1 (p ,, p' ,, p'') =\n (\u03bb \u03c1' \u03c1'' v \u2192 p (\u03c1' \u2218 \u03c1) \u03c1'' v)\n ,,\n (\u03bb \u03c1' \u03c1'' v \u2192 p' (\u03c1' \u2218 \u03c1) \u03c1'' v)\n ,,\n \u03bb \u03c1' q \u2192 p'' (\u03c1' \u2218 \u03c1) q\n\\end{code}\n\n%\\noindent We require various properties about renaming interacting\n%with other operations:\n\n\\begin{code}[hide]\nren\u22a7-reflect : \u2200{K \u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8) A\n \u2192 CR K (ren\u22a7 \u03c1 (reflect A)) (reflect (renNe\u22c6 \u03c1 A))\nren-reify : \u2200{K \u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8){V V'} \u2192 CR K V V'\n \u2192 renNf\u22c6 \u03c1 (reify V) \u2261 reify (ren\u22a7 \u03c1 V')\nren\u22a7\u00b7V : \u2200{K J \u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8){F F'}{V V'} \u2192 CR (K \u21d2 J) F F'\n \u2192 CR K V V' \u2192 CR J (ren\u22a7 \u03c1 (F \u00b7V V)) (ren\u22a7 \u03c1 F' \u00b7V ren\u22a7 \u03c1 V')\n\\end{code}\n\\begin{code}[hide]\nren\u22a7-reflect {*} \u03c1 A = refl\nren\u22a7-reflect {K \u21d2 J} \u03c1 A = refl \n\nren-reify {*} \u03c1 refl = refl\nren-reify {K \u21d2 J} \u03c1 {V = inl A} {inl .A} refl = refl\nren-reify {K \u21d2 J} \u03c1 {V = inl A} {inr F'} () \nren-reify {K \u21d2 J} \u03c1 {V = inr F} {inl A'} () \nren-reify {K \u21d2 J} \u03c1 {V = inr F} {inr F'} (p ,, p' ,, p'') = cong \u019b (trans\n (ren-reify (lift\u22c6 \u03c1) (p'' S (reflectCR (refl {x = ` Z}))))\n (reifyCR (transCR\n (p' S (lift\u22c6 \u03c1) _ _ (reflectCR refl))\n (AppCR\n {V = ren\u22a7 (S \u2218 \u03c1) (inr F')}\n {ren\u22a7 (S \u2218 \u03c1) (inr F')}\n ((\u03bb \u03c1' \u03c1'' v \u2192 p' (\u03c1' \u2218 S \u2218 \u03c1) \u03c1'' v)\n ,,\n (\u03bb \u03c1' \u03c1'' v \u2192 p' (\u03c1' \u2218 S \u2218 \u03c1) \u03c1'' v)\n ,,\n \u03bb \u03c1' q \u2192 (proj\u2082 (proj\u2082 (reflCR (symCR (p ,, p' ,, p''))))\n (\u03c1' \u2218 S \u2218 \u03c1) q))\n (ren\u22a7-reflect (lift\u22c6 \u03c1) (` Z))))))\nren\u22a7\u00b7V {J = *} \u03c1 {inl A} {inl .A} {V}{V'} refl q =\n cong (ne \u2218 (renNe\u22c6 \u03c1 A \u00b7_))\n (trans ( ren-reify \u03c1 (reflCR q)) (reifyCR (renCR \u03c1 q)))\nren\u22a7\u00b7V {J = J \u21d2 K} \u03c1 {inl A} {inl .A} refl q =\n cong (renNe\u22c6 \u03c1 A \u00b7_)\n (trans ( ren-reify \u03c1 (reflCR q)) (reifyCR (renCR \u03c1 q)))\nren\u22a7\u00b7V \u03c1 {inl A} {inr F} () q\nren\u22a7\u00b7V \u03c1 {inr F} {inl A'} () q\nren\u22a7\u00b7V \u03c1 {inr F} {inr F'} (p ,, p' ,, p'') q =\n transCR (p id \u03c1 _ _ q) (p'' \u03c1 (renCR \u03c1 (reflCR (symCR q))))\n\\end{code}\n\n\\noindent Before defining the fundamental theorem of logical relations\nwhich is analogous to \\AgdaFunction{eval} we define an identity\nextension lemma which is used to bootstrap the fundamental theorem. It\nstates that if we evaluate a single term in related environments we\nget related results. Semantic renaming commutes with\n\\AgdaFunction{eval}, and we prove this simultaneously with identity\nextension:\n\n\\begin{code}\nidext : \u2200{\u03d5 \u03c8 K}{\u03b7 \u03b7' : Env \u03d5 \u03c8} \u2192 EnvCR \u03b7 \u03b7' \u2192 (A : \u03d5 \u22a2\u22c6 K)\n \u2192 CR K (eval A \u03b7) (eval A \u03b7')\nren\u22a7-eval : \u2200{\u03d5 \u03c8 \u0398 K}(A : \u03c8 \u22a2\u22c6 K){\u03b7 \u03b7' : Env \u03c8 \u03d5}(p : EnvCR \u03b7 \u03b7')\n \u2192 (\u03c1 : Ren\u22c6 \u03d5 \u0398 ) \u2192 CR K (ren\u22a7 \u03c1 (eval A \u03b7)) (eval A (ren\u22a7 \u03c1 \u2218 \u03b7'))\n\\end{code}\n\\begin{code}[hide]\nidext p (` \u03b1) = p \u03b1\nidext p (\u019b B) =\n (\u03bb \u03c1 \u03c1' V V' q \u2192\n transCR (ren\u22a7-eval B (extendCR (renCR \u03c1 \u2218 reflCR \u2218 p) q) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1 \u03c1' (reflCR (p \u03b1)))})\n B))\n ,,\n (\u03bb \u03c1 \u03c1' V V' q \u2192\n transCR\n (ren\u22a7-eval B (extendCR (renCR \u03c1 \u2218 reflCR \u2218 symCR \u2218 p) q) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1 \u03c1' (reflCR (symCR (p \u03b1))))})\n B)) -- first two terms are identical (except for symCR \u2218 p))\n ,,\n \u03bb \u03c1 q \u2192 idext (extendCR (renCR \u03c1 \u2218 p) q) B\nidext p (A \u00b7 B) = AppCR (idext p A) (idext p B)\nidext p (A \u21d2 B) = cong\u2082 _\u21d2_ (idext p A) (idext p B)\nidext p (\u03a0 A) = cong \u03a0 (idext (extendCR (renCR S \u2218 p) (reflectCR refl)) A)\nidext p (\u03bc A) = cong \u03bc (idext (extendCR (renCR S \u2218 p) refl) A)\n\nren\u22a7-eval (` \u03b1) p \u03c1 = renCR \u03c1 (p \u03b1)\nren\u22a7-eval (\u019b A) {\u03b7}{\u03b7'} p \u03c1 =\n (\u03bb \u03c1' \u03c1'' V V' q \u2192\n transCR (ren\u22a7-eval A (extendCR (renCR (\u03c1' \u2218 \u03c1) \u2218 p) q) \u03c1'')\n (idext (\u03bb { Z \u2192 renCR \u03c1'' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp (\u03c1' \u2218 \u03c1) \u03c1'' (p \u03b1))})\n A))\n ,,\n (\u03bb \u03c1' \u03c1'' V V' q \u2192 transCR\n (ren\u22a7-eval\n A\n (extendCR (renCR \u03c1' \u2218 renCR \u03c1 \u2218 reflCR \u2218 symCR \u2218 p) q) \u03c1'')\n (idext (\u03bb { Z \u2192 renCR \u03c1'' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR\n (ren\u22a7-comp \u03c1' \u03c1'' (renCR \u03c1 (reflCR (symCR (p \u03b1)))))})\n A)) -- again two almost identical terms\n ,,\n \u03bb \u03c1' q \u2192 idext (\u03bb { Z \u2192 q ; (S \u03b1) \u2192 ren\u22a7-comp \u03c1 \u03c1' (p \u03b1) }) A\nren\u22a7-eval (A \u00b7 B) p \u03c1 = transCR\n (ren\u22a7\u00b7V \u03c1 (idext (reflCR \u2218 p) A) (idext (reflCR \u2218 p) B))\n (AppCR (ren\u22a7-eval A p \u03c1) (ren\u22a7-eval B p \u03c1))\nren\u22a7-eval (A \u21d2 B) p \u03c1 =\n cong\u2082 _\u21d2_ (ren\u22a7-eval A p \u03c1) (ren\u22a7-eval B p \u03c1)\nren\u22a7-eval (\u03a0 A) p \u03c1 =\n cong \u03a0 (trans\n (ren\u22a7-eval A\n (extendCR (renCR S \u2218 p) (reflectCR refl))\n (lift\u22c6 \u03c1))\n (idext (\u03bb{ Z \u2192 ren\u22a7-reflect (lift\u22c6 \u03c1) (` Z)\n ; (S \u03b1) \u2192 transCR\n (symCR (ren\u22a7-comp S (lift\u22c6 \u03c1) (reflCR (symCR (p \u03b1)))))\n (ren\u22a7-comp \u03c1 S (reflCR (symCR (p \u03b1))))})\n A))\nren\u22a7-eval (\u03bc A) p \u03c1 =\n cong \u03bc (trans\n (ren\u22a7-eval A\n (extendCR (renCR S \u2218 p) refl)\n (lift\u22c6 \u03c1))\n (idext (\u03bb{ Z \u2192 refl\n ; (S \u03b1) \u2192 transCR\n (symCR (ren\u22a7-comp S (lift\u22c6 \u03c1) (reflCR (symCR (p \u03b1)))))\n (ren\u22a7-comp \u03c1 S (reflCR (symCR (p \u03b1))))})\n A))\n\\end{code}\n\n\\noindent We have proved that semantic renaming commutes with\nevaluation. We also require that syntactic renaming commutes with\nevaluation: that we can either rename before evaluation or evaluate in\na renamed environment:\n\n\\begin{code}\nren-eval : \u2200{\u03d5 \u03c8 \u0398 K}(A : \u0398 \u22a2\u22c6 K){\u03b7 \u03b7' : Env \u03c8 \u03d5}(p : EnvCR \u03b7 \u03b7')(\u03c1 : Ren\u22c6 \u0398 \u03c8)\n \u2192 CR K (eval (ren\u22c6 \u03c1 A) \u03b7) (eval A (\u03b7' \u2218 \u03c1))\n\\end{code}\n\\begin{code}[hide]\nren-eval (` \u03b1) p \u03c1 = p (\u03c1 \u03b1)\nren-eval (\u019b A) p \u03c1 =\n (\u03bb \u03c1' \u03c1'' V V' q \u2192 transCR\n (ren\u22a7-eval (ren\u22c6 (lift\u22c6 \u03c1) A) (extendCR (renCR \u03c1' \u2218 reflCR \u2218 p) q) \u03c1'')\n (idext (\u03bb { Z \u2192 renCR \u03c1'' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1' \u03c1'' (reflCR (p \u03b1)))})\n (ren\u22c6 (lift\u22c6 \u03c1) A)))\n ,,\n (\u03bb \u03c1' \u03c1'' V V' q \u2192 transCR\n (ren\u22a7-eval A (extendCR (renCR \u03c1' \u2218 reflCR \u2218 symCR \u2218 p \u2218 \u03c1) q) \u03c1'')\n (idext (\u03bb { Z \u2192 renCR \u03c1'' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR\n (ren\u22a7-comp \u03c1' \u03c1'' (reflCR (symCR (p (\u03c1 \u03b1)))))})\n A))\n ,,\n \u03bb \u03c1' q \u2192 transCR\n (ren-eval A (extendCR (renCR \u03c1' \u2218 p) q) (lift\u22c6 \u03c1))\n (idext (\u03bb { Z \u2192 reflCR (symCR q)\n ; (S \u03b1) \u2192 renCR \u03c1' (reflCR (symCR (p (\u03c1 \u03b1)))) }) A)\nren-eval (A \u00b7 B) p \u03c1 = AppCR (ren-eval A p \u03c1) (ren-eval B p \u03c1)\nren-eval (A \u21d2 B) p \u03c1 = cong\u2082 _\u21d2_ (ren-eval A p \u03c1) (ren-eval B p \u03c1) \nren-eval (\u03a0 A) p \u03c1 =\n cong \u03a0 (trans (ren-eval\n A\n (extendCR (renCR S \u2218 p)\n (reflectCR (refl {x = ` Z}))) (lift\u22c6 \u03c1))\n (idext (\u03bb{ Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 (renCR S \u2218 reflCR \u2218 symCR \u2218 p) (\u03c1 \u03b1)}) A))\nren-eval (\u03bc A) p \u03c1 =\n cong \u03bc (trans (ren-eval\n A\n (extendCR (renCR S \u2218 p)\n refl) (lift\u22c6 \u03c1))\n (idext (\u03bb{ Z \u2192 refl\n ; (S \u03b1) \u2192 (renCR S \u2218 reflCR \u2218 symCR \u2218 p) (\u03c1 \u03b1)}) A))\n\\end{code}\n\n\\noindent As in our previous renaming lemma we require that we can\neither substitute and then evaluate or, equivalently, evaluate the\nunderlying term in an environment constructed by evaluating everything\nin the substitution. This is the usual \\emph{substitution lemma} from\ndenotational semantics and also one of the laws of an algebra for a\nrelative monad (the other one holds definitionally):\n\n\\begin{code}\nsubst-eval : \u2200{\u03d5 \u03c8 \u0398 K}(A : \u0398 \u22a2\u22c6 K){\u03b7 \u03b7' : Env \u03c8 \u03d5}\n \u2192 (p : EnvCR \u03b7 \u03b7')(\u03c3 : Sub\u22c6 \u0398 \u03c8)\n \u2192 CR K (eval (sub\u22c6 \u03c3 A) \u03b7) (eval A (\u03bb \u03b1 \u2192 eval (\u03c3 \u03b1) \u03b7'))\n\\end{code}\n\\begin{code}[hide]\nsubst-eval (` \u03b1) p \u03c3 = idext p (\u03c3 \u03b1)\nsubst-eval (\u019b A) p \u03c3 =\n (\u03bb \u03c1 \u03c1' v v' q \u2192 transCR\n (ren\u22a7-eval (sub\u22c6 (lifts\u22c6 \u03c3) A) (extendCR (renCR \u03c1 \u2218 reflCR \u2218 p) q) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1 \u03c1' (reflCR (p \u03b1)))})\n (sub\u22c6 (lifts\u22c6 \u03c3) A)))\n ,,\n (\u03bb \u03c1 \u03c1' v v' q \u2192 transCR\n (ren\u22a7-eval A (extendCR (renCR \u03c1 \u2218 idext (reflCR \u2218 symCR \u2218 p) \u2218 \u03c3) q) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR q))\n ; (S \u03b1) \u2192 symCR\n (ren\u22a7-comp \u03c1 \u03c1' (idext (reflCR \u2218 symCR \u2218 p) (\u03c3 \u03b1)))})\n A))\n ,,\n \u03bb \u03c1 q \u2192 transCR (subst-eval A (extendCR (renCR \u03c1 \u2218 p) q) (lifts\u22c6 \u03c3))\n (idext (\u03bb { Z \u2192 reflCR (symCR q)\n ; (S \u03b1) \u2192 transCR\n (ren-eval\n (\u03c3 \u03b1)\n (extendCR (renCR \u03c1 \u2218 reflCR \u2218 symCR \u2218 p) (reflCR (symCR q)))\n S)\n (symCR (ren\u22a7-eval (\u03c3 \u03b1) (reflCR \u2218 symCR \u2218 p) \u03c1))})\n A)\nsubst-eval (A \u00b7 B) p \u03c3 = AppCR (subst-eval A p \u03c3) (subst-eval B p \u03c3)\nsubst-eval (A \u21d2 B) p \u03c3 = cong\u2082 _\u21d2_ (subst-eval A p \u03c3) (subst-eval B p \u03c3)\nsubst-eval (\u03a0 A) p \u03c3 = cong \u03a0 (trans\n (subst-eval A (extendCR (renCR S \u2218 p) (reflectCR (refl {x = ` Z}))) (lifts\u22c6 \u03c3))\n (idext (\u03bb{ Z \u2192 reflectCR (refl {x = ` Z})\n ; (S \u03b1) \u2192 transCR\n (ren-eval\n (\u03c3 \u03b1)\n (extendCR (renCR S \u2218 reflCR \u2218 symCR \u2218 p) (reflectCR refl)) S)\n (symCR (ren\u22a7-eval (\u03c3 \u03b1) (reflCR \u2218 symCR \u2218 p) S)) })\n A))\nsubst-eval (\u03bc A) p \u03c3 = cong \u03bc (trans\n (subst-eval A (extendCR (renCR S \u2218 p) refl) (lifts\u22c6 \u03c3))\n (idext (\u03bb{ Z \u2192 reflectCR {K = *} (refl {x = ` Z})\n ; (S \u03b1) \u2192 transCR\n (ren-eval\n (\u03c3 \u03b1)\n (extendCR (renCR S \u2218 reflCR \u2218 symCR \u2218 p) refl) S)\n (symCR (ren\u22a7-eval (\u03c3 \u03b1) (reflCR \u2218 symCR \u2218 p) S)) })\n A))\n\\end{code}\n\n\\noindent We can now prove the fundamental theorem of logical\nrelations for \\AgdaFunction{CR}. It is defined by recursion on the\n$\\beta$-equality proof:\n\n\\begin{code}\nfund : \u2200{\u03d5 \u03c8 K}{\u03b7 \u03b7' : Env \u03d5 \u03c8}{A A' : \u03d5 \u22a2\u22c6 K}\n \u2192 EnvCR \u03b7 \u03b7' \u2192 A \u2261\u03b2 A' \u2192 CR K (eval A \u03b7) (eval A' \u03b7')\n\\end{code}\n\\begin{code}[hide]\nfund p (refl\u2261\u03b2 A) = idext p A\nfund p (sym\u2261\u03b2 q) = symCR (fund (symCR \u2218 p) q)\nfund p (trans\u2261\u03b2 q r) = transCR (fund (reflCR \u2218 p) q) (fund p r)\nfund p (\u21d2\u2261\u03b2 q r) = cong\u2082 _\u21d2_ (fund p q) (fund p r)\nfund p (\u03a0\u2261\u03b2 q) =\n cong \u03a0 (fund (extendCR (renCR S \u2218 p) (reflectCR refl)) q)\nfund p (\u03bc\u2261\u03b2 q) =\n cong \u03bc (fund (extendCR (renCR S \u2218 p) refl) q)\nfund p (\u019b\u2261\u03b2 {B = B}{B'} q) =\n (\u03bb \u03c1 \u03c1' V V' r \u2192 transCR\n (ren\u22a7-eval B (extendCR (renCR \u03c1 \u2218 reflCR \u2218 p) r) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR r))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1 \u03c1' (reflCR (p \u03b1)))})\n B))\n ,,\n (\u03bb \u03c1 \u03c1' V V' r \u2192 transCR\n (ren\u22a7-eval B' (extendCR (renCR \u03c1 \u2218 reflCR \u2218 symCR \u2218 p) r) \u03c1')\n (idext (\u03bb { Z \u2192 renCR \u03c1' (reflCR (symCR r))\n ; (S \u03b1) \u2192 symCR (ren\u22a7-comp \u03c1 \u03c1' (reflCR (symCR (p \u03b1))))})\n B'))\n ,,\n \u03bb \u03c1 r \u2192 fund (extendCR (renCR \u03c1 \u2218 p) r) q\nfund p (\u00b7\u2261\u03b2 q r) = AppCR (fund p q) (fund p r)\nfund p (\u03b2\u2261\u03b2 B A) =\n transCR (idext (\u03bb { Z \u2192 idext (reflCR \u2218 p) A\n ; (S \u03b1) \u2192 ren\u22a7-id (reflCR (p \u03b1))})\n B)\n (symCR (subst-eval B (symCR \u2218 p) (extend\u22c6 ` A)))\n\\end{code}\n\n\\noindent\nAs for the ordinary identity environment, the proof that the identity\nenvironment is related to itself relies on reflection:\n\n\\begin{code}\nidCR : \u2200{\u03d5} \u2192 EnvCR (idEnv \u03d5) (idEnv \u03d5)\nidCR x = reflectCR refl\n\\end{code}\n\nGiven all these components we can prove the completeness result by\nrunning the fundamental theorem in the identity environment and then\napplying reification. Thus, our normalisation algorithm takes $\\beta$-equal\ntypes to identical normal forms.\n\n\\begin{code}\ncompleteness : \u2200 {K \u03d5} {A B : \u03d5 \u22a2\u22c6 K} \u2192 A \u2261\u03b2 B \u2192 nf A \u2261 nf B\ncompleteness p = reifyCR (fund idCR p)\n\\end{code}\n\n\\noindent Complications due to omitting the $\\eta$-rule and the\nrequirement to avoid extensionality were the main challenges in this\nsection.\n\n\\begin{code}[hide]\n-- misc properties\nevalCRSubst : \u2200{\u03d5 \u03c8 K}{\u03b7 \u03b7' : Env \u03d5 \u03c8}\n \u2192 EnvCR \u03b7 \u03b7'\n \u2192 {t t' : \u03d5 \u22a2\u22c6 K}\n \u2192 t \u2261 t'\n \u2192 CR K (eval t \u03b7) (eval t' \u03b7')\nevalCRSubst p {t = t} refl = idext p t\n\\end{code}\n\n\n\\subsection{Soundness of Type Normalisation}\n\nThe soundness property states that terms are $\\beta$-equal to their\nnormal forms which means that normalisation has preserved the\nmeaning. i.e. that the unique representatives chosen by normalisation\nare actually in the equivalence class.\n\nWe proceed in a similar fashion to the completeness proof by defining a\nlogical relation, \\AgdaFunction{reify}\/\\AgdaFunction{reflect},\nfundamental theorem, identity environment, and then plugging it all\ntogether to get the required result.\n\nTo state the soundness property which relates syntactic types to\nnormal forms we need to convert normal forms back into syntactic\ntypes:\n\n\\begin{code}\nembNf : \u2200{\u0393 K} \u2192 \u0393 \u22a2Nf\u22c6 K \u2192 \u0393 \u22a2\u22c6 K\nembNe : \u2200{\u0393 K} \u2192 \u0393 \u22a2Ne\u22c6 K \u2192 \u0393 \u22a2\u22c6 K\n\\end{code}\n\n%\\noindent\n%We require that embedding commutes with renaming:\n\n\\begin{code}[hide]\nren\u22c6-embNf : \u2200{\u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8){J}(A : \u03d5 \u22a2Nf\u22c6 J)\n \u2192 embNf (renNf\u22c6 \u03c1 A) \u2261 ren\u22c6 \u03c1 (embNf A)\nren-embNe : \u2200{\u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8){J}(A : \u03d5 \u22a2Ne\u22c6 J)\n \u2192 embNe (renNe\u22c6 \u03c1 A) \u2261 ren\u22c6 \u03c1 (embNe A)\n\nembNf (\u019b A) = \u019b (embNf A)\nembNf (ne A) = embNe A\nembNf (A \u21d2 B) = embNf A \u21d2 embNf B\nembNf (\u03a0 A) = \u03a0 (embNf A)\nembNf (\u03bc A) = \u03bc (embNf A)\n\nembNe (` x) = ` x\nembNe (A \u00b7 B) = embNe A \u00b7 embNf B\n\nren\u22c6-embNf \u03c1 (\u019b A) = cong \u019b (ren\u22c6-embNf (lift\u22c6 \u03c1) A)\nren\u22c6-embNf \u03c1 (ne A) = ren-embNe \u03c1 A\nren\u22c6-embNf \u03c1 (A \u21d2 B) = cong\u2082 _\u21d2_ (ren\u22c6-embNf \u03c1 A) (ren\u22c6-embNf \u03c1 B)\nren\u22c6-embNf \u03c1 (\u03a0 A) = cong \u03a0 (ren\u22c6-embNf (lift\u22c6 \u03c1) A)\nren\u22c6-embNf \u03c1 (\u03bc A) = cong \u03bc (ren\u22c6-embNf (lift\u22c6 \u03c1) A)\n\nren-embNe \u03c1 (` x) = refl\nren-embNe \u03c1 (n \u00b7 n') = cong\u2082 _\u00b7_ (ren-embNe \u03c1 n) (ren\u22c6-embNf \u03c1 n')\n\\end{code}\n\n\\noindent The soundness property is a Kripke Logical relation as\nbefore, defined as a \\AgdaDatatype{Set}-valued function by recursion\non kinds. But this time it relates syntactic types and semantic\nvalues. In the first two cases the semantic values are normal or\nneutral forms and we can state the property we require easily. In the\nlast case where we have a semantic function, we would like to state\nthat sound functions take sound arguments to sound results (modulo the\nusual Kripke extension). Indeed, when doing this proof for a version\nof the system with $\\beta\\eta$-equality this was what we needed. Here,\nwe have only $\\beta$-equality for types and we were unable to get the\nproof to go through with the same definition. To solve this problem we\nadded an additional requirement to the semantic function case: we\nrequire that our syntactic type of function kind \\AgdaBound{A} is\n$\\beta$-equal to a $\\lambda$-expression. Note this holds trivially if\nwe have the $\\eta$-rule. \n\n\\begin{code}\nSR : \u2200{\u03d5} K \u2192 \u03d5 \u22a2\u22c6 K \u2192 \u03d5 \u22a7 K \u2192 Set\nSR * A V = A \u2261\u03b2 embNf V\nSR (K \u21d2 J) A (inl A') = A \u2261\u03b2 embNe A'\nSR (K \u21d2 J) A (inr F) = \u03a3 (_ ,\u22c6 K \u22a2\u22c6 J) \u03bb A' \u2192 (A \u2261\u03b2 \u019b A') \u00d7\n \u2200{\u03c8}(\u03c1 : Ren\u22c6 _ \u03c8){B V}\n \u2192 SR K B V \u2192 SR J (ren\u22c6 \u03c1 (\u019b A') \u00b7 B) (ren\u22a7 \u03c1 (inr F) \u00b7V V)\n\\end{code}\n\n\\noindent As before we have a notion of \\AgdaFunction{reify} and\n\\AgdaFunction{reflect} for soundness. Reflect takes soundness results\nabout neutral terms to soundness results about semantic values and\nreify takes soundness results about semantic values to soundness\nresults about normal forms:\n\n\\begin{code}\nreflectSR : \u2200{K \u03d5}{A : \u03d5 \u22a2\u22c6 K}{A' : \u03d5 \u22a2Ne\u22c6 K}\n \u2192 A \u2261\u03b2 embNe A' \u2192 SR K A (reflect A')\nreifySR : \u2200{K \u03d5}{A : \u03d5 \u22a2\u22c6 K}{V : \u03d5\u00a0\u22a7 K}\n \u2192 SR K A V \u2192 A \u2261\u03b2 embNf (reify V)\n\\end{code}\n\\begin{code}[hide]\nreflectSR {*} p = p\nreflectSR {K \u21d2 J} p = p\n\nreifySR {*} p = p\nreifySR {K \u21d2 J} {V = inl A} p = p\nreifySR {K \u21d2 J} {V = inr F} (A' ,, p ,, q) =\n trans\u2261\u03b2 p (subst (\u03bb B \u2192 \u019b B \u2261\u03b2 \u019b (embNf (reify (F S (reflect (` Z))))))\n (trans (sym (sub\u22c6-ren\u22c6 A'))\n (trans (sub\u22c6-cong (\u03bb { Z \u2192 refl\n ; (S x) \u2192 refl}) A')\n (sub\u22c6-id A')))\n (\u019b\u2261\u03b2 (trans\u2261\u03b2 (sym\u2261\u03b2 (\u03b2\u2261\u03b2 _ _))\n (reifySR (q S (reflectSR (refl\u2261\u03b2 (` Z))))))))\n\\end{code}\n\n\\noindent We need a notion of environment for soundness, which will be\nused in the fundamental theorem. Here it is a lifting of the relation\n\\AgdaFunction{SR} which relates syntactic types to semantic values to\na relation which relates type substitutions to type environments:\n\n\\begin{code}\nSREnv : \u2200{\u03d5 \u03c8} \u2192 Sub\u22c6 \u03d5 \u03c8 \u2192 Env \u03d5 \u03c8 \u2192 Set\nSREnv {\u03d5} \u03c3 \u03b7 = \u2200{K}(\u03b1 : \u03d5 \u220b\u22c6 K) \u2192 SR K (\u03c3 \u03b1) (\u03b7 \u03b1)\n\\end{code}\n\\begin{code}[hide]\nSR,,\u22c6 : \u2200{\u03d5 \u03c8}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03b7 : Env \u03d5 \u03c8}\n \u2192 SREnv \u03c3 \u03b7\n \u2192 \u2200{K}{A : \u03c8 \u22a2\u22c6 K}{V : \u03c8 \u22a7 K}\n \u2192 SR K A V\n \u2192 SREnv (extend\u22c6 \u03c3 A) (extende \u03b7 V)\nSR,,\u22c6 p q Z = q\n\\end{code}\n\n\\begin{code}[hide]\nSR,,\u22c6 p q (S \u03b1) = p \u03b1\nrenSR : \u2200{\u03d5 \u03c8}(\u03c1 : Ren\u22c6 \u03d5 \u03c8){K}{A : \u03d5 \u22a2\u22c6 K}{V : \u03d5 \u22a7 K}\n \u2192 SR K A V \u2192 SR K (ren\u22c6 \u03c1 A) (ren\u22a7 \u03c1 V)\nrenSR \u03c1 {*}{A}{A'} p = \n subst (ren\u22c6 \u03c1 A \u2261\u03b2_) (sym (ren\u22c6-embNf \u03c1 A')) (ren\u22c6\u2261\u03b2 \u03c1 p)\nrenSR \u03c1 {K \u21d2 J} {A} {inl A'} p rewrite ren-embNe \u03c1 A' = ren\u22c6\u2261\u03b2 \u03c1 p \nrenSR \u03c1 {K \u21d2 J} {A} {inr F} (A' ,, p ,, q) =\n ren\u22c6 (lift\u22c6 \u03c1) A'\n ,,\n ren\u22c6\u2261\u03b2 \u03c1 p\n ,,\n \u03bb \u03c1' {B}{V} r \u2192 subst (\u03bb A \u2192 SR J (\u019b A \u00b7 B) (F (\u03c1' \u2218 \u03c1) V))\n (trans (ren\u22c6-cong lift\u22c6-comp A') (ren\u22c6-comp A'))\n (q (\u03c1' \u2218 \u03c1) r)\n\\end{code}\n\n\\begin{code}[hide]\nlifts-extend\u22c6 : \u2200{\u03d5 \u03c8 K J}\n \u2192 (\u03c3 : Sub\u22c6 \u03d5 \u03c8)\n \u2192 (\u03b1 : \u03d5 ,\u22c6 J \u220b\u22c6 K)\n \u2192 lifts\u22c6 \u03c3 \u03b1\u00a0\u2261 extend\u22c6 (weaken\u22c6 \u2218 \u03c3) (` Z) \u03b1\nlifts-extend\u22c6 \u03c3 Z = refl\nlifts-extend\u22c6 \u03c3 (S _) = refl\n\nsubstSREnv : \u2200{\u03d5 \u03c8}{\u03c3 \u03c3' : Sub\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200{K}(\u03b1 : \u03d5 \u220b\u22c6 K) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 {\u03b7 : Env \u03d5 \u03c8}\n \u2192 SREnv \u03c3 \u03b7\n -------------------------------\n \u2192 SREnv \u03c3' \u03b7\nsubstSREnv p q \u03b1 rewrite sym (p \u03b1) = q \u03b1\n\nSRweak : \u2200{\u03d5 \u03c8}{\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03b7 : Env \u03d5 \u03c8}\n \u2192 SREnv \u03c3 \u03b7\n \u2192 \u2200 {K}\n -------------------------------------------------------\n \u2192 SREnv (lifts\u22c6 \u03c3) (extende (ren\u22a7 S \u2218 \u03b7) (reflect {K} (` Z)))\nSRweak p = substSREnv (sym \u2218 lifts-extend\u22c6 _)\n (SR,,\u22c6 (renSR S \u2218 p) (reflectSR (refl\u2261\u03b2 (` Z)))) \n\nsubstSR : \u2200{K \u03d5}{A A' : \u03d5 \u22a2\u22c6 K}\n \u2192 A' \u2261\u03b2 A\n \u2192 {V : \u03d5 \u22a7 K}\n \u2192 SR K A V\n ---------------------------\n \u2192 SR K A' V\nsubstSR {*} p q = trans\u2261\u03b2 p q\nsubstSR {K \u21d2 J} p {inl A} q = trans\u2261\u03b2 p q\nsubstSR {K \u21d2 J} p {inr F} (A' ,, q ,, r) = _ ,, trans\u2261\u03b2 p q ,, r\n\nSRApp : \u2200{\u03d5 K J}\n \u2192 {A : \u03d5 \u22a2\u22c6 (K \u21d2 J)}\n \u2192 {V : \u03d5 \u22a7 (K \u21d2 J)}\n \u2192 SR (K \u21d2 J) A V\n \u2192 {B : \u03d5 \u22a2\u22c6 K}\n \u2192 {W : \u03d5 \u22a7 K}\n \u2192 SR K B W\n ---------------------\n \u2192 SR J (A \u00b7 B) (V \u00b7V W)\nSRApp {V = inl A} p q = reflectSR (\u00b7\u2261\u03b2 (reflectSR p) (reifySR q))\nSRApp {V = inr F} (A' ,, p ,, q) r =\n substSR (\u00b7\u2261\u03b2 (subst\n (\u03bb B \u2192 _ \u2261\u03b2 \u019b B)\n (trans (sym (ren\u22c6-id A')) (ren\u22c6-cong (sym \u2218 lift\u22c6-id) A'))\n p)\n (refl\u2261\u03b2 _))\n (q id r)\n\\end{code}\n\n\\noindent The fundamental Theorem of Logical Relations for\n\\AgdaFunction{SR} states that, for any type, if we have a related\nsubstitution and environment then the action of the substitution and\nenvironment on the type will also be related.\n\n\\begin{code}\nevalSR : \u2200{\u03d5 \u03c8 K}(A : \u03d5 \u22a2\u22c6 K){\u03c3 : Sub\u22c6 \u03d5 \u03c8}{\u03b7 : Env \u03d5 \u03c8}\n \u2192 SREnv \u03c3 \u03b7 \u2192 SR K (sub\u22c6 \u03c3 A) (eval A \u03b7)\n\\end{code}\n\\begin{code}[hide]\nevalSR (` \u03b1) p = p \u03b1\nevalSR (\u03a0 B) p = \u03a0\u2261\u03b2 (evalSR B (SRweak p))\nevalSR (\u03bc B) p = \u03bc\u2261\u03b2 (evalSR B (SRweak p))\nevalSR (A \u21d2 B) p = \u21d2\u2261\u03b2 (evalSR A p) (evalSR B p)\nevalSR (\u019b A) {\u03c3}{\u03b7} p =\n sub\u22c6 (lifts\u22c6 \u03c3) A\n ,,\n refl\u2261\u03b2 _\n ,,\n \u03bb \u03c1 {B}{V} q \u2192 substSR\n (\u03b2\u2261\u03b2 _ _)\n (subst (\u03bb A' \u2192 SR _ A' (eval A (extende (ren\u22a7 \u03c1 \u2218 \u03b7) V)))\n (trans (trans (sub\u22c6-cong (\u03bb\n { Z \u2192 refl\n ; (S \u03b1) \u2192 trans (trans (sym (sub\u22c6-id (ren\u22c6 \u03c1 (\u03c3 \u03b1))))\n (sym (sub\u22c6-ren\u22c6 (\u03c3 \u03b1))))\n (sub\u22c6-ren\u22c6 (\u03c3 \u03b1))}) A)\n (sub\u22c6-comp A))\n (sub\u22c6-ren\u22c6 (sub\u22c6 (lifts\u22c6 \u03c3) A)))\n (evalSR A (SR,,\u22c6 (renSR \u03c1 \u2218 p) q)) )\nevalSR (A \u00b7 B) p = SRApp (evalSR A p) (evalSR B p)\n\\end{code}\n\n\\noindent The identity substitution is related to the identity environment:\n\n\\begin{code}\nidSR : \u2200{\u03d5} \u2192 SREnv ` (idEnv \u03d5)\nidSR = reflectSR \u2218 refl\u2261\u03b2 \u2218 `\n\\end{code}\n\n\\noindent Soundness result: all types are $\\beta$-equal to their normal forms.\n\n\\begin{code}\nsoundness : \u2200 {\u03d5 J} \u2192 (A : \u03d5 \u22a2\u22c6 J) \u2192 A \u2261\u03b2 embNf (nf A)\nsoundness A = subst (_\u2261\u03b2 embNf (nf A)) (sub\u22c6-id A) (reifySR (evalSR A idSR))\n\\end{code}\n\nComplications in the definition of \\AgdaFunction{SR} due to omitting\nthe $\\eta$-rule were the biggest challenge in this section.\n\n\\subsection{Stability of Type Normalisation}\n\nThe normalisation algorithm is stable: renormalising a normal form will\nnot change it.\n\nThis property is often omitted from treatments of normalisation. For\nus it is crucial as in the substitution algorithm we define in the\nnext section and in term level definitions we renormalise types.\n\nStability for normal forms is defined mutually with an auxiliary\nproperty for neutral types:\n\n\\begin{code}\nstability : \u2200{K \u03d5}(A : \u03d5 \u22a2Nf\u22c6 K) \u2192 nf (embNf A) \u2261 A\nstabilityNe : \u2200{K \u03d5}(A : \u03d5 \u22a2Ne\u22c6 K) \u2192 eval (embNe A) (idEnv \u03d5) \u2261 reflect A\n\\end{code}\n\n\\noindent We omit the proofs which are a simple simultaneous induction\non normal forms and neutral terms. The most challenging part for us\nwas getting the right statement of the stability property for neutral\nterms.\n\n\\begin{code}[hide]\nstability (\u03a0 B) =\n cong \u03a0 (trans (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 ren\u22a7-reflect S (` \u03b1)})\n (embNf B))\n (stability B))\nstability (\u03bc B) =\n cong \u03bc (trans (idext (\u03bb { Z \u2192 refl\n ; (S \u03b1) \u2192 ren\u22a7-reflect S (` \u03b1)})\n (embNf B))\n (stability B))\nstability (A \u21d2 B) = cong\u2082 _\u21d2_ (stability A) (stability B)\nstability (\u019b B) =\n cong \u019b (trans (reifyCR (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 ren\u22a7-reflect S (` \u03b1)})\n (embNf B)))\n (stability B))\nstability {*} (ne A) = stabilityNe A\nstability {K \u21d2 J} (ne A) = cong reify (stabilityNe A)\n\nstabilityNe (` \u03b1) = refl\nstabilityNe (n \u00b7 n') =\n trans (cong (_\u00b7V (eval (embNf n') (idEnv _))) (stabilityNe n))\n (cong (\u03bb n'' \u2192 reflect (n \u00b7 n'')) (stability n'))\n\\end{code}\n\nStability is quite a strong property. It guarantees both that\n\\AgdaFunction{embNf \u2218 nf} is idempotent and that \\AgdaFunction{nf} is\nsurjective:\n\n\\begin{code}\nidempotent : \u2200{\u03d5 K}(A : \u03d5 \u22a2\u22c6 K)\n \u2192 (embNf \u2218 nf \u2218 embNf \u2218 nf) A \u2261 (embNf \u2218 nf) A\nidempotent A = cong embNf (stability (nf A))\n\nsurjective : \u2200{\u03d5 K}(A : \u03d5 \u22a2Nf\u22c6 K) \u2192 \u03a3 (\u03d5 \u22a2\u22c6 K) \u03bb B \u2192 nf B \u2261 A\nsurjective A = embNf A ,, stability A\n\\end{code}\n\n\\noindent Note we use double comma \\AgdaInductiveConstructor{,,} for\nAgda pairs as we used single comma for contexts.\n\n\\subsection{Normality preserving Type Substitution}\n\\label{sec:normal-substitution}\n\nIn the previous subsections we defined a normaliser. In this\nsubsection we will combine the normaliser with our syntactic\nsubstitution operation on types to yield a normality preserving\nsubstitution. This will be used in later sections to define\nintrinsically typed terms with normal types. We proceed by working\nwith similar interface as we did for ordinary substitutions.\n\nNormality preserving substitutions are functions from type variables\nto normal forms:\n\n\\begin{code}\nSubNf\u22c6 : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nSubNf\u22c6 \u03d5 \u03c8 = \u2200 {J} \u2192 \u03d5 \u220b\u22c6 J \u2192 \u03c8 \u22a2Nf\u22c6 J\n\\end{code}\n\n\\noindent We can lift a substitution over a new bound variable as\nbefore. This is needed for going under binders.\n\n\\begin{code}\nliftsNf\u22c6 : \u2200 {\u03d5 \u03c8}\u2192 SubNf\u22c6 \u03d5 \u03c8 \u2192 \u2200{K} \u2192 SubNf\u22c6 (\u03d5 ,\u22c6 K) (\u03c8 ,\u22c6 K)\nliftsNf\u22c6 \u03c3 Z = ne (` Z)\nliftsNf\u22c6 \u03c3 (S \u03b1) = weakenNf\u22c6 (\u03c3 \u03b1)\n\\end{code}\n\n\\noindent We can extend a substitution by an additional normal type\nanalogously to `cons' for lists:\n\n\\begin{code}\nextendNf\u22c6 : \u2200{\u03d5 \u03c8} \u2192 SubNf\u22c6 \u03d5 \u03c8 \u2192 \u2200{J}(A : \u03c8 \u22a2Nf\u22c6 J) \u2192 SubNf\u22c6 (\u03d5 ,\u22c6 J) \u03c8\nextendNf\u22c6 \u03c3 A Z = A\nextendNf\u22c6 \u03c3 A (S \u03b1) = \u03c3 \u03b1\n\\end{code}\n\n\\noindent We define the action of substitutions on normal types as\nfollows: first we embed the normal type to be acted on into a\nsyntactic type, and compose the normalising substitution with\nembedding into syntactic types to turn it into an ordinary\nsubstitution, and then use our syntactic substitution operation from\n\\cref{sec:type-substitution}. This gives us a syntactic type which we\nnormalise using the normalisation algorithm from\n\\cref{sec:type-normalisation}. This is not efficient. It has to\ntraverse the normal type to convert it back to a syntactic type and it\nmay run the normalisation algorithm on things that contain no\nredexes. However as this is a formalisation primarily, efficiency is\nnot a priority, correctness is.\n\n\\begin{code}\nsubNf\u22c6 : \u2200{\u03d5 \u03c8} \u2192 SubNf\u22c6 \u03d5 \u03c8 \u2192 \u2200 {J} \u2192 \u03d5 \u22a2Nf\u22c6 J \u2192 \u03c8 \u22a2Nf\u22c6 J\nsubNf\u22c6 \u03c1 n = nf (sub\u22c6 (embNf \u2218 \u03c1) (embNf n))\n\\end{code}\n\n\\noindent We verify the same correctness properties of normalising\nsubstitution as we did for ordinary substitution: namely the relative\nmonad laws. Note that the second law \\AgdaFunction{subNf\u22c6-\u220b} doesn't\nhold definitionally this time.\n\n\\begin{code}\nsubNf\u22c6-id : \u2200{\u03d5 J}(A : \u03d5 \u22a2Nf\u22c6 J) \u2192 subNf\u22c6 (ne \u2218 `) A \u2261 A\nsubNf\u22c6-var : \u2200{\u03d5 \u03c8 J}(\u03c3 : SubNf\u22c6 \u03d5 \u03c8)(\u03b1 : \u03d5 \u220b\u22c6 J)\n \u2192 subNf\u22c6 \u03c3 (ne (` \u03b1)) \u2261 \u03c3 \u03b1\nsubNf\u22c6-comp : \u2200{\u03d5 \u03c8 \u0398}(\u03c3 : SubNf\u22c6 \u03d5 \u03c8)(\u03c3' : SubNf\u22c6 \u03c8 \u0398){J}(A : \u03d5 \u22a2Nf\u22c6 J)\n \u2192 subNf\u22c6 (subNf\u22c6 \u03c3' \u2218 \u03c3) A \u2261 subNf\u22c6 \u03c3' (subNf\u22c6 \u03c3 A)\n\\end{code}\n\n\\noindent These properties and the definitions that follow rely on\nproperties of normalisation and often corresponding properties of\nordinary substitution. E.g. the first law \\AgdaFunction{subNf\u22c6-id}\nfollows from \\AgdaFunction{stability} and \\AgdaFunction{sub\u22c6-id}, the\nsecond law follows directly from \\AgdaFunction{stability} (the\ncorresponding property holds definitionally in the ordinary case), and\nthe third law follows from \\AgdaFunction{soundness}, various\ncomponents of \\AgdaFunction{completeness} and\n\\AgdaFunction{sub\u22c6-comp}.\n\n\\begin{code}[hide]\n-- 1st relative monad law for subsNf\u22c6\nsubNf\u22c6-id A = trans (cong nf (sub\u22c6-id (embNf A))) (stability A)\n-- 2nd relative monad law for subNf\u22c6\nsubNf\u22c6-var \u03c1 \u03b1 = stability (\u03c1 \u03b1)\n\n-- pulling a nf through a sub\u22c6 and fusing it with another nf\nsubNf\u22c6-nf : \u2200{\u03d5 \u03c8}(\u03c3 : SubNf\u22c6 \u03d5 \u03c8)\n \u2192 \u2200{J} \u2192 (A : \u03d5 \u22a2\u22c6 J) \u2192 nf (sub\u22c6 (embNf \u2218 \u03c3) A) \u2261 subNf\u22c6 \u03c3 (nf A)\nsubNf\u22c6-nf \u03c3 A = trans\n (reifyCR (subst-eval A idCR (embNf \u2218 \u03c3)))\n (trans\n (sym\n (reifyCR (fund (\u03bb \u03b1 \u2192 idext idCR (embNf (\u03c3 \u03b1))) (sym\u2261\u03b2 (soundness A)))))\n (sym (reifyCR (subst-eval (embNf (nf A)) idCR (embNf \u2218 \u03c3)))))\nsubNf\u22c6-comp \u03c3 \u03c3' A = trans\n (trans\n (trans\n (reifyCR\n (subst-eval\n (embNf A)\n idCR\n (embNf \u2218 nf \u2218 sub\u22c6 (embNf \u2218 \u03c3') \u2218 embNf \u2218 \u03c3)))\n (trans (reifyCR\n (idext\n (\u03bb \u03b1 \u2192 fund\n idCR\n (sym\u2261\u03b2 (soundness (sub\u22c6 (embNf \u2218 \u03c3') (embNf (\u03c3 \u03b1))))))\n (embNf A)))\n (sym\n (reifyCR\n (subst-eval\n (embNf A)\n idCR\n (sub\u22c6 (embNf \u2218 \u03c3') \u2218 embNf \u2218 \u03c3))))))\n (cong nf (sub\u22c6-comp (embNf A)))) (subNf\u22c6-nf \u03c3' (sub\u22c6 (embNf \u2218 \u03c3) (embNf A)))\n\\end{code}\n\\begin{code}[hide]\n-- renNf\u22c6 commutes with subNf\u22c6\nrenNf\u22c6-subNf\u22c6 : \u2200{\u03d5 \u03c8 \u0398}(\u03c3 : SubNf\u22c6 \u03d5 \u03c8)(\u03c1 : Ren\u22c6 \u03c8 \u0398){J}(A : \u03d5 \u22a2Nf\u22c6 J)\n \u2192 subNf\u22c6 (renNf\u22c6 \u03c1 \u2218 \u03c3) A \u2261 renNf\u22c6 \u03c1 (subNf\u22c6 \u03c3 A)\nrenNf\u22c6-subNf\u22c6 \u03c3 \u03c1 A = trans\n (reifyCR\n (transCR\n (transCR\n (subst-eval (embNf A) idCR (embNf \u2218 renNf\u22c6 \u03c1 \u2218 \u03c3))\n (transCR\n (idext\n (\u03bb \u03b1 \u2192 transCR\n (evalCRSubst idCR (ren\u22c6-embNf \u03c1 (\u03c3 \u03b1)))\n (transCR\n (ren-eval (embNf (\u03c3 \u03b1)) idCR \u03c1)\n (idext (symCR \u2218 ren\u22a7-reflect \u03c1 \u2218 `) (embNf (\u03c3 \u03b1)))))\n (embNf A))\n (symCR (subst-eval (embNf A) (renCR \u03c1 \u2218 idCR) (embNf \u2218 \u03c3)))))\n (symCR (ren\u22a7-eval (sub\u22c6 (embNf \u2218 \u03c3) (embNf A)) idCR \u03c1))))\n (sym (ren-reify \u03c1 (idext idCR (sub\u22c6 (embNf \u2218 \u03c3) (embNf A)))))\n\\end{code}\n\n\\noindent Finally, we define the special case for single type variable\nsubstitution that will be needed in the definition of terms in the\nnext section:\n\n\\begin{code}\n_[_]Nf\u22c6 : \u2200{\u03d5 J K} \u2192 \u03d5 ,\u22c6 K \u22a2Nf\u22c6 J \u2192 \u03d5 \u22a2Nf\u22c6 K \u2192 \u03d5 \u22a2Nf\u22c6 J\nA [ B ]Nf\u22c6 = subNf\u22c6 (extendNf\u22c6 (ne \u2218 `) B) A\n\\end{code}\n\n\\noindent The development in this section was straightforward. The\nmost significant hurdle was that we require a complete normalisation\nproof and correctness properties of ordinary substitution to prove\ncorrectness properties of substitution on normal forms. The\nsubstitution algorithm in this section is essentially a rather\nindirect implementation of hereditary substitution.\n\nBefore moving on we list special case auxiliary lemmas that we will need when\ndefining renaming and substitution for terms with normal types in\nsection \\ref{sec:operational-semantics}.\n\n\\begin{code}\nren[]Nf\u22c6 : \u2200{\u03d5 \u0398 J K}(\u03c1 : Ren\u22c6 \u03d5 \u0398)(A : \u03d5 ,\u22c6 K \u22a2Nf\u22c6 J) (B : \u03d5 \u22a2Nf\u22c6 K )\n \u2192 renNf\u22c6 \u03c1 (A [ B ]Nf\u22c6) \u2261 renNf\u22c6 (lift\u22c6 \u03c1) A [ renNf\u22c6 \u03c1 B ]Nf\u22c6\n\nweakenNf\u22c6-subNf\u22c6 : \u2200{\u03a6 \u03a8}(\u03c3 : SubNf\u22c6 \u03a6 \u03a8){K}(A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 weakenNf\u22c6 (subNf\u22c6 \u03c3 A) \u2261 subNf\u22c6 (liftsNf\u22c6 \u03c3 {K = K}) (weakenNf\u22c6 A)\n\nsubNf\u22c6-liftNf\u22c6 : \u2200{\u03a6 \u03a8}(\u03c3 : SubNf\u22c6 \u03a6 \u03a8){K}(B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *)\n \u2192 subNf\u22c6 (liftsNf\u22c6 \u03c3) B\n \u2261\n eval (sub\u22c6 (lifts\u22c6 (embNf \u2218 \u03c3)) (embNf B)) (lifte (idEnv \u03a8))\n\nsubNf\u22c6-[]Nf\u22c6 : \u2200{\u03d5 \u03a8 K}(\u03c3 : SubNf\u22c6 \u03d5 \u03a8)(A : \u03d5 \u22a2Nf\u22c6 K)(B : \u03d5 ,\u22c6 K \u22a2Nf\u22c6 *)\n \u2192 subNf\u22c6 \u03c3 (B [ A ]Nf\u22c6)\n \u2261\n eval (sub\u22c6 (lifts\u22c6 (embNf \u2218 \u03c3)) (embNf B)) (lifte (idEnv \u03a8))\n [ subNf\u22c6 \u03c3 A ]Nf\u22c6\n\\end{code}\n\n\\begin{code}[hide]\n-- Misc properties about subNf\n\n-- congruence for subNf\u22c6 \nsubNf\u22c6-cong : \u2200 {\u03d5 \u03c8}\n \u2192 {\u03c3 \u03c3' : SubNf\u22c6 \u03d5 \u03c8}\n \u2192 (\u2200 {J}(\u03b1 : \u03d5 \u220b\u22c6 J) \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 \u2200{K}(A : \u03d5 \u22a2Nf\u22c6 K)\n -------------------------------\n \u2192 subNf\u22c6 \u03c3 A \u2261 subNf\u22c6 \u03c3' A\nsubNf\u22c6-cong p A =\n reifyCR (evalCRSubst idCR (sub\u22c6-cong (cong embNf \u2218 p) (embNf A)))\n\nweakenNf\u22c6-renNf\u22c6 : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 \u2200{K}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 weakenNf\u22c6 (renNf\u22c6 \u03c1\u22c6 A) \u2261 renNf\u22c6 (lift\u22c6 \u03c1\u22c6 {K = K}) (weakenNf\u22c6 A)\nweakenNf\u22c6-renNf\u22c6 \u03c1\u22c6 A = trans (sym (renNf\u22c6-comp _)) (renNf\u22c6-comp _)\n\n-- subNf\u22c6 commutes with renNf\u22c6\nsubNf\u22c6-renNf\u22c6 : \u2200{\u03d5 \u03c8 \u0398}\n \u2192 (\u03c1 : Ren\u22c6 \u03d5 \u03c8)\n \u2192 (\u03c3 : SubNf\u22c6 \u03c8 \u0398)\n \u2192 \u2200{J}(A : \u03d5 \u22a2Nf\u22c6 J)\n --------------------------------------\n \u2192 subNf\u22c6 (\u03c3 \u2218 \u03c1) A \u2261 subNf\u22c6 \u03c3 (renNf\u22c6 \u03c1 A)\nsubNf\u22c6-renNf\u22c6 \u03c1 \u03c3 A = reifyCR\n (evalCRSubst\n idCR\n (trans (sub\u22c6-ren\u22c6 (embNf A))\n (cong (sub\u22c6 (embNf \u2218 \u03c3)) (sym (ren\u22c6-embNf \u03c1 A)))))\n\nweakenNf\u22c6-subNf\u22c6 \u03c3\u22c6 A = trans\n (sym (renNf\u22c6-subNf\u22c6 \u03c3\u22c6 S A))\n (subNf\u22c6-renNf\u22c6 S (liftsNf\u22c6 \u03c3\u22c6) A)\n\n-- subNf\u22c6 commutes with [_]Nf\u22c6\nsubNf\u22c6-[]Nf\u22c6' : \u2200{\u03d5 \u03a8 K J}\n \u2192 (\u03c3 : SubNf\u22c6 \u03d5 \u03a8)\n \u2192 (A : \u03d5 \u22a2Nf\u22c6 K)\n \u2192 (B : \u03d5 ,\u22c6 K \u22a2Nf\u22c6 J)\n --------------------------------------------------------------\n \u2192 subNf\u22c6 \u03c3 (B [ A ]Nf\u22c6) \u2261 subNf\u22c6 (liftsNf\u22c6 \u03c3) B [ subNf\u22c6 \u03c3 A ]Nf\u22c6\nsubNf\u22c6-[]Nf\u22c6' \u03c3 A B = trans\n (sym (subNf\u22c6-comp (extendNf\u22c6 (ne \u2218 `) A) \u03c3 B))\n (trans\n (subNf\u22c6-cong\n {\u03c3 = subNf\u22c6 \u03c3 \u2218 extendNf\u22c6 (ne \u2218 `) A}\n {\u03c3' = subNf\u22c6 (extendNf\u22c6 (ne \u2218 `) (subNf\u22c6 \u03c3 A)) \u2218 liftsNf\u22c6 \u03c3}\n (\u03bb { Z \u2192 sym (subNf\u22c6-var (extendNf\u22c6 (ne \u2218 `) (subNf\u22c6 \u03c3 A)) Z) \n ; (S \u03b1) \u2192 trans\n (trans (subNf\u22c6-var \u03c3 \u03b1) (sym (subNf\u22c6-id (\u03c3 \u03b1))))\n (subNf\u22c6-renNf\u22c6\n S\n (extendNf\u22c6 (ne \u2218 `) (subNf\u22c6 \u03c3 A))\n (\u03c3 \u03b1))})\n B)\n (subNf\u22c6-comp (liftsNf\u22c6 \u03c3) (extendNf\u22c6 (ne \u2218 `) (subNf\u22c6 \u03c3 A)) B))\n\n-- embNf\u22c6 commutes with lift\nsubNf-lemma : \u2200{\u03d5 \u03a8 K J}\n \u2192 (\u03c3 : SubNf\u22c6 \u03d5 \u03a8)\n \u2192 (A : \u03d5 ,\u22c6 K \u22a2\u22c6 J)\n \u2192 sub\u22c6 (lifts\u22c6 (embNf \u2218 \u03c3)) A \u2261 sub\u22c6 (embNf \u2218 liftsNf\u22c6 \u03c3) A\nsubNf-lemma \u03c3 A =\n sub\u22c6-cong (\u03bb { Z \u2192 refl ; (S \u03b1) \u2192 sym (ren\u22c6-embNf S (\u03c3 \u03b1))}) A\n\n-- repairing a mismatch in how the identity can be constructed\nsubNf-lemma' : \u2200{\u03d5 K J}\n \u2192 (A : \u03d5 ,\u22c6 K \u22a2\u22c6 J)\n \u2192 nf A \u2261 reify (eval A (extende (ren\u22a7 S \u2218 idEnv _) (reflect (` Z))))\nsubNf-lemma' A = reifyCR\n (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S \u03b1) \u2192 symCR (ren\u22a7-reflect S (` \u03b1))}) A)\n\n-- combining the two previous lemmas\nsubNf\u22c6-[]Nf\u22c6 \u03c3 A B =\n trans (subNf\u22c6-[]Nf\u22c6' \u03c3 A B)\n (trans (cong (\u03bb B \u2192 nf B [ subNf\u22c6 \u03c3 A ]Nf\u22c6)\n (sym (subNf-lemma \u03c3 (embNf B))))\n (cong (\u03bb B \u2192 B [ subNf\u22c6 \u03c3 A ]Nf\u22c6)\n (subNf-lemma' (sub\u22c6 (lifts\u22c6 (embNf \u2218 \u03c3)) (embNf B)))))\n\nsubNf\u22c6-liftNf\u22c6 \u03c3\u22c6 B = trans\n (evalCRSubst idCR (sym (subNf-lemma \u03c3\u22c6 (embNf B))))\n (subNf-lemma' (sub\u22c6 (lifts\u22c6 (embNf \u2218 \u03c3\u22c6)) (embNf B)))\n\nweakenNf\u22c6[] : \u2200 {\u03a6 K}(B : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 A \u2261 weakenNf\u22c6 A [ B ]Nf\u22c6\nweakenNf\u22c6[] B A = trans\n (trans (sym (stability A))\n (evalCRSubst idCR (sym (sub\u22c6-id (embNf A)))))\n (subNf\u22c6-renNf\u22c6 S (extendNf\u22c6 (ne \u2218 `) B) A)\n\n\n-- renNf\u22c6 commutes with [_]Nf\u22c6\nren[]Nf\u22c6 \u03c1 A B = trans\n (sym (renNf\u22c6-subNf\u22c6 (extendNf\u22c6 (ne \u2218 `) B) \u03c1 A))\n (trans\n (subNf\u22c6-cong\n {\u03c3 = renNf\u22c6 \u03c1 \u2218 extendNf\u22c6 (ne \u2218 `) B}\n {\u03c3' = extendNf\u22c6 (ne \u2218 `) (renNf\u22c6 \u03c1 B) \u2218 lift\u22c6 \u03c1}\n (\u03bb { Z \u2192 refl ; (S \u03b1) \u2192 refl})\n A)\n (subNf\u22c6-renNf\u22c6 (lift\u22c6 \u03c1) (extendNf\u22c6 (ne \u2218 `) (renNf\u22c6 \u03c1 B)) A))\n\\end{code}\n\n\\subsection{Terms with normal types}\n\\label{sec:terms-normal-types}\n\n\\begin{code}[hide]\ninfix 4 _\u220b_\ninfix 4 _\u22a2_\ninfixl 5 _,_\n\\end{code}\n\nWe are now ready to define the algorithmic syntax where terms have\nnormal types and the problematic conversion rule is not needed.\n\nThe definition is largely identical except wherever a syntactic type\nappeared before, we have a normal type, wherever an operation on\nsyntactic types appeared before we have the corresponding operation on\nnormal types. Note that the kind level remains the same, so we reuse\n\\AgdaDatatype{Ctx\u22c6} for example.\n\n\\subsubsection{Term Contexts}\n\nTerm level contexts are indexed by their type level contexts.\n\n\\begin{code}\ndata CtxNf : Ctx\u22c6 \u2192 Set where\n \u2205 : CtxNf \u2205\n _,\u22c6_ : \u2200{\u03a6} \u2192 CtxNf \u03a6 \u2192 \u2200 J \u2192 CtxNf (\u03a6 ,\u22c6 J)\n _,_ : \u2200{\u03a6} \u2192 CtxNf \u03a6 \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 CtxNf \u03a6\n\\end{code}\n\n\\noindent Let \\AgdaBound{\u0393}, \\AgdaBound{\u0394} range over contexts.\n\n\\subsubsection{Term Variables}\n\nNote that in the \\AgdaInductiveConstructor{T} case, we are required to\nweaken (normal) types.\n\n\\begin{code}\ndata _\u220bNf_ : \u2200 {\u03a6} \u2192 CtxNf \u03a6 \u2192 \u03a6 \u22a2Nf\u22c6 * \u2192 Set where\n Z : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 , A \u220bNf A\n S : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}{B : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u220bNf A \u2192 \u0393 , B \u220bNf A\n T : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}{K} \u2192 \u0393 \u220bNf A \u2192 \u0393 ,\u22c6 K \u220bNf weakenNf\u22c6 A\n\\end{code}\n\n\\noindent Let \\AgdaBound{x}, \\AgdaBound{y} range over variables.\n\n\\subsubsection{Terms}\n\nNote the absence of the conversion rule. The types of terms are\nunique so it is not possible to coerce a term into a different type.\n\n\\begin{code}\ndata _\u22a2Nf_ {\u03a6} \u0393 : \u03a6 \u22a2Nf\u22c6 * \u2192 Set where\n ` : \u2200{A} \u2192 \u0393 \u220bNf A \u2192 \u0393 \u22a2Nf A\n \u019b : \u2200{A B} \u2192 \u0393 , A \u22a2Nf B \u2192 \u0393 \u22a2Nf A \u21d2 B\n _\u00b7_ : \u2200{A B} \u2192 \u0393 \u22a2Nf A \u21d2 B \u2192 \u0393 \u22a2Nf A \u2192 \u0393 \u22a2Nf B\n \u039b : \u2200{K B} \u2192 \u0393 ,\u22c6 K \u22a2Nf B \u2192 \u0393 \u22a2Nf \u03a0 B\n _\u00b7\u22c6_ : \u2200{K B} \u2192 \u0393 \u22a2Nf \u03a0 B \u2192 (A : \u03a6 \u22a2Nf\u22c6 K) \u2192 \u0393 \u22a2Nf B [ A ]Nf\u22c6\n wrap : \u2200 A \u2192 \u0393 \u22a2Nf A [ \u03bc A ]Nf\u22c6 \u2192 \u0393 \u22a2Nf \u03bc A\n unwrap : \u2200{A} \u2192 \u0393 \u22a2Nf \u03bc A \u2192 \u0393 \u22a2Nf A [ \u03bc A ]Nf\u22c6\n\\end{code}\n\n\\noindent Let \\AgdaBound{L}, \\AgdaBound{M} range over terms.\n\nWe now have an intrinsically typed definition of terms with types that\nare guaranteed to be normal. By side-stepping the conversion problem\nwe can define an operational semantics for this syntax which we will\ndo in section \\ref{sec:operational-semantics}. In the next section we\nwill reflect on the correspondence between this syntax and the syntax\nwith conversion presented in section \\ref{sec:intrinsically-typed}.\n\nWe define two special cases of \\AgdaFunction{subst} which allow us to\nsubstitute the types of variables or terms by propositionally equal\ntypes. While it is the case that types are now represented uniquely we\nstill want or need to to prove that two types are equal, especially in\nthe presence of (Agda) variables, cf., while the natural number 7 has\na unique representation in Agda we still might want to prove that for\nany natural numbers \\AgdaBound{m} and \\AgdaBound{n}, \\AgdaBound{m}\n\\AgdaFunction{+} \\AgdaBound{n} \\AgdaDatatype{\u2261} \\AgdaBound{n}\n\\AgdaFunction{+} \\AgdaBound{m}.\n\n\\begin{code}\nconv\u220bNf : \u2200 {\u03a6 \u0393}{A A' : \u03a6 \u22a2Nf\u22c6 *} \u2192 A \u2261 A' \u2192 (\u0393 \u220bNf A) \u2192 \u0393 \u220bNf A'\nconv\u220bNf refl \u03b1 = \u03b1\n\nconv\u22a2 : \u2200 {\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}{A' : \u03a6 \u22a2\u22c6 *} \u2192 A \u2261 A' \u2192 \u0393 \u22a2 A \u2192 \u0393 \u22a2 A'\nconv\u22a2 refl \u03b1 = \u03b1\n\\end{code}\n\n\\noindent We see these operations in in use in section\n\\ref{sec:operational-semantics}.\n\n\\section{Correspondence between declarative and algorithmic type systems}\n\\label{sec:correspondence}\n\nWe now have two versions of the syntax\/typing rules. Should we just\nthrow away the old one and use the new one? No. The first version is\nthe standard textbook version and the second version is an algorithmic\nversion suitable for implementation. To reconcile the two we prove the\nsecond version is sound and complete with respect to the first. This\nis analogous to proving that a typechecker is sound and complete with\nrespect to the typing rules. Additionally, we prove that before and\nafter normalising the type, terms erase to the same untyped terms. The\nconstructions in this section became significantly simpler and easier\nafter switching from inductive-recursive term contexts to indexed term\ncontexts.\n\nThere is an interesting parallel here with the metatheory of\nTwelf\\footnote{We thank an anonymous reviewer for bringing this to our\nattention.}. In Twelf, hereditary substitution are central to the\nmetatheory and the semantics is defined on a version of the syntax\nwhere both types and terms are canonical (i.e. they are\nnormalised). In our setting only the types are normalised\n(viz. canonical). But, the situation is similar: there are two\nversions of the syntax, one with a semantics (the canonical system),\nand one without (the ordinary system). Martens and Crary\\cite{lfinlf}\nmake the case that the ordinary version is the programmer's interface,\nor the external language in compiler terminology, and the canonical\nversion is the internal language in compiler terminology. In their\nsetting the payoff is also the same: by moving from a language with\ntype equivalence to one where types are uniquely represented, the\nsemantics and metatheory become much simpler.\n\nThere is also a parallel with how type checking algorithms are\ndescribed in the literature: they are often presented an alternative\nset of typing rules and then they are proved sound and complete with\nrespect to the original typing rules. We will draw on this analogy in\nthe rest of this section as our syntaxes are also type systems.\n\n\\subsection{Soundness of Typing}\n\nFrom a typing point of view, soundness states that anything typeable\nin the new type system is also typeable in the old one. From our\nsyntactic point of view this corresponds to taking an algorithmic term\nand embedding it back into a declarative term.\n\nWe have already defined an operation to embed normal types into\nsyntactic types. But, we need an additional operation here: term\ncontexts contain types so we must embed term contexts with normal type\ninto term contexts with syntactic types.\n\n\\begin{code}\nembCtx : \u2200{\u03a6} \u2192 CtxNf \u03a6 \u2192 Ctx \u03a6\nembCtx \u2205 = \u2205\nembCtx (\u0393 ,\u22c6 K) = embCtx \u0393 ,\u22c6 K\nembCtx (\u0393 , A) = embCtx \u0393 , embNf A\n\\end{code}\n\\begin{code}[hide]\nlemT' : \u2200{\u0393 \u0393' J K}(A : \u0393 \u22a2Nf\u22c6 K)\n \u2192 (p : \u0393 \u2261 \u0393')\n \u2192 (q : \u0393 ,\u22c6 J \u2261 \u0393' ,\u22c6 J)\n \u2192 weaken\u22c6 (subst (_\u22a2\u22c6 K) p (embNf A))\n \u2261\n subst (_\u22a2\u22c6 K) q (embNf (renNf\u22c6 S A))\nlemT' A refl refl = sym (ren\u22c6-embNf S A)\n\nconv\u220b : \u2200 {\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}{A' : \u03a6 \u22a2\u22c6 *}\n \u2192 A \u2261 A' \u2192\n (\u0393 \u220b A) \u2192 \u0393 \u220b A'\nconv\u220b refl \u03b1 = \u03b1\n\nembTyVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u0393 \u220bNf A\n \u2192 embCtx \u0393 \u220b embNf A\nembTyVar Z = Z\nembTyVar (S \u03b1) = S (embTyVar \u03b1)\nembTyVar {\u0393 = \u0393 ,\u22c6 K} (T {A = A} \u03b1) =\n conv\u220b (sym (ren\u22c6-embNf S A)) (T (embTyVar \u03b1))\n\nlem-embNf[] : \u2200{\u0393 K}(A : \u0393 \u22a2Nf\u22c6 K)(B : \u0393 ,\u22c6 K \u22a2Nf\u22c6 *) \u2192\n embNf B [ embNf A ]\u22c6 \u2261\u03b2 embNf (B [ A ]Nf\u22c6)\nlem-embNf[] A B = subst\n (embNf B [ embNf A ]\u22c6 \u2261\u03b2_)\n (cong\n embNf\n (trans\n (trans\n (subst-eval (embNf B) idCR (extend\u22c6 ` (embNf A)))\n (idext (\u03bb { Z \u2192 idext idCR (embNf A)\n ; (S \u03b1) \u2192 reflectCR (refl {x = ` \u03b1})}) (embNf B)))\n (sym (subst-eval (embNf B) idCR (embNf \u2218 extendNf\u22c6 (ne \u2218 `) A)))))\n (soundness (embNf B [ embNf A ]\u22c6))\n\nlem-embNf\u03bc : \u2200{\u0393}(A : \u0393 ,\u22c6 * \u22a2Nf\u22c6 *) \u2192 \n embNf (A [ \u03bc A ]Nf\u22c6)\n \u2261\u03b2\n embNf A [ \u03bc (embNf A) ]\u22c6\nlem-embNf\u03bc A = subst\n (_\u2261\u03b2 embNf A [ \u03bc (embNf A) ]\u22c6)\n (cong embNf (trans (subst-eval (embNf A) idCR (extend\u22c6 ` (\u03bc (embNf A)))) (trans (idext (\u03bb { Z \u2192 refl ; (S \u03b1) \u2192 idCR \u03b1}) (embNf A)) (sym (subst-eval (embNf A) idCR (embNf \u2218 (extendNf\u22c6 (ne \u2218 `) (\u03bc A)))))))) (sym\u2261\u03b2 (soundness (embNf A [ \u03bc (embNf A) ]\u22c6)))\n\\end{code}\n\n\\noindent Embedding for terms takes a term with a normal type and\nproduces a term with a syntactic type.\n\\begin{code}\nembTy : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 embCtx \u0393 \u22a2 embNf A\n\\end{code}\n\n\\begin{code}[hide]\nembTy (` \u03b1) = ` (embTyVar \u03b1)\nembTy (\u019b t) = \u019b (embTy t)\nembTy (t \u00b7 u) = embTy t \u00b7 embTy u\nembTy (\u039b t) = \u039b (embTy t)\nembTy (_\u00b7\u22c6_ {B = B} t A) = conv (lem-embNf[] A B) (embTy t \u00b7\u22c6 embNf A)\nembTy (wrap A L) = wrap (embNf A) (conv (lem-embNf\u03bc A) (embTy L))\nembTy (unwrap {A = A} L) = conv (sym\u2261\u03b2 (lem-embNf\u03bc A)) (unwrap (embTy L))\n\\end{code}\n\n\\noindent Soundness of typing is a direct corollary of \\AgdaFunction{embTy}:\n\n\\begin{code}\nsoundnessT : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 embCtx \u0393 \u22a2 embNf A\nsoundnessT = embTy\n\\end{code}\n\n\\noindent Soundness gives us one direction of the correspondence\nbetween systems. The other direction is given by completeness.\n\n\\subsection{Completeness of Typing}\n\nCompleteness of typing states that anything typeable by the original\ndeclarative system is typeable by the new system, i.e. we do not lose\nany well typed programs by moving to the new system. From our\nsyntactic point of view, it states that we can take any declarative\nterm of a given type and normalise its type to produce an algorithmic\nterm with a type that is $\\beta$-equal to the type we started with.\n\nWe have already defined normalisation for types. Again, we must provide\nan operation that normalises a context:\n\n\\begin{code}\nnfCtx : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 CtxNf \u03a6\nnfCtx \u2205 = \u2205\nnfCtx (\u0393 ,\u22c6 K) = nfCtx \u0393 ,\u22c6 K\nnfCtx (\u0393 , A) = nfCtx \u0393 , nf A\n\\end{code}\n\\begin{code}[hide]\n\nren-nf : \u2200{\u03d5 \u03c8 K}(\u03c3 : Ren\u22c6 \u03d5 \u03c8)(A : \u03d5 \u22a2\u22c6 K) \u2192\n renNf\u22c6 \u03c3 (nf A) \u2261 nf (ren\u22c6 \u03c3 A)\nren-nf \u03c3 A = trans\n (ren-reify \u03c3 (idext idCR A))\n (reifyCR\n (transCR\n (ren\u22a7-eval A idCR \u03c3)\n (transCR\n (idext (ren\u22a7-reflect \u03c3 \u2218 `) A)\n (symCR (ren-eval A idCR \u03c3)) )))\n\nnfTyVar : \u2200{\u03a6 \u0393}\n \u2192 {A : \u03a6 \u22a2\u22c6 *}\n \u2192 \u0393 \u220b A\n \u2192 nfCtx \u0393 \u220bNf nf A\nnfTyVar Z = Z\nnfTyVar (S \u03b1) = S (nfTyVar \u03b1)\nnfTyVar {\u0393 ,\u22c6 K} (T {A = A} \u03b1) = conv\u220bNf (ren-nf S A) (T (nfTyVar \u03b1))\n\n-- fixing a mismatch with lift\nsubNf\u22c6-lemma' : \u2200{\u03d5 K J}\n \u2192 (B : \u03d5 ,\u22c6 K \u22a2\u22c6 J)\n \u2192 nf B \u2261 reify (eval B (extende (ren\u22a7 S \u2218 idEnv _) (reflect (` Z))))\nsubNf\u22c6-lemma' B = reifyCR\n (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S x) \u2192 symCR (ren\u22a7-reflect S (` x))}) B)\n\nlem\u03a0nf : \u2200{\u0393 K}(B : \u0393 ,\u22c6 K \u22a2\u22c6 *) \u2192 \u03a0 (nf B) \u2261 nf (\u03a0 B)\nlem\u03a0nf B = cong \u03a0 (subNf\u22c6-lemma' B)\n\nlem\u03bcnf : \u2200{\u0393}(B : \u0393 ,\u22c6 * \u22a2\u22c6 *) \u2192 \u03bc (nf B) \u2261 nf (\u03bc B)\nlem\u03bcnf B = cong \u03bc (subNf\u22c6-lemma' B)\n\nlem-nf\u03bc : \u2200{\u0393}(A : \u0393 ,\u22c6 * \u22a2\u22c6 *) \u2192\n nf (A [ \u03bc A ]\u22c6) \u2261 nf A [ \u03bc (nf A) ]Nf\u22c6\nlem-nf\u03bc A = trans (subst-eval A idCR (extend\u22c6 ` (\u03bc A))) (trans (fund (\u03bb { Z \u2192 cong \u03bc (fund (\u03bb { Z \u2192 refl ; (S \u03b1) \u2192 renCR S (reflectCR refl)}) (soundness A)) ; (S \u03b1) \u2192 idCR \u03b1}) (soundness A)) (sym (subst-eval (embNf (nf A)) idCR (embNf \u2218 extendNf\u22c6 (ne \u2218 `) (\u03bc (nf A))))))\n\n\nlem[] : \u2200{\u03a6 K}(A : \u03a6 \u22a2Nf\u22c6 K)(B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *) \u2192\n B [ A ]Nf\u22c6 \u2261 nf (sub\u22c6 (extend\u22c6 ` (embNf A)) (embNf B))\nlem[] A B = evalCRSubst idCR (sub\u22c6-cong (\u03bb {Z \u2192 refl;(S \u03b1) \u2192 refl}) (embNf B)) \n\nlem[]' : \u2200{\u03a6 K}(A : \u03a6 \u22a2\u22c6 K)(B : \u03a6 ,\u22c6 K \u22a2\u22c6 *) \u2192\n eval B (lifte (idEnv \u03a6)) [ nf A ]Nf\u22c6 \u2261 nf (sub\u22c6 (extend\u22c6 ` A) B)\nlem[]' {\u0393} A B = trans\n (lem[] (nf A) (eval B (lifte (idEnv _))))\n (cong\n (subst (_\u22a2Nf\u22c6 *) refl)\n (trans\n (trans\n (subst-eval\n (embNf (eval B (lifte (idEnv _))))\n idCR\n (extend\u22c6 ` (embNf (nf A))))\n (trans\n (fund\n (\u03bb x \u2192 idext idCR (extend\u22c6 ` (embNf (nf A)) x))\n (sym\u2261\u03b2 (evalSR B (SRweak idSR))))\n (trans\n (subst-eval\n B\n (\u03bb x \u2192 idext idCR (extend\u22c6 ` (embNf (nf A)) x))\n (lifts\u22c6 `))\n (idext (\u03bb { Z \u2192 symCR (fund idCR (soundness A))\n ; (S \u03b1) \u2192 idCR \u03b1}) B))))\n (sym (subst-eval B idCR (extend\u22c6 ` A)))))\n\\end{code}\n\n\\noindent We observe at this point (just before we use it) that\nconversion is derivable for the algorithmic syntax. It computes:\n\n\\begin{code}\nconv\u22a2Nf : \u2200 {\u03a6 \u0393}{A A' : \u03a6 \u22a2Nf\u22c6 *} \u2192 A \u2261 A' \u2192 \u0393 \u22a2Nf A \u2192 \u0393 \u22a2Nf A'\nconv\u22a2Nf refl L = L\n\\end{code}\n\n\\noindent The operation that normalises the types of terms takes a\ndeclarative term and produces an algorithmic term. We omit the\nmajority of the definition, but include the case for a conversion. In\nthis case we have a term \\AgdaBound{t} of type \\AgdaBound{$\\Gamma$}\n\\AgdaDatatype{$\\vdash$} \\AgdaBound{A} and a proof \\AgdaBound{p} that\n\\AgdaBound{A} \\AgdaDatatype{$\\equiv_\\beta$} \\AgdaBound{B}. We require\na term of of type \\AgdaBound{$\\Gamma$} \\AgdaDatatype{\u22a2Nf}\n\\AgdaFunction{nf} \\AgdaBound{B}. By inductive hypothesis\/recursive\ncall \\AgdaFunction{nfType} \\AgdaBound{t} : \\AgdaBound{$\\Gamma$}\n\\AgdaDatatype{\u22a2Nf} \\AgdaFunction{nf} \\AgdaBound{A}. But, via\ncompleteness of normalisation we know that if \\AgdaBound{A}\n\\AgdaDatatype{$\\equiv_\\beta$} \\AgdaBound{B} then \\AgdaFunction{nf}\n\\AgdaBound{B} \\AgdaDatatype{$\\equiv$} \\AgdaFunction{nf} \\AgdaBound{A},\nso we invoke the conversion function \\AgdaFunction{conv\u22a2Nf}\nwith the completeness proof and and the recursive call as arguments:\n\n\\begin{AgdaSuppressSpace}\n\\begin{code}\nnfType : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 nfCtx \u0393 \u22a2Nf nf A\nnfType (conv p t) = conv\u22a2Nf (completeness p) (nfType t)\n\\end{code}\n\n$\\quad\\vdots\\quad$ (remaining cases omitted)\\\\\n\n\\begin{code}[hide]\nnfType (` \u03b1) = ` (nfTyVar \u03b1)\nnfType (\u019b t) = \u019b (nfType t)\nnfType (t \u00b7 u) = nfType t \u00b7 nfType u\nnfType (\u039b {B = B} t) = conv\u22a2Nf (lem\u03a0nf B) (\u039b (nfType t))\nnfType {\u0393} (_\u00b7\u22c6_ {B = B} t A) = conv\u22a2Nf (lem[]' A B) (nfType t \u00b7\u22c6 nf A)\nnfType {\u0393} (wrap A L) =\n conv\u22a2Nf (lem\u03bcnf A) (wrap (nf A) (conv\u22a2Nf (lem-nf\u03bc A) (nfType L)))\nnfType {\u0393} (unwrap {A = A} L) =\n conv\u22a2Nf (sym (lem-nf\u03bc A))\n (unwrap (conv\u22a2Nf (sym (lem\u03bcnf A)) (nfType L)))\n\\end{code}\n\\end{AgdaSuppressSpace}\n\n\\noindent The operation \\AgdaFunction{nfType} is not quite the same as\ncompleteness. Additionally we need that the original type is\n$\\beta$-equal to the new type. This follows from soundness of\nnormalisation.\n\n\\begin{code}\ncompletenessT : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u22a2 A\n \u2192 nfCtx \u0393 \u22a2Nf nf A \u00d7 (A \u2261\u03b2 embNf (nf A))\ncompletenessT {A = A} t = nfType t ,, soundness A\n\\end{code}\n\n\\subsection{Erasure}\n\nWe have two version of terms, and we can convert from one to the\nother. But, how do we know that after conversion, we still have the\n\\emph{same} term? One answer is to show that that the term before\nconversion and the term after conversion both erase to the same\nuntyped term. First, we define untyped (but intrinsically scoped) \u03bb-terms:\n\n\\begin{code}\ndata _\u22a2 : \u2115 \u2192 Set where\n ` : \u2200{n} \u2192 Fin n \u2192 n \u22a2\n \u019b : \u2200{n} \u2192 suc n \u22a2 \u2192 n \u22a2\n _\u00b7_ : \u2200{n} \u2192 n \u22a2 \u2192 n \u22a2 \u2192 n \u22a2\n\\end{code}\n\n\\noindent Following the pattern of the soundness and completeness\nproofs we deal in turn with contexts, variables, and then terms. In\nthis case erasing a context corresponds to counting the number of term\nvariables in the context:\n\n\\begin{code}\nlen : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u2115\nlen \u2205 = 0\nlen (\u0393 ,\u22c6 K) = len \u0393\nlen (\u0393 , A) = suc (len \u0393)\n\\end{code}\n\n\\noindent Erasure for variables converts them to elements of\n\\AgdaDatatype{Fin}:\n\n\\begin{code}\neraseVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u220b A \u2192 Fin (len \u0393)\neraseVar Z = zero\neraseVar (S \u03b1) = suc (eraseVar \u03b1) \neraseVar (T \u03b1) = eraseVar \u03b1\n\\end{code}\n\n\\noindent Erasure for terms is straightforward:\n\n\\begin{code}\nerase : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 len \u0393 \u22a2\nerase (` \u03b1) = ` (eraseVar \u03b1)\nerase (\u019b L) = \u019b (erase L) \nerase (L \u00b7 M) = erase L \u00b7 erase M\nerase (\u039b L) = erase L\nerase (L \u00b7\u22c6 A) = erase L\nerase (wrap A L) = erase L\nerase (unwrap L) = erase L\nerase (conv p L) = erase L\n\\end{code}\n\n\n\\noindent Note that we drop \\AgdaFunction{wrap} and\n\\AgdaFunction{unwrap} when erasing as these special type casts merely\nindicate at which isomorphic type we want the term to\nconsidered. Without types \\AgdaFunction{wrap} and\n\\AgdaFunction{unwrap} serve no purpose.\n\nErasure from algorithmic terms proceeds in the same way as\ndeclarative terms. The only difference is the that there is no case for\n\\AgdaInductiveConstructor{conv}:\n\n\\begin{code}\nlenNf : \u2200{\u03a6} \u2192 CtxNf \u03a6 \u2192 \u2115\neraseVarNf : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u220bNf A \u2192 Fin (lenNf \u0393)\neraseNf : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 lenNf \u0393 \u22a2\n\\end{code}\n\\begin{code}[hide]\nlenNf \u2205 = 0\nlenNf (\u0393 ,\u22c6 K) = lenNf \u0393\nlenNf (\u0393 , A) = suc (lenNf \u0393)\n\neraseVarNf Z = zero\neraseVarNf (S \u03b1) = suc (eraseVarNf \u03b1) \neraseVarNf (T \u03b1) = eraseVarNf \u03b1\n\neraseNf (` \u03b1) = ` (eraseVarNf \u03b1)\neraseNf (\u019b L) = \u019b (eraseNf L) \neraseNf (L \u00b7 M) = eraseNf L \u00b7 eraseNf M\neraseNf (\u039b L) = eraseNf L\neraseNf (L \u00b7\u22c6 A) = eraseNf L\neraseNf (wrap A L) = eraseNf L\neraseNf (unwrap L) = eraseNf L\n\\end{code}\n\n\\noindent Having defined erasure for both term representations we\nproceed with the proof that normalising types preserves meaning of\nterms. We deal with contexts first, then variables, and then\nterms. Normalising types in the context preserves the number of term\nvariables in the context:\n\n\\begin{code}\nsameLen : \u2200 {\u03a6}(\u0393 : Ctx \u03a6) \u2192 lenNf (nfCtx \u0393) \u2261 len \u0393\n\\end{code}\n\\begin{code}[hide]\nsameLen \u2205 = refl\nsameLen (\u0393 ,\u22c6 J) = sameLen \u0393\nsameLen (\u0393 , A) = cong suc (sameLen \u0393)\n\nlemsuc : \u2200{n n'}(p : suc n \u2261 suc n')(q : n \u2261 n')(i : Fin n) \u2192\n suc (subst Fin q i) \u2261 subst Fin p (suc i)\nlemsuc refl refl i = refl\n\nlemT : \u2200{\u03a6 K}{\u0393 : CtxNf \u03a6}{A A' : \u03a6 \u22a2Nf\u22c6 *}{A'' : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 (p : weakenNf\u22c6 {K = K} A \u2261 A'')(q : A \u2261 A')(x : \u0393 \u220bNf A)\n \u2192 eraseVarNf x \u2261 eraseVarNf (conv\u220bNf p (T x))\nlemT refl refl x = refl\n\nlemT'' : \u2200{\u03a6 K}{\u0393 : Ctx \u03a6}{A A' : \u03a6 \u22a2\u22c6 *}{A'' : \u03a6 ,\u22c6 K \u22a2\u22c6 *}\n \u2192 (p : weaken\u22c6 {K = K} A \u2261 A'')(q : A \u2261 A')(x : \u0393 \u220b A)\n \u2192 eraseVar x \u2261 eraseVar (conv\u220b p (T x))\nlemT'' refl refl x = refl\n\\end{code}\n\n\\noindent The main complication in the proofs about variables and\nterms below is that \\AgdaFunction{sameLen} appears in the types. It\ncomplicates each case as the \\AgdaFunction{subst} prevents things from\ncomputing when its proof argument is not\n\\AgdaInductiveConstructor{refl}. This can be worked around using\nAgda's \\emph{with} feature which allows us to abstract over additional\narguments such as those which are stuck. However in this case we would\nneed to abstract over so many arguments that the proof becomes\nunreadable. Instead we prove a simple lemma for each case which\nachieves the same as using \\emph{with}. We show the simplest instance\n\\AgdaFunction{lemzero} for the \\AgdaInductiveConstructor{Z} variable\nwhich abstracts over proof of \\AgdaFunction{sameLen} and replaces it\nwith an arbitrary proof \\AgdaBound{p} that we can pattern match on.\n\n\\begin{code}\nlemzero : \u2200{n n'}(p : suc n \u2261 suc n') \u2192 zero \u2261 subst Fin p zero\nlemzero refl = refl\n\nsameVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}(x : \u0393 \u220b A)\n \u2192 eraseVar x \u2261 subst Fin (sameLen \u0393) (eraseVarNf (nfTyVar x))\nsameVar {\u0393 = \u0393 , _} Z = lemzero (cong suc (sameLen \u0393))\n\\end{code}\n$\\quad\\vdots\\quad$ (remaining cases omitted)\\\\\n\\begin{code}[hide]\nsameVar {\u0393 = \u0393 , _} (S x) = trans\n (cong suc (sameVar x))\n (lemsuc (cong suc (sameLen \u0393)) (sameLen \u0393) (eraseVarNf (nfTyVar x)))\nsameVar {\u0393 = \u0393 ,\u22c6 _} (T {A = A} x) = trans\n (sameVar x)\n (cong (subst Fin (sameLen \u0393)) (lemT (ren-nf S A) refl (nfTyVar x)))\n\nlemVar : \u2200{n n'}(p : n \u2261 n')(i : Fin n) \u2192 ` (subst Fin p i) \u2261 subst _\u22a2 p (` i)\nlemVar refl i = refl\n\nlem\u019b : \u2200{n n'}(p : n \u2261 n')(q : suc n \u2261 suc n')(t : suc n \u22a2)\n \u2192 \u019b (subst _\u22a2 q t) \u2261 subst _\u22a2 p (\u019b t) \nlem\u019b refl refl t = refl\n\nlem\u00b7 : \u2200{n n'}(p : n \u2261 n')(t u : n \u22a2)\n \u2192 subst _\u22a2 p t \u00b7 subst _\u22a2 p u \u2261 subst _\u22a2 p (t \u00b7 u)\nlem\u00b7 refl t u = refl\n\nlem\u03a0 : \u2200{\u0393 K }(B : \u0393 ,\u22c6 K \u22a2\u22c6 *) \u2192\n nf (\u03a0 B) \u2261 \u03a0 (nf B)\nlem\u03a0 B = cong \u03a0 (sym (subNf-lemma' B))\n\nlem-erase : \u2200{\u03a6 \u0393}{A A' : \u03a6 \u22a2Nf\u22c6 *}(p : A \u2261 A')(t : \u0393 \u22a2Nf A)\n \u2192 eraseNf t \u2261 eraseNf (conv\u22a2Nf p t)\nlem-erase refl t = refl\n\\end{code}\n\n\\begin{code}\nsame : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}(t : \u0393 \u22a2 A)\n \u2192 erase t \u2261 subst _\u22a2 (sameLen \u0393) (eraseNf (nfType t))\n\\end{code}\n\\begin{code}[hide]\nsame {\u0393 = \u0393}(` x) =\n trans (cong ` (sameVar x)) (lemVar (sameLen \u0393) (eraseVarNf (nfTyVar x)))\nsame {\u0393 = \u0393} (\u019b L) = trans\n (cong \u019b (same L))\n (lem\u019b (sameLen \u0393) (cong suc (sameLen \u0393)) (eraseNf (nfType L)))\nsame {\u0393 = \u0393} (L \u00b7 M) = trans\n (cong\u2082 _\u00b7_ (same L) (same M))\n (lem\u00b7 (sameLen \u0393) (eraseNf (nfType L)) (eraseNf (nfType M)))\nsame {\u0393 = \u0393} (\u039b {B = B} L) = trans\n (same L)\n (cong (subst _\u22a2 (sameLen \u0393)) (lem-erase (lem\u03a0nf B) (\u039b (nfType L))))\nsame {\u0393 = \u0393} (_\u00b7\u22c6_ {B = B} L A) = trans\n (same L)\n (cong (subst _\u22a2 (sameLen \u0393)) (lem-erase (lem[]' A B) (nfType L \u00b7\u22c6 nf A)))\nsame {\u0393 = \u0393} (wrap A L) = trans\n (same L)\n (cong (subst _\u22a2 (sameLen \u0393))\n (trans (lem-erase (lem-nf\u03bc A) (nfType L))\n (lem-erase (lem\u03bcnf A) (wrap _ (conv\u22a2Nf (lem-nf\u03bc A) (nfType L))))))\nsame {\u0393 = \u0393} (unwrap {A = A} L) = trans\n (same L)\n (cong (subst _\u22a2 (sameLen \u0393))\n (trans (lem-erase (sym (lem\u03bcnf A)) (nfType L))\n (lem-erase (sym (lem-nf\u03bc A))\n (unwrap (conv\u22a2Nf (sym (lem\u03bcnf A)) (nfType L))))))\nsame {\u0393 = \u0393} (conv p L) = trans\n (same L)\n (cong (subst _\u22a2 (sameLen \u0393)) (lem-erase (completeness p) (nfType L)))\n\\end{code}\n\n\\noindent This result indicates that when normalising the type of a\nterm we preserve the meaning of the term where the \\emph{meaning} of a\nterm is taken to be the underlying untyped term.\n\nA similar result holds for embedding terms with normal types back into\nterms with ordinary type but we omit it here.\n\n\\begin{code}[hide]\n-- the other direction\nsame'Len : \u2200 {\u03a6}(\u0393 : CtxNf \u03a6) \u2192 len (embCtx \u0393) \u2261 lenNf \u0393\nsame'Len \u2205 = refl\nsame'Len (\u0393 ,\u22c6 J) = same'Len \u0393\nsame'Len (\u0393 , A) = cong suc (same'Len \u0393)\n\nsame'Var : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 \u220bNf A)\n \u2192 eraseVarNf x \u2261 subst Fin (same'Len \u0393) (eraseVar (embTyVar x))\nsame'Var {\u0393 = \u0393 , _} Z = lemzero (cong suc (same'Len \u0393))\nsame'Var {\u0393 = \u0393 , _} (S x) = trans\n (cong suc (same'Var x))\n (lemsuc (cong suc (same'Len \u0393)) (same'Len \u0393) (eraseVar (embTyVar x)))\nsame'Var {\u0393 = \u0393 ,\u22c6 _} (T {A = A} x) = trans\n (same'Var x)\n (cong (subst Fin (same'Len \u0393))\n (lemT'' (sym (ren\u22c6-embNf S A)) refl (embTyVar x)))\n\nsame' : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}(L : \u0393 \u22a2Nf A)\n \u2192 eraseNf L \u2261 subst _\u22a2 (same'Len \u0393) (erase (embTy L))\nsame' {\u0393 = \u0393} (` x) =\n trans (cong ` (same'Var x)) (lemVar (same'Len \u0393) (eraseVar (embTyVar x)))\nsame' {\u0393 = \u0393} (\u019b L) = trans\n (cong \u019b (same' L))\n (lem\u019b (same'Len \u0393) (cong suc (same'Len \u0393)) (erase (embTy L)))\nsame' {\u0393 = \u0393} (L \u00b7 M) = trans\n (cong\u2082 _\u00b7_ (same' L) (same' M))\n (lem\u00b7 (same'Len \u0393) (erase (embTy L)) (erase (embTy M)))\nsame' {\u0393 = \u0393} (\u039b L) = same' L\nsame' {\u0393 = \u0393} (L \u00b7\u22c6 A) = same' L\nsame' {\u0393 = \u0393} (wrap A L) = same' L\nsame' {\u0393 = \u0393} (unwrap L) = same' L\n\\end{code}\n\n\\section{Operational Semantics}\n\\label{sec:operational-semantics}\n\nWe will define the operational semantics on the algorithmic\nsyntax. Indeed, this was the motivation for introducing the\nalgorithmic syntax: to provide a straightforward way to define the\nsemantics without having to deal with type equality coercions. The\noperational semantics is defined as a call-by-value small-step\nreduction relation. The relation is typed so it is not necessary to\nprove preservation as it holds intrinsically. We prove progress for\nthis relation which shows that programs cannot get stuck. As the\nreduction relation contains $\\beta$-rules we need to implement\nsubstitution for algorithmic terms before proceeding. As we did for\ntypes, we define renaming first and then use it to define\nsubstitution.\n\n\n\\subsection{Renaming for terms}\n\nWe index term level renamings\/substitutions by their type\nlevel counter parts.\n\nRenamings are functions from term variables to terms. The type of the\noutput variable is the type of the input variable renamed by the type\nlevel renaming.\n\n\\begin{code}\nRenNf : \u2200 {\u03a6 \u03a8} \u0393 \u0394 \u2192 Ren\u22c6 \u03a6 \u03a8 \u2192 Set\nRenNf \u0393 \u0394 \u03c1 = {A : _ \u22a2Nf\u22c6 *} \u2192 \u0393 \u220bNf A \u2192 \u0394 \u220bNf renNf\u22c6 \u03c1 A\n\\end{code}\n\n\\noindent We can lift a renaming both over a new term variable and\nover a new type variable. These operations are needed to push\nrenamings under binders (\\AgdaInductiveConstructor{$\\lambda$} and\n\\AgdaInductiveConstructor{$\\Lambda$} respectively).\n\n%In the code below we often omit the propositional equality coercion\n%\\AgdaFunction{subst} and its equational proof argument preferring to\n%show the underlying operation only.\n\n\\begin{code}\nliftNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}{\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8} \u2192 RenNf \u0393 \u0394 \u03c1\u22c6\n \u2192 {B : \u03a6 \u22a2Nf\u22c6 *} \u2192 RenNf (\u0393 , B) (\u0394 , renNf\u22c6 \u03c1\u22c6 B) \u03c1\u22c6\nliftNf \u03c1 Z = Z\nliftNf \u03c1 (S x) = S (\u03c1 x)\n\n\u22c6liftNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}{\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8} \u2192 RenNf \u0393 \u0394 \u03c1\u22c6\n \u2192 (\u2200 {K} \u2192 RenNf (\u0393 ,\u22c6 K) (\u0394 ,\u22c6 K) (lift\u22c6 \u03c1\u22c6))\n\u22c6liftNf \u03c1 (T x) = conv\u220bNf (trans (sym (renNf\u22c6-comp _)) (renNf\u22c6-comp _)) (T (\u03c1 x))\n\\end{code}\n\n\\noindent Next we define the functorial action of renaming on\nterms. In the type instantiation, wrap, unwrap cases we need a proof\nas this is where substitutions appear in types.\n\n\\begin{code}\nrenNf : \u2200 {\u03a6 \u03a8 \u0393 \u0394}{\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8} \u2192 RenNf \u0393 \u0394 \u03c1\u22c6\n \u2192 ({A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 \u0394 \u22a2Nf renNf\u22c6 \u03c1\u22c6 A )\nrenNf \u03c1 (` x) = ` (\u03c1 x)\nrenNf \u03c1 (\u019b N) = \u019b (renNf (liftNf \u03c1) N)\nrenNf \u03c1 (L \u00b7 M) = renNf \u03c1 L \u00b7 renNf \u03c1 M \nrenNf \u03c1 (\u039b N) = \u039b (renNf (\u22c6liftNf \u03c1) N)\nrenNf \u03c1 (_\u00b7\u22c6_ {B = B} t A) =\n conv\u22a2Nf (sym (ren[]Nf\u22c6 _ B A)) (renNf \u03c1 t \u00b7\u22c6 renNf\u22c6 _ A)\nrenNf \u03c1 (wrap A L) =\n wrap _ (conv\u22a2Nf (ren[]Nf\u22c6 _ A (\u03bc A)) (renNf \u03c1 L))\nrenNf \u03c1 (unwrap {A = A} L) =\n conv\u22a2Nf (sym (ren[]Nf\u22c6 _ A (\u03bc A))) (unwrap (renNf \u03c1 L))\n\\end{code}\n\n\\noindent Weakening by a type is a special case. Another proof is\nneeded here.\n\n\\begin{code}\nweakenNf : \u2200 {\u03d5 \u0393}{A : \u03d5 \u22a2Nf\u22c6 *}{B : \u03d5 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 \u0393 , B \u22a2Nf A\nweakenNf {A = A} x =\n conv\u22a2Nf (renNf\u22c6-id A) (renNf (conv\u220bNf (sym (renNf\u22c6-id _)) \u2218 S) x)\n\\end{code}\n\n\\noindent We can also weaken by a kind:\n\n\\begin{code}\n\u22c6weakenNf : \u2200 {\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}{K} \u2192 \u0393 \u22a2Nf A \u2192 \u0393 ,\u22c6 K \u22a2Nf weakenNf\u22c6 A\n\u22c6weakenNf x = renNf T x\n\\end{code}\n\n\\subsection{Substitution}\n\nSubstitutions are defined as functions from type variables to\nterms. Like renamings they are indexed by their type level\ncounterpart, which is used in the return type.\n\n\\begin{code}\nSubNf : \u2200 {\u03a6 \u03a8} \u0393 \u0394 \u2192 SubNf\u22c6 \u03a6 \u03a8 \u2192 Set\nSubNf \u0393 \u0394 \u03c1 = {A : _ \u22a2Nf\u22c6 *} \u2192 \u0393 \u220bNf A \u2192 \u0394 \u22a2Nf subNf\u22c6 \u03c1 A\n\\end{code}\n\n\\noindent We define lifting of a substitution over a type and a kind so\nthat we can push substitutions under binders. Agda is not able to\ninfer the type level normalising substitution in many cases so we\ninclude it explicitly.\n\n\\begin{code}[hide]\nrenVal-reflect : \u2200{\u03a6 \u03a8 K}\n \u2192 (\u03c1 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 (n : \u03a6 \u22a2Ne\u22c6 K)\n --------------------------------------------------------\n \u2192 CR K (ren\u22a7 \u03c1 (reflect n)) (reflect (renNe\u22c6 \u03c1 n))\nrenVal-reflect {K = *} \u03c1 n = refl\nrenVal-reflect {K = K \u21d2 J} \u03c1 n = refl \n\\end{code}\n\n\\begin{code}\nliftsNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8) \u2192 SubNf \u0393 \u0394 \u03c3\u22c6\n \u2192 {B : _ \u22a2Nf\u22c6 *} \u2192 SubNf (\u0393 , B) (\u0394 , subNf\u22c6 \u03c3\u22c6 B) \u03c3\u22c6\nliftsNf _ \u03c3 Z = ` Z\nliftsNf _ \u03c3 (S x) = weakenNf (\u03c3 x)\n\n\u22c6liftsNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8) \u2192 SubNf \u0393 \u0394 \u03c3\u22c6\n \u2192 \u2200 {K} \u2192 SubNf (\u0393 ,\u22c6 K) (\u0394 ,\u22c6 K) (liftsNf\u22c6 \u03c3\u22c6)\n\u22c6liftsNf \u03c3\u22c6 \u03c3 (T {A = A} x) =\n conv\u22a2Nf (weakenNf\u22c6-subNf\u22c6 \u03c3\u22c6 A) (\u22c6weakenNf (\u03c3 x))\n\\end{code}\n\n\\noindent Having defined lifting we are now ready to define\nsubstitution on terms:\n\n\\begin{code}\nsubNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8) \u2192 SubNf \u0393 \u0394 \u03c3\u22c6\n \u2192 ({A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 \u0394 \u22a2Nf subNf\u22c6 \u03c3\u22c6 A)\nsubNf \u03c3\u22c6 \u03c3 (` k) = \u03c3 k\nsubNf \u03c3\u22c6 \u03c3 (\u019b N) = \u019b (subNf \u03c3\u22c6 (liftsNf \u03c3\u22c6 \u03c3) N)\nsubNf \u03c3\u22c6 \u03c3 (L \u00b7 M) = subNf \u03c3\u22c6 \u03c3 L \u00b7 subNf \u03c3\u22c6 \u03c3 M\nsubNf \u03c3\u22c6 \u03c3 (\u039b {B = B} N) =\n \u039b (conv\u22a2Nf (subNf\u22c6-liftNf\u22c6 \u03c3\u22c6 B) (subNf (liftsNf\u22c6 \u03c3\u22c6) (\u22c6liftsNf \u03c3\u22c6 \u03c3) N))\nsubNf \u03c3\u22c6 \u03c3 (_\u00b7\u22c6_ {B = B} L M) =\n conv\u22a2Nf (sym (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 M B)) (subNf \u03c3\u22c6 \u03c3 L \u00b7\u22c6 subNf\u22c6 \u03c3\u22c6 M)\nsubNf \u03c3\u22c6 \u03c3 (wrap A L) =\n wrap _ (conv\u22a2Nf (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 (\u03bc A) A) (subNf \u03c3\u22c6 \u03c3 L))\nsubNf \u03c3\u22c6 \u03c3 (unwrap {A = A} L) =\n conv\u22a2Nf (sym (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 (\u03bc A) A)) (unwrap (subNf \u03c3\u22c6 \u03c3 L)) \n\\end{code}\n\n\\begin{code}[hide]\nextendNf : \u2200{\u03a6 \u03a8 \u0393 \u0394}\n \u2192 (\u03c3\u22c6 : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 ({A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u220bNf A \u2192 \u0394 \u22a2Nf subNf\u22c6 \u03c3\u22c6 A)\n \u2192 {A : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 (t : \u0394 \u22a2Nf subNf\u22c6 \u03c3\u22c6 A)\n \u2192 ({B : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 , A \u220bNf B \u2192 \u0394 \u22a2Nf subNf\u22c6 \u03c3\u22c6 B)\nextendNf \u03c3\u22c6 \u03c3 t Z = t\nextendNf \u03c3\u22c6 \u03c3 t (S x) = \u03c3 x\n\nlem : \u2200 {\u03a6 \u0393 K} {B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *}{A : \u03a6 \u22a2Nf\u22c6 K} \u2192 (x : \u0393 ,\u22c6 K \u220bNf B) \u2192 \n \u0393 \u22a2Nf subNf\u22c6 (extendNf\u22c6 (\u03bb x\u2081 \u2192 ne (` x\u2081)) A) B\nlem (T x) = conv\u22a2Nf (weakenNf\u22c6[] _ _) (` x)\n\\end{code}\n\n\\noindent We define special cases for single type and term variable\nsubstitution into a term, but omit their long winded and not very\ninformative definitions.\n\n\\begin{code}\n_[_]Nf : \u2200{\u03a6 \u0393}{A B : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 , B \u22a2Nf A \u2192 \u0393 \u22a2Nf B \u2192 \u0393 \u22a2Nf A\n_\u22c6[_]Nf : \u2200{\u03a6 \u0393 K}{B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *}\n \u2192 \u0393 ,\u22c6 K \u22a2Nf B \u2192 (A : \u03a6 \u22a2Nf\u22c6 K) \u2192 \u0393 \u22a2Nf B [ A ]Nf\u22c6\n\\end{code}\n\\begin{code}[hide]\n_[_]Nf {A = A}{B} b a =\n conv\u22a2Nf\n (subNf\u22c6-id A)\n (subNf (ne \u2218 `)\n (extendNf (ne \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id _)) \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id B)) a))\n b)\n_\u22c6[_]Nf b A = subNf (extendNf\u22c6 (ne \u2218 `) A) lem b\n\\end{code}\nWe now have all the equipment we need to specify small-step reduction.\n\\subsection{Reduction}\n\nBefore defining the reduction relation we define a value predicate\non terms that captures which terms cannot be reduced any\nfurther. We do not wish to perform unnecessary computation so we do\nnot compute under the binder in the \\AgdaInductiveConstructor{\u019b}\ncase. However, we do want to have the property that when you erase a\nvalue it remains a value. A typed value, after erasure, should\nnot require any further reduction to become an untyped value. This\ngives us a close correspondence between the typed and untyped\noperational semantics. So, it is essential in the\n\\AgdaInductiveConstructor{\u039b} and \\AgdaInductiveConstructor{wrap} cases\nthat the bodies are values as both of these constructors are removed\nby erasure.\n\n\\begin{code}\ndata Value {\u03a6}{\u0393} : {A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 Set where\n V-\u019b : \u2200{A B}(L : \u0393 , A \u22a2Nf B) \u2192 Value (\u019b L)\n V-\u039b : \u2200{K B}{L : \u0393 ,\u22c6 K \u22a2Nf B} \u2192 Value L \u2192 Value (\u039b L)\n V-wrap : \u2200{A}{L : \u0393 \u22a2Nf A [ \u03bc A ]Nf\u22c6} \u2192 Value L \u2192 Value (wrap A L)\n\\end{code}\n\n\\noindent We give the dynamics of the term language as a small-step\nreduction relation. The relation is is typed and terms on the left and\nright hand side have the same type so it is impossible to violate\npreservation. We have two congruence (xi) rules for application and\nonly one for type application, types are unique so the type argument\ncannot reduce. Indeed, no reduction of types is either possible or\nneeded. There are three computation (beta) rules, one for application,\none for type application and one for recursive types. We allow\nreduction in almost any term argument in the xi rules except under a\n\\AgdaInductiveConstructor{\u019b}. Allowing reduction under\n\\AgdaInductiveConstructor{\u039b} and \\AgdaInductiveConstructor{wrap} is\nrequired to ensure that their bodies become values. The value\ncondition on the function term in rule \\AgdaInductiveConstructor{\u03be-\u00b7\u2082}\nensures that, in an application, we reduce the function before the\nargument. The value condition on the argument in rule\n\\AgdaInductiveConstructor{\u03b2-\u019b} ensures that the our semantics is\ncall-by-value.\n\n\\begin{code}[hide]\ninfix 2 _\u2014\u2192_\n\\end{code}\n\\begin{code}\ndata _\u2014\u2192_ {\u03a6}{\u0393} : {A : \u03a6 \u22a2Nf\u22c6 *} \u2192 (\u0393 \u22a2Nf A) \u2192 (\u0393 \u22a2Nf A) \u2192 Set where\n \u03be-\u00b7\u2081 : \u2200{A B}{L L' : \u0393 \u22a2Nf A \u21d2 B}{M : \u0393 \u22a2Nf A}\n \u2192 L \u2014\u2192 L' \u2192 L \u00b7 M \u2014\u2192 L' \u00b7 M\n \u03be-\u00b7\u2082 : \u2200{A B}{V : \u0393 \u22a2Nf A \u21d2 B}{M M' : \u0393 \u22a2Nf A}\n \u2192 Value V \u2192 M \u2014\u2192 M' \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M'\n \u03be-\u039b : \u2200{K B}{L L' : \u0393 ,\u22c6 K \u22a2Nf B}\n \u2192 L \u2014\u2192 L' \u2192 \u039b L \u2014\u2192 \u039b L'\n \u03be-\u00b7\u22c6 : \u2200{K B}{L L' : \u0393 \u22a2Nf \u03a0 B}{A : \u03a6 \u22a2Nf\u22c6 K}\n \u2192 L \u2014\u2192 L' \u2192 L \u00b7\u22c6 A \u2014\u2192 L' \u00b7\u22c6 A\n \u03be-unwrap : \u2200{A}{L L' : \u0393 \u22a2Nf \u03bc A}\n \u2192 L \u2014\u2192 L' \u2192 unwrap L \u2014\u2192 unwrap L'\n \u03be-wrap : {A : \u03a6 ,\u22c6 * \u22a2Nf\u22c6 *}{L L' : \u0393 \u22a2Nf A [ \u03bc A ]Nf\u22c6}\n \u2192 L \u2014\u2192 L' \u2192 wrap A L \u2014\u2192 wrap A L'\n \u03b2-\u019b : \u2200{A B}{L : \u0393 , A \u22a2Nf B}{M : \u0393 \u22a2Nf A}\n \u2192 Value M \u2192 \u019b L \u00b7 M \u2014\u2192 L [ M ]Nf\n \u03b2-\u039b : \u2200{K B}{L : \u0393 ,\u22c6 K \u22a2Nf B}{A : \u03a6 \u22a2Nf\u22c6 K}\n \u2192 Value L\n \u2192 \u039b L \u00b7\u22c6 A \u2014\u2192 L \u22c6[ A ]Nf\n \u03b2-wrap : \u2200{A}{L : \u0393 \u22a2Nf A [ \u03bc A ]Nf\u22c6} \u2192 Value L\n \u2192 unwrap (wrap A L) \u2014\u2192 L\n\\end{code}\n\n\\subsection{Progress and preservation}\n\nThe reduction relation is typed. The definition guarantees that the\nterms before and after reduction will have the same type. Therefore it\nis not necessary to prove type preservation.\n\nProgress captures the property that reduction of terms should not get\nstuck, either a term is already a value or it can make a reduction\nstep. Progress requires proof. We show the proof in complete\ndetail. In an earlier version of this work when we did not reduce\nunder \\AgdaInductiveConstructor{\u039b} and we proved progress directly for\nclosed terms, i.e. for terms in the empty context. Reducing under the\n\\AgdaInductiveConstructor{\u039b} binder means that we need to reduce in\nnon-empty contexts so our previous simple approach no longer works.\n\nThere are several approaches to solving this including: (1) modifying\nterm syntax to ensure that the bodies of\n\\AgdaInductiveConstructor{\u039b}-expressions are already in fully reduced\nform (the so-called \\emph{value restriction}). This means that we need\nonly make progress in the empty context as no further progress is\nnecessary when we are in a non-empty context. This has the downside of\nchanging the language slightly but keeps progress simple; (2) defining\nneutral terms (terms whose reduction is blocked by a variable),\nproving a version of progress for open terms, observing that there are\nno neutral terms in the empty context and deriving progress for closed\nterms as a corollary. This has the disadvantage of having to introduce\nneutral terms only to rule them out and complicating the progress\nproof; (3) observe that \\AgdaInductiveConstructor{\u039b} only binds type\nvariables and not term variables and only term variables can block\nprogress, prove progress for terms in contexts that contain no term\nvariables and derive closed progress as a simple corollary. We choose\noption 3 here as the language remains the same and the progress proof\nis relatively unchanged, it just requires an extra condition on the\ncontext. The only cost is an additional predicate on contexts and an\nadditional lemma.\n\nBefore starting the progress proof we need to capture the property of\na context not containing any term variables. Our term contexts are\nindexed by type contexts, if we wanted to rule out type variables we\ncould talk about term contexts indexed by the empty type context, but\nwe cannot use the same trick for ruling out term variables. So, we use\na recursive predicate on contexts \\AgdaFunction{NoVar}. The empty\ncontext satisfies it, a context extended by (the kind of) a type\nvariable does if the underlying context does, and a context\ncontaining (the type of) a term variable does not.\n\n\\begin{code}\nNoVar : \u2200{\u03a6} \u2192 CtxNf \u03a6 \u2192 Set\nNoVar \u2205 = \u22a4\nNoVar (\u0393 ,\u22c6 J) = NoVar \u0393\nNoVar (\u0393 , A) = \u22a5\n\\end{code}\n\n\\noindent We can prove easily that it is impossible to have term variable in a\ncontext containing no term variables. There is only one case and the\nproperty follows by induction on variables:\n\n\\begin{code}\nnoVar : \u2200{\u03a6 \u0393} \u2192 NoVar \u0393 \u2192 {A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 \u220bNf A) \u2192 \u22a5\nnoVar p (T x) = noVar p x\n\\end{code}\n\n\\noindent We can now prove progress. The proof is the same as the one\nfor closed terms, except for the extra argument \\AgdaBound{p} :\n\\AgdaFunction{NoVar} \\AgdaBound{\u0393}.\n\n\\begin{code}\nprogress : \u2200{\u03a6}{\u0393} \u2192 NoVar \u0393 \u2192 {A : \u03a6 \u22a2Nf\u22c6 *}(L : \u0393 \u22a2Nf A)\n \u2192 Value L \u228e \u03a3 (\u0393 \u22a2Nf A) \u03bb L' \u2192 L \u2014\u2192 L'\n\\end{code}\n\n\\noindent The variable case is impossible.\n\n\\begin{code}\nprogress p (` x) = \u22a5-elim (noVar p x)\n\\end{code}\n\n\\noindent Any \\AgdaInductiveConstructor{\u019b}-expression is a value as we\ndo not reduce under the binder.\n\n\\begin{code}\nprogress p (\u019b L) = inl (V-\u019b L)\n\\end{code}\n\n\\noindent In the application case we first examine the result of the recursive\ncall on the function term, if it is a value, it must be a\n\\AgdaInductiveConstructor{\u019b}-expression, so we examine the recursive\ncall on the argument term. If this is a value then we perform\n\u03b2-reduction. Otherwise we make the appropriate \u03be-step.\n\n\\begin{code}\nprogress p (L \u00b7 M) with progress p L\nprogress p (L \u00b7 M) | inl V with progress p M\nprogress p (\u019b L \u00b7 M) | inl (V-\u019b L) | inl V = inr (L [ M ]Nf ,, \u03b2-\u019b V)\nprogress p (L \u00b7 M) | inl V | inr (M' ,, q) = inr (L \u00b7 M' ,, \u03be-\u00b7\u2082 V q)\nprogress p (L \u00b7 M) | inr (L' ,, q) = inr (L' \u00b7 M ,, \u03be-\u00b7\u2081 q)\n\\end{code}\n\n\\noindent As we must reduce under \\AgdaInductiveConstructor{\u039b} and\n\\AgdaInductiveConstructor{wrap} in both cases we make a recursive call\non their bodies and proceed accordingly. Notice that the argument\n\\AgdaBound{p} is unchanged in the recursive call to the body of a\n\\AgdaInductiveConstructor{\u039b} as \\AgdaFunction{NoVar} (\\AgdaBound{\u0393}\n\\AgdaInductiveConstructor{,\u22c6} \\AgdaBound{K}) = \\AgdaFunction{NoVar}\n\\AgdaBound{\u0393}.\n\n\\begin{code}\nprogress p (\u039b L) with progress p L\n... | inl V = inl (V-\u039b V)\n... | inr (L' ,, q) = inr (\u039b L' ,, \u03be-\u039b q)\nprogress p (wrap A L) with progress p L\n... | inl V = inl (V-wrap V)\n... | inr (L' ,, q) = inr (wrap A L' ,, \u03be-wrap q)\n\\end{code}\n\n\\noindent In the type application case we first examine the result of\nrecursive call on the type function argument. If it is a value it must\nbe a \\AgdaInductiveConstructor{\u039b}-expression and we perform\n\u03b2-reduction. Otherwise we perform a \u03be-step.\n\n\n\\begin{code}\nprogress p (L \u00b7\u22c6 A) with progress p L\nprogress p (\u039b L \u00b7\u22c6 A) | inl (V-\u039b V) = inr (L \u22c6[ A ]Nf ,, (\u03b2-\u039b V))\nprogress p (L \u00b7\u22c6 A) | inr (L' ,, q) = inr (L' \u00b7\u22c6 A ,, \u03be-\u00b7\u22c6 q)\n\\end{code}\n\n\\noindent In the \\AgdaInductiveConstructor{unwrap} case we examine the\nresult of the recursive call on the body. If it is a value it must be\na wrap and we perform \u03b2-reduction or a \u03be-step otherwise. That\ncompletes the proof. \n\n\\begin{code}\nprogress p (unwrap L) with progress p L\nprogress p (unwrap (wrap A L)) | inl (V-wrap V) = inr (L ,, \u03b2-wrap V)\nprogress p (unwrap L) | inr (L' ,, q) = inr (unwrap L' ,, \u03be-unwrap q)\n\\end{code}\n\n\\noindent Progress in the empty context \\AgdaFunction{progress\u2205} is a\nsimple corollary. The empty context trivially satisfies\n\\AgdaFunction{NoVar} as \\AgdaFunction{NoVar}\n\\AgdaInductiveConstructor{\u2205} = \\AgdaDatatype{\u22a4}:\n\n\\begin{code}\nprogress\u2205 : \u2200{A}(L : \u2205 \u22a2Nf A) \u2192 Value L \u228e \u03a3 (\u2205 \u22a2Nf A) \u03bb L' \u2192 L \u2014\u2192 L'\nprogress\u2205 = progress tt\n\\end{code}\n\n\\subsection{Erasure}\n\nWe can extend our treatment of erasure from\nsyntax to (operational) semantics. Indeed, when defining values were\ncareful to ensure this was possible.\n\n\\begin{code}[hide]\ninfix 2 _U\u2014\u2192_\nopen import Data.List hiding ([_]; map)\n\nRenU : \u2115 \u2192 \u2115 \u2192 Set\nRenU m n = Fin m \u2192 Fin n\n\nliftU : \u2200{m n} \u2192 RenU m n \u2192 RenU (suc m) (suc n)\nliftU \u03c1 zero = zero\nliftU \u03c1 (suc x) = suc (\u03c1 x)\n\nrenU : \u2200{m n} \u2192 RenU m n \u2192 m \u22a2 \u2192 n \u22a2\nrenU \u03c1 (` x) = ` (\u03c1 x)\nrenU \u03c1 (\u019b t) = \u019b (renU (liftU \u03c1) t)\nrenU \u03c1 (t \u00b7 u) = renU \u03c1 t \u00b7 renU \u03c1 u\n\nliftU-cong : \u2200{m n}{\u03c1 \u03c1' : RenU m n}\n \u2192 (\u2200 \u03b1 \u2192 \u03c1 \u03b1 \u2261 \u03c1' \u03b1)\n \u2192 (\u03b1 : Fin (suc m))\n \u2192 liftU \u03c1 \u03b1 \u2261 liftU \u03c1' \u03b1\nliftU-cong p zero = refl\nliftU-cong p (suc \u03b1) = cong suc (p \u03b1)\n\nrenU-cong : \u2200{m n}{\u03c1 \u03c1' : RenU m n}\n \u2192 (\u2200 \u03b1 \u2192 \u03c1 \u03b1 \u2261 \u03c1' \u03b1)\n \u2192 (t : m \u22a2)\n \u2192 renU \u03c1 t \u2261 renU \u03c1' t\n\nrenU-cong p (` x) = cong ` (p x)\nrenU-cong p (\u019b t) = cong \u019b (renU-cong (liftU-cong p) t)\nrenU-cong p (t \u00b7 u) = cong\u2082 _\u00b7_ (renU-cong p t) (renU-cong p u)\n\nrenU-id : \u2200{n} \u2192 (t : n \u22a2) \u2192 t \u2261 renU id t\n\nliftU-id : \u2200{n} \u2192 (\u03b1 : Fin (suc n)) \u2192 id \u03b1 \u2261 liftU id \u03b1\nliftU-id zero = refl\nliftU-id (suc \u03b1) = refl\n\nrenU-id (` x) = refl\nrenU-id (\u019b t) = cong\n \u019b\n (trans\n (renU-id t)\n (renU-cong liftU-id t)) \nrenU-id (t \u00b7 u) = cong\u2082 _\u00b7_ (renU-id t) (renU-id u)\n\n--\n\nSubU : \u2115 \u2192 \u2115 \u2192 Set\nSubU m n = Fin m \u2192 n \u22a2\n\nliftUs : \u2200{m n} \u2192 SubU m n \u2192 SubU (suc m) (suc n)\nliftUs \u03c1 zero = ` zero\nliftUs \u03c1 (suc x) = renU suc (\u03c1 x)\n\nsubU : \u2200{m n} \u2192 SubU m n \u2192 m \u22a2 \u2192 n \u22a2\nsubU \u03c3 (` x) = \u03c3 x\nsubU \u03c3 (\u019b t) = \u019b (subU (liftUs \u03c3) t) \nsubU \u03c3 (t \u00b7 u) = subU \u03c3 t \u00b7 subU \u03c3 u\n\nextendU : \u2200{m n} \u2192 SubU m n \u2192 n \u22a2 \u2192 SubU (suc m) n\nextendU \u03c3 t zero = t\nextendU \u03c3 t (suc x) = \u03c3 x\n\nliftsU-cong : \u2200{m n}{\u03c3 \u03c3' : SubU m n}\n \u2192 (\u2200 \u03b1 \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 (\u03b1 : Fin (suc m))\n \u2192 liftUs \u03c3 \u03b1 \u2261 liftUs \u03c3' \u03b1\nliftsU-cong p zero = refl\nliftsU-cong p (suc \u03b1) = cong (renU suc) (p \u03b1) \n\nsubU-cong : \u2200{m n}{\u03c3 \u03c3' : SubU m n}\n \u2192 (\u2200 \u03b1 \u2192 \u03c3 \u03b1 \u2261 \u03c3' \u03b1)\n \u2192 (t : m \u22a2)\n \u2192 subU \u03c3 t \u2261 subU \u03c3' t\nsubU-cong p (` x) = p x\nsubU-cong p (\u019b t) = cong \u019b (subU-cong (liftsU-cong p) t)\nsubU-cong p (t \u00b7 u) = cong\u2082 _\u00b7_ (subU-cong p t) (subU-cong p u)\n\nliftsU-id : \u2200{n} \u2192 (\u03b1 : Fin (suc n)) \u2192 ` \u03b1 \u2261 liftUs ` \u03b1\nliftsU-id zero = refl\nliftsU-id (suc \u03b1) = refl\n\nsubU-id : \u2200{n} \u2192 (t : n \u22a2) \u2192 t \u2261 subU ` t\nsubU-id (` x) = refl\nsubU-id (\u019b t) = cong \u019b (trans (subU-id t) (subU-cong liftsU-id t))\nsubU-id (t \u00b7 u) = cong\u2082 _\u00b7_ (subU-id t) (subU-id u)\n\\end{code}\n\n\\noindent To define the $\\beta$-rule we need need to be able to\nperform substitution on one variable only. As for syntaxes in earlier\nsections we define parallel renaming and substitution first and get\nsubstitution on one variable as a special case. We omit the details\nhere which are analogous to earlier sections.\n\n\\begin{code}\n_[_]U : \u2200{n} \u2192 suc n \u22a2 \u2192 n \u22a2 \u2192 n \u22a2\n\\end{code}\n\\begin{code}[hide]\nt [ u ]U = subU (extendU ` u) t\n\\end{code}\n\\begin{code}[hide]\nbackVar\u22c6 : \u2200{\u03a6}(\u0393 : CtxNf \u03a6) \u2192 Fin (lenNf \u0393) \u2192 \u03a6 \u22a2Nf\u22c6 *\nbackVar\u22c6 (\u0393 ,\u22c6 J) x = weakenNf\u22c6 (backVar\u22c6 \u0393 x)\nbackVar\u22c6 (\u0393 , A) zero = A\nbackVar\u22c6 (\u0393 , A) (suc x) = backVar\u22c6 \u0393 x\n\nbackVar : \u2200{\u03a6}(\u0393 : CtxNf \u03a6)(i : Fin (lenNf \u0393)) \u2192 \u0393 \u220bNf (backVar\u22c6 \u0393 i)\nbackVar (\u0393 ,\u22c6 J) x = T (backVar \u0393 x)\nbackVar (\u0393 , A) zero = Z\nbackVar (\u0393 , A) (suc x) = S (backVar \u0393 x)\n\nbackVar\u22c6-eraseVar : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 \u220bNf A) \u2192\n backVar\u22c6 \u0393 (eraseVarNf x) \u2261 A\nbackVar\u22c6-eraseVar Z = refl\nbackVar\u22c6-eraseVar (S x) = backVar\u22c6-eraseVar x\nbackVar\u22c6-eraseVar (T x) = cong weakenNf\u22c6 (backVar\u22c6-eraseVar x)\n\nconv\u220b-S : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{B A A' : \u03a6 \u22a2Nf\u22c6 *}(p : A \u2261 A')(x : \u0393 \u220bNf A) \u2192\n conv\u220bNf p (S {B = B} x) \u2261 S (conv\u220bNf p x)\nconv\u220b-S refl x = refl\n\nconv\u220b-T : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A A' : \u03a6 \u22a2Nf\u22c6 *}{K} \u2192\n (p : A \u2261 A')(q : weakenNf\u22c6 {K = K} A \u2261 weakenNf\u22c6 A') \u2192 (x : \u0393 \u220bNf A) \u2192\n conv\u220bNf q (T x) \u2261 T (conv\u220bNf p x)\nconv\u220b-T refl refl x = refl\n\ncong-erase-ren : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c1 : RenNf \u0393 \u0394 \u03c1\u22c6){A A' : \u03a6 \u22a2Nf\u22c6 *}(p : A' \u2261 A)\n \u2192 (x : \u0393 \u220bNf A)(x' : \u0393 \u220bNf A') \u2192 conv\u220bNf p x' \u2261 x\n \u2192 eraseVarNf (\u03c1 x) \u2261 eraseVarNf (\u03c1 x')\ncong-erase-ren \u03c1\u22c6 \u03c1 refl x .x refl = refl\n\nbackVar-eraseVar : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 \u220bNf A) \u2192\n conv\u220bNf (backVar\u22c6-eraseVar x) (backVar \u0393 (eraseVarNf x)) \u2261 x\nbackVar-eraseVar Z = refl\nbackVar-eraseVar {\u0393 = \u0393 , A} (S x) = trans\n ((conv\u220b-S (backVar\u22c6-eraseVar x) (backVar _ (eraseVarNf x))))\n (cong S (backVar-eraseVar x))\nbackVar-eraseVar (T x) = trans\n (conv\u220b-T (backVar\u22c6-eraseVar x)\n (cong weakenNf\u22c6 (backVar\u22c6-eraseVar x))\n (backVar _ (eraseVarNf x)))\n (cong T (backVar-eraseVar x))\n\n\neraseVar-backVar : \u2200{\u03a6}(\u0393 : CtxNf \u03a6)(x : Fin (lenNf \u0393)) \u2192\n eraseVarNf (backVar \u0393 x) \u2261 x\neraseVar-backVar \u2205 ()\neraseVar-backVar (\u0393 ,\u22c6 J) x = eraseVar-backVar \u0393 x\neraseVar-backVar (\u0393 , A) zero = refl\neraseVar-backVar (\u0393 , A) (suc x) = cong suc (eraseVar-backVar \u0393 x)\n\n--\n\nerase-Ren : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 RenNf \u0393 \u0394 \u03c1\u22c6 \u2192 RenU (lenNf \u0393) (lenNf \u0394) \nerase-Ren \u03c1\u22c6 \u03c1 i = eraseVarNf (\u03c1 (backVar _ i))\n\next-erase : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c1 : RenNf \u0393 \u0394 \u03c1\u22c6){A : \u03a6 \u22a2Nf\u22c6 *}(\u03b1 : Fin (lenNf (\u0393 , A)))\n \u2192 erase-Ren \u03c1\u22c6 (liftNf \u03c1 {B = A}) \u03b1 \u2261 liftU (erase-Ren \u03c1\u22c6 \u03c1) \u03b1\next-erase \u03c1\u22c6 \u03c1 zero = refl\next-erase \u03c1\u22c6 \u03c1 (suc \u03b1) = refl\n\nconv\u220b-erase : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A A' : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 (p : A \u2261 A')\n \u2192 (x : \u0393 \u220bNf A)\n \u2192 eraseVarNf (conv\u220bNf p x) \u2261 eraseVarNf x\nconv\u220b-erase refl x = refl\n\nconv\u22a2-erase : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A A' : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 (p : A \u2261 A')\n \u2192 (t : \u0393 \u22a2Nf A)\n \u2192 eraseNf (conv\u22a2Nf p t) \u2261 eraseNf t\nconv\u22a2-erase refl t = refl\n\next\u22c6-erase : \u2200{\u03a6 \u03a8 K}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c1 : RenNf \u0393 \u0394 \u03c1\u22c6)(\u03b1 : Fin (lenNf \u0393))\n \u2192 erase-Ren (lift\u22c6 \u03c1\u22c6 {K = K}) (\u22c6liftNf \u03c1) \u03b1 \u2261 erase-Ren \u03c1\u22c6 \u03c1 \u03b1\next\u22c6-erase {\u0393 = \u0393} \u03c1\u22c6 \u03c1 \u03b1 = conv\u220b-erase\n (trans (sym (renNf\u22c6-comp (backVar\u22c6 \u0393 \u03b1))) (renNf\u22c6-comp (backVar\u22c6 \u0393 \u03b1)))\n (T (\u03c1 (backVar \u0393 \u03b1))) \n\nren-erase : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c1\u22c6 : Ren\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c1 : RenNf \u0393 \u0394 \u03c1\u22c6){A : \u03a6 \u22a2Nf\u22c6 *} \u2192 (t : \u0393 \u22a2Nf A)\n \u2192 eraseNf (renNf \u03c1 t) \u2261 renU (erase-Ren \u03c1\u22c6 \u03c1) (eraseNf t)\nren-erase \u03c1\u22c6 \u03c1 (` x) = cong `\n (cong-erase-ren\n \u03c1\u22c6\n \u03c1\n (backVar\u22c6-eraseVar x)\n x\n (backVar _ (eraseVarNf x))\n (backVar-eraseVar x))\nren-erase \u03c1\u22c6 \u03c1 (\u019b t) = cong \u019b\n (trans\n ((ren-erase \u03c1\u22c6 (liftNf \u03c1) t))\n (renU-cong (ext-erase \u03c1\u22c6 \u03c1) (eraseNf t)))\nren-erase \u03c1\u22c6 \u03c1 (t \u00b7 u) =\n cong\u2082 _\u00b7_ (ren-erase \u03c1\u22c6 \u03c1 t) (ren-erase \u03c1\u22c6 \u03c1 u)\nren-erase \u03c1\u22c6 \u03c1 (\u039b t) = trans\n (ren-erase (lift\u22c6 \u03c1\u22c6) (\u22c6liftNf \u03c1) t)\n (renU-cong (ext\u22c6-erase \u03c1\u22c6 \u03c1) (eraseNf t))\nren-erase \u03c1\u22c6 \u03c1 (_\u00b7\u22c6_ {B = B} t A) = trans\n ((conv\u22a2-erase (sym (ren[]Nf\u22c6 \u03c1\u22c6 B A)) (renNf \u03c1 t \u00b7\u22c6 renNf\u22c6 \u03c1\u22c6 A)))\n (ren-erase \u03c1\u22c6 \u03c1 t)\nren-erase \u03c1\u22c6 \u03c1 (wrap A t) = trans\n (conv\u22a2-erase (ren[]Nf\u22c6 \u03c1\u22c6 A (\u03bc A)) (renNf \u03c1 t))\n (ren-erase \u03c1\u22c6 \u03c1 t)\nren-erase \u03c1\u22c6 \u03c1 (unwrap {A = A} t) = trans\n (conv\u22a2-erase (sym (ren[]Nf\u22c6 \u03c1\u22c6 A (\u03bc A))) (unwrap (renNf \u03c1 t)))\n (ren-erase \u03c1\u22c6 \u03c1 t)\n\n--\nerase-Sub : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8)\n \u2192 SubNf \u0393 \u0394 \u03c3\u22c6 \u2192 SubU (lenNf \u0393) (lenNf \u0394) \nerase-Sub \u03c3\u22c6 \u03c3 i = eraseNf (\u03c3 (backVar _ i))\n\ncong-erase-sub : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c3 : SubNf \u0393 \u0394 \u03c3\u22c6){A A' : \u03a6 \u22a2Nf\u22c6 *}(p : A' \u2261 A)\n \u2192 (x : \u0393 \u220bNf A)(x' : \u0393 \u220bNf A') \u2192 conv\u220bNf p x' \u2261 x\n \u2192 eraseNf (\u03c3 x) \u2261 eraseNf (\u03c3 x')\ncong-erase-sub \u03c3\u22c6 \u03c3 refl x .x refl = refl\n\nexts-erase : \u2200 {\u03a6 \u03a8}{\u0393 \u0394}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8)(\u03c3 : SubNf \u0393 \u0394 \u03c3\u22c6)\n \u2192 {B : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 (\u03b1 : Fin (suc (lenNf \u0393)))\n \u2192 erase-Sub \u03c3\u22c6 (liftsNf \u03c3\u22c6 \u03c3 {B}) \u03b1 \u2261 liftUs (erase-Sub \u03c3\u22c6 \u03c3) \u03b1\nexts-erase \u03c3\u22c6 \u03c3 zero = refl\nexts-erase {\u0393 = \u0393}{\u0394} \u03c3\u22c6 \u03c3 {B} (suc \u03b1) = trans\n (conv\u22a2-erase\n (renNf\u22c6-id (subNf\u22c6 \u03c3\u22c6 (backVar\u22c6 \u0393 \u03b1)))\n (renNf (conv\u220bNf (sym (renNf\u22c6-id _)) \u2218 S) (\u03c3 (backVar \u0393 \u03b1))) )\n (trans (ren-erase id (conv\u220bNf (sym (renNf\u22c6-id _)) \u2218 S) (\u03c3 (backVar \u0393 \u03b1)))\n (renU-cong (\u03bb \u03b2 \u2192 trans\n (conv\u220b-erase (sym (renNf\u22c6-id _)) (S (backVar \u0394 \u03b2)))\n (cong suc (eraseVar-backVar \u0394 \u03b2)))\n (eraseNf (\u03c3 (backVar \u0393 \u03b1)))))\n\nexts\u22c6-erase : \u2200 {\u03a6 \u03a8}{\u0393 \u0394}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8)(\u03c3 : SubNf \u0393 \u0394 \u03c3\u22c6)\n \u2192 {B : \u03a6 \u22a2Nf\u22c6 *}\n \u2192 \u2200{K}\n \u2192 (\u03b1 : Fin (lenNf \u0393))\n \u2192 erase-Sub (liftsNf\u22c6 \u03c3\u22c6 {K}) ( \u22c6liftsNf \u03c3\u22c6 \u03c3) \u03b1 \u2261 erase-Sub \u03c3\u22c6 \u03c3 \u03b1 \nexts\u22c6-erase {\u0393 = \u0393}{\u0394} \u03c3\u22c6 \u03c3 {B} \u03b1 = trans\n (conv\u22a2-erase\n (weakenNf\u22c6-subNf\u22c6 \u03c3\u22c6 (backVar\u22c6 \u0393 \u03b1))\n ((\u22c6weakenNf (\u03c3 (backVar \u0393 \u03b1))))) -- \n (trans\n (ren-erase S T (\u03c3 (backVar \u0393 \u03b1)))\n (trans\n (renU-cong (eraseVar-backVar \u0394) (eraseNf (\u03c3 (backVar \u0393 \u03b1))))\n (sym (renU-id (eraseNf (\u03c3 (backVar \u0393 \u03b1)))))))\n\nsub-erase : \u2200{\u03a6 \u03a8}{\u0393 : CtxNf \u03a6}{\u0394 : CtxNf \u03a8}(\u03c3\u22c6 : SubNf\u22c6 \u03a6 \u03a8)\n \u2192 (\u03c3 : SubNf \u0393 \u0394 \u03c3\u22c6){A : \u03a6 \u22a2Nf\u22c6 *} \u2192 (t : \u0393 \u22a2Nf A)\n \u2192 eraseNf (subNf \u03c3\u22c6 \u03c3 t) \u2261 subU (erase-Sub \u03c3\u22c6 \u03c3) (eraseNf t) \nsub-erase \u03c3\u22c6 \u03c3 (` x) =\n cong-erase-sub\n \u03c3\u22c6\n \u03c3\n (backVar\u22c6-eraseVar x)\n x\n (backVar _ (eraseVarNf x))\n (backVar-eraseVar x)\nsub-erase \u03c3\u22c6 \u03c3 (\u019b t) = cong \u019b\n (trans ((sub-erase \u03c3\u22c6 (liftsNf \u03c3\u22c6 \u03c3) t))\n (subU-cong (exts-erase \u03c3\u22c6 \u03c3) (eraseNf t)))\nsub-erase \u03c3\u22c6 \u03c3 (t \u00b7 u) = cong\u2082 _\u00b7_ (sub-erase \u03c3\u22c6 \u03c3 t) (sub-erase \u03c3\u22c6 \u03c3 u)\nsub-erase \u03c3\u22c6 \u03c3 (\u039b {B = B} t) = trans\n ((conv\u22a2-erase (subNf\u22c6-liftNf\u22c6 \u03c3\u22c6 B) (subNf (liftsNf\u22c6 \u03c3\u22c6) (\u22c6liftsNf \u03c3\u22c6 \u03c3) t)))\n (trans (sub-erase (liftsNf\u22c6 \u03c3\u22c6) (\u22c6liftsNf \u03c3\u22c6 \u03c3) t)\n (subU-cong (exts\u22c6-erase \u03c3\u22c6 \u03c3 {B = \u03a0 B}) (eraseNf t)))\nsub-erase \u03c3\u22c6 \u03c3 (_\u00b7\u22c6_ {B = B} t A) = trans\n ((conv\u22a2-erase (sym (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 A B)) (subNf \u03c3\u22c6 \u03c3 t \u00b7\u22c6 subNf\u22c6 \u03c3\u22c6 A)))\n (sub-erase \u03c3\u22c6 \u03c3 t)\nsub-erase \u03c3\u22c6 \u03c3 (wrap A t) = trans\n (conv\u22a2-erase (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 (\u03bc A) A) (subNf \u03c3\u22c6 \u03c3 t))\n (sub-erase \u03c3\u22c6 \u03c3 t)\nsub-erase \u03c3\u22c6 \u03c3 (unwrap {A = A} t) = trans\n (conv\u22a2-erase (sym (subNf\u22c6-[]Nf\u22c6 \u03c3\u22c6 (\u03bc A) A)) (unwrap (subNf \u03c3\u22c6 \u03c3 t)))\n (sub-erase \u03c3\u22c6 \u03c3 t)\n\\end{code}\n\n\\noindent When erasing reduction steps below we will require two\nproperties about pushing erasure through a normalising single variable\nsubstitution. These properties follow from properties of parallel\nrenaming and substitution:\n\n\\begin{code}\neraseNf-\u22c6[]Nf : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{K B}(L : \u0393 ,\u22c6 K \u22a2Nf B)(A : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 eraseNf L \u2261 eraseNf (L \u22c6[ A ]Nf)\neraseNf-[]Nf : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}{A B}(L : \u0393 , A \u22a2Nf B)(M : \u0393 \u22a2Nf A)\n \u2192 eraseNf L [ eraseNf M ]U \u2261 eraseNf (L [ M ]Nf)\n\\end{code}\n\n\\begin{code}[hide]\neraseNf-\u22c6[]Nf {\u0393 = \u0393} N A = trans\n (trans\n (subU-id (eraseNf N))\n (subU-cong\n (\u03bb \u03b1 \u2192 trans\n (cong ` (sym (eraseVar-backVar \u0393 \u03b1)))\n ((sym (conv\u22a2-erase (weakenNf\u22c6[] _ _) (` (backVar \u0393 \u03b1))))))\n (eraseNf N)))\n (sym (sub-erase (extendNf\u22c6 (ne \u2218 `) A) lem N))\n\neraseNf-[]Nf {\u0393 = \u0393}{A = A}{B} N W = trans\n (trans\n (subU-cong\n (\u03bb{ zero \u2192 sym (conv\u22a2-erase (sym (subNf\u22c6-id A)) W)\n ; (suc \u03b1) \u2192 trans\n (cong ` (sym (eraseVar-backVar \u0393 \u03b1)))\n (sym (conv\u22a2-erase\n (sym (subNf\u22c6-id (backVar\u22c6 \u0393 \u03b1)))\n (` (backVar \u0393 \u03b1))))})\n (eraseNf N))\n ((sym (sub-erase\n (ne \u2218 `)\n (extendNf\n (ne \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id _)) \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id A)) W))\n N))))\n (sym\n (conv\u22a2-erase\n (subNf\u22c6-id B)\n ( subNf (ne \u2218 `)\n (extendNf\n (ne \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id _)) \u2218 `)\n (conv\u22a2Nf (sym (subNf\u22c6-id A)) W))\n N )))\n\\end{code}\n\n\\noindent There is only one value in untyped lambda calculus: lambda.\n\n\\begin{code}\ndata UValue {n} : n \u22a2 \u2192 Set where\n V-\u019b : (t : suc n \u22a2) \u2192 UValue (\u019b t)\n\\end{code}\n\n\\noindent We define a call-by-value small-step reduction relation that\nis intrinsically scoped.\n\n\\begin{code}\ndata _U\u2014\u2192_ {n} : n \u22a2 \u2192 n \u22a2 \u2192 Set where\n \u03be-\u00b7\u2081 : {L L' M : n \u22a2} \u2192 L U\u2014\u2192 L' \u2192 L \u00b7 M U\u2014\u2192 L' \u00b7 M\n \u03be-\u00b7\u2082 : {L M M' : n \u22a2} \u2192 UValue L \u2192 M U\u2014\u2192 M' \u2192 L \u00b7 M U\u2014\u2192 L \u00b7 M'\n \u03b2-\u019b : {L : suc n \u22a2}{M : n \u22a2} \u2192 UValue M \u2192 \u019b L \u00b7 M U\u2014\u2192 L [ M ]U\n\\end{code}\n\n\\noindent Erasing values is straightforward. The only tricky part is\nto ensure that in values the subterms of the values for wrap and \u039b are\nalso values as discussed earlier. This ensures that after when we\nerase a typed value we will always get an untyped value:\n\n\\begin{code}\neraseVal : \u2200{\u03a6 A}{\u0393 : CtxNf \u03a6}{t : \u0393 \u22a2Nf A} \u2192 Value t \u2192 UValue (eraseNf t)\neraseVal (V-\u019b t) = V-\u019b (eraseNf t)\neraseVal (V-\u039b v) = eraseVal v\neraseVal (V-wrap v) = eraseVal v\n\\end{code}\n\n\\noindent Erasing a reduction step is more subtle as we may either get\na typed reduction step (e.g., \\AgdaInductiveConstructor{\u03b2-\u019b}) or the\nstep may disappear (e.g., \\AgdaInductiveConstructor{\u03b2-wrap}). In the\nlatter case the erasure of the terms before and after reduction will\nbe identical:\n\n\\begin{code}\nerase\u2014\u2192 : \u2200{\u03a6 A}{\u0393 : CtxNf \u03a6}{t t' : \u0393 \u22a2Nf A}\n \u2192 t \u2014\u2192 t' \u2192 eraseNf t U\u2014\u2192 eraseNf t' \u228e eraseNf t \u2261 eraseNf t'\n\\end{code}\n\n\\noindent In the congruence cases for application what we need to do\ndepends on the result of erasing the underlying reduction step. We\nmake use of \\AgdaFunction{map} for Sum types for this purpose, the\nfirst argument explains what to do if the underlying step corresponds\nto a untyped reduction step (we create an untyped congruence reducing\nstep) and the second argument explains what to do if the underlying\nstep disappears (we create an equality proof):\n\n\\begin{code}\nerase\u2014\u2192 (\u03be-\u00b7\u2081 {M = M} p) =\n Sum.map \u03be-\u00b7\u2081 (cong (_\u00b7 eraseNf M)) (erase\u2014\u2192 p)\nerase\u2014\u2192 (\u03be-\u00b7\u2082 {V = V} p q) =\n Sum.map (\u03be-\u00b7\u2082 (eraseVal p)) (cong (eraseNf V \u00b7_)) (erase\u2014\u2192 q)\n\\end{code}\n\n\\noindent In the following cases the outer reduction step is removed:\n\n\\begin{code}\nerase\u2014\u2192 (\u03be-\u00b7\u22c6 p) = erase\u2014\u2192 p\nerase\u2014\u2192 (\u03be-\u039b p) = erase\u2014\u2192 p\nerase\u2014\u2192 (\u03be-unwrap p) = erase\u2014\u2192 p\nerase\u2014\u2192 (\u03be-wrap p) = erase\u2014\u2192 p\n\\end{code}\n\n\\noindent In the case of \u03b2-reduction for an ordinary application we\nalways produce a corresponding untyped \u03b2-reduction step:\n\n\\begin{code}\nerase\u2014\u2192 (\u03b2-\u019b {L = L}{M = M} V) = inl (subst\n (\u019b (eraseNf L) \u00b7 eraseNf M U\u2014\u2192_)\n (eraseNf-[]Nf L M)\n (_U\u2014\u2192_.\u03b2-\u019b {L = eraseNf L}{M = eraseNf M} (eraseVal V)))\n\\end{code}\n\n\\noindent In the other two \u03b2-reduction cases the step is always\nremoved, e.g., \\AgdaInductiveConstructor{unwrap}\n(\\AgdaInductiveConstructor{wrap} \\AgdaBound{A} \\AgdaBound{L})\n\\AgdaDatatype{\u2014\u2192} \\AgdaBound{L} becomes \\AgdaBound{L} \\AgdaDatatype{\u2261}\n\\AgdaBound{L}:\n\n\\begin{code}\nerase\u2014\u2192 (\u03b2-\u039b {L = L}{A = A} V) = inr (eraseNf-\u22c6[]Nf L A)\nerase\u2014\u2192 (\u03b2-wrap _) = inr refl\n\\end{code}\n\n\\noindent That concludes the proof: either a typed reduction step\ncorresponds to an untyped one or no step at all.\n\nWe can combine erasure of values and reduction steps to get a progress\nlike result for untyped terms via erasure. Via typed progress we\neither arrive immediately at an untyped value, or a typed reduction\nstep must exist and it will corr respond to an untyped step, or the\nstep disappears:\n\n\\begin{code}\nerase-progress\u2205 : \u2200{A : \u2205 \u22a2Nf\u22c6 *}(L : \u2205 \u22a2Nf A)\n \u2192 UValue (eraseNf L)\n \u228e \u03a3 (\u2205 \u22a2Nf A) \u03bb L' \u2192 (L \u2014\u2192 L')\n \u00d7 (eraseNf L U\u2014\u2192 eraseNf L' \u228e eraseNf L \u2261 eraseNf L')\nerase-progress\u2205 L =\n Sum.map eraseVal (\u03bb {(L' ,, p) \u2192 L' ,, p ,, (erase\u2014\u2192 p)}) (progress\u2205 L)\n\\end{code}\n\n\\section{Execution}\n\\label{sec:execution}\n\\begin{code}[hide]\nopen import Data.Nat\nopen import Data.Maybe as Maybe\n\\end{code}\n\nWe can iterate progress an arbitrary number of times to run\nprograms. First, we define the reflexive transitive closure of\nreduction. We will use this to represent traces of execution:\n\n\\begin{code}\ndata _\u2014\u2192*_ {\u03a6 \u0393} : {A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2Nf A \u2192 \u0393 \u22a2Nf A \u2192 Set where\n refl\u2014\u2192 : \u2200{A}{M : \u0393 \u22a2Nf A} \u2192 M \u2014\u2192* M\n trans\u2014\u2192 : \u2200{A}{M M' M'' : \u0393 \u22a2Nf A}\n \u2192 M \u2014\u2192 M' \u2192 M' \u2014\u2192* M'' \u2192 M \u2014\u2192* M''\n\\end{code}\n\n\\noindent The \\AgdaFunction{run} function takes a number of allowed\nsteps and a term. It returns another term, a proof that the original\nterm reduces to the new term in zero or more steps and possibly a\nproof that the new term is a value. If no value proof is returned this\nindicates that we did not reach a value in the allowed number of\nsteps.\n\nIf we are allowed zero more steps we return failure immediately. If we\nare allowed more steps then we call progress to make one. If we get a\nvalue back we return straight away with a value. If we have not yet\nreached a value we call \\AgdaFunction{run} recursively having spent a\nstep. We then prepend our step to the sequence of steps returned by\nrun and return:\n\n\\begin{code}\nrun : \u2200 {A : \u2205 \u22a2Nf\u22c6 *} \u2192 \u2115 \u2192 (M : \u2205 \u22a2Nf A)\n \u2192 \u03a3 (\u2205 \u22a2Nf A) \u03bb M' \u2192 (M \u2014\u2192* M') \u00d7 Maybe (Value M')\nrun zero M = M ,, refl\u2014\u2192 ,, nothing\nrun (suc n) M with progress\u2205 M\nrun (suc n) M | inl V = M ,, refl\u2014\u2192 ,, just V\nrun (suc n) M | inr (M' ,, p) with run n M'\n... | M'' ,, q ,, mV = M'' ,, trans\u2014\u2192 p q ,, mV\n\\end{code}\n\n\\subsection{Erasure}\n\nGiven that the evaluator \\AgdaFunction{run} produces a trace of\nreduction that (if it doesn't run out of allowed steps) leads to a\nvalue we can erase the trace and value to yield a trace of untyped\nexecution leading to an untyped value. Note that the untyped trace may\nbe shorter as some steps may disappear.\n\nWe define the the reflexive transitive closure of untyped reduction\nanalogously to the typed version:\n\n\\begin{code}\ndata _U\u2014\u2192*_ {n} : n \u22a2 \u2192 n \u22a2 \u2192 Set where\n reflU\u2014\u2192 : {M : n \u22a2} \u2192 M U\u2014\u2192* M\n transU\u2014\u2192 : {M M' M'' : n \u22a2}\n \u2192 M U\u2014\u2192 M' \u2192 M' U\u2014\u2192* M'' \u2192 M U\u2014\u2192* M''\n\\end{code}\n\n\\noindent We can erase a typed trace to yield an untyped trace. The\nreflexive case is straightforwards. In the transitive case, we may\nhave a step \\AgdaBound{p} that corresponds to an untyped or it may\ndisappear. We use case \\AgdaFunction{[\\_,\\_]} instead of\n\\AgdaFunction{map} this time. It is like \\AgdaFunction{map} but\ninstead of producing another sum it (in the non-dependent case that we\nare in) produces a result of an the same type in each case (in our\ncase \\AgdaFunction{erase} \\AgdaBound{M} \\AgdaDatatype{\u2014\u2192}\n\\AgdaFunction{erase} \\AgdaBound{M''}). In the first case we get an\nuntyped \\AgdaBound{step} and rest of the trace is handled by the\nrecursive call. In the second case \\AgdaBound{eq} is an equation\n\\AgdaFunction{erase} \\AgdaBound{M} \\AgdaDatatype{\u2261}\n\\AgdaFunction{erase} \\AgdaBound{M'} which we use to coerce the\nrecursive call of type \\AgdaFunction{erase} \\AgdaBound{M'}\n\\AgdaDatatype{\u2014\u2192} \\AgdaFunction{erase} \\AgdaBound{M''} into type\n\\AgdaFunction{erase} \\AgdaBound{M} \\AgdaDatatype{\u2014\u2192}\n\\AgdaFunction{erase} \\AgdaBound{M''} and the length of the trace is\nreduced:\n\n\\begin{code}\nerase\u2014\u2192* : \u2200{\u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}{\u0393 : CtxNf \u03a6}{t t' : \u0393 \u22a2Nf A}\n \u2192 t \u2014\u2192* t' \u2192 eraseNf t U\u2014\u2192* eraseNf t'\nerase\u2014\u2192* refl\u2014\u2192 = reflU\u2014\u2192\nerase\u2014\u2192* (trans\u2014\u2192 {M'' = M''} p q) =\n [ (\u03bb step \u2192 transU\u2014\u2192 step (erase\u2014\u2192* q))\n , (\u03bb eq \u2192 subst (_U\u2014\u2192* eraseNf M'') (sym eq) (erase\u2014\u2192* q))\n ] (erase\u2014\u2192 p)\n\\end{code}\n\n\\noindent Finally we can use \\AgdaFunction{run} to get an untyped\ntrace leading to a value, allowed steps permitting.\n\n\\begin{code}\nerase-run : \u2200 {A : \u2205 \u22a2Nf\u22c6 *} \u2192 \u2115 \u2192 (M : \u2205 \u22a2Nf A)\n \u2192 \u03a3 (0 \u22a2) \u03bb M' \u2192 (eraseNf M U\u2014\u2192* M') \u00d7 Maybe (UValue M')\nerase-run n M with run n M\n... | M' ,, p ,, mv = eraseNf M' ,, erase\u2014\u2192* p ,, Maybe.map eraseVal mv\n\\end{code}\n\n\n\\section{Examples}\n\\label{sec:examples}\n\nUsing only the facilities of System $F$ without the extensions of type\nfunctions and recursive types we can define natural numbers as Church\nNumerals:\n\\begin{code}\n\u2115\u1d9c : \u2200{\u03a6} \u2192 \u03a6 \u22a2Nf\u22c6 *\n\u2115\u1d9c = \u03a0 ((ne (` Z)) \u21d2 (ne (` Z) \u21d2 ne (` Z)) \u21d2 (ne (` Z)))\n\nZero\u1d9c : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf \u2115\u1d9c\nZero\u1d9c = \u039b (\u019b (\u019b (` (S Z))))\n\nSucc\u1d9c : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf \u2115\u1d9c \u21d2 \u2115\u1d9c\nSucc\u1d9c = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (ne (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n\nTwo\u1d9c : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf \u2115\u1d9c\nTwo\u1d9c = Succ\u1d9c \u00b7 (Succ\u1d9c \u00b7 Zero\u1d9c)\n\nFour\u1d9c : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf \u2115\u1d9c\nFour\u1d9c = Succ\u1d9c \u00b7 (Succ\u1d9c \u00b7 (Succ\u1d9c \u00b7 (Succ\u1d9c \u00b7 Zero\u1d9c)))\n\nTwoPlusTwo\u1d9c : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf \u2115\u1d9c\nTwoPlusTwo\u1d9c = Two\u1d9c \u00b7\u22c6 \u2115\u1d9c \u00b7 Two\u1d9c \u00b7 Succ\u1d9c\n\\end{code}\n\n\\noindent Using the full facilities of System $F_{\\omega\\mu}$ we can\ndefine natural numbers as Scott Numerals \\cite{plotkin}. We the $Z$\ncombinator instead of the $Y$ combinator as it works for both lazy and\nstrict languages.\n\n\\begin{code}\nG : \u2200{\u03a6} \u2192 \u03a6 ,\u22c6 * \u22a2Nf\u22c6 *\nG = \u03a0 (ne (` Z) \u21d2 (ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` Z))\n\nM : \u2200{\u03a6} \u2192 \u03a6 \u22a2Nf\u22c6 *\nM = \u03bc G\n\nN : \u2200{\u03a6} \u2192 \u03a6 \u22a2Nf\u22c6 *\nN = G [ M ]Nf\u22c6\n\nZero : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N\nZero = \u039b (\u019b (\u019b (` (S (Z )))))\n\nSucc : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N \u21d2 N\nSucc = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 wrap _ (` (S (S (T Z))))))))\n\nTwo : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N\nTwo = Succ \u00b7 (Succ \u00b7 Zero)\n\nFour : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N\nFour = Succ \u00b7 (Succ \u00b7 (Succ \u00b7 (Succ \u00b7 Zero)))\n\ncase : \u2200{\u03a6}{\u0393 : CtxNf \u03a6}\n \u2192 \u0393 \u22a2Nf N \u21d2 (\u03a0 (ne (` Z) \u21d2 (N \u21d2 ne (` Z)) \u21d2 ne (` Z)))\ncase = \u019b (\u039b (\u019b (\u019b (\n (` (S (S (T Z)))) \u00b7\u22c6 ne (` Z) \u00b7 (` (S Z)) \u00b7 (\u019b (` (S Z) \u00b7 unwrap (` Z)))))))\n\nZ-comb : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192\n \u0393 \u22a2Nf \u03a0 (\u03a0 (((ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` (S Z)) \u21d2 ne (` Z))\n \u21d2 ne (` (S Z)) \u21d2 ne (` Z)))\nZ-comb = \u039b (\u039b (\u019b (\u019b (` (S Z) \u00b7 \u019b (unwrap (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z))\n \u00b7 wrap (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))\n (\u019b (` (S Z) \u00b7 \u019b (unwrap (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z))))))\n\nPlus : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N \u21d2 N \u21d2 N\nPlus = \u019b (\u019b ((Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 (\u019b (\u019b ((((case \u00b7 ` Z) \u00b7\u22c6 N)\n \u00b7 ` (S (S (S Z)))) \u00b7 (\u019b (Succ \u00b7 (` (S (S Z)) \u00b7 ` Z)))))) \u00b7 ` (S Z)))\n\nTwoPlusTwo : \u2200{\u03a6}{\u0393 : CtxNf \u03a6} \u2192 \u0393 \u22a2Nf N\nTwoPlusTwo = (Plus \u00b7 Two) \u00b7 Two\n\\end{code}\n\n\\section{Scaling up from System $F_{\\omega\\mu}$ to Plutus Core}\n\\label{sec:real-world}\n\nThis formalisation forms the basis of a formalisation of Plutus\nCore. There are two key extensions.\n\n\\subsection{Higher kinded recursive types}\n\nIn this paper we used $\\mu : (* \\rightarrow *) \\rightarrow *$. This is\neasy to understand and makes it possible to express simple examples\ndirectly. This corresponds to the version of recursive types one\nmight use in ordinary System $F$. In System $F_\\omega$ we have a\ngreater degree of freedom. We have settled on an indexed version of\n$\\mu : ((k \\rightarrow *) \\rightarrow k \\rightarrow *) \\rightarrow k\n\\rightarrow *$ that supports the encoding of mutually defined\ndatatypes. This extension is straightforward in iso-recursive types,\nin equi-recursive it is not. We chose to present the restricted\nversion in this paper as it is simpler and sufficient to present our\nexamples. See the accompanying paper \\cite{compilation} for a more\ndetailed discussion of higher kinded recursive types.\n\n\\subsection{Integers, bytestrings and cryptographic operations}\n\nIn Plutus Core we also extend System $F_{\\omega\\mu}$ with integers and\nbytestrings and some cryptographic operations such as checking\nsignatures. Before thinking about how to add these features to our\nlanguage, there is a choice to be made when modelling integers and\nbytestrings and cryptographic operations in Agda about whether we\nconsider them internal or external to our model. We are modelling the\nHaskell implementation of Plutus Core which uses the Haskell\nbytestring library. We chose to model the Plutus Core implementation\nalone and consider bytestrings as an external black box. We assume\n(i.e. postulate in Agda) an interface given as a type for bytestrings\nand various operations such as take, drop, append etc. We can also\nmake clear our expectations of this interface by assuming\n(postulating) some properties such as that append is\nassociative. Using pragmas in Agda we can ensure that when we compile\nour Agda program to Haskell these opaque bytestring operations are\ncompiled to the real operations of the Haskell bytestring library. We\nhave taken a slightly different approach with integers as Agda and\nHaskell have native support for integers and Agda integers are already\ncompiled to Haskell integers by default so we just make use of this\nbuiltin support. Arguably this brings integers inside our model. One\ncould also treat integers as a blackbox. We treat cryptographic\noperations as a blackbox as we do with bytestrings.\n\nTo add integers and bytestrings to the System $F_{\\omega\\mu}$ we add\ntype constants as types and term constants as terms. The type of a\nterm constant is a type constant. This ensures that we can have term\nvariables whose type is type constant but not term constants whose\ntype is a type variable. To support the operations for integers and\nbytestrings we add a builtin constructor to the term language,\nsignatures for each operation, and a semantics for builtins that\napplies the appropriate underlying function to its arguments. The\nunderlying function is postulated in Agda and when compiled to Haskell\nit runs the appropriate native Haskell function or library\nfunction. Note that the cryptographic functions are operations on\nbytestrings.\n\nAdding this functionality did not pose any particular formalisation\nchallenges except for the fact it was quite a lot of work. However,\ncompiling our implementation of builtins to Haskell did trigger\nseveral bugs in Agda's GHC backend which were rapidly diagnosed and\nfixed by the Agda developers.\n\n\\subsection{Using our implementation for testing}\n\nAs we can compile our Agda Plutus Core interpreter to Haskell we can\ntest the production Haskell Plutus Core interpreter against it. We\nmake use of the production system's parser and pretty printer which we\nimport as a Haskell library and use the same libraries for bytestrings\nand cryptographic functions. The parser produces intrinsically typed\nterms which we scope check and convert to a representation with de\nBruijn indices. We cannot currently use the intrinsically typed\nimplementation we describe in this paper directly as we must type\ncheck terms first and formalising a type checker is future\nwork. Instead we have implemented a separate extrinsically typed\nversion that we use for testing. After evaluation we convert the de\nBruijn syntax back to a named syntax and pretty print the output. We\nhave proven that for any well-typed term the intrinsic and extrinsic\nversions give the same results after erasure.\n\n\\section*{Acknowledgements}\n\nWe thank the anonymous reviewers for their helpful comments and\ninsightful constructive criticism. We thank IOHK for their support of\nthis work. We thank our colleagues Marko Dimja\u0161evi\u0107, Kenneth\nMacKenzie, and Michael Peyton Jones for helpful comments on an\nmultiple drafts. The first author would like to James McKinna for\nspending an afternoon explaining pure type systems and Guillaume\nAllais, Apostolis Xekoukoulotakis and Ulf Norell for help with\ndiagnosing and fixing bugs that we encountered in Agda's GHC backend\nin the course of writing this paper.\n\n%functional programs on a blockchain. In addition to the features\n%presented here Plutus Core also has intrinsically sized integer and\n%bytestrings and a more liberal version of recursive types.\n%\n%The extension to intrinsically sized integers and bytestrings requires\n%quite a bit of infrastructure but the liberalisation of recursive\n%types is almost trivial in this setting.\n%\n\\bibliography{bibliography}\n\\end{document}\n\nSome additional proofs added after publication and not shown in the paper\n\n\\begin{code}\nopen import Relation.Nullary\n\n-- a value can make no progress\n\nval : \u2200{\u03a6 \u0393}{\u03c3 : \u03a6 \u22a2Nf\u22c6 *}{t : \u0393 \u22a2Nf \u03c3} \u2192 Value t \u2192 \u00ac (\u03a3 (\u0393 \u22a2Nf \u03c3) (t \u2014\u2192_))\nval (V-\u019b p) ()\nval (V-\u039b p) (.(\u039b _) ,, \u03be-\u039b q) = val p (_ ,, q)\nval (V-wrap p) (.(wrap _ _) ,, \u03be-wrap q) = val p (_ ,, q)\n\n-- exclusive or\n_xor_ : Set \u2192 Set \u2192 Set\nA xor B = (A \u228e B) \u00d7 \u00ac (A \u00d7 B)\n\n-- progress can be upgraded to an xor using val\n\nprogress-xor : {\u03c3 : \u2205 \u22a2Nf\u22c6 *}(t : \u2205 \u22a2Nf \u03c3) \u2192 Value t xor \u03a3 (\u2205 \u22a2Nf \u03c3) (t \u2014\u2192_)\nprogress-xor t = progress _ t ,, \u03bb{(v ,, p) \u2192 val v p}\n\n-- The reduction rules are deterministic\n\ndet : \u2200{\u03a6 \u0393}{\u03c3 : \u03a6 \u22a2Nf\u22c6 *}{t t' t'' : \u0393 \u22a2Nf \u03c3}\n \u2192 (p : t \u2014\u2192 t')(q : t \u2014\u2192 t'') \u2192 t' \u2261 t''\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2081 q) = cong (_\u00b7 _) (det p q)\ndet (\u03be-\u00b7\u2081 p) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val w (_ ,, p))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2081 q) = \u22a5-elim (val v (_ ,, q))\ndet (\u03be-\u00b7\u2082 v p) (\u03be-\u00b7\u2082 w q) = cong (_ \u00b7_) (det p q)\ndet (\u03be-\u00b7\u2082 v p) (\u03b2-\u019b w) = \u22a5-elim (val w (_ ,, p))\ndet (\u03be-\u00b7\u22c6 p) (\u03be-\u00b7\u22c6 q) = cong (_\u00b7\u22c6 _) (det p q)\ndet (\u03b2-\u039b v) (\u03be-\u00b7\u22c6 q) = \u22a5-elim (val (V-\u039b v) (_ ,, q))\ndet (\u03be-\u00b7\u22c6 p) (\u03b2-\u039b v) = \u22a5-elim (val (V-\u039b v) (_ ,, p))\ndet (\u03be-\u039b p) (\u03be-\u039b q) = cong \u039b (det p q)\ndet (\u03b2-\u019b v) (\u03be-\u00b7\u2082 w q) = \u22a5-elim (val v (_ ,, q))\ndet (\u03b2-\u019b v) (\u03b2-\u019b w) = refl\ndet (\u03b2-\u039b v) (\u03b2-\u039b w) = refl\ndet (\u03b2-wrap p) (\u03b2-wrap q) = refl\ndet (\u03b2-wrap p) (\u03be-unwrap q) = \u22a5-elim (val (V-wrap p) (_ ,, q))\ndet (\u03be-unwrap p) (\u03b2-wrap q) = \u22a5-elim (val (V-wrap q) (_ ,, p))\ndet (\u03be-unwrap p) (\u03be-unwrap q) = cong unwrap (det p q)\ndet (\u03be-wrap p) (\u03be-wrap q) = cong (wrap _) (det p q)\n\\end{code}\n","avg_line_length":39.1445475638,"max_line_length":270,"alphanum_fraction":0.6287482292} +{"size":7108,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\r\ntitle : \"FreshId: Generation of fresh names\"\r\npermalink : \/FreshId\r\n---\r\n\r\n\r\nGeneration of fresh names, where names are strings.\r\nEach name has a base (a string not ending in a prime)\r\nand a suffix (a sequence of primes).\r\n\r\nBased on an earlier version fixed by James McKinna.\r\n\r\n\\begin{code}\r\nmodule FreshId where\r\n\r\nimport Relation.Binary.PropositionalEquality as Eq\r\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\r\nopen import Data.Empty using (\u22a5; \u22a5-elim)\r\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr;\r\n reverse; replicate; length)\r\nopen import Data.List.Properties using (reverse-involutive)\r\nopen import Data.List.All using (All; []; _\u2237_)\r\nopen import Data.Nat using (\u2115; zero; suc; _+_; _\u2238_; _\u2264_; _\u2294_)\r\nopen import Data.Nat.Properties using (\u2264-refl; \u2264-trans; m\u2264m\u2294n; n\u2264m\u2294n; 1+n\u2270n)\r\nopen import Data.Bool using (Bool; true; false; T)\r\nopen import Data.Char using (Char)\r\nimport Data.Char as Char using (_\u225f_)\r\nopen import Data.String using (String; toList; fromList; _\u225f_;\r\n toList\u2218fromList; fromList\u2218toList)\r\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082; \u2203; \u2203-syntax)\r\n renaming (_,_ to \u27e8_,_\u27e9)\r\nopen import Data.Unit using (\u22a4; tt)\r\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\r\nopen import Function using (_\u2218_)\r\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\r\nopen import Relation.Nullary.Negation using (\u00ac?)\r\n-- open import Relation.Nullary.Decidable using (\u230a_\u230b)\r\nopen import Relation.Unary using (Decidable)\r\nimport Data.Nat as Nat\r\nimport Data.String as String\r\nimport Collections\r\n\r\npattern [_] x = x \u2237 []\r\npattern [_,_] x y = x \u2237 y \u2237 []\r\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\r\npattern [_,_,_,_] x y z w = x \u2237 y \u2237 z \u2237 w \u2237 []\r\n\\end{code}\r\n\r\n## DropWhile and TakeWhile for decidable predicates\r\n\r\n\\begin{code}\r\nHead : \u2200 {A : Set} \u2192 (A \u2192 Set) \u2192 List A \u2192 Set\r\nHead P [] = \u22a5\r\nHead P (x \u2237 xs) = P x\r\n\r\nmodule TakeDrop {A : Set} {P : A \u2192 Set} (P? : Decidable P) where\r\n\r\n takeWhile : List A \u2192 List A\r\n takeWhile [] = []\r\n takeWhile (x \u2237 xs) with P? x\r\n ... | yes _ = x \u2237 takeWhile xs\r\n ... | no _ = []\r\n\r\n dropWhile : List A \u2192 List A\r\n dropWhile [] = []\r\n dropWhile (x \u2237 xs) with P? x\r\n ... | yes _ = dropWhile xs\r\n ... | no _ = x \u2237 xs\r\n\r\n takeWhile-lemma : \u2200 (xs : List A) \u2192 All P (takeWhile xs)\r\n takeWhile-lemma [] = []\r\n takeWhile-lemma (x \u2237 xs) with P? x\r\n ... | yes px = px \u2237 takeWhile-lemma xs\r\n ... | no _ = []\r\n\r\n dropWhile-lemma : \u2200 (xs : List A) \u2192 \u00ac Head P (dropWhile xs)\r\n dropWhile-lemma [] = \u03bb()\r\n dropWhile-lemma (x \u2237 xs) with P? x\r\n ... | yes _ = dropWhile-lemma xs\r\n ... | no \u00acpx = \u00acpx\r\n\\end{code}\r\n\r\n## Abstract operators prefix, suffix, and make\r\n\r\n\\begin{code}\r\nId : Set\r\nId = String\r\n\r\nopen Collections (Id) (String._\u225f_)\r\n\r\nmodule IdBase\r\n (P : Char \u2192 Set)\r\n (P? : \u2200 (c : Char) \u2192 Dec (P c))\r\n (to\u2115 : List Char \u2192 \u2115)\r\n (from\u2115 : \u2115 \u2192 List Char)\r\n (to\u2115\u2218from\u2115 : \u2200 (n : \u2115) \u2192 to\u2115 (from\u2115 n) \u2261 n)\r\n (from\u2115\u2218to\u2115 : \u2200 (s : List Char) \u2192 (All P s) \u2192 from\u2115 (to\u2115 s) \u2261 s)\r\n where\r\n\r\n open TakeDrop\r\n\r\n isPrefix : String \u2192 Set\r\n isPrefix s = \u00ac Head P (reverse (toList s))\r\n\r\n Prefix : Set\r\n Prefix = \u2203[ s ] (isPrefix s)\r\n\r\n body : Prefix \u2192 String\r\n body = proj\u2081\r\n\r\n prop : (p : Prefix) \u2192 isPrefix (body p)\r\n prop = proj\u2082\r\n\r\n make : Prefix \u2192 \u2115 \u2192 Id\r\n make p n = fromList (toList (body p) ++ from\u2115 n)\r\n\r\n prefixS : Id \u2192 String\r\n prefixS = fromList \u2218 reverse \u2218 dropWhile P? \u2218 reverse \u2218 toList\r\n\r\n prefixS-lemma : \u2200 (x : Id) \u2192 isPrefix (prefixS x)\r\n prefixS-lemma x\r\n rewrite toList\u2218fromList ((reverse \u2218 dropWhile P? \u2218 reverse \u2218 toList) x)\r\n | reverse-involutive ((dropWhile P? \u2218 reverse \u2218 toList) x)\r\n = dropWhile-lemma P? ((reverse \u2218 toList) x)\r\n\r\n prefix : Id \u2192 Prefix\r\n prefix x = \u27e8 prefixS x , prefixS-lemma x \u27e9\r\n\r\n suffix : Id \u2192 \u2115\r\n suffix = length \u2218 takeWhile P? \u2218 reverse \u2218 toList\r\n\r\n _\u225fPr_ : \u2200 (p q : Prefix) \u2192 Dec (body p \u2261 body q)\r\n p \u225fPr q = (body p) String.\u225f (body q)\r\n\r\n prefix-lemma : \u2200 (p : Prefix) (n : \u2115) \u2192 prefix (make p n) \u2261 p\r\n prefix-lemma p n = {! h (f p)!}\r\n where\r\n f : Prefix \u2192 List Char\r\n f = reverse \u2218 toList \u2218 body\r\n g : List Char \u2192 Prefix\r\n g s = \u27e8 (fromList \u2218 reverse) s , prop p \u27e9\r\n h : \u2200 (s : List Char) \u2192 prefix (make (g s) n) \u2261 g s\r\n h = ?\r\n\r\n suffix-lemma : \u2200 (p : Prefix) (n : \u2115) \u2192 suffix (make p n) \u2261 n\r\n suffix-lemma = {!!}\r\n\r\n make-lemma : \u2200 (x : Id) \u2192 make (prefix x) (suffix x) \u2261 x\r\n make-lemma = {!!}\r\n\\end{code}\r\n\r\n## Main lemmas\r\n\r\n\\begin{code}\r\nmodule IdLemmas\r\n (Prefix : Set)\r\n (prefix : Id \u2192 Prefix)\r\n (suffix : Id \u2192 \u2115)\r\n (make : Prefix \u2192 \u2115 \u2192 Id)\r\n (body : Prefix \u2192 String)\r\n (_\u225fPr_ : \u2200 (p q : Prefix) \u2192 Dec (body p \u2261 body q))\r\n (prefix-lemma : \u2200 (p : Prefix) (n : \u2115) \u2192 prefix (make p n) \u2261 p)\r\n (suffix-lemma : \u2200 (p : Prefix) (n : \u2115) \u2192 suffix (make p n) \u2261 n)\r\n (make-lemma : \u2200 (x : Id) \u2192 make (prefix x) (suffix x) \u2261 x)\r\n where\r\n\r\n bump : Prefix \u2192 Id \u2192 \u2115\r\n bump p x with p \u225fPr prefix x\r\n ... | yes _ = suc (suffix x)\r\n ... | no _ = zero\r\n\r\n next : Prefix \u2192 List Id \u2192 \u2115\r\n next p = foldr _\u2294_ 0 \u2218 map (bump p)\r\n\r\n fresh : Id \u2192 List Id \u2192 Id\r\n fresh x xs = make p (next p xs)\r\n where\r\n p = prefix x\r\n\r\n \u2294-lemma : \u2200 {p w xs} \u2192 w \u2208 xs \u2192 bump p w \u2264 next p xs\r\n \u2294-lemma {p} {_} {_ \u2237 xs} here = m\u2264m\u2294n _ (next p xs)\r\n \u2294-lemma {p} {w} {x \u2237 xs} (there x\u2208) =\r\n \u2264-trans (\u2294-lemma {p} {w} x\u2208) (n\u2264m\u2294n (bump p x) (next p xs))\r\n\r\n bump-lemma : \u2200 {p n} \u2192 bump p (make p n) \u2261 suc n\r\n bump-lemma {p} {n}\r\n with p \u225fPr prefix (make p n)\r\n ... | yes eqn rewrite suffix-lemma p n = refl\r\n ... | no p\u2262 rewrite prefix-lemma p n = \u22a5-elim (p\u2262 refl)\r\n\r\n fresh-lemma : \u2200 {w x xs} \u2192 w \u2208 xs \u2192 w \u2262 fresh x xs\r\n fresh-lemma {w} {x} {xs} w\u2208 = h {prefix x}\r\n where\r\n h : \u2200 {p} \u2192 w \u2262 make p (next p xs)\r\n h {p} refl\r\n with \u2294-lemma {p} {make p (next p xs)} {xs} w\u2208\r\n ... | leq rewrite bump-lemma {p} {next p xs} = 1+n\u2270n leq\r\n\\end{code}\r\n\r\n## Test cases\r\n\r\n\\begin{code}\r\nprime : Char\r\nprime = '\u2032'\r\n\r\nisPrime : Char \u2192 Set\r\nisPrime c = c \u2261 prime\r\n\r\nisPrime? : (c : Char) \u2192 Dec (isPrime c)\r\nisPrime? c = c Char.\u225f prime\r\n\r\nto\u2115 : List Char \u2192 \u2115\r\nto\u2115 s = length s\r\n\r\nfrom\u2115 : \u2115 \u2192 List Char\r\nfrom\u2115 n = replicate n prime\r\n\r\nto\u2115\u2218from\u2115 : \u2200 (n : \u2115) \u2192 to\u2115 (from\u2115 n) \u2261 n\r\nto\u2115\u2218from\u2115 = {!!}\r\n\r\nfrom\u2115\u2218to\u2115 : \u2200 (s : List Char) \u2192 All isPrime s \u2192 from\u2115 (to\u2115 s) \u2261 s\r\nfrom\u2115\u2218to\u2115 = {!!}\r\n\r\nopen IdBase (isPrime) (isPrime?) (to\u2115) (from\u2115) (to\u2115\u2218from\u2115) (from\u2115\u2218to\u2115)\r\nopen IdLemmas (Prefix) (prefix) (suffix) (make) (body) (_\u225fPr_)\r\n (prefix-lemma) (suffix-lemma) (make-lemma)\r\n\r\nx0 = \"x\"\r\nx1 = \"x\u2032\"\r\nx2 = \"x\u2032\u2032\"\r\nx3 = \"x\u2032\u2032\u2032\"\r\ny0 = \"y\"\r\ny1 = \"y\u2032\"\r\nzs0 = \"zs\"\r\nzs1 = \"zs\u2032\"\r\nzs2 = \"zs\u2032\u2032\"\r\n\r\n_ : fresh x0 [ x0 , x1 , x2 , zs2 ] \u2261 x3\r\n_ = refl\r\n\r\n-- fresh \"x\" [ \"x\" , \"x\u2032\" , \"x\u2032\u2032\" , \"y\" ] \u2261 \"x\u2032\u2032\u2032\"\r\n\r\n_ : fresh zs0 [ x0 , x1 , x2 , zs1 ] \u2261 zs2\r\n_ = refl\r\n\\end{code}\r\n\r\n\r\n\r\n","avg_line_length":28.546184739,"max_line_length":77,"alphanum_fraction":0.5296848621} +{"size":31342,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Inference: Bidirectional type inference\"\npermalink : \/Inference\/\n---\n\n\\begin{code}\nmodule plfa.Inference where\n\\end{code}\n\nSo far in our development, type derivations for the corresponding\nterm have been provided by fiat.\nIn Chapter [Lambda]({{ site.baseurl }}{% link out\/plfa\/Lambda.md %})\ntype derivations were given separately from the term, while\nin Chapter [DeBruijn]({{ site.baseurl }}{% link out\/plfa\/DeBruijn.md %})\nthe type derivation was inherently part of the term.\n\nIn practice, one often writes down a term with a few decorations and\napplies an algorithm to _infer_ the corresponding type derivation.\nIndeed, this is exactly what happens in Agda: we specify the types for\ntop-level function declarations, and the remaining type information is\ninferred from this. The style of inference used is descended from an\nalgorithm called _bidirectional_ type inference, which will be\npresented in this chapter.\n\nThis chapter ties our previous developments together. We begin with\na term with some type annotations, quite close to the raw terms of\nChapter [Lambda]({{ site.baseurl }}{% link out\/plfa\/Lambda.md %}),\nand from it we compute a term with inherent types, in the style of\nChapter [DeBruijn]({{ site.baseurl }}{% link out\/plfa\/DeBruijn.md %}).\n\n## Introduction: Inference rules as algorithms {#algorithms}\n\nIn the calculus we have considered so far, a term may have more than\none type. For example,\n\n (\u019b x \u21d2 x) \u2982 (A \u21d2 A)\n\nfor _every_ type `A`. We start by considering a small language for\nlambda terms where every term has a unique type. All we need do\nis decorate each abstraction term with the type of its argument.\nThis gives us the grammar:\n\n L, M, N ::= decorated terms\n x variable\n \u019b x \u2982 A \u21d2 N abstraction (decorated)\n L \u00b7 M application\n\nEach of the associated type rules can be read as an algorithm for\ntype checking. For each typing judgment, we label each position\nas either an _input_ or an _output_.\n\nFor the judgment\n\n \u0393 \u220b x \u2982 A\n\nwe take the context `\u0393` and the variable `x` as inputs, and the\ntype `A` as output. Consider the rules:\n\n ----------------- Z\n \u0393 , x \u2982 A \u220b x \u2982 A\n\n \u0393 \u220b y \u2982 B\n ----------------- S\n \u0393 , x \u2982 A \u220b y \u2982 B\n\nFrom the inputs we can determine which rule applies: if the last\nvariable in the context matches the given variable then the first\nrule applies, else the second. (For de Bruijn indices, it is even\neasier: zero matches the first rule and successor the second.)\nFor the first rule, the output type can be read off as the last\ntype in the input context. For the second rule, the inputs of the\nconclusion determine the inputs of the hypothesis, and the ouptut\nof the hypothesis determines the output of the conclusion.\n\nFor the judgment\n\n \u0393 \u22a2 M \u2982 A\n\nwe take the context `\u0393` and term `M` as inputs, and the type `A`\nas ouput. Consider the rules:\n\n \u0393 \u220b x \u2982 A\n -----------\n \u0393 \u22a2 ` x \u2982 A\n\n \u0393 , x \u2982 A \u22a2 N \u2982 B\n ---------------------------\n \u0393 \u22a2 (\u019b x \u2982 A \u21d2 N) \u2982 (A \u21d2 B)\n\n \u0393 \u22a2 L \u2982 A \u21d2 B\n \u0393 \u22a2 M \u2982 A\u2032\n A \u2261 A\u2032\n -------------\n \u0393 \u22a2 L \u00b7 M \u2982 B\n\nThe term input determines which rule applies: variables use the first\nrule, abstractions the second, and applications the third. In such a\nsituation, we say the rules are _syntax directed_. For the\nvariable rule, the inputs of the conclusion determine the inputs of\nthe hypothesis, and the output of the hypothesis determines the output\nof the conclusion. Same for the abstraction rule \u2014 the bound variable\nand argument type of the abstraction are carried into the context of\nthe hypothesis, and this is why we added the argument type to the\nabstraction. For the application rule, we add a third hypothesis to\ncheck whether domain of the function matches the type of the argument;\nthis judgment is decidable when both types are given as inputs. The\ninputs of the conclusion determine the inputs of the first two\nhypotheses, the outputs of the first two hypotheses determine the\ninputs of the third hypothesis, and the output of the first hypothesis\ndetermines the output of the conclusion.\n\nConverting the above to an algorithm is straightforwart, as is adding\nnaturals and fixpoint. We omit the details. Instead, we consider a\ndetailed description of an approach that requires less obtrusive\ndecoration. The idea is to break the normal typing judgment into two\njudgments, one that produces the type as an output (as above), and\nanother that takes it as an input.\n\n\n## Synthesising and inheriting types\n\nIn addition to the lookup judgment for variables, which will remain\nas before, we now have two judgments for the type of the term.\n\n \u0393 \u22a2 M \u2191 A\n \u0393 \u22a2 M \u2193 A\n\nThe first of these _synthesises_ the type of a term, as before,\nwhile the second _inherits_ the type. In the first, the context\nand term are inputs and the type is an output, while in the\nsecond, all three of the context, term, and type are inputs.\n\nWhich terms use synthesis and which inheritance? Our approach will be\nthat the main term in a _deconstructor_ are typed via synthesis while\n_constructors_ a typed via inheritance. For instance, the function in\nan application is typed via synthesis, but an abstraction is typed via\ninheritance. The inherited type in an abstraction term serves the\nsame purpose as the argument type decoration of the previous section.\n\nTerms that deconstruct a value of a type always have a main term\n(supplying an argument of the required type) and often have\nside-terms. For application, the main term supplies the function and\nthe side term supplies the argument. For case terms, the main term\nsupplies a natural and the side terms are the two branches. In a\ndeconstructor, the main term will be typed using synthesis but the\nside terms will be typed using inheritance. As we will see, this\nleads naturally to an application as a whole being typed by synthesis,\nwhile a case term as a whole will be typed by inheritance.\nVariables are naturally typed by synthesis, since we can look up\nthe type in the input context. Fixed points will be naturally\ntyped by inheritance.\n\nIn order to get a syntax-directed type system we break terms into two\nkinds, `Term\u207a` and `Term\u207b, which are typed by synthesis and\ninheritance, respectively. At some points, we may expect a subterm to\nbe typed by synthesis when in fact it is typed by inheritance, or\nvice-versa, and this gives rise to two new term forms.\n\nFor instance, we said above that the argument of an application is\ntyped by inheritance and that variables are typed by synthesis, giving\na mismatch if the argument of an application is a variable. Hence, we\nneed a way to treat a synthesized term as if it is inherited. We\nintroduce a new term form, `M \u2191` for this purpose. The typing judgment\nchecks that the inherited and synthesised types match.\n\nSimilarly, we said above that the function of an application is typed\nby synthesis and that abstractions are typed by inheritance, giving a\nmismatch if the function of an application is a variable. Hence, we\nneed a way to treat an inherited term as if it is synthesised. We\nintroduce a new term form `M \u2193 A` for this purpose. The typing\njudgment returns `A` as the synthesized type of the term as a whole,\nas well as using it as the inherited type for `M`.\n\nThe term form `M \u2193 A` represents the only place terms need to\nbe decorated with types. It only appears when switching from\nsynthesis to inheritance, that is, when a term that _deconstructs_\na value of a type contains a term that _constructs_ a value of a\ntype, in other words, a place where a \u03b2 reduction will occur.\nTypically, we will find that such declarations are only required\non top level declarations.\n\nWe can extract the grammar for terms from the above:\n\n L\u207a, M\u207a, N\u207a ::= terms with synthesized type\n x variable\n L\u207a \u00b7 M- application\n M\u207b \u2193 A switch to inherited\n\n L\u207b, M\u207b, N\u207b ::= terms with inherited type\n \u019b x \u21d2 N abstraction\n `zero zero\n `suc M\u207b successor\n case L\u207a [zero\u21d2 M\u207b |suc x \u21d2 N\u207b ] case\n \u03bc x \u21d2 N fixpoint\n M \u2191 switch to synthesized\n\nWith the grammar in hand, we can begin the formal development.\n\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.List using (List; []; _\u2237_; map; foldr; filter; length)\nopen import Data.Nat using (\u2115; zero; suc; _+_)\nopen import Data.String using (String; _\u225f_; _++_)\nopen import Data.Product\n using (_\u00d7_; proj\u2081; proj\u2082; \u2203; \u2203-syntax)\n renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Function using (_\u2218_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Negation using (\u00ac?)\n\npattern [_] w = w \u2237 []\npattern [_,_] w x = w \u2237 x \u2237 []\npattern [_,_,_] w x y = w \u2237 x \u2237 y \u2237 []\npattern [_,_,_,_] w x y z = w \u2237 x \u2237 y \u2237 z \u2237 []\n\\end{code}\n\nOnce we have a type derivation, it will be easy to construct\nfrom it the inherently typed representation. In order that we\ncan compare with our previous development, we import\nmodule `pfla.DeBruijn`.\n\n\\begin{code}\nopen import plfa.DeBruijn as DB using (Type; `\u2115; _\u21d2_)\n\\end{code}\n\nThe phrase `as DB` allows us to refer to definitions\nfrom that module as, for instance, `DB._\u22a2_`, which is\ninvoked as `\u0393 DB.\u22a2 A`, where `\u0393` has type\n`DB.Context` and `A` has type `DB.Type`. We also import\n`Type` and its constructors directly, so the latter may\nalso be referred to as just `Type`.\n\n\n## Syntax\n\nFirst, we get all our infix declarations out of the way.\nWe list separately operators for judgments and terms.\n\n\\begin{code}\ninfix 4 _\u220b_\u2982_\ninfix 4 _\u22a2_\u2191_\ninfix 4 _\u22a2_\u2193_\ninfixl 5 _,_\u2982_\n\ninfix 5 \u019b_\u21d2_\ninfix 5 \u03bc_\u21d2_\ninfix 6 _\u2191\ninfix 6 _\u2193_\ninfixl 7 _\u00b7_\ninfix 8 `suc_\ninfix 9 `_\n\\end{code}\n\nIdentifiers are as before.\n\\begin{code}\nId : Set\nId = String\n\\end{code}\n\nAnd so are contexts. (Recall that `Type` is imported from\n[DeBruijn]({{ site.baseurl }}{% link out\/plfa\/DeBruijn.md %}).)\n\\begin{code}\ndata Context : Set where\n \u2205 : Context\n _,_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Context\n\\end{code}\n\nThe syntax of terms is defined by mutual recursion.\nWe use `Term\u207a` and `Term\u207b`\nfor terms with synthesized and inherited types, respectively.\nNote the inclusion of the switching forms,\n`M \u2193 A` and `M \u2191`.\n\\begin{code}\ndata Term\u207a : Set\ndata Term\u207b : Set\n\ndata Term\u207a where\n `_ : Id \u2192 Term\u207a\n _\u00b7_ : Term\u207a \u2192 Term\u207b \u2192 Term\u207a\n _\u2193_ : Term\u207b \u2192 Type \u2192 Term\u207a\n\ndata Term\u207b where\n \u019b_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n `zero : Term\u207b\n `suc_ : Term\u207b \u2192 Term\u207b\n `case_[zero\u21d2_|suc_\u21d2_] : Term\u207a \u2192 Term\u207b \u2192 Id \u2192 Term\u207b \u2192 Term\u207b\n \u03bc_\u21d2_ : Id \u2192 Term\u207b \u2192 Term\u207b\n _\u2191 : Term\u207a \u2192 Term\u207b\n\\end{code}\nThe choice as to whether each term is synthesized or\ninherited follows the discussion above, and can be read\noff from the preceding (informal) grammar. Main terms in\ndeconstructors synthesise, constructors and side terms\nin deconstructors inherit.\n\n## Example terms\n\nWe can recreate the examples from preceding chapters.\nFirst, computing two plus two on naturals.\n\\begin{code}\ntwo : Term\u207b\ntwo = `suc (`suc `zero)\n\nplus : Term\u207a\nplus = (\u03bc \"p\" \u21d2 \u019b \"m\" \u21d2 \u019b \"n\" \u21d2\n `case (` \"m\") [zero\u21d2 ` \"n\" \u2191\n |suc \"m\" \u21d2 `suc (` \"p\" \u00b7 (` \"m\" \u2191) \u00b7 (` \"n\" \u2191) \u2191) ])\n \u2193 `\u2115 \u21d2 `\u2115 \u21d2 `\u2115\n\n2+2 : Term\u207a\n2+2 = plus \u00b7 two \u00b7 two\n\\end{code}\nThe only change is to decorate with down and up arrows as required.\nThe only type decoration required is for `plus`.\n\nNext, computing two plus two with Church numerals.\n\\begin{code}\nCh : Type\nCh = (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115\n\ntwo\u1d9c : Term\u207b\ntwo\u1d9c = (\u019b \"s\" \u21d2 \u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 (` \"z\" \u2191) \u2191) \u2191)\n\nplus\u1d9c : Term\u207a\nplus\u1d9c = (\u019b \"m\" \u21d2 \u019b \"n\" \u21d2 \u019b \"s\" \u21d2 \u019b \"z\" \u21d2\n ` \"m\" \u00b7 (` \"s\" \u2191) \u00b7 (` \"n\" \u00b7 (` \"s\" \u2191) \u00b7 (` \"z\" \u2191) \u2191) \u2191)\n \u2193 Ch \u21d2 Ch \u21d2 Ch\n\nsuc\u1d9c : Term\u207b\nsuc\u1d9c = \u019b \"x\" \u21d2 `suc (` \"x\" \u2191)\n\n2+2\u1d9c : Term\u207a\n2+2\u1d9c = plus\u1d9c \u00b7 two\u1d9c \u00b7 two\u1d9c \u00b7 suc\u1d9c \u00b7 `zero\n\\end{code}\nThe only type decoration required is for `plus\u1d9c`. One is not even\nrequired for `suc\u1d9c`, which inherits its type as an argument of `plus\u1d9c`.\n\n## Bidirectional type checking\n\nThe typing rules for variables are as in\n[Lambda]({{ site.baseurl }}{% link out\/plfa\/Lambda.md %}).\n\\begin{code}\ndata _\u220b_\u2982_ : Context \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 x A}\n --------------------\n \u2192 \u0393 , x \u2982 A \u220b x \u2982 A\n\n S : \u2200 {\u0393 x y A B}\n \u2192 x \u2262 y\n \u2192 \u0393 \u220b x \u2982 A\n -----------------\n \u2192 \u0393 , y \u2982 B \u220b x \u2982 A\n\\end{code}\n\nAs with syntax, the judgments for synthesizing\nand inheriting types are mutually recursive.\n\\begin{code}\ndata _\u22a2_\u2191_ : Context \u2192 Term\u207a \u2192 Type \u2192 Set\ndata _\u22a2_\u2193_ : Context \u2192 Term\u207b \u2192 Type \u2192 Set\n\ndata _\u22a2_\u2191_ where\n\n Ax : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x \u2982 A\n -----------\n \u2192 \u0393 \u22a2 ` x \u2191 A\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L \u2191 A \u21d2 B\n \u2192 \u0393 \u22a2 M \u2193 A\n -------------\n \u2192 \u0393 \u22a2 L \u00b7 M \u2191 B\n\n \u22a2\u2193 : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M \u2193 A\n ---------------\n \u2192 \u0393 \u22a2 (M \u2193 A) \u2191 A\n\ndata _\u22a2_\u2193_ where\n\n \u22a2\u019b : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 B\n -------------------\n \u2192 \u0393 \u22a2 \u019b x \u21d2 N \u2193 A \u21d2 B\n\n \u22a2zero : \u2200 {\u0393}\n --------------\n \u2192 \u0393 \u22a2 `zero \u2193 `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M \u2193 `\u2115\n ---------------\n \u2192 \u0393 \u22a2 `suc M \u2193 `\u2115\n\n \u22a2case : \u2200 {\u0393 L M x N A}\n \u2192 \u0393 \u22a2 L \u2191 `\u2115\n \u2192 \u0393 \u22a2 M \u2193 A\n \u2192 \u0393 , x \u2982 `\u2115 \u22a2 N \u2193 A\n -------------------------------------\n \u2192 \u0393 \u22a2 `case L [zero\u21d2 M |suc x \u21d2 N ] \u2193 A\n\n \u22a2\u03bc : \u2200 {\u0393 x N A}\n \u2192 \u0393 , x \u2982 A \u22a2 N \u2193 A\n -----------------\n \u2192 \u0393 \u22a2 \u03bc x \u21d2 N \u2193 A\n\n \u22a2\u2191 : \u2200 {\u0393 M A B}\n \u2192 \u0393 \u22a2 M \u2191 A\n \u2192 A \u2261 B\n -------------\n \u2192 \u0393 \u22a2 (M \u2191) \u2193 B\n\\end{code}\nWe follow the same convention as\nChapter [Lambda]({{ site.baseurl }}{% link out\/plfa\/Lambda.md %}),\nprefacing the constructor with `\u22a2` to derive the name of the\ncorresponding type rule.\n\nThe most interesting rules are those for `\u22a2\u2191` and `\u22a2\u2193`.\nThe former both passes the type decoration as the inherited type and returns\nit as the synthesised type. The latter takes the synthesised type and the\ninherited type and confirms they are identical. (It should remind you of\nthe equality test in the application rule in the first\n[section]({{ site.baseurl }}{% link out\/plfa\/Inference.md %}\/#algorithms).)\n\n\n## Type equality\n\nThe rule for `M \u2191` requires the ability to decide whether two types\nare equal. It is straightforward to code.\n\\begin{code}\n_\u225fTp_ : (A B : Type) \u2192 Dec (A \u2261 B)\n`\u2115 \u225fTp `\u2115 = yes refl\n`\u2115 \u225fTp (A \u21d2 B) = no \u03bb()\n(A \u21d2 B) \u225fTp `\u2115 = no \u03bb()\n(A \u21d2 B) \u225fTp (A\u2032 \u21d2 B\u2032)\n with A \u225fTp A\u2032 | B \u225fTp B\u2032\n... | no A\u2262 | _ = no \u03bb{refl \u2192 A\u2262 refl}\n... | yes _ | no B\u2262 = no \u03bb{refl \u2192 B\u2262 refl}\n... | yes refl | yes refl = yes refl\n\\end{code}\n\n\n## Type inference monad\n\nOne construct you will find in the functional programmer's toolbox\nis the _monad_, which can describe error handling, state, and\nmany other computational effects. Here we introduce a monad to\nmanage error messages in our inferencer.\n\nType inference will either yield a value (such as a synthesized type)\nor an error message (for instance, when inherited and synthesized\ntypes differ). An error message is given by a string.\n\\begin{code}\nMessage : Set\nMessage = String\n\\end{code}\nThe type `I A` represents the result of inference, where `A` is an\narbitrary Agda set representing the type of the result returned;\nin our case, we will return evidence for type judgments.\nNote here `A` ranges over\nAgda sets rather than types of our target lambda calculus.\n\\begin{code}\ndata I (A : Set) : Set where\n error\u207a : Message \u2192 Term\u207a \u2192 List Type \u2192 I A\n error\u207b : Message \u2192 Term\u207b \u2192 List Type \u2192 I A\n return : A \u2192 I A\n\\end{code}\nThere are three possible constructors, two for errors and one to\nreturn a value. An error also takes a message, a term, and a list of\ntypes relevant to the error; there is one variant for each sort of\nterm. Return embeds values of type `A` into the type `I A`.\n\nWe need a way to compose functions that may return error messages,\nand monads provide the required structure.\nA monad is equipped with an operation, usually written `_>>=_`\nand pronounced _bind_.\n\\begin{code}\n_>>=_ : \u2200 {A B : Set} \u2192 I A \u2192 (A \u2192 I B) \u2192 I B\nerror\u207a msg M As >>= k = error\u207a msg M As\nerror\u207b msg M As >>= k = error\u207b msg M As\nreturn x >>= k = k x\n\\end{code}\nIf the left argument raises an error, the bind term raises\nthe same error. If the right argument returns a value, the\nbind term applies its left argument to that value.\nThere is a conflict in our conventions: here `A` ranges over Agda\nsets, while `As` ranges over types of our target lambda calculus.\n\nA monad is a bit like a monoid, in that it should satisfy something\nakin to a left and right identity law and an associativity law. The\nrole of the identity is played by `return`. In our case,\nall three laws are trivial to prove.\n\\begin{code}\nidentity\u02e1 : \u2200 {A B : Set} (x : A) (k : A \u2192 I B) \u2192 return x >>= k \u2261 k x\nidentity\u02e1 x k = refl\n\nidentity\u02b3 : \u2200 {A B : Set} (m : I A) \u2192 m >>= (\u03bb x \u2192 return x) \u2261 m\nidentity\u02b3 (error\u207a _ _ _) = refl\nidentity\u02b3 (error\u207b _ _ _) = refl\nidentity\u02b3 (return _) = refl\n\nassoc : \u2200 {X Y Z : Set} (m : I X) (k : X \u2192 I Y) (h : Y \u2192 I Z) \u2192\n (m >>= \u03bb x \u2192 k x) >>= (\u03bb y \u2192 h y) \u2261 m >>= (\u03bb x \u2192 k x >>= (\u03bb y \u2192 h y))\nassoc (error\u207a _ _ _) k h = refl\nassoc (error\u207b _ _ _) k h = refl\nassoc (return _) k h = refl\n\\end{code}\nThe left-hand side of the associativity law can be abbreviated to\n`(m >>= k) >>= h`, but it is written as above to make clear that\nthe law is about re-arranging parentheses.\n\n## Syntactic sugar for monads\n\nAgda has built-in syntax to support the use of monads, which\ntranslates into applications of the binding operator `_>>=_`. Such\ntranslation of one construct into another is referred to as _syntactic\nsugar_, and we will apply it to sweeten our subsequent presentation.\n\nWriting\n\n do x \u2190 M\n N\n\ntranslates to\n\n M >>= \u03bb x \u2192 N\n\nHere `x` is an Agda variable and `M` and `N` are terms of Agda\n(rather than of our target lambda calculus). Applying the notations\nwe have learned to Agda itself, we can write\n\n \u0393 \u22a2 M : I A\n \u0393 , x : A \u22a2 N : I B\n -------------------\n \u0393 \u22a2 (do x \u2190 M\n N) : I B\n\nThat is, term `M` has type `I A`, variable `x` has type `A`, and term\n`N` has type `I B` and may contain `x` as a free variable, and the\nwhole term has type `I B`. One can read this as follows:\nEvaluate `M`; if it fails, yield the error message; if it succeeds,\nbind `x` to the value returned and evaluate `N`.\n\nSimilarly, writing\n\n do x \u2190 L\n y \u2190 M\n N\n\ntranslates to\n\n L >>= \u03bb x \u2192 (M >>= \u03bb y \u2192 N)\n\nIf `x` does not appear free in `N`, then by the associative law we\ncan parenthesise either way; though `x` may appear free in `N`.\nWe can describe the types as before:\n\n \u0393 \u22a2 L : I A\n \u0393 , x : A \u22a2 M : I B\n \u0393 , x : A , y : B \u22a2 N : I C\n ---------------------------\n \u0393 \u22a2 (do x \u2190 L\n y \u2190 M\n N) : I C\n\nWe can read this as: Evaluate `L`; if it fails, yield the error\nmessage; if it succeeds, bind `x` the the value returned and\nevaluate `M`; if it fails, yield the error message; if it\nsucceeds, bind `y` to the value returned and evaluate `N`.\n\nAdditionally, writing\n\n do P \u2190 L\n where Q \u2192 M\n N\n\ntranslates to\n\n L >>= \u03bb{ P \u2192 N ; Q \u2192 M }\n\n\nwhere `P`, `Q` are Agda patterns, and `L`, `M`, `N` are Agda terms.\nExtending our notation to allow a pattern to the left of a turnstyle, we have:\n\n \u0393 \u22a2 L : I A\n \u0393 , P : A \u22a2 N : I B\n \u0393 , Q : A \u22a2 M : I B\n ---------------------------\n \u0393 \u22a2 (do P \u2190 L\n where Q \u2192 M\n N) : I B\n\nOne can read this form as follows: Evaluate `M`; if it fails, yield\nthe error message; if it succeeds, match `P` to the value returned and\nevaluate `N` (which may contain variables matched by `P`); otherwise\nmatch `Q` to the value returned and evaluate `M` (which may contain\nvariables matched by `Q`); one of `P` and `Q` must match.\n\nThe notations extend to any number of bindings or patterns. Thus,\n\n do x\u2081 \u2190 M\u2081\n ...\n x\u2099 \u2190 M\u2099\n N\n\ntranslates to\n\n M\u2081 >>= (\u03bb x\u2081 \u2192 ... M\u2099 >>= (\u03bb x\u2099 \u2192 N)...)\n\nand\n\n do P \u2190 L\n where Q\u2081 \u2192 M\u2081\n ...\n Q\u2099 \u2192 M\u2099\n N\n\ntranslates to\n\n L >>= \u03bb{ P \u2192 N ; Q\u2081 \u2192 M\u2081 ; ... ; Q\u2099 \u2192 M\u2099 }\n\nWe will apply this sugar to sweeten our subsequent presentation.\n\n\n## Lookup type of a variable in the context\n\nGiven a context `\u0393` and a variable `x`, we return a type `A` and\nevidence that `\u0393 \u220b x \u2982 A`. If `x` does not appear in `\u0393`, then\nwe raise an error.\n\\begin{code}\nlookup : \u2200 (\u0393 : Context) (x : Id) \u2192 I (\u2203[ A ](\u0393 \u220b x \u2982 A))\nlookup \u2205 x =\n error\u207a \"variable not bound\" (` x) []\nlookup (\u0393 , y \u2982 B) x with x \u225f y\n... | yes refl =\n return \u27e8 B , Z \u27e9\n... | no x\u2262y =\n do \u27e8 A , \u22a2x \u27e9 \u2190 lookup \u0393 x\n return \u27e8 A , S x\u2262y \u22a2x \u27e9\n\\end{code}\nThere are three cases.\n\n* If the context is empty, we raise an error.\n\n* If the variable appears in the most recent binding, we\n return its corresponding type.\n\n* If the variable does not appear in the most recent binding,\n we recurse.\n\n## Synthesize and inherit types\n\nThe table has been set, the starters consumed, and we are ready\nfor the main course. We have two mutually recursive functions,\none for synthesis and one for inheritance. Synthesis is given\na context `\u0393` and a synthesis term `M` and\nreturns a type `A` and evidence that `\u0393 \u22a2 M \u2191 A`.\nInheritance is given a context `\u0393`, an inheritance term `M`,\nand a type `A` and reuturns evidence that `\u0393 \u22a2 M \u2193 A`.\nAn error is raised when appropriate.\n\\begin{code}\nsynthesize : \u2200 (\u0393 : Context) (M : Term\u207a) \u2192 I (\u2203[ A ](\u0393 \u22a2 M \u2191 A))\ninherit : \u2200 (\u0393 : Context) (M : Term\u207b) (A : Type) \u2192 I (\u0393 \u22a2 M \u2193 A)\n\\end{code}\n\nWe first consider the code for synthesis.\n\\begin{code}\nsynthesize \u0393 (` x) =\n do \u27e8 A , \u22a2x \u27e9 \u2190 lookup \u0393 x\n return \u27e8 A , Ax \u22a2x \u27e9\nsynthesize \u0393 (L \u00b7 M) =\n do \u27e8 A \u21d2 B , \u22a2L \u27e9 \u2190 synthesize \u0393 L\n where \u27e8 `\u2115 , _ \u27e9 \u2192 error\u207a \"must apply function\" (L \u00b7 M) []\n \u22a2M \u2190 inherit \u0393 M A\n return \u27e8 B , \u22a2L \u00b7 \u22a2M \u27e9\nsynthesize \u0393 (M \u2193 A) =\n do \u22a2M \u2190 inherit \u0393 M A\n return \u27e8 A , \u22a2\u2193 \u22a2M \u27e9\n\\end{code}\nThere are three cases.\n\n* If the term is a variable, we use lookup as defined above.\n\n* If the term is an application, we recurse to synthesize the type of\n the function. We check that the synthesied type is a function of\n the form `A \u21d2 B`. If it is not (e.g., it is of type `` `\u2115 ``), then\n we report an error. The argument is typed by inheriting `A`, and\n type `B` is returned as the synthesised type of the term as a whole.\n\n* If the term switches from synthesized to inherited, then the type\n decoration `A` in the contained term is typed by inheriting `A`, and\n `A` is also returned as the synthesized type of the term as a whole.\n\nWe next consider the code for inheritance.\n\\begin{code}\ninherit \u0393 (\u019b x \u21d2 N) (A \u21d2 B) =\n do \u22a2N \u2190 inherit (\u0393 , x \u2982 A) N B\n return (\u22a2\u019b \u22a2N)\ninherit \u0393 (\u019b x \u21d2 N) `\u2115 =\n error\u207b \"lambda cannot be of type natural\" (\u019b x \u21d2 N) []\ninherit \u0393 `zero `\u2115 =\n return \u22a2zero\ninherit \u0393 `zero (A \u21d2 B) =\n error\u207b \"zero cannot be function\" `zero [ A \u21d2 B ]\ninherit \u0393 (`suc M) `\u2115 =\n do \u22a2M \u2190 inherit \u0393 M `\u2115\n return (\u22a2suc \u22a2M)\ninherit \u0393 (`suc M) (A \u21d2 B) =\n error\u207b \"suc cannot be function\" (`suc M) [ A \u21d2 B ]\ninherit \u0393 (`case L [zero\u21d2 M |suc x \u21d2 N ]) A =\n do \u27e8 `\u2115 , \u22a2L \u27e9 \u2190 synthesize \u0393 L\n where \u27e8 B \u21d2 C , _ \u27e9 \u2192 error\u207b \"cannot case on function\"\n (`case L [zero\u21d2 M |suc x \u21d2 N ])\n [ B \u21d2 C ]\n \u22a2M \u2190 inherit \u0393 M A\n \u22a2N \u2190 inherit (\u0393 , x \u2982 `\u2115) N A\n return (\u22a2case \u22a2L \u22a2M \u22a2N)\ninherit \u0393 (\u03bc x \u21d2 M) A =\n do \u22a2M \u2190 inherit (\u0393 , x \u2982 A) M A\n return (\u22a2\u03bc \u22a2M)\ninherit \u0393 (M \u2191) B =\n do \u27e8 A , \u22a2M \u27e9 \u2190 synthesize \u0393 M\n yes A\u2261B \u2190 return (A \u225fTp B)\n where no _ \u2192 error\u207b \"inheritance and synthesis conflict\" (M \u2191) [ A , B ]\n return (\u22a2\u2191 \u22a2M A\u2261B)\n\\end{code}\nThere are nine cases. We consider those for abstraction\nand for switching from inherited to synthesized.\n\n* If the term is an abstraction and the inherited type is of the form `A \u21d2 B`\n then we extend the context by giving the variable type `A` and\n recuse to type the body by inheriting type `B`.\n\n* If the term is an abstraction and the inherited type is not a function\n (e.g., of the form `` `\u2115 ``), then we report an error.\n\n* If the term switches from inherited to synthesised, then\n we synthesise the type of the contained term and compare it\n to the inherited type. If they are not equal, we raise an error.\n\nThe remaining cases are similar, and their code can pretty much be\nread directly from the corresponding typing rules.\n\n## Testing the example terms\n\nFirst, we copy a function introduced earlier that makes it easy to\ncompute the evidence that two variable names are distinct.\n\\begin{code}\n_\u2260_ : \u2200 (x y : Id) \u2192 x \u2262 y\nx \u2260 y with x \u225f y\n... | no x\u2262y = x\u2262y\n... | yes _ = \u22a5-elim impossible\n where postulate impossible : \u22a5\n\\end{code}\n\nHere is the result of typing two plus two on naturals.\n\\begin{code}\n\u22a22+2 : \u2205 \u22a2 2+2 \u2191 `\u2115\n\u22a22+2 =\n (\u22a2\u2193\n (\u22a2\u03bc\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2case (Ax (S (\"m\" \u2260 \"n\") Z)) (\u22a2\u2191 (Ax Z) refl)\n (\u22a2suc\n (\u22a2\u2191\n (Ax\n (S (\"p\" \u2260 \"m\")\n (S (\"p\" \u2260 \"n\")\n (S (\"p\" \u2260 \"m\") Z)))\n \u00b7 \u22a2\u2191 (Ax Z) refl\n \u00b7 \u22a2\u2191 (Ax (S (\"n\" \u2260 \"m\") Z)) refl)\n refl))))))\n \u00b7 \u22a2suc (\u22a2suc \u22a2zero)\n \u00b7 \u22a2suc (\u22a2suc \u22a2zero))\n\\end{code}\nWe confirm that synthesis on the relevant term returns\nnatural as the type and the above derivation.\n\\begin{code}\n_ : synthesize \u2205 2+2 \u2261 return \u27e8 `\u2115 , \u22a22+2 \u27e9\n_ = refl\n\\end{code}\nIndeed, the above derivation was computed by evaluating the\nterm on the left, and editing. The only editing required is to\nreplace Agda's representation of the evidence that two strings are\nunequal (which it can print not read) by equivalent calls to `\u2260`.\n\nHere is the result of typing two plus two with Church numerals.\n\\begin{code}\n\u22a22+2\u1d9c : \u2205 \u22a2 2+2\u1d9c \u2191 `\u2115\n\u22a22+2\u1d9c =\n \u22a2\u2193\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (Ax\n (S (\"m\" \u2260 \"z\")\n (S (\"m\" \u2260 \"s\")\n (S (\"m\" \u2260 \"n\") Z)))\n \u00b7 \u22a2\u2191 (Ax (S (\"s\" \u2260 \"z\") Z)) refl\n \u00b7\n \u22a2\u2191\n (Ax\n (S (\"n\" \u2260 \"z\")\n (S (\"n\" \u2260 \"s\") Z))\n \u00b7 \u22a2\u2191 (Ax (S (\"s\" \u2260 \"z\") Z)) refl\n \u00b7 \u22a2\u2191 (Ax Z) refl)\n refl)\n refl)))))\n \u00b7\n \u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (Ax (S (\"s\" \u2260 \"z\") Z) \u00b7\n \u22a2\u2191 (Ax (S (\"s\" \u2260 \"z\") Z) \u00b7 \u22a2\u2191 (Ax Z) refl)\n refl)\n refl))\n \u00b7\n \u22a2\u019b\n (\u22a2\u019b\n (\u22a2\u2191\n (Ax (S (\"s\" \u2260 \"z\") Z) \u00b7\n \u22a2\u2191 (Ax (S (\"s\" \u2260 \"z\") Z) \u00b7 \u22a2\u2191 (Ax Z) refl)\n refl)\n refl))\n \u00b7 \u22a2\u019b (\u22a2suc (\u22a2\u2191 (Ax Z) refl))\n \u00b7 \u22a2zero\n\\end{code}\nWe confirm that synthesis on the relevant term returns\nnatural as the type and the above derivation.\n\\begin{code}\n_ : synthesize \u2205 2+2\u1d9c \u2261 return \u27e8 `\u2115 , \u22a22+2\u1d9c \u27e9\n_ = refl\n\\end{code}\nAgain, the above derivation was computed by evaluating the\nterm on the left, and editing.\n\n## Testing the error cases\n\nIt is important not just to check that code works as intended,\nbut also that it fails as intended. Here is one test case to\nexercise each of the possible error messages.\n\\begin{code}\n_ : synthesize \u2205 ((\u019b \"x\" \u21d2 ` \"y\" \u2191) \u2193 (`\u2115 \u21d2 `\u2115)) \u2261\n error\u207a \"variable not bound\" (` \"y\") []\n_ = refl\n\n_ : synthesize \u2205 ((two \u2193 `\u2115) \u00b7 two) \u2261\n error\u207a \"must apply function\"\n ((`suc (`suc `zero) \u2193 `\u2115) \u00b7 `suc (`suc `zero)) []\n_ = refl\n\n_ : synthesize \u2205 (two\u1d9c \u2193 `\u2115) \u2261\n error\u207b \"lambda cannot be of type natural\"\n (\u019b \"s\" \u21d2 (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 (` \"z\" \u2191) \u2191) \u2191)) []\n_ = refl\n\n_ : synthesize \u2205 (`zero \u2193 `\u2115 \u21d2 `\u2115) \u2261\n error\u207b \"zero cannot be function\" `zero [ `\u2115 \u21d2 `\u2115 ]\n_ = refl\n\n_ : synthesize \u2205 (two \u2193 `\u2115 \u21d2 `\u2115) \u2261\n error\u207b \"suc cannot be function\" (`suc (`suc `zero)) [ `\u2115 \u21d2 `\u2115 ]\n_ = refl\n\n_ : synthesize \u2205\n ((`case (two\u1d9c \u2193 Ch) [zero\u21d2 `zero |suc \"x\" \u21d2 ` \"x\" \u2191 ] \u2193 `\u2115) ) \u2261\n error\u207b \"cannot case on function\"\n `case (\u019b \"s\" \u21d2 (\u019b \"z\" \u21d2 ` \"s\" \u00b7 (` \"s\" \u00b7 (` \"z\" \u2191) \u2191) \u2191))\n \u2193 (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115 [zero\u21d2 `zero |suc \"x\" \u21d2 ` \"x\" \u2191 ]\n [ (`\u2115 \u21d2 `\u2115) \u21d2 `\u2115 \u21d2 `\u2115 ]\n_ = refl\n\n_ : synthesize \u2205 (((\u019b \"x\" \u21d2 ` \"x\" \u2191) \u2193 `\u2115 \u21d2 (`\u2115 \u21d2 `\u2115))) \u2261\n error\u207b \"inheritance and synthesis conflict\" (` \"x\" \u2191) [ `\u2115 , `\u2115 \u21d2 `\u2115 ]\n_ = refl\n\\end{code}\n\n\n## Erasure\n\nFrom the evidence that a decorated term has the correct type it is\neasy to extract the corresponding inherently typed term. We use the\nname `DB` to refer to the code in\nChapter [DeBruijn]({{ site.baseurl }}{% link out\/plfa\/DeBruijn.md %}).\nIt is easy to define an _erasure_ function that takes evidence of a\ntype judgment into the corresponding inherently typed term.\n\nFirst, we give code to erase a context.\n\\begin{code}\n\u2225_\u2225\u0393 : Context \u2192 DB.Context\n\u2225 \u2205 \u2225\u0393 = DB.\u2205\n\u2225 \u0393 , x \u2982 A \u2225\u0393 = \u2225 \u0393 \u2225\u0393 DB., A\n\\end{code}\nIt simply drops the variable names.\n\nNext, we give code to erase a lookup judgment.\n\\begin{code}\n\u2225_\u2225\u220b : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x \u2982 A \u2192 \u2225 \u0393 \u2225\u0393 DB.\u220b A\n\u2225 Z \u2225\u220b = DB.Z\n\u2225 S x\u2262 \u22a2x \u2225\u220b = DB.S \u2225 \u22a2x \u2225\u220b\n\\end{code}\nIt just drops the evidence that variable names are distinct.\n\nFinally, we give the code to erase a typing judgment.\nJust as there are two mutually recursive typing judgments,\nthere are two mutually recursive erasure functions.\n\\begin{code}\n\u2225_\u2225\u207a : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2191 A \u2192 \u2225 \u0393 \u2225\u0393 DB.\u22a2 A\n\u2225_\u2225\u207b : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M \u2193 A \u2192 \u2225 \u0393 \u2225\u0393 DB.\u22a2 A\n\n\u2225 Ax \u22a2x \u2225\u207a = DB.` \u2225 \u22a2x \u2225\u220b\n\u2225 \u22a2L \u00b7 \u22a2M \u2225\u207a = \u2225 \u22a2L \u2225\u207a DB.\u00b7 \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2\u2193 \u22a2M \u2225\u207a = \u2225 \u22a2M \u2225\u207b\n\n\u2225 \u22a2\u019b \u22a2N \u2225\u207b = DB.\u019b \u2225 \u22a2N \u2225\u207b\n\u2225 \u22a2zero \u2225\u207b = DB.`zero\n\u2225 \u22a2suc \u22a2M \u2225\u207b = DB.`suc \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2case \u22a2L \u22a2M \u22a2N \u2225\u207b = DB.case \u2225 \u22a2L \u2225\u207a \u2225 \u22a2M \u2225\u207b \u2225 \u22a2N \u2225\u207b\n\u2225 \u22a2\u03bc \u22a2M \u2225\u207b = DB.\u03bc \u2225 \u22a2M \u2225\u207b\n\u2225 \u22a2\u2191 \u22a2M refl \u2225\u207b = \u2225 \u22a2M \u2225\u207a\n\\end{code}\nErasure replaces constructors for each typing judgment\nby the corresponding term constructor from `DB`. The\nconstructors that correspond to switching from synthesized\nto inherited or vice versa are dropped.\n\nWe confirm that the erasure of the type derivations in\nthis chapter yield the corresponding inherently typed terms\nfrom the earlier chapter.\n\\begin{code}\n_ : \u2225 \u22a22+2 \u2225\u207a \u2261 DB.2+2\n_ = refl\n\n_ : \u2225 \u22a22+2\u1d9c \u2225\u207a \u2261 DB.2+2\u1d9c\n_ = refl\n\\end{code}\nThus, we have confirmed that bidirectional type inference to\nconvert decorated versions of the lambda terms from\nChapter [Lambda]({{ site.baseurl }}{% link out\/plfa\/Lambda.md %})\nto the inherently typed terms of\nChapter [DeBruijn]({{ site.baseurl }}{% link out\/plfa\/DeBruijn.md %}).\n\n#### Exercise (`decoration`)\n\nExtend bidirectional inference to include each of the constructs in\nChapter [More]({{ site.baseurl }}{% link out\/plfa\/More.md %}).\n\n\n## Bidirectional inference in Agda\n\nAgda itself uses bidirectional inference. This explains why\nconstructors can be overloaded while other defined names cannot \u2014 here\nby _overloaded_ we mean that the same name can be used for\nconstructors of different types. Constructors are typed by\ninheritance, and so the name is available when resolving the\nconstructor, whereas variables are typed by synthesis, and so each\nvariable must have a unique type.\n\n\n## Unicode\n\nThis chapter uses the following unicode\n\n \u2193 U+2193: DOWNWARDS ARROW (\\d)\n \u2191 U+2191: UPWARDS ARROW (\\u)\n \u2190 U+2190: LEFTWARDS ARROW (\\l)\n \u2225 U+2225: PARALLEL TO (\\||)\n","avg_line_length":32.0470347648,"max_line_length":79,"alphanum_fraction":0.617765299} +{"size":2977,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule Declarative.Erasure where\n\\end{code}\n\n\\begin{code}\nopen import Declarative\nopen import Type.RenamingSubstitution as T\nopen import Untyped\nopen import Untyped.RenamingSubstitution as U\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Declarative\nopen import Builtin\nopen import Utils\nopen import Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2\u22c6_ ` con\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\n renaming (TermCon to TyTermCon)\n\nopen import Data.Nat\nopen import Data.Fin using (Fin;zero;suc)\nopen import Data.Vec using ([]; _\u2237_;_++_)\nopen import Data.List using (List;length;[];_\u2237_)\nopen import Relation.Binary.PropositionalEquality\nopen import Data.Product using (proj\u2081;proj\u2082)\n\nlen : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u2115\nlen \u2205 = 0\nlen (\u0393 ,\u22c6 K) = len \u0393\nlen (\u0393 , A) = suc (len \u0393)\n\nlen\u22c6 : Ctx\u22c6 \u2192 \u2115\nlen\u22c6 \u2205 = 0\nlen\u22c6 (\u0393 ,\u22c6 K) = suc (len\u22c6 \u0393)\n\nlemma : (b : Builtin)\n \u2192 len\u22c6 (proj\u2081 (SIG b)) + length (proj\u2081 (proj\u2082 (SIG b))) \u2261 arity b\nlemma addInteger = refl\nlemma subtractInteger = refl\nlemma multiplyInteger = refl\nlemma divideInteger = refl\nlemma quotientInteger = refl\nlemma remainderInteger = refl\nlemma modInteger = refl\nlemma lessThanInteger = refl\nlemma lessThanEqualsInteger = refl\nlemma greaterThanInteger = refl\nlemma greaterThanEqualsInteger = refl\nlemma equalsInteger = refl\nlemma concatenate = refl\nlemma takeByteString = refl\nlemma dropByteString = refl\nlemma sha2-256 = refl\nlemma sha3-256 = refl\nlemma verifySignature = refl\nlemma equalsByteString = refl\nlemma ifThenElse = refl\n\nlemma\u2264 : (b : Builtin) \u2192 len\u22c6 (proj\u2081 (SIG b)) + length (proj\u2081 (proj\u2082 (SIG b))) \u2264\u2034 arity b\nlemma\u2264 b rewrite lemma b = \u2264\u2034-refl\n\neraseVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u220b A \u2192 Fin (len \u0393)\neraseVar Z = zero \neraseVar (S \u03b1) = suc (eraseVar \u03b1) \neraseVar (T \u03b1) = eraseVar \u03b1\n\neraseTC : \u2200{\u03a6}{\u0393 : Ctx \u03a6}{A : \u03a6 \u22a2\u22c6 *} \u2192 TyTermCon A \u2192 TermCon\neraseTC (integer i) = integer i\neraseTC (bytestring b) = bytestring b\neraseTC (string s) = string s\neraseTC (bool b) = bool b \neraseTC (char c) = char c\neraseTC unit = unit\n\neraseTel\u22c6 : \u2200{\u03a6}(\u0393 : Ctx \u03a6)(\u0394 : Ctx\u22c6) \u2192 Untyped.Tel (len\u22c6 \u0394) (len \u0393) \neraseTel\u22c6 _ \u2205 = []\neraseTel\u22c6 \u0393 (\u0394 ,\u22c6 K) = eraseTel\u22c6 \u0393 \u0394 :< con unit\n\neraseTel : \u2200{\u03a6 \u0393 \u0394}{\u03c3 : T.Sub \u0394 \u03a6}{As : List (\u0394 \u22a2\u22c6 *)}\n \u2192 Declarative.Tel \u0393 \u0394 \u03c3 As\n \u2192 Untyped.Tel (length As) (len \u0393) \nerase : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 len \u0393 \u22a2\n\nerase (` \u03b1) = ` (eraseVar \u03b1)\nerase (\u019b t) = \u019b (erase t) \nerase (t \u00b7 u) = erase t \u00b7 erase u\nerase (\u039b t) = \u019b (U.weaken (erase t))\nerase (t \u00b7\u22c6 A) = erase t \u00b7 plc_dummy\nerase (wrap1 pat arg t) = erase t\nerase (unwrap1 t) = erase t\nerase (conv p t) = erase t\nerase {\u0393 = \u0393} (con t) = con (eraseTC {\u0393 = \u0393} t)\nerase {\u0393 = \u0393} (builtin bn \u03c3 ts) =\n builtin bn (lemma\u2264 bn) (eraseTel\u22c6 \u0393 (proj\u2081 (SIG bn)) ++ eraseTel ts)\nerase (error A) = error\n\neraseTel {As = []} ts = []\neraseTel {As = x \u2237 As} (t \u2237 tel) = erase t \u2237 eraseTel tel\n\\end{code}\n","avg_line_length":29.4752475248,"max_line_length":89,"alphanum_fraction":0.6331877729} +{"size":15394,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"---\ntitle : \"Quantifiers: Universals and existentials\"\nlayout : page\nprev : \/Negation\/\npermalink : \/Quantifiers\/\nnext : \/Decidable\/\n---\n\n\\begin{code}\nmodule plfa.Quantifiers where\n\\end{code}\n\nThis chapter introduces universal and existential quantification.\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_)\nopen import Data.Nat.Properties.Simple using (+-suc)\nopen import Relation.Nullary using (\u00ac_)\nopen import Function using (_\u2218_)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import plfa.Isomorphism using (_\u2243_; \u2243-sym; \u2243-trans; _\u2272_; extensionality)\n\\end{code}\n\n\n## Universals\n\nWe formalise universal quantification using the\ndependent function type, which has appeared throughout this book.\n\nGiven a variable `x` of type `A` and a proposition `B x` which\ncontains `x` as a free variable, the universally quantified\nproposition `\u2200 (x : A) \u2192 B x` holds if for every term `M` of type\n`A` the proposition `B M` holds. Here `B M` stands for\nthe proposition `B x` with each free occurrence of `x` replaced by\n`M`. Variable `x` appears free in `B x` but bound in\n`\u2200 (x : A) \u2192 B x`. \n\nEvidence that `\u2200 (x : A) \u2192 B x` holds is of the form\n\n \u03bb (x : A) \u2192 N x\n\nwhere `N x` is a term of type `B x`, and `N x` and `B x` both contain\na free variable `x` of type `A`. Given a term `L` providing evidence\nthat `\u2200 (x : A) \u2192 B x` holds, and a term `M` of type `A`, the term `L\nM` provides evidence that `B M` holds. In other words, evidence that\n`\u2200 (x : A) \u2192 B x` holds is a function that converts a term `M` of type\n`A` into evidence that `B M` holds.\n\nPut another way, if we know that `\u2200 (x : A) \u2192 B x` holds and that `M`\nis a term of type `A` then we may conclude that `B M` holds:\n\\begin{code}\n\u2200-elim : \u2200 {A : Set} {B : A \u2192 Set}\n \u2192 (L : \u2200 (x : A) \u2192 B x)\n \u2192 (M : A)\n -----------------\n \u2192 B M\n\u2200-elim L M = L M\n\\end{code}\nAs with `\u2192-elim`, the rule corresponds to function application.\n\nFunctions arise as a special case of dependent functions,\nwhere the range does not depend on a variable drawn from the domain.\nWhen a function is viewed as evidence of implication, both its\nargument and result are viewed as evidence, whereas when a dependent\nfunction is viewed as evidence of a universal, its argument is viewed\nas an element of a data type and its result is viewed as evidence of\na proposition that depends on the argument. This difference is largely\na matter of interpretation, since in Agda a value of a type and\nevidence of a proposition are indistinguishable.\n\nDependent function types are sometimes referred to as dependent\nproducts, because if `A` is a finite type with values `x\u2081 , \u22ef , x\u2099`,\nand if each of the types `B x\u2081 , \u22ef , B x\u2099` has `m\u2081 , \u22ef , m\u2099` distinct\nmembers, then `\u2200 (x : A) \u2192 B x` has `m\u2081 * \u22ef * m\u2099` members. Indeed,\nsometimes the notation `\u2200 (x : A) \u2192 B x` is replaced by a notation\nsuch as `\u03a0[ x \u2208 A ] (B x)`, where `\u03a0` stands for product. However, we\nwill stick with the name dependent function, because (as we will see)\ndependent product is ambiguous.\n\n\n#### Exercise `\u2200-distrib-\u00d7` (recommended)\n\nShow that universals distribute over conjunction:\n\\begin{code}\npostulate\n \u2200-distrib-\u00d7 : \u2200 {A : Set} {B C : A \u2192 Set} \u2192\n (\u2200 (x : A) \u2192 B x \u00d7 C x) \u2243 (\u2200 (x : A) \u2192 B x) \u00d7 (\u2200 (x : A) \u2192 C x)\n\\end{code}\nCompare this with the result (`\u2192-distrib-\u00d7`) in\nChapter [Connectives][plfa.Connectives].\n\n#### Exercise `\u228e\u2200-implies-\u2200\u228e`\n\nShow that a disjunction of universals implies a universal of disjunctions:\n\\begin{code}\npostulate\n \u228e\u2200-implies-\u2200\u228e : \u2200 {A : Set} { B C : A \u2192 Set } \u2192\n (\u2200 (x : A) \u2192 B x) \u228e (\u2200 (x : A) \u2192 C x) \u2192 \u2200 (x : A) \u2192 B x \u228e C x\n\\end{code}\nDoes the converse hold? If so, prove; if not, explain why.\n\n\n## Existentials\n\nGiven a variable `x` of type `A` and a proposition `B x` which\ncontains `x` as a free variable, the existentially quantified\nproposition `\u03a3[ x \u2208 A ] B x` holds if for some term `M` of type\n`A` the proposition `B M` holds. Here `B M` stands for\nthe proposition `B x` with each free occurrence of `x` replaced by\n`M`. Variable `x` appears free in `B x` but bound in\n`\u03a3[ x \u2208 A ] B x`.\n\nWe formalise existential quantification by declaring a suitable\ninductive type:\n\\begin{code}\ndata \u03a3 (A : Set) (B : A \u2192 Set) : Set where\n \u27e8_,_\u27e9 : (x : A) \u2192 B x \u2192 \u03a3 A B\n\\end{code}\nWe define a convenient syntax for existentials as follows:\n\\begin{code}\n\u03a3-syntax = \u03a3\ninfix 2 \u03a3-syntax\nsyntax \u03a3-syntax A (\u03bb x \u2192 B) = \u03a3[ x \u2208 A ] B\n\\end{code}\nThis is our first use of a syntax declaration, which specifies that\nthe term on the left may be written with the syntax on the right.\nThe special syntax is available only when the identifier\n`\u03a3-syntax` is imported.\n\nEvidence that `\u03a3[ x \u2208 A ] B x` holds is of the form\n`\u27e8 M , N \u27e9` where `M` is a term of type `A`, and `N` is evidence\nthat `B M` holds.\n\nEquivalently, we could also declare existentials as a record type:\n\\begin{code}\nrecord \u03a3\u2032 (A : Set) (B : A \u2192 Set) : Set where\n field\n proj\u2081\u2032 : A\n proj\u2082\u2032 : B proj\u2081\u2032\n\\end{code}\nHere record construction\n\n record\n { proj\u2081\u2032 = M\n ; proj\u2082\u2032 = N\n }\n\ncorresponds to the term\n\n \u27e8 M , N \u27e9\n\nwhere `M` is a term of type `A` and `N` is a term of type `B M`.\n\nProducts arise a special case of existentials, where the second\ncomponent does not depend on a variable drawn from the first\ncomponent. When a product is viewed as evidence of a conjunction,\nboth of its components are viewed as evidence, whereas when it is\nviewed as evidence of an existential, the first component is viewed as\nan element of a datatype and the second component is viewed as\nevidence of a proposition that depends on the first component. This\ndifference is largely a matter of interpretation, since in Agda a value\nof a type and evidence of a proposition are indistinguishable.\n\nExistentials are sometimes referred to as dependent sums,\nbecause if `A` is a finite type with values `x\u2081 , \u22ef , x\u2099`, and if\neach of the types `B x\u2081 , \u22ef B x\u2099` has `m\u2081 , \u22ef , m\u2099` distinct members,\nthen `\u03a3[ x \u2208 A] B x` has `m\u2081 + \u22ef + m\u2099` members, which explains the\nchoice of notation for existentials, since `\u03a3` stands for sum.\n\nExistentials are sometimes referred to as dependent products, since\nproducts arise as a special case. However, that choice of names is\ndoubly confusing, since universals also have a claim to the name dependent\nproduct and since existentials also have a claim to the name dependent sum.\n\nA common notation for existentials is `\u2203` (analogous to `\u2200` for universals).\nWe follow the convention of the Agda standard library, and reserve this\nnotation for the case where the domain of the bound variable is left implicit:\n\\begin{code}\n\u2203 : \u2200 {A : Set} (B : A \u2192 Set) \u2192 Set\n\u2203 {A} B = \u03a3 A B\n\n\u2203-syntax = \u2203\nsyntax \u2203-syntax (\u03bb x \u2192 B) = \u2203[ x ] B\n\\end{code}\nThe special syntax is available only when the identifier `\u2203-syntax` is imported.\nWe will tend to use this syntax, since it is shorter and more familiar.\n\nGiven evidence that `\u2200 x \u2192 B x \u2192 C` holds, where `C` does not contain\n`x` as a free variable, and given evidence that `\u2203[ x ] B x` holds, we\nmay conclude that `C` holds:\n\\begin{code}\n\u2203-elim : \u2200 {A : Set} {B : A \u2192 Set} {C : Set}\n \u2192 (\u2200 x \u2192 B x \u2192 C)\n \u2192 \u2203[ x ] B x\n ---------------\n \u2192 C\n\u2203-elim f \u27e8 x , y \u27e9 = f x y\n\\end{code}\nIn other words, if we know for every `x` of type `A` that `B x`\nimplies `C`, and we know for some `x` of type `A` that `B x` holds,\nthen we may conclude that `C` holds. This is because we may\ninstantiate that proof that `\u2200 x \u2192 B x \u2192 C` to any value `x` of type\n`A` and any `y` of type `B x`, and exactly such values are provided by\nthe evidence for `\u2203[ x ] B x`.\n\nIndeed, the converse also holds, and the two together form an isomorphism:\n\\begin{code}\n\u2200\u2203-currying : \u2200 {A : Set} {B : A \u2192 Set} {C : Set}\n \u2192 (\u2200 x \u2192 B x \u2192 C) \u2243 (\u2203[ x ] B x \u2192 C)\n\u2200\u2203-currying =\n record\n { to = \u03bb{ f \u2192 \u03bb{ \u27e8 x , y \u27e9 \u2192 f x y }}\n ; from = \u03bb{ g \u2192 \u03bb{ x \u2192 \u03bb{ y \u2192 g \u27e8 x , y \u27e9 }}}\n ; from\u2218to = \u03bb{ f \u2192 refl }\n ; to\u2218from = \u03bb{ g \u2192 extensionality \u03bb{ \u27e8 x , y \u27e9 \u2192 refl }}\n }\n\\end{code}\nThe result can be viewed as a generalisation of currying. Indeed, the code to\nestablish the isomorphism is identical to what we wrote when discussing\n[implication][plfa.Connectives#implication].\n\n#### Exercise `\u2203-distrib-\u228e` (recommended)\n\nShow that existentials distribute over disjunction:\n\\begin{code}\npostulate\n \u2203-distrib-\u228e : \u2200 {A : Set} {B C : A \u2192 Set} \u2192\n \u2203[ x ] (B x \u228e C x) \u2243 (\u2203[ x ] B x) \u228e (\u2203[ x ] C x)\n\\end{code}\n\n#### Exercise `\u2203\u00d7-implies-\u00d7\u2203`\n\nShow that an existential of conjunctions implies a conjunction of existentials:\n\\begin{code}\npostulate\n \u2203\u00d7-implies-\u00d7\u2203 : \u2200 {A : Set} { B C : A \u2192 Set } \u2192\n \u2203[ x ] (B x \u00d7 C x) \u2192 (\u2203[ x ] B x) \u00d7 (\u2203[ x ] C x)\n\\end{code}\nDoes the converse hold? If so, prove; if not, explain why.\n\n\n## An existential example\n\nRecall the definitions of `even` and `odd` from\nChapter [Relations][plfa.Relations]:\n\\begin{code}\ndata even : \u2115 \u2192 Set\ndata odd : \u2115 \u2192 Set\n\ndata even where\n\n even-zero : even zero\n\n even-suc : \u2200 {n : \u2115}\n \u2192 odd n\n ------------\n \u2192 even (suc n)\n\ndata odd where\n odd-suc : \u2200 {n : \u2115}\n \u2192 even n\n -----------\n \u2192 odd (suc n)\n\\end{code}\nA number is even if it is zero or the successor of an odd number, and\nodd if it the successor of an even number.\n\nWe will show that a number is even if and only if it is twice some\nother number, and odd if and only if it is one more than twice\nsome other number. In other words, we will show:\n\n`even n` iff `\u2203[ m ] ( m * 2 \u2261 n)`\n\n`odd n` iff `\u2203[ m ] (1 + m * 2 \u2261 n)`\n\nBy convention, one tends to write constant factors first and to put\nthe constant term in a sum last. Here we've reversed each of those\nconventions, because doing so eases the proof.\n\nHere is the proof in the forward direction:\n\\begin{code}\neven-\u2203 : \u2200 {n : \u2115} \u2192 even n \u2192 \u2203[ m ] ( m * 2 \u2261 n)\nodd-\u2203 : \u2200 {n : \u2115} \u2192 odd n \u2192 \u2203[ m ] (1 + m * 2 \u2261 n)\n\neven-\u2203 even-zero = \u27e8 zero , refl \u27e9\neven-\u2203 (even-suc o) with odd-\u2203 o\n... | \u27e8 m , refl \u27e9 = \u27e8 suc m , refl \u27e9\n\nodd-\u2203 (odd-suc e) with even-\u2203 e\n... | \u27e8 m , refl \u27e9 = \u27e8 m , refl \u27e9\n\\end{code}\nWe define two mutually recursive functions. Given\nevidence that `n` is even or odd, we return a\nnumber `m` and evidence that `m * 2 \u2261 n` or `1 + m * 2 \u2261 n`.\nWe induct over the evidence that `n` is even or odd:\n\n* If the number is even because it is zero, then we return a pair\nconsisting of zero and the evidence that twice zero is zero.\n\n* If the number is even because it is one more than an odd number,\nthen we apply the induction hypothesis to give a number `m` and\nevidence that `1 + m * 2 \u2261 n`. We return a pair consisting of `suc m`\nand evidence that `suc m * 2` \u2261 suc n`, which is immediate after\nsubstituting for `n`.\n\n* If the number is odd because it is the successor of an even number,\nthen we apply the induction hypothesis to give a number `m` and\nevidence that `m * 2 \u2261 n`. We return a pair consisting of `suc m` and\nevidence that `1 + m * 2 \u2261 suc n`, which is immediate after\nsubstituting for `n`.\n\nThis completes the proof in the forward direction.\n\nHere is the proof in the reverse direction:\n\\begin{code}\n\u2203-even : \u2200 {n : \u2115} \u2192 \u2203[ m ] ( m * 2 \u2261 n) \u2192 even n\n\u2203-odd : \u2200 {n : \u2115} \u2192 \u2203[ m ] (1 + m * 2 \u2261 n) \u2192 odd n\n\n\u2203-even \u27e8 zero , refl \u27e9 = even-zero\n\u2203-even \u27e8 suc m , refl \u27e9 = even-suc (\u2203-odd \u27e8 m , refl \u27e9)\n\n\u2203-odd \u27e8 m , refl \u27e9 = odd-suc (\u2203-even \u27e8 m , refl \u27e9)\n\\end{code}\nGiven a number that is twice some other number we must show it is\neven, and a number that is one more than twice some other number we\nmust show it is odd. We induct over the evidence of the existential,\nand in the even case consider the two possibilities for the number\nthat is doubled:\n\n- In the even case for `zero`, we must show `zero * 2` is even, which\nfollows by `even-zero`.\n\n- In the even case for `suc n`, we must show `suc m * 2` is even. The\ninductive hypothesis tells us that `1 + m * 2` is odd, from which the\ndesired result follows by `even-suc`.\n\n- In the odd case, we must show `1 + m * 2` is odd. The inductive\nhypothesis tell us that `m * 2` is even, from which the desired result\nfollows by `odd-suc`.\n\nThis completes the proof in the backward direction.\n\n#### Exercise `\u2203-even-odd`\n\nHow do the proofs become more difficult if we replace `m * 2` and `1 + m * 2`\nby `2 * m` and `2 * m + 1`? Rewrite the proofs of `\u2203-even` and `\u2203-odd` when\nrestated in this way.\n\n#### Exercise `\u2203-+-\u2264`\n\nShow that `y \u2264 z` holds if and only if there exists a `x` such that\n`x + y \u2261 z`.\n\n\n## Existentials, Universals, and Negation\n\nNegation of an existential is isomorphic to the universal\nof a negation. Considering that existentials are generalised\ndisjunction and universals are generalised conjunction, this\nresult is analogous to the one which tells us that negation\nof a disjunction is isomorphic to a conjunction of negations:\n\\begin{code}\n\u00ac\u2203\u2243\u2200\u00ac : \u2200 {A : Set} {B : A \u2192 Set}\n \u2192 (\u00ac \u2203[ x ] B x) \u2243 \u2200 x \u2192 \u00ac B x\n\u00ac\u2203\u2243\u2200\u00ac =\n record\n { to = \u03bb{ \u00ac\u2203xy x y \u2192 \u00ac\u2203xy \u27e8 x , y \u27e9 }\n ; from = \u03bb{ \u2200\u00acxy \u27e8 x , y \u27e9 \u2192 \u2200\u00acxy x y }\n ; from\u2218to = \u03bb{ \u00ac\u2203xy \u2192 extensionality \u03bb{ \u27e8 x , y \u27e9 \u2192 refl } }\n ; to\u2218from = \u03bb{ \u2200\u00acxy \u2192 refl }\n }\n\\end{code}\nIn the `to` direction, we are given a value `\u00ac\u2203xy` of type\n`\u00ac \u2203[ x ] B x`, and need to show that given a value\n`x` that `\u00ac B x` follows, in other words, from\na value `y` of type `B x` we can derive false. Combining\n`x` and `y` gives us a value `(x , y)` of type `\u2203[ x ] B x`,\nand applying `\u00ac\u2203xy` to that yields a contradiction.\n\nIn the `from` direction, we are given a value `\u2200\u00acxy` of type\n`\u2200 x \u2192 \u00ac B x`, and need to show that from a value `\u27e8 x , y \u27e9`\nof type `\u2203[ x ] B x` we can derive false. Applying `\u2200\u00acxy`\nto `x` gives a value of type `\u00ac B x`, and applying that to `y` yields\na contradiction.\n\nThe two inverse proofs are straightforward, where one direction\nrequires extensionality.\n\n\n#### Exercise `\u2203\u00ac-implies-\u00ac\u2200` (recommended)\n\nShow that existential of a negation implies negation of a universal:\n\\begin{code}\npostulate\n \u2203\u00ac-implies-\u00ac\u2200 : \u2200 {A : Set} {B : A \u2192 Set}\n \u2192 \u2203[ x ] (\u00ac B x)\n --------------\n \u2192 \u00ac (\u2200 x \u2192 B x)\n\\end{code}\nDoes the converse hold? If so, prove; if not, explain why.\n\n\n#### Exercise `Bin-isomorphism` (stretch) {#Bin-isomorphism}\n\nRecall that Exercises\n[Bin][plfa.Naturals#Bin],\n[Bin-laws][plfa.Induction#Bin-laws], and\n[Bin-predicates][plfa.Relations#Bin-predicates]\ndefine a datatype of bitstrings representing natural numbers:\n\\begin{code}\ndata Bin : Set where\n nil : Bin\n x0_ : Bin \u2192 Bin\n x1_ : Bin \u2192 Bin\n\\end{code}\nAnd ask you to define the following functions and predicates:\n\n to : \u2115 \u2192 Bin\n from : Bin \u2192 \u2115\n Can : Bin \u2192 Set\n\nAnd to establish the following properties:\n\n from (to n) \u2261 n\n\n ----------\n Can (to n)\n\n Can x\n ---------------\n to (from x) \u2261 x\n\nUsing the above, establish that there is an isomorphism between `\u2115` and\n`\u2203[ x ](Can x)`.\n\n\n## Standard Prelude\n\nDefinitions similar to those in this chapter can be found in the standard library:\n\\begin{code}\nimport Data.Product using (\u03a3; _,_; \u2203; \u03a3-syntax; \u2203-syntax)\n\\end{code}\n\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u03a0 U+03A0 GREEK CAPITAL LETTER PI (\\Pi)\n \u03a3 U+03A3 GREEK CAPITAL LETTER SIGMA (\\Sigma)\n \u2203 U+2203 THERE EXISTS (\\ex, \\exists)\n","avg_line_length":33.6113537118,"max_line_length":82,"alphanum_fraction":0.6497986228} +{"size":2632,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"---\ntitle : \"PUC-Rio: Course notes\"\nlayout : page\npermalink : \/PUC\/\n---\n\n## Staff\n\n* **Instructor**\n [Philip Wadler](https:\/\/homepages.inf.ed.ac.uk\/wadler)\n\n## Schedule\n\nLectures and tutorials take place Fridays and some Thursdays in 548L.\n* **13.00--14.30pm** Lecture\n* **14.30--16.00pm** Tutorial\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Fri 29 Mar<\/b><\/td>\n Naturals<\/a><\/td>\n <\/tr>\n
Fri 5 Apr<\/b><\/td>\n Induction<\/a> & Relations<\/a><\/td>\n <\/tr>\n
Thu 11 Apr<\/b><\/td>\n Relations<\/a><\/td>\n <\/tr>\n
Fri 19 Apr<\/b><\/td>\n (Miami)<\/td>\n <\/tr>\n
Fri 26 Apr<\/b><\/td>\n Equality<\/a> &\n Isomorphism<\/a> &\n Connectives<\/a><\/td> \n <\/tr>\n
Fri 3 May<\/b><\/td>\n Negation<\/a> &\n Quantifiers<\/a> &\n Decidable<\/a> &\n Lists<\/a><\/td>\n <\/tr>\n
Fri 10 May<\/b><\/td>\n (Melbourne)<\/td>\n <\/tr>\n
Fri 17 May<\/b><\/td>\n (Sydney)<\/td>\n <\/tr>\n
Fri 24 May<\/b><\/td>\n Lambda<\/a> &\n Properties<\/a><\/td>\n <\/tr>\n
Fri 31 May<\/b><\/td>\n (Padova)<\/td>\n <\/tr>\n
Fri 7 June<\/b><\/td>\n DeBruijn<\/a> &\n More<\/a><\/td>\n <\/tr>\n
Fri 14 June<\/b><\/td>\n (Buenos Aires)<\/td>\n <\/tr>\n
Fri 21 June<\/b><\/td>\n Inference<\/a> &\n Untyped<\/a><\/td>\n <\/tr>\n
Fri 28 June<\/b><\/td>\n Propositions as Types & mock exam<\/td>\n <\/tr>\n
Fri 5 July<\/b><\/td>\n exam<\/td>\n <\/tr>\n<\/table>\n\n\n## Assignments\n\nFor instructions on how to set up Agda for PLFA see [Getting Started](\/GettingStarted\/).\n\n* [PUC-Assignment 1](\/PUC-Assignment1\/) due Friday 26 April.\n* [PUC-Assignment 2](\/PUC-Assignment2\/) due Wednesday 22 May.\n* [PUC-Assignment 3](\/PUC-Assignment3\/) due Wednesday 12 June.\n* [PUC-Assignment 4](\/PUC-Assignment4\/) due Tuesday 25 June.\n* [Assignment 5](\/tspl\/Assignment5.pdf) due Tuesday 25 June.\n Use file [Exam][Exam]. Despite the rubric, do **all three questions**.\n\nSubmit assignments by email to [wadler@inf.ed.ac.uk](mailto:wadler@inf.ed.ac.uk).\nAttach a single file named Assignment1.lagda or the like. Include\nyour name and email in the submitted file. Ignore instructions to use\n\"submit\".\n","avg_line_length":25.0666666667,"max_line_length":88,"alphanum_fraction":0.592325228} +{"size":3533,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Type.BetaNormal where\n\\end{code}\n\n## Fixity declarations\n\nTo begin, we get all our infix declarations out of the way.\n\\begin{code}\ninfix 4 _\u22a2Nf\u22c6_\ninfix 4 _\u22a2Ne\u22c6_\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nopen import Type.RenamingSubstitution\nopen import Builtin.Constant.Type\n\nopen import Relation.Binary.PropositionalEquality\n renaming (subst to substEq) using (_\u2261_; refl; cong; cong\u2082; trans; sym)\nopen import Function\nopen import Agda.Builtin.Nat\n\\end{code}\n\n## Type \u03b2-normal forms\n\nWe mutually define normal forms and neutral terms. It is guaranteed\nthat not further beta reductions are possible. Neutral terms can be\nvariables, neutral applications (where the term in the function\nposition cannot be a lambda), or recursive types. Normal forms can be\npi types, function types, lambdas or neutral terms.\n\n\\begin{code}\nopen import Data.String\n\ndata _\u22a2Nf\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Set\n\ndata _\u22a2Ne\u22c6_ : Ctx\u22c6 \u2192 Kind \u2192 Set where\n ` : \u2200 {\u03a6 J}\n \u2192 \u03a6 \u220b\u22c6 J\n --------\n \u2192 \u03a6 \u22a2Ne\u22c6 J\n\n _\u00b7_ : \u2200{\u03a6 K J}\n \u2192 \u03a6 \u22a2Ne\u22c6 (K \u21d2 J)\n \u2192 \u03a6 \u22a2Nf\u22c6 K\n ------\n \u2192 \u03a6 \u22a2Ne\u22c6 J\n\n \u03bc1 : \u2200{\u03c6 K}\n ---------------------------------\n \u2192 \u03c6 \u22a2Ne\u22c6 ((K \u21d2 *) \u21d2 K \u21d2 *) \u21d2 K \u21d2 *\n\ndata _\u22a2Nf\u22c6_ where\n\n \u03a0 : \u2200 {\u03a6 K}\n \u2192 String\n \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *\n -----------\n \u2192 \u03a6 \u22a2Nf\u22c6 *\n\n _\u21d2_ : \u2200 {\u03a6}\n \u2192 \u03a6 \u22a2Nf\u22c6 *\n \u2192 \u03a6 \u22a2Nf\u22c6 *\n ------\n \u2192 \u03a6 \u22a2Nf\u22c6 *\n\n \u019b : \u2200 {\u03a6 K J}\n \u2192 String\n \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J\n -----------\n \u2192 \u03a6 \u22a2Nf\u22c6 (K \u21d2 J)\n\n ne : \u2200{\u03c6 K}\n \u2192 \u03c6 \u22a2Ne\u22c6 K\n --------\n \u2192 \u03c6 \u22a2Nf\u22c6 K\n\n con : \u2200{\u03c6} \u2192 TyCon \u2192 \u03c6 \u22a2Nf\u22c6 *\n\n\\end{code}\n\n# Renaming\n\nWe need to be able to weaken (introduce a new variable into the\ncontext) in normal forms so we define renaming which subsumes\nweakening.\n\n\\begin{code}\nrenNf : \u2200 {\u03a6 \u03a8}\n \u2192 Ren \u03a6 \u03a8\n -----------------------------\n \u2192 (\u2200 {J} \u2192 \u03a6 \u22a2Nf\u22c6 J \u2192 \u03a8 \u22a2Nf\u22c6 J)\nrenNe : \u2200 {\u03a6 \u03a8}\n \u2192 Ren \u03a6 \u03a8\n -------------------------------\n \u2192 (\u2200 {J} \u2192 \u03a6 \u22a2Ne\u22c6 J \u2192 \u03a8 \u22a2Ne\u22c6 J)\n\nrenNf \u03c1 (\u03a0 x A) = \u03a0 x (renNf (ext \u03c1) A)\nrenNf \u03c1 (A \u21d2 B) = renNf \u03c1 A \u21d2 renNf \u03c1 B\nrenNf \u03c1 (\u019b x B) = \u019b x (renNf (ext \u03c1) B)\nrenNf \u03c1 (ne A) = ne (renNe \u03c1 A)\nrenNf \u03c1 (con tcn) = con tcn\n\nrenNe \u03c1 (` x) = ` (\u03c1 x)\nrenNe \u03c1 (A \u00b7 x) = renNe \u03c1 A \u00b7 renNf \u03c1 x\nrenNe \u03c1 \u03bc1 = \u03bc1\n\\end{code}\n\n\\begin{code}\nweakenNf : \u2200 {\u03a6 J K}\n \u2192 \u03a6 \u22a2Nf\u22c6 J\n -------------\n \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J\nweakenNf = renNf S\n\\end{code}\n\nEmbedding normal forms back into terms\n\n\\begin{code}\nembNf : \u2200{\u0393 K} \u2192 \u0393 \u22a2Nf\u22c6 K \u2192 \u0393 \u22a2\u22c6 K\nembNe : \u2200{\u0393 K} \u2192 \u0393 \u22a2Ne\u22c6 K \u2192 \u0393 \u22a2\u22c6 K\n\nembNf (\u03a0 x B) = \u03a0 x (embNf B)\nembNf (A \u21d2 B) = embNf A \u21d2 embNf B\nembNf (\u019b x B) = \u019b x (embNf B)\nembNf (ne B) = embNe B\nembNf (con tcn) = con tcn\n\nembNe (` x) = ` x\nembNe (A \u00b7 B) = embNe A \u00b7 embNf B\nembNe \u03bc1 = \u03bc1\n\\end{code}\n\n\\begin{code}\nren-embNf : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c1 : Ren \u03a6 \u03a8)\n \u2192 \u2200 {J}\n \u2192 (n : \u03a6 \u22a2Nf\u22c6 J)\n -----------------------------------------\n \u2192 embNf (renNf \u03c1 n) \u2261\u03b1 ren \u03c1 (embNf n)\n\nren-embNe : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c1 : Ren \u03a6 \u03a8)\n \u2192 \u2200 {J}\n \u2192 (n : \u03a6 \u22a2Ne\u22c6 J)\n --------------------------------------------\n \u2192 embNe (renNe \u03c1 n) \u2261\u03b1 ren \u03c1 (embNe n)\n\nren-embNf \u03c1 (\u03a0 x B) = \u03a0\u2261\u03b1 (ren-embNf (ext \u03c1) B)\nren-embNf \u03c1 (A \u21d2 B) = \u21d2\u2261\u03b1 (ren-embNf \u03c1 A) (ren-embNf \u03c1 B)\nren-embNf \u03c1 (\u019b x B) = \u019b\u2261\u03b1 (ren-embNf (ext \u03c1) B)\nren-embNf \u03c1 (ne n) = ren-embNe \u03c1 n\nren-embNf \u03c1 (con tcn ) = con\u2261\u03b1 -- refl\n\nren-embNe \u03c1 (` x) = var\u2261\u03b1 refl -- refl\nren-embNe \u03c1 (n \u00b7 n') = \u00b7\u2261\u03b1 (ren-embNe \u03c1 n) (ren-embNf \u03c1 n')\nren-embNe \u03c1 \u03bc1 = \u03bc\u2261\u03b1 -- refl\n\\end{code}\n\n# Assemblies\n\n\\begin{code}\nbooleanNf : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 *\nbooleanNf = \u03a0 \"\u03b1\" (ne (` Z) \u21d2 ne (` Z) \u21d2 ne (` Z))\n\\end{code}\n","avg_line_length":21.0297619048,"max_line_length":72,"alphanum_fraction":0.5035380696} +{"size":7008,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"\\begin{code}\nmodule Algorithmic.Examples where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nopen import Type.BetaNormal\nopen import Type.BetaNBE.RenamingSubstitution\nimport Type.RenamingSubstitution as \u22c6\nopen import Algorithmic\nopen import Algorithmic.RenamingSubstitution\nopen import Algorithmic.Evaluation\n\nopen import Relation.Binary.PropositionalEquality renaming (subst to substEq)\n\\end{code}\n\n## Examples\n\n### Scott Numerals\n\nFrom http:\/\/lucacardelli.name\/Papers\/Notes\/scott2.pdf\n\nM = \u03bc X . G X\nG X = \u2200 R. R \u2192 (X \u2192 R) \u2192 R)\n\u03bc X . G X = \u2200 X . (G X \u2192 X) \u2192 X -- what is the status of this?\nN = G M\nin : N \u2192 M\nout : M \u2192 N\n\n0 = \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . x\n : N\nsucc = \u03bb n : N . \u039b R . \u03bb x : R . \u03bb y : M \u2192 R . y (in n)\n : N \u2192 N\ncase = \u03bb n : N . \u039b R . \u03bb a : R . \u03bb f : N \u2192 N . n [R] a (f \u2218 out)\n : N \u2192 \u2200 R . R \u2192 (N \u2192 R) \u2192 R\n\n\n--\n\n\\begin{code}\n-- bound variable names inserted below are not meaningful\nmodule Scott where\n open import Type.BetaNBE\n open import Type.BetaNBE.Stability\n\n _\u00b7Nf_ : \u2200{\u0393}{K J}\n \u2192 \u0393 \u22a2Nf\u22c6 K \u21d2 J\n \u2192 \u0393 \u22a2Nf\u22c6 K\n \u2192 \u0393 \u22a2Nf\u22c6 J\n f \u00b7Nf a = nf (embNf f \u00b7 embNf a)\n\n \u03bc0 : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 (* \u21d2 *) \u21d2 *\n \u03bc0 = \u019b \"x\" (ne (\u03bc1 \u00b7 \u019b \"t\" (\u019b \"z\" (ne (` Z \u00b7 ne (` (S Z) \u00b7 ne (` Z))))) \u00b7 ne (` Z)))\n\n wrap0 : \u2200{\u03a6 \u0393}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 * \u21d2 *)\n \u2192 \u0393 \u22a2 A \u00b7Nf (\u03bc0 \u00b7Nf A)\n \u2192 \u0393 \u22a2 \u03bc0 \u00b7Nf A\n wrap0 A X rewrite stability A = wrap1 _ A X\n\n unwrap0 : \u2200{\u03a6 \u0393}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 * \u21d2 *)\n \u2192 \u0393 \u22a2 \u03bc0 \u00b7Nf A\n \u2192 \u0393 \u22a2 A \u00b7Nf (\u03bc0 \u00b7Nf A)\n unwrap0 A X rewrite stability A = unwrap1 X\n \n G : \u2200{\u0393} \u2192 \u0393 ,\u22c6 * \u22a2Nf\u22c6 *\n G = \u03a0 \"\u03b1\" (ne (` Z) \u21d2 (ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` Z))\n \n M : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 *\n M = \u03bc0 \u00b7Nf \u019b \"x\" G\n\n N : \u2200{\u0393} \u2192 \u0393 \u22a2Nf\u22c6 *\n N = G [ M ]Nf\n\n Zero : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n Zero = \u039b \"\u03b1\" (\u019b \"x\" (\u019b \"y\" (` (S (Z )))))\n\n\n Succ : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 N\n Succ = \u019b \"x\" (\u039b \"\u03b1\" (\u019b \"y\" (\u019b \"z\" (` Z \u00b7 wrap0 (\u019b \"x\" G) (` (S (S (T Z))))))))\n\n One : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n One = Succ \u00b7 Zero\n \n Two : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n case : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 (\u03a0 \"\u03b1\" (ne (` Z) \u21d2 (N \u21d2 ne (` Z)) \u21d2 ne (` Z)))\n case = \u019b \"x\" (\u039b \"\u03b1\" (\u019b \"y\" (\u019b \"z\" ((` (S (S (T Z)))) \u00b7\u22c6 ne (` Z) \u00b7 (` (S Z)) \u00b7 (\u019b \"a\" (` (S Z) \u00b7 unwrap0 (\u019b \"x\" G) (` Z) ))))))\n\n{-\n Y-comb : \u2200{\u0393} \u2192 \u0393 \u22a2 \u03a0 ((ne (` Z) \u21d2 ne (` Z)) \u21d2 ne (` Z))\n Y-comb = \u039b (\u019b ((\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) \u00b7 wrap (ne (` Z) \u21d2 ne (` (S Z))) \u2022 (\u019b (` (S Z) \u00b7 (unwrap \u2022 refl (` Z) \u00b7 (` Z)))) refl ))\n-}\n Z-comb : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192\n \u0393 \u22a2 \u03a0 \"\u03b1\" (\u03a0 \"\u03b2\" (((ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` (S Z)) \u21d2 ne (` Z)) \u21d2 ne (` (S Z)) \u21d2 ne (` Z)))\n Z-comb = \u039b \"\u03b1\" (\u039b \"\u03b2\" (\u019b \"f\" (\u019b \"r\" (` (S Z) \u00b7 \u019b \"x\" (unwrap0 (\u019b \"y\" (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z)) \u00b7 wrap0 (\u019b \"y\" (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (\u019b \"r\" (` (S Z) \u00b7 \u019b \"x\" (unwrap0 (\u019b \"y\" (ne (` Z) \u21d2 ne (` (S (S Z))) \u21d2 ne (` (S Z)))) (` (S Z)) \u00b7 ` (S Z) \u00b7 ` Z))))))\n\n OnePlus : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 (N \u21d2 N) \u21d2 N \u21d2 N\n OnePlus = \u019b \"x\" (\u019b \"y\" ((((case \u00b7 (` Z)) \u00b7\u22c6 N) \u00b7 One) \u00b7 (\u019b \"z\" (Succ \u00b7 (` (S (S Z)) \u00b7 (` Z))))))\n\n OnePlusOne : \u2205 \u22a2 N\n OnePlusOne = (Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 OnePlus \u00b7 One\n\n -- Roman's more efficient version\n Plus : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N \u21d2 N \u21d2 N\n Plus = \u019b \"x\" (\u019b \"y\" ((Z-comb \u00b7\u22c6 N) \u00b7\u22c6 N \u00b7 (\u019b \"x\" (\u019b \"y\" ((((case \u00b7 ` Z) \u00b7\u22c6 N) \u00b7 ` (S (S (S Z)))) \u00b7 (\u019b \"x\" (Succ \u00b7 (` (S (S Z)) \u00b7 ` Z)))))) \u00b7 ` (S Z)))\n\n TwoPlusTwo : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 N\n TwoPlusTwo = (Plus \u00b7 Two) \u00b7 Two\n\\end{code}\n\neval (gas 10000000) Scott.Four\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\neval (gas 10000000) Scott.Two\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n wrap (\u03a0 (` Z) \u21d2 ((` (S Z)) \u21d2 (` Z)) \u21d2 (` Z))\n (\u039b (\u019b (\u019b (` (S Z)))))))))))))\n .Term.Reduction.Value.V-\u039b_)\n\n### Church Numerals\n\n\\begin{code}\nmodule Church where\n\n N : \u2200{\u03a6} \u2192 \u03a6 \u22a2Nf\u22c6 *\n N = \u03a0 \"\u03b1\" ((ne (` Z)) \u21d2 (ne (` Z) \u21d2 ne (` Z)) \u21d2 (ne (` Z)))\n\n Zero : \u2205 \u22a2 N\n Zero = \u039b \"\u03b1\" (\u019b \"x\" (\u019b \"y\" (` (S Z))))\n\n Succ : \u2205 \u22a2 N \u21d2 N\n Succ = \u019b \"x\" (\u039b \"\u03b1\" (\u019b \"y\" (\u019b \"z\" (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (ne (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n \n Iter : \u2205 \u22a2 \u03a0 \"\u03b1\" (ne (` Z) \u21d2 (ne (` Z) \u21d2 ne (` Z)) \u21d2 N \u21d2 ne (` Z))\n Iter = \u039b \"\u03b1\" (\u019b \"x\" (\u019b \"y\" (\u019b \"z\" ((` Z) \u00b7\u22c6 ne (` Z) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\n -- two plus two\n One : \u2205 \u22a2 N\n One = Succ \u00b7 Zero\n\n Two : \u2205 \u22a2 N\n Two = Succ \u00b7 One\n\n Three : \u2205 \u22a2 N\n Three = Succ \u00b7 Two\n\n Four : \u2205 \u22a2 N\n Four = Succ \u00b7 Three\n\n Plus : \u2205 \u22a2 N \u2192 \u2205 \u22a2 N \u2192 \u2205 \u22a2 N\n Plus x y = Iter \u00b7\u22c6 N \u00b7 x \u00b7 Succ \u00b7 y -- by induction on the second y\n\n TwoPlusTwo = Plus Two Two\n\n TwoPlusTwo' : \u2205 \u22a2 N\n TwoPlusTwo' = Two \u00b7\u22c6 N \u00b7 Two \u00b7 Succ\n\nopen Church public\n\\end{code}\n\n-- Church \"4\"\neval (gas 100000000) Four\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using iterator\neval (gas 100000000) (Plus Two Two)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7\n (((\u039b\n (\u019b\n (\u019b\n (` Z) \u00b7 (((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z)) \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z))\n \u00b7 (` Z)))))\n V-\u039b_)\n\n-- Church \"2 + 2\" using the numerals directly\neval (gas 10000000) (Two \u00b7\u22c6 N \u00b7 Two \u00b7 Succ)\n\n(done\n (\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b\n (\u019b\n (\u019b\n ((` Z) \u00b7\n ((((\u039b (\u019b (\u019b (` (S Z))))) \u00b7\u22c6 (` Z)) \u00b7 (` (S Z))) \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n \u00b7\u22c6 (` Z))\n \u00b7 (` (S Z)))\n \u00b7 (` Z))))))\n V-\u039b_)\n","avg_line_length":23.6756756757,"max_line_length":325,"alphanum_fraction":0.3266267123} +{"size":25894,"ext":"lagda","lang":"Literate Agda","max_stars_count":1003.0,"content":"---\ntitle : \"TypedBarendregt: Raw terms with types (broken)\"\nlayout : page\npermalink : \/TypedBarendregt\n---\n\nThis version uses raw terms. Substitution presumes that no\ngeneration of fresh names is required.\n\nThe substitution algorithm is based on one by McBride.\nIt is given a map from names to terms. Say the mapping of a\nname is trivial if it takes a name to a term consisting of\njust the variable with that name. No fresh names are required\nif the mapping on each variable is either trivial or to a\nclosed term. However, the proof of correctness currently\ncontains a hole, and may be difficult to repair.\n\n## Imports\n\n\\begin{code}\nmodule TypedBarendregt where\n\\end{code}\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong; cong\u2082; _\u2262_)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Data.List using (List; []; _\u2237_; _++_; map; foldr; filter)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _\u2294_; _\u2264_; _\u225f_)\nopen import Data.Nat.Properties using (\u2264-refl; \u2264-trans; m\u2264m\u2294n; n\u2264m\u2294n; 1+n\u2270n)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Function using (_\u2218_)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Negation using (\u00ac?)\nimport Collections\n\npattern [_] x = x \u2237 []\npattern [_,_] x y = x \u2237 y \u2237 []\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\n\\end{code}\n\n\n## Syntax\n\n\\begin{code}\ninfix 4 _\u220b_`:_\ninfix 4 _\u22a2_`:_\ninfixl 5 _,_`:_\ninfixr 6 _`\u2192_\ninfix 6 `\u03bb_`\u2192_\ninfixl 7 `if0_then_else_\ninfix 8 `suc_ `pred_ `Y_\ninfixl 9 _\u00b7_\n\nId : Set\nId = \u2115\n\ndata Type : Set where\n `\u2115 : Type\n _`\u2192_ : Type \u2192 Type \u2192 Type\n\ndata Ctx : Set where\n \u03b5 : Ctx\n _,_`:_ : Ctx \u2192 Id \u2192 Type \u2192 Ctx\n\ndata Term : Set where\n `_ : Id \u2192 Term\n `\u03bb_`\u2192_ : Id \u2192 Term \u2192 Term\n _\u00b7_ : Term \u2192 Term \u2192 Term\n `zero : Term \n `suc_ : Term \u2192 Term\n `pred_ : Term \u2192 Term\n `if0_then_else_ : Term \u2192 Term \u2192 Term \u2192 Term\n `Y_ : Term \u2192 Term\n\ndata _\u220b_`:_ : Ctx \u2192 Id \u2192 Type \u2192 Set where\n\n Z : \u2200 {\u0393 A x}\n --------------------\n \u2192 \u0393 , x `: A \u220b x `: A\n\n S : \u2200 {\u0393 A B x w}\n \u2192 w \u2262 x\n \u2192 \u0393 \u220b w `: B\n --------------------\n \u2192 \u0393 , x `: A \u220b w `: B\n\ndata _\u22a2_`:_ : Ctx \u2192 Term \u2192 Type \u2192 Set where\n\n Ax : \u2200 {\u0393 A x}\n \u2192 \u0393 \u220b x `: A\n --------------\n \u2192 \u0393 \u22a2 ` x `: A\n\n \u22a2\u03bb : \u2200 {\u0393 x N A B}\n \u2192 \u0393 , x `: A \u22a2 N `: B\n --------------------------\n \u2192 \u0393 \u22a2 (`\u03bb x `\u2192 N) `: A `\u2192 B\n\n _\u00b7_ : \u2200 {\u0393 L M A B}\n \u2192 \u0393 \u22a2 L `: A `\u2192 B\n \u2192 \u0393 \u22a2 M `: A\n ----------------\n \u2192 \u0393 \u22a2 L \u00b7 M `: B\n\n \u22a2zero : \u2200 {\u0393}\n ----------------\n \u2192 \u0393 \u22a2 `zero `: `\u2115\n\n \u22a2suc : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M `: `\u2115\n -----------------\n \u2192 \u0393 \u22a2 `suc M `: `\u2115\n\n \u22a2pred : \u2200 {\u0393 M}\n \u2192 \u0393 \u22a2 M `: `\u2115\n ------------------\n \u2192 \u0393 \u22a2 `pred M `: `\u2115\n\n \u22a2if0 : \u2200 {\u0393 L M N A}\n \u2192 \u0393 \u22a2 L `: `\u2115\n \u2192 \u0393 \u22a2 M `: A\n \u2192 \u0393 \u22a2 N `: A\n ------------------------------\n \u2192 \u0393 \u22a2 `if0 L then M else N `: A\n\n \u22a2Y : \u2200 {\u0393 M A}\n \u2192 \u0393 \u22a2 M `: A `\u2192 A\n ----------------\n \u2192 \u0393 \u22a2 `Y M `: A\n\\end{code}\n\n## Test examples\n\n\\begin{code}\nm n p s z : Id\nm = 0\nn = 1\np = 2\ns = 3\nz = 4\n\ns\u2262z : s \u2262 z\ns\u2262z ()\n\nn\u2262z : n \u2262 z\nn\u2262z ()\n\nn\u2262s : n \u2262 s\nn\u2262s ()\n\nm\u2262z : m \u2262 z\nm\u2262z ()\n\nm\u2262s : m \u2262 s\nm\u2262s ()\n\nm\u2262n : m \u2262 n\nm\u2262n ()\n\np\u2262n : p \u2262 n\np\u2262n ()\n\np\u2262m : p \u2262 m\np\u2262m ()\n\ntwo : Term\ntwo = `suc `suc `zero\n\n\u22a2two : \u03b5 \u22a2 two `: `\u2115\n\u22a2two = \u22a2suc (\u22a2suc \u22a2zero)\n\nplus : Term\nplus = `Y (`\u03bb p `\u2192 `\u03bb m `\u2192 `\u03bb n `\u2192\n `if0 ` m then ` n else `suc (` p \u00b7 (`pred (` m)) \u00b7 ` n))\n\n\u22a2plus : \u03b5 \u22a2 plus `: `\u2115 `\u2192 `\u2115 `\u2192 `\u2115\n\u22a2plus = (\u22a2Y (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2if0 (Ax \u22a2m) (Ax \u22a2n) (\u22a2suc (Ax \u22a2p \u00b7 (\u22a2pred (Ax \u22a2m)) \u00b7 Ax \u22a2n)))))))\n where\n \u22a2p = S p\u2262n (S p\u2262m Z)\n \u22a2m = S m\u2262n Z\n \u22a2n = Z\n\nfour : Term\nfour = plus \u00b7 two \u00b7 two\n\n\u22a2four : \u03b5 \u22a2 four `: `\u2115\n\u22a2four = \u22a2plus \u00b7 \u22a2two \u00b7 \u22a2two\n\nCh : Type\nCh = (`\u2115 `\u2192 `\u2115) `\u2192 `\u2115 `\u2192 `\u2115\n\ntwoCh : Term\ntwoCh = `\u03bb s `\u2192 `\u03bb z `\u2192 (` s \u00b7 (` s \u00b7 ` z))\n\n\u22a2twoCh : \u03b5 \u22a2 twoCh `: Ch\n\u22a2twoCh = (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2s \u00b7 (Ax \u22a2s \u00b7 Ax \u22a2z))))\n where\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nplusCh : Term\nplusCh = `\u03bb m `\u2192 `\u03bb n `\u2192 `\u03bb s `\u2192 `\u03bb z `\u2192\n ` m \u00b7 ` s \u00b7 (` n \u00b7 ` s \u00b7 ` z)\n\n\u22a2plusCh : \u03b5 \u22a2 plusCh `: Ch `\u2192 Ch `\u2192 Ch\n\u22a2plusCh = (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (\u22a2\u03bb (Ax \u22a2m \u00b7 Ax \u22a2s \u00b7 (Ax \u22a2n \u00b7 Ax \u22a2s \u00b7 Ax \u22a2z))))))\n where\n \u22a2m = S m\u2262z (S m\u2262s (S m\u2262n Z))\n \u22a2n = S n\u2262z (S n\u2262s Z)\n \u22a2s = S s\u2262z Z\n \u22a2z = Z\n\nfromCh : Term\nfromCh = `\u03bb m `\u2192 ` m \u00b7 (`\u03bb s `\u2192 `suc ` s) \u00b7 `zero\n\n\u22a2fromCh : \u03b5 \u22a2 fromCh `: Ch `\u2192 `\u2115\n\u22a2fromCh = (\u22a2\u03bb (Ax \u22a2m \u00b7 (\u22a2\u03bb (\u22a2suc (Ax \u22a2s))) \u00b7 \u22a2zero))\n where\n \u22a2m = Z\n \u22a2s = Z\n\nfourCh : Term\nfourCh = fromCh \u00b7 (plusCh \u00b7 twoCh \u00b7 twoCh)\n\n\u22a2fourCh : \u03b5 \u22a2 fourCh `: `\u2115\n\u22a2fourCh = \u22a2fromCh \u00b7 (\u22a2plusCh \u00b7 \u22a2twoCh \u00b7 \u22a2twoCh)\n\\end{code}\n\n\n## Erasure\n\n\\begin{code}\nlookup : \u2200 {\u0393 x A} \u2192 \u0393 \u220b x `: A \u2192 Id\nlookup {\u0393 , x `: A} Z = x\nlookup {\u0393 , x `: A} (S w\u2262 \u22a2w) = lookup {\u0393} \u22a2w\n\nerase : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M `: A \u2192 Term\nerase (Ax \u22a2w) = ` lookup \u22a2w\nerase (\u22a2\u03bb {x = x} \u22a2N) = `\u03bb x `\u2192 erase \u22a2N\nerase (\u22a2L \u00b7 \u22a2M) = erase \u22a2L \u00b7 erase \u22a2M\nerase (\u22a2zero) = `zero\nerase (\u22a2suc \u22a2M) = `suc (erase \u22a2M)\nerase (\u22a2pred \u22a2M) = `pred (erase \u22a2M)\nerase (\u22a2if0 \u22a2L \u22a2M \u22a2N) = `if0 (erase \u22a2L) then (erase \u22a2M) else (erase \u22a2N)\nerase (\u22a2Y \u22a2M) = `Y (erase \u22a2M)\n\\end{code}\n\n### Properties of erasure\n\n\\begin{code}\ncong\u2083 : \u2200 {A B C D : Set} (f : A \u2192 B \u2192 C \u2192 D) {s t u v x y} \u2192 \n s \u2261 t \u2192 u \u2261 v \u2192 x \u2261 y \u2192 f s u x \u2261 f t v y\ncong\u2083 f refl refl refl = refl\n\nlookup-lemma : \u2200 {\u0393 x A} \u2192 (\u22a2x : \u0393 \u220b x `: A) \u2192 lookup \u22a2x \u2261 x\nlookup-lemma Z = refl\nlookup-lemma (S w\u2262 \u22a2w) = lookup-lemma \u22a2w\n\nerase-lemma : \u2200 {\u0393 M A} \u2192 (\u22a2M : \u0393 \u22a2 M `: A) \u2192 erase \u22a2M \u2261 M\nerase-lemma (Ax \u22a2x) = cong `_ (lookup-lemma \u22a2x)\nerase-lemma (\u22a2\u03bb {x = x} \u22a2N) = cong (`\u03bb x `\u2192_) (erase-lemma \u22a2N)\nerase-lemma (\u22a2L \u00b7 \u22a2M) = cong\u2082 _\u00b7_ (erase-lemma \u22a2L) (erase-lemma \u22a2M)\nerase-lemma (\u22a2zero) = refl\nerase-lemma (\u22a2suc \u22a2M) = cong `suc_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2pred \u22a2M) = cong `pred_ (erase-lemma \u22a2M)\nerase-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) = cong\u2083 `if0_then_else_\n (erase-lemma \u22a2L) (erase-lemma \u22a2M) (erase-lemma \u22a2N)\nerase-lemma (\u22a2Y \u22a2M) = cong `Y_ (erase-lemma \u22a2M)\n\\end{code}\n\n\n## Substitution\n\n### Lists as sets\n\n\\begin{code}\nopen Collections (Id) (_\u225f_) hiding (_\u2209_)\n\\end{code}\n\n### Free variables\n\n\\begin{code}\nfree : Term \u2192 List Id\nfree (` x) = [ x ]\nfree (`\u03bb x `\u2192 N) = free N \\\\ x\nfree (L \u00b7 M) = free L ++ free M\nfree (`zero) = []\nfree (`suc M) = free M\nfree (`pred M) = free M\nfree (`if0 L then M else N) = free L ++ free M ++ free N\nfree (`Y M) = free M\n\\end{code}\n\n### Maps\n\n\\begin{code}\ntm-\u2205 : Id \u2192 Term\ntm-\u2205 x = ` x\n\nid-\u2205 : Id \u2192 Id\nid-\u2205 x = x\n\ninfixl 5 _,_\u21a6_\n\n_,_\u21a6_ : \u2200 {X : Set} \u2192 (Id \u2192 X) \u2192 Id \u2192 X \u2192 (Id \u2192 X)\n(\u03c1 , x \u21a6 y) w with w \u225f x\n... | yes _ = y\n... | no _ = \u03c1 w\n\\end{code}\n\n### Fresh identifier\n\n\\begin{code}\nfresh : List Id \u2192 Id\nfresh = foldr _\u2294_ 0 \u2218 map suc\n\n\u2294-lemma : \u2200 {w xs} \u2192 w \u2208 xs \u2192 suc w \u2264 fresh xs\n\u2294-lemma {_} {_ \u2237 xs} here = m\u2264m\u2294n _ (fresh xs)\n\u2294-lemma {_} {_ \u2237 xs} (there x\u2208) = \u2264-trans (\u2294-lemma x\u2208) (n\u2264m\u2294n _ (fresh xs))\n\nfresh-lemma : \u2200 {x xs} \u2192 x \u2208 xs \u2192 x \u2262 fresh xs\nfresh-lemma x\u2208 refl = 1+n\u2270n (\u2294-lemma x\u2208)\n\\end{code}\n\n### Imposing the Barendregt Convention\n\n\\begin{code}\nrename : List Id \u2192 (Id \u2192 Id) \u2192 Term \u2192 Term\nrename ys \u03c3 (` x) = ` \u03c3 x\nrename ys \u03c3 (`\u03bb x `\u2192 M) = `\u03bb y `\u2192 rename (y \u2237 ys) (\u03c3 , x \u21a6 y) M\n where\n y = fresh ys \nrename ys \u03c3 (L \u00b7 M) = rename ys \u03c3 L \u00b7 rename ys \u03c3 M\nrename ys \u03c3 `zero = `zero\nrename ys \u03c3 (`suc M) = `suc (rename ys \u03c3 M)\nrename ys \u03c3 (`pred M) = `pred (rename ys \u03c3 M)\nrename ys \u03c3 (`if0 L then M else N) =\n `if0 (rename ys \u03c3 L) then (rename ys \u03c3 M) else (rename ys \u03c3 N) \nrename ys \u03c3 (`Y M) = `Y (rename ys \u03c3 M)\n\nbarendregt : Term \u2192 Term\nbarendregt M = rename (free M) id-\u2205 M\n\\end{code}\n\nSome test cases.\n\\begin{code}\n_ : barendregt plus \u2261\n `Y (`\u03bb 0 `\u2192 (`\u03bb 1 `\u2192 (`\u03bb 2 `\u2192\n `if0 ` 1 then ` 2 else `suc (` 0) \u00b7 (`pred (` 1)) \u00b7 (` 2))))\n_ = refl\n\n_ : barendregt plusCh \u2261\n (`\u03bb 0 `\u2192 (`\u03bb 1 `\u2192 (`\u03bb 2 `\u2192 (`\u03bb 3 `\u2192 (` 0) \u00b7 (` 2) \u00b7 ((` 1) \u00b7 (` 2) \u00b7 (` 3))))))\n_ = refl\n\\end{code}\n\n\n### Substitution\n\n\\begin{code}\nsubst : (Id \u2192 Term) \u2192 Term \u2192 Term\nsubst \u03c1 (` x) = \u03c1 x\nsubst \u03c1 (`\u03bb x `\u2192 N) = `\u03bb x `\u2192 subst (\u03c1 , x \u21a6 ` x) N\nsubst \u03c1 (L \u00b7 M) = subst \u03c1 L \u00b7 subst \u03c1 M\nsubst \u03c1 (`zero) = `zero\nsubst \u03c1 (`suc M) = `suc (subst \u03c1 M)\nsubst \u03c1 (`pred M) = `pred (subst \u03c1 M)\nsubst \u03c1 (`if0 L then M else N)\n = `if0 (subst \u03c1 L) then (subst \u03c1 M) else (subst \u03c1 N)\nsubst \u03c1 (`Y M) = `Y (subst \u03c1 M) \n \n_[_:=_] : Term \u2192 Id \u2192 Term \u2192 Term\nN [ x := M ] = subst (tm-\u2205 , x \u21a6 M) N\n\\end{code}\n\n### Testing substitution\n\n\\begin{code}\n_ : (` s \u00b7 ` s \u00b7 ` z) [ z := `zero ] \u2261 (` s \u00b7 ` s \u00b7 `zero)\n_ = refl\n\n_ : (` s \u00b7 ` s \u00b7 ` z) [ s := (`\u03bb m `\u2192 `suc ` m) ] [ z := `zero ] \n \u2261 (`\u03bb m `\u2192 `suc ` m) \u00b7 (`\u03bb m `\u2192 `suc ` m) \u00b7 `zero\n_ = refl\n\n_ : (`\u03bb m `\u2192 ` m \u00b7 ` n) [ n := ` s \u00b7 ` z ]\n \u2261 `\u03bb m `\u2192 ` m \u00b7 (` s \u00b7 ` z)\n_ = refl\n\n_ : subst (tm-\u2205 , m \u21a6 two , n \u21a6 four) (` m \u00b7 ` n) \u2261 (two \u00b7 four)\n_ = refl\n\\end{code}\n\n\n## Values\n\n\\begin{code}\ndata Value : Term \u2192 Set where\n\n Zero :\n ----------\n Value `zero\n\n Suc : \u2200 {V}\n \u2192 Value V\n --------------\n \u2192 Value (`suc V)\n \n Fun : \u2200 {x N}\n ---------------\n \u2192 Value (`\u03bb x `\u2192 N)\n\\end{code}\n\n## Reduction\n\n\\begin{code}\ninfix 4 _\u27f6_\n\ndata _\u27f6_ : Term \u2192 Term \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {L L\u2032 M}\n \u2192 L \u27f6 L\u2032\n -----------------\n \u2192 L \u00b7 M \u27f6 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {V M M\u2032}\n \u2192 Value V\n \u2192 M \u27f6 M\u2032\n -----------------\n \u2192 V \u00b7 M \u27f6 V \u00b7 M\u2032\n\n \u03b2-\u2192 : \u2200 {x N V}\n \u2192 Value V\n ---------------------------------\n \u2192 (`\u03bb x `\u2192 N) \u00b7 V \u27f6 N [ x := V ]\n\n \u03be-suc : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n -------------------\n \u2192 `suc M \u27f6 `suc M\u2032\n\n \u03be-pred : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n ---------------------\n \u2192 `pred M \u27f6 `pred M\u2032\n\n \u03b2-pred-zero :\n ----------------------\n `pred `zero \u27f6 `zero\n\n \u03b2-pred-suc : \u2200 {V}\n \u2192 Value V\n ---------------------\n \u2192 `pred (`suc V) \u27f6 V\n\n \u03be-if0 : \u2200 {L L\u2032 M N}\n \u2192 L \u27f6 L\u2032\n -----------------------------------------------\n \u2192 `if0 L then M else N \u27f6 `if0 L\u2032 then M else N\n\n \u03b2-if0-zero : \u2200 {M N}\n -------------------------------\n \u2192 `if0 `zero then M else N \u27f6 M\n \n \u03b2-if0-suc : \u2200 {V M N}\n \u2192 Value V\n ----------------------------------\n \u2192 `if0 (`suc V) then M else N \u27f6 N\n\n \u03be-Y : \u2200 {M M\u2032}\n \u2192 M \u27f6 M\u2032\n ---------------\n \u2192 `Y M \u27f6 `Y M\u2032\n\n \u03b2-Y : \u2200 {F x N}\n \u2192 F \u2261 `\u03bb x `\u2192 N\n -------------------------\n \u2192 `Y F \u27f6 N [ x := `Y F ]\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\ninfix 2 _\u27f6*_\ninfix 1 begin_\ninfixr 2 _\u27f6\u27e8_\u27e9_\ninfix 3 _\u220e\n\ndata _\u27f6*_ : Term \u2192 Term \u2192 Set where\n\n _\u220e : \u2200 (M : Term)\n -------------\n \u2192 M \u27f6* M\n\n _\u27f6\u27e8_\u27e9_ : \u2200 (L : Term) {M N}\n \u2192 L \u27f6 M\n \u2192 M \u27f6* N\n ---------\n \u2192 L \u27f6* N\n\nbegin_ : \u2200 {M N} \u2192 (M \u27f6* N) \u2192 (M \u27f6* N)\nbegin M\u27f6*N = M\u27f6*N\n\\end{code}\n\n## Sample execution\n\n\\begin{code}\n_ : plus \u00b7 two \u00b7 two \u27f6* (`suc (`suc (`suc (`suc `zero))))\n_ = \n begin\n plus \u00b7 two \u00b7 two\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl)) \u27e9\n (`\u03bb m `\u2192 (`\u03bb n `\u2192 `if0 ` m then ` n else\n `suc (plus \u00b7 (`pred (` m)) \u00b7 (` n)))) \u00b7 two \u00b7 two\n \u27f6\u27e8 \u03be-\u00b7\u2081 (\u03b2-\u2192 (Suc (Suc Zero))) \u27e9\n (`\u03bb n `\u2192 `if0 two then ` n else\n `suc (plus \u00b7 (`pred two) \u00b7 (` n))) \u00b7 two\n \u27f6\u27e8 \u03b2-\u2192 (Suc (Suc Zero)) \u27e9\n `if0 two then two else\n `suc (plus \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03b2-if0-suc (Suc Zero) \u27e9\n `suc (plus \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl))) \u27e9\n `suc ((`\u03bb m `\u2192 (`\u03bb n `\u2192 `if0 ` m then ` n else\n `suc (plus \u00b7 (`pred (` m)) \u00b7 (` n)))) \u00b7 (`pred two) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc (Suc Zero)))) \u27e9\n `suc ((`\u03bb m `\u2192 (`\u03bb n `\u2192 `if0 ` m then ` n else\n `suc (plus \u00b7 (`pred (` m)) \u00b7 (` n)))) \u00b7 (`suc `zero) \u00b7 two)\n \u27f6\u27e8 \u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u2192 (Suc Zero))) \u27e9\n `suc ((`\u03bb n `\u2192 `if0 `suc `zero then ` n else\n `suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 (` n)))) \u00b7 two\n \u27f6\u27e8 \u03be-suc (\u03b2-\u2192 (Suc (Suc Zero))) \u27e9\n `suc (`if0 `suc `zero then two else\n `suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03b2-if0-suc Zero) \u27e9\n `suc (`suc (plus \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2081 (\u03b2-Y refl)))) \u27e9\n `suc (`suc ((`\u03bb m `\u2192 (`\u03bb n `\u2192 `if0 ` m then ` n else \n `suc (plus \u00b7 (`pred (` m)) \u00b7 (` n)))) \u00b7 (`pred (`suc `zero)) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03be-\u00b7\u2082 Fun (\u03b2-pred-suc Zero)))) \u27e9\n `suc (`suc ((`\u03bb m `\u2192 (`\u03bb n `\u2192 `if0 ` m then ` n else\n `suc (plus \u00b7 (`pred (` m)) \u00b7 (` n)))) \u00b7 `zero \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03be-\u00b7\u2081 (\u03b2-\u2192 Zero))) \u27e9\n `suc (`suc ((`\u03bb n `\u2192 `if0 `zero then ` n else\n `suc (plus \u00b7 (`pred `zero) \u00b7 (` n))) \u00b7 two))\n \u27f6\u27e8 \u03be-suc (\u03be-suc (\u03b2-\u2192 (Suc (Suc Zero)))) \u27e9\n `suc (`suc (`if0 `zero then two else\n `suc (plus \u00b7 (`pred `zero) \u00b7 two)))\n \u27f6\u27e8 \u03be-suc (\u03be-suc \u03b2-if0-zero) \u27e9\n `suc (`suc (`suc (`suc `zero)))\n \u220e\n\\end{code}\n\n## Values do not reduce\n\nValues do not reduce.\n\\begin{code}\nVal-\u27f6 : \u2200 {M N} \u2192 Value M \u2192 \u00ac (M \u27f6 N)\nVal-\u27f6 Fun ()\nVal-\u27f6 Zero ()\nVal-\u27f6 (Suc VM) (\u03be-suc M\u27f6N) = Val-\u27f6 VM M\u27f6N\n\\end{code}\n\nAs a corollary, terms that reduce are not values.\n\\begin{code}\n\u27f6-Val : \u2200 {M N} \u2192 (M \u27f6 N) \u2192 \u00ac Value M\n\u27f6-Val M\u27f6N VM = Val-\u27f6 VM M\u27f6N\n\\end{code}\n\n## Reduction is deterministic\n\n\\begin{code}\ndet : \u2200 {M M\u2032 M\u2033}\n \u2192 (M \u27f6 M\u2032)\n \u2192 (M \u27f6 M\u2033)\n ----------\n \u2192 M\u2032 \u2261 M\u2033\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = cong\u2082 _\u00b7_ (det L\u27f6L\u2032 L\u27f6L\u2033) refl\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2082 VL _) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2032)\ndet (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03b2-\u2192 _) = \u22a5-elim (Val-\u27f6 Fun L\u27f6L\u2032)\ndet (\u03be-\u00b7\u2082 VL _) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2033)\ndet (\u03be-\u00b7\u2082 _ M\u27f6M\u2032) (\u03be-\u00b7\u2082 _ M\u27f6M\u2033) = cong\u2082 _\u00b7_ refl (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-\u00b7\u2082 _ M\u27f6M\u2032) (\u03b2-\u2192 VM) = \u22a5-elim (Val-\u27f6 VM M\u27f6M\u2032)\ndet (\u03b2-\u2192 VM) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 Fun L\u27f6L\u2033)\ndet (\u03b2-\u2192 VM) (\u03be-\u00b7\u2082 _ M\u27f6M\u2033) = \u22a5-elim (Val-\u27f6 VM M\u27f6M\u2033)\ndet (\u03b2-\u2192 _) (\u03b2-\u2192 _) = refl\ndet (\u03be-suc M\u27f6M\u2032) (\u03be-suc M\u27f6M\u2033) = cong `suc_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-pred M\u27f6M\u2032) (\u03be-pred M\u27f6M\u2033) = cong `pred_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-pred M\u27f6M\u2032) \u03b2-pred-zero = \u22a5-elim (Val-\u27f6 Zero M\u27f6M\u2032)\ndet (\u03be-pred M\u27f6M\u2032) (\u03b2-pred-suc VM) = \u22a5-elim (Val-\u27f6 (Suc VM) M\u27f6M\u2032)\ndet \u03b2-pred-zero (\u03be-pred M\u27f6M\u2032) = \u22a5-elim (Val-\u27f6 Zero M\u27f6M\u2032)\ndet \u03b2-pred-zero \u03b2-pred-zero = refl\ndet (\u03b2-pred-suc VM) (\u03be-pred M\u27f6M\u2032) = \u22a5-elim (Val-\u27f6 (Suc VM) M\u27f6M\u2032)\ndet (\u03b2-pred-suc _) (\u03b2-pred-suc _) = refl\ndet (\u03be-if0 L\u27f6L\u2032) (\u03be-if0 L\u27f6L\u2033) = cong\u2083 `if0_then_else_ (det L\u27f6L\u2032 L\u27f6L\u2033) refl refl\ndet (\u03be-if0 L\u27f6L\u2032) \u03b2-if0-zero = \u22a5-elim (Val-\u27f6 Zero L\u27f6L\u2032)\ndet (\u03be-if0 L\u27f6L\u2032) (\u03b2-if0-suc VL) = \u22a5-elim (Val-\u27f6 (Suc VL) L\u27f6L\u2032)\ndet \u03b2-if0-zero (\u03be-if0 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 Zero L\u27f6L\u2033)\ndet \u03b2-if0-zero \u03b2-if0-zero = refl\ndet (\u03b2-if0-suc VL) (\u03be-if0 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 (Suc VL) L\u27f6L\u2033)\ndet (\u03b2-if0-suc _) (\u03b2-if0-suc _) = refl\ndet (\u03be-Y M\u27f6M\u2032) (\u03be-Y M\u27f6M\u2033) = cong `Y_ (det M\u27f6M\u2032 M\u27f6M\u2033)\ndet (\u03be-Y M\u27f6M\u2032) (\u03b2-Y refl) = \u22a5-elim (Val-\u27f6 Fun M\u27f6M\u2032)\ndet (\u03b2-Y refl) (\u03be-Y M\u27f6M\u2033) = \u22a5-elim (Val-\u27f6 Fun M\u27f6M\u2033)\ndet (\u03b2-Y refl) (\u03b2-Y refl) = refl\n\\end{code}\n\nAlmost half the lines in the above proof are redundant, for example\n\n det (\u03be-\u00b7\u2081 L\u27f6L\u2032) (\u03be-\u00b7\u2082 VL _) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2032)\n det (\u03be-\u00b7\u2082 VL _) (\u03be-\u00b7\u2081 L\u27f6L\u2033) = \u22a5-elim (Val-\u27f6 VL L\u27f6L\u2033)\n \nare essentially identical. What we might like to do is delete the\nredundant lines and add\n\n det M\u27f6M\u2032 M\u27f6M\u2033 = sym (det M\u27f6M\u2033 M\u27f6M\u2032)\n\nto the bottom of the proof. But this does not work. The termination\nchecker complains, because the arguments have merely switched order\nand neither is smaller.\n\n## Canonical forms\n\n\\begin{code}\ndata Canonical : Term \u2192 Type \u2192 Set where\n\n Zero : \n -------------------\n Canonical `zero `\u2115\n\n Suc : \u2200 {V}\n \u2192 Canonical V `\u2115\n ----------------------\n \u2192 Canonical (`suc V) `\u2115\n \n Fun : \u2200 {x N A B}\n \u2192 \u03b5 , x `: A \u22a2 N `: B\n -------------------------------\n \u2192 Canonical (`\u03bb x `\u2192 N) (A `\u2192 B)\n\\end{code}\n\n## Canonical forms lemma\n\nEvery typed value is canonical.\n\n\\begin{code}\ncanonical : \u2200 {V A}\n \u2192 \u03b5 \u22a2 V `: A\n \u2192 Value V\n -------------\n \u2192 Canonical V A\ncanonical \u22a2zero Zero = Zero\ncanonical (\u22a2suc \u22a2V) (Suc VV) = Suc (canonical \u22a2V VV)\ncanonical (\u22a2\u03bb \u22a2N) Fun = Fun \u22a2N\n\\end{code}\n\nEvery canonical form has a type and a value.\n\n\\begin{code}\ntype : \u2200 {V A}\n \u2192 Canonical V A\n --------------\n \u2192 \u03b5 \u22a2 V `: A\ntype Zero = \u22a2zero\ntype (Suc CV) = \u22a2suc (type CV)\ntype (Fun {x = x} \u22a2N) = \u22a2\u03bb \u22a2N\n\nvalue : \u2200 {V A}\n \u2192 Canonical V A\n -------------\n \u2192 Value V\nvalue Zero = Zero\nvalue (Suc CV) = Suc (value CV)\nvalue (Fun \u22a2N) = Fun\n\\end{code}\n \n## Progress\n\n\\begin{code}\ndata Progress (M : Term) (A : Type) : Set where\n step : \u2200 {N}\n \u2192 M \u27f6 N\n ----------\n \u2192 Progress M A\n done :\n Canonical M A\n -------------\n \u2192 Progress M A\n\nprogress : \u2200 {M A} \u2192 \u03b5 \u22a2 M `: A \u2192 Progress M A\nprogress (Ax ())\nprogress (\u22a2\u03bb \u22a2N) = done (Fun \u22a2N)\nprogress (\u22a2L \u00b7 \u22a2M) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-\u00b7\u2081 L\u27f6L\u2032)\n... | done (Fun _) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-\u00b7\u2082 Fun M\u27f6M\u2032)\n... | done CM = step (\u03b2-\u2192 (value CM))\nprogress \u22a2zero = done Zero\nprogress (\u22a2suc \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-suc M\u27f6M\u2032)\n... | done CM = done (Suc CM)\nprogress (\u22a2pred \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-pred M\u27f6M\u2032)\n... | done Zero = step \u03b2-pred-zero\n... | done (Suc CM) = step (\u03b2-pred-suc (value CM))\nprogress (\u22a2if0 \u22a2L \u22a2M \u22a2N) with progress \u22a2L\n... | step L\u27f6L\u2032 = step (\u03be-if0 L\u27f6L\u2032)\n... | done Zero = step \u03b2-if0-zero\n... | done (Suc CM) = step (\u03b2-if0-suc (value CM))\nprogress (\u22a2Y \u22a2M) with progress \u22a2M\n... | step M\u27f6M\u2032 = step (\u03be-Y M\u27f6M\u2032)\n... | done (Fun _) = step (\u03b2-Y refl)\n\\end{code}\n\n\n## Preservation\n\n### Domain of an environment\n\n\\begin{code}\n{-\ndom : Ctx \u2192 List Id\ndom \u03b5 = []\ndom (\u0393 , x `: A) = x \u2237 dom \u0393\n\ndom-lemma : \u2200 {\u0393 y B} \u2192 \u0393 \u220b y `: B \u2192 y \u2208 dom \u0393\ndom-lemma Z = here\ndom-lemma (S x\u2262y \u22a2y) = there (dom-lemma \u22a2y)\n\nfree-lemma : \u2200 {\u0393 M A} \u2192 \u0393 \u22a2 M `: A \u2192 free M \u2286 dom \u0393\nfree-lemma (Ax \u22a2x) w\u2208 with w\u2208\n... | here = dom-lemma \u22a2x\n... | there () \nfree-lemma {\u0393} (\u22a2\u03bb {N = N} \u22a2N) = \u2237-to-\\\\ (free-lemma \u22a2N)\nfree-lemma (\u22a2L \u00b7 \u22a2M) w\u2208 with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208M = free-lemma \u22a2M \u2208M\nfree-lemma \u22a2zero ()\nfree-lemma (\u22a2suc \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2pred \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208\nfree-lemma (\u22a2if0 \u22a2L \u22a2M \u22a2N) w\u2208\n with ++-to-\u228e w\u2208\n... | inj\u2081 \u2208L = free-lemma \u22a2L \u2208L\n... | inj\u2082 \u2208MN with ++-to-\u228e \u2208MN\n... | inj\u2081 \u2208M = free-lemma \u22a2M \u2208M\n... | inj\u2082 \u2208N = free-lemma \u22a2N \u2208N\nfree-lemma (\u22a2Y \u22a2M) w\u2208 = free-lemma \u22a2M w\u2208 \n-}\n\\end{code}\n\n### Renaming\n\n\\begin{code}\n\u22a2rename : \u2200 {\u0393 \u0394}\n \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 \u0394 \u220b x `: A)\n --------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 \u0393 \u22a2 M `: A \u2192 \u0394 \u22a2 M `: A)\n\u22a2rename \u22a2\u03c3 (Ax \u22a2x) = Ax (\u22a2\u03c3 \u22a2x)\n\u22a2rename {\u0393} {\u0394} \u22a2\u03c3 (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb (\u22a2rename {\u0393\u2032} {\u0394\u2032} \u22a2\u03c3\u2032 \u22a2N)\n where\n \u0393\u2032 = \u0393 , x `: A\n \u0394\u2032 = \u0394 , x `: A\n\n \u22a2\u03c3\u2032 : \u2200 {w B} \u2192 \u0393\u2032 \u220b w `: B \u2192 \u0394\u2032 \u220b w `: B\n \u22a2\u03c3\u2032 Z = Z\n \u22a2\u03c3\u2032 (S w\u2262 \u22a2w) = S w\u2262 (\u22a2\u03c3 \u22a2w)\n\n\u22a2rename \u22a2\u03c3 (\u22a2L \u00b7 \u22a2M) = \u22a2rename \u22a2\u03c3 \u22a2L \u00b7 \u22a2rename \u22a2\u03c3 \u22a2M\n\u22a2rename \u22a2\u03c3 (\u22a2zero) = \u22a2zero\n\u22a2rename \u22a2\u03c3 (\u22a2suc \u22a2M) = \u22a2suc (\u22a2rename \u22a2\u03c3 \u22a2M)\n\u22a2rename \u22a2\u03c3 (\u22a2pred \u22a2M) = \u22a2pred (\u22a2rename \u22a2\u03c3 \u22a2M)\n\u22a2rename \u22a2\u03c3 (\u22a2if0 \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2rename \u22a2\u03c3 \u22a2L) (\u22a2rename \u22a2\u03c3 \u22a2M) (\u22a2rename \u22a2\u03c3 \u22a2N)\n\u22a2rename \u22a2\u03c3 (\u22a2Y \u22a2M) = \u22a2Y (\u22a2rename \u22a2\u03c3 \u22a2M)\n\\end{code}\n\n### Check that a context and term satisfy Barendregt\n\n\\begin{code}\ninfix 3 _\u2209_\ninfix 3 _||_\n\n_\u2209_ : Id \u2192 Ctx \u2192 Set\nx \u2209 \u0393 = \u2200 {A} \u2192 \u00ac (\u0393 \u220b x `: A)\n\ndata ok : \u2200 {\u0393 M A} \u2192 (\u0393 \u22a2 M `: A) \u2192 Set where\n\n ok-Ax : \u2200 {\u0393 A x} {\u22a2x : \u0393 \u220b x `: A}\n -----------\n \u2192 ok (Ax \u22a2x) \n\n ok-\u03bb : \u2200 {\u0393 x N A B} {\u22a2N : \u0393 , x `: A \u22a2 N `: B}\n \u2192 x \u2209 \u0393\n \u2192 ok \u22a2N\n -----------\n \u2192 ok (\u22a2\u03bb \u22a2N)\n\n ok-\u00b7 : \u2200 {\u0393 L M A B} {\u22a2L : \u0393 \u22a2 L `: A `\u2192 B} {\u22a2M : \u0393 \u22a2 M `: A}\n \u2192 ok \u22a2L\n \u2192 ok \u22a2M\n ------------\n \u2192 ok (\u22a2L \u00b7 \u22a2M)\n\n ok-zero : \u2200 {\u0393}\n ---------------\n \u2192 ok (\u22a2zero {\u0393})\n\n ok-suc : \u2200 {\u0393 M} {\u22a2M : \u0393 \u22a2 M `: `\u2115}\n \u2192 ok \u22a2M\n -------------\n \u2192 ok (\u22a2suc \u22a2M)\n \n ok-pred : \u2200 {\u0393 M} {\u22a2M : \u0393 \u22a2 M `: `\u2115}\n \u2192 ok \u22a2M\n --------------\n \u2192 ok (\u22a2pred \u22a2M)\n\n okif0 : \u2200 {\u0393 L M N A} {\u22a2L : \u0393 \u22a2 L `: `\u2115} {\u22a2M : \u0393 \u22a2 M `: A} {\u22a2N : \u0393 \u22a2 N `: A}\n \u2192 ok \u22a2L\n \u2192 ok \u22a2M\n \u2192 ok \u22a2N\n ------------------\n \u2192 ok (\u22a2if0 \u22a2L \u22a2M \u22a2N)\n\n ok-Y : \u2200 {\u0393 M A} {\u22a2M : \u0393 \u22a2 M `: A `\u2192 A}\n \u2192 ok \u22a2M\n -------------\n \u2192 ok (\u22a2Y \u22a2M)\n\nbar-lemma : \u2200 {\u0393 M A}\n \u2192 (\u22a2M : \u0393 \u22a2 M `: A)\n ----------------------\n \u2192 \u0393 \u22a2 barendregt M `: A\nbar-lemma = {!!}\n\nok-lemma : \u2200 {\u0393 M A}\n \u2192 (\u22a2M : \u0393 \u22a2 M `: A)\n ------------------\n \u2192 ok (bar-lemma \u22a2M)\nok-lemma = {!!} \n \ndata _||_ : Ctx \u2192 Term \u2192 Set where\n\n B-var : \u2200 {\u0393 x}\n \u2192 x \u2209 \u0393\n -------------------\n \u2192 \u0393 || ` x\n\n B-\u03bb : \u2200 {\u0393 x N}\n \u2192 \u2200 {A} \u2192 \u0393 , x `: A || N\n ------------------------\n \u2192 \u0393 || `\u03bb x `\u2192 N\n\n B-\u00b7 : \u2200 {\u0393 L M}\n \u2192 \u0393 || L\n \u2192 \u0393 || M\n ----------\n \u2192 \u0393 || L \u00b7 M\n\n B-zero : \u2200 {\u0393}\n -----------\n \u2192 \u0393 || `zero\n\n B-suc : \u2200 {\u0393 M}\n \u2192 \u0393 || M\n ------------\n \u2192 \u0393 || `suc M\n\n B-if0 : \u2200 {\u0393 L M N}\n \u2192 \u0393 || L\n \u2192 \u0393 || M\n \u2192 \u0393 || N\n --------------------------\n \u2192 \u0393 || `if0 L then M else N\n\n B-Y : \u2200 {\u0393 M}\n \u2192 \u0393 || M\n ----------\n \u2192 \u0393 || `Y M\n\\end{code}\n\n\n\n\n### Substitution preserves types\n\n\\begin{code}\n{-\n\u22a2subst : \u2200 {\u0393 \u0394 \u03c1}\n \u2192 (\u2200 {x A} \u2192 \u0394 \u220b x `: A \u2192 \u0393 \u220b x `: A)\n \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 \u0394 \u22a2 \u03c1 x `: A)\n --------------------------------------------------------------\n \u2192 (\u2200 {M A} \u2192 (\u22a2M : \u0393 \u22a2 M `: A) \u2192 ok \u22a2M \u2192 \u0394 \u22a2 subst \u03c1 M `: A)\n-}\n\u22a2subst : \u2200 {\u0393 \u0394 \u03c1}\n \u2192 (\u2200 {x A} \u2192 \u0393 \u220b x `: A \u2192 \u0394 \u22a2 \u03c1 x `: A)\n ---------------------------------------------\n \u2192 (\u2200 {M A} \u2192 \u0393 \u22a2 M `: A \u2192 \u0394 \u22a2 subst \u03c1 M `: A)\n\u22a2subst \u22a2\u03c1 (Ax \u22a2x) = \u22a2\u03c1 \u22a2x\n\u22a2subst {\u0393} {\u0394} {\u03c1} \u22a2\u03c1 (\u22a2\u03bb {x = x} {N = N} {A = A} \u22a2N)\n = \u22a2\u03bb {x = x} {A = A} (\u22a2subst {\u0393\u2032} {\u0394\u2032} {\u03c1\u2032} \u22a2\u03c1\u2032 \u22a2N)\n where\n \u0393\u2032 = \u0393 , x `: A\n \u0394\u2032 = \u0394 , x `: A\n \u03c1\u2032 = \u03c1 , x \u21a6 ` x\n\n \u22a2\u03c3 : \u2200 {w C} \u2192 \u0394 \u220b w `: C \u2192 \u0394\u2032 \u220b w `: C\n \u22a2\u03c3 \u22a2w = S {!!} \u22a2w\n -- this follows because w in \u0394 implies w in \u0393,\n -- and w in \u0393 implies w \u2262 x.\n\n \u22a2\u03c1\u2032 : \u2200 {w C} \u2192 \u0393\u2032 \u220b w `: C \u2192 \u0394\u2032 \u22a2 \u03c1\u2032 w `: C\n \u22a2\u03c1\u2032 {w} Z with w \u225f x\n ... | yes _ = Ax Z\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1\u2032 {w} (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = \u22a2rename {\u0394} {\u0394\u2032} \u22a2\u03c3 (\u22a2\u03c1 \u22a2w)\n\n\u22a2subst \u22a2\u03c1 (\u22a2L \u00b7 \u22a2M) = \u22a2subst \u22a2\u03c1 \u22a2L \u00b7 \u22a2subst \u22a2\u03c1 \u22a2M\n\u22a2subst \u22a2\u03c1 \u22a2zero = \u22a2zero\n\u22a2subst \u22a2\u03c1 (\u22a2suc \u22a2M) = \u22a2suc (\u22a2subst \u22a2\u03c1 \u22a2M)\n\u22a2subst \u22a2\u03c1 (\u22a2pred \u22a2M) = \u22a2pred (\u22a2subst \u22a2\u03c1 \u22a2M)\n\u22a2subst \u22a2\u03c1 (\u22a2if0 \u22a2L \u22a2M \u22a2N)\n = \u22a2if0 (\u22a2subst \u22a2\u03c1 \u22a2L) (\u22a2subst \u22a2\u03c1 \u22a2M) (\u22a2subst \u22a2\u03c1 \u22a2N)\n\u22a2subst \u22a2\u03c1 (\u22a2Y \u22a2M) = \u22a2Y (\u22a2subst \u22a2\u03c1 \u22a2M)\n\\end{code}\n\nLet's look at examples. Assume `M` is closed. Example 1.\n\n subst (tm-\u2205 , \"x\" \u21a6 M) (`\u03bb \"y\" `\u2192 ` \"x\") \u2261 `\u03bb \"y\" `\u2192 M\n\nExample 2.\n\n subst (tm-\u2205 , \"y\" \u21a6 ` \"y\" , \"x\" \u21a6 M) (`\u03bb \"y\" `\u2192 ` \"x\" \u00b7 ` \"y\")\n \u2261\n `\u03bb \"y\" `\u2192 subst (tm-\u2205 , \"y\" \u21a6 ` \"y\" , \"x\" \u21a6 M , \"y\" \u21a6 ` \"y\") (` \"x\" \u00b7 ` \"y\")\n \u2261\n `\u03bb \"y\" `\u2192 (M \u00b7 ` \"y\")\n\nThe hypotheses of the theorem appear to be violated. Drat!\n\n\\begin{code}\n\u22a2substitution : \u2200 {\u0393 x A N B M}\n \u2192 \u0393 , x `: A \u22a2 N `: B\n \u2192 \u0393 \u22a2 M `: A\n ----------------------\n \u2192 \u0393 \u22a2 N [ x := M ] `: B\n\u22a2substitution {\u0393} {x} {A} {N} {B} {M} \u22a2N \u22a2M =\n \u22a2subst {\u0393\u2032} {\u0393} {\u03c1} \u22a2\u03c1 {N} {B} \u22a2N\n where\n \u0393\u2032 = \u0393 , x `: A\n \u03c1 = tm-\u2205 , x \u21a6 M\n \u22a2\u03c1 : \u2200 {w B} \u2192 \u0393\u2032 \u220b w `: B \u2192 \u0393 \u22a2 \u03c1 w `: B\n \u22a2\u03c1 {w} Z with w \u225f x\n ... | yes _ = \u22a2M\n ... | no w\u2262 = \u22a5-elim (w\u2262 refl)\n \u22a2\u03c1 {w} (S w\u2262 \u22a2w) with w \u225f x\n ... | yes refl = \u22a5-elim (w\u2262 refl)\n ... | no _ = Ax \u22a2w\n\\end{code}\n\n### Preservation\n\n\\begin{code}\npreservation : \u2200 {\u0393 M N A}\n \u2192 \u0393 \u22a2 M `: A\n \u2192 M \u27f6 N\n ---------\n \u2192 \u0393 \u22a2 N `: A\npreservation (Ax \u22a2x) ()\npreservation (\u22a2\u03bb \u22a2N) ()\npreservation (\u22a2L \u00b7 \u22a2M) (\u03be-\u00b7\u2081 L\u27f6) = preservation \u22a2L L\u27f6 \u00b7 \u22a2M\npreservation (\u22a2V \u00b7 \u22a2M) (\u03be-\u00b7\u2082 _ M\u27f6) = \u22a2V \u00b7 preservation \u22a2M M\u27f6\npreservation ((\u22a2\u03bb \u22a2N) \u00b7 \u22a2W) (\u03b2-\u2192 _) = \u22a2substitution \u22a2N \u22a2W\npreservation (\u22a2zero) ()\npreservation (\u22a2suc \u22a2M) (\u03be-suc M\u27f6) = \u22a2suc (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2M) (\u03be-pred M\u27f6) = \u22a2pred (preservation \u22a2M M\u27f6)\npreservation (\u22a2pred \u22a2zero) (\u03b2-pred-zero) = \u22a2zero\npreservation (\u22a2pred (\u22a2suc \u22a2M)) (\u03b2-pred-suc _) = \u22a2M\npreservation (\u22a2if0 \u22a2L \u22a2M \u22a2N) (\u03be-if0 L\u27f6) = \u22a2if0 (preservation \u22a2L L\u27f6) \u22a2M \u22a2N\npreservation (\u22a2if0 \u22a2zero \u22a2M \u22a2N) \u03b2-if0-zero = \u22a2M\npreservation (\u22a2if0 (\u22a2suc \u22a2V) \u22a2M \u22a2N) (\u03b2-if0-suc _) = \u22a2N\npreservation (\u22a2Y \u22a2M) (\u03be-Y M\u27f6) = \u22a2Y (preservation \u22a2M M\u27f6)\npreservation (\u22a2Y (\u22a2\u03bb \u22a2N)) (\u03b2-Y refl) = \u22a2substitution \u22a2N (\u22a2Y (\u22a2\u03bb \u22a2N))\n\\end{code}\n\n## Normalise\n\n\\begin{code}\ndata Normalise {M A} (\u22a2M : \u03b5 \u22a2 M `: A) : Set where\n out-of-gas : \u2200 {N} \u2192 M \u27f6* N \u2192 \u03b5 \u22a2 N `: A \u2192 Normalise \u22a2M\n normal : \u2200 {V} \u2192 \u2115 \u2192 Canonical V A \u2192 M \u27f6* V \u2192 Normalise \u22a2M\n\nnormalise : \u2200 {L A} \u2192 \u2115 \u2192 (\u22a2L : \u03b5 \u22a2 L `: A) \u2192 Normalise \u22a2L\nnormalise {L} zero \u22a2L = out-of-gas (L \u220e) \u22a2L\nnormalise {L} (suc m) \u22a2L with progress \u22a2L\n... | done CL = normal (suc m) CL (L \u220e)\n... | step L\u27f6M with preservation \u22a2L L\u27f6M\n... | \u22a2M with normalise m \u22a2M\n... | out-of-gas M\u27f6*N \u22a2N = out-of-gas (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*N) \u22a2N\n... | normal n CV M\u27f6*V = normal n CV (L \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*V)\n\\end{code}\n\n","avg_line_length":25.6884920635,"max_line_length":91,"alphanum_fraction":0.4127597127} +{"size":12620,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\n\nmodule Algorithmic.Erasure.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Function\n\nopen import Utils\nopen import Type\nopen import Type.BetaNormal\nopen import Algorithmic as A\nimport Algorithmic.Reduction as A\nimport Algorithmic.RenamingSubstitution as A\nopen import Algorithmic.Erasure\nopen import Algorithmic.Erasure.RenamingSubstitution\nimport Untyped.Reduction as U\nimport Untyped.RenamingSubstitution as U\nopen import Builtin\nopen import Builtin.Constant.Type\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con\nopen import Untyped\nopen import Type.BetaNBE.RenamingSubstitution\nopen import Data.Sum\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Data.List using (List;[];_\u2237_)\nopen import Data.Vec hiding (map; [_])\nopen import Data.Product hiding (map) renaming (_,_ to _,,_)\nopen import Data.Unit hiding (_\u2264_; _\u2264?_; _\u225f_)\nimport Utils as Util\nopen import Relation.Nullary\nopen import Data.Nat hiding (_<_; _\u2264?_; _^_; _+_; _\u225f_; _*_)\nopen import Data.Integer hiding (suc)\nopen import Data.Fin using (suc)\nimport Data.Bool as B\n\\end{code}\n\n\\begin{code}\neraseCtx : \u2200{\u03a6}(\u0393 : Ctx \u03a6) \u2192 Bwd U.Label\neraseCtx \u2205 = []\neraseCtx (\u0393 ,\u22c6 J) = eraseCtx \u0393 :< U.Type\neraseCtx (\u0393 , A) = eraseCtx \u0393 :< U.Term\n\nerase\u2264C' : \u2200{\u03a6 \u03a6'}{\u0393 : Ctx \u03a6}{\u0393' : Ctx \u03a6'} \u2192 \u0393 A.\u2264C' \u0393' \u2192 eraseCtx \u0393 U.\u2264L eraseCtx \u0393'\nerase\u2264C' A.base = U.base\nerase\u2264C' (A.skip\u22c6 p) = U.skipType (erase\u2264C' p)\nerase\u2264C' (A.skip p) = U.skipTerm (erase\u2264C' p)\n\n-- there could be a simpler version of this without p and q...\nerase-arity-lem : \u2200 b {\u03a6}{\u0393}(p : proj\u2081 (ISIG b) \u2261 \u03a6)(q : subst Ctx p (proj\u2081 (proj\u2082 (ISIG b))) \u2261 \u0393) \u2192 eraseCtx \u0393 \u2261 U.arity b\nerase-arity-lem addInteger refl refl = refl\nerase-arity-lem subtractInteger refl refl = refl\nerase-arity-lem multiplyInteger refl refl = refl\nerase-arity-lem divideInteger refl refl = refl\nerase-arity-lem quotientInteger refl refl = refl\nerase-arity-lem remainderInteger refl refl = refl\nerase-arity-lem modInteger refl refl = refl\nerase-arity-lem lessThanInteger refl refl = refl\nerase-arity-lem lessThanEqualsInteger refl refl = refl\nerase-arity-lem equalsInteger refl refl = refl\nerase-arity-lem appendByteString refl refl = refl\nerase-arity-lem lessThanByteString refl refl = refl\nerase-arity-lem lessThanEqualsByteString refl refl = refl\nerase-arity-lem sha2-256 refl refl = refl\nerase-arity-lem sha3-256 refl refl = refl\nerase-arity-lem verifySignature refl refl = refl\nerase-arity-lem equalsByteString refl refl = refl\nerase-arity-lem ifThenElse refl refl = refl\nerase-arity-lem appendString refl refl = refl\nerase-arity-lem trace refl refl = refl\nerase-arity-lem equalsString refl refl = refl\nerase-arity-lem encodeUtf8 refl refl = refl\nerase-arity-lem decodeUtf8 refl refl = refl\nerase-arity-lem fstPair refl refl = refl\nerase-arity-lem sndPair refl refl = refl\nerase-arity-lem nullList refl refl = refl\nerase-arity-lem headList refl refl = refl\nerase-arity-lem tailList refl refl = refl\nerase-arity-lem chooseList refl refl = refl\nerase-arity-lem constrData refl refl = refl\nerase-arity-lem mapData refl refl = refl\nerase-arity-lem listData refl refl = refl\nerase-arity-lem iData refl refl = refl\nerase-arity-lem bData refl refl = refl\nerase-arity-lem unConstrData refl refl = refl\nerase-arity-lem unMapData refl refl = refl\nerase-arity-lem unListData refl refl = refl\nerase-arity-lem unIData refl refl = refl\nerase-arity-lem unBData refl refl = refl\nerase-arity-lem equalsData refl refl = refl\nerase-arity-lem chooseData refl refl = refl\nerase-arity-lem chooseUnit refl refl = refl\nerase-arity-lem mkPairData refl refl = refl\nerase-arity-lem mkNilData refl refl = refl\nerase-arity-lem mkNilPairData refl refl = refl\nerase-arity-lem mkCons refl refl = refl\nerase-arity-lem consByteString refl refl = refl\nerase-arity-lem sliceByteString refl refl = refl\nerase-arity-lem lengthOfByteString refl refl = refl\nerase-arity-lem indexByteString refl refl = refl\nerase-arity-lem blake2b-256 refl refl = refl\n\neraseITel : \u2200 b {\u03a6}(\u0394 : Ctx \u03a6)(\u03c3 : SubNf \u03a6 \u2205)\n \u2192 A.ITel b \u0394 \u03c3 \u2192 U.ITel b (eraseCtx \u0394)\n\neraseFVal : {A B : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 A.\u22a2 A \u21d2 B}\n \u2192 A.Value t \u2192 U.FValue (erase t)\neraseFVal (A.V-\u019b t) = U.V-\u019b (erase t)\neraseFVal (A.V-I\u21d2 b p q r \u03c3 p' vs t) =\n U.V-builtin b (erase-arity-lem b p q) (erase\u2264C' p') (eraseITel b _ \u03c3 vs) (erase t)\n\neraseVal : {A : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 A.\u22a2 A}\n \u2192 A.Value t \u2192 U.Value (erase t)\neraseVal v@(A.V-\u019b t) = U.V-F (eraseFVal v)\neraseVal (A.V-\u039b t) = U.V-delay\neraseVal (A.V-wrap v) = eraseVal v\neraseVal (A.V-con cn) = U.V-con (eraseTC {\u2205}{\u2205} cn)\neraseVal (A.V-I\u03a0 b p q r \u03c3 p' vs t) = U.V-builtin\u22c6 b (erase-arity-lem b p q) (erase\u2264C' p') (eraseITel b _ \u03c3 vs) (erase t)\neraseVal v@(A.V-I\u21d2 b p q r \u03c3 p\u2081 x t) = U.V-F (eraseFVal v)\n\neraseITel b \u2205 \u03c3 vs = tt\neraseITel b (\u0394 ,\u22c6 J) \u03c3 (vs ,, A) = eraseITel b \u0394 (\u03c3 \u2218 S) vs\neraseITel b (\u0394 , A) \u03c3 (vs ,, t ,, v) =\n eraseITel b \u0394 \u03c3 vs ,, erase t ,, eraseVal v\n\n\neraseErr : \u2200{\u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}{\u0393 : A.Ctx \u03a6}{e : \u0393 A.\u22a2 A}\n \u2192 A.Error e \u2192 U.Error (erase e)\neraseErr A.E-error = U.E-error\n\\end{code}\n\n\\begin{code}\nerase-decIf : \u2200{\u03a6}{\u0393 : A.Ctx \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}{X}(p : Dec X)(t f : \u0393 A.\u22a2 A) \u2192\n Util.decIf p (erase t) (erase f) \u2261 erase (Util.decIf p t f)\nerase-decIf (yes p) t f = refl\nerase-decIf (no \u00acp) t f = refl\n\\end{code}\n\n\\begin{code}\n-- We want that when a builtin eventually computes the untyped and\n-- typed semantics would give the same answer. Here we just\n-- exhaustively pattern match on the builtin and its typed args to get\n-- it to compute.\nerase-BUILTIN : \u2200 b (\u03c3 : SubNf (proj\u2081 (ISIG b)) \u2205)(vs : A.ITel b (proj\u2081 (proj\u2082 (ISIG b))) \u03c3) \u2192\n proj\u2081\n (U.IBUILTIN' b (erase-arity-lem b refl refl) (eraseITel b (proj\u2081 (proj\u2082 (ISIG b))) \u03c3 vs))\n \u2261 erase (proj\u2081 (A.IBUILTIN b \u03c3 vs))\nerase-BUILTIN addInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN subtractInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN multiplyInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN divideInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN quotientInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN remainderInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN modInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN lessThanInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i Data.Integer. (r -> r) -> r\nN : \u2200{\u03a6} \u2192 \u03a6 \u22a2\u22c6 *\nN = \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 ` Z)\n\n-- \/\\(r :: *) -> \\(z : r) (f : r -> r) -> z\nZero : \u2205 \u22a2 N\nZero = \u039b (\u019b (\u019b (` (S Z))))\n\n-- \\(n : nat) -> \/\\(r :: *) -> \\(z : r) (f : r -> r) -> f (n {r} z f)\nSucc : \u2205 \u22a2 N \u21d2 N\nSucc = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (` Z) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n\nIter : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 N \u21d2 (` Z))\nIter = \u039b (\u019b (\u019b (\u019b ((` Z) \u00b7\u22c6 (` Z) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\nopen import Builtin.Constant.Type\nopen import Data.Integer\nopen import Data.Nat\nopen import Agda.Builtin.Sigma renaming (_,_ to _,,_)\n\ncon0 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon0 = con (integer (\u2124.pos 0))\n\ncon1 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon1 = con (integer (\u2124.pos 1))\n\ninc : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer \u21d2 con integer\ninc = \u019b (ibuiltin addInteger \u00b7 con1 \u00b7 ` Z)\n\nNat2Int : \u2205 \u22a2 N \u21d2 con integer\nNat2Int = \u019b (Iter\n \u00b7\u22c6 con integer\n \u00b7 con0\n \u00b7 inc\n \u00b7 ` Z)\n\\end{code}\n","avg_line_length":23.6470588235,"max_line_length":77,"alphanum_fraction":0.5008291874} +{"size":4149,"ext":"lagda","lang":"Literate Agda","max_stars_count":12.0,"content":"\\begin{code}\nmodule Type.BetaNBE where\n\\end{code}\n\n## Imports\n\n\\begin{code}\nopen import Type\nopen import Type.BetaNormal\nopen import Type.RenamingSubstitution\nopen import Type.Equality\n\nopen import Function\nopen import Data.Sum\nopen import Data.Empty\nopen import Data.Product\n\nopen import Relation.Binary.PropositionalEquality hiding ([_]; subst)\n\\end{code}\n\nValues are defined by induction on kind. At kind # and * they are\ninert and defined to be just normal forms. At function kind they are\neither neutral or Kripke functions\n\n\\begin{code}\nVal : Ctx\u22c6 \u2192 Kind \u2192 Set\nVal \u03a6 # = \u03a6 \u22a2Nf\u22c6 #\nVal \u03a6 * = \u03a6 \u22a2Nf\u22c6 *\nVal \u03a6 (\u03c3 \u21d2 \u03c4) = \u03a6 \u22a2NeN\u22c6 (\u03c3 \u21d2 \u03c4) \u228e \u2200 {\u03a8} \u2192 Ren \u03a6 \u03a8 \u2192 Val \u03a8 \u03c3 \u2192 Val \u03a8 \u03c4\n\\end{code}\n\nWe can embed neutral terms into values at any kind using reflect.\nreflect is quite simple in this version of NBE and not mutually\ndefined with reify.\n\n\\begin{code}\nreflect : \u2200{\u03a6 \u03c3} \u2192 \u03a6 \u22a2NeN\u22c6 \u03c3 \u2192 Val \u03a6 \u03c3\nreflect {\u03c3 = #} n = ne n\nreflect {\u03c3 = *} n = ne n\nreflect {\u03c3 = \u03c3 \u21d2 \u03c4} n = inj\u2081 n\n\\end{code}\n\nA shorthand for creating a new fresh variable as a value which we need\nin reify\n\n\\begin{code}\nfresh : \u2200 {\u03a6 \u03c3} \u2192 Val (\u03a6 ,\u22c6 \u03c3) \u03c3\nfresh = reflect (` Z)\n\\end{code}\n\nRenaming for values\n\n\\begin{code}\nrenameVal : \u2200 {\u03c3 \u03a6 \u03a8} \u2192 Ren \u03a6 \u03a8 \u2192 Val \u03a6 \u03c3 \u2192 Val \u03a8 \u03c3\nrenameVal {#} \u03c8 n = renameNf \u03c8 n\nrenameVal {*} \u03c8 n = renameNf \u03c8 n\nrenameVal {\u03c3 \u21d2 \u03c4} \u03c8 (inj\u2081 n) = inj\u2081 (renameNeN \u03c8 n)\nrenameVal {\u03c3 \u21d2 \u03c4} \u03c8 (inj\u2082 f) = inj\u2082 (\u03bb \u03c1' \u2192 f (\u03c1' \u2218 \u03c8))\n\\end{code}\n\nWeakening for values\n\n\\begin{code}\nweakenVal : \u2200 {\u03c3 \u03a6 K} \u2192 Val \u03a6 \u03c3 \u2192 Val (\u03a6 ,\u22c6 K) \u03c3\nweakenVal = renameVal S\n\\end{code}\n\nReify takes a value and yields a normal form.\n\n\\begin{code}\nreify : \u2200 {\u03c3 \u03a6} \u2192 Val \u03a6 \u03c3 \u2192 \u03a6 \u22a2Nf\u22c6 \u03c3\nreify {#} n = n\nreify {*} n = n\nreify {\u03c3 \u21d2 \u03c4} (inj\u2081 n) = ne n\nreify {\u03c3 \u21d2 \u03c4} (inj\u2082 f) = \u019b (reify (f S fresh))\n\\end{code}\n\nAn environment is a mapping from variables to values\n\n\\begin{code}\nEnv : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nEnv \u03a8 \u03a6 = \u2200{J} \u2192 \u03a8 \u220b\u22c6 J \u2192 Val \u03a6 J\n\\end{code}\n\n'cons' for environments\n\n\\begin{code}\n_,,\u22c6_ : \u2200{\u03a8 \u03a6} \u2192 (\u03c3 : Env \u03a6 \u03a8) \u2192 \u2200{K}(A : Val \u03a8 K) \u2192 Env (\u03a6 ,\u22c6 K) \u03a8\n(\u03c3 ,,\u22c6 A) Z = A\n(\u03c3 ,,\u22c6 A) (S \u03b1) = \u03c3 \u03b1\n\\end{code}\n\n\\begin{code}\nexte : \u2200 {\u03a6 \u03a8} \u2192 Env \u03a6 \u03a8 \u2192 (\u2200 {K} \u2192 Env (\u03a6 ,\u22c6 K) (\u03a8 ,\u22c6 K))\nexte \u03b7 = (weakenVal \u2218 \u03b7) ,,\u22c6 fresh\n{-\n-- this version would be more analogous to ext and exts but would\n-- require changing some proofs for terms\nexte \u03b7 Z = fresh\nexte \u03b7 (S \u03b1) = weakenVal (\u03b7 \u03b1)\n-}\n\\end{code}\n\n\nApplication for values. As values at function type can be semantic\nfunctions or neutral terms we need this function to unpack them. If\nthe function is neutral we create a neutral application by reifying\nthe value and applying the neutral application constructor, then we\nrefect the neutral application into a value. If the function is a\nsemantic function we apply it to the identity renaming and then to\nthe argument. In this case, the function and argument are in the same\ncontext so we do not need the Kripke extension, hence the identity\nrenaming.\n\n\\begin{code}\n_\u00b7V_ : \u2200{\u03a6 K J} \u2192 Val \u03a6 (K \u21d2 J) \u2192 Val \u03a6 K \u2192 Val \u03a6 J\ninj\u2081 n \u00b7V v = reflect (n \u00b7 reify v)\ninj\u2082 f \u00b7V v = f id v\n\\end{code}\n\nEvaluation a term in an environment yields a value. The most\ninteresting cases are \u019b where we introduce a new Kripke function that\nwill evaluate when it receives an argument and \u03a0\/\u03bc where we need to go\nunder the binder and extend the environement before evaluating and\nreifying.\n\n\\begin{code}\neval : \u2200{\u03a6 \u03a8 K} \u2192 \u03a8 \u22a2\u22c6 K \u2192 Env \u03a8 \u03a6 \u2192 Val \u03a6 K\neval (` \u03b1) \u03b7 = \u03b7 \u03b1\neval (\u03a0 B) \u03b7 = \u03a0 (reify (eval B (exte \u03b7)))\neval (A \u21d2 B) \u03b7 = reify (eval A \u03b7) \u21d2 reify (eval B \u03b7)\neval (\u019b B) \u03b7 = inj\u2082 \u03bb \u03c1 v \u2192 eval B ((renameVal \u03c1 \u2218 \u03b7) ,,\u22c6 v)\neval (A \u00b7 B) \u03b7 = eval A \u03b7 \u00b7V eval B \u03b7\neval \u03bc1 \u03b7 = inj\u2081 \u03bc1\neval (size\u22c6 n) \u03b7 = size\u22c6 n\neval (con tcn s) \u03b7 = con tcn (reify (eval s \u03b7))\n\\end{code}\n\nIdentity environment\n\n\\begin{code}\nidEnv : \u2200 \u03a6 \u2192 Env \u03a6 \u03a6\nidEnv \u03a6 = reflect \u2218 `\n\\end{code}\n\nNormalisating a term yields a normal form. We evaluate in the identity\nenvironment to yield a value in the same context as the original term\nand then reify to yield a normal form\n\n\\begin{code}\nnf : \u2200{\u03a6 K} \u2192 \u03a6 \u22a2\u22c6 K \u2192 \u03a6 \u22a2Nf\u22c6 K\nnf t = reify (eval t (idEnv _))\n\\end{code}\n","avg_line_length":26.9415584416,"max_line_length":70,"alphanum_fraction":0.644010605} +{"size":11107,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Scoped: Scoped and Typed DeBruijn representation\"\npermalink : \/Scoped\n---\n\n## Imports\n\n\\begin{code}\nmodule Scoped where\n\\end{code}\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\n-- open Eq.\u2261-Reasoning\nopen import Data.Nat using (\u2115; zero; suc; _+_; _\u2238_)\nopen import Data.Product using (_\u00d7_; proj\u2081; proj\u2082; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Data.Sum using (_\u228e_; inj\u2081; inj\u2082)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Relation.Nullary.Decidable using (map)\nopen import Relation.Nullary.Negation using (contraposition)\nopen import Relation.Nullary.Product using (_\u00d7-dec_)\nopen import Data.Unit using (\u22a4; tt)\nopen import Data.Empty using (\u22a5; \u22a5-elim)\nopen import Function using (_\u2218_)\nopen import Function.Equivalence using (_\u21d4_; equivalence)\n\\end{code}\n\n## Syntax\n\n\\begin{code}\ninfixr 5 _\u21d2_\n\ndata Type : Set where\n o : Type\n _\u21d2_ : Type \u2192 Type \u2192 Type\n\ndata Env : Set where\n \u03b5 : Env\n _,_ : Env \u2192 Type \u2192 Env\n\ndata Var : Env \u2192 Type \u2192 Set where\n Z : \u2200 {\u0393 : Env} {A : Type} \u2192 Var (\u0393 , A) A\n S : \u2200 {\u0393 : Env} {A B : Type} \u2192 Var \u0393 B \u2192 Var (\u0393 , A) B\n\ndata Exp : Env \u2192 Type \u2192 Set where\n var : \u2200 {\u0393 : Env} {A : Type} \u2192 Var \u0393 A \u2192 Exp \u0393 A\n abs : \u2200 {\u0393 : Env} {A B : Type} \u2192 Exp (\u0393 , A) B \u2192 Exp \u0393 (A \u21d2 B)\n app : \u2200 {\u0393 : Env} {A B : Type} \u2192 Exp \u0393 (A \u21d2 B) \u2192 Exp \u0393 A \u2192 Exp \u0393 B\n\\end{code}\n\n\n## Untyped DeBruijn\n\n\\begin{code}\ndata DB : Set where\n var : \u2115 \u2192 DB\n abs : DB \u2192 DB\n app : DB \u2192 DB \u2192 DB\n\\end{code}\n\n\n# PH representation\n\n\\begin{code}\ndata PH (X : Type \u2192 Set) : Type \u2192 Set where\n var : \u2200 {A : Type} \u2192 X A \u2192 PH X A\n abs : \u2200 {A B : Type} \u2192 (X A \u2192 PH X B) \u2192 PH X (A \u21d2 B)\n app : \u2200 {A B : Type} \u2192 PH X (A \u21d2 B) \u2192 PH X A \u2192 PH X B\n\\end{code}\n\n\n# Convert PHOAS to DB\n\n\\begin{code}\nPH\u2192DB : \u2200 {A} \u2192 (\u2200 {X} \u2192 PH X A) \u2192 DB\nPH\u2192DB M = h M 0\n where\n K : Type \u2192 Set\n K A = \u2115\n\n h : \u2200 {A} \u2192 PH K A \u2192 \u2115 \u2192 DB\n h (var k) j = var (j \u2238 k)\n h (abs N) j = abs (h (N (j + 1)) (j + 1))\n h (app L M) j = app (h L j) (h M j)\n\\end{code}\n\n\n# Test examples\n\n\\begin{code}\nChurch : Type\nChurch = (o \u21d2 o) \u21d2 o \u21d2 o\n\ntwoExp : Exp \u03b5 Church\ntwoExp = (abs (abs (app (var (S Z)) (app (var (S Z)) (var Z)))))\n\ntwoPH : \u2200 {X} \u2192 PH X Church\ntwoPH = (abs (\u03bb f \u2192 (abs (\u03bb x \u2192 (app (var f) (app (var f) (var x)))))))\n\ntwoDB : DB\ntwoDB = (abs (abs (app (var 1) (app (var 1) (var 0)))))\n\nex : PH\u2192DB twoPH \u2261 twoDB\nex = refl\n\\end{code}\n\n## Decide whether environments and types are equal\n\n\\begin{code}\n_\u225fT_ : \u2200 (A B : Type) \u2192 Dec (A \u2261 B)\no \u225fT o = yes refl\no \u225fT (A\u2032 \u21d2 B\u2032) = no (\u03bb())\n(A \u21d2 B) \u225fT o = no (\u03bb())\n(A \u21d2 B) \u225fT (A\u2032 \u21d2 B\u2032) = map (equivalence obv1 obv2) ((A \u225fT A\u2032) \u00d7-dec (B \u225fT B\u2032))\n where\n obv1 : \u2200 {A B A\u2032 B\u2032 : Type} \u2192 (A \u2261 A\u2032) \u00d7 (B \u2261 B\u2032) \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032\n obv1 \u27e8 refl , refl \u27e9 = refl\n obv2 : \u2200 {A B A\u2032 B\u2032 : Type} \u2192 A \u21d2 B \u2261 A\u2032 \u21d2 B\u2032 \u2192 (A \u2261 A\u2032) \u00d7 (B \u2261 B\u2032)\n obv2 refl = \u27e8 refl , refl \u27e9\n\n_\u225f_ : \u2200 (\u0393 \u0394 : Env) \u2192 Dec (\u0393 \u2261 \u0394)\n\u03b5 \u225f \u03b5 = yes refl\n\u03b5 \u225f (\u0393 , A) = no (\u03bb())\n(\u0393 , A) \u225f \u03b5 = no (\u03bb())\n(\u0393 , A) \u225f (\u0394 , B) = map (equivalence obv1 obv2) ((\u0393 \u225f \u0394) \u00d7-dec (A \u225fT B))\n where\n obv1 : \u2200 {\u0393 \u0394 A B} \u2192 (\u0393 \u2261 \u0394) \u00d7 (A \u2261 B) \u2192 (\u0393 , A) \u2261 (\u0394 , B)\n obv1 \u27e8 refl , refl \u27e9 = refl\n obv2 : \u2200 {\u0393 \u0394 A B} \u2192 (\u0393 , A) \u2261 (\u0394 , B) \u2192 (\u0393 \u2261 \u0394) \u00d7 (A \u2261 B)\n obv2 refl = \u27e8 refl , refl \u27e9\n\\end{code}\n\n\n## Convert Phoas to Exp\n\n\\begin{code}\npostulate\n impossible : \u2200 {A : Set} \u2192 A\n\ncompare : \u2200 (A : Type) (\u0393 \u0394 : Env) \u2192 Var \u0394 A -- Extends (\u0393 , A) \u0394\ncompare A \u0393 \u0394 with (\u0393 , A) \u225f \u0394\ncompare A \u0393 \u0394 | yes refl = Z\ncompare A \u0393 (\u0394 , B) | no \u0393A\u2260\u0394B = S (compare A \u0393 \u0394)\ncompare A \u0393 \u03b5 | no \u0393A\u2260\u0394B = impossible\n\nPH\u2192Exp : \u2200 {A : Type} \u2192 (\u2200 {X} \u2192 PH X A) \u2192 Exp \u03b5 A\nPH\u2192Exp M = h M \u03b5\n where\n K : Type \u2192 Set\n K A = Env\n\n h : \u2200 {A} \u2192 PH K A \u2192 (\u0394 : Env) \u2192 Exp \u0394 A\n h {A} (var \u0393) \u0394 = var (compare A \u0393 \u0394)\n h {A \u21d2 B} (abs N) \u0394 = abs (h (N \u0394) (\u0394 , A))\n h (app L M) \u0394 = app (h L \u0394) (h M \u0394)\n\nexPH : PH\u2192Exp twoPH \u2261 twoExp\nexPH = refl\n\\end{code}\n\n## When one environment extends another\n\nWe could get rid of the use of `impossible` above if we could prove\nthat `Extends (\u0393 , A) \u0394` in the `(var \u0393)` case of the definition of `h`.\n\n\\begin{code}\ndata Extends : (\u0393 : Env) \u2192 (\u0394 : Env) \u2192 Set where\n Z : \u2200 {\u0393 : Env} \u2192 Extends \u0393 \u0393\n S : \u2200 {A : Type} {\u0393 \u0394 : Env} \u2192 Extends \u0393 \u0394 \u2192 Extends \u0393 (\u0394 , A)\n\nextract : \u2200 {A : Type} {\u0393 \u0394 : Env} \u2192 Extends (\u0393 , A) \u0394 \u2192 Var \u0394 A\nextract Z = Z\nextract (S k) = S (extract k)\n\\end{code}\n\n# Test code for semantics\n\n\\begin{code}\nplus : Exp \u03b5 (Church \u21d2 Church \u21d2 Church)\nplus = PH\u2192Exp (abs \u03bb{m \u2192 (abs \u03bb{n \u2192 (abs \u03bb{s \u2192 (abs \u03bb{z \u2192\n (app (app (var m) (var s)) (app (app (var n) (var s)) (var z)))})})})})\n\none : Exp \u03b5 Church\none = PH\u2192Exp (abs \u03bb{s \u2192 (abs \u03bb{z \u2192 (app (var s) (var z))})})\n\ntwo : Exp \u03b5 Church\ntwo = (app (app plus one) one)\n\nfour : Exp \u03b5 Church\nfour = (app (app plus two) two)\n\\end{code}\n\n\n# Denotational semantics\n\n\\begin{code}\ntype : Type \u2192 Set\ntype o = \u2115\ntype (A \u21d2 B) = type A \u2192 type B\n\nenv : Env \u2192 Set\nenv \u03b5 = \u22a4\nenv (\u0393 , A) = env \u0393 \u00d7 type A\n\nlookup : \u2200 {\u0393 : Env} {A : Type} \u2192 Var \u0393 A \u2192 env \u0393 \u2192 type A\nlookup Z \u27e8 \u03c1 , v \u27e9 = v\nlookup (S n) \u27e8 \u03c1 , v \u27e9 = lookup n \u03c1\n\neval : \u2200 {\u0393 : Env} {A : Type} \u2192 Exp \u0393 A \u2192 env \u0393 \u2192 type A\neval (var n) \u03c1 = lookup n \u03c1\neval (abs N) \u03c1 = \u03bb{ v \u2192 eval N \u27e8 \u03c1 , v \u27e9 }\neval (app L M) \u03c1 = eval L \u03c1 (eval M \u03c1)\n\nex\u2080 : eval four tt suc zero \u2261 4\nex\u2080 = refl\n\\end{code}\n\n# Operational semantics - with substitution a la Darais (31 lines)\n\n## Remove variable from environment (4 lines)\n\n\\begin{code}\ninfix 4 _\u229d_\n_\u229d_ : \u2200 {A : Type} (\u0393 : Env) \u2192 Var \u0393 A \u2192 Env\n(\u0393 , B) \u229d Z = \u0393\n(\u0393 , B) \u229d S k = (\u0393 \u229d k) , B\n\\end{code}\n\n## Rebuild environment (6 lines)\n\n\\begin{code}\nshunt : \u2200 (\u0393 \u0394 : Env) \u2192 Env\nshunt \u0393 \u03b5 = \u0393\nshunt \u0393 (\u0394 , A) = shunt (\u0393 , A) \u0394\n\nweaken : \u2200 (\u0393 \u0394 : Env) {A : Type} (k : Var \u0393 A) \u2192 Var (shunt \u0393 \u0394) A\nweaken \u0393 \u03b5 k = k\nweaken \u0393 (\u0394 , A) k = weaken (\u0393 , A) \u0394 (S k)\n\\end{code}\n\n## Lift term to a larger environment (8 lines)\n\n\\begin{code}\nliftvar : \u2200 {\u0393 : Env} {A B : Type} (j : Var \u0393 B) (k : Var (\u0393 \u229d j) A) \u2192 Var \u0393 A\nliftvar Z k = S k\nliftvar (S j) Z = Z\nliftvar (S j) (S k) = S (liftvar j k)\n\nlift : \u2200 {\u0393 : Env} {A B : Type} (j : Var \u0393 B) (M : Exp (\u0393 \u229d j) A) \u2192 Exp \u0393 A\nlift j (var k) = var (liftvar j k)\nlift j (abs N) = abs (lift (S j) N)\nlift j (app L M) = app (lift j L) (lift j M)\n\\end{code}\n\n## Substitution (13 lines)\n\n\\begin{code}\nsubstvar : \u2200 (\u0393 \u0394 : Env) {A B : Type} (j : Var \u0393 B) (k : Var \u0393 A) (P : Exp (shunt (\u0393 \u229d k) \u0394) A) \u2192 Exp (shunt (\u0393 \u229d k) \u0394) B\nsubstvar \u0393 \u0394 Z Z P = P\nsubstvar (\u0393 , A) \u0394 Z (S k) P = var (weaken ((\u0393 \u229d k) , A) \u0394 Z)\nsubstvar (\u0393 , A) \u0394 (S j) Z P = var (weaken \u0393 \u0394 j)\nsubstvar (\u0393 , A) \u0394 (S j) (S k) P = substvar \u0393 (\u0394 , A) j k P\n\nsubst : \u2200 {\u0393 : Env} {A B : Type} (N : Exp \u0393 B) (k : Var \u0393 A) (M : Exp (\u0393 \u229d k) A) \u2192 Exp (\u0393 \u229d k) B\nsubst {\u0393} (var j) k P = substvar \u0393 \u03b5 j k P\nsubst (abs N) k P = abs (subst N (S k) (lift Z P))\nsubst (app L M) k P = app (subst L k P) (subst M k P)\n\\end{code}\n\n# Operational semantics - with simultaneous substitution, a la McBride (18 lines)\n\n## Renaming (7 lines)\n\n\\begin{code}\nextend : \u2200 {\u0393 \u0394 : Env} {B : Type} \u2192 (\u2200 {A : Type} \u2192 Var \u0393 A \u2192 Var \u0394 A) \u2192 Var \u0394 B \u2192 (\u2200 {A : Type} \u2192 Var (\u0393 , B) A \u2192 Var \u0394 A)\nextend \u03c1 j Z = j\nextend \u03c1 j (S k) = \u03c1 k\n\nrename : \u2200 {\u0393 \u0394 : Env} \u2192 (\u2200 {A : Type} \u2192 Var \u0393 A \u2192 Var \u0394 A) \u2192 (\u2200 {A : Type} \u2192 Exp \u0393 A \u2192 Exp \u0394 A)\nrename \u03c1 (var n) = var (\u03c1 n)\nrename \u03c1 (abs N) = abs (rename (extend (S \u2218 \u03c1) Z) N)\nrename \u03c1 (app L M) = app (rename \u03c1 L) (rename \u03c1 M)\n\\end{code}\n\n## Substitution (9 lines)\n\n\\begin{code}\next : \u2200 {\u0393 \u0394 : Env} {B : Type} \u2192 (\u2200 {A : Type} \u2192 Var \u0393 A \u2192 Exp \u0394 A) \u2192 Exp \u0394 B \u2192 (\u2200 {A : Type} \u2192 Var (\u0393 , B) A \u2192 Exp \u0394 A)\next \u03c1 j Z = j\next \u03c1 j (S k) = \u03c1 k\n\nsub : \u2200 {\u0393 \u0394 : Env} \u2192 (\u2200 {A : Type} \u2192 Var \u0393 A \u2192 Exp \u0394 A) \u2192 (\u2200 {A : Type} \u2192 Exp \u0393 A \u2192 Exp \u0394 A)\nsub \u03c1 (var n) = \u03c1 n\nsub \u03c1 (app L M) = app (sub \u03c1 L) (sub \u03c1 M)\nsub \u03c1 (abs N) = abs (sub (ext (rename S \u2218 \u03c1) (var Z)) N)\n\nsubstitute : \u2200 {\u0393 : Env} {A B : Type} \u2192 Exp (\u0393 , A) B \u2192 Exp \u0393 A \u2192 Exp \u0393 B\nsubstitute N M = sub (ext var M) N\n\\end{code}\n\n## Value\n\n\\begin{code}\ndata Val : {\u0393 : Env} {A : Type} \u2192 Exp \u0393 A \u2192 Set where\n Fun : \u2200 {\u0393 : Env} {A B : Type} {N : Exp (\u0393 , A) B} \u2192\n Val (abs N)\n\\end{code}\n\n## Reduction step\n\n\\begin{code}\ndata _\u27f6_ : {\u0393 : Env} {A : Type} \u2192 Exp \u0393 A \u2192 Exp \u0393 A \u2192 Set where\n \u03be\u2081 : \u2200 {\u0393 : Env} {A B : Type} {L : Exp \u0393 (A \u21d2 B)} {L\u2032 : Exp \u0393 (A \u21d2 B)} {M : Exp \u0393 A} \u2192\n L \u27f6 L\u2032 \u2192\n app L M \u27f6 app L\u2032 M\n \u03be\u2082 : \u2200 {\u0393 : Env} {A B : Type} {L : Exp \u0393 (A \u21d2 B)} {M : Exp \u0393 A} {M\u2032 : Exp \u0393 A} \u2192\n Val L \u2192\n M \u27f6 M\u2032 \u2192\n app L M \u27f6 app L M\u2032\n \u03b2 : \u2200 {\u0393 : Env} {A B : Type} {N : Exp (\u0393 , A) B} {M : Exp \u0393 A} \u2192\n Val M \u2192\n app (abs N) M \u27f6 substitute N M\n\\end{code}\n\n## Reflexive and transitive closure\n\n\\begin{code}\ndata _\u27f6*_ : {\u0393 : Env} {A : Type} \u2192 Exp \u0393 A \u2192 Exp \u0393 A \u2192 Set where\n reflexive : \u2200 {\u0393 : Env} {A : Type} {M : Exp \u0393 A} \u2192\n M \u27f6* M\n inclusion : \u2200 {\u0393 : Env} {A : Type} {L M : Exp \u0393 A} \u2192\n L \u27f6 M \u2192\n L \u27f6* M\n transitive : \u2200 {\u0393 : Env} {A : Type} {L M N : Exp \u0393 A} \u2192\n L \u27f6* M \u2192\n M \u27f6* N \u2192\n L \u27f6* N\n\\end{code}\n\n## Displaying reduction sequences\n\n\\begin{code}\ninfix 1 begin_\ninfixr 2 _\u27f6\u27e8_\u27e9_\ninfix 3 _\u220e\n\nbegin_ : {\u0393 : Env} {A : Type} {M N : Exp \u0393 A} \u2192 (M \u27f6* N) \u2192 (M \u27f6* N)\nbegin steps = steps\n\n_\u27f6\u27e8_\u27e9_ : {\u0393 : Env} {A : Type} (L : Exp \u0393 A) {M N : Exp \u0393 A} \u2192 (L \u27f6 M) \u2192 (M \u27f6* N) \u2192 (L \u27f6* N)\nL \u27f6\u27e8 L\u27f6M \u27e9 M\u27f6*N = transitive (inclusion L\u27f6M) M\u27f6*N\n\n_\u220e : {\u0393 : Env} {A : Type} (M : Exp \u0393 A) \u2192 M \u27f6* M\nM \u220e = reflexive\n\\end{code}\n\n## Example reduction sequence\n\n\\begin{code}\nex\u2081 : (app (abs (var Z)) (abs (var Z))) \u27f6* (abs (var Z))\nex\u2081 =\n begin\n (app (abs {\u0393 = \u03b5} {A = o \u21d2 o} (var Z)) (abs (var Z)))\n \u27f6\u27e8 \u03b2 Fun \u27e9\n (abs (var Z))\n \u220e\n\\end{code}\n\n\\begin{code}\n\u0393one : \u2200 {\u0393} \u2192 Exp \u0393 Church\n\u0393one = (abs (abs (app (var (S Z)) (var Z))))\n\nex\u2082 : two \u27f6* (abs (abs (app (app \u0393one (var (S Z))) (app (app \u0393one (var (S Z))) (var Z)))))\nex\u2082 =\n begin\n (app (app plus one) one)\n \u27f6\u27e8 \u03be\u2081 (\u03b2 Fun) \u27e9\n (app (abs (abs (abs (app (app \u0393one (var (S Z))) (app (app (var (S (S Z))) (var (S Z))) (var Z)))))) \u0393one)\n \u27f6\u27e8 \u03b2 Fun \u27e9\n (abs (abs (app (app \u0393one (var (S Z))) (app (app \u0393one (var (S Z))) (var Z)))))\n \u220e\n\\end{code}\n\n\\begin{code}\nprogress : \u2200 {A : Type} \u2192 (M : Exp \u03b5 A) \u2192 (\u2203[ N ] (M \u27f6 N)) \u228e Val M\nprogress (var ())\nprogress (abs N) = inj\u2082 Fun\nprogress (app L M) with progress L\nprogress (app L M) | inj\u2081 \u27e8 L\u2032 , r \u27e9 = inj\u2081 \u27e8 app L\u2032 M , \u03be\u2081 r \u27e9\nprogress (app (abs N) M) | inj\u2082 Fun with progress M\nprogress (app (abs N) M) | inj\u2082 Fun | inj\u2081 \u27e8 M\u2032 , r \u27e9 = inj\u2081 \u27e8 app (abs N) M\u2032 , \u03be\u2082 Fun r \u27e9\nprogress (app (abs N) M) | inj\u2082 Fun | inj\u2082 ValM = inj\u2081 \u27e8 substitute N M , \u03b2 ValM \u27e9\n\\end{code}\n\n\n\\begin{code}\nex\u2083 : progress (app (app plus one) one) \u2261\n inj\u2081 \u27e8 (app (abs (abs (abs (app (app \u0393one (var (S Z))) (app (app (var (S (S Z))) (var (S Z))) (var Z)))))) \u0393one) , \u03be\u2081 (\u03b2 Fun) \u27e9\nex\u2083 = refl\n\nex\u2084 : progress (app (abs (abs (abs (app (app \u0393one (var (S Z))) (app (app (var (S (S Z))) (var (S Z))) (var Z)))))) \u0393one) \u2261\n inj\u2081 \u27e8 (abs (abs (app (app \u0393one (var (S Z))) (app (app \u0393one (var (S Z))) (var Z))))) , \u03b2 Fun \u27e9\nex\u2084 = refl\n\nex\u2085 : progress (abs (abs (app (app \u0393one (var (S Z))) (app (app \u0393one (var (S Z))) (var Z))))) \u2261 inj\u2082 Fun\nex\u2085 = refl\n\\end{code}\n","avg_line_length":27.1564792176,"max_line_length":129,"alphanum_fraction":0.515710813} +{"size":29857,"ext":"lagda","lang":"Literate Agda","max_stars_count":3.0,"content":"---\ntitle : \"Lists: Lists and higher-order functions\"\nlayout : page\nprev : \/Decidable\/\npermalink : \/Lists\/\nnext : \/Lambda\/\n---\n\n\\begin{code}\nmodule plfa.Lists where\n\\end{code}\n\nThis chapter discusses the list data type. It gives further examples\nof many of the techniques we have developed so far, and provides\nexamples of polymorphic types and higher-order functions.\n\n## Imports\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl; sym; trans; cong)\nopen Eq.\u2261-Reasoning\nopen import Data.Bool using (Bool; true; false; T; _\u2227_; _\u2228_; not)\nopen import Data.Nat using (\u2115; zero; suc; _+_; _*_; _\u2238_; _\u2264_; s\u2264s; z\u2264n)\nopen import Data.Nat.Properties using\n (+-assoc; +-identity\u02e1; +-identity\u02b3; *-assoc; *-identity\u02e1; *-identity\u02b3)\nopen import Relation.Nullary using (\u00ac_; Dec; yes; no)\nopen import Data.Product using (_\u00d7_; \u2203; \u2203-syntax) renaming (_,_ to \u27e8_,_\u27e9)\nopen import Function using (_\u2218_)\nopen import Level using (Level)\nopen import plfa.Isomorphism using (_\u2243_; _\u21d4_)\n\\end{code}\n\n\n## Lists\n\nLists are defined in Agda as follows:\n\\begin{code}\ndata List (A : Set) : Set where\n [] : List A\n _\u2237_ : A \u2192 List A \u2192 List A\n\ninfixr 5 _\u2237_\n\\end{code}\nLet's unpack this definition. If `A` is a set, then `List A` is a set.\nThe next two lines tell us that `[]` (pronounced _nil_) is a list of\ntype `A` (often called the _empty_ list), and that `_\u2237_` (pronounced\n_cons_, short for _constructor_) takes a value of type `A` and a value\nof type `List A` and returns a value of type `List A`. Operator `_\u2237_`\nhas precedence level 5 and associates to the right.\n\nFor example,\n\\begin{code}\n_ : List \u2115\n_ = 0 \u2237 1 \u2237 2 \u2237 []\n\\end{code}\ndenotes the list of the first three natural numbers. Since `_::_`\nassociates to the right, the term parses as `0 \u2237 (1 \u2237 (2 \u2237 []))`.\nHere `0` is the first element of the list, called the _head_,\nand `1 \u2237 (2 \u2237 [])` is a list of the remaining elements, called the\n_tail_. A list is a strange beast: it has a head and a tail,\nnothing in between, and the tail is itself another list!\n\nAs we've seen, parameterised types can be translated to\nindexed types. The definition above is equivalent to the following:\n\\begin{code}\ndata List\u2032 : Set \u2192 Set where\n []\u2032 : \u2200 {A : Set} \u2192 List\u2032 A\n _\u2237\u2032_ : \u2200 {A : Set} \u2192 A \u2192 List\u2032 A \u2192 List\u2032 A\n\\end{code}\nEach constructor takes the parameter as an implicit argument.\nThus, our example list could also be written\n\\begin{code}\n_ : List \u2115\n_ = _\u2237_ {\u2115} 0 (_\u2237_ {\u2115} 1 (_\u2237_ {\u2115} 2 ([] {\u2115})))\n\\end{code}\nwhere here we have provided the implicit parameters explicitly.\n\nIncluding the pragma\n\n {-# BUILTIN LIST List #-}\n\ntells Agda that the type `List` corresponds to the Haskell type\nlist, and the constructors `[]` and `_\u2237_` correspond to nil and\ncons respectively, allowing a more efficient representation of lists.\n\n\n## List syntax\n\nWe can write lists more conveniently by introducing the following definitions:\n\\begin{code}\npattern [_] z = z \u2237 []\npattern [_,_] y z = y \u2237 z \u2237 []\npattern [_,_,_] x y z = x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_] w x y z = w \u2237 x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_,_] v w x y z = v \u2237 w \u2237 x \u2237 y \u2237 z \u2237 []\npattern [_,_,_,_,_,_] u v w x y z = u \u2237 v \u2237 w \u2237 x \u2237 y \u2237 z \u2237 []\n\\end{code}\nThis is our first use of pattern declarations. For instance,\nthe third line tells us that `[ x , y , z ]` is equivalent to\n`x \u2237 y \u2237 z \u2237 []`, and permits the former to appear either in\na pattern on the left-hand side of an equation, or a term\non the right-hand side of an equation.\n\n\n## Append\n\nOur first function on lists is written `_++_` and pronounced\n_append_:\n\n\\begin{code}\ninfixr 5 _++_\n\n_++_ : \u2200 {A : Set} \u2192 List A \u2192 List A \u2192 List A\n[] ++ ys = ys\n(x \u2237 xs) ++ ys = x \u2237 (xs ++ ys)\n\\end{code}\nThe type `A` is an implicit argument to append, making it a\n_polymorphic_ function (one that can be used at many types). The\nempty list appended to another list yields the other list. A\nnon-empty list appended to another list yields a list with head the\nsame as the head of the first list and tail the same as the tail of\nthe first list appended to the second list.\n\nHere is an example, showing how to compute the result\nof appending two lists:\n\\begin{code}\n_ : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] \u2261 [ 0 , 1 , 2 , 3 , 4 ]\n_ =\n begin\n 0 \u2237 1 \u2237 2 \u2237 [] ++ 3 \u2237 4 \u2237 []\n \u2261\u27e8\u27e9\n 0 \u2237 (1 \u2237 2 \u2237 [] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 (2 \u2237 [] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 2 \u2237 ([] ++ 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 0 \u2237 1 \u2237 2 \u2237 3 \u2237 4 \u2237 []\n \u220e\n\\end{code}\nAppending two lists requires time linear in the\nnumber of elements in the first list.\n\n\n## Reasoning about append\n\nWe can reason about lists in much the same way that we reason\nabout numbers. Here is the proof that append is associative:\n\\begin{code}\n++-assoc : \u2200 {A : Set} (xs ys zs : List A)\n \u2192 (xs ++ ys) ++ zs \u2261 xs ++ (ys ++ zs)\n++-assoc [] ys zs =\n begin\n ([] ++ ys) ++ zs\n \u2261\u27e8\u27e9\n ys ++ zs\n \u2261\u27e8\u27e9\n [] ++ (ys ++ zs)\n \u220e\n++-assoc (x \u2237 xs) ys zs =\n begin\n (x \u2237 xs ++ ys) ++ zs\n \u2261\u27e8\u27e9\n x \u2237 ((xs ++ ys) ++ zs)\n \u2261\u27e8 cong (x \u2237_) (++-assoc xs ys zs) \u27e9\n x \u2237 (xs ++ (ys ++ zs))\n \u2261\u27e8\u27e9\n x \u2237 xs ++ (ys ++ zs)\n \u220e\n\\end{code}\nThe proof is by induction on the first argument. The base case instantiates\nto `[]`, and follows by straightforward computation.\nThe inductive case instantiates to `x \u2237 xs`,\nand follows by straightforward computation combined with the\ninductive hypothesis. As usual, the inductive hypothesis is indicated by a recursive\ninvocation of the proof, in this case `++-assoc xs ys zs`.\n\nRecall that Agda supports [sections][plfa.Induction#sections].\nApplying `cong (x \u2237_)` promotes the inductive hypothesis\n\n xs ++ (ys ++ zs) \u2261 (xs ++ ys) ++ zs\n\nto the equality\n\n x \u2237 (xs ++ (ys ++ zs)) \u2261 x \u2237 ((xs ++ ys) ++ zs)\n\nwhich is needed in the proof.\n\nIt is also easy to show that `[]` is a left and right identity for `_++_`.\nThat it is a left identity is immediate from the definition:\n\\begin{code}\n++-identity\u02e1 : \u2200 {A : Set} (xs : List A) \u2192 [] ++ xs \u2261 xs\n++-identity\u02e1 xs =\n begin\n [] ++ xs\n \u2261\u27e8\u27e9\n xs\n \u220e\n\\end{code}\nThat it is a right identity follows by simple induction:\n\\begin{code}\n++-identity\u02b3 : \u2200 {A : Set} (xs : List A) \u2192 xs ++ [] \u2261 xs\n++-identity\u02b3 [] =\n begin\n [] ++ []\n \u2261\u27e8\u27e9\n []\n \u220e\n++-identity\u02b3 (x \u2237 xs) =\n begin\n (x \u2237 xs) ++ []\n \u2261\u27e8\u27e9\n x \u2237 (xs ++ [])\n \u2261\u27e8 cong (x \u2237_) (++-identity\u02b3 xs) \u27e9\n x \u2237 xs\n \u220e\n\\end{code}\nAs we will see later,\nthese three properties establish that `_++_` and `[]` form\na _monoid_ over lists.\n\n## Length\n\nOur next function finds the length of a list:\n\\begin{code}\nlength : \u2200 {A : Set} \u2192 List A \u2192 \u2115\nlength [] = zero\nlength (x \u2237 xs) = suc (length xs)\n\\end{code}\nAgain, it takes an implicit parameter `A`.\nThe length of the empty list is zero.\nThe length of a non-empty list\nis one greater than the length of the tail of the list.\n\nHere is an example showing how to compute the length of a list:\n\\begin{code}\n_ : length [ 0 , 1 , 2 ] \u2261 3\n_ =\n begin\n length (0 \u2237 1 \u2237 2\u00a0\u2237 [])\n \u2261\u27e8\u27e9\n suc (length (1 \u2237 2 \u2237 []))\n \u2261\u27e8\u27e9\n suc (suc (length (2 \u2237 [])))\n \u2261\u27e8\u27e9\n suc (suc (suc (length {\u2115} [])))\n \u2261\u27e8\u27e9\n suc (suc (suc zero))\n \u220e\n\\end{code}\nComputing the length of a list requires time\nlinear in the number of elements in the list.\n\nIn the second-to-last line, we cannot write simply `length []` but\nmust instead write `length {\u2115} []`. Since `[]` has no elements, Agda\nhas insufficient information to infer the implicit parameter.\n\n\n## Reasoning about length\n\nThe length of one list appended to another is the\nsum of the lengths of the lists:\n\\begin{code}\nlength-++ : \u2200 {A : Set} (xs ys : List A)\n \u2192 length (xs ++ ys) \u2261 length xs + length ys\nlength-++ {A} [] ys =\n begin\n length ([] ++ ys)\n \u2261\u27e8\u27e9\n length ys\n \u2261\u27e8\u27e9\n length {A} [] + length ys\n \u220e\nlength-++ (x \u2237 xs) ys =\n begin\n length ((x \u2237 xs) ++ ys)\n \u2261\u27e8\u27e9\n suc (length (xs ++ ys))\n \u2261\u27e8 cong suc (length-++ xs ys) \u27e9\n suc (length xs + length ys)\n \u2261\u27e8\u27e9\n length (x \u2237 xs) + length ys\n \u220e\n\\end{code}\nThe proof is by induction on the first arugment. The base case\ninstantiates to `[]`, and follows by straightforward computation. As\nbefore, Agda cannot infer the implicit type parameter to `length`, and\nit must be given explicitly. The inductive case instantiates to\n`x \u2237 xs`, and follows by straightforward computation combined with the\ninductive hypothesis. As usual, the inductive hypothesis is indicated\nby a recursive invocation of the proof, in this case `length-++ xs ys`,\nand it is promoted by the congruence `cong suc`.\n\n\n## Reverse\n\nUsing append, it is easy to formulate a function to reverse a list:\n\\begin{code}\nreverse : \u2200 {A : Set} \u2192 List A \u2192 List A\nreverse [] = []\nreverse (x \u2237 xs) = reverse xs ++ [ x ]\n\\end{code}\nThe reverse of the empty list is the empty list.\nThe reverse of a non-empty list\nis the reverse of its tail appended to a unit list\ncontaining its head.\n\nHere is an example showing how to reverse a list:\n\\begin{code}\n_ : reverse [ 0 , 1 , 2 ] \u2261 [ 2 , 1 , 0 ]\n_ =\n begin\n reverse (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n reverse (1 \u2237 2 \u2237 []) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (reverse (2 \u2237 []) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n ((reverse [] ++ [ 2 ]) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (([] ++ [ 2 ]) ++ [ 1 ]) ++ [ 0 ]\n \u2261\u27e8\u27e9\n (([] ++ 2 \u2237 []) ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n (2 \u2237 [] ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n 2 \u2237 ([] ++ 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n (2 \u2237 1 \u2237 []) ++ 0 \u2237 []\n \u2261\u27e8\u27e9\n 2 \u2237 (1 \u2237 [] ++ 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 ([] ++ 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 0 \u2237 []\n \u2261\u27e8\u27e9\n [ 2 , 1 , 0 ]\n \u220e\n\\end{code}\nReversing a list in this way takes time _quadratic_ in the length of\nthe list. This is because reverse ends up appending lists of lengths\n`1`, `2`, up to `n - 1`, where `n` is the length of the list being\nreversed, append takes time linear in the length of the first\nlist, and the sum of the numbers up to `n - 1` is `n * (n - 1) \/ 2`.\n(We will validate that last fact in an exercise later in this chapter.)\n\n#### Exercise `reverse-++-commute` (recommended)\n\nShow that the reverse of one list appended to another is the\nreverse of the second appended to the reverse of the first:\n\\begin{code}\npostulate\n reverse-++-commute : \u2200 {A : Set} {xs ys : List A}\n \u2192 reverse (xs ++ ys) \u2261 reverse ys ++ reverse xs\n\\end{code}\n\n#### Exercise `reverse-involutive` (recommended)\n\nA function is an _involution_ if when applied twice it acts\nas the identity function. Show that reverse is an involution:\n\\begin{code}\npostulate\n reverse-involutive : \u2200 {A : Set} {xs : List A}\n \u2192 reverse (reverse xs) \u2261 xs\n\\end{code}\n\n\n## Faster reverse\n\nThe definition above, while easy to reason about, is less efficient than\none might expect since it takes time quadratic in the length of the list.\nThe idea is that we generalise reverse to take an additional argument:\n\\begin{code}\nshunt : \u2200 {A : Set} \u2192 List A \u2192 List A \u2192 List A\nshunt [] ys = ys\nshunt (x \u2237 xs) ys = shunt xs (x \u2237 ys)\n\\end{code}\nThe definition is by recursion on the first argument. The second argument\nactually becomes _larger_, but this is not a problem because the argument\non which we recurse becomes _smaller_.\n\nShunt is related to reverse as follows:\n\\begin{code}\nshunt-reverse : \u2200 {A : Set} (xs ys : List A)\n \u2192 shunt xs ys \u2261 reverse xs ++ ys\nshunt-reverse [] ys =\n begin\n shunt [] ys\n \u2261\u27e8\u27e9\n ys\n \u2261\u27e8\u27e9\n reverse [] ++ ys\n \u220e\nshunt-reverse (x \u2237 xs) ys =\n begin\n shunt (x \u2237 xs) ys\n \u2261\u27e8\u27e9\n shunt xs (x \u2237 ys)\n \u2261\u27e8 shunt-reverse xs (x \u2237 ys) \u27e9\n reverse xs ++ (x \u2237 ys)\n \u2261\u27e8\u27e9\n reverse xs ++ ([ x ] ++ ys)\n \u2261\u27e8 sym (++-assoc (reverse xs) [ x ] ys) \u27e9\n (reverse xs ++ [ x ]) ++ ys\n \u2261\u27e8\u27e9\n reverse (x \u2237 xs) ++ ys\n \u220e\n\\end{code}\nThe proof is by induction on the first argument.\nThe base case instantiates to `[]`, and follows by straightforward computation.\nThe inductive case instantiates to `x \u2237 xs` and follows by the inductive\nhypothesis and associativity of append. When we invoke the inductive hypothesis,\nthe second argument actually becomes *larger*, but this is not a problem because\nthe argument on which we induct becomes *smaller*.\n\nGeneralising on an auxiliary argument, which becomes larger as the argument on\nwhich we recurse or induct becomes smaller, is a common trick. It belongs in\nyour quiver of arrows, ready to slay the right problem.\n\nHaving defined shunt be generalisation, it is now easy to respecialise to\ngive a more efficient definition of reverse:\n\\begin{code}\nreverse\u2032 : \u2200 {A : Set} \u2192 List A \u2192 List A\nreverse\u2032 xs = shunt xs []\n\\end{code}\n\nGiven our previous lemma, it is straightforward to show\nthe two definitions equivalent:\n\\begin{code}\nreverses : \u2200 {A : Set} (xs : List A)\n \u2192 reverse\u2032 xs \u2261 reverse xs\nreverses xs =\n begin\n reverse\u2032 xs\n \u2261\u27e8\u27e9\n shunt xs []\n \u2261\u27e8 shunt-reverse xs [] \u27e9\n reverse xs ++ []\n \u2261\u27e8 ++-identity\u02b3 (reverse xs) \u27e9 \n reverse xs\n \u220e\n\\end{code}\n\nHere is an example showing fast reverse of the list `[ 0 , 1 , 2 ]`:\n\\begin{code}\n_ : reverse\u2032 [ 0 , 1 , 2 ] \u2261 [ 2 , 1 , 0 ]\n_ =\n begin\n reverse\u2032 (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n shunt (0 \u2237 1 \u2237 2 \u2237 []) []\n \u2261\u27e8\u27e9\n shunt (1 \u2237 2 \u2237 []) (0 \u2237 [])\n \u2261\u27e8\u27e9\n shunt (2 \u2237 []) (1 \u2237 0 \u2237 [])\n \u2261\u27e8\u27e9\n shunt [] (2 \u2237 1 \u2237 0 \u2237 [])\n \u2261\u27e8\u27e9\n 2 \u2237 1 \u2237 0 \u2237 []\n \u220e\n\\end{code}\nNow the time to reverse a list is linear in the length of the list.\n\n## Map {#Map}\n\nMap applies a function to every element of a list to generate a corresponding list.\nMap is an example of a _higher-order function_, one which takes a function as an\nargument and returns a function as a result:\n\\begin{code}\nmap : \u2200 {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\\end{code}\nMap of the empty list is the empty list.\nMap of a non-empty list yields a list\nwith head the same as the function applied to the head of the given list,\nand tail the same as map of the function applied to the tail of the given list.\n\nHere is an example showing how to use map to increment every element of a list:\n\\begin{code}\n_ : map suc [ 0 , 1 , 2 ] \u2261 [ 1 , 2 , 3 ]\n_ =\n begin\n map suc (0 \u2237 1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 map suc (1 \u2237 2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 map suc (2 \u2237 [])\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 suc 2 \u2237 map suc []\n \u2261\u27e8\u27e9\n suc 0 \u2237 suc 1 \u2237 suc 2 \u2237 []\n \u2261\u27e8\u27e9\n 1 \u2237 2 \u2237 3 \u2237 []\n \u220e\n\\end{code}\nMap requires time linear in the length of the list.\n\nIt is often convenient to exploit currying by applying\nmap to a function to yield a new function, and at a later\npoint applying the resulting function:\n\\begin{code}\nsucs : List \u2115 \u2192 List \u2115\nsucs = map suc\n\n_ : sucs [ 0 , 1 , 2 ] \u2261 [ 1 , 2 , 3 ]\n_ =\n begin\n sucs [ 0 , 1 , 2 ]\n \u2261\u27e8\u27e9\n map suc [ 0 , 1 , 2 ]\n \u2261\u27e8\u27e9\n [ 1 , 2 , 3 ]\n \u220e\n\\end{code}\n\nAny type that is parameterised on another type, such as lists, has a\ncorresponding map, which accepts a function and returns a function\nfrom the type parameterised on the domain of the function to the type\nparameterised on the range of the function. Further, a type that is\nparameterised on _n_ types will have a map that is parameterised on\n_n_ functions.\n\n\n#### Exercise `map-compose`\n\nProve that the map of a composition is equal to the composition of two maps:\n\\begin{code}\npostulate\n map-compose : \u2200 {A B C : Set} {f : A \u2192 B} {g : B \u2192 C}\n \u2192 map (g \u2218 f) \u2261 map g \u2218 map f\n\\end{code}\nThe last step of the proof requires extensionality.\n\n#### Exercise `map-++-commute`\n\nProve the following relationship between map and append:\n\\begin{code}\npostulate\n map-++-commute : \u2200 {A B : Set} {f : A \u2192 B} {xs ys : List A}\n \u2192 map f (xs ++ ys) \u2261 map f xs ++ map f ys\n\\end{code}\n\n#### Exercise `map-Tree`\n\nDefine a type of trees with leaves of type `A` and internal\nnodes of type `B`:\n\\begin{code}\ndata Tree (A B : Set) : Set where\n leaf : A \u2192 Tree A B\n node : Tree A B \u2192 B \u2192 Tree A B \u2192 Tree A B\n\\end{code}\nDefine a suitable map operator over trees:\n\\begin{code}\npostulate\n map-Tree : \u2200 {A B C D : Set}\n \u2192 (A \u2192 C) \u2192 (B \u2192 D) \u2192 Tree A B \u2192 Tree C D\n\\end{code}\n\n\n## Fold {#Fold}\n\nFold takes an operator and a value, and uses the operator to combine\neach of the elements of the list, taking the given value as the result\nfor the empty list:\n\\begin{code}\nfoldr : \u2200 {A B : Set} \u2192 (A \u2192 B \u2192 B) \u2192 B \u2192 List A \u2192 B\nfoldr _\u2297_ e [] = e\nfoldr _\u2297_ e (x \u2237 xs) = x \u2297 foldr _\u2297_ e xs\n\\end{code}\nFold of the empty list is the given value.\nFold of a non-empty list uses the operator to combine\nthe head of the list and the fold of the tail of the list.\n\nHere is an example showing how to use fold to find the sum of a list:\n\\begin{code}\n_ : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] \u2261 10\n_ =\n begin\n foldr _+_ 0 (1 \u2237 2 \u2237 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 1 + foldr _+_ 0 (2 \u2237 3 \u2237 4 \u2237 [])\n \u2261\u27e8\u27e9\n 1 + (2 + foldr _+_ 0 (3 \u2237 4 \u2237 []))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + foldr _+_ 0 (4 \u2237 [])))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + (4 + foldr _+_ 0 [])))\n \u2261\u27e8\u27e9\n 1 + (2 + (3 + (4 + 0)))\n \u220e\n\\end{code}\nFold requires time linear in the length of the list.\n\nIt is often convenient to exploit currying by applying\nfold to an operator and a value to yield a new function,\nand at a later point applying the resulting function:\n\\begin{code}\nsum : List \u2115 \u2192 \u2115\nsum = foldr _+_ 0\n\n_ : sum [ 1 , 2 , 3 , 4 ] \u2261 10\n_ =\n begin\n sum [ 1 , 2 , 3 , 4 ]\n \u2261\u27e8\u27e9\n foldr _+_ 0 [ 1 , 2 , 3 , 4 ]\n \u2261\u27e8\u27e9\n 10\n \u220e\n\\end{code}\n\nJust as the list type has two constructors, `[]` and `_\u2237_`,\nso the fold function takes two arguments, `e` and `_\u2297_`\n(in addition to the list argument).\nIn general, a data type with _n_ constructors will have\na corresponding fold function that takes _n_ arguments.\n\n#### Exercise `product` (recommended)\n\nUse fold to define a function to find the product of a list of numbers.\nFor example:\n\n product [ 1 , 2 , 3 , 4 ] \u2261 24\n\n#### Exercise `foldr-++` (recommended)\n\nShow that fold and append are related as follows:\n\\begin{code}\npostulate\n foldr-++ : \u2200 {A B : Set} (_\u2297_ : A \u2192 B \u2192 B) (e : B) (xs ys : List A) \u2192\n foldr _\u2297_ e (xs ++ ys) \u2261 foldr _\u2297_ (foldr _\u2297_ e ys) xs\n\\end{code}\n\n\n#### Exercise `map-is-foldr`\n\nShow that map can be defined using fold:\n\\begin{code}\npostulate\n map-is-foldr : \u2200 {A B : Set} {f : A \u2192 B} \u2192\n map f \u2261 foldr (\u03bb x xs \u2192 f x \u2237 xs) []\n\\end{code}\nThis requires extensionality.\n\n#### Exercise `fold-Tree`\n\nDefine a suitable fold function for the type of trees given earlier:\n\\begin{code}\npostulate\n fold-Tree : \u2200 {A B C : Set}\n \u2192 (A \u2192 C) \u2192 (C \u2192 B \u2192 C \u2192 C) \u2192 Tree A B \u2192 C\n\\end{code}\n\n#### Exercise `map-is-fold-Tree`\n\nDemonstrate an anologue of `map-is-foldr` for the type of trees.\n\n#### Exercise `sum-downFrom` (stretch)\n\nDefine a function that counts down as follows:\n\\begin{code}\ndownFrom : \u2115 \u2192 List \u2115\ndownFrom zero = []\ndownFrom (suc n) = n \u2237 downFrom n\n\\end{code}\nFor example:\n\\begin{code}\n_ : downFrom 3 \u2261 [ 2 , 1 , 0 ]\n_ = refl\n\\end{code}\nProve that the sum of the numbers `(n - 1) + \u22ef + 0` is\nequal to `n * (n \u2238 1) \/ 2`:\n\\begin{code}\npostulate\n sum-downFrom : \u2200 (n : \u2115)\n \u2192 sum (downFrom n) * 2 \u2261 n * (n \u2238 1)\n\\end{code}\n\n\n## Monoids\n\nTypically when we use a fold the operator is associative and the\nvalue is a left and right identity for the value, meaning that the\noperator and the value form a _monoid_.\n\nWe can define a monoid as a suitable record type:\n\\begin{code}\nrecord IsMonoid {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) : Set where\n field\n assoc : \u2200 (x y z : A) \u2192 (x \u2297 y) \u2297 z \u2261 x \u2297 (y \u2297 z)\n identity\u02e1 : \u2200 (x : A) \u2192 e \u2297 x \u2261 x\n identity\u02b3 : \u2200 (x : A) \u2192 x \u2297 e \u2261 x\n\nopen IsMonoid\n\\end{code}\n\nAs examples, sum and zero, multiplication and one, and append and the empty\nlist, are all examples of monoids:\n\\begin{code}\n+-monoid : IsMonoid _+_ 0\n+-monoid =\n record\n { assoc = +-assoc\n ; identity\u02e1 = +-identity\u02e1\n ; identity\u02b3 = +-identity\u02b3\n }\n\n*-monoid : IsMonoid _*_ 1\n*-monoid =\n record\n { assoc = *-assoc\n ; identity\u02e1 = *-identity\u02e1\n ; identity\u02b3 = *-identity\u02b3\n }\n\n++-monoid : \u2200 {A : Set} \u2192 IsMonoid {List A} _++_ []\n++-monoid =\n record\n { assoc = ++-assoc\n ; identity\u02e1 = ++-identity\u02e1\n ; identity\u02b3 = ++-identity\u02b3\n }\n\\end{code}\n\nIf `_\u2297_` and `e` form a monoid, then we can re-express fold on the\nsame operator and an arbitrary value:\n\\begin{code}\nfoldr-monoid : \u2200 {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) \u2192 IsMonoid _\u2297_ e \u2192\n \u2200 (xs : List A) (y : A) \u2192 foldr _\u2297_ y xs \u2261 foldr _\u2297_ e xs \u2297 y\nfoldr-monoid _\u2297_ e \u2297-monoid [] y =\n begin\n foldr _\u2297_ y []\n \u2261\u27e8\u27e9\n y\n \u2261\u27e8 sym (identity\u02e1 \u2297-monoid y) \u27e9\n (e \u2297 y)\n \u2261\u27e8\u27e9\n foldr _\u2297_ e [] \u2297 y\n \u220e\nfoldr-monoid _\u2297_ e \u2297-monoid (x \u2237 xs) y =\n begin\n foldr _\u2297_ y (x \u2237 xs)\n \u2261\u27e8\u27e9\n x \u2297 (foldr _\u2297_ y xs)\n \u2261\u27e8 cong (x \u2297_) (foldr-monoid _\u2297_ e \u2297-monoid xs y) \u27e9\n x \u2297 (foldr _\u2297_ e xs \u2297 y)\n \u2261\u27e8 sym (assoc \u2297-monoid x (foldr _\u2297_ e xs) y) \u27e9\n (x \u2297 foldr _\u2297_ e xs) \u2297 y\n \u2261\u27e8\u27e9\n foldr _\u2297_ e (x \u2237 xs) \u2297 y\n \u220e\n\\end{code}\n\nAs a consequence, using a previous exercise, we have the following:\n\\begin{code}\nfoldr-monoid-++ : \u2200 {A : Set} (_\u2297_ : A \u2192 A \u2192 A) (e : A) \u2192 IsMonoid _\u2297_ e \u2192\n \u2200 (xs ys : List A) \u2192 foldr _\u2297_ e (xs ++ ys) \u2261 foldr _\u2297_ e xs \u2297 foldr _\u2297_ e ys\nfoldr-monoid-++ _\u2297_ e monoid-\u2297 xs ys =\n begin\n foldr _\u2297_ e (xs ++ ys)\n \u2261\u27e8 foldr-++ _\u2297_ e xs ys \u27e9\n foldr _\u2297_ (foldr _\u2297_ e ys) xs\n \u2261\u27e8 foldr-monoid _\u2297_ e monoid-\u2297 xs (foldr _\u2297_ e ys) \u27e9\n foldr _\u2297_ e xs \u2297 foldr _\u2297_ e ys\n \u220e\n\\end{code}\n\n#### Exercise `foldl`\n\nDefine a function `foldl` which is analogous to `foldr`, but where\noperations associate to the left rather than the right. For example:\n\n foldr _\u2297_ e [ x , y , z ] = x \u2297 (y \u2297 (z \u2297 e))\n foldl _\u2297_ e [ x , y , z ] = ((e \u2297 x) \u2297 y) \u2297 z\n\n\n#### Exercise `foldr-monoid-foldl`\n\nShow that if `_\u2295_` and `e` form a monoid, then `foldr _\u2297_ e` and\n`foldl _\u2297_ e` always compute the same result.\n\n\n## All {#All}\n\nWe can also define predicates over lists. Two of the most important\nare `All` and `Any`.\n\nPredicate `All P` holds if predicate `P` is satisfied by every element of a list:\n\\begin{code}\ndata All {A : Set} (P : A \u2192 Set) : List A \u2192 Set where\n [] : All P []\n _\u2237_ : \u2200 {x : A} {xs : List A} \u2192 P x \u2192 All P xs \u2192 All P (x \u2237 xs)\n\\end{code}\nThe type has two constructors, reusing the names of the same constructors for lists.\nThe first asserts that `P` holds for every element of the empty list.\nThe second asserts that if `P` holds of the head of a list and for every\nelement of the tail of a list, then `P` holds for every element of the list.\nAgda uses types to disambiguate whether the constructor is building\na list or evidence that `All P` holds.\n\nFor example, `All (_\u2264 2)` holds of a list where every element is less\nthan or equal to two. Recall that `z\u2264n` proves `zero \u2264 n` for any\n`n`, and that if `m\u2264n` proves `m \u2264 n` then `s\u2264s m\u2264n` proves `suc m \u2264\nsuc n`, for any `m` and `n`:\n\\begin{code}\n_ : All (_\u2264 2) [ 0 , 1 , 2 ]\n_ = z\u2264n \u2237 s\u2264s z\u2264n \u2237 s\u2264s (s\u2264s z\u2264n) \u2237 []\n\\end{code}\nHere `_\u2237_` and `[]` are the constructors of `All P` rather than of `List A`.\nThe three items are proofs of `0 \u2264 2`, `1 \u2264 2`, and `2 \u2264 2`, respectively.\n\n(One might wonder whether a pattern such as `[_,_,_]` can be used to\nconstruct values of type `All` as well as type `List`, since both use\nthe same constructors. Indeed it can, so long as both types are in\nscope when the pattern is declared. That's not the case here, since\n`List` is defined before `[_,_,_]`, but `All` is defined later.)\n\n\n## Any\n\nPredicate `Any P` holds if predicate `P` is satisfied by some element of a list:\n\\begin{code}\ndata Any {A : Set} (P : A \u2192 Set) : List A \u2192 Set where\n here : \u2200 {x : A} {xs : List A} \u2192 P x \u2192 Any P (x \u2237 xs)\n there : \u2200 {x : A} {xs : List A} \u2192 Any P xs \u2192 Any P (x \u2237 xs)\n\\end{code}\nThe first constructor provides evidence that the head of the list\nsatisfies `P`, while the second provides evidence that some element of\nthe tail of the list satisfies `P`. For example, we can define list\nmembership as follows:\n\\begin{code}\ninfix 4 _\u2208_ _\u2209_\n\n_\u2208_ : \u2200 {A : Set} (x : A) (xs : List A) \u2192 Set\nx \u2208 xs = Any (x \u2261_) xs\n\n_\u2209_ : \u2200 {A : Set} (x : A) (xs : List A) \u2192 Set\nx \u2209 xs = \u00ac (x \u2208 xs)\n\\end{code}\nFor example, zero is an element of the list `[ 0 , 1 , 0 , 2 ]`. Indeed, we can demonstrate\nthis fact in two different ways, corresponding to the two different\noccurrences of zero in the list, as the first element and as the third element:\n\\begin{code}\n_ : 0 \u2208 [ 0 , 1 , 0 , 2 ]\n_ = here refl\n\n_ : 0 \u2208 [ 0 , 1 , 0 , 2 ]\n_ = there (there (here refl))\n\\end{code}\nFurther, we can demonstrate that three is not in the list, because\nany possible proof that it is in the list leads to contradiction:\n\\begin{code}\nnot-in : 3 \u2209 [ 0 , 1 , 0 , 2 ]\nnot-in (here ())\nnot-in (there (here ()))\nnot-in (there (there (here ())))\nnot-in (there (there (there (here ()))))\nnot-in (there (there (there (there ()))))\n\\end{code}\nThe five occurrences of `()` attest to the fact that there is no\npossible evidence for `3 \u2261 0`, `3 \u2261 1`, `3 \u2261 0`, `3 \u2261 2`, and\n`3 \u2208 []`, respectively.\n\n## All and append\n\nA predicate holds for every element of one list appended to another if and\nonly if it holds for every element of each list:\n\\begin{code}\nAll-++-\u21d4 : \u2200 {A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P (xs ++ ys) \u21d4 (All P xs \u00d7 All P ys)\nAll-++-\u21d4 xs ys =\n record\n { to = to xs ys\n ; from = from xs ys\n }\n where\n\n to : \u2200 {A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P (xs ++ ys) \u2192 (All P xs \u00d7 All P ys)\n to [] ys Pys = \u27e8 [] , Pys \u27e9\n to (x \u2237 xs) ys (Px \u2237 Pxs++ys) with to xs ys Pxs++ys\n ... | \u27e8 Pxs , Pys \u27e9 = \u27e8 Px \u2237 Pxs , Pys \u27e9\n\n from : \u2200 { A : Set} {P : A \u2192 Set} (xs ys : List A) \u2192\n All P xs \u00d7 All P ys \u2192 All P (xs ++ ys)\n from [] ys \u27e8 [] , Pys \u27e9 = Pys\n from (x \u2237 xs) ys \u27e8 Px \u2237 Pxs , Pys \u27e9 = Px \u2237 from xs ys \u27e8 Pxs , Pys \u27e9\n\\end{code}\n\n#### Exercise `Any-++-\u21d4` (recommended)\n\nProve a result similar to `All-++-\u2194`, but with `Any` in place of `All`, and a suitable\nreplacement for `_\u00d7_`. As a consequence, demonstrate an equivalence relating\n`_\u2208_` and `_++_`.\n\n#### Exercise `All-++-\u2243` (stretch)\n\nShow that the equivalence `All-++-\u21d4` can be extended to an isomorphism.\n\n#### Exercise `\u00acAny\u2243All\u00ac` (stretch)\n\nFirst generalise composition to arbitrary levels, using\n[universe polymorphism][plfa.Equality#unipoly]:\n\\begin{code}\n_\u2218\u2032_ : \u2200 {\u2113\u2081 \u2113\u2082 \u2113\u2083 : Level} {A : Set \u2113\u2081} {B : Set \u2113\u2082} {C : Set \u2113\u2083}\n \u2192 (B \u2192 C) \u2192 (A \u2192 B) \u2192 A \u2192 C\n(g \u2218\u2032 f) x = g (f x)\n\\end{code}\n\nShow that `Any` and `All` satisfy a version of De Morgan's Law:\n\\begin{code}\npostulate\n \u00acAny\u2243All\u00ac : \u2200 {A : Set} (P : A \u2192 Set) (xs : List A)\n \u2192 (\u00ac_ \u2218\u2032 Any P) xs \u2243 All (\u00ac_ \u2218\u2032 P) xs\n\\end{code}\n\nDo we also have the following?\n\\begin{code}\npostulate\n \u00acAll\u2243Any\u00ac : \u2200 {A : Set} (P : A \u2192 Set) (xs : List A)\n \u2192 (\u00ac_ \u2218\u2032 All P) xs \u2243 Any (\u00ac_ \u2218\u2032 P) xs\n\\end{code}\nIf so, prove; if not, explain why.\n\n\n## Decidability of All\n\nIf we consider a predicate as a function that yields a boolean,\nit is easy to define an analogue of `All`, which returns true if\na given predicate returns true for every element of a list:\n\\begin{code}\nall : \u2200 {A : Set} \u2192 (A \u2192 Bool) \u2192 List A \u2192 Bool\nall p = foldr _\u2227_ true \u2218 map p\n\\end{code}\nThe function can be written in a particularly compact style by\nusing the higher-order functions `map` and `foldr`.\n\nAs one would hope, if we replace booleans by decidables there is again\nan analogue of `All`. First, return to the notion of a predicate `P` as\na function of type `A \u2192 Set`, taking a value `x` of type `A` into evidence\n`P x` that a property holds for `x`. Say that a predicate `P` is _decidable_\nif we have a function that for a given `x` can decide `P x`:\n\\begin{code}\nDecidable : \u2200 {A : Set} \u2192 (A \u2192 Set) \u2192 Set\nDecidable {A} P = \u2200 (x : A) \u2192 Dec (P x)\n\\end{code}\nThen if predicate `P` is decidable, it is also decidable whether every\nelement of a list satisfies the predicate:\n\\begin{code}\nAll? : \u2200 {A : Set} {P : A \u2192 Set} \u2192 Decidable P \u2192 Decidable (All P)\nAll? P? [] = yes []\nAll? P? (x \u2237 xs) with P? x | All? P? xs\n... | yes Px | yes Pxs = yes (Px \u2237 Pxs)\n... | no \u00acPx | _ = no \u03bb{ (Px \u2237 Pxs) \u2192 \u00acPx Px }\n... | _ | no \u00acPxs = no \u03bb{ (Px \u2237 Pxs) \u2192 \u00acPxs Pxs }\n\\end{code}\nIf the list is empty, then trivially `P` holds for every element of\nthe list. Otherwise, the structure of the proof is similar to that\nshowing that the conjuction of two decidable propositions is itself\ndecidable, using `_\u2237_` rather than `\u27e8_,_\u27e9` to combine the evidence for\nthe head and tail of the list.\n\n\n#### Exercise `any?` (stretch)\n\nJust as `All` has analogues `all` and `all?` which determine whether a\npredicate holds for every element of a list, so does `Any` have\nanalogues `any` and `any?` which determine whether a predicates holds\nfor some element of a list. Give their definitions.\n\n\n#### Exercise `filter?` (stretch)\n\nDefine the following variant of the traditional `filter` function on lists,\nwhich given a list and a decidable predicate returns all elements of the\nlist satisfying the predicate:\n\\begin{code}\npostulate\n filter? : \u2200 {A : Set} {P : A \u2192 Set}\n \u2192 (P? : Decidable P) \u2192 List A \u2192 \u2203[ ys ]( All P ys )\n\\end{code}\n\n\n## Standard Library\n\nDefinitions similar to those in this chapter can be found in the standard library:\n\\begin{code}\nimport Data.List using (List; _++_; length; reverse; map; foldr; downFrom)\nimport Data.List.All using (All; []; _\u2237_)\nimport Data.List.Any using (Any; here; there)\nimport Data.List.Membership.Propositional using (_\u2208_)\nimport Data.List.Properties\n using (reverse-++-commute; map-compose; map-++-commute; foldr-++)\n renaming (mapIsFold to map-is-foldr)\nimport Algebra.Structures using (IsMonoid)\nimport Relation.Unary using (Decidable)\nimport Relation.Binary using (Decidable)\n\\end{code}\nThe standard library version of `IsMonoid` differs from the\none given here, in that it is also parameterised on an equivalence relation.\n\nBoth `Relation.Unary` and `Relation.Binary` define a version of `Decidable`,\none for unary relations (as used in this chapter where `P` ranges over\nunary predicates) and one for binary relations (as used earlier, where `_\u2264_`\nranges over a binary relation).\n\n## Unicode\n\nThis chapter uses the following unicode:\n\n \u2237 U+2237 PROPORTION (\\::)\n \u2297 U+2297 CIRCLED TIMES (\\otimes, \\ox)\n \u2208 U+2208 ELEMENT OF (\\in)\n \u2209 U+2209 NOT AN ELEMENT OF (\\inn, \\notin)\n","avg_line_length":29.3579154376,"max_line_length":92,"alphanum_fraction":0.6201560773} +{"size":6992,"ext":"lagda","lang":"Literate Agda","max_stars_count":1299.0,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\nmodule Scoped.Erasure.RenamingSubstitution where\n\\end{code}\n\n\\begin{code}\nopen import Untyped\nimport Untyped.RenamingSubstitution as U\nopen import Scoped\nopen import Scoped.Erasure\nimport Scoped.RenamingSubstitution as S\n\nopen import Data.Nat\nopen import Data.Fin\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Function\nopen import Data.Vec\nopen import Data.Sum\nopen import Data.Product\nopen import Utils\n\\end{code}\n\n\\begin{code}\nbackVar : \u2200{n}{w : Weird\u2115 n} \u2192 Fin (len w) \u2192 WeirdFin w\nbackVar {w = S w} zero = Z\nbackVar {w = S w} (suc i) = S (backVar i)\nbackVar {w = T w} i = T (backVar i)\n\nbackVar-eraseVar : \u2200{n}{w : Weird\u2115 n}(i : WeirdFin w)\n \u2192 backVar (eraseVar i) \u2261 i\nbackVar-eraseVar Z = refl\nbackVar-eraseVar (S i) = cong S (backVar-eraseVar i)\nbackVar-eraseVar (T i) = cong T (backVar-eraseVar i)\n\neraseVar-backVar : \u2200{n}{w : Weird\u2115 n}(i : Fin (len w))\n \u2192 eraseVar (backVar {n}{w} i) \u2261 i\neraseVar-backVar {w = S w} zero = refl\neraseVar-backVar {w = S w} (suc i) = cong suc (eraseVar-backVar {w = w} i)\neraseVar-backVar {w = T w} i = eraseVar-backVar {w = w} i\n\nerase-Ren : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 S.Ren w w'\n \u2192 U.Ren (len w) (len w')\nerase-Ren \u03c1 i = eraseVar (\u03c1 (backVar i))\n\nlift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (\u03b1 : Fin (len (S w)))\n \u2192 U.lift (erase-Ren \u03c1) \u03b1 \u2261 erase-Ren (S.lift \u03c1) \u03b1\nlift-erase \u03c1 zero = refl\nlift-erase \u03c1 (suc \u03b1) = refl\n\nren-eraseTel\u22c6 : \u2200{m n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1\u22c6 : S.Ren\u22c6 n n')\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (As : Tel\u22c6 n m)\n \u2192 U.renTel (erase-Ren \u03c1) (eraseTel\u22c6 w As) \u2261 eraseTel\u22c6 w' (S.ren\u22c6T \u03c1\u22c6 As)\nren-eraseTel\u22c6 \u03c1\u22c6 \u03c1 [] = refl\nren-eraseTel\u22c6 \u03c1\u22c6 \u03c1 (A \u2237 As) = cong (con unit \u2237_) (ren-eraseTel\u22c6 \u03c1\u22c6 \u03c1 As)\n\nren-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1\u22c6 : S.Ren\u22c6 n n')\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (t : ScopedTm w)\n \u2192 U.ren (erase-Ren \u03c1) (eraseTm t) \u2261 eraseTm (S.ren \u03c1\u22c6 \u03c1 t)\n\nren-eraseTel : \u2200{m n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c1\u22c6 : S.Ren\u22c6 n n')\n \u2192 (\u03c1 : S.Ren w w')\n \u2192 (ts : Vec (ScopedTm w) m)\n \u2192 U.renTel (erase-Ren \u03c1) (eraseTel ts) \u2261 eraseTel (S.renT \u03c1\u22c6 \u03c1 ts)\nren-eraseTel \u03c1\u22c6 \u03c1 [] = refl\nren-eraseTel \u03c1\u22c6 \u03c1 (t \u2237 ts) =\n cong\u2082 _\u2237_ (ren-erase \u03c1\u22c6 \u03c1 t) (ren-eraseTel \u03c1\u22c6 \u03c1 ts)\n\nren-erase \u03c1\u22c6 \u03c1 (` x) =\n cong (` \u2218 eraseVar \u2218 \u03c1) (backVar-eraseVar x) \nren-erase \u03c1\u22c6 \u03c1 (\u039b K t) = cong \u019b (trans\n (sym (U.ren-comp suc (U.lift (erase-Ren \u03c1)) (eraseTm t)))\n (trans (U.ren-comp (erase-Ren (S.\u22c6lift \u03c1)) suc (eraseTm t))\n (cong U.weaken (ren-erase (S.lift\u22c6 \u03c1\u22c6) (S.\u22c6lift \u03c1) t))))\nren-erase \u03c1\u22c6 \u03c1 (t \u00b7\u22c6 A) = cong (_\u00b7 plc_dummy) (ren-erase \u03c1\u22c6 \u03c1 t)\nren-erase \u03c1\u22c6 \u03c1 (\u019b A t) = cong\n \u019b\n (trans (U.ren-cong (lift-erase \u03c1) (eraseTm t)) (ren-erase \u03c1\u22c6 (S.lift \u03c1) t))\nren-erase \u03c1\u22c6 \u03c1 (t \u00b7 u) =\n cong\u2082 _\u00b7_ (ren-erase \u03c1\u22c6 \u03c1 t) (ren-erase \u03c1\u22c6 \u03c1 u)\nren-erase \u03c1\u22c6 \u03c1 (con x) = refl\nren-erase \u03c1\u22c6 \u03c1 (error x) = refl\nren-erase \u03c1\u22c6 \u03c1 (builtin bn (inj\u2081 (p , refl)) As ts) = cong (builtin bn _) (ren-eraseTel \u03c1\u22c6 \u03c1 ts)\nren-erase {w = w} \u03c1\u22c6 \u03c1 (builtin bn (inj\u2082 (refl , snd)) As ts) = cong\n (builtin bn _)\n (trans\n (U.renTel++ (erase-Ren \u03c1) (eraseTel\u22c6 w As) (eraseTel ts))\n (cong\u2082 _++_ (ren-eraseTel\u22c6 \u03c1\u22c6 \u03c1 As) (ren-eraseTel \u03c1\u22c6 \u03c1 ts)))\nren-erase \u03c1\u22c6 \u03c1 (wrap pat ar t) = ren-erase \u03c1\u22c6 \u03c1 t\nren-erase \u03c1\u22c6 \u03c1 (unwrap t) = ren-erase \u03c1\u22c6 \u03c1 t\n\n--\n\nerase-Sub : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 S.Sub w w' \u2192 U.Sub (len w) (len w')\nerase-Sub \u03c3 i = eraseTm (\u03c3 (backVar i))\n\nslift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (\u03b1 : Fin (len (S w)))\n \u2192 U.lifts (erase-Sub \u03c3) \u03b1 \u2261 erase-Sub (S.slift \u03c3) \u03b1\nslift-erase \u03c3 zero = refl\nslift-erase {w' = w'} \u03c3 (suc \u03b1) = trans\n (U.ren-cong (cong suc \u2218 sym \u2218 eraseVar-backVar {w = w'})\n (eraseTm (\u03c3 (backVar \u03b1))))\n (ren-erase id S (\u03c3 (backVar \u03b1)))\n\n\u22c6slift-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (\u03b1 : Fin (len w))\n \u2192 erase-Sub \u03c3 \u03b1 \u2261 erase-Sub (S.\u22c6slift \u03c3) \u03b1\n\u22c6slift-erase {w' = w'} \u03c3 \u03b1 = trans\n (trans (U.ren-id (eraseTm (\u03c3 (backVar \u03b1))))\n (U.ren-cong (sym \u2218 eraseVar-backVar {w = w'})\n (eraseTm (\u03c3 (backVar \u03b1)))))\n (ren-erase suc T (\u03c3 (backVar \u03b1)))\n\nsub-eraseTel\u22c6 : \u2200{m n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3\u22c6 : S.Sub\u22c6 n n')\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (As : Tel\u22c6 n m)\n \u2192 U.subTel (erase-Sub \u03c3) (eraseTel\u22c6 w As) \u2261 eraseTel\u22c6 w' (S.sub\u22c6T \u03c3\u22c6 As)\nsub-eraseTel\u22c6 \u03c3\u22c6 \u03c3 [] = refl\nsub-eraseTel\u22c6 \u03c3\u22c6 \u03c3 (A \u2237 As) = cong (con unit \u2237_) (sub-eraseTel\u22c6 \u03c3\u22c6 \u03c3 As)\n\nsub-erase : \u2200{n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3\u22c6 : S.Sub\u22c6 n n')\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (t : ScopedTm w)\n \u2192 U.sub (erase-Sub \u03c3) (eraseTm t) \u2261 eraseTm (S.sub \u03c3\u22c6 \u03c3 t)\n\nsubTel-erase : \u2200{m n n'}{w : Weird\u2115 n}{w' : Weird\u2115 n'}\n \u2192 (\u03c3\u22c6 : S.Sub\u22c6 n n')\n \u2192 (\u03c3 : S.Sub w w')\n \u2192 (ts : Vec (ScopedTm w) m)\n \u2192 U.subTel (erase-Sub \u03c3) (eraseTel ts) \u2261 eraseTel (S.subT \u03c3\u22c6 \u03c3 ts)\nsubTel-erase \u03c3\u22c6 \u03c3 [] = refl\nsubTel-erase \u03c3\u22c6 \u03c3 (t \u2237 ts) =\n cong\u2082 _\u2237_ (sub-erase \u03c3\u22c6 \u03c3 t) (subTel-erase \u03c3\u22c6 \u03c3 ts)\n\nsub-erase \u03c3\u22c6 \u03c3 (` x) = cong (eraseTm \u2218 \u03c3) (backVar-eraseVar x)\nsub-erase \u03c3\u22c6 \u03c3 (\u039b K t) = cong \u019b (trans\n (sym (U.sub-ren suc (U.lifts (erase-Sub \u03c3)) (eraseTm t)))\n (trans (trans (U.sub-cong (cong U.weaken \u2218 \u22c6slift-erase \u03c3 ) (eraseTm t)) (U.ren-sub (erase-Sub (S.\u22c6slift \u03c3)) suc (eraseTm t)))\n (cong U.weaken (sub-erase (S.slift\u22c6 \u03c3\u22c6) (S.\u22c6slift \u03c3) t))))\nsub-erase \u03c3\u22c6 \u03c3 (t \u00b7\u22c6 A) = cong (_\u00b7 plc_dummy) (sub-erase \u03c3\u22c6 \u03c3 t)\nsub-erase \u03c3\u22c6 \u03c3 (\u019b A t) = cong\n \u019b\n (trans (U.sub-cong (slift-erase \u03c3) (eraseTm t))\n (sub-erase \u03c3\u22c6 (S.slift \u03c3) t))\nsub-erase \u03c3\u22c6 \u03c3 (t \u00b7 u) =\n cong\u2082 _\u00b7_ (sub-erase \u03c3\u22c6 \u03c3 t) (sub-erase \u03c3\u22c6 \u03c3 u)\nsub-erase \u03c3\u22c6 \u03c3 (con c) = refl\nsub-erase \u03c3\u22c6 \u03c3 (error A) = refl\nsub-erase \u03c3\u22c6 \u03c3 (builtin bn (inj\u2081 (p , refl)) As ts) = cong\n (builtin bn _)\n (subTel-erase \u03c3\u22c6 \u03c3 ts)\nsub-erase {w = w} \u03c3\u22c6 \u03c3 (builtin b (inj\u2082 (refl , p)) As ts) = cong\n (builtin b _)\n (trans\n (U.subTel++ (erase-Sub \u03c3) (eraseTel\u22c6 w As) (eraseTel ts))\n (cong\u2082 _++_ (sub-eraseTel\u22c6 \u03c3\u22c6 \u03c3 As) (subTel-erase \u03c3\u22c6 \u03c3 ts)))\nsub-erase \u03c3\u22c6 \u03c3 (wrap pat arg t) = sub-erase \u03c3\u22c6 \u03c3 t\nsub-erase \u03c3\u22c6 \u03c3 (unwrap t) = sub-erase \u03c3\u22c6 \u03c3 t\n\nerase-extend : \u2200{n}{w : Weird\u2115 n}\n \u2192 (u : ScopedTm w)\n \u2192 (\u03b1 : Fin (suc (len w)))\n \u2192 erase-Sub (S.ext ` u) \u03b1 \u2261 U.extend ` (eraseTm u) \u03b1 \nerase-extend u zero = refl\nerase-extend {w = w} u (suc \u03b1) = cong ` (eraseVar-backVar {w = w} \u03b1)\n\nlem[] : \u2200{n}{w : Weird\u2115 n}(t : ScopedTm (S w))(u : ScopedTm w) \u2192\n eraseTm (t S.[ u ]) \u2261 eraseTm t U.[ eraseTm u ]\nlem[] t u = trans\n (sym (sub-erase ` (S.ext ` u) t))\n (U.sub-cong (erase-extend u) (eraseTm t))\n\nlem[]\u22c6 : \u2200{n}{w : Weird\u2115 n}(t : ScopedTm (T w))(A : ScopedTy n) \u2192\n eraseTm (t S.[ A ]\u22c6) \u2261 eraseTm t\nlem[]\u22c6 {w = w} t A = trans\n (sym (sub-erase (S.ext\u22c6 ` A) (S.\u22c6ext `) t))\n (trans (U.sub-cong (cong ` \u2218 eraseVar-backVar {w = w})\n (eraseTm t))\n (sym (U.sub-id (eraseTm t))))\n\\end{code}\n","avg_line_length":36.0412371134,"max_line_length":128,"alphanum_fraction":0.5536327231} +{"size":46690,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Scoped.Extrication.Reduction where\n\nopen import Type\nopen import Type.BetaNormal\nopen import Algorithmic as A\nopen import Scoped\nopen import Scoped.Extrication\nopen import Scoped.Extrication.RenamingSubstitution\nopen import Algorithmic.Reduction as AR\nopen import Algorithmic.Evaluation as AE\nopen import Scoped.Reduction as SR\n\nopen import Relation.Binary.PropositionalEquality as Eq\nopen import Function\nopen import Data.Sum\nopen import Utils\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.List\nopen import Data.Unit\nopen import Data.Nat\nopen import Data.Integer\nopen import Data.Bool\nopen import Relation.Nullary\n\nopen import Builtin\nopen import Builtin.Constant.Term\nopen import Builtin.Constant.Type\n\nopen import Relation.Nullary\n\n\nopen import Builtin.Signature\n Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con booleanNf \n\nextricate\u2014\u2192 : \u2200{\u03a6 \u0393 K}{A : \u03a6 \u22a2Nf\u22c6 K}{t t' : \u0393 \u22a2 A}\n \u2192 t AR.\u2014\u2192 t' \u2192 extricate t SR.\u2014\u2192 extricate t'\n\nextricateVal : \u2200{\u03a6 \u0393 K}{A : \u03a6 \u22a2Nf\u22c6 K}{t : \u0393 \u22a2 A}\n \u2192 AR.Value t \u2192 SR.Value (extricate t)\nextricateE : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}{t : \u0393 \u22a2 A}\n \u2192 AR.Error t \u2192 SR.Error (extricate t)\nextricateE E-error = E-error _\nextricateE (E-\u00b7\u2081 p) = E-\u00b7\u2081 (extricateE p)\nextricateE (E-\u00b7\u2082 p) = E-\u00b7\u2082 (extricateE p)\nextricateE (E-\u00b7\u22c6 p) = E-\u00b7\u22c6 (extricateE p)\nextricateE (E-unwrap p) = E-unwrap (extricateE p)\nextricateE (E-\u039b x) = {!!}\nextricateE (E-wrap x) = {!!}\nextricateE (E-builtin bn \u03c3 tel Bs Ds telB vtel e p telD) =\n E-builtin (extricateE e)\n\nextricateVal V-\u019b = V-\u019b _ _ _\nextricateVal (V-\u039b p) = {!!} -- SR.V-\u039b _ _ _\nextricateVal (V-wrap p) = {!!} -- V-wrap _ _ _\nextricateVal (V-con cn) = V-con (extricateC cn)\n\nextricateVTel : \u2200 {\u03a6} \u0393 \u0394 (\u03c3 : \u2200 {K} \u2192 \u0394 \u220b\u22c6 K \u2192 \u03a6 \u22a2Nf\u22c6 K)\n (As : List (\u0394 \u22a2Nf\u22c6 *))\n (tel : A.Tel \u0393 \u0394 \u03c3 As)\n \u2192 AR.VTel \u0393 \u0394 \u03c3 As tel\n \u2192 SR.VTel (len \u0393) (extricateTel \u03c3 As tel)\n\nextricateVTel \u0393 \u0394 \u03c3 [] _ _ = tt\nextricateVTel \u0393 \u0394 \u03c3 (A \u2237 As) (t \u03a3., tel) (v \u03a3., vtel) =\n extricateVal v ,, extricateVTel \u0393 \u0394 \u03c3 As tel vtel\n\nextricate-decIf : \u2200{X \u03a6}{\u0393 : A.Ctx \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}(p : Dec X)(t f : \u0393 A.\u22a2 A) \u2192 decIf p (extricate t) (extricate f) \u2261 extricate (decIf p t f)\nextricate-decIf (yes p) t f = refl\nextricate-decIf (no \u00acp) t f = refl\n\nextricate-if : \u2200{\u03a6}{\u0393 : A.Ctx \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}(b : Bool)(t f : \u0393 A.\u22a2 A) \u2192 (if b then extricate t else extricate f) \u2261 extricate (if b then t else f)\nextricate-if Bool.true t f = refl\nextricate-if Bool.false t f = refl\n\nextricate-VERIFYSIG : \u2200{\u03a6}{\u0393 : Ctx \u03a6}(p : Maybe Bool) \u2192 SR.VERIFYSIG p \u2261 extricate {\u03a6}{\u0393} (AR.VERIFYSIG p)\nextricate-VERIFYSIG (just Bool.false) = refl\nextricate-VERIFYSIG (just Bool.true) = refl\nextricate-VERIFYSIG nothing = refl\n\nextricate\u2014\u2192 (\u03be-\u00b7\u2081 p) = \u03be-\u00b7\u2081 (extricate\u2014\u2192 p)\nextricate\u2014\u2192 (\u03be-\u00b7\u2082 p q) = \u03be-\u00b7\u2082 (extricateVal p) (extricate\u2014\u2192 q)\nextricate\u2014\u2192 (\u03be-\u00b7\u22c6 p) = \u03be-\u00b7\u22c6 (extricate\u2014\u2192 p)\nextricate\u2014\u2192 (\u03be-\u039b x) = {!!}\nextricate\u2014\u2192 (\u03be-wrap x) = {!!}\nextricate\u2014\u2192 (\u03b2-\u019b {A = A}{N = N}{W = W}) = Eq.subst\n (\u019b _ (extricateNf\u22c6 A) (extricate N) \u00b7 extricate W SR.\u2014\u2192_)\n (lem[] N W)\n SR.\u03b2-\u019b\nextricate\u2014\u2192 (\u03b2-\u039b {K = K}{N = N}{W = W}) = Eq.subst\n (\u039b _ (extricateK K) (extricate N) \u00b7\u22c6 extricateNf\u22c6 W SR.\u2014\u2192_)\n (lem[]\u22c6 N W)\n SR.\u03b2-\u039b\nextricate\u2014\u2192 \u03b2-wrap1 = \u03b2-wrap\nextricate\u2014\u2192 (\u03be-unwrap1 p) = \u03be-unwrap (extricate\u2014\u2192 p)\nextricate\u2014\u2192 (\u03b2-builtin addInteger \u03c3 _ vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) =\n \u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG addInteger))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin subtractInteger \u03c3 _ vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) =\n \u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG subtractInteger))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin multiplyInteger \u03c3 _ vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) =\n \u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG multiplyInteger))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin divideInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin divideInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (div i j))))\n (SR.\u03b2-builtin {b = divideInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) _ vtel)) \nextricate\u2014\u2192 (\u03b2-builtin quotientInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin quotientInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (quot i j))))\n (SR.\u03b2-builtin {b = quotientInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) _ vtel)) \nextricate\u2014\u2192 (\u03b2-builtin remainderInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin remainderInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (rem i j))))\n (SR.\u03b2-builtin {b = remainderInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) _ vtel)) \nextricate\u2014\u2192 (\u03b2-builtin modInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin modInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (mod i j))))\n (SR.\u03b2-builtin {b = modInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) _ vtel))\nextricate\u2014\u2192 (\u03b2-builtin lessThanInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin lessThanInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (i Builtin.Constant.Type.? j) A.true A.false)\n (SR.\u03b2-builtin {b = greaterThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanInteger))) _ vtel))\nextricate\u2014\u2192 (\u03b2-builtin greaterThanEqualsInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin greaterThanEqualsInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (i Builtin.Constant.Type.\u2265? j) A.true A.false)\n (SR.\u03b2-builtin {b = greaterThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) _ vtel))\nextricate\u2014\u2192 (\u03b2-builtin equalsInteger \u03c3 tel vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = Eq.subst\n (builtin equalsInteger [] (con (integer i) \u2237 con (integer j) \u2237 []) SR.\u2014\u2192_)\n (extricate-decIf (i Data.Integer.\u225f j) A.true A.false)\n (SR.\u03b2-builtin {b = equalsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) _ vtel))\nextricate\u2014\u2192 (\u03b2-builtin concatenate \u03c3 tel vtel@(V-con (bytestring b) ,, V-con (bytestring b') ,, tt)) =\n SR.\u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG concatenate))) _ vtel) \nextricate\u2014\u2192 (\u03b2-builtin takeByteString \u03c3 tel vtel@(V-con (integer i) ,, V-con (bytestring b) ,, tt)) =\n SR.\u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG takeByteString))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin dropByteString \u03c3 tel vtel@(V-con (integer i) ,, V-con (bytestring b) ,, tt)) =\n SR.\u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG dropByteString))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin sha2-256 \u03c3 tel vtel@(V-con (bytestring b) ,, tt)) = SR.\u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG sha2-256))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin sha3-256 \u03c3 tel vtel@(V-con (bytestring b) ,, tt)) = SR.\u03b2-builtin (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG sha3-256))) _ vtel)\nextricate\u2014\u2192 (\u03b2-builtin verifySignature \u03c3 tel vtel@(V-con (bytestring k) ,, V-con (bytestring d) ,, V-con (bytestring c) ,, tt)) = Eq.subst\n (builtin verifySignature [] (con (bytestring k) \u2237 con (bytestring d) \u2237 con (bytestring c) \u2237 []) SR.\u2014\u2192_)\n (extricate-VERIFYSIG (verifySig k d c))\n (SR.\u03b2-builtin {b = verifySignature}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) _ vtel))\nextricate\u2014\u2192 (\u03b2-builtin equalsByteString \u03c3 tel vtel@(V-con (bytestring b) ,, V-con (bytestring b') ,, tt)) = Eq.subst\n (builtin equalsByteString [] (con (bytestring b) \u2237 con (bytestring b') \u2237 []) SR.\u2014\u2192_)\n (extricate-if (equals b b') A.true A.false)\n (SR.\u03b2-builtin {b = equalsByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) _ vtel))\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin addInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = addInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG addInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin addInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = addInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG addInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin addInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin addInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin subtractInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = subtractInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG subtractInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin subtractInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = subtractInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG subtractInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin subtractInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin subtractInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin multiplyInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = multiplyInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG multiplyInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin multiplyInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = multiplyInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG multiplyInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin multiplyInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin multiplyInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin divideInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = divideInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin divideInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = divideInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin divideInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin divideInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin quotientInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = quotientInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin quotientInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = quotientInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin quotientInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin quotientInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin remainderInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = remainderInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin remainderInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = remainderInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin remainderInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin remainderInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin modInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = modInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin modInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = modInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin modInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin modInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin lessThanInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = lessThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin lessThanInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = lessThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) _\nextricate\u2014\u2192 (\u03be-builtin lessThanInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin lessThanInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin lessThanEqualsInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = lessThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanEqualsInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin lessThanEqualsInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = lessThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanEqualsInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin lessThanEqualsInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin lessThanEqualsInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin greaterThanInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = greaterThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin greaterThanInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = greaterThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin greaterThanInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin greaterThanInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin greaterThanEqualsInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = greaterThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin greaterThanEqualsInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = greaterThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin greaterThanEqualsInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin greaterThanEqualsInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin equalsInteger \u03c3 tel [] .(con integer \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = equalsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con integer \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin equalsInteger \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = equalsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin equalsInteger \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin equalsInteger \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin concatenate \u03c3 tel [] .(con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = concatenate}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG concatenate))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin concatenate \u03c3 tel (.(con bytestring) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = concatenate}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG concatenate))) tel}(extricateVTel _ _ \u03c3 (con bytestring \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin concatenate \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin concatenate \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin takeByteString \u03c3 tel [] .(con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = takeByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG takeByteString))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin takeByteString \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = takeByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG takeByteString))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin takeByteString \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin takeByteString \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin dropByteString \u03c3 tel [] .(con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = dropByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG dropByteString))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin dropByteString \u03c3 tel (.(con integer) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = dropByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG dropByteString))) tel}(extricateVTel _ _ \u03c3 (con integer \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin dropByteString \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin dropByteString \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin sha2-256 \u03c3 tel [] .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = sha2-256}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG sha2-256))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin sha2-256 \u03c3 tel (B \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin sha2-256 \u03c3 tel (B \u2237 B' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin sha3-256 \u03c3 tel [] .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = sha3-256}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG sha3-256))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin sha3-256 \u03c3 tel (B \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin sha3-256 \u03c3 tel (B \u2237 B' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin verifySignature \u03c3 tel [] .(con bytestring \u2237 con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = verifySignature}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin verifySignature \u03c3 tel (.(con bytestring) \u2237 []) .(con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = verifySignature}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) tel}(extricateVTel _ _ \u03c3 (con bytestring \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin verifySignature \u03c3 tel (.(con bytestring) \u2237 .(con bytestring) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = verifySignature}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) tel}(extricateVTel _ _ \u03c3 (con bytestring \u2237 con bytestring \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin verifySignature \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin verifySignature \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 B''' \u2237 Bs) Ds telB telD vtel p ())\n\nextricate\u2014\u2192 {\u0393 = \u0393} (\u03be-builtin equalsByteString \u03c3 tel [] .(con bytestring \u2237 []) telB telD vtel p refl) =\n SR.\u03be-builtin {b = equalsByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) tel}(extricateVTel \u0393 _ \u03c3 [] _ tt) (extricate\u2014\u2192 p) (extricateTel \u03c3 (con bytestring \u2237 []) _)\nextricate\u2014\u2192 (\u03be-builtin equalsByteString \u03c3 tel (.(con bytestring) \u2237 []) .[] telB telD vtel p refl) =\n SR.\u03be-builtin {b = equalsByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) tel}(extricateVTel _ _ \u03c3 (con bytestring \u2237 []) _ vtel) (extricate\u2014\u2192 p) (extricateTel \u03c3 [] tt)\nextricate\u2014\u2192 (\u03be-builtin equalsByteString \u03c3 tel (B \u2237 B' \u2237 []) Ds telB telD vtel p ())\nextricate\u2014\u2192 (\u03be-builtin equalsByteString \u03c3 tel (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB telD vtel p ())\n\n-- extrication for a sequence of steps\n\nextricate\u2014\u2192\u22c6 : \u2200{\u03a6 \u0393 K}{A : \u03a6 \u22a2Nf\u22c6 K}{t t' : \u0393 \u22a2 A}\n \u2192 t AR.\u2014\u21a0 t' \u2192 extricate t SR.\u2014\u2192\u22c6 extricate t'\nextricate\u2014\u2192\u22c6 refl\u2014\u21a0 = refl\nextricate\u2014\u2192\u22c6 (trans\u2014\u21a0 r p) = _\u2014\u2192\u22c6_.trans (extricate\u2014\u2192 r) (extricate\u2014\u2192\u22c6 p)\n\nlem-step : \u2200{t t' t'' : ScopedTm Z}(p : t SR.\u2014\u2192 t')(r : t' \u2261 t'')\n \u2192 SR.Progress.step (Eq.subst (SR._\u2014\u2192_ t) r p) \u2261 step p\nlem-step p refl = refl\n\n-- given the result of intrinsic progress this gives rise to extrinsic progress\nextricateProgress : \u2200{A}{t : \u2205 \u22a2 A} \u2192 AR.Progress t\n \u2192 SR.Progress (extricate t)\nextricateProgress (step p) = step (extricate\u2014\u2192 p)\nextricateProgress (done v) = done (extricateVal v)\nextricateProgress (error e) = error (extricateE e)\nextricateProgress (neutral p) = {!!}\n\n\nextricateProgressTel : \u2200{\u03a6 \u03a8}{\u0393 : Ctx \u03a6}\n {\u03c3 : \u2200{K} \u2192 \u03a8 \u220b\u22c6 K \u2192 \u03a6 \u22a2Nf\u22c6 K}\n {As : List (\u03a8 \u22a2Nf\u22c6 *)} \u2192\n (tel : A.Tel \u0393 \u03a8 \u03c3 As)\n \u2192 AR.TelProgress tel\n \u2192 SR.TelProgress (extricateTel \u03c3 As tel)\nextricateProgressTel tel (done vtel) =\n done (extricateTel _ _ tel) (extricateVTel _ _ _ _ tel vtel)\nextricateProgressTel tel (step Bs Ds telB vtelB p q telD) =\n step\n (extricateTel _ _ tel)\n (extricateTel _ _ telB)\n (extricateVTel _ _ _ _ telB vtelB)\n (extricate\u2014\u2192 p)\n (extricateTel _ _ telD)\nextricateProgressTel tel (error Bs Ds telB vtelB e q telD) =\n error\n (extricateTel _ _ tel)\n (extricateTel _ _ telB)\n (extricateVTel _ _ _ _ telB vtelB)\n (extricateE e)\n (extricateTel _ _ telD)\nextricateProgressTel tel (neutral Bs Ds telB vtelB e q telD) = {!!}\n\n-- proofs below\n\nextricate-progress-\u00b7 : \u2200{A B}{t : \u2205 \u22a2 A \u21d2 B}(p : AR.Progress t) \u2192 (u : \u2205 \u22a2 A)\n \u2192 extricateProgress (AR.progress-\u00b7 p u) \u2261 SR.progress\u00b7 (extricateProgress p) (extricate u)\nextricate-progress-\u00b7 (step p) u = refl\nextricate-progress-\u00b7 (done (V-\u019b {A = A}{x = x}{N = N})) u = lem-step \u03b2-\u019b (lem[] N u)\nextricate-progress-\u00b7 (error e) u = refl\nextricate-progress-\u00b7 (neutral p) u = {!!}\n\nextricate-progress-\u00b7\u22c6 : \u2200{K x B}{t : \u2205 \u22a2 \u03a0 x B}(p : AR.Progress t) \u2192 (A : \u2205 \u22a2Nf\u22c6 K)\n \u2192 extricateProgress (AR.progress-\u00b7\u22c6 p A) \u2261 SR.progress\u00b7\u22c6 (extricateProgress p) (extricateNf\u22c6 A)\nextricate-progress-\u00b7\u22c6 (step p) A = refl\nextricate-progress-\u00b7\u22c6 (done (V-\u039b {N = N} p)) A = {!!} -- lem-step \u03b2-\u039b (lem[]\u22c6 N A)\nextricate-progress-\u00b7\u22c6 (error e) A = refl\nextricate-progress-\u00b7\u22c6 (neutral p) A = {!!}\n\nextricate-progress-unwrap : \u2200{K}{pat}{arg : \u2205 \u22a2Nf\u22c6 K}{t : \u2205 \u22a2 ne ((\u03bc1 \u00b7 pat) \u00b7 arg)}(p : AR.Progress t)\n \u2192 extricateProgress (AR.progress-unwrap p) \u2261 SR.progress-unwrap (extricateProgress p)\nextricate-progress-unwrap (step p) = refl\nextricate-progress-unwrap (done V-wrap1) = {!!} -- refl\nextricate-progress-unwrap (error e) = refl\nextricate-progress-unwrap (neutral p) = {!!}\n\nextricate-progress-builtin : \u2200 bn\n (\u03c3 : \u2200{J} \u2192 proj\u2081 (SIG bn) \u220b\u22c6 J \u2192 \u2205 \u22a2Nf\u22c6 J)\n (tel : A.Tel \u2205 (proj\u2081 (SIG bn)) \u03c3 (proj\u2081 (proj\u2082 (SIG bn))))\n (telp : AR.TelProgress tel)\n \u2192 extricateProgress (AR.progress-builtin bn \u03c3 tel telp)\n \u2261\n SR.progress-builtin\n bn\n (extricateSub \u03c3)\n (extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG bn))) tel)\n (extricateProgressTel tel telp)\nextricate-progress-builtin addInteger \u03c3 _ (done (V-con (integer i) ,, V-con (integer j) ,, tt)) = refl\nextricate-progress-builtin subtractInteger \u03c3 tel (done (V-con (integer i) ,, V-con (integer j) ,, tt)) = refl\nextricate-progress-builtin multiplyInteger \u03c3 tel (done (V-con (integer i) ,, V-con (integer j) ,, tt)) = refl\nextricate-progress-builtin divideInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = divideInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG divideInteger))) _ vtel)) \n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (div i j))))\nextricate-progress-builtin quotientInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = quotientInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG quotientInteger))) _ vtel)) \n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (quot i j))))\nextricate-progress-builtin remainderInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = remainderInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG remainderInteger))) _ vtel)) \n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (rem i j))))\nextricate-progress-builtin modInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = modInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG modInteger))) _ vtel)) \n (extricate-decIf (\u2223 j \u2223 Data.Nat.\u225f 0) (error (con integer)) (con (integer (mod i j))))\nextricate-progress-builtin lessThanInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = lessThanInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG lessThanInteger))) _ vtel)) \n (extricate-decIf (i Builtin.Constant.Type.? j) A.true A.false)\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (done vtel@ (V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = greaterThanEqualsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG greaterThanEqualsInteger))) _ vtel)) \n (extricate-decIf (i Builtin.Constant.Type.\u2265? j) A.true A.false)\nextricate-progress-builtin equalsInteger \u03c3 tel (done vtel@(V-con (integer i) ,, V-con (integer j) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = equalsInteger}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG equalsInteger))) _ vtel)) \n (extricate-decIf (i Data.Integer.\u225f j) A.true A.false)\nextricate-progress-builtin concatenate \u03c3 tel (done (V-con (bytestring b) ,, V-con (bytestring b') ,, tt)) = refl\nextricate-progress-builtin takeByteString \u03c3 tel (done (V-con (integer i) ,, V-con (bytestring b) ,, tt)) = refl\nextricate-progress-builtin dropByteString \u03c3 tel (done (V-con (integer i) ,, V-con (bytestring b) ,, tt)) = refl\nextricate-progress-builtin sha2-256 \u03c3 tel (done (V-con (bytestring b) ,, tt)) = refl\nextricate-progress-builtin sha3-256 \u03c3 tel (done (V-con (bytestring b) ,, tt)) = refl\nextricate-progress-builtin verifySignature \u03c3 tel (done vtel@(V-con (bytestring k) ,, V-con (bytestring d) ,, V-con (bytestring c) ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = verifySignature}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG verifySignature))) _ vtel)) \n (extricate-VERIFYSIG (verifySig k d c))\nextricate-progress-builtin equalsByteString \u03c3 tel (done vtel@(V-con (bytestring b) ,, V-con (bytestring b') ,, tt)) = lem-step\n (SR.\u03b2-builtin {b = equalsByteString}{As = []}{extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) tel} (extricateVTel _ _ \u03c3 (proj\u2081 (proj\u2082 (SIG equalsByteString))) _ vtel)) \n (extricate-if (equals b b') A.true A.false)\nextricate-progress-builtin addInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin addInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin addInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin addInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin subtractInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin subtractInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin subtractInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin subtractInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin multiplyInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin multiplyInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin multiplyInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin multiplyInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin divideInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin divideInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin divideInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin divideInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin quotientInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin quotientInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin quotientInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin quotientInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin remainderInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin remainderInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin remainderInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin remainderInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin modInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin modInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin modInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin modInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin lessThanInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin lessThanInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin lessThanInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin lessThanInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin greaterThanInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin greaterThanInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin greaterThanInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin greaterThanInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin equalsInteger \u03c3 tel (step [] .(con integer \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin equalsInteger \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin equalsInteger \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin equalsInteger \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin concatenate \u03c3 tel (step [] .(con bytestring \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin concatenate \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin concatenate \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin concatenate \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin takeByteString \u03c3 tel (step [] .(con bytestring \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin takeByteString \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin takeByteString \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin takeByteString \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin dropByteString \u03c3 tel (step [] .(con bytestring \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin dropByteString \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin dropByteString \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin dropByteString \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\n\nextricate-progress-builtin sha2-256 \u03c3 tel (step [] .[] telB vtelB p refl telD) = refl\nextricate-progress-builtin sha2-256 \u03c3 tel (step (B \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin sha2-256 \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin sha3-256 \u03c3 tel (step [] .[] telB vtelB p refl telD) = refl\nextricate-progress-builtin sha3-256 \u03c3 tel (step (B \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin sha3-256 \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin verifySignature \u03c3 tel (step [] .(con bytestring \u2237 con bytestring \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin verifySignature \u03c3 tel (step (.(con bytestring) \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin verifySignature \u03c3 tel (step (.(con bytestring) \u2237 .(con bytestring) \u2237 []) .[] telB vtelB p refl telD) = refl\nextricate-progress-builtin verifySignature \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin verifySignature \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 B''' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin equalsByteString \u03c3 tel (step [] .(con bytestring \u2237 []) telB vtelB p refl telD) = refl\nextricate-progress-builtin equalsByteString \u03c3 tel (step (._ \u2237 []) Ds telB vtelB p refl telD) = refl\nextricate-progress-builtin equalsByteString \u03c3 tel (step (B \u2237 B' \u2237 []) Ds telB vtelB p () telD)\nextricate-progress-builtin equalsByteString \u03c3 tel (step (B \u2237 B' \u2237 B'' \u2237 Bs) Ds telB vtelB p () telD)\nextricate-progress-builtin addInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin subtractInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin multiplyInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin divideInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin quotientInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin remainderInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin modInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin lessThanInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin greaterThanInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin equalsInteger \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin concatenate \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin takeByteString \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin dropByteString \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin sha2-256 \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin sha3-256 \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin verifySignature \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin equalsByteString \u03c3 tel (error Bs Ds telB vtelB e q telD) = refl\nextricate-progress-builtin addInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin subtractInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin multiplyInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin divideInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin quotientInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin remainderInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin modInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin lessThanInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin lessThanEqualsInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin greaterThanInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin greaterThanEqualsInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin equalsInteger \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin concatenate \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin takeByteString \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin dropByteString \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin sha2-256 \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin sha3-256 \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin verifySignature \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\nextricate-progress-builtin equalsByteString \u03c3 tel (neutral Bs Ds telB vtelB p q telD) = {!!}\n\nopen import Type.BetaNBE.RenamingSubstitution\n\nextricateProgressTelCons-progressTelCons : \u2200 \u03a6 (A : \u03a6 \u22a2Nf\u22c6 *)(As : List (\u03a6 \u22a2Nf\u22c6 *)) \u2192 \n (\u03c3 : \u2200{J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u2205 \u22a2Nf\u22c6 J)\n (t : \u2205 \u22a2 substNf \u03c3 A)\n (tp : AR.Progress t)\n (tel : A.Tel \u2205 \u03a6 \u03c3 As)\n (telp : AR.TelProgress tel)\n \u2192 extricateProgressTel {As = A \u2237 As} (t ,, tel) (AR.progressTelCons tp telp)\n \u2261\n SR.progressTelCons (extricateProgress tp) (extricateProgressTel tel telp)\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (step p) tel telp = refl\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (neutral x) tel telp = {!!}\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (error e) tel telp = refl\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (done vt) tel (done vtel) = refl\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (done vt) tel (step Bs Ds telB vtelB p q telD) = refl\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (done vt) tel (error Bs Ds telB vtelB e p telD) = refl\nextricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (done x) tel (neutral Bs Ds telB x\u2081 x\u2082 x\u2083 x\u2084) = {!!}\n\n\n\ncong\u2084 : \u2200{A C : Set}{B : A \u2192 Set}{D : C \u2192 Set}{E : A \u2192 C \u2192 Set}(f : \u2200{a} \u2192 B a \u2192 \u2200{c} \u2192 D c \u2192 E a c)\n \u2192 (a : A)\n \u2192 {b b' : B a} \u2192 b \u2261 b'\n \u2192 (c : C)\n \u2192 {d d' : D c} \u2192 d \u2261 d'\n \u2192 f {a} b {c} d \u2261 f {a} b' {c} d'\ncong\u2084 f a refl c refl = refl\n \nextricate-progress : \u2200{A}(t : \u2205 \u22a2 A) \u2192 extricateProgress (AR.progress t) \u2261 SR.progress (extricate t)\n\nextricateTel-progressTel : \u2200 \u03a6 (As : List (\u03a6 \u22a2Nf\u22c6 *)) \u2192 \n (\u03c3 : \u2200{J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u2205 \u22a2Nf\u22c6 J)\n ( tel : A.Tel \u2205 \u03a6 \u03c3 As)\n \u2192 extricateProgressTel tel (AR.progressTel tel) \u2261 SR.progressTel (extricateTel \u03c3 As tel)\nextricateTel-progressTel \u03a6 [] \u03c3 tt = refl\nextricateTel-progressTel \u03a6 (A \u2237 As) \u03c3 (t ,, tel) = Eq.trans\n (extricateProgressTelCons-progressTelCons \u03a6 A As \u03c3 t (AR.progress t) tel (AR.progressTel tel))\n (cong\u2084\n {B = SR.Progress}\n SR.progressTelCons\n (extricate t)\n (extricate-progress t)\n (extricateTel _ _ tel)\n (extricateTel-progressTel \u03a6 As \u03c3 tel))\n\nextricate-progress (\u019b x t) = refl\nextricate-progress (t \u00b7 u) = Eq.trans\n (extricate-progress-\u00b7 (AR.progress t) u)\n (cong (\u03bb p \u2192 SR.progress\u00b7 p (extricate u)) (extricate-progress t))\nextricate-progress (\u039b x t) = {!!} -- refl\nextricate-progress (t \u00b7\u22c6 A) = Eq.trans\n (extricate-progress-\u00b7\u22c6 (AR.progress t) A)\n (cong (\u03bb p \u2192 SR.progress\u00b7\u22c6 p (extricateNf\u22c6 A)) (extricate-progress t))\nextricate-progress (wrap1 pat arg t) = {!!} -- refl\nextricate-progress (unwrap1 t) = Eq.trans\n (extricate-progress-unwrap (AR.progress t))\n (cong SR.progress-unwrap (extricate-progress t))\nextricate-progress (con x) = refl\nextricate-progress (builtin bn \u03c3 tel) = Eq.trans\n (extricate-progress-builtin bn \u03c3 tel (AR.progressTel tel))\n (cong (SR.progress-builtin bn (extricateSub \u03c3) (extricateTel \u03c3 (proj\u2081 (proj\u2082 (SIG bn))) tel))\n (extricateTel-progressTel (proj\u2081 (SIG bn)) (proj\u2081 (proj\u2082 (SIG bn))) \u03c3 tel))\nextricate-progress (error A) = refl\n\n-- we could extricate Finished separately\nextricateSteps : {A : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 \u22a2 A} \u2192 AE.Steps t \u2192\n SR.Steps (extricate t)\nextricateSteps (steps p (done N VN)) =\n _ ,, extricate\u2014\u2192\u22c6 p ,, inj\u2081 (just (extricateVal VN))\nextricateSteps (steps p out-of-gas) = _ ,, extricate\u2014\u2192\u22c6 p ,, (inj\u2081 nothing)\nextricateSteps (steps p (error e)) = _ ,, extricate\u2014\u2192\u22c6 p ,, inj\u2082 (extricateE e)\nextricateSteps (steps x (neutral x\u2081)) = {!!}\n\nextricate-run\u2014\u2192 : \u2200{A}{t t' : \u2205 \u22a2 A}(p : t AR.\u2014\u2192 t')(q : AE.Steps t') \u2192\n extricateSteps (eval\u2014\u2192 p q) \u2261 run\u2014\u2192 (extricate\u2014\u2192 p) (extricateSteps q)\nextricate-run\u2014\u2192 p (steps q (done N VN)) = refl\nextricate-run\u2014\u2192 p (steps q out-of-gas) = refl\nextricate-run\u2014\u2192 p (steps q (error e)) = refl\nextricate-run\u2014\u2192 p (steps q (neutral n)) = {!!}\n\nextricate-run : \u2200{A}(t : \u2205 \u22a2 A) n\n \u2192 extricateSteps (eval (gas n) t) \u2261 SR.run (extricate t) n\n\nextricate-runProg : \u2200{A}{t : \u2205 \u22a2 A}(p : AR.Progress t) n\n \u2192 extricateSteps (evalProg (gas n) p) \u2261 SR.runProg n (extricateProgress p)\n\nextricate-runProg (step p) n = Eq.trans\n (extricate-run\u2014\u2192 p (eval (gas n) _))\n (cong (run\u2014\u2192 (extricate\u2014\u2192 p)) (extricate-run _ n))\nextricate-runProg (done v) n = refl\nextricate-runProg (error e) n = refl\nextricate-runProg (neutral p) n = {!!}\n\nextricate-run t zero = refl\nextricate-run t (\u2115.suc n) = Eq.trans\n (extricate-runProg (AR.progress t) n)\n (cong (runProg n) (extricate-progress t)) \n\\end{code}\n","avg_line_length":75.5501618123,"max_line_length":210,"alphanum_fraction":0.6733347612} +{"size":12751,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\nmodule Algorithmic.Erasure where\n\\end{code}\n\n\\begin{code}\nopen import Algorithmic as A\nopen import Untyped\nopen import Untyped.RenamingSubstitution as U\nopen import Type.BetaNormal\nopen import Type.BetaNBE\nopen import Type.BetaNBE.Completeness\nopen import Type\nopen import Type.BetaNBE.RenamingSubstitution\nopen import Function hiding (_\u220b_)\nopen import Builtin hiding (length)\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con as DC renaming (TermCon to TyTermCon)\nimport Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con as AC renaming (TermCon to TyTermCon)\nimport Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2\u22c6_ ` con as DS\nimport Builtin.Signature Ctx\u22c6 Kind \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con as AS\nopen import Type.RenamingSubstitution as T\nopen import Type.Equality\nopen import Type.BetaNBE.Soundness\nopen import Utils\n\n\nopen import Data.Nat\nopen import Data.Nat.Properties\nopen import Data.Fin using (Fin;zero;suc)\nopen import Data.List using (List;length;[];_\u2237_)\nopen import Data.Vec hiding (length)\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Relation.Binary.PropositionalEquality\n\\end{code}\n\n\\begin{code}\nlen : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u2115\nlen \u2205 = 0\nlen (\u0393 ,\u22c6 K) = len \u0393\nlen (\u0393 , A) = suc (len \u0393)\n\nlen\u22c6 : Ctx\u22c6 \u2192 \u2115\nlen\u22c6 \u2205 = 0\nlen\u22c6 (\u0393 ,\u22c6 K) = suc (len\u22c6 \u0393)\n\nlemma : (b : Builtin) \u2192 len\u22c6 (proj\u2081 (AS.SIG b)) + length (proj\u2081 (proj\u2082 (AS.SIG b))) \u2261 arity b\nlemma addInteger = refl\nlemma subtractInteger = refl\nlemma multiplyInteger = refl\nlemma divideInteger = refl\nlemma quotientInteger = refl\nlemma remainderInteger = refl\nlemma modInteger = refl\nlemma lessThanInteger = refl\nlemma lessThanEqualsInteger = refl\nlemma greaterThanInteger = refl\nlemma greaterThanEqualsInteger = refl\nlemma equalsInteger = refl\nlemma concatenate = refl\nlemma takeByteString = refl\nlemma dropByteString = refl\nlemma sha2-256 = refl\nlemma sha3-256 = refl\nlemma verifySignature = refl\nlemma equalsByteString = refl\nlemma ifThenElse = refl\nlemma charToString = refl\nlemma append = refl\nlemma trace = refl\n\nlemma\u2264 : (b : Builtin)\n \u2192 len\u22c6 (proj\u2081 (AS.SIG b)) + length (proj\u2081 (proj\u2082 (AS.SIG b))) \u2264\u2034 arity b\nlemma\u2264 b rewrite lemma b = \u2264\u2034-refl\n\\end{code}\n\n\\begin{code}\neraseVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u220b A \u2192 Fin (len \u0393)\neraseVar Z = zero\neraseVar (S \u03b1) = suc (eraseVar \u03b1) \neraseVar (T \u03b1) = eraseVar \u03b1\n\neraseTC : \u2200{\u03a6}{\u0393 : Ctx \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 AC.TyTermCon A \u2192 TermCon\neraseTC (AC.integer i) = integer i\neraseTC (AC.bytestring b) = bytestring b\neraseTC (AC.string s) = string s\neraseTC (AC.bool b) = bool b\neraseTC (AC.char c) = char c\neraseTC AC.unit = unit\n\nerase : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 len \u0393 \u22a2\nerase (` \u03b1) = ` (eraseVar \u03b1)\nerase (\u019b t) = \u019b (erase t) \nerase (t \u00b7 u) = erase t \u00b7 erase u\nerase (\u039b t) = \u019b (U.weaken (erase t))\nerase (_\u00b7\u22c6_ t A) = erase t \u00b7 plc_dummy\nerase (wrap A B t) = erase t\nerase (unwrap t) = erase t\nerase {\u0393 = \u0393} (con t) = con (eraseTC {\u0393 = \u0393} t)\nerase (ibuiltin b) = error\nerase (error A) = error\n\\end{code}\n\nPorting this from declarative required basically deleting one line but\nI don't think I can fully exploit this by parameterizing the module as\nI need to pattern match on the term constructors\n\n# Erasing decl\/alg terms agree\n\n\\begin{code}\nopen import Relation.Binary.PropositionalEquality\nimport Declarative as D\nimport Declarative.Erasure as D\nopen import Algorithmic.Completeness\n\nopen import Utils\n\nlenLemma : \u2200 {\u03a6}(\u0393 : D.Ctx \u03a6) \u2192 len (nfCtx \u0393) \u2261 D.len \u0393\nlenLemma D.\u2205 = refl\nlenLemma (\u0393 D.,\u22c6 J) = lenLemma \u0393\nlenLemma (\u0393 D., A) = cong suc (lenLemma \u0393)\n\nlenLemma\u22c6 : \u2200 \u03a6 \u2192 D.len\u22c6 \u03a6 \u2261 len\u22c6 \u03a6\nlenLemma\u22c6 \u2205 = refl\nlenLemma\u22c6 (\u03a6 ,\u22c6 K) = cong suc (lenLemma\u22c6 \u03a6)\n\nnfTypeSIG\u2261\u2081' : (bn : Builtin)\n \u2192 D.len\u22c6 (proj\u2081 (DS.SIG bn)) \u2261 len\u22c6 (proj\u2081 (AS.SIG bn))\nnfTypeSIG\u2261\u2081' b = trans (cong D.len\u22c6 (nfTypeSIG\u2261\u2081 b)) (lenLemma\u22c6 _)\n\n\n-- these lemmas for each clause of eraseVar and erase below could be\n-- avoided by using with but it would involve doing with on a long\n-- string of arguments, both contexts, equality proof above, and\n-- before and after versions of all arguments and all recursive calls\n\nlemzero : \u2200{n n'}(p : suc n \u2261 suc n') \u2192 zero \u2261 subst Fin p zero\nlemzero refl = refl\n\nlemsuc : \u2200{n n'}(p : suc n \u2261 suc n')(q : n \u2261 n')(i : Fin n) \u2192\n suc (subst Fin q i) \u2261 subst Fin p (suc i)\nlemsuc refl refl i = refl\n\nopen import Type.BetaNormal.Equality\nopen import Function\n\nsameTC : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}(tcn : DC.TyTermCon A)\n \u2192 D.eraseTC {\u0393 = \u0393} tcn \u2261 eraseTC {\u0393 = nfCtx \u0393} (nfTypeTC tcn)\nsameTC (DC.integer i) = refl\nsameTC (DC.bytestring b) = refl\nsameTC (DC.string s) = refl\nsameTC (DC.bool b) = refl\nsameTC (DC.char c) = refl\nsameTC DC.unit = refl\n\nlem\u2261Ctx : \u2200{\u03a6}{\u0393 \u0393' : Ctx \u03a6} \u2192 \u0393 \u2261 \u0393' \u2192 len \u0393 \u2261 len \u0393'\nlem\u2261Ctx refl = refl\n\nlem-conv\u220b : \u2200{\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2Nf\u22c6 *}(p : \u0393 \u2261 \u0393')(q : A \u2261 A')(x : \u0393 A.\u220b A)\n \u2192 subst Fin (lem\u2261Ctx p) (eraseVar x) \u2261 eraseVar (conv\u220b p q x)\nlem-conv\u220b refl refl x = refl\n\nsameVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}(x : \u0393 D.\u220b A)\n \u2192 D.eraseVar x \u2261 subst Fin (lenLemma \u0393) (eraseVar (nfTyVar x))\nsameVar {\u0393 = \u0393 D., _} D.Z = lemzero (cong suc (lenLemma \u0393))\nsameVar {\u0393 = \u0393 D., _} (D.S x) = trans\n (cong suc (sameVar x))\n (lemsuc (cong suc (lenLemma \u0393)) (lenLemma \u0393) (eraseVar (nfTyVar x)))\nsameVar {\u0393 = \u0393 D.,\u22c6 _} (D.T {A = A} x) = trans\n (sameVar x)\n (cong (subst Fin (lenLemma \u0393)) (lem-conv\u220b refl (ren-nf S A) (T (nfTyVar x))))\n\nlemVar : \u2200{n n'}(p : n \u2261 n')(i : Fin n) \u2192 ` (subst Fin p i) \u2261 subst _\u22a2 p (` i)\nlemVar refl i = refl\n\nlem\u019b : \u2200{n n'}(p : n \u2261 n')(q : suc n \u2261 suc n')(t : suc n \u22a2)\n \u2192 \u019b (subst _\u22a2 q t) \u2261 subst _\u22a2 p (\u019b t) \nlem\u019b refl refl t = refl\n\nlem\u00b7 : \u2200{n n'}(p : n \u2261 n')(t u : n \u22a2) \u2192 subst _\u22a2 p t \u00b7 subst _\u22a2 p u \u2261 subst _\u22a2 p (t \u00b7 u)\nlem\u00b7 refl t u = refl\n\nlem-weaken : \u2200{n n'}(p : n \u2261 n')(q : suc n \u2261 suc n')(t : n \u22a2)\n \u2192 U.weaken (subst _\u22a2 p t) \u2261 subst _\u22a2 q (U.weaken t)\nlem-weaken refl refl t = refl\n\nlemcon' : \u2200{n n'}(p : n \u2261 n')(tcn : TermCon) \u2192 con tcn \u2261 subst _\u22a2 p (con tcn)\nlemcon' refl tcn = refl\n\nlemerror : \u2200{n n'}(p : n \u2261 n') \u2192 error \u2261 subst _\u22a2 p error\nlemerror refl = refl\n\nlem[]' : \u2200{n n'}(p : n \u2261 n') \u2192\n [] \u2261 subst (\u03bb n \u2192 Vec (n \u22a2) 0) p []\nlem[]' refl = refl\n\nlem[]'' : \u2200{n n'}(p : n \u2261 n') \u2192\n [] \u2261 subst (\u03bb n \u2192 Vec (n \u22a2) 0) p []\nlem[]'' refl = refl\n\nlem-plc_dummy : \u2200{n n'}(p : n \u2261 n') \u2192\n plc_dummy \u2261 subst _\u22a2 p plc_dummy\nlem-plc_dummy refl = refl\n\n\nlem\u2237 : \u2200{m n n'}(p : n \u2261 n')(t : n \u22a2)(ts : Vec (n \u22a2) m)\n \u2192 subst _\u22a2 p t \u2237 subst (\u03bb n \u2192 Vec (n \u22a2) m) p ts \u2261 subst (\u03bb n \u2192 Vec (n \u22a2) (suc m)) p (t \u2237 ts) \nlem\u2237 refl t ts = refl\n\nlem\u2237' : \u2200{A : Set}{n n'}(p : n \u2261 n')(q : suc n \u2261 suc n')(t : A)(ts : Vec A n)\n \u2192 t \u2237 subst (Vec A) p ts \u2261 subst (Vec A) q (t \u2237 ts) \nlem\u2237' refl refl t ts = refl\n\nlem:< : \u2200{m n n'}(p : n \u2261 n')(ts : Vec (n \u22a2) m)(t : n \u22a2)\n \u2192 subst (\u03bb n \u2192 Vec (n \u22a2) m) p ts :< subst _\u22a2 p t \u2261 subst (\u03bb n \u2192 Vec (n \u22a2) (suc m)) p (ts :< t) \nlem:< refl ts t = refl\n\nlem:<' : \u2200{A : Set}{n n'}(p : n \u2261 n')(q : suc n \u2261 suc n')(ts : Vec A n)(t : A)\n \u2192 subst (Vec A) p ts :< t \u2261 subst (Vec A) q (ts :< t) \nlem:<' refl refl ts t = refl\n\n\nlemTel : \u2200{m n n'}(p : n \u2261 n')(bn : Builtin)(ts : Vec (n \u22a2) m)\n \u2192 (q : m \u2264\u2034 arity bn)\n \u2192 builtin bn q (subst (\u03bb n \u2192 Vec (n \u22a2) m) p ts)\n \u2261 subst _\u22a2 p (builtin bn q ts)\nlemTel refl bn ts q = refl\n\nlem-erase : \u2200{\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2Nf\u22c6 *}(p : \u0393 \u2261 \u0393')(q : A \u2261 A')(t : \u0393 A.\u22a2 A)\n \u2192 subst _\u22a2 (lem\u2261Ctx p) (erase t) \u2261 erase (conv\u22a2 p q t)\nlem-erase refl refl t = refl\n\nlem-subst : \u2200{n}(t : n \u22a2)(p : n \u2261 n) \u2192 subst _\u22a2 p t \u2261 t\nlem-subst t refl = refl\n\nlem-builtin : \u2200{m n n'}(b : Builtin)(ts : Untyped.Tel n m)\n \u2192 (p : n \u2264\u2034 arity b)\n \u2192 (q : n' \u2264\u2034 arity b)\n \u2192 (r : n \u2261 n')\n \u2192 Untyped.builtin b p ts \u2261 builtin b q (subst (Vec (m \u22a2)) r ts)\nlem-builtin b ts p q refl = cong (\u03bb p \u2192 builtin b p ts) (lem\u2264\u2034 p q)\n\n\nlem-erase' : \u2200{\u03a6 \u0393}{A A' : \u03a6 \u22a2Nf\u22c6 *}(q : A \u2261 A')(t : \u0393 A.\u22a2 A)\n \u2192 erase t \u2261 erase (conv\u22a2 refl q t)\nlem-erase' {\u0393 = \u0393} p t = trans\n (sym (lem-subst (erase t) (lem\u2261Ctx {\u0393 = \u0393} refl)))\n (lem-erase refl p t)\n\nsame : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *}(t : \u0393 D.\u22a2 A)\n \u2192 D.erase t \u2261 subst _\u22a2 (lenLemma \u0393) (erase (nfType t)) \nsubst++ : \u2200{A : Set}{m m' n n'}\n \u2192 (as : Vec A m)\n \u2192 (as' : Vec A n)\n \u2192 (p : m \u2261 m')\n \u2192 (q : n \u2261 n')\n \u2192 (r : m + n \u2261 m' + n')\n \u2192 subst (Vec A) r (as ++ as')\n \u2261 subst (Vec A) p as ++ subst (Vec A) q as'\nsubst++ as as' refl refl refl = refl\n\nsubst++' : \u2200{o o' m n}\n \u2192 (as : Vec (o \u22a2) m)\n \u2192 (as' : Vec (o \u22a2) n)\n \u2192 (p : o \u2261 o')\n \u2192 subst (\u03bb o \u2192 Vec (o \u22a2) (m + n)) p (as ++ as')\n \u2261 subst (\u03bb o \u2192 Vec (o \u22a2) m) p as ++ subst (\u03bb o \u2192 Vec (o \u22a2) n) p as'\nsubst++' as as' refl = refl\n\n+cancel : \u2200{m m' n n'} \u2192 m + n \u2261 m' + n' \u2192 m \u2261 m' \u2192 n \u2261 n'\n+cancel p refl = +-cancel\u02e1-\u2261 _ p\n\nopen import Data.Unit\nsame {\u0393 = \u0393}(D.` x) =\n trans (cong ` (sameVar x)) (lemVar (lenLemma \u0393) (eraseVar (nfTyVar x)))\nsame {\u0393 = \u0393} (D.\u019b t) = trans\n (cong \u019b (same t))\n (lem\u019b (lenLemma \u0393) (cong suc (lenLemma \u0393)) (erase (nfType t)))\nsame {\u0393 = \u0393} (t D.\u00b7 u) = trans\n (cong\u2082 _\u00b7_ (same t) (same u))\n (lem\u00b7 (lenLemma \u0393) (erase (nfType t)) (erase (nfType u)))\nsame {\u0393 = \u0393} (D.\u039b {B = B} t) = trans (trans (trans (cong (\u019b \u2218 U.weaken) (same t)) (cong \u019b (lem-weaken (lenLemma \u0393) (cong suc (lenLemma \u0393)) (erase (nfType t))))) (lem\u019b (lenLemma \u0393) (cong suc (lenLemma \u0393)) (U.weaken (erase (nfType t))))) (cong (subst _\u22a2 (lenLemma \u0393) \u2218 \u019b \u2218 U.weaken) (lem-erase' (subNf-lemma' B) (nfType t)))\nsame {\u0393 = \u0393} (D._\u00b7\u22c6_ {B = B} t A) = trans\n (cong (_\u00b7 plc_dummy) (same t))\n (trans\n (trans (cong\u2082 _\u00b7_ (cong (subst _\u22a2 (lenLemma \u0393)) (lem-erase' (lem\u03a0 B) (nfType t)) ) (lem-plc_dummy (lenLemma \u0393)))\n (lem\u00b7 (lenLemma \u0393) (erase (conv\u22a2 refl (lem\u03a0 B) (nfType t))) plc_dummy))\n (cong (subst _\u22a2 (lenLemma \u0393)) (lem-erase' (lem[] A B) (conv\u22a2 refl (lem\u03a0 B) (nfType t) \u00b7\u22c6 nf A)))) \nsame {\u0393 = \u0393} (D.wrap A B t) = trans\n (same t)\n (cong (subst _\u22a2 (lenLemma \u0393)) (lem-erase' (stability-\u03bc A B) (nfType t)))\nsame {\u0393 = \u0393} (D.unwrap {A = A}{B = B} t) = trans\n (same t)\n (cong\n (subst _\u22a2 (lenLemma \u0393))\n (lem-erase' (sym (stability-\u03bc A B)) (unwrap (nfType t)))) \nsame {\u0393 = \u0393} (D.conv p t) = trans\n (same t)\n (cong (subst _\u22a2 (lenLemma \u0393)) (lem-erase' (completeness p) (nfType t)))\nsame {\u0393 = \u0393} (D.con tcn) = trans\n (cong con (sameTC {\u0393 = \u0393} tcn))\n (lemcon' (lenLemma \u0393) (eraseTC {\u0393 = nfCtx \u0393} (nfTypeTC tcn)))\n\nsame {\u0393 = \u0393} (D.ibuiltin b) = trans (lemerror (lenLemma \u0393)) (cong (subst _\u22a2 (lenLemma \u0393)) (lem-erase refl (itype-lem b) (ibuiltin b))) \nsame {\u0393 = \u0393} (D.error A) = lemerror (lenLemma \u0393)\n\nopen import Algorithmic.Soundness\n\nsame'Len : \u2200 {\u03a6}(\u0393 : A.Ctx \u03a6) \u2192 D.len (embCtx \u0393) \u2261 len \u0393\nsame'Len \u2205 = refl\nsame'Len (\u0393 ,\u22c6 J) = same'Len \u0393\nsame'Len (\u0393 , A) = cong suc (same'Len \u0393)\n\nlem-Dconv\u220b : \u2200{\u03a6 \u0393 \u0393'}{A A' : \u03a6 \u22a2\u22c6 *}(p : \u0393 \u2261 \u0393')(q : A \u2261 A')(x : \u0393 D.\u220b A)\n \u2192 subst Fin (cong D.len p) (D.eraseVar x) \u2261 D.eraseVar (D.conv\u220b p q x)\nlem-Dconv\u220b refl refl x = refl\n\nsame'Var : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 A.\u220b A)\n \u2192 eraseVar x \u2261 subst Fin (same'Len \u0393) (D.eraseVar (embVar x))\nsame'Var {\u0393 = \u0393 , _} Z = lemzero (cong suc (same'Len \u0393))\nsame'Var {\u0393 = \u0393 , _} (S x) = trans\n (cong suc (same'Var x))\n (lemsuc (cong suc (same'Len \u0393)) (same'Len \u0393) (D.eraseVar (embVar x)))\nsame'Var {\u0393 = \u0393 ,\u22c6 _} (T {A = A} x) = trans\n (same'Var x)\n (cong (subst Fin (same'Len \u0393)) (lem-Dconv\u220b refl (sym (ren-embNf S A))\n (D.T (embVar x))))\n\nsame'TC : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}(tcn : AC.TyTermCon A)\n \u2192 eraseTC {\u0393 = \u0393} tcn \u2261 D.eraseTC {\u03a6}{\u0393 = embCtx \u0393} (embTC tcn)\nsame'TC (AC.integer i) = refl\nsame'TC (AC.bytestring b) = refl\nsame'TC (AC.string s) = refl\nsame'TC (AC.bool b) = refl\nsame'TC (AC.char c) = refl\nsame'TC AC.unit = refl\n\nsame' : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2Nf\u22c6 *}(x : \u0393 A.\u22a2 A)\n \u2192 erase x \u2261 subst _\u22a2 (same'Len \u0393) (D.erase (emb x))\nsame' {\u0393 = \u0393} (` x) =\n trans (cong ` (same'Var x)) (lemVar (same'Len \u0393) (D.eraseVar (embVar x)))\nsame' {\u0393 = \u0393} (\u019b t) = trans\n (cong \u019b (same' t))\n (lem\u019b (same'Len \u0393) (cong suc (same'Len \u0393)) (D.erase (emb t)))\nsame' {\u0393 = \u0393} (t \u00b7 u) = trans\n (cong\u2082 _\u00b7_ (same' t) (same' u))\n (lem\u00b7 (same'Len \u0393) (D.erase (emb t)) (D.erase (emb u)))\nsame' {\u0393 = \u0393} (\u039b t) = trans\n (trans (cong (\u019b \u2218 U.weaken) (same' t))\n (cong \u019b\n (lem-weaken (same'Len \u0393) (cong suc (same'Len \u0393)) (D.erase (emb t)))))\n (lem\u019b (same'Len \u0393) (cong suc (same'Len \u0393)) (U.weaken (D.erase (emb t))))\nsame' {\u0393 = \u0393} (_\u00b7\u22c6_ t A) = trans\n (cong\u2082 _\u00b7_ (same' t) (lem-plc_dummy (same'Len \u0393)))\n (lem\u00b7 (same'Len \u0393) (D.erase (emb t)) plc_dummy)\nsame' {\u0393 = \u0393} (wrap A B t) = same' t\nsame' {\u0393 = \u0393} (unwrap t) = same' t\nsame' {\u0393 = \u0393} (con x) = trans (cong con (same'TC {\u0393 = \u0393} x)) (lemcon' (same'Len \u0393) (D.eraseTC {\u0393 = embCtx \u0393}(embTC x)))\nsame' {\u0393 = \u0393} (ibuiltin b) = lemerror (same'Len \u0393)\nsame' {\u0393 = \u0393} (error A) = lemerror (same'Len \u0393)\n\\end{code}\n","avg_line_length":35.4194444444,"max_line_length":322,"alphanum_fraction":0.5725825425} +{"size":9758,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\n\nmodule Algorithmic.Erasure.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Function\n\nopen import Type\nopen import Type.BetaNormal\nopen import Algorithmic as A\nimport Algorithmic.Reduction as A\nimport Algorithmic.RenamingSubstitution as A\nopen import Algorithmic.Erasure\nopen import Algorithmic.Erasure.RenamingSubstitution\nimport Untyped.Reduction as U\nimport Untyped.RenamingSubstitution as U\nopen import Builtin\nopen import Builtin.Constant.Type\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2Nf\u22c6_ con\nopen import Untyped\nopen import Builtin.Signature Ctx\u22c6 Kind\n \u2205 _,\u22c6_ * _\u220b\u22c6_ Z S _\u22a2Nf\u22c6_ (ne \u2218 `) con\nopen import Type.BetaNBE.RenamingSubstitution\nopen import Data.Sum\nopen import Relation.Binary.PropositionalEquality hiding ([_])\nopen import Data.List using (List;[];_\u2237_)\nopen import Data.Vec hiding (map; [_])\nopen import Data.Product hiding (map) renaming (_,_ to _,,_)\nopen import Data.Unit hiding (_\u2264_; _\u2264?_; _\u225f_)\nimport Utils as Util\nopen import Relation.Nullary\nopen import Data.Nat hiding (_<_; _\u2264?_; _^_; _+_; _\u225f_; _*_)\nopen import Data.Integer hiding (suc)\nopen import Data.Fin using (suc)\nimport Data.Bool as B\n\\end{code}\n\n\\begin{code}\neraseCtx : \u2200{\u03a6}(\u0393 : Ctx \u03a6) \u2192 U.Bwd U.Label\neraseCtx \u2205 = U.[]\neraseCtx (\u0393 ,\u22c6 J) = eraseCtx \u0393 U.:< U.Type\neraseCtx (\u0393 , A) = eraseCtx \u0393 U.:< U.Term\n\nerase\u2264C' : \u2200{\u03a6 \u03a6'}{\u0393 : Ctx \u03a6}{\u0393' : Ctx \u03a6'} \u2192 \u0393 \u2264C' \u0393' \u2192 eraseCtx \u0393 U.\u2264L eraseCtx \u0393'\nerase\u2264C' base = U.base\nerase\u2264C' (skip\u22c6 p) = U.skipType (erase\u2264C' p)\nerase\u2264C' (skip p) = U.skipTerm (erase\u2264C' p)\n\n-- there could be a simpler version of this without p and q...\nerase-arity-lem : \u2200 b {\u03a6}{\u0393}(p : proj\u2081 (ISIG b) \u2261 \u03a6)(q : subst Ctx p (proj\u2081 (proj\u2082 (ISIG b))) \u2261 \u0393) \u2192 eraseCtx \u0393 \u2261 U.arity b\nerase-arity-lem addInteger refl refl = refl\nerase-arity-lem subtractInteger refl refl = refl\nerase-arity-lem multiplyInteger refl refl = refl\nerase-arity-lem divideInteger refl refl = refl\nerase-arity-lem quotientInteger refl refl = refl\nerase-arity-lem remainderInteger refl refl = refl\nerase-arity-lem modInteger refl refl = refl\nerase-arity-lem lessThanInteger refl refl = refl\nerase-arity-lem lessThanEqualsInteger refl refl = refl\nerase-arity-lem equalsInteger refl refl = refl\nerase-arity-lem appendByteString refl refl = refl\nerase-arity-lem lessThanByteString refl refl = refl\nerase-arity-lem lessThanEqualsByteString refl refl = refl\nerase-arity-lem sha2-256 refl refl = refl\nerase-arity-lem sha3-256 refl refl = refl\nerase-arity-lem verifySignature refl refl = refl\nerase-arity-lem equalsByteString refl refl = refl\nerase-arity-lem ifThenElse refl refl = refl\nerase-arity-lem appendString refl refl = refl\nerase-arity-lem trace refl refl = refl\n\neraseITel : \u2200 b {\u03a6}(\u0394 : Ctx \u03a6)(\u03c3 : SubNf \u03a6 \u2205)\n \u2192 A.ITel b \u0394 \u03c3 \u2192 U.ITel b (eraseCtx \u0394)\n\neraseFVal : {A B : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 A.\u22a2 A \u21d2 B}\n \u2192 A.Value t \u2192 U.FValue (erase t)\neraseFVal (A.V-\u019b t) = U.V-\u019b (erase t)\neraseFVal (A.V-I\u21d2 b p q r \u03c3 p' vs t) =\n U.V-builtin b (erase-arity-lem b p q) (erase\u2264C' p') (eraseITel b _ \u03c3 vs) (erase t)\n\neraseVal : {A : \u2205 \u22a2Nf\u22c6 *}{t : \u2205 A.\u22a2 A}\n \u2192 A.Value t \u2192 U.Value (erase t)\neraseVal v@(A.V-\u019b t) = U.V-F (eraseFVal v)\neraseVal (A.V-\u039b t) = U.V-delay\neraseVal (A.V-wrap v) = eraseVal v\neraseVal (A.V-con cn) = U.V-con (eraseTC {\u2205}{\u2205} cn)\neraseVal (A.V-I\u03a0 b p q r \u03c3 p' vs t) = U.V-builtin\u22c6 b (erase-arity-lem b p q) (erase\u2264C' p') (eraseITel b _ \u03c3 vs) (erase t)\neraseVal v@(A.V-I\u21d2 b p q r \u03c3 p\u2081 x t) = U.V-F (eraseFVal v)\n\neraseITel b \u2205 \u03c3 vs = tt\neraseITel b (\u0394 ,\u22c6 J) \u03c3 (vs ,, A) = eraseITel b \u0394 (\u03c3 \u2218 S) vs\neraseITel b (\u0394 , A) \u03c3 (vs ,, t ,, v) =\n eraseITel b \u0394 \u03c3 vs ,, erase t ,, eraseVal v\n\n\neraseErr : \u2200{\u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}{\u0393 : A.Ctx \u03a6}{e : \u0393 A.\u22a2 A}\n \u2192 A.Error e \u2192 U.Error (erase e)\neraseErr A.E-error = U.E-error\n\\end{code}\n\n\\begin{code}\nerase-decIf : \u2200{\u03a6}{\u0393 : A.Ctx \u03a6}{A : \u03a6 \u22a2Nf\u22c6 *}{X}(p : Dec X)(t f : \u0393 A.\u22a2 A) \u2192\n Util.decIf p (erase t) (erase f) \u2261 erase (Util.decIf p t f)\nerase-decIf (yes p) t f = refl\nerase-decIf (no \u00acp) t f = refl\n\\end{code}\n\n\\begin{code}\n-- We want that when a builtin eventually computes the untyped and\n-- typed semantics would give the same answer. Here we just\n-- exhaustively pattern match on the builtin and its typed args to get\n-- it to compute.\nerase-BUILTIN : \u2200 b (\u03c3 : SubNf (proj\u2081 (ISIG b)) \u2205)(vs : A.ITel b (proj\u2081 (proj\u2082 (ISIG b))) \u03c3) \u2192\n proj\u2081\n (U.IBUILTIN' b (erase-arity-lem b refl refl) (eraseITel b (proj\u2081 (proj\u2082 (ISIG b))) \u03c3 vs))\n \u2261 erase (proj\u2081 (A.IBUILTIN b \u03c3 vs))\nerase-BUILTIN addInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN subtractInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN multiplyInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) = refl\nerase-BUILTIN divideInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN quotientInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN remainderInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN modInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i' \u225f +0\n... | yes p = refl\n... | no \u00acp = refl\nerase-BUILTIN lessThanInteger \u03c3 ((tt ,, _ ,, A.V-con (integer i)) ,, _ ,, A.V-con (integer i')) with i Data.Integer.?_;_\u2265?_)\nimport Data.Bool as Bool\n\\end{code}\n\n## Values\n\n\\begin{code}\ndata Value : \u2200 {J \u0393} {A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} \u2192 \u0393 \u22a2 A \u2192 Set where\n\n V-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B}\n ---------------------------\n \u2192 Value (\u019b N)\n\n V-\u039b_ : \u2200 {\u0393 K} {B : \u2225 \u0393 \u2225 ,\u22c6 K \u22a2\u22c6 *}\n \u2192 {N : \u0393 ,\u22c6 K \u22a2 B}\n ----------------\n \u2192 Value (\u039b N)\n\n V-wrap1 : \u2200{\u0393 K}\n \u2192 {pat : \u2225 \u0393 \u2225 \u22a2\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {arg : \u2225 \u0393 \u2225 \u22a2\u22c6 K}\n \u2192 {term : \u0393 \u22a2 pat \u00b7 (\u03bc1 \u00b7 pat) \u00b7 arg}\n \u2192 Value (wrap1 pat arg term)\n\n V-con : \u2200{\u0393}{n}{tcn : TyCon}\n \u2192 (cn : TermCon (con tcn (size\u22c6 n)))\n \u2192 Value (con {\u0393} cn)\n\n\\end{code}\n\n## BUILTIN\n\n\\begin{code}\nVTel : \u2200 \u0393 \u0394 \u2192 \u22c6.Sub \u0394 \u2225 \u0393 \u2225 \u2192 List (\u0394 \u22a2\u22c6 *) \u2192 Set\nVTel \u0393 \u0394 \u03c3 [] = \u22a4\nVTel \u0393 \u0394 \u03c3 (A \u2237 As) = \u03a3 (\u0393 \u22a2 \u22c6.subst \u03c3 A) \u03bb t \u2192 Value t \u00d7 VTel \u0393 \u0394 \u03c3 As\n\nBUILTIN : \u2200{\u0393}\n \u2192 (bn : Builtin)\n \u2192 let \u0394 ,, As ,, C = SIG bn in\n (\u03c3 : \u22c6.Sub \u0394 \u2225 \u0393 \u2225)\n \u2192 (vtel : VTel \u0393 \u0394 \u03c3 As)\n -----------------------------\n \u2192 Maybe (\u0393 \u22a2 \u22c6.subst \u03c3 C)\nBUILTIN addInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n addInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (i + j)\n... | yes r = just (con (integer s (i + j) r))\n... | no \u00acr = nothing\nBUILTIN subtractInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n subtractInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (i - j)\n... | yes r = just (con (integer s (i - j) r))\n... | no \u00acp = nothing\nBUILTIN multiplyInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n multiplyInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (i ** j)\n... | yes r = just (con (integer s (i ** j) r))\n... | no \u00acp = nothing\nBUILTIN divideInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n divideInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (div i j)\n... | yes r = just (con (integer s (div i j) r))\n... | no \u00acr = nothing\nBUILTIN quotientInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n quotientInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (quot i j)\n... | yes r = just (con (integer s (quot i j) r))\n... | no \u00acr = nothing\nBUILTIN remainderInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n remainderInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (rem i j)\n... | yes r = just (con (integer s (rem i j) r))\n... | no \u00acr = nothing\nBUILTIN modInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n modInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s (pos 0) q) ,, tt)\n | size\u22c6 s\n = nothing\nBUILTIN\n modInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with boundedI? s (mod i j)\n... | yes r = just (con (integer s (mod i j) r))\n... | no \u00acr = nothing\nBUILTIN lessThanInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n lessThanInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with i ? j\n... | yes _ = just true\n... | no _ = just false \nBUILTIN greaterThanEqualsInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n greaterThanEqualsInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with i \u2265? j\n... | yes _ = just true\n... | no _ = just false\nBUILTIN equalsInteger \u03c3 vtel with \u03c3 Z\nBUILTIN\n equalsInteger\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (integer .s j q) ,, tt)\n | size\u22c6 s\n with i \u225f j\n... | yes _ = just true\n... | no _ = just false\nBUILTIN resizeInteger \u03c3 vtel with \u03c3 Z | \u03c3 (S Z)\nBUILTIN\n resizeInteger\n \u03c3\n (_ ,, V-con (size s') ,, _ ,, V-con (integer s i p) ,, tt)\n | size\u22c6 s'\n | size\u22c6 s\n with boundedI? s' i\n... | yes r = just (con (integer s' i r))\n... | no \u00acr = nothing\nBUILTIN sizeOfInteger \u03c3 vtel with \u03c3 Z\nBUILTIN sizeOfInteger \u03c3 (_ ,, V-con (integer s i x) ,, tt) | .(size\u22c6 s) =\n just (con (size s))\nBUILTIN intToByteString \u03c3 vtel with \u03c3 Z | \u03c3 (S Z)\nBUILTIN\n intToByteString\n \u03c3\n (_ ,, V-con (size s) ,, _ ,, V-con (integer s' i p) ,, tt)\n | size\u22c6 s\n | size\u22c6 s' with boundedI? s i\n... | no _ = nothing\n... | yes q with boundedB? s (int2ByteString i)\n... | yes r = just (con (bytestring s (int2ByteString i) r))\n... | no _ = nothing\n-- ^ should be impossible if we prove something about int2ByteString\nBUILTIN concatenate \u03c3 vtel with \u03c3 Z\nBUILTIN\n concatenate\n \u03c3\n (_ ,, V-con (bytestring s b p) ,, _ ,, V-con (bytestring .s b' q) ,, tt)\n | size\u22c6 s\n with boundedB? s (append b b')\n... | yes r = just (con (bytestring s (append b b') r))\n... | no \u00acr = nothing \n\nBUILTIN takeByteString \u03c3 vtel with \u03c3 Z | \u03c3 (S Z)\nBUILTIN\n takeByteString\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (bytestring s' b q) ,, tt)\n | .(size\u22c6 s')\n | size\u22c6 s\n with boundedB? s' (take i b)\n... | yes r = just (con (bytestring s' (take i b) r))\n... | no r = nothing\n-- ^ this is impossible but we haven't proved that take cannot\n-- increase the length\nBUILTIN dropByteString \u03c3 vtel with \u03c3 Z | \u03c3 (S Z) \nBUILTIN\n dropByteString\n \u03c3\n (_ ,, V-con (integer s i p) ,, _ ,, V-con (bytestring s' b q) ,, tt)\n | size\u22c6 s'\n | size\u22c6 s with boundedB? s' (drop i b)\n... | yes r = just (con (bytestring s' (drop i b) r))\n... | no \u00acr = nothing\n-- ^ this is impossible but we haven't proved that drop cannot\n-- increase the length\nBUILTIN sha2-256 \u03c3 vtel with \u03c3 Z\nBUILTIN\n sha2-256\n \u03c3\n (_ ,, V-con (bytestring s b p) ,, tt)\n | size\u22c6 s with boundedB? 32 (SHA2-256 b)\n... | yes q = just (con (bytestring 32 (SHA2-256 b) q))\n... | no _ = nothing\n-- ^ should be impossible\nBUILTIN sha3-256 \u03c3 vtel with \u03c3 Z\nBUILTIN\n sha3-256\n \u03c3\n (_ ,, V-con (bytestring s b p) ,, tt)\n | size\u22c6 s with boundedB? 32 (SHA3-256 b)\n... | yes q = just (con (bytestring 32 (SHA3-256 b) q))\n... | no _ = nothing\n-- ^ should be impossible\nBUILTIN verifySignature \u03c3 vtel with \u03c3 Z | \u03c3 (S Z) | \u03c3 (S (S Z))\nBUILTIN\n verifySignature\n \u03c3\n ( _ ,, V-con (bytestring s k p)\n ,, _ ,, V-con (bytestring s' d p')\n ,, _ ,, V-con (bytestring s'' c p'')\n ,, tt)\n | size\u22c6 s''\n | size\u22c6 s'\n | size\u22c6 s\n with verifySig k d c\n... | just Bool.true = just true\n... | just Bool.false = just false\n... | nothing = nothing\nBUILTIN resizeByteString \u03c3 vtel with \u03c3 Z | \u03c3 (S Z)\nBUILTIN\n resizeByteString\n \u03c3\n (_ ,, V-con (size s) ,, _ ,, V-con (bytestring s' b p) ,, tt)\n | size\u22c6 s\n | size\u22c6 s'\n with boundedB? s b\n... | yes q = just (con (bytestring s b q))\n... | no _ = nothing\nBUILTIN equalsByteString \u03c3 vtel with \u03c3 Z\nBUILTIN\n equalsByteString\n \u03c3\n (_ ,, V-con (bytestring s b p) ,, _ ,, V-con (bytestring .s b' q) ,, tt)\n | size\u22c6 s\n with equals b b'\n... | Bool.true = just true\n... | Bool.false = just false\n\\end{code}\n\n# recontructing the telescope after a reduction step\n\n\\begin{code}\nreconstTel : \u2200{\u0393 \u0394 As} Bs Ds\n \u2192 (\u03c3 : \u22c6.Sub \u0394 \u2225 \u0393 \u2225)\n \u2192 (vtel : VTel \u0393 \u0394 \u03c3 Bs)\n \u2192 \u2200{C}(t' : \u0393 \u22a2 \u22c6.subst \u03c3 C)\n \u2192 (p : Bs ++ (C \u2237 Ds) \u2261 As)\n \u2192 (tel' : Tel \u0393 \u0394 \u03c3 Ds)\n \u2192 Tel \u0393 \u0394 \u03c3 As\nreconstTel [] Ds \u03c3 vtel t' refl tel' = t' ,, tel'\nreconstTel (B \u2237 Bs) Ds \u03c3 (X ,, VX ,, vtel) t' refl tel' =\n X ,, reconstTel Bs Ds \u03c3 vtel t' refl tel'\n\\end{code}\n\n## Intrinsically Type Preserving Reduction\n\n\\begin{code}\ninfix 2 _\u2014\u2192_\n\ndata _\u2014\u2192_ : \u2200 {J \u0393} {A : \u2225 \u0393 \u2225 \u22a2\u22c6 J} \u2192 (\u0393 \u22a2 A) \u2192 (\u0393 \u22a2 A) \u2192 Set where\n\n \u03be-\u00b7\u2081 : \u2200 {\u0393 A B} {L L\u2032 : \u0393 \u22a2 A \u21d2 B} {M : \u0393 \u22a2 A}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7 M \u2014\u2192 L\u2032 \u00b7 M\n\n \u03be-\u00b7\u2082 : \u2200 {\u0393 A B} {V : \u0393 \u22a2 A \u21d2 B} {M M\u2032 : \u0393 \u22a2 A}\n \u2192 Value V\n \u2192 M \u2014\u2192 M\u2032\n ---------------\n \u2192 V \u00b7 M \u2014\u2192 V \u00b7 M\u2032\n\n \u03be-\u00b7\u22c6 : \u2200 {\u0393 K}{B : \u2225 \u0393 \u2225 ,\u22c6 K \u22a2\u22c6 *}{L L\u2032 : \u0393 \u22a2 \u03a0 B}{A}\n \u2192 L \u2014\u2192 L\u2032\n -----------------\n \u2192 L \u00b7\u22c6 A \u2014\u2192 L\u2032 \u00b7\u22c6 A\n \n \u03b2-\u019b : \u2200 {\u0393 A B} {N : \u0393 , A \u22a2 B} {W : \u0393 \u22a2 A}\n \u2192 Value W\n --------------------\n \u2192 (\u019b N) \u00b7 W \u2014\u2192 N [ W ]\n\n \u03b2-\u039b : \u2200 {\u0393 K}{B : \u2225 \u0393 \u2225 ,\u22c6 K \u22a2\u22c6 *}{N : \u0393 ,\u22c6 K \u22a2 B}{W}\n ----------------------\n \u2192 (\u039b N) \u00b7\u22c6 W \u2014\u2192 N [ W ]\u22c6\n\n \u03b2-wrap1 : \u2200{\u0393 K}\n \u2192 {pat : \u2225 \u0393 \u2225 \u22a2\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {arg : \u2225 \u0393 \u2225 \u22a2\u22c6 K}\n \u2192 {term : \u0393 \u22a2 pat \u00b7 (\u03bc1 \u00b7 pat) \u00b7 arg}\n \u2192 unwrap1 (wrap1 pat arg term) \u2014\u2192 term\n\n \u03be-unwrap1 : \u2200{\u0393 K}\n \u2192 {pat : \u2225 \u0393 \u2225 \u22a2\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *}\n \u2192 {arg : \u2225 \u0393 \u2225 \u22a2\u22c6 K}\n \u2192 {M M' : \u0393 \u22a2 \u03bc1 \u00b7 pat \u00b7 arg}\n \u2192 M \u2014\u2192 M'\n \u2192 unwrap1 M \u2014\u2192 unwrap1 M'\n\n \u03b2-builtin : \u2200{\u0393}\n \u2192 (bn : Builtin)\n \u2192 let \u0394 ,, As ,, C = SIG bn in\n (\u03c3 : \u22c6.Sub \u0394 \u2225 \u0393 \u2225)\n \u2192 (tel : Tel \u0393 \u0394 \u03c3 As)\n \u2192 (vtel : VTel \u0393 \u0394 \u03c3 As)\n -----------------------------\n \u2192 builtin bn \u03c3 tel \u2014\u2192 maybe id (error _) (BUILTIN bn \u03c3 vtel)\n\n \u03be-builtin : \u2200{\u0393} \u2192 (bn : Builtin)\n \u2192 let \u0394 ,, As ,, C = SIG bn in\n (\u03c3 : \u22c6.Sub \u0394 \u2225 \u0393 \u2225)\n \u2192 (tel : Tel \u0393 \u0394 \u03c3 As)\n \u2192 \u2200 Bs Ds\n \u2192 (vtel : VTel \u0393 \u0394 \u03c3 Bs)\n \u2192 \u2200{C}{t t' : \u0393 \u22a2 \u22c6.subst \u03c3 C}\n \u2192 t \u2014\u2192 t'\n \u2192 (p : Bs ++ (C \u2237 Ds) \u2261 As)\n \u2192 (tel' : Tel \u0393 \u0394 \u03c3 Ds)\n \u2192 builtin bn \u03c3 tel\n \u2014\u2192\n builtin bn \u03c3 (reconstTel Bs Ds \u03c3 vtel t' p tel')\n\\end{code}\n\n\n\\begin{code}\ndata Progress {A : \u2205 \u22a2\u22c6 *} (M : \u2205 \u22a2 A) : Set where\n step : \u2200 {N}\n \u2192 M \u2014\u2192 N\n -------------\n \u2192 Progress M\n done :\n Value M\n ----------\n \u2192 Progress M\n error : Progress M \n\\end{code}\n\n\\begin{code}\n\ndata TelProgress\n {\u0393}\n {\u0394}\n {\u03c3 : \u22c6.Sub \u0394 \u2225 \u0393 \u2225}\n {As : List (\u0394 \u22a2\u22c6 *)}\n (tel : Tel \u0393 \u0394 \u03c3 As)\n : Set where\n done : VTel \u0393 \u0394 \u03c3 As \u2192 TelProgress tel\n step : \u2200 Bs Ds\n \u2192 VTel \u0393 \u0394 \u03c3 Bs\n \u2192 \u2200{C}{t t' : \u0393 \u22a2 \u22c6.subst \u03c3 C}\n \u2192 t \u2014\u2192 t'\n \u2192 Bs ++ (C \u2237 Ds) \u2261 As\n \u2192 Tel \u0393 \u0394 \u03c3 Ds\n \u2192 TelProgress tel\n error : TelProgress tel\n\nprogress : \u2200 {A} \u2192 (M : \u2205 \u22a2 A) \u2192 Progress M\nprogressTel : \u2200 {\u0394}{\u03c3 : \u22c6.Sub \u0394 \u2205}{As : List (\u0394 \u22a2\u22c6 *)}\n \u2192 (tel : Tel\u00a0\u2205 \u0394 \u03c3 As) \u2192 TelProgress tel\n\nprogressTel {As = []} tel = done tt\nprogressTel {As = A \u2237 As} (t ,, tel) with progress t\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | step p = step [] As tt p refl tel\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | done vt with progressTel tel\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | done vt | done vtel =\n done (t ,, vt ,, vtel)\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | done vt | step Bs Ds vtel p q tel' =\n step (A \u2237 Bs) Ds (t ,, vt ,, vtel) p (cong (A \u2237_) q) tel'\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | done vt | error = error\nprogressTel {\u03c3 = _} {A \u2237 As} (t ,, tel) | error = error\n\nprogress (` ())\nprogress (\u019b M) = done V-\u019b\nprogress (L \u00b7 M) with progress L\n... | error = error\n... | step p = step (\u03be-\u00b7\u2081 p)\nprogress (.(\u019b _) \u00b7 M) | done V-\u019b with progress M\nprogress (.(\u019b _) \u00b7 M) | done V-\u019b | step p = step (\u03be-\u00b7\u2082 V-\u019b p)\nprogress (.(\u019b _) \u00b7 M) | done V-\u019b | done VM = step (\u03b2-\u019b VM)\nprogress (.(\u019b _) \u00b7 M) | done V-\u019b | error = error\nprogress (\u039b M) = done V-\u039b_\nprogress (M \u00b7\u22c6 A) with progress M\nprogress (M \u00b7\u22c6 A) | step p = step (\u03be-\u00b7\u22c6 p)\nprogress (.(\u039b _) \u00b7\u22c6 A) | done V-\u039b_ = step \u03b2-\u039b\nprogress (M \u00b7\u22c6 A) | error = error\nprogress (wrap1 _ _ t) = done V-wrap1\nprogress (unwrap1 t) with progress t\nprogress (unwrap1 t) | step p = step (\u03be-unwrap1 p)\nprogress (unwrap1 .(wrap1 _ _ _)) | done V-wrap1 = step \u03b2-wrap1\nprogress (unwrap1 t) | error = error\nprogress (conv p t) = error\nprogress (con (integer s i p)) = done (V-con _)\nprogress (con (bytestring s x p)) = done (V-con _)\nprogress (con (size s)) = done (V-con _)\nprogress (builtin bn \u03c3 X) with progressTel X\nprogress (builtin bn \u03c3 X) | done VX = step (\u03b2-builtin bn \u03c3 X VX)\nprogress (builtin bn \u03c3 X) | step Bs Ds vtel p q tel' =\n step (\u03be-builtin bn \u03c3 X Bs Ds vtel p q tel')\nprogress (builtin bn \u03c3 X) | error = error\nprogress (error A) = error\n\\end{code}\n","avg_line_length":27.4396551724,"max_line_length":90,"alphanum_fraction":0.5396638391} +{"size":20379,"ext":"lagda","lang":"Literate Agda","max_stars_count":135.0,"content":"\\begin{code}\n{-# OPTIONS --rewriting #-}\nmodule Main where\nopen import Agda.Builtin.IO\nimport IO.Primitive as IO using (return;_>>=_)\nopen import Agda.Builtin.Unit\nopen import Agda.Builtin.String\nopen import Function\nopen import Data.Sum\nopen import Data.String\nopen import Agda.Builtin.TrustMe\nopen import Relation.Binary.PropositionalEquality\nopen import Agda.Builtin.Nat\nopen import Data.Nat\nopen import Agda.Builtin.Int\nopen import Data.Integer\nopen import Data.Integer.Show\nopen import Data.Product renaming (_,_ to _,,_)\nopen import Data.Bool\nopen import Data.Fin\nopen import Data.Vec hiding (_>>=_;_++_)\nopen import Data.List hiding (_++_)\nimport Debug.Trace as D\n\nopen import Type\nopen import Builtin\nopen import Check\nopen import Scoped.Extrication\nopen import Type.BetaNBE\nopen import Type.BetaNormal\nimport Untyped as U\nopen import Untyped.CEK as U\nimport Scoped as S\nopen import Raw\nopen import Scoped\nopen import Utils hiding (ByteString)\nopen import Algorithmic hiding (Term;Type)\nopen import Algorithmic.ReductionEC\nopen import Algorithmic.Reduction\nopen import Algorithmic.CK\nopen import Algorithmic.CEKV\nopen import Algorithmic.Erasure\nimport Algorithmic.Evaluation as L\n\n\n\n-- There's a long prelude here that could go in a different file but\n-- currently it's only used here\n\n-- Text Stuff\n\npostulate\n putStrLn : String \u2192 IO \u22a4\n\n{-# FOREIGN GHC import qualified Data.Text as T #-}\n{-# FOREIGN GHC import qualified Data.Text.IO as TextIO #-}\n{-# COMPILE GHC putStrLn = TextIO.putStrLn #-}\n\n-- IO Stuff\n\ninstance\n IOMonad : Monad IO\n IOMonad = record { return = IO.return; _>>=_ = IO._>>=_ }\n\n-- Bytestring stuff\n\npostulate\n ByteString : Set\n getContents : IO String\n readFile : String \u2192 IO String\n\n{-# FOREIGN GHC import qualified Data.ByteString.Lazy as BSL #-}\n{-# COMPILE GHC ByteString = type BSL.ByteString #-}\n{-# COMPILE GHC readFile = \\s -> TextIO.readFile (T.unpack s) #-}\n{-# COMPILE GHC getContents = TextIO.getContents #-}\n\n-- System.Exit stuff\n\npostulate\n exitFailure : IO \u22a4\n exitSuccess : IO \u22a4\n\n{-# FOREIGN GHC import System.Exit #-}\n{-# COMPILE GHC exitSuccess = exitSuccess #-}\n{-# COMPILE GHC exitFailure = exitFailure #-}\n\n-- System.Environment stuff\n\npostulate\n getArgs : IO (List String)\n\n{-# FOREIGN GHC import System.Environment #-}\n{-# COMPILE GHC getArgs = (fmap . fmap) T.pack $ getArgs #-}\n\n-- Misc stuff\n\n{-# FOREIGN GHC import Data.Either #-}\n{-# FOREIGN GHC import Control.Monad.Trans.Except #-}\n\npostulate\n TermN : Set -- term with names\n Term : Set -- DeBruijn term\n TypeN : Set\n Type : Set\n ProgramN : Set\n Program : Set\n convTm : Term \u2192 RawTm\n convTy : Type \u2192 RawTy\n unconvTy : RawTy \u2192 Type\n unconvTm : RawTm \u2192 Term\n convP : Program \u2192 RawTm\n ParseError : Set\n parse : String \u2192 Either ParseError ProgramN\n parseTm : String \u2192 Either ParseError TermN\n parseTy : String \u2192 Either ParseError TypeN\n showTerm : RawTm \u2192 String\n deBruijnify : ProgramN \u2192 Either FreeVariableError Program\n deBruijnifyTm : TermN \u2192 Either FreeVariableError Term\n deBruijnifyTy : TypeN \u2192 Either FreeVariableError Type\n\n ProgramNU : Set\n ProgramU : Set\n TermNU : Set\n TermU : Set\n deBruijnifyU : ProgramNU \u2192 Either FreeVariableError ProgramU\n deBruijnifyTmU : TermNU \u2192 Either FreeVariableError TermU\n parseU : String \u2192 Either ParseError ProgramNU\n parseTmU : String \u2192 Either ParseError TermNU\n convPU : ProgramU \u2192 U.Untyped\n convTmU : TermU \u2192 U.Untyped\n unconvTmU : U.Untyped \u2192 TermU\n \n\n{-# FOREIGN GHC import PlutusCore.Name #-}\n{-# FOREIGN GHC import PlutusCore.Parser #-}\n{-# FOREIGN GHC import PlutusCore.Pretty #-}\n{-# FOREIGN GHC import PlutusCore.DeBruijn #-}\n{-# FOREIGN GHC import PlutusCore.Error #-}\n{-# FOREIGN GHC import PlutusCore.Quote #-}\n{-# FOREIGN GHC import qualified UntypedPlutusCore as U #-}\n{-# FOREIGN GHC import qualified UntypedPlutusCore.Parser as U #-}\n\n{-# FOREIGN GHC import Raw #-}\n{-# COMPILE GHC convP = convP #-}\n{-# COMPILE GHC convPU = U.convP #-}\n{-# COMPILE GHC convTm = conv #-}\n{-# COMPILE GHC convTy = convT #-}\n{-# COMPILE GHC unconvTy = unconvT 0 #-}\n{-# COMPILE GHC unconvTm = unconv 0 #-}\n{-# FOREIGN GHC import Data.Bifunctor #-}\n{-# FOREIGN GHC import Data.Functor #-}\n{-# COMPILE GHC ParseError = type PlutusCore.Error.ParserErrorBundle #-}\n\n{-# COMPILE GHC parse = runQuoteT . parseProgram #-}\n{-# COMPILE GHC parseU = runQuoteT . U.parseProgram #-}\n{-# COMPILE GHC parseTm = runQuoteT . parseTerm #-}\n{-# COMPILE GHC parseTy = runQuoteT . parseType #-}\n{-# COMPILE GHC parseTmU = runQuoteT . U.parseTerm #-}\n{-# COMPILE GHC deBruijnify = \\ (Program ann ver tm) -> second (void . Program ann ver) . runExcept $ deBruijnTerm tm #-}\n{-# COMPILE GHC deBruijnifyTm = second void . runExcept . deBruijnTerm #-}\n{-# COMPILE GHC deBruijnifyTy = second void . runExcept . deBruijnTy #-}\n{-# FOREIGN GHC import PlutusCore #-}\n{-# COMPILE GHC ProgramN = type PlutusCore.Program TyName Name DefaultUni DefaultFun PlutusCore.SourcePos #-}\n{-# COMPILE GHC Program = type PlutusCore.Program NamedTyDeBruijn NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TermN = type PlutusCore.Term TyName Name DefaultUni DefaultFun PlutusCore.SourcePos #-}\n{-# COMPILE GHC Term = type PlutusCore.Term NamedTyDeBruijn NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TypeN = type PlutusCore.Type TyName DefaultUni PlutusCore.SourcePos #-}\n{-# COMPILE GHC Type = type PlutusCore.Type NamedTyDeBruijn DefaultUni () #-}\n{-# COMPILE GHC showTerm = T.pack . show #-}\n\n{-# COMPILE GHC ProgramNU = type U.Program Name DefaultUni DefaultFun PlutusCore.SourcePos #-}\n{-# COMPILE GHC ProgramU = type U.Program NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC TermNU = type U.Term Name DefaultUni DefaultFun PlutusCore.SourcePos #-}\n{-# COMPILE GHC TermU = type U.Term NamedDeBruijn DefaultUni DefaultFun () #-}\n{-# COMPILE GHC deBruijnifyU = \\ (U.Program ann ver tm) -> second (void . U.Program ann ver) . runExcept $ U.deBruijnTerm tm #-}\n{-# COMPILE GHC deBruijnifyTmU = second void . runExcept . U.deBruijnTerm #-}\n{-# COMPILE GHC convTmU = U.conv #-}\n{-# COMPILE GHC unconvTmU = U.uconv 0 #-}\n\npostulate\n prettyPrintTm : RawTm \u2192 String\n prettyPrintTy : RawTy \u2192 String\n\n prettyPrintUTm : U.Untyped \u2192 String\n\n{-# FOREIGN GHC {-# LANGUAGE TypeApplications #-} #-}\n{-# COMPILE GHC prettyPrintTm = display @T.Text . unconv 0 #-}\n{-# COMPILE GHC prettyPrintTy = display @T.Text . unconvT 0 #-}\n\n{-# FOREIGN GHC import qualified Untyped as U #-}\n{-# COMPILE GHC prettyPrintUTm = display @T.Text . U.uconv 0 #-}\n\ndata EvalMode : Set where\n U TL L TCK CK TCEK : EvalMode\n\n{-# COMPILE GHC EvalMode = data EvalMode (U | TL | L | TCK | CK | TCEK) #-}\n\n-- the Error's returned by `plc-agda` and the haskell interface to `metatheory`.\n\ndata ERROR : Set where\n typeError : String \u2192 ERROR\n parseError : ParseError \u2192 ERROR\n scopeError : ScopeError \u2192 ERROR\n runtimeError : RuntimeError \u2192 ERROR\n\n\nuglyTypeError : TypeError \u2192 String\nuglyTypeError (kindMismatch K K' x) = \"kindMismatch\"\nuglyTypeError (notStar K x) = \"notStar\"\nuglyTypeError (notFunKind K x) = \"NotFunKind\"\nuglyTypeError (notPat K x) = \"notPat\"\nuglyTypeError UnknownType = \"UnknownType\"\nuglyTypeError (notPi A x) = \"notPi\"\nuglyTypeError (notMu A x) = \"notMu\"\nuglyTypeError (notFunType A x) = \"notFunType\"\nuglyTypeError (typeMismatch A A' x) =\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A))\n ++\n \" doesn't match \"\n ++\n prettyPrintTy (extricateScopeTy (extricateNf\u22c6 A'))\nuglyTypeError builtinError = \"builtinError\"\n\n-- the haskell version of Error is defined in Raw\n{-# FOREIGN GHC import Raw #-}\n\n{-# COMPILE GHC ERROR = data ERROR (TypeError | ParseError | ScopeError | RuntimeError) #-}\n\nparsePLC : String \u2192 Either ERROR (ScopedTm Z)\nparsePLC plc = do\n namedprog \u2190 withE parseError $ parse plc\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnify namedprog\n withE scopeError $ scopeCheckTm {0}{Z} (shifter Z (convP prog))\n -- ^ FIXME: this should have an interface that guarantees that the\n -- shifter is run\n\nopen import Data.Empty\nparseUPLC : String \u2192 Either ERROR (\u22a5 U.\u22a2)\nparseUPLC plc = do\n namedprog \u2190 withE parseError $ parseU plc\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnifyU namedprog\n withE scopeError $ U.scopeCheckU0 (convPU prog)\n\ntypeCheckPLC : ScopedTm Z \u2192 Either TypeError (\u03a3 (\u2205 \u22a2Nf\u22c6 *) (\u2205 \u22a2_))\ntypeCheckPLC t = inferType _ t\n\n\nmaxsteps = 10000000000\n\nopen import Data.String\n\nreportError : ERROR \u2192 String\nreportError (parseError _) = \"parseError\"\nreportError (typeError s) = \"typeError: \" ++ s\nreportError (scopeError _) = \"scopeError\"\nreportError (runtimeError gasError) = \"gasError\"\nreportError (runtimeError userError) = \"userError\"\nreportError (runtimeError runtimeTypeError) = \"runtimeTypeError\"\n\n\nexecutePLC : EvalMode \u2192 ScopedTm Z \u2192 Either ERROR String\nexecutePLC U t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n \u25a1 V \u2190 withE runtimeError $ U.stepper maxsteps (\u03b5 \u037e [] \u25bb erase t)\n where \u25c6 \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n\n{-\njust t' \u2190 withE runtimeError $ U.stepper maxsteps (\u03b5 \u037e [] \u25bb erase t)\n where nothing \u2192 inj\u2081 (runtimeError userError)\n -}\n return $ prettyPrintUTm (U.extricateU0 (U.discharge V))\nexecutePLC TL t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n t' \u2190 withE runtimeError $ L.stepper t maxsteps\n return (prettyPrintTm (unshifter Z (extricateScope (extricate t'))))\nexecutePLC TCK t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n \u25a1 {t = t} v \u2190 withE runtimeError $ Algorithmic.CK.stepper maxsteps (\u03b5 \u25bb t)\n where \u25c6 _ \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return (prettyPrintTm (unshifter Z (extricateScope (extricate t))))\nexecutePLC TCEKV t = do\n (A ,, t) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) $ typeCheckPLC t\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CEKV.stepper maxsteps (\u03b5 \u037e [] \u25bb t)\n where \u25c6 _ \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return (prettyPrintTm (unshifter Z (extricateScope (extricate (Algorithmic.CEKV.discharge V)))))\n\nexecuteUPLC : \u22a5 U.\u22a2 \u2192 Either ERROR String\nexecuteUPLC t = do\n \u25a1 V \u2190 withE runtimeError $ U.stepper maxsteps (\u03b5 \u037e [] \u25bb t)\n where \u25c6 \u2192 inj\u2081 (runtimeError userError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return $ prettyPrintUTm (U.extricateU0 (U.discharge V))\n\nevalString : EvalMode \u2192 String \u2192 Either ERROR String\nevalString U b = do\n t \u2190 parseUPLC b\n executeUPLC t\nevalString m b = do\n{-\n -- some debugging code\n namedprog \u2190 withE parseError $ parse b\n prog \u2190 withE (ERROR.scopeError \u2218 freeVariableError) $ deBruijnify namedprog\n let shiftedprog = shifter Z (convP prog)\n scopedprog \u2190 withE scopeError $ scopeCheckTm {0}{Z} shiftedprog\n let extricatedprog = extricateScope scopedprog\n let unshiftedprog = unshifter Z extricatedprog\n return (\"orginal: \" ++ rawPrinter (convP prog) ++ \"\\n\" ++\n \"shifted: \" ++ rawPrinter shiftedprog ++ \"\\n\" ++\n \"instrinsically scoped: \" ++ Scoped.ugly scopedprog ++ \"\\n\" ++\n \"extricated: \" ++ rawPrinter extricatedprog ++ \"\\n\" ++\n \"unshifted: \" ++ rawPrinter unshiftedprog ++ \"\\n\" ++\n \"unconved: \" ++ prettyPrintTm unshiftedprog ++ \"\\n\")\n-}\n t \u2190 parsePLC b\n executePLC m t\n\ntypeCheckString : String \u2192 Either ERROR String\ntypeCheckString b = do\n t \u2190 parsePLC b\n (A ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e) ) $ typeCheckPLC t\n{-\n -- some debugging code\n let extricatedtype = extricateScopeTy (extricateNf\u22c6 A)\n let unshiftedtype = unshifterTy Z extricatedtype\n return (\"original: \" ++ \"???\" ++ \"\\n\" ++\n \"extricated: \" ++ rawTyPrinter extricatedtype ++ \"\\n\" ++\n \"unshifted: \" ++ rawTyPrinter unshiftedtype ++ \"\\n\" ++\n \"unconved: \" ++ prettyPrintTy unshiftedtype ++ \"\\n\")\n-}\n return (prettyPrintTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 A))))\n\njunk : \u2200{n} \u2192 Vec String n\njunk {zero} = []\njunk {Nat.suc n} = Data.Integer.Show.show (pos n) \u2237 junk\n\nblah : String \u2192 String \u2192 String\nblah plc1 plc2 with parseTm plc1 | parseTm plc2\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = rawPrinter (convTm plc1'') ++ \" || \" ++ rawPrinter (convTm plc2'')\nblah plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = \"deBruijnifying failed\"\nblah plc1 plc2 | _ | _ = \"parsing failed\"\n\n{-# COMPILE GHC blah as blah #-}\n\nprintTy : String \u2192 String\nprintTy b with parseTy b\n... | inj\u2081 _ = \"parseTy error\"\n... | inj\u2082 A with deBruijnifyTy A\n... | inj\u2081 _ = \"deBruinjifyTy error\"\n... | inj\u2082 A' = rawTyPrinter (convTy A')\n\n{-# COMPILE GHC printTy as printTy #-}\n\nalphaTy : String \u2192 String \u2192 Bool\nalphaTy plc1 plc2 with parseTy plc1 | parseTy plc2\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTy plc1' | deBruijnifyTy plc2'\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = decRTy (convTy plc1'') (convTy plc2'')\nalphaTy plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaTy plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTy as alphaTy #-}\n\nalphaTm : String \u2192 String \u2192 Bool\nalphaTm plc1 plc2 with parseTm plc1 | parseTm plc2\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTm plc1' | deBruijnifyTm plc2'\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = decRTm (convTm plc1'') (convTm plc2'')\nalphaTm plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaTm plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaTm as alphaTm #-}\n\nalphaU : String \u2192 String \u2192 Bool\nalphaU plc1 plc2 with parseTmU plc1 | parseTmU plc2\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' with deBruijnifyTmU plc1' | deBruijnifyTmU plc2'\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | inj\u2082 plc1'' | inj\u2082 plc2'' = U.decUTm (convTmU plc1'') (convTmU plc2'')\nalphaU plc1 plc2 | inj\u2082 plc1' | inj\u2082 plc2' | _ | _ = Bool.false\nalphaU plc1 plc2 | _ | _ = Bool.false\n\n{-# COMPILE GHC alphaU as alphaU #-}\n\n\n-- Opt stuff\n\n{-# FOREIGN GHC import Opts #-}\n\ndata Input : Set where\n FileInput : String \u2192 Input\n StdInput : Input\n\n{-# COMPILE GHC Input = data Input (FileInput | StdInput) #-}\n\ndata EvalOptions : Set where\n EvalOpts : Input \u2192 EvalMode \u2192 EvalOptions\n\n{-# COMPILE GHC EvalOptions = data EvalOptions (EvalOpts) #-}\n\ndata TCOptions : Set where\n TCOpts : Input \u2192 TCOptions\n\n{-# COMPILE GHC TCOptions = data TCOptions (TCOpts) #-}\n\ndata Command : Set where\n Evaluate : EvalOptions \u2192 Command\n TypeCheck : TCOptions \u2192 Command\n\n{-# COMPILE GHC Command = data Command (Evaluate | TypeCheck) #-}\n\npostulate execP : IO Command\n\n{-# COMPILE GHC execP = execP #-}\n\nevalInput : EvalMode \u2192 Input \u2192 IO (Either ERROR String)\nevalInput m (FileInput fn) = fmap (evalString m) (readFile fn)\nevalInput m StdInput = fmap (evalString m) getContents\n\ntcInput : Input \u2192 IO (Either ERROR String)\ntcInput (FileInput fn) = fmap typeCheckString (readFile fn)\ntcInput StdInput = fmap typeCheckString getContents\n\n\nmain' : Command \u2192 IO \u22a4\nmain' (Evaluate (EvalOpts i m)) = do\n inj\u2082 s \u2190 evalInput m i\n where\n inj\u2081 e \u2192 putStrLn (reportError e) >> exitFailure\n putStrLn s >> exitSuccess\nmain' (TypeCheck (TCOpts i)) = do\n inj\u2082 s \u2190 tcInput i\n where\n inj\u2081 e \u2192 putStrLn (reportError e) >> exitFailure\n putStrLn s >> exitSuccess\n\nmain : IO \u22a4\nmain = execP >>= main'\n\nliftSum : {A : Set} \u2192 Either ERROR A \u2192 Maybe A\nliftSum (inj\u2082 a) = just a\nliftSum (inj\u2081 e) = nothing\n\n\n-- a Haskell interface to the kindchecker:\ncheckKindX : Type \u2192 Kind \u2192 Either ERROR \u22a4\ncheckKindX ty k = do\n ty \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n (k' ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty)\n _ \u2190 withE ((\u03bb e \u2192 ERROR.typeError (uglyTypeError e)) \u2218 kindMismatch _ _) (meqKind k k')\n return tt\n\n{-# COMPILE GHC checkKindX as checkKindAgda #-}\n\n\n-- a Haskell interface to kind inference:\n\ninferKind\u2205 : Type \u2192 Either ERROR Kind\ninferKind\u2205 ty = do\n ty \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n (k ,, _) \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty)\n return k\n\n{-# COMPILE GHC inferKind\u2205 as inferKindAgda #-}\n\nopen import Type.BetaNormal\n\n-- a Haskell interface to the type normalizer:\nnormalizeType : Type \u2192 Either ERROR Type\nnormalizeType ty = do\n ty' \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n _ ,, n \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferKind \u2205 ty')\n return (unconvTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 n))))\n\n{-# COMPILE GHC normalizeType as normalizeTypeAgda #-}\n\n-- Haskell interface to type checker:\ninferType\u2205 : Term \u2192 Either ERROR Type\ninferType\u2205 t = do\n t' \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, _ \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 t')\n return (unconvTy (unshifterTy Z (extricateScopeTy (extricateNf\u22c6 ty))))\n\n{-# COMPILE GHC inferType\u2205 as inferTypeAgda #-}\n\n\n-- FIXME: we have a checkType function now...\ncheckType\u2205 : Type \u2192 Term \u2192 Either ERROR \u22a4\ncheckType\u2205 ty t = do\n ty' \u2190 withE scopeError (scopeCheckTy (shifterTy Z (convTy ty)))\n tyN \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (checkKind \u2205 ty' *)\n t' \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n withE (\u03bb e \u2192 typeError (uglyTypeError e)) (checkType \u2205 t' tyN)\n{-\n tyN' ,, tmC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 t')\n refl \u2190 withE ((\u03bb e \u2192 typeError (uglyTypeError e)) \u2218 kindMismatch _ _) (meqKind k *)\n refl \u2190 withE ((\u03bb e \u2192 typeError (uglyTypeError e)) \u2218 typeMismatch _ _) (meqNfTy tyN tyN')\n-}\n return _\n\n-- Haskell interface to type normalizer (for terms)\n-- the type checker\/inferer could also return such a term\nnormalizeTypeTerm : Term \u2192 Either ERROR Term\nnormalizeTypeTerm t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n _ ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n return (unconvTm (unshifter Z (extricateScope (extricate tC))))\n\n{-# COMPILE GHC normalizeTypeTerm as normalizeTypeTermAgda #-}\n\n\n{-# COMPILE GHC checkType\u2205 as checkTypeAgda #-}\n\n-- Haskell interface to (typechecked and proven correct) reduction\n\nrunTL : Term \u2192 Either ERROR Term\nrunTL t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n _ ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n t \u2190 withE runtimeError $ L.stepper tC maxsteps\n return (unconvTm (unshifter Z (extricateScope (extricate t))))\n\n{-# COMPILE GHC runTL as runTLAgda #-}\n\n-- note the interfaces to evaluation below catch userErrors and replace them with error terms\n\n-- Haskell interface to (typechecked) CK\nrunTCK : Term \u2192 Either ERROR Term\nrunTCK t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CK.stepper maxsteps (\u03b5 \u25bb tC)\n where (_ \u25bb _) \u2192 inj\u2081 (runtimeError gasError)\n (_ \u25c5 _) \u2192 inj\u2081 (runtimeError gasError)\n \u25c6 A \u2192 return (unconvTm (unshifter Z (extricateScope {0}{Z} (extricate (error ty)))))\n return (unconvTm (unshifter Z (extricateScope (extricate (Algorithmic.CK.discharge V)))))\n\n{-# COMPILE GHC runTCK as runTCKAgda #-}\n\n-- Haskell interface to (typechecked) CEKV\nrunTCEK : Term \u2192 Either ERROR Term\nrunTCEK t = do\n tDB \u2190 withE scopeError (scopeCheckTm {0}{Z} (shifter Z (convTm t)))\n ty ,, tC \u2190 withE (\u03bb e \u2192 typeError (uglyTypeError e)) (inferType \u2205 tDB)\n \u25a1 V \u2190 withE runtimeError $ Algorithmic.CEKV.stepper maxsteps (\u03b5 \u037e [] \u25bb tC)\n where (_ \u037e _ \u25bb _) \u2192 inj\u2081 (runtimeError gasError)\n (_ \u25c5 _) \u2192 inj\u2081 (runtimeError gasError)\n \u25c6 A \u2192 return (unconvTm (unshifter Z (extricateScope {0}{Z} (extricate (error ty)))))\n return (unconvTm (unshifter Z (extricateScope (extricate (Algorithmic.CEKV.discharge V)))))\n\n{-# COMPILE GHC runTCEK as runTCEKAgda #-}\n\npostulate showU : TermU -> String\n\n{-# COMPILE GHC showU = T.pack . show #-}\n\nrunU : TermU \u2192 Either ERROR TermU\nrunU t = do\n tDB \u2190 withE scopeError $ U.scopeCheckU0 (convTmU t)\n \u25a1 V \u2190 withE runtimeError $ U.stepper maxsteps (\u03b5 \u037e [] \u25bb tDB)\n where \u25c6 \u2192 return (unconvTmU U.UError)\n _ \u2192 inj\u2081 (runtimeError gasError)\n return (unconvTmU (U.extricateU0 (U.discharge V)))\n\n{-# COMPILE GHC runU as runUAgda #-}\n\\end{code}\n","avg_line_length":36.0053003534,"max_line_length":137,"alphanum_fraction":0.6940477943} +{"size":13358,"ext":"lagda","lang":"Literate Agda","max_stars_count":1299.0,"content":"\\begin{code}\nmodule Type.BetaNBE.RenamingSubstitution where\n\nopen import Utils\nopen import Type\nopen import Type.Equality\nopen import Type.RenamingSubstitution\nopen import Type.BetaNormal\nopen import Type.BetaNormal.Equality\nopen import Type.BetaNBE\nopen import Type.BetaNBE.Soundness\nopen import Type.BetaNBE.Completeness\nopen import Type.BetaNBE.Stability\n\nopen import Relation.Binary.PropositionalEquality hiding (subst; [_])\nopen import Function\n\\end{code}\n\n\nRenaming is defined in the file Type.BetaNormal as it used in the\nNBE algorithm.\n\nreify \u2218 reflect preserves the neutral term\n\n\\begin{code}\nreify-reflect : \u2200{K \u03a6}(n : \u03a6 \u22a2Ne\u22c6 K) \u2192 reify (reflect n) \u2261 ne n\nreify-reflect {*} n = refl\nreify-reflect {K \u21d2 J} n = refl\n\\end{code}\n\neval is closed under propositional equality for terms\n\n\\begin{code}\nevalCRSubst : \u2200{\u03a6 \u03a8 K}{\u03b7 \u03b7' : Env \u03a6 \u03a8}\n \u2192 EnvCR \u03b7 \u03b7'\n \u2192 {t t' : \u03a6 \u22a2\u22c6 K}\n \u2192 t \u2261 t'\n \u2192 CR K (eval t \u03b7) (eval t' \u03b7')\nevalCRSubst p {t = t} q = fund p (\u22612\u03b2 q) \n\\end{code}\n\n\\begin{code}\nren-nf : \u2200{\u03d5 \u03c8 K}(\u03c3 : Ren \u03d5 \u03c8)(A : \u03d5 \u22a2\u22c6 K) \u2192\n renNf \u03c3 (nf A) \u2261 nf (ren \u03c3 A)\nren-nf \u03c3 A = trans\n (ren-reify (idext idCR A) \u03c3)\n (reifyCR\n (transCR\n (renVal-eval A idCR \u03c3)\n (transCR\n (idext (renVal-reflect \u03c3 \u2218 `) A)\n (symCR (ren-eval A idCR \u03c3)) )))\n\\end{code}\n\n\\begin{code}\nren-nf-\u03bc : \u2200 {\u03a6 \u03a8}{K}\n \u2192 (\u03c1\u22c6 : Ren \u03a6 \u03a8)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *)\n \u2192 (B : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 renNf \u03c1\u22c6\n (nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B))\n \u2261\n nf\n (embNf (renNf \u03c1\u22c6 A) \u00b7 \u019b (\u03bc (embNf (weakenNf (renNf \u03c1\u22c6 A))) (` Z)) \u00b7\n embNf (renNf \u03c1\u22c6 B))\nren-nf-\u03bc \u03c1\u22c6 A B = trans\n (ren-nf \u03c1\u22c6 (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B))\n (trans\n (cong\u2082\n (\u03bb X Y \u2192 nf (X \u00b7 \u019b (\u03bc (ren (ext \u03c1\u22c6) (embNf (weakenNf A))) (` Z)) \u00b7 Y))\n (sym (ren-embNf \u03c1\u22c6 A))\n (sym (ren-embNf \u03c1\u22c6 B)))\n (trans\n (cong\n (\u03bb X \u2192 nf (embNf (renNf \u03c1\u22c6 A) \u00b7 \u019b (\u03bc (ren (ext \u03c1\u22c6) X) (` Z)) \u00b7 embNf (renNf \u03c1\u22c6 B)))\n (ren-embNf S A))\n (cong\n (\u03bb X \u2192 nf (embNf (renNf \u03c1\u22c6 A) \u00b7 \u019b (\u03bc X (` Z)) \u00b7 embNf (renNf \u03c1\u22c6 B)))\n (trans\n (sym (ren-comp (embNf A)))\n (trans (sym (ren-embNf (S \u2218 \u03c1\u22c6) A)) (cong embNf (renNf-comp A)))))))\n\\end{code}\n\n\\begin{code}\nSubNf : Ctx\u22c6 \u2192 Ctx\u22c6 \u2192 Set\nSubNf \u03c6 \u03a8 = \u2200 {J} \u2192 \u03c6 \u220b\u22c6 J \u2192 \u03a8 \u22a2Nf\u22c6 J\n\\end{code}\n\nSubstitution for normal forms:\n1. embed back into syntax;\n2. perform substitution;\n3. renormalize.\n\n\\begin{code}\nsubNf : \u2200 {\u03a6 \u03a8}\n \u2192 SubNf \u03a6 \u03a8\n -------------------------\n \u2192 (\u2200 {J} \u2192 \u03a6 \u22a2Nf\u22c6 J \u2192 \u03a8 \u22a2Nf\u22c6 J)\nsubNf \u03c1 n = nf (sub (embNf \u2218 \u03c1) (embNf n))\n\\end{code}\n\nFirst monad law for subNf\n\n\\begin{code}\nsubNf-id : \u2200 {\u03a6 J}\n \u2192 (n : \u03a6 \u22a2Nf\u22c6 J)\n \u2192 subNf (ne \u2218 `) n \u2261 n\nsubNf-id n = trans\n (reifyCR (fund idCR (\u22612\u03b2 (sub-id (embNf n)))))\n (stability n)\n\\end{code}\n\nThis version of the first monad law might be \u03b7 compatible as it doesn't rely\non sub-id\n\n\\begin{code}\nsubNf-id' : \u2200 {\u03a6 J}\n \u2192 (n : \u03a6 \u22a2Nf\u22c6 J)\n \u2192 subNf (nf \u2218 `) n \u2261 n\nsubNf-id' n = trans\n (reifyCR\n (transCR\n (sub-eval (embNf n) idCR (embNf \u2218 nf \u2218 `))\n (idext\n (\u03bb \u03b1 \u2192 fund idCR (\u22612\u03b2 (cong embNf (stability (ne (` \u03b1))))))\n (embNf n))))\n (stability n)\n\\end{code}\n\nSecond monad law for subNf\nThis is often holds definitionally for substitution (e.g. sub) but not here.\n\n\\begin{code}\nsubNf-\u220b : \u2200 {\u03a6 \u03a8 J}\n \u2192 (\u03c1 : SubNf \u03a6 \u03a8)\n \u2192 (\u03b1 : \u03a6 \u220b\u22c6 J)\n \u2192 subNf \u03c1 (ne (` \u03b1)) \u2261 \u03c1 \u03b1\nsubNf-\u220b \u03c1 \u03b1 = stability (\u03c1 \u03b1) \n\\end{code}\n\n\n\nTwo lemmas that aim to remove a superfluous additional normalisation\nvia stability\n\n\\begin{code}\nsubNf-nf : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c3 : \u2200 {J} \u2192 \u03a6 \u220b\u22c6 J \u2192 \u03a8 \u22a2Nf\u22c6 J)\n \u2192 \u2200 {J}\n \u2192 (t : \u03a6 \u22a2\u22c6 J)\n -------------------------------------------\n \u2192 nf (sub (embNf \u2218 \u03c3) t) \u2261 subNf \u03c3 (nf t)\nsubNf-nf \u03c3 t = trans\n (reifyCR (sub-eval t idCR (embNf \u2218 \u03c3)))\n (trans\n (sym\n (reifyCR (fund (\u03bb x \u2192 idext idCR (embNf (\u03c3 x))) (sym\u2261\u03b2 (soundness t)))))\n (sym (reifyCR (sub-eval (embNf (nf t)) idCR (embNf \u2218 \u03c3)))))\n\\end{code}\n\nThird Monad Law for subNf\n\n\\begin{code}\nsubNf-comp : \u2200{\u03a6 \u03a8 \u0398}\n (g : SubNf \u03a6 \u03a8)\n (f : SubNf \u03a8 \u0398)\n \u2192 \u2200{J}(A : \u03a6 \u22a2Nf\u22c6 J)\n -----------------------------------------------\n \u2192 subNf (subNf f \u2218 g) A \u2261 subNf f (subNf g A)\nsubNf-comp g f A = trans\n (trans\n (trans\n (reifyCR\n (sub-eval\n (embNf A)\n idCR\n (embNf \u2218 nf \u2218 sub (embNf \u2218 f) \u2218 embNf \u2218 g)))\n (trans (reifyCR\n (idext\n (\u03bb x \u2192 fund\n idCR\n (sym\u2261\u03b2 (soundness (sub (embNf \u2218 f) (embNf (g x))))))\n (embNf A)))\n (sym\n (reifyCR\n (sub-eval\n (embNf A)\n idCR\n (sub (embNf \u2218 f) \u2218 embNf \u2218 g))))))\n (completeness (\u22612\u03b2 (sub-comp (embNf A)))))\n (subNf-nf f (sub (embNf \u2218 g) (embNf A)))\n\\end{code}\n\nextending a normal substitution\n\n\\begin{code}\nextsNf : \u2200 {\u03a6 \u03a8}\n \u2192 SubNf \u03a6 \u03a8\n -------------------------------\n \u2192 \u2200 {K} \u2192 SubNf (\u03a6 ,\u22c6 K) (\u03a8 ,\u22c6 K)\nextsNf \u03c3 Z = ne (` Z)\nextsNf \u03c3 (S \u03b1) = weakenNf (\u03c3 \u03b1)\n\\end{code}\n\ncons for normal substitutions\n\n\\begin{code}\nsubNf-cons : \u2200{\u03a6 \u03a8}\n \u2192 (\u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 \u2200{J}(A : \u03a8 \u22a2Nf\u22c6 J)\n \u2192 (\u2200{K} \u2192 \u03a6 ,\u22c6 J \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\nsubNf-cons \u03c3 A Z = A\nsubNf-cons \u03c3 A (S x) = \u03c3 x\n\\end{code}\n\nSubstitution of one variable\n\n\\begin{code}\n_[_]Nf : \u2200 {\u03a6 J K}\n \u2192 \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J\n \u2192 \u03a6 \u22a2Nf\u22c6 K \n ------\n \u2192 \u03a6 \u22a2Nf\u22c6 J\nA [ B ]Nf = subNf (subNf-cons (ne \u2218 `) B) A\n\\end{code}\n\nCongruence lemma for sub\n\\begin{code}\nsubNf-cong : \u2200 {\u03a6 \u03a8}\n \u2192 {f g : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K}\n \u2192 (\u2200 {J}(x : \u03a6 \u220b\u22c6 J) \u2192 f x \u2261 g x)\n \u2192 \u2200{K}(A : \u03a6 \u22a2Nf\u22c6 K)\n -------------------------------\n \u2192 subNf f A \u2261 subNf g A\nsubNf-cong p A =\n reifyCR (fund idCR (\u22612\u03b2 (sub-cong (cong embNf \u2218 p ) (embNf A))))\n\\end{code}\n\n\\begin{code}\nsubNf-cong' : \u2200 {\u03a6 \u03a8}\n \u2192 (f : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 \u2200{K}{A A' : \u03a6 \u22a2Nf\u22c6 K}\n \u2192 A \u2261 A'\n -------------------------------\n \u2192 subNf f A \u2261 subNf f A'\nsubNf-cong' f p = cong (subNf f) p\n\\end{code}\n\nPushing renaming through normal substitution\n\n\\begin{code}\nrenNf-subNf : \u2200{\u03a6 \u03a8 \u0398}\n \u2192 (g : SubNf \u03a6 \u03a8)\n \u2192 (f : Ren \u03a8 \u0398)\n \u2192 \u2200{J}(A : \u03a6 \u22a2Nf\u22c6 J)\n -----------------------------------------------------\n \u2192 subNf (renNf f \u2218 g) A \u2261 renNf f (subNf g A)\nrenNf-subNf g f A = trans\n (reifyCR\n (transCR\n (transCR\n (sub-eval (embNf A) idCR (embNf \u2218 renNf f \u2218 g))\n (transCR\n (idext\n (\u03bb \u03b1 \u2192 transCR\n (evalCRSubst idCR (ren-embNf f (g \u03b1)))\n (transCR\n (ren-eval (embNf (g \u03b1)) idCR f)\n (idext (symCR \u2218 renVal-reflect f \u2218 `) (embNf (g \u03b1)))))\n (embNf A))\n (symCR (sub-eval (embNf A) (renCR f \u2218 idCR) (embNf \u2218 g)))))\n (symCR (renVal-eval (sub (embNf \u2218 g) (embNf A)) idCR f))))\n (sym (ren-reify (idext idCR (sub (embNf \u2218 g) (embNf A))) f))\n\\end{code}\n\nPushing a substitution through a renaming\n\n\\begin{code}\nsubNf-renNf : \u2200{\u03a6 \u03a8 \u0398}\n \u2192 (g : Ren \u03a6 \u03a8)\n \u2192 (f : SubNf \u03a8 \u0398)\n \u2192 \u2200{J}(A : \u03a6 \u22a2Nf\u22c6 J)\n --------------------------------------\n \u2192 subNf (f \u2218 g) A \u2261 subNf f (renNf g A)\nsubNf-renNf g f A = reifyCR\n (transCR\n (sub-eval (embNf A) idCR (embNf \u2218 f \u2218 g))\n (transCR\n (transCR\n (symCR (ren-eval (embNf A) (\u03bb \u03b1 \u2192 idext idCR (embNf (f \u03b1))) g))\n (symCR\n (evalCRSubst (\u03bb \u03b1 \u2192 idext idCR (embNf (f \u03b1))) (ren-embNf g A))))\n (symCR (sub-eval (embNf (renNf g A)) idCR (embNf \u2218 f)))))\n\\end{code}\n\nPushing renaming through a one variable normal substitution\n\n\\begin{code}\nren[]Nf : \u2200 {\u03a6 \u0398 J K}\n \u2192 (\u03c1 : Ren \u03a6 \u0398)\n \u2192 (t : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J)\n \u2192 (u : \u03a6 \u22a2Nf\u22c6 K )\n --------------------------------------------------------------\n \u2192 renNf \u03c1 (t [ u ]Nf) \u2261 (renNf (ext \u03c1) t [ renNf \u03c1 u ]Nf)\nren[]Nf \u03c1 t u = trans\n (sym (renNf-subNf (subNf-cons (ne \u2218 `) u) \u03c1 t))\n (trans\n (subNf-cong\n {f = renNf \u03c1 \u2218 subNf-cons (ne \u2218 `) u}\n {g = subNf-cons (ne \u2218 `) (renNf \u03c1 u) \u2218 ext \u03c1}\n (\u03bb { Z \u2192 refl ; (S \u03b1) \u2192 refl})\n t)\n (subNf-renNf (ext \u03c1)(subNf-cons (ne \u2218 `) (renNf \u03c1 u)) t))\n\\end{code}\n\nPushing a normal substitution through a one place normal substitution\n\n\\begin{code}\nsub[]Nf : \u2200{\u03a6 \u03a8 K J}\n \u2192 (\u03c1 : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 (B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J)\n --------------------------------------------------------------\n \u2192 subNf \u03c1 (B [ A ]Nf) \u2261 (subNf (extsNf \u03c1) B [ subNf \u03c1 A ]Nf)\nsub[]Nf \u03c1 A B = trans\n (sym (subNf-comp (subNf-cons (ne \u2218 `) A) \u03c1 B))\n (trans\n (subNf-cong\n {f = subNf \u03c1 \u2218 subNf-cons (ne \u2218 `) A}\n {g = subNf (subNf-cons (ne \u2218 `) (subNf \u03c1 A)) \u2218 extsNf \u03c1}\n (\u03bb { Z \u2192 sym (subNf-\u220b (subNf-cons (ne \u2218 `) (subNf \u03c1 A)) Z) \n ; (S \u03b1) \u2192 trans\n (trans (subNf-\u220b \u03c1 \u03b1) (sym (subNf-id (\u03c1 \u03b1))))\n (subNf-renNf\n S\n (subNf-cons (ne \u2218 `) (subNf \u03c1 A))\n (\u03c1 \u03b1))})\n B)\n (subNf-comp (extsNf \u03c1) (subNf-cons (ne \u2218 `) (subNf \u03c1 A)) B))\n\\end{code}\n\nExtending a normal environment and then embedding is the same as\nembedding and then extending.\n\n\\begin{code}\nsubNf-lemma : \u2200{\u03a6 \u03a8 K J}\n (\u03c1 : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 (t : \u03a6 ,\u22c6 K \u22a2\u22c6 J)\n \u2192 sub (exts (embNf \u2218 \u03c1)) t \u2261 sub (embNf \u2218 extsNf \u03c1) t\nsubNf-lemma \u03c1 t =\n sub-cong (\u03bb { Z \u2192 refl ; (S x) \u2192 sym (ren-embNf S (\u03c1 x))}) t\n\\end{code}\n\nRepair a mismatch between two different ways of extending an environment\n\n\\begin{code}\nsubNf-lemma' : \u2200{\u03a6 K J}\n \u2192 (B : \u03a6 ,\u22c6 K \u22a2\u22c6 J)\n \u2192 nf B \u2261 reify (eval B ((renVal S \u2218 idEnv _) ,,\u22c6 fresh))\nsubNf-lemma' B = reifyCR\n (idext (\u03bb { Z \u2192 reflectCR refl\n ; (S x) \u2192 symCR (renVal-reflect S (` x))}) B)\n\\end{code}\n\ncombining the above lemmas\n\nnote: there are several mismatches here, one due to two different ways\nof extending a normal substitution and another due to two different\nways of extending an environment\n\n\\begin{code}\nsub[]Nf' : \u2200{\u03a6 \u03a8 K J}\n \u2192 (\u03c1 : \u2200{K} \u2192 \u03a6 \u220b\u22c6 K \u2192 \u03a8 \u22a2Nf\u22c6 K)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 (B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 J)\n \u2192 subNf \u03c1 (B [ A ]Nf)\n \u2261\n ((reify (eval (sub (exts (embNf \u2218 \u03c1)) (embNf B))\n ((renVal S \u2218 idEnv _) ,,\u22c6 fresh)))\n [ subNf \u03c1 A ]Nf)\nsub[]Nf' \u03c1 A B =\n trans (sub[]Nf \u03c1 A B)\n (subNf-cong' (subNf-cons (ne \u2218 `) (subNf \u03c1 A))\n {A = subNf (extsNf \u03c1) B}\n {A' =\n reify\n (eval (sub (exts (embNf \u2218 \u03c1)) (embNf B))\n ((renVal S \u2218 idEnv _) ,,\u22c6 fresh))}\n (trans (sym (completeness (\u22612\u03b2 (subNf-lemma \u03c1 (embNf B)))))\n (subNf-lemma' (sub (exts (embNf \u2218 \u03c1)) (embNf B)))))\n\\end{code}\n\n\\begin{code}\nweakenNf-renNf : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c1\u22c6 : Ren \u03a6 \u03a8)\n \u2192 \u2200{K}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 weakenNf (renNf \u03c1\u22c6 A) \u2261 renNf (ext \u03c1\u22c6 {K = K}) (weakenNf A)\nweakenNf-renNf \u03c1\u22c6 A = trans (sym (renNf-comp _)) (renNf-comp _)\n\nweakenNf-subNf : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c3\u22c6 : SubNf \u03a6 \u03a8)\n \u2192 \u2200{K}\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 weakenNf (subNf \u03c3\u22c6 A) \u2261 subNf (extsNf \u03c3\u22c6 {K = K}) (weakenNf A)\nweakenNf-subNf \u03c3\u22c6 A = trans\n (sym (renNf-subNf \u03c3\u22c6 S A))\n (subNf-renNf S (extsNf \u03c3\u22c6) A)\n\nweakenNf[] : \u2200 {\u03a6 K}(B : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 *)\n \u2192 A \u2261 (weakenNf A [ B ]Nf)\nweakenNf[] B A = trans\n (trans (sym (stability A))\n (evalCRSubst idCR (sym (sub-id (embNf A)))))\n (subNf-renNf S (subNf-cons (ne \u2218 `) B) A)\n\nopen import Data.Sum\n\nsub-nf-\u03a0 : \u2200 {\u03a6 \u03a8}\n \u2192 (\u03c3\u22c6 : SubNf \u03a6 \u03a8)\n \u2192 \u2200{K}\n \u2192 (B : \u03a6 ,\u22c6 K \u22a2Nf\u22c6 *)\n \u2192 subNf (extsNf \u03c3\u22c6) B\n \u2261\n eval (sub (exts (embNf \u2218 \u03c3\u22c6)) (embNf B)) (exte (idEnv \u03a8))\nsub-nf-\u03a0 \u03c3\u22c6 B = trans\n (evalCRSubst idCR (sym (subNf-lemma \u03c3\u22c6 (embNf B))))\n (subNf-lemma' (sub (exts (embNf \u2218 \u03c3\u22c6)) (embNf B)))\n\nsub-nf-\u03bc : \u2200 {\u03a6 \u03a8}{K}\n \u2192 (\u03c3\u22c6 : SubNf \u03a6 \u03a8)\n \u2192 (A : \u03a6 \u22a2Nf\u22c6 (K \u21d2 *) \u21d2 K \u21d2 *)\n \u2192 (B : \u03a6 \u22a2Nf\u22c6 K)\n \u2192 subNf \u03c3\u22c6 (nf (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B))\n \u2261\n nf\n (embNf (subNf \u03c3\u22c6 A) \u00b7\n \u019b (\u03bc (embNf (weakenNf (subNf \u03c3\u22c6 A))) (` Z))\n \u00b7 embNf (subNf \u03c3\u22c6 B))\nsub-nf-\u03bc \u03c3\u22c6 A B = trans\n (sym (subNf-nf \u03c3\u22c6 (embNf A \u00b7 \u019b (\u03bc (embNf (weakenNf A)) (` Z)) \u00b7 embNf B)))\n (completeness\n {s = sub (embNf \u2218 \u03c3\u22c6) (embNf A) \u00b7 \u019b (\u03bc (sub (exts (embNf \u2218 \u03c3\u22c6)) (embNf (weakenNf A))) (` Z)) \u00b7 sub (embNf \u2218 \u03c3\u22c6) (embNf B)}\n {(embNf (subNf \u03c3\u22c6 A) \u00b7 \u019b (\u03bc (embNf (weakenNf (subNf \u03c3\u22c6 A))) (` Z)) \u00b7 embNf (subNf \u03c3\u22c6 B))}\n (\u00b7\u2261\u03b2\n (\u00b7\u2261\u03b2\n (soundness (sub (embNf \u2218 \u03c3\u22c6) (embNf A)))\n (\u019b\u2261\u03b2 (\u03bc\u2261\u03b2\n (trans\u2261\u03b2\n (trans\u2261\u03b2\n (\u22612\u03b2 (cong (sub (exts (embNf \u2218 \u03c3\u22c6))) (ren-embNf S A)))\n (trans\u2261\u03b2\n (\u22612\u03b2 (sym (sub-ren (embNf A))))\n (trans\u2261\u03b2\n (soundness (sub (weaken \u2218 embNf \u2218 \u03c3\u22c6) (embNf A)))\n (\u22612\u03b2\n (cong embNf {nf (sub (weaken \u2218 embNf \u2218 \u03c3\u22c6) (embNf A))}{subNf (renNf S \u2218 \u03c3\u22c6) A}\n (cong nf (sub-cong (sym \u2218 ren-embNf S \u2218 \u03c3\u22c6) (embNf A))))))))\n (\u22612\u03b2 (cong embNf (renNf-subNf \u03c3\u22c6 S A))))\n (refl\u2261\u03b2 (` Z)))))\n (soundness (sub (embNf \u2218 \u03c3\u22c6) (embNf B)))))\n\\end{code}\n\n\\begin{code}\nsubNf-cons-[]Nf : \u2200{\u03a6 K \u03a8'}{\u03c3 : SubNf \u03a8' \u03a6}{A : \u03a6 \u22a2Nf\u22c6 K}(X : \u03a8' ,\u22c6 K \u22a2Nf\u22c6 *) \u2192 \n subNf (subNf-cons \u03c3 A) X\n \u2261\n reify (eval (sub (exts (embNf \u2218 \u03c3)) (embNf X)) (exte (idEnv \u03a6))) [ A ]Nf\nsubNf-cons-[]Nf {\u03c3 = \u03c3}{A} X = trans\n (trans (subNf-cong {f = subNf-cons \u03c3 A}{g = subNf (subNf-cons (ne \u2218 `) A) \u2218 extsNf \u03c3} (\u03bb {Z \u2192 sym (stability A) ; (S \u03b1) \u2192 trans (trans (sym (stability (\u03c3 \u03b1))) (cong nf (sym (sub-id (embNf (\u03c3 \u03b1)))))) (subNf-renNf S (subNf-cons (ne \u2218 `) A) (\u03c3 \u03b1)) }) X)\n (subNf-comp (extsNf \u03c3)\n (subNf-cons (ne \u2218 `) A)\n X))\n (cong (_[ A ]Nf)\n (sub-nf-\u03a0 \u03c3 X))\n\\end{code}\n","avg_line_length":27.9456066946,"max_line_length":252,"alphanum_fraction":0.488171882} +{"size":13695,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Scoped.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Scoped\nopen import Scoped.RenamingSubstitution\nopen import Builtin\nopen import Builtin.Constant.Type\n\nopen import Utils\n\nopen import Data.Sum renaming (inj\u2081 to inl; inj\u2082 to inr)\nopen import Data.Product\nopen import Data.List hiding ([_]; drop; take)\nopen import Function\nopen import Data.Integer as I\nopen import Data.Nat as N hiding (_?_;_\u2265?_)\nopen import Relation.Nullary\nopen import Relation.Binary.PropositionalEquality hiding ([_];trans)\nimport Data.Bool as B\n\\end{code}\n\n\\begin{code}\ninfix 2 _\u2014\u2192_\n\\end{code}\n\n\\begin{code}\ndata Value {n}{w : Weird\u2115 n} : ScopedTm w \u2192 Set where\n V-\u019b : \u2200 (A : ScopedTy n)(t : ScopedTm (S w)) \u2192 Value (\u019b A t)\n V-\u039b : \u2200 {K}(t : ScopedTm (T w)) \u2192 Value (\u039b K t)\n V-con : (tcn : TermCon) \u2192 Value (con {n} tcn)\n V-wrap : (A B : ScopedTy n){t : ScopedTm w} \u2192 Value t \u2192 Value (wrap A B t)\n V-builtin : (b : Builtin)\n (As : List (ScopedTy n))\n (ts : List (ScopedTm w))\n \u2192 Value (builtin b As ts)\n\nTel : \u2200{n} \u2192 Weird\u2115 n \u2192 Set\nTel w = List (ScopedTm w)\n\nopen import Data.Unit\nVTel : \u2200{n}(w : Weird\u2115 n) \u2192 Tel w \u2192 Set\nVTel w [] = \u22a4\nVTel w (t \u2237 ts) = Value t \u00d7 VTel w ts\n\n-- a term that satisfies this predicate has an error term in it somewhere\n-- or we encountered a rumtime type error\ndata Error {n}{w : Weird\u2115 n} : ScopedTm w \u2192 Set where\n -- a genuine runtime error returned from a builtin\n E-error : (A : ScopedTy n) \u2192 Error (error A)\n\n -- error inside somewhere\n E-\u00b7\u2081 : {L M : ScopedTm w} \u2192 Error L \u2192 Error (L \u00b7 M)\n E-\u00b7\u2082 : {L M : ScopedTm w} \u2192 Error M \u2192 Error (L \u00b7 M)\n \n E-\u00b7\u22c6 : {L : ScopedTm w}{A : ScopedTy n} \u2192 Error L \u2192 Error (L \u00b7\u22c6 A)\n E-\u039b : \u2200{K}{L : ScopedTm (T w)} \u2192 Error L \u2192 Error (\u039b K L)\n\n E-unwrap : {L : ScopedTm w} \u2192 Error L \u2192 Error (unwrap L)\n E-wrap : {A B : ScopedTy n}{L : ScopedTm w} \u2192 Error L \u2192 Error (wrap A B L)\n \n -- runtime type errors\n -- these couldn't happen in the intrinsically typed version\n E-\u039b\u00b7 : \u2200{K}{L : ScopedTm (T w)}{M : ScopedTm w} \u2192 Error (\u039b K L \u00b7 M)\n E-\u019b\u00b7\u22c6 : \u2200{B : ScopedTy n}{L : ScopedTm (S w)}{A : ScopedTy n}\n \u2192 Error (\u019b B L \u00b7\u22c6 A)\n E-con\u00b7 : \u2200{tcn}{M : ScopedTm w} \u2192 Error (con tcn \u00b7 M)\n E-con\u00b7\u22c6 : \u2200{tcn}{A : ScopedTy n} \u2192 Error (con tcn \u00b7\u22c6 A)\n E-wrap\u00b7 : {A B : ScopedTy n}{t M : ScopedTm w} \u2192 Error (wrap A B t \u00b7 M)\n E-wrap\u00b7\u22c6 : {A' B A : ScopedTy n}{t : ScopedTm w}\n \u2192 Error (wrap A' B t \u00b7\u22c6 A)\n E-\u019bunwrap : {A : ScopedTy n}{t : ScopedTm (S w)}\n \u2192 Error (unwrap (\u019b A t) )\n E-\u039bunwrap : \u2200{K}{t : ScopedTm (T w)} \u2192 Error (unwrap (\u039b K t))\n E-conunwrap : \u2200{tcn} \u2192 Error (unwrap (con tcn))\n\n -- this stuff is required due to unsaturated builtins in term args only\n E-builtin\u00b7\u22c6 : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {A : ScopedTy n}\n \u2192 Error (builtin b As ts \u00b7\u22c6 A)\n\n E-builtinunwrap : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n \u2192 Error (unwrap (builtin b As ts))\n\n -- an error occured in one of reducing an argument\n E-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {t : ScopedTm w}\n \u2192 Error t\n \u2192 Error (builtin b As ts)\n\nVERIFYSIG : \u2200{n}{w : Weird\u2115 n} \u2192 Maybe B.Bool \u2192 ScopedTm w\nVERIFYSIG (just B.false) = false\nVERIFYSIG (just B.true) = true\nVERIFYSIG nothing = error ScopedBoolean\n\n\nBUILTIN : \u2200{n}{w : Weird\u2115 n} \u2192 Builtin\n \u2192 List (ScopedTy n) \u2192 (ts : Tel w) \u2192 VTel w ts \u2192 ScopedTm w\nBUILTIN addInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.+ i'))\nBUILTIN addInteger _ _ _ = error (con integer)\nBUILTIN subtractInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.- i'))\nBUILTIN subtractInteger _ _ _ = error (con integer)\nBUILTIN multiplyInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n con (integer (i I.* i'))\nBUILTIN multiplyInteger _ _ _ = error (con integer)\nBUILTIN divideInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (div i i')))\nBUILTIN divideInteger _ _ _ = error (con integer)\nBUILTIN quotientInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (quot i i')))\nBUILTIN quotientInteger _ _ _ = error (con integer)\nBUILTIN remainderInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (rem i i')))\nBUILTIN remainderInteger _ _ _ = error (con integer)\nBUILTIN modInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (\u2223 i' \u2223 N.\u225f 0) (error (con integer)) (con (integer (mod i i')))\nBUILTIN modInteger _ _ _ = error (con integer)\n-- Int -> Int -> Bool\nBUILTIN lessThanInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i'), tt) =\n decIf (i ? i') true false\nBUILTIN greaterThanInteger _ _ _ = error ScopedBoolean\nBUILTIN greaterThanEqualsInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (i \u2265? i') true false\nBUILTIN greaterThanEqualsInteger _ _ _ = error ScopedBoolean\nBUILTIN equalsInteger _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer i') , tt) =\n decIf (i I.\u225f i') true false\nBUILTIN equalsInteger _ _ _ = error ScopedBoolean\n-- BS -> BS -> BS\nBUILTIN concatenate _ (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) = con (bytestring (append b b'))\nBUILTIN concatenate _ _ _ = error (con bytestring)\n-- Int -> BS -> BS\nBUILTIN takeByteString _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) = con (bytestring (take i b))\nBUILTIN takeByteString _ _ _ = error (con bytestring)\nBUILTIN dropByteString _ (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) = con (bytestring (drop i b))\nBUILTIN dropByteString _ _ _ = error (con bytestring)\n-- BS -> BS\nBUILTIN sha2-256 _ (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA2-256 b))\nBUILTIN sha2-256 _ _ _ = error (con bytestring)\nBUILTIN sha3-256 _ (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA3-256 b))\nBUILTIN sha3-256 _ _ _ = error (con bytestring)\nBUILTIN verifySignature _ (_ \u2237 _ \u2237 _ \u2237 []) (V-con (bytestring k) , V-con (bytestring d) , V-con (bytestring c) , tt) = VERIFYSIG (verifySig k d c)\nBUILTIN verifySignature _ _ _ = error (con bytestring)\n-- Int -> Int\nBUILTIN equalsByteString _ (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n B.if equals b b' then true else false\nBUILTIN equalsByteString _ _ _ = error ScopedBoolean\n\ndata _\u2014\u2192_ {n}{w : Weird\u2115 n} : ScopedTm w \u2192 ScopedTm w \u2192 Set where\n \u03be-\u00b7\u2081 : {L L' M : ScopedTm w} \u2192 L \u2014\u2192 L' \u2192 L \u00b7 M \u2014\u2192 L' \u00b7 M\n \u03be-\u00b7\u2082 : {L M M' : ScopedTm w} \u2192 Value L \u2192 M \u2014\u2192 M' \u2192 L \u00b7 M \u2014\u2192 L \u00b7 M'\n \u03be-\u00b7\u22c6 : {L L' : ScopedTm w}{A : ScopedTy n} \u2192 L \u2014\u2192 L' \u2192 L \u00b7\u22c6 A \u2014\u2192 L' \u00b7\u22c6 A\n \u03be-wrap : {A B : ScopedTy n}{L L' : ScopedTm w}\n \u2192 L \u2014\u2192 L' \u2192 wrap A B L \u2014\u2192 wrap A B L'\n \u03b2-\u019b : \u2200{A : ScopedTy n}{L : ScopedTm (S w)}{M : ScopedTm w}\n \u2192 (\u019b A L) \u00b7 M \u2014\u2192 (L [ M ])\n \u03b2-\u039b : \u2200{K}{L : ScopedTm (T w)}{A : ScopedTy n}\n \u2192 (\u039b K L) \u00b7\u22c6 A \u2014\u2192 (L [ A ]\u22c6)\n \u03be-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {tel : Tel w}\n {telA : Tel w}\n (vs : VTel w telA)\n {t t' : ScopedTm w}\n \u2192 t \u2014\u2192 t'\n \u2192 (telB : List (ScopedTm w))\n \u2192 tel \u2261 telA ++ Data.List.[ t ] ++ telB\n \u2192 builtin b As tel \u2014\u2192 builtin b As (telA ++ Data.List.[ t' ] ++ telB)\n \u03b2-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : Tel w}\n (vs : VTel w ts)\n \u2192 builtin b As ts \u2014\u2192 BUILTIN b As ts vs\n sat-builtin : {b : Builtin}\n {As : List (ScopedTy n)}\n {ts : List (ScopedTm w)}\n {t : ScopedTm w}\n \u2192 builtin b As ts \u00b7 t \u2014\u2192 builtin b As (ts ++ Data.List.[ t ])\n\n \u03be-unwrap : {t t' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 unwrap t \u2014\u2192 unwrap t'\n \u03b2-wrap : {A B : ScopedTy n}{t : ScopedTm w} \u2192 unwrap (wrap A B t) \u2014\u2192 t\n\\end{code}\n\n\\begin{code}\ndata _\u2014\u2192\u22c6_ {n}{w : Weird\u2115 n} : ScopedTm w \u2192 ScopedTm w \u2192 Set where\n refl : {t : ScopedTm w} \u2192 t \u2014\u2192\u22c6 t\n trans : {t t' t'' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 t' \u2014\u2192\u22c6 t'' \u2192 t \u2014\u2192\u22c6 t''\n\\end{code}\n\n\\begin{code}\ndata Progress {n}{i : Weird\u2115 n}(t : ScopedTm i) : Set where\n step : \u2200{t'} \u2192 t \u2014\u2192 t' \u2192 Progress t\n done : Value t \u2192 Progress t\n error : Error t \u2192 Progress t\n \ndata TelProgress {n}{w : Weird\u2115 n} : Tel w \u2192 Set where\n done : (tel : Tel w)(vtel : VTel w tel) \u2192 TelProgress tel\n step : (tel : Tel w)(telA : Tel w)(vtelA : VTel w telA)\n \u2192 {t t' : ScopedTm w} \u2192 t \u2014\u2192 t' \u2192 (telB : Tel w) \u2192 tel \u2261 telA ++ Data.List.[ t ] ++ telB \u2192 TelProgress tel\n error : (tel : Tel w)(telA : Tel w)(vtelA : VTel w telA){t : ScopedTm w}\n \u2192 Error t \u2192 (telB : Tel w) \u2192 TelProgress tel\n\\end{code}\n\n\\begin{code}\nprogress\u00b7 : \u2200{n}{i : Weird\u2115 n}{t : ScopedTm i}\n \u2192 Progress t \u2192 (u : ScopedTm i) \u2192 Progress (t \u00b7 u)\nprogress\u00b7 (step p) u = step (\u03be-\u00b7\u2081 p)\nprogress\u00b7 (done (V-\u019b A t)) u = step \u03b2-\u019b\nprogress\u00b7 (done (V-\u039b p)) u = error E-\u039b\u00b7\nprogress\u00b7 (done (V-con tcn)) u = error E-con\u00b7\nprogress\u00b7 (done (V-wrap A B t)) u = error E-wrap\u00b7\nprogress\u00b7 (done (V-builtin b As ts)) u = step sat-builtin\nprogress\u00b7 (error e) u = error (E-\u00b7\u2081 e)\n\nprogress\u00b7\u22c6 : \u2200{n}{i : Weird\u2115 n}{t : ScopedTm i}\n \u2192 Progress t \u2192 (A : ScopedTy n) \u2192 Progress (t \u00b7\u22c6 A)\nprogress\u00b7\u22c6 (step p) A = step (\u03be-\u00b7\u22c6 p)\nprogress\u00b7\u22c6 (done (V-\u019b B t)) A = error E-\u019b\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-\u039b p)) A = step \u03b2-\u039b\nprogress\u00b7\u22c6 (done (V-con tcn)) A = error E-con\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-wrap pat arg t)) A = error E-wrap\u00b7\u22c6\nprogress\u00b7\u22c6 (done (V-builtin b As ts)) A = error E-builtin\u00b7\u22c6\nprogress\u00b7\u22c6 (error e) A = error (E-\u00b7\u22c6 e)\n\nprogress-unwrap : \u2200{n}{i : Weird\u2115 n}{t : ScopedTm i}\n \u2192 Progress t \u2192 Progress (unwrap t)\nprogress-unwrap (step p) = step (\u03be-unwrap p)\nprogress-unwrap (done (V-\u019b A t)) = error E-\u019bunwrap\nprogress-unwrap (done (V-\u039b p)) = error E-\u039bunwrap\nprogress-unwrap (done (V-con tcn)) = error E-conunwrap\nprogress-unwrap (done (V-wrap A B t)) = step \u03b2-wrap\nprogress-unwrap (done (V-builtin b As ts)) = error E-builtinunwrap\nprogress-unwrap (error e) = error (E-unwrap e)\n\nprogress-builtin : \u2200 {n}{i : Weird\u2115 n} bn\n \u2192 (As : List (ScopedTy n)) (tel : Tel i)\n \u2192 TelProgress tel \u2192 Progress (builtin bn As tel)\nprogress-builtin bn As tel p with arity bn N.\u225f Data.List.length tel\nprogress-builtin bn As tel (done .tel vtel) | yes p =\n step (\u03b2-builtin vtel)\nprogress-builtin bn As tel (step .tel telA vtelA x telB q) | yes p =\n step (\u03be-builtin vtelA x telB q)\nprogress-builtin bn As tel (error .tel telA vtelA x telB) | yes p =\n error (E-builtin x)\nprogress-builtin bn As tel p | no \u00acp = done (V-builtin bn As tel)\n\nprogressTelCons : \u2200{n}{i : Weird\u2115 n}{t : ScopedTm i}\n \u2192 Progress t \u2192 {tel : Tel i} \u2192 TelProgress tel \u2192 TelProgress (t \u2237 tel)\nprogressTelCons {t = t}(step p){tel} q = step (t \u2237 tel) [] tt p tel refl\nprogressTelCons (done v) (done tel vtel) = done (_ \u2237 tel) (v , vtel)\nprogressTelCons (done v) (step tel telA vtelA p telB q) =\n step (_ \u2237 tel) (_ \u2237 telA) (v , vtelA) p telB (cong (_ \u2237_) q)\nprogressTelCons (done v) (error tel telA vtelA p telB) =\n error (_ \u2237 tel) (_ \u2237 telA) (v , vtelA) p telB\nprogressTelCons {t = t}(error e){tel} q = error (t \u2237 tel) [] tt e tel\n\nopen import Data.Empty\n\nNoVar : \u2200{n} \u2192 Weird\u2115 n \u2192 Set\nNoVar Z = \u22a4\nNoVar (S i) = \u22a5\nNoVar (T i) = NoVar i\n\nnoVar : \u2200{n}{i : Weird\u2115 n} \u2192 NoVar i \u2192 WeirdFin i \u2192 \u22a5\nnoVar p (T x) = noVar p x\n\nprogress : \u2200{n}{i : Weird\u2115 n} \u2192 NoVar i \u2192 (t : ScopedTm i) \u2192 Progress t\n\nprogressTel : \u2200{n}{i : Weird\u2115 n} \u2192 NoVar i \u2192 (tel : Tel i) \u2192 TelProgress tel\nprogressTel p [] = done [] tt\nprogressTel p (t \u2237 tel) = progressTelCons (progress p t) (progressTel p tel)\n\nprogress p (` x) = \u22a5-elim (noVar p x)\nprogress p (\u039b K t) = done (V-\u039b t) \nprogress p (t \u00b7\u22c6 A) = progress\u00b7\u22c6 (progress p t) A\nprogress p (\u019b A t) = done (V-\u019b A t)\nprogress p (t \u00b7 u) = progress\u00b7 (progress p t) u\nprogress p (con c) = done (V-con c)\nprogress p (error A) = error (E-error A)\nprogress p (builtin b As ts) = progress-builtin b As ts (progressTel p ts)\nprogress p (wrap A B t) with progress p t\nprogress p (wrap A B t) | step q = step (\u03be-wrap q)\nprogress p (wrap A B t) | done q = done (V-wrap A B q)\nprogress p (wrap A B t) | error q = error (E-wrap q)\nprogress p (unwrap t) = progress-unwrap (progress p t)\n\\end{code}\n\n\\begin{code}\nopen import Data.Nat\n\nSteps : ScopedTm Z \u2192 Set\nSteps t = \u03a3 (ScopedTm Z) \u03bb t' \u2192 t \u2014\u2192\u22c6 t' \u00d7 (Maybe (Value t') \u228e Error t')\n\nrun\u2014\u2192 : {t t' : ScopedTm Z} \u2192 t \u2014\u2192 t' \u2192 Steps t' \u2192 Steps t\nrun\u2014\u2192 p (t' , ps , q) = _ , ((trans p ps) , q)\n\nrun : (t : ScopedTm Z) \u2192 \u2115 \u2192 Steps t\nrunProg : \u2115 \u2192 {t : ScopedTm Z} \u2192 Progress t \u2192 Steps t\n\nrun t 0 = t , (refl , inl nothing) -- out of fuel\nrun t (suc n) = runProg n (progress tt t)\n\nrunProg n (step {t' = t'} p) = run\u2014\u2192 p (run t' n)\nrunProg n (done V) = _ , refl , inl (just V)\nrunProg n (error e) = _ , refl , inr e \n\\end{code}\n","avg_line_length":42.6635514019,"max_line_length":146,"alphanum_fraction":0.5740051114} +{"size":33214,"ext":"lagda","lang":"Literate Agda","max_stars_count":null,"content":"---\ntitle : \"Naturals: Natural numbers\"\nlayout : page\nprev : \/Preface\/\npermalink : \/Naturals\/\nnext : \/Induction\/\n---\n\n\\begin{code}\nmodule plfa.Naturals where\n\\end{code}\n\nThe night sky holds more stars than I can count, though fewer than five\nthousand are visible to the naked eye. The observable universe\ncontains about seventy sextillion stars.\n\nBut the number of stars is finite, while natural numbers are infinite.\nCount all the stars, and you will still have as many natural numbers\nleft over as you started with.\n\n\n## The naturals are an inductive datatype\n\nEveryone is familiar with the natural numbers:\n\n 0\n 1\n 2\n 3\n ...\n\nand so on. We write `\u2115` for the *type* of natural numbers, and say that\n`0`, `1`, `2`, `3`, and so on are *values* of type `\u2115`, indicated by\nwriting `0 : \u2115`, `1 : \u2115`, `2 : \u2115`, `3 : \u2115`, and so on.\n\nThe set of natural numbers is infinite, yet we can write down\nits definition in just a few lines. Here is the definition\nas a pair of inference rules:\n\n --------\n zero : \u2115\n\n m : \u2115\n ---------\n suc m : \u2115\n\nAnd here is the definition in Agda:\n\\begin{code}\ndata \u2115 : Set where\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n\\end{code}\nHere `\u2115` is the name of the *datatype* we are defining,\nand `zero` and `suc` (short for *successor*) are the\n*constructors* of the datatype.\n\nBoth definitions above tell us the same two things:\n\n* _Base case_: `zero` is a natural number.\n* _Inductive case_: if `m` is a natural number, then `suc m` is also a\n natural number.\n\nFurther, these two rules give the *only* ways of creating natural numbers.\nHence, the possible natural numbers are\n\n zero\n suc zero\n suc (suc zero)\n suc (suc (suc zero))\n ...\n\nWe write `0` as shorthand for `zero`; and `1` is shorthand\nfor `suc zero`, the successor of zero, that is, the natural that comes\nafter zero; and `2` is shorthand for `suc (suc zero)`, which is the\nsame as `suc 1`, the successor of one; and `3` is shorthand for the\nsuccessor of two; and so on.\n\n#### Exercise `seven` {#seven}\n\nWrite out `7` in longhand.\n\n\n## Unpacking the inference rules\n\nLet's unpack the inference rules. Each inference rule consists of\nzero or more _judgments_ written above a horizontal line, called the\n_hypotheses_, and a single judgment written below, called the\n_conclusion_. The first rule is the base case. It has no hypotheses,\nand the conclusion asserts that `zero` is a natural. The second rule\nis the inductive case. It has one hypothesis, which assumes that `m`\nis a natural, and in that case the conclusion asserts that `suc m`\nis a also a natural.\n\n\n## Unpacking the Agda definition\n\nLet's unpack the Agda definition. The keyword `data` tells us this is an\ninductive definition, that is, that we are defining a new datatype\nwith constructors. The phrase\n\n \u2115 : Set\n\ntells us that `\u2115` is the name of the new datatype, and that it is a\n`Set`, which is the way in Agda of saying that it is a type. The\nkeyword `where` separates the declaration of the datatype from the\ndeclaration of its constructors. Each constructor is declared on a\nseparate line, which is indented to indicate that it belongs to the\ncorresponding `data` declaration. The lines\n\n zero : \u2115\n suc : \u2115 \u2192 \u2115\n\ngive _signatures_ specifying the types of the constructors `zero` and `suc`.\nThey tell us that `zero` is a natural number and that `suc` takes a natural\nnumber as argument and returns a natural number.\n\nYou may have noticed that `\u2115` and `\u2192` don't appear on your keyboard.\nThey are symbols in _unicode_. At the end of each chapter is a list\nof all unicode symbols introduced in the chapter, including\ninstructions on how to type them in the Emacs text editor. Here\n_type_ refers to typing with fingers as opposed to data types!\n\n\n## The story of creation\n\nLet's look again at the rules that define the natural numbers:\n\n* _Base case_: `zero` is a natural number.\n* _Inductive case_: if `m` is a natural number, then `suc m` is also a\n natural number.\n\nHold on! The second line defines natural numbers in terms of natural\nnumbers. How can that possibly be allowed? Isn't this as useless a\ndefinition as \"Brexit means Brexit\"?\n\nIn fact, it is possible to assign our definition a meaning without\nresorting to unpermitted circularities. Furthermore, we can do so\nwhile only working with _finite_ sets and never referring to the\n_infinite_ set of natural numbers.\n\nWe will think of it as a creation story. To start with, we know about\nno natural numbers at all.\n\n -- in the beginning, there are no natural numbers\n\nNow, we apply the rules to all the natural numbers we know about. The\nbase case tells us that `zero` is a natural number, so we add it to the set\nof known natural numbers. The inductive case tells us that if `m` is a\nnatural number (on the day before today) then `suc m` is also a\nnatural number (today). We didn't know about any natural numbers\nbefore today, so the inductive case doesn't apply.\n\n -- on the first day, there is one natural number\n zero : \u2115\n\nThen we repeat the process. On the next day we know about all the\nnumbers from the day before, plus any numbers added by the rules. The\nbase case tells us that `zero` is a natural number, but we already knew\nthat. But now the inductive case tells us that since `zero` was a natural\nnumber yesterday, then `suc zero` is a natural number today.\n\n -- on the second day, there are two natural numbers\n zero : \u2115\n suc zero : \u2115\n\nAnd we repeat the process again. Now the inductive case\ntells us that since `zero` and `suc zero` are both natural numbers, then\n`suc zero` and `suc (suc zero)` are natural numbers. We already knew about\nthe first of these, but the second is new.\n\n -- on the third day, there are three natural numbers\n zero : \u2115\n suc zero : \u2115\n suc (suc zero) : \u2115\n\nYou've got the hang of it by now.\n\n -- on the fourth day, there are four natural numbers\n zero : \u2115\n suc zero : \u2115\n suc (suc zero) : \u2115\n suc (suc (suc zero)) : \u2115\n\nThe process continues. On the _n_'th day there will be _n_ distinct\nnatural numbers. Every natural number will appear on some given day.\nIn particular, the number _n_ first appears on day _n+1_. And we\nnever actually define the set of numbers in terms of itself. Instead,\nwe define the set of numbers on day _n+1_ in terms of the set of\nnumbers on day _n_.\n\nA process like this one is called _inductive_. We start with nothing, and\nbuild up a potentially infinite set by applying rules that convert one\nfinite set into another finite set.\n\nThe rule defining zero is called a _base case_, because it introduces\na natural number even when we know no other natural numbers. The rule\ndefining successor is called an _inductive case_, because it\nintroduces more natural numbers once we already know some. Note the\ncrucial role of the base case. If we only had inductive rules, then\nwe would have no numbers in the beginning, and still no numbers on the\nsecond day, and on the third, and so on. An inductive definition lacking\na base case is useless, as in the phrase \"Brexit means Brexit\".\n\n\n## Philosophy and history\n\nA philosopher might observe that our reference to the first day,\nsecond day, and so on, implicitly involves an understanding of natural\nnumbers. In this sense, our definition might indeed be regarded as in\nsome sense circular, but we need not let this disturb us.\nEveryone possesses a good informal understanding of the natural\nnumbers, which we may take as a foundation for their formal\ndescription.\n\nWhile the natural numbers have been understood for as long as people\ncan count, the inductive definition of the natural numbers is relatively\nrecent. It can be traced back to Richard Dedekind's paper \"_Was sind\nund was sollen die Zahlen?_\" (What are and what should be the\nnumbers?), published in 1888, and Giuseppe Peano's book \"_Arithmetices\nprincipia, nova methodo exposita_\" (The principles of arithmetic\npresented by a new method), published the following year.\n\n\n## A pragma\n\nIn Agda, any text following `--` or enclosed between `{-`\nand `-}` is considered a _comment_. Comments have no effect on the\ncode, with the exception of one special kind of comment, called a\n_pragma_, which is enclosed between `{-#` and `#-}`.\n\nIncluding the line\n\\begin{code}\n{-# BUILTIN NATURAL \u2115 #-}\n\\end{code}\ntells Agda that `\u2115` corresponds to the natural numbers, and hence one\nis permitted to type `0` as shorthand for `zero`, `1` as shorthand for\n`suc zero`, `2` as shorthand for `suc (suc zero)`, and so on. The\ndeclaration is not permitted unless the type given has exactly two\nconstructors, one with no arguments (corresponding to zero) and\none with a single argument the same as the type being defined\n(corresponding to successor).\n\nAs well as enabling the above shorthand, the pragma also enables a\nmore efficient internal representation of naturals using the Haskell\ntype for arbitrary-precision integers. Representing the natural _n_\nwith `zero` and `suc` requires space proportional to _n_, whereas\nrepresenting it as an arbitary-precision integer in Haskell only\nrequires space proportional to the logarithm of _n_.\n\n\n## Imports\n\nShortly we will want to write some equations that hold between\nterms involving natural numbers. To support doing so, we import\nthe definition of equality and notations for reasoning\nabout it from the Agda standard library.\n\n\\begin{code}\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_\u2261_; refl)\nopen Eq.\u2261-Reasoning using (begin_; _\u2261\u27e8\u27e9_; _\u220e)\n\\end{code}\n\nThe first line brings the standard library module that defines\nequality into scope and gives it the name `Eq`. The second line\nopens that module, that is, adds all the names specified in the\n`using` clause into the current scope. In this case the names added\nare `_\u2261_`, the equality operator, and `refl`, the name for evidence\nthat two terms are equal. The third line takes a module that\nspecifies operators to support reasoning about equivalence, and adds\nall the names specified in the `using` clause into the current scope.\nIn this case, the names added are `begin_`, `_\u2261\u27e8\u27e9_`, and `_\u220e`. We\nwill see how these are used below. We take these as givens for now,\nbut will see how they are defined in\nChapter [Equality][plfa.Equality].\n\nAgda uses underbars to indicate where terms appear in infix or mixfix\noperators. Thus, `_\u2261_` and `_\u2261\u27e8\u27e9_` are infix (each operator is written\nbetween two terms), while `begin_` is prefix (it is written before a\nterm), and `_\u220e` is postfix (it is written after a term).\n\nParentheses and semicolons are among the few characters that cannot\nappear in names, so we do not need extra spaces in the `using` list.\n\n\n## Operations on naturals are recursive functions {#plus}\n\nNow that we have the natural numbers, what can we do with them?\nFor instance, can we define arithmetic operations such as\naddition and multiplication?\n\nAs a child I spent much time memorising tables of addition and\nmultiplication. At first the rules seemed tricky and I would often\nmake mistakes. It came as a shock to me to discover _recursion_,\na simple technique by which every one of the infinite possible\ninstances of addition and multiplication can be specified in\njust a couple of lines.\n\nHere is the definition of addition in Agda:\n\\begin{code}\n_+_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero + n = n\nsuc m + n = suc (m + n)\n\\end{code}\n\nLet's unpack this definition. Addition is an infix operator. It is\nwritten with underbars where the argument go, hence its name is\n`_+_`. The first line is a signature specifying the type of the operator.\nThe type `\u2115 \u2192 \u2115 \u2192 \u2115`, indicates that addition accepts two naturals\nand returns a natural. Infix notation is just a shorthand for application;\nthe terms `m + n` and `_+_ m n` are equivalent.\n\nThe definition has a base case and an inductive case, corresponding to\nthose for the natural numbers. The base case says that adding zero to\na number, `zero + n`, returns that number, `n`. The inductive case\nsays that adding the successor of a number to another number,\n`(suc m) + n`, returns the successor of adding the two numbers, `suc (m + n)`.\nWe say we use _pattern matching_ when constructors appear on the\nleft-hand side of an equation.\n\nIf we write `zero` as `0` and `suc m` as `1 + m`, the definition turns\ninto two familiar equations.\n\n 0 + n \u2261 n\n (1 + m) + n \u2261 1 + (m + n)\n\nThe first follows because zero is an identity for addition, and the\nsecond because addition is associative. In its most general form,\nassociativity is written\n\n (m + n) + p \u2261 m + (n + p)\n\nmeaning that the location of parentheses is irrelevant. We get the\nsecond equation from the third by taking `m` to be `1`, `n` to be `m`,\nand `p` to be `n`. We write `=` for definitions, while we\nwrite `\u2261` for assertions that two already defined things are the same.\n\nThe definition is _recursive_, in that the last line defines addition\nin terms of addition. As with the inductive definition of the\nnaturals, the apparent circularity is not a problem. It works because\naddition of larger numbers is defined in terms of addition of smaller\nnumbers. Such a definition is called _well founded_.\n\nFor example, let's add two and three.\n\\begin{code}\n_ : 2 + 3 \u2261 5\n_ =\n begin\n 2 + 3\n \u2261\u27e8\u27e9 -- is shorthand for\n (suc (suc zero)) + (suc (suc (suc zero)))\n \u2261\u27e8\u27e9 -- inductive case\n suc ((suc zero) + (suc (suc (suc zero))))\n \u2261\u27e8\u27e9 -- inductive case\n suc (suc (zero + (suc (suc (suc zero)))))\n \u2261\u27e8\u27e9 -- base case\n suc (suc (suc (suc (suc zero))))\n \u2261\u27e8\u27e9 -- is longhand for\n 5\n \u220e\n\\end{code}\nWe can write the same derivation more compactly by only\nexpanding shorthand as needed.\n\\begin{code}\n_ : 2 + 3 \u2261 5\n_ =\n begin\n 2 + 3\n \u2261\u27e8\u27e9\n suc (1 + 3)\n \u2261\u27e8\u27e9\n suc (suc (0 + 3))\n \u2261\u27e8\u27e9\n suc (suc 3)\n \u2261\u27e8\u27e9\n 5\n \u220e\n\\end{code}\nThe first line matches the inductive case by taking `m = 1` and `n = 3`,\nthe second line matches the inductive case by taking `m = 0` and `n = 3`,\nand the third line matches the base case by taking `n = 3`.\n\nBoth derivations consist of a signature (written with a colon, `:`),\ngiving a type, and a binding (written with an equal sign, `=`),\ngiving a term of the given type. Here we use the dummy name `_`. The\ndummy name can be reused, and is convenient for examples. Names other\nthan `_` must be used only once in a module.\n\nHere the type is `2 + 3 \u2261 5` and the term provides _evidence_ for the\ncorresponding equation, here written in tabular form as a chain of\nequations. The chain starts with `begin` and finishes with `\u220e`\n(pronounced \"qed\" or \"tombstone\", the latter from its appearance), and\nconsists of a series of terms separated by `\u2261\u27e8\u27e9`.\n\nIn fact, both proofs are longer than need be, and Agda is satisfied\nwith the following.\n\\begin{code}\n_ : 2 + 3 \u2261 5\n_ = refl\n\\end{code}\nAgda knows how to compute the value of `2 + 3`, and so can immediately\ncheck it is the same as `5`. A binary relation is said to be _reflexive_\nif every value relates to itself. Evidence that a value is equal to\nitself is written `refl`.\n\nIn the chains of equations, all Agda checks is that each term\nsimplifies to the same value. If we jumble the equations, omit lines, or\nadd extraneous lines it will still be accepted. It's up to us to write\nthe equations in an order that makes sense to the reader.\n\nHere `2 + 3 \u2261 5` is a type, and the chains of equations (and also\n`refl`) are terms of the given type; alternatively, one can think of\neach term as _evidence_ for the assertion `2 + 3 \u2261 5`. This duality\nof interpretation---of a type as a proposition, and of a term as\nevidence---is central to how we formalise concepts in Agda, and will\nbe a running theme throughout this book.\n\nNote that when we use the word _evidence_ it is nothing equivocal. It\nis not like testimony in a court which must be weighed to determine\nwhether the witness is trustworthy. Rather, it is ironclad. The\nother word for evidence, which we will use interchangeably, is _proof_.\n\n#### Exercise `+-example` {#plus-example}\n\nCompute `3 + 4`, writing out your reasoning as a chain of equations.\n\n\n## Multiplication\n\nOnce we have defined addition, we can define multiplication\nas repeated addition.\n\\begin{code}\n_*_ : \u2115 \u2192 \u2115 \u2192 \u2115\nzero * n = zero\n(suc m) * n = n + (m * n)\n\\end{code}\n\nAgain, rewriting gives us two familiar equations.\n\n 0 * n \u2261 0\n (1 + m) * n \u2261 n + (m * n)\n\nThe first follows because zero times anything is zero, and the second\nfollows because multiplication distributes over addition.\nIn its most general form, distribution of multiplication over addition\nis written\n\n (m + n) * p \u2261 (m * p) + (n * p)\n\nWe get the second equation from the third by taking `m` to be `1`, `n`\nto be `m`, and `p` to be `n`, and then using the fact that one is an\nidentity for multiplication, so `1 * n \u2261 n`.\n\nAgain, the definition is well-founded in that multiplication of\nlarger numbers is defined in terms of multiplication of smaller numbers.\n\nFor example, let's multiply two and three.\n\\begin{code}\n_ =\n begin\n 2 * 3\n \u2261\u27e8\u27e9 -- inductive case\n 3 + (1 * 3)\n \u2261\u27e8\u27e9 -- inductive case\n 3 + (3 + (0 * 3))\n \u2261\u27e8\u27e9 -- base case\n 3 + (3 + 0)\n \u2261\u27e8\u27e9 -- simplify\n 6\n \u220e\n\\end{code}\nThe first line matches the inductive case by taking `m = 1` and `n = 3`,\nThe second line matches the inductive case by taking `m = 0` and `n = 3`,\nand the third line matches the base case by taking `n = 3`.\nHere we have omitted the signature declaring `_ : 2 * 3 \u2261 6`, since\nit can easily be inferred from the corresponding term.\n\n\n#### Exercise `*-example` {#times-example}\n\nCompute `3 * 4`, writing out your reasoning as a chain of equations.\n\n\n#### Exercise `_^_` (recommended) {#power}\n\nDefine exponentiation, which is given by the following equations.\n\n n ^ 0 = 1\n n ^ (1 + m) = n * (n ^ m)\n\nCheck that `3 ^ 4` is `81`.\n\n\n## Monus\n\nWe can also define subtraction. Since there are no negative\nnatural numbers, if we subtract a larger number from a smaller\nnumber we will take the result to be zero. This adaption of\nsubtraction to naturals is called _monus_ (a twist on _minus_).\n\nMonus is our first use of a definition that uses pattern\nmatching against both arguments.\n\\begin{code}\n_\u2238_ : \u2115 \u2192 \u2115 \u2192 \u2115\nm \u2238 zero = m\nzero \u2238 (suc n) = zero\n(suc m) \u2238 (suc n) = m \u2238 n\n\\end{code}\nWe can do a simple analysis to show that all the cases are covered.\n\n * Consider the second argument.\n + If it is `zero`, then the first equation applies.\n + If it is `suc n`, then consider the first argument.\n - If it is `zero`, then the second equation applies.\n - If it is `suc m`, then the third equation applies.\n\nAgain, the recursive definition is well-founded because\nmonus on bigger numbers is defined in terms of monus on\nsmaller numbers.\n\nFor example, let's subtract two from three.\n\\begin{code}\n_ =\n begin\n 3 \u2238 2\n \u2261\u27e8\u27e9\n 2 \u2238 1\n \u2261\u27e8\u27e9\n 1 \u2238 0\n \u2261\u27e8\u27e9\n 1\n \u220e\n\\end{code}\nWe did not use the second equation at all, but it will be required\nif we try to subtract a larger number from a smaller one.\n\\begin{code}\n_ =\n begin\n 2 \u2238 3\n \u2261\u27e8\u27e9\n 1 \u2238 2\n \u2261\u27e8\u27e9\n 0 \u2238 1\n \u2261\u27e8\u27e9\n 0\n \u220e\n\\end{code}\n\n#### Exercise `\u2238-examples` (recommended) {#monus-examples}\n\nCompute `5 \u2238 3` and `3 \u2238 5`, writing out your reasoning as a chain of equations.\n\n\n## Precedence\n\nWe often use _precedence_ to avoid writing too many parentheses.\nApplication _binds more tightly than_ (or _has precedence over_) any\noperator, and so we may write `suc m + n` to mean `(suc m) + n`.\nAs another example, we say that multiplication binds more tightly than\naddition, and so write `n + m * n` to mean `n + (m * n)`.\nWe also sometimes say that addition _associates to the left_, and\nso write `m + n + p` to mean `(m + n) + p`.\n\nIn Agda the precedence and associativity of infix operators\nneeds to be declared.\n\\begin{code}\ninfixl 6 _+_ _\u2238_\ninfixl 7 _*_\n\\end{code}\nThis states operators `_+_` and `_\u2238_` have precedence level 6,\nand operator `_*_` has precedence level 7.\nAddition and monus bind less tightly than multiplication\nbecause they have lower precedence.\nWriting `infixl` indicates that all three\noperators associate to the left. One can also write `infixr` to\nindicate that an operator associates to the right, or just `infix` to\nindicate that parentheses are always required to disambiguate.\n\n\n## Currying\n\nWe have chosen to represent a function of two arguments in terms\nof a function of the first argument that returns a function of the\nsecond argument. This trick goes by the name _currying_.\n\nAgda, like other functional languages such as Haskell and ML,\nis designed to make currying easy to use. Function\narrows associate to the right and application associates to the left:\n\n`\u2115 \u2192 \u2115 \u2192 \u2115` stands for `\u2115 \u2192 (\u2115 \u2192 \u2115)`\n\nand\n\n`_+_ 2 3` stands for `(_+_ 2) 3`.\n\nThe term `_+_ 2` by itself stands for the function that adds two to\nits argument, hence applying it to three yields five.\n\nCurrying is named for Haskell Curry, after whom the programming\nlanguage Haskell is also named. Curry's work dates to the 1930's.\nWhen I first learned about currying, I was told it was misattributed,\nsince the same idea was previously proposed by Moses Sch\u00f6nfinkel in\nthe 1920's. I was told a joke: \"It should be called sch\u00f6nfinkeling,\nbut currying is tastier\". Only later did I learn that the explanation\nof the misattribution was itself a misattribution. The idea actually\nappears in the _Begriffschrift_ of Gottlob Frege, published in 1879.\n\n## The story of creation, revisited\n\nJust as our inductive definition defines the naturals in terms of the\nnaturals, so does our recursive definition define addition in terms\nof addition.\n\nAgain, it is possible to assign our definition a meaning without\nresorting to unpermitted circularities. We do so by reducing our\ndefinition to equivalent inference rules for judgements about equality.\n\n n : \u2115\n --------------\n zero + n = n\n\n m + n = p\n ---------------------\n (suc m) + n = suc p\n\nHere we assume we have already defined the infinite set of natural\nnumbers, specifying the meaning of the judgment `n : \u2115`. The first\ninference rule is the base case. It asserts that if `n` is a natural number\nthen adding zero to it gives `n`. The second inference rule is the inductive\ncase. It asserts that if adding `m` and `n` gives `p`, then adding `suc m` and\n`n` gives `suc p`.\n\nAgain we resort to a creation story, where this time we are\nconcerned with judgements about addition.\n\n -- in the beginning, we know nothing about addition\n\nNow, we apply the rules to all the judgment we know about.\nThe base case tells us that `zero + n = n` for every natural `n`,\nso we add all those equations. The inductive case tells us that if\n`m + n = p` (on the day before today) then `suc m + n = suc p`\n(today). We didn't know any equations about addition before today,\nso that rule doesn't give us any new equations.\n\n -- on the first day, we know about addition of 0\n 0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 ...\n\nThen we repeat the process, so on the next day we know about all the\nequations from the day before, plus any equations added by the rules.\nThe base case tells us nothing new, but now the inductive case adds\nmore equations.\n\n -- on the second day, we know about addition of 0 and 1\n 0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 0 + 3 = 3 ...\n 1 + 0 = 1 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 ...\n\nAnd we repeat the process again.\n\n -- on the third day, we know about addition of 0, 1, and 2\n 0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 0 + 3 = 3 ...\n 1 + 0 = 1 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 ...\n 2 + 0 = 2 2 + 1 = 3 2 + 2 = 4 2 + 3 = 5 ...\n\nYou've got the hang of it by now.\n\n -- on the fourth day, we know about addition of 0, 1, 2, and 3\n 0 + 0 = 0 0 + 1 = 1 0 + 2 = 2 0 + 3 = 3 ...\n 1 + 0 = 1 1 + 1 = 2 1 + 2 = 3 1 + 3 = 4 ...\n 2 + 0 = 2 2 + 1 = 3 2 + 2 = 4 2 + 3 = 5 ...\n 3 + 0 = 3 3 + 1 = 4 3 + 2 = 5 3 + 3 = 6 ...\n\nThe process continues. On the _m_'th day we will know all the\nequations where the first number is less than _m_.\n\nAs we can see, the reasoning that justifies inductive and recursive\ndefinitions is quite similar. They might be considered two sides of\nthe same coin.\n\n\n## The story of creation, finitely {#finite-creation}\n\nThe above story was told in a stratified way. First, we create\nthe infinite set of naturals. We take that set as given when\ncreating instances of addition, so even on day one we have an\ninfinite set of instances.\n\nInstead, we could choose to create both the naturals and the instances\nof addition at the same time. Then on any day there would be only\na finite set of instances.\n\n -- in the beginning, we know nothing\n\nNow, we apply the rules to all the judgment we know about. Only the\nbase case for naturals applies.\n\n -- on the first day, we know zero\n 0 : \u2115\n\nAgain, we apply all the rules we know. This gives us a new natural,\nand our first equation about addition.\n\n -- on the second day, we know one and all sums that yield zero\n 0 : \u2115\n 1 : \u2115 0 + 0 = 0\n\nThen we repeat the process. We get one more equation about addition\nfrom the base case, and also get an equation from the inductive case,\napplied to equation of the previous day.\n\n -- on the third day, we know two and all sums that yield one\n 0 : \u2115\n 1 : \u2115 0 + 0 = 0\n 2 : \u2115 0 + 1 = 1 1 + 0 = 1\n\nYou've got the hang of it by now.\n\n -- on the fourth day, we know three and all sums that yield two\n 0 : \u2115\n 1 : \u2115 0 + 0 = 0\n 2 : \u2115 0 + 1 = 1 1 + 0 = 1\n 3 : \u2115 0 + 2 = 2 1 + 1 = 2 2 + 0 = 2\n\nOn the _n_'th day there will be _n_ distinct natural numbers, and _n \u00d7\n(n-1) \/ 2_ equations about addition. The number _n_ and all equations\nfor addition of numbers less than _n_ first appear by day _n+1_.\nThis gives an entirely finitist view of infinite sets of data and\nequations relating the data.\n\n\n## Writing definitions interactively\n\nAgda is designed to be used with the Emacs text editor, and the two\nin combination provide features that help to create definitions\nand proofs interactively.\n\nBegin by typing\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n m + n = ?\n\nThe question mark indicates that you would like Agda to help with\nfilling in that part of the code. If you type `C-c C-l` (pressing\nthe control key while hitting the `c` key followed by the `l` key)\nthe question mark will be replaced.\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n m + n = { }0\n\nThe empty braces are called a *hole*, and 0 is a number used for\nreferring to the hole. The hole will display highlighted in green.\nEmacs will also create a window displaying the text\n\n ?0 : \u2115\n\nto indicate that hole 0 is to be filled in with a term of type `\u2115`.\nTyping `C-c C-f` will move you into the next hole.\n\nWe wish to define addition by recursion on the first argument.\nMove the cursor into the hole and type `C-c C-c`. You will be given\nthe prompt:\n\n pattern variables to case (empty for split on result):\n\nTyping `m` will cause a split on that variable, resulting\nin an update to the code.\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n zero + n = { }0\n suc m + n = { }1\n\nThere are now two holes, and the window at the bottom tells you the\nrequired type of each.\n\n ?0 : \u2115\n ?1 : \u2115\n\nGoing into hole 0 and type `C-c C-,` will display information on the\nrequired type of the hole, and what free variables are available.\n\n Goal: \u2115\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n n : \u2115\n\nThis strongly suggests filling the hole with `n`. After the hole is\nfilled, you can type `C-c C-space`, which will remove the hole.\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n zero + n = n\n suc m + n = { }1\n\nAgain, going into hole 1 and type `C-c C-,` will display information on the\nrequired type of the hole, and what free variables are available.\n\n Goal: \u2115\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n n : \u2115\n m : \u2115\n\nGoing into the hole and type `C-c C-r` will fill it in with a constructor\n(if there is a unique choice) or tell you what constructors you might use,\nif there is a choice. In this case, it displays the following.\n\n Don't know which constructor to introduce of zero or suc\n\nFilling the hole with `suc ?` and typing `C-c C-space` results in the following.\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n zero + n = n\n suc m + n = suc { }1\n\nGoing into the new hole and typing `C-c C-,` gives similar information to before.\n\n Goal: \u2115\n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n n : \u2115\n m : \u2115\n\nWe can fill the hole with `m + n` and type `C-c C-space` to complete the program.\n\n _+_ : \u2115 \u2192 \u2115 \u2192 \u2115\n zero + n = n\n suc m + n = suc (m + n)\n\nExploiting interaction to this degree is probably not helpful for a program this\nsimple, but the same techniques can help with more complex programs. Even for\na program this simple, using `C-c C-c` to split cases can be helpful.\n\n\n## More pragmas\n\nIncluding the lines\n\\begin{code}\n{-# BUILTIN NATPLUS _+_ #-}\n{-# BUILTIN NATTIMES _*_ #-}\n{-# BUILTIN NATMINUS _\u2238_ #-}\n\\end{code}\ntells Agda that these three operators correspond to the usual ones,\nand enables it to perform these computations using the corresponding\nHaskell operators on the arbitrary-precision integer type.\nRepresenting naturals with `zero` and `suc` requires time proportional\nto _m_ to add _m_ and _n_, whereas representing naturals as integers\nin Haskell requires time proportional to the larger of the logarithms\nof _m_ and _n_. Similarly, representing naturals with `zero`\nand `suc` requires time proportional to the product of _m_ and _n_ to\nmultiply _m_ and _n_, whereas representing naturals as integers in\nHaskell requires time proportional to the sum of the logarithms of\n_m_ and _n_.\n\n\n#### Exercise `Bin` (stretch) {#Bin}\n\nA more efficient representation of natural numbers uses a binary\nrather than a unary system. We represent a number as a bitstring.\n\\begin{code}\ndata Bin : Set where\n nil : Bin\n x0_ : Bin \u2192 Bin\n x1_ : Bin \u2192 Bin\n\\end{code}\nFor instance, the bitstring\n\n 1011\n\nstanding for the number eleven is encoded, right to left, as\n\n x1 x1 x0 x1 nil\n\nRepresentations are not unique due to leading zeros.\nHence, eleven is also represented by `001011`, encoded as\n\n x1 x1 x0 x1 x0 x0 nil\n\nDefine a function\n\n inc : Bin \u2192 Bin\n\nthat converts a bitstring to the bitstring for the next higher\nnumber. For example, since `1100` encodes twelve, we should have\n\n inc (x1 x1 x0 x1 nil) \u2261 x0 x0 x1 x1 nil\n\nConfirm that this gives the correct answer for the bitstrings\nencoding zero through four.\n\nUsing the above, define a pair of functions to convert\nbetween the two representations.\n\n to : \u2115 \u2192 Bin\n from : Bin \u2192 \u2115\n\nFor the former, choose the bitstring to have no leading zeros if it\nrepresents a positive natural, and represent zero by `x0 nil`.\nConfirm that these both give the correct answer for zero through four.\n\n\n\n## Standard library\n\nAt the end of each chapter, we will show where to find relevant\ndefinitions in the standard library. The naturals, constructors for\nthem, and basic operators upon them, are defined in the standard\nlibrary module `Data.Nat`.\n\n\\begin{code}\n-- import Data.Nat using (\u2115; zero; suc; _+_; _*_; _^_; _\u2238_)\n\\end{code}\n\nNormally, we will show an import as running code, so Agda will\ncomplain if we attempt to import a definition that is not available.\nThis time, however, we have only shown the import as a comment. Both\nthis chapter and the standard library invoke the `NATURAL` pragma, the\nformer on `\u2115`, and the latter on the equivalent type `Data.Nat.\u2115`.\nSuch a pragma can only be invoked once, as invoking it twice would\nraise confusion as to whether `2` is a value of type `\u2115` or type\n`Data.Nat.\u2115`. Similar confusions arise if other pragmas are invoked\ntwice. For this reason, we will usually avoid pragmas in future chapters.\nInformation on pragmas can be found in the Agda documentation.\n\n\n## Unicode\n\nThis chapter uses the following unicode.\n\n \u2115 U+2115 DOUBLE-STRUCK CAPITAL N (\\bN)\n \u2192 U+2192 RIGHTWARDS ARROW (\\to, \\r)\n \u2238 U+2238 DOT MINUS (\\.-)\n \u2261 U+2261 IDENTICAL TO (\\==)\n \u27e8 U+27E8 MATHEMATICAL LEFT ANGLE BRACKET (\\<)\n \u27e9 U+27E9 MATHEMATICAL RIGHT ANGLE BRACKET (\\>)\n \u220e U+220E END OF PROOF (\\qed)\n\nEach line consists of the Unicode character (`\u2115`), the corresponding\ncode point (`U+2115`), the name of the character (`DOUBLE-STRUCK CAPITAL N`),\nand the sequence to type into Emacs to generate the character (`\\bN`).\n\nThe command `\\r` gives access to a wide variety of rightward arrows.\nAfter typing `\\r`, one can access the many available arrows by using\nthe left, right, up, and down keys to navigate. The command remembers\nwhere you navigated to the last time, and starts with the same\ncharacter next time. The command `\\l` works similarly for left arrows.\n\nIn place of left, right, up, and down keys, one may also use control characters.\n\n C-b left (backward one character)\n C-f right (forward one character)\n C-p up (to the previous line)\n C-n down (to the next line)\n\nWe write `C-b` to stand for control-b, and similarly. One can also navigate\nleft and right by typing the digits that appear in the displayed list.\n","avg_line_length":34.9253417455,"max_line_length":81,"alphanum_fraction":0.7044318661} +{"size":1223,"ext":"lagda","lang":"Literate Agda","max_stars_count":135.0,"content":"\\begin{code}\nmodule Declarative.Examples.StdLib.ChurchNat where\n\\end{code}\n\n\\begin{code}\nopen import Utils\nopen import Type\nopen import Declarative\nopen import Builtin\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\n\nopen import Data.Unit\n\\end{code}\n\n\\begin{code}\n-- all (r :: *). r -> (r -> r) -> r\nN : \u2200{\u03a6} \u2192 \u03a6 \u22a2\u22c6 *\nN = \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 ` Z)\n\n-- \/\\(r :: *) -> \\(z : r) (f : r -> r) -> z\nZero : \u2205 \u22a2 N\nZero = \u039b (\u019b (\u019b (` (S Z))))\n\n-- \\(n : nat) -> \/\\(r :: *) -> \\(z : r) (f : r -> r) -> f (n {r} z f)\nSucc : \u2205 \u22a2 N \u21d2 N\nSucc = \u019b (\u039b (\u019b (\u019b (` Z \u00b7 ((` (S (S (T Z)))) \u00b7\u22c6 (` Z) \u00b7 (` (S Z)) \u00b7 (` Z))))))\n\nIter : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 \u03a0 (` Z \u21d2 (` Z \u21d2 ` Z) \u21d2 N \u21d2 (` Z))\nIter = \u039b (\u019b (\u019b (\u019b ((` Z) \u00b7\u22c6 (` Z) \u00b7 (` (S (S Z))) \u00b7 (` (S Z))))))\n\nopen import Builtin.Constant.Type\nopen import Data.Integer\nopen import Data.Nat\nopen import Agda.Builtin.Sigma renaming (_,_ to _,,_)\n\ncon0 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon0 = con (integer (\u2124.pos 0))\n\ncon1 : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer\ncon1 = con (integer (\u2124.pos 1))\n\ninc : \u2200{\u03a6}{\u0393 : Ctx \u03a6} \u2192 \u0393 \u22a2 con integer \u21d2 con integer\ninc = \u019b (builtin addInteger \u00b7 con1 \u00b7 ` Z)\n\nNat2Int : \u2205 \u22a2 N \u21d2 con integer\nNat2Int = \u019b (Iter\n \u00b7\u22c6 con integer\n \u00b7 con0\n \u00b7 inc\n \u00b7 ` Z)\n\\end{code}\n","avg_line_length":23.5192307692,"max_line_length":77,"alphanum_fraction":0.5053147997} +{"size":1589,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Declarative.Erasure where\n\\end{code}\n\n\\begin{code}\nopen import Declarative\nopen import Type.RenamingSubstitution as T\nopen import Untyped\nopen import Untyped.RenamingSubstitution as U\n\\end{code}\n\n\\begin{code}\nopen import Type\nopen import Declarative\nopen import Builtin.Constant.Term Ctx\u22c6 Kind * _\u22a2\u22c6_ con\n renaming (TermCon to TyTermCon)\nopen import Data.Nat\nopen import Data.Fin\nopen import Data.List\n\nlen : \u2200{\u03a6} \u2192 Ctx \u03a6 \u2192 \u2115\nlen \u2205 = 0\nlen (\u0393 ,\u22c6 K) = len \u0393\nlen (\u0393 , A) = suc (len \u0393)\n\neraseVar : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u220b A \u2192 Fin (len \u0393)\neraseVar Z = zero \neraseVar (S \u03b1) = suc (eraseVar \u03b1) \neraseVar (T \u03b1) = eraseVar \u03b1\n\neraseTC : \u2200{\u03a6}{\u0393 : Ctx \u03a6}{A : \u03a6 \u22a2\u22c6 *} \u2192 TyTermCon A \u2192 TermCon\neraseTC (integer i) = integer i\neraseTC (bytestring b) = bytestring b\neraseTC (string s) = string s\n\neraseTel : \u2200{\u03a6 \u0393 \u0394}{\u03c3 : T.Sub \u0394 \u03a6}{As : List (\u0394 \u22a2\u22c6 *)}\n \u2192 Declarative.Tel \u0393 \u0394 \u03c3 As\n \u2192 Untyped.Tel (len \u0393)\nerase : \u2200{\u03a6 \u0393}{A : \u03a6 \u22a2\u22c6 *} \u2192 \u0393 \u22a2 A \u2192 len \u0393 \u22a2\n\nerase (` \u03b1) = ` (eraseVar \u03b1)\nerase (\u019b t) = \u019b (erase t) \nerase (t \u00b7 u) = erase t \u00b7 erase u\nerase (\u039b t) = \u019b (U.weaken (erase t))\nerase (t \u00b7\u22c6 A) = erase t \u00b7 plc_dummy\nerase (wrap1 pat arg t) = erase t\nerase (unwrap1 t) = erase t\nerase (conv p t) = erase t\nerase {\u03a6}{\u0393} (con t) = con (eraseTC {\u0393 = \u0393} t)\nerase (builtin bn \u03c3 ts) = builtin bn (eraseTel ts)\nerase (error A) = error\n\nopen import Data.Product renaming (_,_ to _,,_)\n\neraseTel {As = []} _ = []\neraseTel {As = x \u2237 As} (t ,, tel) = erase t \u2237 eraseTel tel\n\\end{code}\n","avg_line_length":27.3965517241,"max_line_length":61,"alphanum_fraction":0.5909376967} +{"size":7593,"ext":"lagda","lang":"Literate Agda","max_stars_count":1.0,"content":"\\begin{code}\nmodule Untyped.Reduction where\n\\end{code}\n\n\\begin{code}\nopen import Untyped\nopen import Untyped.RenamingSubstitution\nopen import Builtin\nopen import Builtin.Constant.Type\n\nimport Data.Bool as Bool\nopen import Data.Nat using (\u2115;suc;zero)\nopen import Data.Integer using (_+_;_-_;_*_;\u2223_\u2223;_? j) plc_true plc_false \nBUILTIN greaterThanEqualsInteger (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer j) , tt) =\n decIf (i Builtin.Constant.Type.\u2265? j) plc_true plc_false \nBUILTIN equalsInteger (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (integer j) , tt) =\n decIf (i \u225f j) plc_true plc_false \nBUILTIN concatenate (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n con (bytestring (append b b'))\nBUILTIN takeByteString (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) =\n con (bytestring (take i b))\nBUILTIN dropByteString (_ \u2237 _ \u2237 []) (V-con (integer i) , V-con (bytestring b) , tt) =\n con (bytestring (drop i b))\nBUILTIN sha2-256 (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA2-256 b))\nBUILTIN sha3-256 (_ \u2237 []) (V-con (bytestring b) , tt) = con (bytestring (SHA3-256 b))\nBUILTIN verifySignature (_ \u2237 _ \u2237 _ \u2237 []) (V-con (bytestring k) , V-con (bytestring d) , V-con (bytestring c) , tt) = VERIFYSIG (verifySig k d c)\nBUILTIN equalsByteString (_ \u2237 _ \u2237 []) (V-con (bytestring b) , V-con (bytestring b') , tt) =\n Bool.if (equals b b') then plc_true else plc_false \nBUILTIN _ _ _ = error\n\ndata ProgList {n} (tel : Tel n) : Set where\n done : VTel n tel \u2192 ProgList tel\n step : (tel' : Tel n) \u2192 VTel n tel' \u2192 {t t' : n \u22a2} \u2192 t \u2014\u2192 t' \u2192 (tel'' : Tel n)\n \u2192 tel \u2261 tel' ++ Data.List.[ t ] ++ tel'' \u2192 ProgList tel \n error : (tel' : Tel n) \u2192 VTel n tel' \u2192 {t : n \u22a2} \u2192 Error t \u2192 Tel n\n \u2192 ProgList tel\n\ndata Progress {n}(M : n \u22a2) : Set where\n step : \u2200{N}\n \u2192 M \u2014\u2192 N\n -------------\n \u2192 Progress M\n done :\n Value M\n ----------\n \u2192 Progress M\n error :\n Error M\n -------\n \u2192 Progress M\n\nprogress-\u00b7 : \u2200{n}{t : n \u22a2} \u2192 Progress t \u2192 (u : n \u22a2) \u2192 Progress (t \u00b7 u)\nprogress-\u00b7 (step p) u = step (\u03be-\u00b7\u2081 p)\nprogress-\u00b7 (done (V-\u019b t)) u = step \u03b2-\u019b\nprogress-\u00b7 (done (V-con tcn)) u = error E-todo\nprogress-\u00b7 (done (V-builtin b ts)) u = step sat-builtin\nprogress-\u00b7 (error e) u = error E-todo\n\n\nprogress : (t : 0 \u22a2) \u2192 Progress t\nprogressList : (tel : Tel 0) \u2192 ProgList {0} tel\nprogressList [] = done _\nprogressList (t \u2237 ts) with progress t\nprogressList (t \u2237 ts) | done vt with progressList ts\nprogressList (t \u2237 ts) | done vt | done vs = done (vt , vs)\nprogressList (t \u2237 ts) | done vt | step ts' vs p ts'' p' =\n step (t \u2237 ts') (vt , vs) p ts'' (cong (t \u2237_) p')\nprogressList (t \u2237 ts) | done vt | error ts' vs e ts'' =\n error (t \u2237 ts') (vt , vs) e ts''\nprogressList (t \u2237 ts) | error e = error [] _ e ts\nprogressList (t \u2237 ts) | step p = step [] _ p ts refl\n\nprogress (` ())\nprogress (\u019b t) = done (V-\u019b t)\nprogress (t \u00b7 u) = progress-\u00b7 (progress t) u\nprogress (con tcn) = done (V-con tcn)\nprogress (builtin b ts) with progressList ts\nprogress (builtin b ts) | done vs =\n step (\u03b2-builtin ts vs)\nprogress (builtin b ts) | step ts' vs p ts'' p' =\n step (\u03be-builtin b ts vs p ts'' p')\nprogress (builtin b ts) | error ts' vs e ts'' =\n error E-todo\nprogress error = error E-error\n\\end{code}\n\n\\begin{code}\nrun : \u2200(t : 0 \u22a2) \u2192 \u2115\n \u2192 \u03a3 (0 \u22a2) \u03bb t' \u2192 t \u2014\u2192\u22c6 t' \u00d7 (Maybe (Value t') \u228e Error t')\nrun t 0 = t , refl , inl nothing\nrun t (suc n) with progress t\nrun t (suc n) | done vt = t , refl , inl (just vt)\nrun t (suc n) | error et = t , refl , inr et\nrun t (suc n) | step {N = t'} p with run t' n\nrun t (suc n) | step p | t'' , q , mvt'' = t'' , trans p q , mvt''\n\\end{code}\n","avg_line_length":35.4813084112,"max_line_length":144,"alphanum_fraction":0.5423416304}