### changeset 57:dfcd72dc697e

ReDefine Delta used non-empty-list for infinite changes
author Yasutaka Higa Sat, 22 Nov 2014 12:29:32 +0900 bfb6be9a689d 1229ee398567 agda/delta.agda delta.hs 2 files changed, 45 insertions(+), 154 deletions(-) [+]
line wrap: on
line diff
--- a/agda/delta.agda	Wed Nov 19 21:09:45 2014 +0900
+++ b/agda/delta.agda	Sat Nov 22 12:29:32 2014 +0900
@@ -7,50 +7,45 @@

module delta where

-DeltaLog : Set
-DeltaLog = List String

data Delta {l : Level} (A : Set l) : (Set (suc l)) where
-  mono    : DeltaLog -> A -> Delta A
-  delta   : DeltaLog -> A -> Delta A -> Delta A
-
-logAppend : {l : Level} {A : Set l} -> DeltaLog -> Delta A -> Delta A
-logAppend l (mono lx x)    = mono  (l ++ lx) x
-logAppend l (delta lx x d) = delta (l ++ lx) x (logAppend l d)
+  mono    : A -> Delta A
+  delta   : A -> Delta A -> Delta A

deltaAppend : {l : Level} {A : Set l} -> Delta A -> Delta A -> Delta A
-deltaAppend (mono lx x) d     = delta lx x d
-deltaAppend (delta lx x d) ds = delta lx x (deltaAppend d ds)
+deltaAppend (mono x) d     = delta x d
+deltaAppend (delta x d) ds = delta x (deltaAppend d ds)

headDelta : {l : Level} {A : Set l} -> Delta A -> Delta A
-headDelta (mono lx x)    = mono lx x
-headDelta (delta lx x _) = mono lx x
+headDelta (mono x)    = mono x
+headDelta (delta x _) = mono x

tailDelta : {l : Level} {A : Set l} -> Delta A -> Delta A
-tailDelta (mono lx x)   = mono lx x
-tailDelta (delta _ _ d) = d
+tailDelta (mono x)     = mono x
+tailDelta (delta  _ d) = d

-- Functor
fmap : {l ll : Level} {A : Set l} {B : Set ll} -> (A -> B) -> (Delta A) -> (Delta B)
-fmap f (mono lx x)    = mono lx (f x)
-fmap f (delta lx x d) = delta lx (f x) (fmap f d)
+fmap f (mono x)    = mono (f x)
+fmap f (delta x d) = delta (f x) (fmap f d)
+

