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