added ListsAns

This commit is contained in:
wadler 2018-02-07 11:46:48 -04:00
parent 8011389aa9
commit e37d1692c9
3 changed files with 97 additions and 29 deletions

View file

@ -14,7 +14,7 @@ import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; sym; trans; cong)
open Eq.≡-Reasoning
open import Data.Nat using (; zero; suc; _+_; _*_)
open import Data.Nat.Properties.Simple using (distribʳ-*-+)
open import Data.Nat.Properties.Simple using (distribʳ-*-+; *-comm)
\end{code}
## Lists
@ -380,33 +380,6 @@ of the first list, reversing a list in this way takes
time proportional to the *square* of the length of the
list, since `1 + ⋯ + n ≡ n * (n + 1) / 2`.
\begin{code}
upto : → List
upto zero = []
upto (suc n) = suc n ∷ upto n
sum : List
sum [] = zero
sum (x ∷ xs) = x + sum xs
sum-upto : ∀ (n : ) → 2 * sum (upto n) ≡ n * suc n
sum-upto zero = refl
sum-upto (suc n) = {!!}
{-
begin
2 * sum (upto (suc n))
≡⟨⟩
2 * sum (suc n ∷ upto n)
≡⟨⟩
2 * (suc n + sum (upto n))
≡⟨ +-dist-* 2 (suc n) (sum (upto n)) ⟩
(2 * suc n) + (2 * sum (upto n))
≡⟨ cong (_+_ (2 * suc n)) (sup-upto n) ⟩
(2 * suc n) + (n * suc n)
≡⟨ sym (+-dist-*
-}
\end{code}
## Reverse
@ -431,6 +404,45 @@ ex₆ : foldr _+_ 0 ([ 1 , 2 , 3 ]) ≡ 6
ex₆ = refl
\end{code}
\begin{code}
downto : → List
downto zero = []
downto (suc n) = suc n ∷ downto n
sum : List
sum = foldr _+_ 0
infix 6 _+
_+ :
(m +) n = m + n
cong2 : ∀ {A B C : Set} {x x : A} {y y : B} →
(f : A → B → C) → (x ≡ x) → (y ≡ y) → (f x y ≡ f x y)
cong2 f x≡x y≡y rewrite x≡x | y≡y = refl
sum-downto : ∀ (n : ) → sum (downto n) * 2 ≡ suc n * n
sum-downto zero = refl
sum-downto (suc n) =
begin
sum (downto (suc n)) * 2
≡⟨⟩
sum (suc n ∷ downto n) * 2
≡⟨⟩
(suc n + sum (downto n)) * 2
≡⟨ distribʳ-*-+ 2 (suc n) (sum (downto n)) ⟩
suc n * 2 + sum (downto n) * 2
≡⟨ cong (suc n * 2 +) (sum-downto n) ⟩
suc n * 2 + suc n * n
≡⟨ cong2 _+_ (*-comm (suc n) 2) (*-comm (suc n) n) ⟩
2 * suc n + n * suc n
≡⟨ sym (distribʳ-*-+ (suc n) 2 n)⟩
(2 + n) * suc n
\end{code}
\begin{code}
data _∈_ {A : Set} (x : A) : List A → Set where

56
src/ListsAns.lagda Normal file
View file

@ -0,0 +1,56 @@
---
title : "Lists Answers"
layout : page
permalink : /ListsAns
---
\begin{code}
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; sym; trans; cong)
open Eq.≡-Reasoning
open import Data.Nat using (; suc; zero; _+_; _*_)
open import Data.Nat.Properties.Simple using (*-comm; distribʳ-*-+)
open import Data.List using (List; []; _∷_; _++_; foldr)
*-distrib-+ : ∀ (m n p : ) → (m + n) * p ≡ m * p + n * p
*-distrib-+ m n p = distribʳ-*-+ p m n
\end{code}
*Sum of count*
\begin{code}
sum : List
sum = foldr _+_ 0
countdown : → List
countdown zero = []
countdown (suc n) = suc n ∷ countdown n
infix 6 _+
_+ :
(m +) n = m + n
cong2 : ∀ {A B C : Set} {x x : A} {y y : B} →
(f : A → B → C) → (x ≡ x) → (y ≡ y) → (f x y ≡ f x y)
cong2 f x≡x y≡y rewrite x≡x | y≡y = refl
sum-countdown : ∀ (n : ) → sum (countdown n) * 2 ≡ suc n * n
sum-countdown zero = refl
sum-countdown (suc n) =
begin
sum (countdown (suc n)) * 2
≡⟨⟩
sum (suc n ∷ countdown n) * 2
≡⟨⟩
(suc n + sum (countdown n)) * 2
≡⟨ *-distrib-+ (suc n) (sum (countdown n)) 2 ⟩
suc n * 2 + sum (countdown n) * 2
≡⟨ cong (suc n * 2 +) (sum-countdown n) ⟩
suc n * 2 + suc n * n
≡⟨ cong2 _+_ (*-comm (suc n) 2) (*-comm (suc n) n) ⟩
2 * suc n + n * suc n
≡⟨ sym (*-distrib-+ 2 n (suc n))⟩
(2 + n) * suc n
\end{code}

View file

@ -6,7 +6,7 @@ permalink : /PropertiesAns
\begin{code}
open import Data.Nat using (; suc; zero; _+_; _*_; _∸_)
open import Properties using (+-assoc; +-comm)
open import Data.Nat.Properties.Simple using (+-assoc; +-comm)
open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym)
\end{code}