### changeset 76:c7076f9bbaed

Refactors
author Yasutaka Higa Mon, 01 Dec 2014 11:47:52 +0900 a4eb68476766 4b16b485a4b2 agda/delta.agda 1 files changed, 54 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
```--- a/agda/delta.agda	Mon Dec 01 11:42:32 2014 +0900
+++ b/agda/delta.agda	Mon Dec 01 11:47:52 2014 +0900
@@ -1,5 +1,6 @@
open import list
open import basic
+open import nat

open import Level
open import Relation.Binary.PropositionalEquality
@@ -24,6 +25,10 @@
tailDelta (mono x)     = mono x
tailDelta (delta  _ d) = d

+n-tail : {l : Level} {A : Set l} -> Nat -> ((Delta A) -> (Delta A))
+n-tail O = id
+n-tail (S n) =  tailDelta ∙ (n-tail n)
+

-- Functor
fmap : {l ll : Level} {A : Set l} {B : Set ll} -> (A -> B) -> (Delta A) -> (Delta B)
@@ -63,6 +68,42 @@

-- proofs

+-- sub-proofs
+
+n-tail-plus : {l : Level} {A : Set l} -> (n : Nat) -> ((n-tail {l} {A} n) ∙ tailDelta) ≡ n-tail (S n)
+n-tail-plus O = refl
+n-tail-plus (S n) = begin
+  n-tail (S n) ∙ tailDelta             ≡⟨ refl ⟩
+  (tailDelta ∙ (n-tail n)) ∙ tailDelta ≡⟨ refl ⟩
+  tailDelta ∙ ((n-tail n) ∙ tailDelta) ≡⟨ cong (\t -> tailDelta ∙ t) (n-tail-plus n) ⟩
+  n-tail (S (S n))
+  ∎
+
+n-tail-add : {l : Level} {A : Set l} {d : Delta A} -> (n m : Nat) -> (n-tail {l} {A} n) ∙ (n-tail m) ≡ n-tail (n + m)
+n-tail-add (S n) O = begin
+  n-tail (S n) ∙ n-tail O  ≡⟨ refl ⟩
+  n-tail (S n)             ≡⟨ cong (\n -> n-tail n) (int-add-right-zero (S n))⟩
+  n-tail (S n + O)
+  ∎
+n-tail-add {l} {A} {d} (S n) (S m) =      begin
+  n-tail (S n) ∙ n-tail (S m)             ≡⟨ refl ⟩
+  (tailDelta ∙ (n-tail n)) ∙ n-tail (S m) ≡⟨ refl ⟩
+  tailDelta ∙ ((n-tail n) ∙ n-tail (S m)) ≡⟨ cong (\t -> tailDelta ∙ t) (n-tail-add {l} {A} {d} n (S m)) ⟩
+  tailDelta ∙ (n-tail (n + (S m)))        ≡⟨ refl ⟩
+  n-tail (S (n + S m))                    ≡⟨ refl ⟩
+  n-tail (S n + S m)                      ∎
+
+tail-delta-to-mono : {l : Level} {A : Set l} -> (n : Nat) -> (x : A) ->
+  (n-tail n) (mono x) ≡ (mono x)
+tail-delta-to-mono O x = refl
+tail-delta-to-mono (S n) x =      begin
+  n-tail (S n) (mono x)           ≡⟨ refl ⟩
+  tailDelta (n-tail n (mono x))   ≡⟨ refl ⟩
+  tailDelta (n-tail n (mono x))   ≡⟨ cong (\t -> tailDelta t) (tail-delta-to-mono n x) ⟩
+  tailDelta (mono x)              ≡⟨ refl ⟩
+  mono x                          ∎
+
-- Functor-laws

-- Functor-law-1 : T(id) = id'
@@ -80,59 +121,9 @@

-data Int : Set where
-  O  : Int
-  S : Int -> Int

-_+_ : Int -> Int -> Int
-O + n = n
-(S m) + n = S (m + n)
-postulate int-add-assoc : (n m : Int) -> n + m ≡ m + n
-postulate int-add-right-zero : (n : Int) -> n  ≡ n + O
-postulate int-add-right : (n m : Int) -> S n + S m ≡ S (S (n + m))
-
-
-
-n-tail : {l : Level} {A : Set l} -> Int -> ((Delta A) -> (Delta A))
-n-tail O = id
-n-tail (S n) =  tailDelta ∙ (n-tail n)
-
-n-tail-plus : {l : Level} {A : Set l} -> (n : Int) -> ((n-tail {l} {A} n) ∙ tailDelta) ≡ n-tail (S n)
-n-tail-plus O = refl
-n-tail-plus (S n) = begin
-  n-tail (S n) ∙ tailDelta             ≡⟨ refl ⟩
-  (tailDelta ∙ (n-tail n)) ∙ tailDelta ≡⟨ refl ⟩
-  tailDelta ∙ ((n-tail n) ∙ tailDelta) ≡⟨ cong (\t -> tailDelta ∙ t) (n-tail-plus n) ⟩
-  n-tail (S (S n))
-  ∎
-
-n-tail-add : {l : Level} {A : Set l} {d : Delta A} -> (n m : Int) -> (n-tail {l} {A} n) ∙ (n-tail m) ≡ n-tail (n + m)
-n-tail-add (S n) O = begin
-  n-tail (S n) ∙ n-tail O  ≡⟨ refl ⟩
-  n-tail (S n)             ≡⟨ cong (\n -> n-tail n) (int-add-right-zero (S n))⟩
-  n-tail (S n + O)
-  ∎
-n-tail-add {l} {A} {d} (S n) (S m) =      begin
-  n-tail (S n) ∙ n-tail (S m)             ≡⟨ refl ⟩
-  (tailDelta ∙ (n-tail n)) ∙ n-tail (S m) ≡⟨ refl ⟩
-  tailDelta ∙ ((n-tail n) ∙ n-tail (S m)) ≡⟨ cong (\t -> tailDelta ∙ t) (n-tail-add {l} {A} {d} n (S m)) ⟩
-  tailDelta ∙ (n-tail (n + (S m)))        ≡⟨ refl ⟩
-  n-tail (S (n + S m))                    ≡⟨ refl ⟩
-  n-tail (S n + S m)                      ∎
-
-tail-delta-to-mono : {l : Level} {A : Set l} -> (n : Int) -> (x : A) ->
-  (n-tail n) (mono x) ≡ (mono x)
-tail-delta-to-mono O x = refl
-tail-delta-to-mono (S n) x =      begin
-  n-tail (S n) (mono x)           ≡⟨ refl ⟩
-  tailDelta (n-tail n (mono x))   ≡⟨ refl ⟩
-  tailDelta (n-tail n (mono x))   ≡⟨ cong (\t -> tailDelta t) (tail-delta-to-mono n x) ⟩
-  tailDelta (mono x)              ≡⟨ refl ⟩
-  mono x                          ∎
-
-monad-law-1-5 : {l : Level} {A : Set l} -> (m : Int) (n : Int) -> (ds : Delta (Delta A)) ->
-  n-tail n (bind ds (n-tail m))  ≡ bind (n-tail n ds) (n-tail (m + n))
+monad-law-1-5 : {l : Level} {A : Set l} -> (m : Nat) (n : Nat) -> (ds : Delta (Delta A)) ->
+  n-tail n (bind ds (n-tail m))  ≡ bind (n-tail n ds) (n-tail (m + n))
monad-law-1-5 O O ds = refl
monad-law-1-5 O (S n) (mono ds) = begin
n-tail (S n) (bind (mono ds) (n-tail O)) ≡⟨ refl ⟩
@@ -164,7 +155,7 @@
n-tail n (bind (mono (delta x ds)) (n-tail (S m))) ≡⟨ refl ⟩
n-tail n (n-tail (S m) (delta x ds)) ≡⟨ cong (\t -> n-tail n (t (delta x ds))) (sym (n-tail-plus m)) ⟩
n-tail n (((n-tail m) ∙ tailDelta) (delta x ds)) ≡⟨ refl ⟩
-  n-tail n ((n-tail m) ds) ≡⟨ cong (\t -> t ds) (n-tail-add n m)  ⟩
+  n-tail n ((n-tail m) ds) ≡⟨ cong (\t -> t ds) (n-tail-add {d = ds} n m)  ⟩
n-tail (n + m) ds  ≡⟨ cong (\n -> n-tail n ds) (int-add-assoc n m) ⟩
n-tail (m + n) ds  ≡⟨ refl ⟩
((n-tail (m + n)) ∙ tailDelta) (delta x ds)  ≡⟨ cong (\t -> t (delta x ds)) (n-tail-plus (m + n))⟩
@@ -193,8 +184,8 @@
bind (n-tail (S n) (delta d ds)) (n-tail (S m + S n))
∎

-monad-law-1-4 : {l : Level} {A : Set l} -> (m n : Int) -> (dd : Delta (Delta A)) ->
-  headDelta ((n-tail n) (bind dd (n-tail m))) ≡ headDelta ((n-tail (m + n)) (headDelta (n-tail n dd)))
+monad-law-1-4 : {l : Level} {A : Set l} -> (m n : Nat) -> (dd : Delta (Delta A)) ->
+  headDelta ((n-tail n) (bind dd (n-tail m))) ≡ headDelta ((n-tail (m + n)) (headDelta (n-tail n dd)))
monad-law-1-4 O O (mono dd) = refl
monad-law-1-4 O O (delta dd dd₁) = refl
monad-law-1-4 O (S n) (mono dd) = begin
@@ -217,7 +208,7 @@
∎
monad-law-1-4 (S m) n (mono dd) = begin
headDelta (n-tail n (bind (mono dd) (n-tail (S m)))) ≡⟨ refl ⟩
-  headDelta (n-tail n ((n-tail (S m)) dd))≡⟨ cong (\t -> headDelta (t dd)) (n-tail-add n (S m)) ⟩
+  headDelta (n-tail n ((n-tail (S m)) dd))≡⟨ cong (\t -> headDelta (t dd)) (n-tail-add {d = dd} n (S m)) ⟩
headDelta (n-tail (n + S m) dd) ≡⟨ cong (\n -> headDelta ((n-tail n) dd)) (int-add-assoc n (S m)) ⟩
headDelta (n-tail (S m + n) dd) ≡⟨ refl ⟩
headDelta (n-tail (S m + n) (headDelta (mono dd))) ≡⟨ cong (\de -> headDelta (n-tail (S m + n) (headDelta de))) (sym (tail-delta-to-mono n dd)) ⟩
@@ -238,8 +229,8 @@
headDelta ((((n-tail n) ∙ tailDelta) (delta (headDelta ((n-tail (S m)) d)) (bind ds (tailDelta ∙ (n-tail (S m))))))) ≡⟨ refl ⟩
headDelta (n-tail n (bind ds (tailDelta ∙ (n-tail (S m))))) ≡⟨ refl ⟩
headDelta (n-tail n (bind ds  (n-tail (S (S m))))) ≡⟨ monad-law-1-4 (S (S m)) n ds ⟩
-  headDelta (n-tail ((S (S m) +  n)) (headDelta (n-tail n ds))) ≡⟨ cong (\nm -> headDelta ((n-tail nm) (headDelta (n-tail n ds)))) (sym (int-add-right m n))  ⟩
-  headDelta (n-tail (S m + S n) (headDelta (n-tail n ds))) ≡⟨ refl ⟩
+  headDelta (n-tail ((S (S m) +  n)) (headDelta (n-tail n ds))) ≡⟨ cong (\nm -> headDelta ((n-tail nm) (headDelta (n-tail n ds)))) (sym (int-add-right m n))  ⟩
+  headDelta (n-tail (S m + S n) (headDelta (n-tail n ds))) ≡⟨ refl ⟩
headDelta (n-tail (S m + S n) (headDelta (((n-tail n) ∙ tailDelta) (delta d ds)))) ≡⟨ cong (\t -> headDelta (n-tail (S m + S n) (headDelta (t (delta d ds))))) (n-tail-plus n) ⟩
headDelta (n-tail (S m + S n) (headDelta (n-tail (S n) (delta d ds))))
∎
@@ -248,14 +239,14 @@
monad-law-1-2 (delta _ _) = refl

-monad-law-1-3 : {l : Level} {A : Set l} -> (n : Int) -> (d : Delta (Delta (Delta A))) ->
+monad-law-1-3 : {l : Level} {A : Set l} -> (n : Nat) -> (d : Delta (Delta (Delta A))) ->
bind (fmap mu d) (n-tail n) ≡ bind (bind d (n-tail n)) (n-tail n)
monad-law-1-3 O (mono d)     = refl
monad-law-1-3 O (delta d ds) = begin
-  bind (fmap mu (delta d ds)) (n-tail O) ≡⟨ refl ⟩
-  bind (delta (mu d) (fmap mu ds)) (n-tail O) ≡⟨ refl ⟩
-  delta (headDelta (mu d)) (bind (fmap mu ds) tailDelta) ≡⟨ cong (\dx -> delta dx (bind (fmap mu ds) tailDelta)) (monad-law-1-2 d) ⟩
+  bind (fmap mu (delta d ds)) (n-tail O) ≡⟨ refl ⟩
+  bind (delta (mu d) (fmap mu ds)) (n-tail O) ≡⟨ refl ⟩
+  delta (headDelta (mu d)) (bind (fmap mu ds) tailDelta) ≡⟨ cong (\dx -> delta dx (bind (fmap mu ds) tailDelta)) (monad-law-1-2 d) ⟩
delta (headDelta (headDelta d)) (bind (bind ds tailDelta) tailDelta) ≡⟨ refl ⟩
bind (delta (headDelta d) (bind ds tailDelta)) (n-tail O) ≡⟨ refl ⟩
bind (bind (delta d ds) (n-tail O)) (n-tail O)
@@ -313,59 +304,10 @@
bind (bind (delta (delta d dd) ds) (n-tail (S n))) (n-tail (S n))
∎

-{-
-monad-law-1-3 (S n) (mono d) = begin
- bind (fmap mu (mono d)) (n-tail (S n)) ≡⟨ refl ⟩
- bind (mono (mu d)) (n-tail (S n)) ≡⟨ refl ⟩
- n-tail (S n) (mu d) ≡⟨ {!!} ⟩
- bind (n-tail (S n) d) (n-tail (S n)) ≡⟨ refl ⟩
- bind (bind (mono d) (n-tail (S n))) (n-tail (S n))
- ∎
-monad-law-1-3 (S n) (delta d ds) = begin
-  bind (fmap mu (delta d ds)) (n-tail (S n)) ≡⟨ refl ⟩
-  bind (delta (mu d) (fmap mu ds)) (n-tail (S n)) ≡⟨ refl ⟩
-  delta (headDelta ((n-tail (S n)) (mu d))) (bind (fmap mu ds) (tailDelta ∙ (n-tail (S n)))) ≡⟨ refl ⟩
-  delta (headDelta ((n-tail (S n)) (mu d))) (bind (fmap mu ds) (n-tail (S (S n)))) ≡⟨ {!!} ⟩
-  delta (headDelta ((n-tail (S n)) (headDelta ((n-tail (S n)) d)))) (bind (bind ds (n-tail (S (S n)))) (n-tail (S (S n)))) ≡⟨ refl ⟩
-  delta (headDelta ((n-tail (S n)) (headDelta ((n-tail (S n)) d)))) (bind (bind ds (n-tail (S (S n)))) (tailDelta ∙ (n-tail (S n)))) ≡⟨ refl ⟩
-  delta (headDelta ((n-tail (S n)) (headDelta ((n-tail (S n)) d)))) (bind (bind ds (tailDelta ∙ (n-tail (S n)))) (tailDelta ∙ (n-tail (S n)))) ≡⟨ refl ⟩
-  bind (delta (headDelta ((n-tail (S n)) d)) (bind ds (tailDelta ∙ (n-tail (S n))))) (n-tail (S n)) ≡⟨ refl ⟩
-  bind (bind (delta d ds) (n-tail (S n))) (n-tail (S n))
-  ∎
--}

-- monad-law-1 : join . fmap join = join . join
monad-law-1 : {l : Level} {A : Set l} -> (d : Delta (Delta (Delta A))) -> ((mu ∙ (fmap mu)) d) ≡ ((mu ∙ mu) d)
-{-
-monad-law-1 (delta x (mono d)) = begin
-  (mu ∙ fmap mu) (delta x (mono d)) ≡⟨ refl ⟩
-  mu (fmap mu (delta x (mono d))) ≡⟨ refl ⟩
-  mu (delta (mu x) (mono (mu d))) ≡⟨ refl ⟩
-  delta (headDelta (mu x)) (bind (mono (mu d)) tailDelta) ≡⟨ refl ⟩
-  delta (headDelta (mu x)) (tailDelta (mu d)) ≡⟨ cong (\dx -> delta dx (tailDelta (mu d))) (monad-law-1-2 x) ⟩
-  delta (headDelta (headDelta x)) (bind (tailDelta d) tailDelta)  ≡⟨ refl ⟩
-  mu (delta (headDelta x) (tailDelta d))  ≡⟨ refl ⟩
-  mu (delta (headDelta x) (bind (mono d) tailDelta))  ≡⟨ refl ⟩
-  mu (mu (delta x (mono d)))  ≡⟨ refl ⟩
-  (mu ∙ mu) (delta x (mono d))
-  ∎
-monad-law-1 (delta x (delta d ds)) = begin
-  (mu ∙ fmap mu) (delta x (delta d ds)) ≡⟨ refl ⟩
-  mu (fmap mu (delta x (delta d ds))) ≡⟨ refl ⟩
-  mu (delta (mu x) (delta (mu d) (fmap mu ds))) ≡⟨ refl ⟩
-  delta (headDelta (mu x)) (bind (delta (mu d) (fmap mu ds)) tailDelta) ≡⟨ refl ⟩
-  delta (headDelta (mu x)) (delta (headDelta (tailDelta (mu d))) (bind (fmap mu ds) (tailDelta ∙ tailDelta))) ≡⟨ {!!} ⟩
-  delta (headDelta (headDelta x)) (delta (headDelta (tailDelta (headDelta (tailDelta d)))) (bind (bind ds (tailDelta ∙ tailDelta)) (tailDelta ∙ tailDelta))) ≡⟨ refl ⟩
-  delta (headDelta (headDelta x)) (bind (delta (headDelta (tailDelta d)) (bind ds (tailDelta ∙ tailDelta))) tailDelta) ≡⟨ refl ⟩
-  delta (headDelta (headDelta x)) (bind (bind (delta d ds) tailDelta) tailDelta) ≡⟨ refl ⟩
-  mu (delta (headDelta x) (bind (delta d ds) tailDelta)) ≡⟨ refl ⟩
-  mu (mu (delta x (delta d ds))) ≡⟨ refl ⟩
-  (mu ∙ mu) (delta x (delta d ds))
-  ∎
--}
-
monad-law-1 (delta x d) = begin
(mu ∙ fmap mu) (delta x d)
≡⟨ refl ⟩```