-{-# NO_TERMINATION_CHECK #-}
-mu : {l : Level} {A : Set l} -> Delta (Delta A) -> Delta A
-mu (mono ld d)     = logAppend ld d
-mu (delta ld d ds) = deltaAppend (logAppend ld (headDelta d)) (mu (fmap tailDelta ds))
+
+-- TODO: mu
+-- TODO: bind
+

eta : {l : Level} {A : Set l} -> A -> Delta A
-eta x = mono [] x
+eta x = mono x

returnS : {l : Level} {A : Set l} -> A -> Delta A
-returnS x = mono [[ (show x) ]] x
+returnS x = mono x

returnSS : {l : Level} {A : Set l} -> A -> A -> Delta A
-returnSS x y = delta [[ (show x) ]] x (mono [[ (show y) ]] y)
+returnSS x y = deltaAppend (returnS x) (returnS y)

@@ -59,133 +54,37 @@

_>>=_ : {l ll : Level} {A : Set l} {B : Set ll} ->
(x : Delta A) -> (f : A -> (Delta B)) -> (Delta B)
-x >>= f = mu (fmap f x)
+(mono x) >>= f    = f x
+(delta x d) >>= f = deltaAppend (headDelta (f x)) (d >>= (tailDelta ∙ f))

-- proofs

--- sub proofs
-twice-log-append : {l : Level} {A : Set l} -> (l : List String) -> (ll : List String) -> (d : Delta A) ->
-                   logAppend l (logAppend ll d) ≡ logAppend (l ++ ll) d
-twice-log-append l ll (mono lx x) = begin
-  mono (l ++ (ll ++ lx)) x
-  ≡⟨ cong (\l -> mono l x) (list-associative l ll lx) ⟩
-  mono (l ++ ll ++ lx) x
-  ∎
-twice-log-append l ll (delta lx x d) = begin
-  delta (l ++ (ll ++ lx)) x (logAppend l (logAppend ll d))
-  ≡⟨ cong (\lx -> delta lx x (logAppend l (logAppend ll d))) (list-associative l ll lx) ⟩
-  delta (l ++ ll ++ lx) x (logAppend l (logAppend ll d))
-  ≡⟨ cong (delta (l ++ ll ++ lx) x) (twice-log-append l ll d) ⟩
-  delta (l ++ ll ++ lx) x (logAppend (l ++ ll) d)
-  ∎
-

-- Functor-laws

-- Functor-law-1 : T(id) = id'
functor-law-1 :  {l : Level} {A : Set l} ->  (d : Delta A) -> (fmap id) d ≡ id d
-functor-law-1 (mono lx x)    = refl
-functor-law-1 (delta lx x d) = cong (delta lx x) (functor-law-1 d)
+functor-law-1 (mono x)    = refl
+functor-law-1 (delta x d) = cong (delta x) (functor-law-1 d)

-- Functor-law-2 : T(f . g) = T(f) . T(g)
functor-law-2 : {l ll lll : Level} {A : Set l} {B : Set ll} {C : Set lll} ->
(f : B -> C) -> (g : A -> B) -> (d : Delta A) ->
(fmap (f ∙ g)) d ≡ ((fmap f) ∙ (fmap g)) d
-functor-law-2 f g (mono lx x)    = refl
-functor-law-2 f g (delta lx x d) = cong (delta lx (f (g x))) (functor-law-2 f g d)
+functor-law-2 f g (mono x)    = refl
+functor-law-2 f g (delta x d) = cong (delta (f (g x))) (functor-law-2 f g d)

-- 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 (mono lx (mono llx (mono lllx x))) = begin
-    mono (lx ++ (llx ++ lllx)) x
-  ≡⟨ cong (\l -> mono l x) (list-associative lx llx lllx) ⟩
-    mono (lx ++ llx ++ lllx) x
-  ∎
-monad-law-1 (mono lx (mono llx (delta lllx x d))) = begin
-    delta (lx ++ (llx ++ lllx)) x (logAppend lx (logAppend llx d))
-  ≡⟨ cong (\l -> delta l x (logAppend lx (logAppend llx d))) (list-associative lx llx lllx) ⟩
-    delta (lx ++ llx ++ lllx) x (logAppend lx (logAppend llx d))
-  ≡⟨ cong (\d -> delta (lx ++ llx ++ lllx) x d) (twice-log-append lx llx d) ⟩
-    delta (lx ++ llx ++ lllx) x (logAppend (lx ++ llx) d)
-  ∎
-monad-law-1 (mono lx (delta ld (mono x x₁) (mono x₂ (mono x₃ x₄)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (mono (lx ++ (x₂ ++ x₃)) x₄)
-  ≡⟨ cong (\l -> delta l x₁(mono (lx ++ (x₂ ++ x₃)) x₄)) (list-associative lx ld x) ⟩
-  delta (lx ++ ld ++ x) x₁ (mono (lx ++ (x₂ ++ x₃)) x₄)
-  ≡⟨ cong (\l -> delta (lx ++ ld ++ x) x₁ (mono l x₄)) (list-associative lx x₂ x₃) ⟩
-  delta (lx ++ ld ++ x) x₁ (mono (lx ++ x₂ ++ x₃) x₄)
-  ∎
-monad-law-1 (mono lx (delta ld (mono x x₁) (mono x₂ (delta x₃ x₄ ds)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (logAppend lx (logAppend x₂ ds))
-  ≡⟨ cong (\l -> delta l x₁ (logAppend lx (logAppend x₂ ds))) (list-associative lx ld x) ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend lx (logAppend x₂ ds))
-  ≡⟨ cong (\d -> delta (lx ++ ld ++ x) x₁ d)  (twice-log-append lx x₂ ds) ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend (lx ++ x₂) ds)
-  ∎
-monad-law-1 (mono lx (delta ld (delta x x₁ (mono x₂ x₃)) (mono x₄ (mono x₅ x₆)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (mono (lx ++ (x₄ ++ x₅)) x₆)
-  ≡⟨ cong (\l -> delta l x₁ (mono (lx ++ (x₄ ++ x₅)) x₆)) (list-associative lx ld x ) ⟩
-  delta (lx ++ ld ++ x) x₁  (mono (lx ++ (x₄ ++ x₅)) x₆)
-  ≡⟨ cong (\l -> delta (lx ++ ld ++ x) x₁ (mono l x₆))  (list-associative lx x₄ x₅)⟩
-  delta (lx ++ ld ++ x) x₁ (mono (lx ++ x₄ ++ x₅) x₆)
-  ∎
-monad-law-1 (mono lx (delta ld (delta x x₁ (mono x₂ x₃)) (mono x₄ (delta x₅ x₆ ds)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (logAppend lx (logAppend x₄ ds))
-  ≡⟨ cong (\l -> delta l x₁(logAppend lx (logAppend x₄ ds))) (list-associative lx ld x ) ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend lx (logAppend x₄ ds))
-  ≡⟨ cong (\d -> delta (lx ++ ld ++ x) x₁ d) (twice-log-append lx x₄ ds ) ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend (lx ++ x₄) ds)
-  ∎
-
-monad-law-1 (mono lx (delta ld (delta x x₁ (delta ly y (mono x₂ x₃))) (mono x₄ (mono x₅ x₆)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (mono (lx ++ (x₄ ++ x₅)) x₆)
-  ≡⟨ {!!} ⟩
-  delta (lx ++ ld ++ x) x₁ (mono (lx ++ x₄ ++ x₅) x₆)
-  ∎
-monad-law-1 (mono lx (delta ld (delta x x₁ (delta ly y (mono x₂ x₃))) (mono x₄ (delta x₅ x₆ ds)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (logAppend lx (logAppend x₄ ds))
-  ≡⟨ {!!} ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend (lx ++ x₄) ds)
-  ∎
-monad-law-1 (mono lx (delta ld (delta x x₁ (delta ly y (delta x₂ x₃ d))) (mono x₄ (mono x₅ x₆)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (mono (lx ++ (x₄ ++ x₅)) x₆)
-  ≡⟨ {!!} ⟩
-  delta (lx ++ ld ++ x) x₁ (mono (lx ++ x₄ ++ x₅) x₆)
-  ∎
-monad-law-1 (mono lx (delta ld (delta x x₁ (delta ly y (delta x₂ x₃ d))) (mono x₄ (delta x₅ x₆ ds)))) = begin
-  delta (lx ++ (ld ++ x)) x₁ (logAppend lx (logAppend x₄ ds))
-  ≡⟨ {!!} ⟩
-  delta (lx ++ ld ++ x) x₁ (logAppend (lx ++ x₄) ds)
-  ∎
-
+--monad-law-1 : {l : Level} {A : Set l} -> (d : Delta (Delta (Delta A))) -> ((mu ∙ (fmap mu)) d) ≡ ((mu ∙ mu) d)

-
-monad-law-1 (mono lx (delta ld d (delta x ds ds₁))) = {!!}
-
-
-
-monad-law-1 (delta lx x d) = {!!}
-
{-
--- monad-law-2 : join . fmap return = join . return = id
--- monad-law-2-1 join . fmap return = join . return
-monad-law-2-1 : {l : Level} {A : Set l} -> (s : Delta  A) ->
-  (mu ∙ fmap eta) s ≡ (mu ∙ eta) s
-monad-law-2-1 (similar lx x ly y) = begin
-    similar (lx ++ []) x (ly ++ []) y
-  ≡⟨ cong (\left-list -> similar left-list x (ly ++ []) y) (empty-append lx)⟩
-    similar lx x (ly ++ []) y
-  ≡⟨ cong (\right-list -> similar lx x right-list y) (empty-append ly) ⟩
-    similar lx x ly y
-  ∎
-
-- monad-law-2-2 :  join . return = id
monad-law-2-2 : {l : Level} {A : Set l } -> (s : Delta A) -> (mu ∙ eta) s ≡ id s
monad-law-2-2 (similar lx x ly y) = refl
@@ -266,4 +165,5 @@
≡⟨ refl ⟩
((similar lx x ly y) >>= k) >>= h
∎
--}
+
+-}
\ No newline at end of file
--- a/delta.hs	Wed Nov 19 21:09:45 2014 +0900
+++ b/delta.hs	Sat Nov 22 12:29:32 2014 +0900
@@ -1,48 +1,39 @@
import Control.Applicative
import Data.Numbers.Primes -- $cabal install primes -type DeltaLog = [String] - -data Delta a = Mono DeltaLog a | Delta DeltaLog a (Delta a) deriving Show - -logAppend :: DeltaLog -> Delta a -> Delta a -logAppend l (Mono lx x) = Mono (l ++ lx) x -logAppend l (Delta lx x d) = Delta (l ++ lx) x (logAppend l d) +data Delta a = Mono a | Delta a (Delta a) deriving Show deltaAppend :: Delta a -> Delta a -> Delta a -deltaAppend (Mono lx x) d = Delta lx x d -deltaAppend (Delta lx x d) ds = Delta lx x (deltaAppend d ds) +deltaAppend (Mono x) d = Delta x d +deltaAppend (Delta x d) ds = Delta x (deltaAppend d ds) headDelta :: Delta a -> Delta a -headDelta d@(Mono _ _) = d -headDelta (Delta lx x _) = Mono lx x +headDelta d@(Mono _) = d +headDelta (Delta x _) = Mono x tailDelta :: Delta a -> Delta a -tailDelta d@(Mono _ _) = d -tailDelta (Delta _ _ ds) = ds +tailDelta d@(Mono _) = d +tailDelta (Delta _ ds) = ds instance Functor Delta where - fmap f (Mono lx x) = Mono lx (f x) - fmap f (Delta lx x d) = Delta lx (f x) (fmap f d) + fmap f (Mono x) = Mono (f x) + fmap f (Delta x d) = Delta (f x) (fmap f d) instance Applicative Delta where - pure f = Mono [] f - (Mono lf f) <*> (Mono lx x) = Mono (lf ++ lx) (f x) - df@(Mono lf f) <*> (Delta lx x d) = Delta (lf ++ lx) (f x) (df <*> d) - (Delta lf f df) <*> d@(Mono lx x) = Delta (lf ++ lx) (f x) (df <*> d) - (Delta lf f df) <*> (Delta lx x d) = Delta (lf ++ lx) (f x) (df <*> d) + pure f = Mono f + (Mono f) <*> (Mono x) = Mono (f x) + df@(Mono f) <*> (Delta x d) = Delta (f x) (df <*> d) + (Delta f df) <*> d@(Mono x) = Delta (f x) (df <*> d) + (Delta f df) <*> (Delta x d) = Delta (f x) (df <*> d) -mu :: Delta (Delta a) -> Delta a -mu (Mono ld d) = logAppend ld d -mu (Delta ld d ds) = (logAppend ld$ headDelta d) deltaAppend (mu $fmap tailDelta ds) - instance Monad Delta where - return x = Mono [] x - d >>= f = mu$ fmap f d
+    return x = Mono x
+    (Mono x)    >>= f  = f x
+    (Delta x d) >>= f  = (headDelta (f x)) deltaAppend (d >>= (tailDelta . f))

returnS :: (Show s) => s -> Delta s
-returnS x = Mono [(show x)] x
+returnS x = Mono x

returnSS :: (Show s) => s -> s -> Delta s
returnSS x y = (returnS x) deltaAppend (returnS y)