File size: 32,554 Bytes
803451e
 
 
 
1
2
3
4
5
{"text": "-- exercises-03-wednesday.agda\n\nopen import mylib\n\n{-\n  Part 1 : Recursion via patterns\n  Define the following functions using pattern matching and structural\n  recursion on the natural numbers.\n-}\n\n-- Define a function even that determines wether its input is even.\neven :  ℕ → Bool\n{-\n  even zero = true\n  even (suc n) = ! even n\n-}\neven zero = true\neven (suc zero) = false\neven (suc (suc n)) = even n\n\n-- Define a function sum that sums the numbers from 0 until n-1\nsum : ℕ → ℕ\nsum 0 = 0\nsum (suc n) =  n + sum n\n\n-- Define a function max that calculates the maximum of 2 numbers\nmax : ℕ → ℕ → ℕ\nmax zero n = n\nmax (suc m) zero = suc m\nmax (suc m) (suc n) = suc (max m n)\n\n-- Define a function fib which calculates the nth item of the\n-- Fibonacci sequence: 1,1,2,3,5,8,13\n-- (each number is the sum of the two previous ones).\nfib : ℕ → ℕ\nfib zero = 1\nfib (suc zero) = 1\nfib (suc (suc n)) = fib n + fib (suc n)\n\n-- Define a function eq that determines wether two numbers are equal.\neq : ℕ → ℕ → Bool\neq zero zero = true\neq zero (suc m) = false\neq (suc n) zero = false\neq (suc n) (suc m) = eq n m\n\n-- Define a function rem such that rem m n returns the remainder\n-- when dividing m by suc n (this way we avoid division by 0).\nrem : ℕ → ℕ → ℕ\nrem zero n = zero\nrem (suc m) n = if eq (rem m n) n then 0 else suc (rem m n)\n\n-- Define a function div such that div m n returns the result\n-- of dividing m by suc n (ignoring the remainder)\ndiv : ℕ → ℕ → ℕ\ndiv zero n = zero\ndiv (suc m) n = if eq (rem m n) n then suc (div m n) else div m n\n\n{-\n  Part 2 : Iterator and recursor \n  Define all the functions of part 1 but this time only use the \n  iterator Itℕ. That is NO PATTERNMATCHING (not even in lambdas) and \n  NO RECURSION. \n\n  Naming convention if the function in part 1 is called f then call it \n  f-i if you only use the iterator.\n\n  Test the functions with at least the same test cases.\n\n  Hint: you may want to derive the recursor first (from the iterator):\n  Rℕ : M → (ℕ → M → M) → ℕ → M\n  where the method can access the current number. Using pattern matching \n  the recursor can be defined as follows: \n  Rℕ z s zero = z\n  Rℕ z s (suc n) = s n (Rℕ z s n)\n-}\n\nItℕ : M → (M → M) → ℕ → M\nItℕ z s zero = z\nItℕ z s (suc n) = s (Itℕ z s n)\n\neven-i :  ℕ → Bool\neven-i = Itℕ true (λ even-n → ! even-n)\n\n{-\nsum : ℕ → ℕ\nsum 0 = 0\nsum (suc n) =  n + sum n\n-}\nsum-i-aux : ℕ → ℕ × ℕ -- sum-i-aux n = (n , sum n)\nsum-i-aux = Itℕ (0 , 0)\n  λ n-sum-n → (suc (proj₁ n-sum-n)) , ((proj₁ n-sum-n) + (proj₂ n-sum-n))\n\nsum-i : ℕ → ℕ\nsum-i n = proj₂ (sum-i-aux n)\n\n{-\nfib : ℕ → ℕ\nfib zero = 1\nfib (suc zero) = 1\nfib (suc (suc n)) = fib n + fib (suc n)\n\nfib-i-aux : ℕ → ℕ × ℕ  = fib-i-aux n = fib n , fib (suc n)\n-}\n", "meta": {"hexsha": "a7a3c3a9c1b06c80d0a1aeafe824e6786ecf33d8", "size": 2702, "ext": "agda", "lang": "Agda", "max_stars_repo_path": "Type Theory/exercises-03-wednesday.agda", "max_stars_repo_name": "FoxySeta/mgs-2021", "max_stars_repo_head_hexsha": "f328e596d98a7d052b34144447dd14de0f57e534", "max_stars_repo_licenses": ["MIT"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Type Theory/exercises-03-wednesday.agda", "max_issues_repo_name": "FoxySeta/mgs-2021", "max_issues_repo_head_hexsha": "f328e596d98a7d052b34144447dd14de0f57e534", "max_issues_repo_licenses": ["MIT"], "max_issues_count": 2, "max_issues_repo_issues_event_min_datetime": "2021-07-14T20:34:53.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-14T20:35:48.000Z", "max_forks_repo_path": "Type Theory/exercises-03-wednesday.agda", "max_forks_repo_name": "FoxySeta/mgs-2021", "max_forks_repo_head_hexsha": "f328e596d98a7d052b34144447dd14de0f57e534", "max_forks_repo_licenses": ["MIT"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 25.7333333333, "max_line_length": 73, "alphanum_fraction": 0.6350851221, "num_tokens": 959, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9669140206578809, "lm_q2_score": 0.9219218262741298, "lm_q1q2_score": 0.8914191397749752}}
{"text": "module x01naturals where\n\n{-\n------------------------------------------------------------------------------\nnaturals : inductive datatype\n\ndefinition as a pair of inference rules:\n                 -- no assumptions\n    ---------\n    zero  : ℕ    -- base case\n\n    m     : ℕ    -- assuming m is Natural\n    ---------\n    suc m : ℕ    -- inductive case : then suc m is also a Natural\n\nin Agda:\n-}\n\ndata ℕ : Set where\n  zero :     ℕ -- base case\n  suc  : ℕ → ℕ -- inductive case\n\n-- #### Exercise Write out `7` in longhand.\n\nseven : ℕ\nseven = suc (suc (suc (suc (suc (suc (suc zero))))))\n\n{-\ninference rules consists of\n- zero or more _judgments_ written above a horizontal line, called the _hypotheses_\n- single judgment written below, called the _conclusion_\n\n`Set` : the way in Agda of saying that it is a type\n\nInductive case defines natural numbers in terms of natural numbers.\nBase case jump starts the process of induction.\n\n## Philosophy and history\n\ninductive def of nats is relatively recent\n- 1888\n  Richard Dedekind's paper \"_Was sind und was sollen die Zahlen?_\"\n  (What are and what should be the numbers?)\n- 1889\n  Giuseppe Peano's book \"_Arithmetices principia, nova methodo exposita_\"\n  (The principles of arithmetic presented by a new method)\n\n------------------------------------------------------------------------------\n## PRAGMA\n\nthe line\n-}\n{-# BUILTIN NATURAL ℕ #-}\n{-\ntells Agda that `ℕ` corresponds to the natural numbers\n- pragma must be given a previously declared type (in this case `ℕ`)\n- enables 0, 1, 2, ... shorthand\n- enables efficient internal Haskell representation\n\n## IMPORTS\n\nTo write equations that hold between terms involving natural numbers\nimport the definition of equality and notations for reasoning about equality\nfrom the Agda standard library:\n-}\n\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq             using (_≡_; refl)\nopen Eq.≡-Reasoning using (begin_; _≡⟨⟩_; _∎)\n\n{-\n1st line : brings equality module into scope and gives it the name `Eq`.\n2nd line : opens that module and adds names in `using` clause into scope.\n3rd line : opens module that specifies operators to support reasoning about equivalence\n           and adds names in `using` clause into scope.\n\nlater will see their definitions in Chapter Equality\n\nunderbars indicate where terms appear\n- `_≡_` and `_≡⟨⟩_`  : terms on each side\n- `begin_` is prefix : terms after\n- : `_∎`             : terms before\n\n------------------------------------------------------------------------------\n## operations on naturals are recursive functions\n-}\n\n-- ADDITION\n_+_ : ℕ → ℕ → ℕ\nzero    + n =          n  -- base case      :  0      + n  ≡  n\n(suc m) + n = suc (m + n) -- inductive case : (1 + m) + n  ≡  1 + (m + n) : associativity\n\n{-\nInductive definition works because addition of larger numbers is defined\nin terms of addition of smaller numbers : WELL FOUNDED\n-}\n\n_ : 2 + 3 ≡ 5\n_ =\n  begin\n    2 + 3                                       ≡⟨⟩ -- is shorthand for\n    (suc (suc zero)) + (suc (suc (suc zero)))   ≡⟨⟩ -- inductive case\n    suc ((suc zero)  + (suc (suc (suc zero))))  ≡⟨⟩ -- inductive case\n    suc (suc (zero   + (suc (suc (suc zero))))) ≡⟨⟩ -- base case\n    suc (suc           (suc (suc (suc zero))))  ≡⟨⟩ -- is longhand for\n    5\n  ∎ -- ∎ is QED\n\n-- equivalent\n\n_ : 2 + 3 ≡ 5\n_ =\n  begin\n    2 + 3             ≡⟨⟩\n    suc      (1 + 3)  ≡⟨⟩\n    suc (suc (0 + 3)) ≡⟨⟩\n    suc (suc      3)  ≡⟨⟩\n    5\n\n\n-- A binary relation is said to be REFLEXIVE if every value relates to itself.\n-- Evidence that a value is equal to itself is written `refl`\n\n_ : 2 + 3 ≡ 5\n_ = refl\n\n{-\nabove derivations consist of\n- a signature (written with a colon, `:`), giving a type\n- a binding (written with an equal sign, `=`), giving a term of the given type.\n\nthe dummy name `_` can be reused\nconsists of a series of terms separated by `≡⟨⟩`.\n\nduality of interpretation\n- type as a proposition\n- term as evidence (aka proof)\n\n#### Exercise `+-example` Compute `3 + 4`\n-}\n\n_ : 3 + 4 ≡ 7\n_ =\n  begin\n    3 + 4                   ≡⟨⟩\n    suc           (2 + 4)   ≡⟨⟩\n    suc (suc      (1 + 4))  ≡⟨⟩\n    suc (suc (suc (0 + 4))) ≡⟨⟩\n    suc (suc (suc      4))  ≡⟨⟩\n    7\n\n\n_ : 3 + 4 ≡ 7\n_ = refl\n\n{-\n------------------------------------------------------------------------------\n## MULTIPLICATION\n-}\n\n_*_ : ℕ → ℕ → ℕ\nzero    * n  =  zero        --  0      * n  ≡  0\n(suc m) * n  =  n + (m * n) -- (1 + m) * n  ≡  n + (m * n)\n\n{-\ncomputing `m * n` returns the sum of `m` copies of `n`.\n\n2nd line because multiplication distributes over addition:\n\n    (m + n) * p  ≡  (m * p) + (n * p)\n\nWe get the second equation from the third by\n- taking `m` to be `1`\n- `n` to be `m`\n- `p` to be `n`\n- then use fact that one is identity for multiplication, so\n- `1 * n ≡ n`\n\nWELL FOUNDED : _*_ of larger numbers is defined in terms of _*_ of smaller numbers\n-}\n\n_ : 2 * 3 ≡ 6\n_ =\n  begin\n    2 * 3             ≡⟨⟩ -- inductive case\n    3 + (1 * 3)       ≡⟨⟩ -- inductive case\n    3 + (3 + (0 * 3)) ≡⟨⟩ -- base case\n    3 + (3 + 0)       ≡⟨⟩ -- simplify\n    6\n\n\n{-\n#### Exercise `*-example` Compute `3 * 4`\n-}\n\n_ : 3 * 4 ≡ 12\n_ =\n  begin\n    3 * 4                   ≡⟨⟩\n    4 + (2 * 4)             ≡⟨⟩\n    4 + (4 + (1 * 4))       ≡⟨⟩\n    4 + (4 + (4 + (0 * 4))) ≡⟨⟩ -- base case\n    4 + (4 + (4 +  0))      ≡⟨⟩ -- addition\n    12\n\n\n-- HC\n\n_*hc_ : ℕ → ℕ → ℕ\nn *hc zero    =  zero\nn *hc (suc m) =  (n *hc m) + n\n\n_ : 2 *hc 3 ≡ 6\n_ =\n  begin\n       2 *hc 3                  ≡⟨⟩\n      (2 *hc 2)             + 2 ≡⟨⟩\n     ((2 *hc 1)        + 2) + 2 ≡⟨⟩\n    (((2 *hc 0)  + 2)  + 2) + 2 ≡⟨⟩\n     ((      0   + 2)  + 2) + 2 ≡⟨⟩\n     (             2   + 2) + 2 ≡⟨⟩\n    6\n\n\n{-\n------------------------------------------------------------------------------\n#### Exercise (recommended) Define EXPONENTIATION\n-}\n\n_^_ : ℕ → ℕ → ℕ\nm ^ 0       = 1            -- m ^ 0       = 1\nm ^ (suc n) = m * (m ^ n)  -- m ^ (1 + n) = m * (m ^ n)\n\n-- Check that `3 ^ 4` is `81`.\n_ : 3 ^ 4 ≡ 81\n_ = refl\n\n_ : 2 ^ 3 ≡ 8\n_ = refl\n\n{-\n------------------------------------------------------------------------------\n## MONUS SUBTRACTION\n\nuses pattern matching against both arguments:\n-}\n\n_∸_ : ℕ → ℕ → ℕ\nm     ∸ zero   =  m\nzero  ∸ suc n  =  zero\nsuc m ∸ suc n  =  m ∸ n\n\n-- WELL FOUNDED : monus on bigger numbers is defined in terms of monus on smaller numbers.\n\n_ =\n  begin\n    3 ∸ 2 ≡⟨⟩\n    2 ∸ 1 ≡⟨⟩\n    1 ∸ 0 ≡⟨⟩\n    1\n\n\n_ =\n  begin\n    2 ∸ 3 ≡⟨⟩\n    1 ∸ 2 ≡⟨⟩\n    0 ∸ 1 ≡⟨⟩\n    0\n\n\n{-\n#### Exercise Compute `5 ∸ 3` and `3 ∸ 5`, reasoning as a chain of equations.\n-}\n\n_ =\n  begin\n    5 ∸ 3 ≡⟨⟩\n    4 ∸ 2 ≡⟨⟩\n    3 ∸ 1 ≡⟨⟩\n    2 ∸ 0 ≡⟨⟩\n    2\n\n\n_ =\n  begin\n    3 ∸ 5 ≡⟨⟩\n    2 ∸ 4 ≡⟨⟩\n    1 ∸ 3 ≡⟨⟩\n    0 ∸ 2 ≡⟨⟩\n    0\n\n\n{-\n------------------------------------------------------------------------------\n## PRECEDENCE\n\nApplication higher than operators   : `suc m + n` means `(suc m) + n`\n\nmultiplication higher than addition : `n + m * n` means `n + (m * n)`\n\naddition _associates to the left_   : `m + n + p` means `(m + n) + p`\n\ndeclare precedence and associativity of infix operators\n-}\n\ninfixl 6  _+_  _∸_\ninfixl 7  _*_\n\n{-\n`infixl` : associate to the left\n`infixr` : associate to the right\n`infix`  : indicates that parentheses are required to disambiguate\n\n------------------------------------------------------------------------------\n## CURRYING\n\na function of two arguments in terms of\n- a function of the first argument\n- that returns a function of the second argument\n\nFunction arrows associate to the right : `ℕ → ℕ → ℕ` stands for `ℕ → (ℕ → ℕ)`\nApplication associates to the left     : `_+_ 2 3` stands for `(_+_ 2) 3`\n\nNamed for Haskell Curry.\n\nThe idea actually appears in the _Begriffsschrift_ of Gottlob Frege, published in 1879.\n\n------------------------------------------------------------------------------\n## The story of creation, revisited\n\ninductive definition defines naturals in terms of naturals\nrecursive definition defines addition in terms of addition\n\n           n : ℕ\n    --------------\n    zero + n  =  n\n\n         m  + n  =      p\n    ---------------------\n    (suc m) + n  =  suc p\n\n------------------------------------------------------------------------------\n## The story of creation, finitely {name=finite-creation}\nSKIPPED\n\n------------------------------------------------------------------------------\n## Writing definitions interactively\n\n    _+_ : ℕ → ℕ → ℕ\n    m + n = ?\n\n? : ask Agda to fill in\n`C-c C-l`\n? replaced:\n\n    _+_ : ℕ → ℕ → ℕ\n    m + n = { }0\n\nempty braces : a \"numbered\" *hole*\nEmacs will also create a window displaying the text\n\n    ?0 : ℕ\n\nto indicate that hole 0 is to be filled in with a term of type `ℕ`.\n\n`C-c C-f` (for **f**orward) will move you into the next hole.\n\nTo define addition by recursion on the first argument:\ncursor in hole : `C-c C-c` (for **c**ase).\nprompt:\n\n    pattern variables to case (empty for split on result):\n\nType `m` : case split on `m`:\n\n    _+_ : ℕ → ℕ → ℕ\n    zero + n = { }0\n    suc m + n = { }1\n\nwindow at the bottom type of each:\n\n    ?0 : ℕ\n    ?1 : ℕ\n\nin hole 0 : `C-c C-,`\ndisplays info on required type of hole, and what free variables are available:\n\n    Goal: ℕ\n    ————————————————————————————————————————————————————————————\n    n : ℕ\n\nsuggests filling the hole with `n`\nafter hole filled in : `C-c C-space` : removes hole:\n\n    _+_ : ℕ → ℕ → ℕ\n    zero + n = n\n    suc m + n = { }1\n\nin hole 1 : `C-c C-,`\ndisplays info\n\n    Goal: ℕ\n    ————————————————————————————————————————————————————————————\n    n : ℕ\n    m : ℕ\n\nin hole : `C-c C-r` (for **r**efine)\nwill fill in with a constructor (if there is a unique choice)\nor tell you what constructors you might use, if there is a choice\n\n    Don't know which constructor to introduce of zero or suc\n\nfill in hole with `suc ?` : `C-c C-space`\n\n    _+_ : ℕ → ℕ → ℕ\n    zero + n = n\n    suc m + n = suc { }1\n\nin new hole : `C-c C-,`\n\n    Goal: ℕ\n    ————————————————————————————————————————————————————————————\n    n : ℕ\n    m : ℕ\n\nfill hole with `m + n` : `C-c C-space`\ncomplete the program:\n\n    _+_ : ℕ → ℕ → ℕ\n    zero + n = n\n    suc m + n = suc (m + n)\n\n------------------------------------------------------------------------------\n## More pragmas\n-}\n\n{-# BUILTIN NATPLUS  _+_ #-}\n{-# BUILTIN NATTIMES _*_ #-}\n{-# BUILTIN NATMINUS _∸_ #-}\n\n{-\ntells Agda correspondance between operators correspond and the usual ones.\nEnables using corresponding Haskell operators on arbitrary-precision integer type.\n\n------------------------------------------------------------------------------\n#### Exercise `Bin` (stretch) {name=Bin} represent nat as bitstring\n-}\n\ndata Bin : Set where\n  ⟨⟩ : Bin\n  _O : Bin → Bin\n  _I : Bin → Bin\n\n{-\nbitstring 1011 (eleven) encoded as ⟨⟩ I O I I\nor, with leading zeros,        ⟨⟩ O O I O I I\n\ndefine\n\n    inc : Bin → Bin\n\nconverts bitstring to bitstring for next higher number\n-}\n\n-- this definition has TWO forms of ZERO: '⟨⟩' and '⟨⟩ O'\n-- will probably cause problems later on\ninc : Bin → Bin\ninc  ⟨⟩    = ⟨⟩      I\ninc (⟨⟩ O) = ⟨⟩      I\ninc (⟨⟩ I) = ⟨⟩   I  O\ninc (b  O) =      b  I\ninc (b  I) = (inc b) O\n\n-- `IOII` eleven to `1100` twelve\n_ : inc (⟨⟩ I O I I) ≡ ⟨⟩ I I O O\n_ = refl\n\n-- Confirm correct answer for the bitstrings encoding zero through four.\n\n_ : inc (⟨⟩     O) ≡ ⟨⟩     I\n_ = refl\n_ : inc (⟨⟩     I) ≡ ⟨⟩   I O\n_ = refl\n_ : inc (⟨⟩   I O) ≡ ⟨⟩   I I\n_ = refl\n_ : inc (⟨⟩   I I) ≡ ⟨⟩ I O O\n_ = refl\n_ : inc (⟨⟩ I O O) ≡ ⟨⟩ I O I\n_ = refl\n\n{-\nusing above, define\n\n    to   : ℕ → Bin\n    from : Bin → ℕ\n\n`to` should not have leading zeros, except represent zero as `⟨⟩ O`\n\nconfirm for zero through four\n-}\n\nto : ℕ → Bin\nto   zero  = ⟨⟩ O\nto (suc m) = inc (to m)\n\nfrom : Bin → ℕ\nfrom     ⟨⟩ = 0\nfrom (b  O) = 2 * from b\nfrom (b  I) = 2 * from b + 1\n\n_ : from (⟨⟩     O) ≡ 0\n_ = refl\n_ : from (⟨⟩     I) ≡ 1\n_ = refl\n_ : from (⟨⟩   I O) ≡ 2\n_ = refl\n_ : from (⟨⟩   I I) ≡ 3\n_ = refl\n_ : from (⟨⟩ I O O) ≡ 4\n_ = refl\n_ : from (⟨⟩ I I O) ≡ 6\n_ = refl\n\n_ : to 0 ≡ (⟨⟩     O)\n_ = refl\n_ : to 1 ≡ (⟨⟩     I)\n_ = refl\n_ : to 2 ≡ (⟨⟩   I O)\n_ = refl\n_ : to 3 ≡ (⟨⟩   I I)\n_ = refl\n_ : to 4 ≡ (⟨⟩ I O O)\n_ = refl\n_ : to 6 ≡ (⟨⟩ I I O)\n_ = refl\n\n_ : from (to 12) ≡ 12\n_ = refl\n\n_ : to (from (⟨⟩ I I O O)) ≡ ⟨⟩ I I O O\n_ = refl\n\n-- 842 exercise : bin-+\n\n_bin-+_ : Bin → Bin → Bin\n⟨⟩     bin-+ ⟨⟩     = ⟨⟩\n⟨⟩     bin-+ b      = b\nb      bin-+ ⟨⟩     = b\n(bl O) bin-+ (br O) = let r = bl bin-+ br in      r  O\n(bl O) bin-+ (br I) = let r = bl bin-+ br in      r  I\n(bl I) bin-+ (br O) = let r = bl bin-+ br in      r  I\n(bl I) bin-+ (br I) = let r = bl bin-+ br in (inc r) O\n\n_ : (⟨⟩)       bin-+ (⟨⟩)     ≡ (⟨⟩)\n_ = refl\n_ : (⟨⟩)       bin-+ (⟨⟩   O) ≡ (⟨⟩       O)\n_ = refl\n_ : (⟨⟩     O) bin-+ (⟨⟩)     ≡ (⟨⟩       O)\n_ = refl\n_ : (⟨⟩     O) bin-+ (⟨⟩   O) ≡ (⟨⟩       O)\n_ = refl\n_ : (⟨⟩     O) bin-+ (⟨⟩   I) ≡ (⟨⟩       I)\n_ = refl\n_ : (⟨⟩     I) bin-+ (⟨⟩   I) ≡ (⟨⟩     I O)\n_ = refl\n_ : (⟨⟩   I O) bin-+ (⟨⟩ I O) ≡ (⟨⟩   I O O)\n_ = refl\n_ : (⟨⟩   I I) bin-+ (⟨⟩ I I) ≡ (⟨⟩   I I O)\n_ = refl\n_ : (⟨⟩ I O I) bin-+ (⟨⟩   I) ≡ (⟨⟩   I I O)\n_ = refl\n_ : (⟨⟩ I I I) bin-+ (⟨⟩   I) ≡ (⟨⟩ I O O O)\n_ = refl\n\n------------------------------------------------------------------------------\n-- hc exercise : explore two representation of ZERO\n\nz1-bin-+ : ∀ (b1 b2 : Bin)\n        → b1          ≡ ⟨⟩\n        → b1 bin-+ b2 ≡ b2\nz1-bin-+ b1 ⟨⟩ p     -- (b1 bin-+  ⟨⟩)    ≡ ⟨⟩\n  rewrite\n    p                -- (⟨⟩ bin-+  ⟨⟩)    ≡ ⟨⟩\n                     --            ⟨⟩     ≡ ⟨⟩\n  = refl\nz1-bin-+ b1 (⟨⟩ O) p -- (b1 bin-+ (⟨⟩ O)) ≡ (⟨⟩ O)\n  rewrite\n    p                -- (⟨⟩ bin-+ (⟨⟩ O)) ≡ (⟨⟩ O)\n                     --           (⟨⟩ O)  ≡ (⟨⟩ O)\n  = refl\nz1-bin-+ b1 (b O) p  -- (b1 bin-+ (b  O)) ≡ (b  O)\n  rewrite\n    p                -- (⟨⟩ bin-+  (b O)) ≡ (b  O)\n                     --            (b O)  ≡ (b  O)\n  = refl\nz1-bin-+ b1 (b I) p  -- (b1 bin-+  (b I)) ≡ (b  I)\n  rewrite\n    p                -- (⟨⟩ bin-+  (b I)) ≡ (b  I)\n                     --            (b I)  ≡ (b  I)\n  = refl\n\nz-bin-+ : ∀ (b1 b2 : Bin)\n        → b1          ≡ (⟨⟩ O)\n        → b1 bin-+ b2 ≡ b2\nz-bin-+ b1 ⟨⟩ p     -- (b1     bin-+ ⟨⟩)     ≡ ⟨⟩\n  rewrite\n    p               -- ((⟨⟩ O) bin-+ ⟨⟩)     ≡ ⟨⟩\n                    --  (⟨⟩ O)               ≡ ⟨⟩\n  = {!!}\nz-bin-+ b1 (⟨⟩ O) p -- (b1     bin-+ (⟨⟩ O)) ≡ (⟨⟩ O)\n  rewrite\n    p               -- ((⟨⟩ O) bin-+ (⟨⟩ O)) ≡ (⟨⟩ O)\n                    --               (⟨⟩ O)  ≡ (⟨⟩ O)\n  = refl\nz-bin-+ b1 (b O) p  -- (b1     bin-+ (b  O)) ≡ (b  O)\n  rewrite\n    p               -- ((⟨⟩ O) bin-+ (b  O)) ≡ (b  O)\n                    -- ((⟨⟩    bin-+ b)  O)  ≡ (b  O)\n  = {!!}\nz-bin-+ b1 (b I) p  -- (b1     bin-+ (b  I)) ≡ (b  I)\n  rewrite\n    p               -- ((⟨⟩ O) bin-+ (b  I)) ≡ (b  I)\n                    -- ((⟨⟩    bin-+ b)  I)  ≡ (b  I)\n  = {!!}\n\nhc : ∀ (m n : ℕ) →  from (to m bin-+ to n) ≡ m + n\nhc  zero   n     -- from (to zero bin-+ to n) ≡ zero + n\n  = {!!}         -- from ( (⟨⟩ O) bin-+ to n) ≡        n\nhc (suc m) n = {!!}\n\n{-\n------------------------------------------------------------------------------\n## Standard library\n\nwhere to find relevant definitions in the standard library\n\nNaturals, constructors, operators :\n\nimport Data.Nat using (ℕ; zero; suc; _+_; _*_; _^_; _∸_)\n\nhttps://agda.readthedocs.io/en/v2.6.1/language/pragmas.html\n\n------------------------------------------------------------------------------\n## Unicode\n\nThis chapter uses the following unicode:\n\n char  code    name                             emacs\n------------------------------------------------------\n    ℕ  U+2115  DOUBLE-STRUCK CAPITAL N          (\\bN)\n    →  U+2192  RIGHTWARDS ARROW                 (\\to, \\r, \\->)\n    ∸  U+2238  DOT MINUS                        (\\.-)\n    ≡  U+2261  IDENTICAL TO                     (\\==)\n    ⟨  U+27E8  MATHEMATICAL LEFT ANGLE BRACKET  (\\<)\n    ⟩  U+27E9  MATHEMATICAL RIGHT ANGLE BRACKET (\\>)\n    ∎  U+220E  END OF PROOF                     (\\qed)\n\n\n`\\r` : variety of right arrows\n\n`\\l` : variety of left arrows\n\nAll the characters supported by `agda-mode`:\n\n    M-x agda-input-show-translations\n\nTo see how to input an existing specific Unicode character in an agda file,\nmove cursor to character\n\n    M-x quail-show-key\n-}\n\n\n\n", "meta": {"hexsha": "4bd982345eb69efd96087a6fb29b80172284b584", "size": 16174, "ext": "agda", "lang": "Agda", "max_stars_repo_path": "agda/book/Programming_Language_Foundations_in_Agda/x01naturals.agda", "max_stars_repo_name": "haroldcarr/learn-haskell-coq-ml-etc", "max_stars_repo_head_hexsha": "3dc7abca7ad868316bb08f31c77fbba0d3910225", "max_stars_repo_licenses": ["Unlicense"], "max_stars_count": 36, "max_stars_repo_stars_event_min_datetime": "2015-01-29T14:37:15.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-30T06:55:03.000Z", "max_issues_repo_path": "agda/book/Programming_Language_Foundations_in_Agda/x01naturals.agda", "max_issues_repo_name": "haroldcarr/learn-haskell-coq-ml-etc", "max_issues_repo_head_hexsha": "3dc7abca7ad868316bb08f31c77fbba0d3910225", "max_issues_repo_licenses": ["Unlicense"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "agda/book/Programming_Language_Foundations_in_Agda/x01naturals.agda", "max_forks_repo_name": "haroldcarr/learn-haskell-coq-ml-etc", "max_forks_repo_head_hexsha": "3dc7abca7ad868316bb08f31c77fbba0d3910225", "max_forks_repo_licenses": ["Unlicense"], "max_forks_count": 8, "max_forks_repo_forks_event_min_datetime": "2015-04-13T21:40:15.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-21T15:58:10.000Z", "avg_line_length": 23.9614814815, "max_line_length": 90, "alphanum_fraction": 0.4559169037, "num_tokens": 5735, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9664104919313443, "lm_q2_score": 0.9173026629768591, "lm_q1q2_score": 0.8864909177773986}}
{"text": "module sum-downFrom where\n\nimport Relation.Binary.PropositionalEquality as Eq\nopen Eq using (_≡_; refl; sym; cong)\nopen Eq.≡-Reasoning\nopen import Data.Nat using (ℕ; zero; suc; _+_; _*_; _∸_; _≤_; s≤s; z≤n)\nopen import Data.Nat.Properties using\n  (*-suc; *-identityʳ; *-distribʳ-+; *-distribˡ-∸; +-∸-assoc; +-∸-comm; m+n∸m≡n; m≤m*n)\n\nopen import lists using (List; []; _∷_; [_,_,_]; sum)\n\n-- (n - 1), ⋯ , 0 を返す\ndownFrom : ℕ → List ℕ\ndownFrom zero    = []\ndownFrom (suc n) = n ∷ downFrom n\n\n_ : downFrom 3 ≡ [ 2 , 1 , 0 ]\n_ = refl\n\n-- n ≤ n * n の証明\nn≤n*n : ∀ (n : ℕ) → n ≤ n * n\nn≤n*n zero    = z≤n\nn≤n*n (suc n) = m≤m*n (suc n) (s≤s z≤n)\n\n-- n ≤ n * 2 の証明\nn≤n*2 : ∀ (n : ℕ) → n ≤ n * 2\nn≤n*2 n = m≤m*n n (s≤s z≤n)\n\n-- n * 2 ∸ n = n の証明\nn*2∸n≡n : ∀ (n : ℕ) → n * 2 ∸ n ≡ n\nn*2∸n≡n n =\n  begin\n    n * 2 ∸ n\n  ≡⟨ cong (_∸ n) (*-suc n 1) ⟩ -- 積の展開\n    n + n * 1 ∸ n\n  ≡⟨ m+n∸m≡n n (n * 1) ⟩ -- n ∸ n の除去\n    n * 1\n  ≡⟨ *-identityʳ n ⟩ -- * 1 の除去\n    n\n\n\n-- m * (n ∸ 1) = m * n ∸ m の証明\nm*[n∸1]≡m*n∸m : ∀ (m n : ℕ) → m * (n ∸ 1) ≡ m * n ∸ m\nm*[n∸1]≡m*n∸m m n =\n  begin\n    m * (n ∸ 1)\n  ≡⟨ *-distribˡ-∸ m n 1 ⟩ -- n * の分配\n    m * n ∸ m * 1\n  ≡⟨ cong (m * n ∸_) (*-identityʳ m) ⟩ -- * 1 の除去\n    m * n ∸ m\n\n\n-- (n - 1) + ⋯ + 0 と n * (n ∸ 1) / 2 が等しいことの証明\nsum-downFrom : ∀ (n : ℕ) → sum (downFrom n) * 2 ≡ n * (n ∸ 1)\nsum-downFrom zero =\n  begin\n    sum (downFrom zero) * 2\n  ≡⟨⟩\n    sum [] * 2\n  ≡⟨⟩\n    zero\n    -- = zero * (zero ∸ 1)\n\nsum-downFrom (suc n) =\n  begin\n    sum (downFrom (suc n)) * 2\n  ≡⟨⟩\n    sum (n ∷ downFrom n) * 2\n  ≡⟨⟩\n    (n + sum (downFrom n)) * 2\n  ≡⟨ *-distribʳ-+ 2 n (sum (downFrom n)) ⟩ -- * 2 の分配\n    (n * 2) + (sum (downFrom n)) * 2\n  ≡⟨ cong (n * 2 +_) (sum-downFrom n) ⟩ -- 帰納法\n    (n * 2) + (n * (n ∸ 1))\n  ≡⟨ cong (n * 2 +_) (m*[n∸1]≡m*n∸m n n) ⟩ -- n * の分配\n    (n * 2) + (n * n ∸ n)\n  ≡⟨ sym (+-∸-assoc (n * 2) (n≤n*n n)) ⟩ -- 結合法則\n    (n * 2) + n * n ∸ n\n  ≡⟨ +-∸-comm (n * n) (n≤n*2 n) ⟩ -- 交換法則\n    (n * 2) ∸ n + n * n\n  ≡⟨ cong (_+ n * n) (n*2∸n≡n n) ⟩ -- n ∸ n の除去\n    n + n * n\n    -- = n * (suc n)\n    -- = (suc n) * n\n    -- = (suc n) * ((suc n) ∸ 1)\n\n", "meta": {"hexsha": "14301fd8a7dfba069f44363617059136f4432394", "size": 2099, "ext": "agda", "lang": "Agda", "max_stars_repo_path": "part1/lists/sum-downFrom.agda", "max_stars_repo_name": "akiomik/plfa-solutions", "max_stars_repo_head_hexsha": "df7722b88a9b3dfde320a690b78c4c1ef8c7c547", "max_stars_repo_licenses": ["Apache-2.0"], "max_stars_count": 1, "max_stars_repo_stars_event_min_datetime": "2020-07-07T09:42:22.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-07T09:42:22.000Z", "max_issues_repo_path": "part1/lists/sum-downFrom.agda", "max_issues_repo_name": "akiomik/plfa-solutions", "max_issues_repo_head_hexsha": "df7722b88a9b3dfde320a690b78c4c1ef8c7c547", "max_issues_repo_licenses": ["Apache-2.0"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "part1/lists/sum-downFrom.agda", "max_forks_repo_name": "akiomik/plfa-solutions", "max_forks_repo_head_hexsha": "df7722b88a9b3dfde320a690b78c4c1ef8c7c547", "max_forks_repo_licenses": ["Apache-2.0"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 24.1264367816, "max_line_length": 87, "alphanum_fraction": 0.4249642687, "num_tokens": 1246, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9615338123908151, "lm_q2_score": 0.9032942099580604, "lm_q1q2_score": 0.8685479254115233}}
{"text": "module perm-keep-length where\n\nopen import nat\nopen import list\nopen import nondet\nopen import bool\nopen import bool-thms\nopen import eq\nopen import nat-thms\nopen import nondet-thms\n\n-- Non-deterministic insert:\nndinsert : {a : Set} → a → 𝕃 a  → ND (𝕃 a)\nndinsert x []        = Val (x :: [])\nndinsert x (y :: ys) = Val (x :: y :: ys)\n                    ?? (_::_ y) $* (ndinsert x ys)\n                    \n-- Permutation:\nperm : {a : Set} → 𝕃 a  → ND (𝕃 a)\nperm []        = Val []\nperm (x :: xs) = ndinsert x *$* (perm xs)\n\n\n----------------------------------------------------------------------\n\n-- Non-deterministic insertion increases the list length by one:\ninsert-inc-length : {a : Set} (x : a) → (xs : 𝕃 a)\n      → (ndinsert x xs) satisfy (λ ys → length ys =ℕ suc (length xs)) ≡ tt\ninsert-inc-length x [] = refl\ninsert-inc-length x (y :: ys)\n rewrite =ℕ-refl (length ys)\n       | satisfy-$* (_::_ y) (ndinsert x ys)\n                    (λ zs → length zs =ℕ suc (suc (length ys)))\n       | insert-inc-length x ys = refl\n\n-- If the length of the input list is n, ndinsert increases it to (suc n):\ninsert-suc-length : {a : Set} → (n : ℕ) (x : a) (xs : 𝕃 a)\n       → length xs =ℕ n ≡ tt\n       → ndinsert x xs satisfy (λ ys → length ys =ℕ suc n) ≡ tt\ninsert-suc-length n x [] p = p \ninsert-suc-length n x (y :: ys) p\n rewrite =ℕ-to-≡ {suc (length ys)} {n} p | =ℕ-refl n |\n         satisfy-$* (_::_ y) (ndinsert x ys) (λ zs → length zs =ℕ suc n)\n       | sym (=ℕ-to-≡ {suc (length ys)} {n} p)\n       | insert-inc-length x ys\n = refl\n\n-- The previous lemma is also valid on non-deterministic lists:\nins-suc-nondet : {a : Set} → (n : ℕ) → (x : a) → (t : ND (𝕃 a))\n       → t satisfy (λ xs → length xs =ℕ n) ≡ tt\n       → (ndinsert x *$* t) satisfy (λ xs → length xs =ℕ suc n) ≡ tt\nins-suc-nondet n x (Val xs) p = insert-suc-length n x xs p\nins-suc-nondet n x (t1 ?? t2) p\n rewrite ins-suc-nondet n x t1 (&&-fst p) \n       | ins-suc-nondet n x t2 (&&-snd {t1 satisfy (λ xs → length xs =ℕ n)} p)\n = refl\n\n-- The length of a permuted list is identical to the length of the list:\nperm-length : {a : Set} → (xs : 𝕃 a)\n       → (perm xs) satisfy (λ ys → length ys =ℕ length xs) ≡ tt\nperm-length [] = refl\nperm-length (x :: xs) = ins-suc-nondet (length xs) x (perm xs) (perm-length xs)\n", "meta": {"hexsha": "c32477c2e87213a70c0bf9174ab700e0f8614578", "size": 2278, "ext": "agda", "lang": "Agda", "max_stars_repo_path": "nondet/perm-keep-length.agda", "max_stars_repo_name": "mihanus/curry-agda", "max_stars_repo_head_hexsha": "b7cfdda11cdadeba882b6b72d75448acd8b0a294", "max_stars_repo_licenses": ["BSD-3-Clause"], "max_stars_count": null, "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "nondet/perm-keep-length.agda", "max_issues_repo_name": "mihanus/curry-agda", "max_issues_repo_head_hexsha": "b7cfdda11cdadeba882b6b72d75448acd8b0a294", "max_issues_repo_licenses": ["BSD-3-Clause"], "max_issues_count": null, "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "nondet/perm-keep-length.agda", "max_forks_repo_name": "mihanus/curry-agda", "max_forks_repo_head_hexsha": "b7cfdda11cdadeba882b6b72d75448acd8b0a294", "max_forks_repo_licenses": ["BSD-3-Clause"], "max_forks_count": null, "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null, "avg_line_length": 36.1587301587, "max_line_length": 79, "alphanum_fraction": 0.5496049166, "num_tokens": 780, "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9609517083920617, "lm_q2_score": 0.8947894562828416, "lm_q1q2_score": 0.8598494566662006}}