view agda/delta.agda @ 62:0f308ddd6136

Trying prove infinite delta by equiv-reasoning
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Tue, 25 Nov 2014 12:34:09 +0900
parents 73bb981cb1c6
children 474ed34e4f02
line wrap: on
line source

open import list
open import basic

open import Level
open import Relation.Binary.PropositionalEquality
open ≡-Reasoning

module delta where


data Delta {l : Level} (A : Set l) : (Set (suc l)) where
  mono    : A -> Delta A
  delta   : A -> Delta A -> Delta A

deltaAppend : {l : Level} {A : Set l} -> Delta A -> Delta A -> Delta A
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 x)    = mono x
headDelta (delta x _) = mono x

tailDelta : {l : Level} {A : Set l} -> Delta A -> Delta A
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 x)    = mono (f x)
fmap f (delta x d) = delta (f x) (fmap f d)



-- Monad (Category)
eta : {l : Level} {A : Set l} -> A -> Delta A
eta x = mono x

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

mu : {l : Level} {A : Set l} -> Delta (Delta A) -> Delta A
mu d = bind d id 


returnS : {l : Level} {A : Set l} -> A -> Delta A
returnS x = mono x

returnSS : {l : Level} {A : Set l} -> A -> A -> Delta A
returnSS x y = deltaAppend (returnS x) (returnS y)


-- Monad (Haskell)
return : {l : Level} {A : Set l} -> A -> Delta A
return = eta

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



-- proofs

-- sub proofs

head-delta-natural-transformation : {l ll : Level} {A : Set l} {B : Set ll} -> 
                                  (f : A -> B) (d : Delta A) -> (headDelta (fmap f d)) ≡ fmap f (headDelta d)
head-delta-natural-transformation f (mono x)    = refl
head-delta-natural-transformation f (delta x d) = refl

tail-delta-natural-transfomation  : {l ll : Level} {A : Set l} {B : Set ll} -> 
                                  (f : A -> B) (d : Delta A) -> (tailDelta (fmap f d)) ≡ fmap f (tailDelta d)
tail-delta-natural-transfomation f (mono x) = refl
tail-delta-natural-transfomation f (delta x d) = refl

delta-append-natural-transfomation : {l ll : Level} {A : Set l} {B : Set ll} -> 
                                  (f : A -> B) (d : Delta A) (dd : Delta A) -> 
                                  deltaAppend (fmap f d) (fmap f dd) ≡ fmap f (deltaAppend d dd)
delta-append-natural-transfomation f (mono x) dd    = refl
delta-append-natural-transfomation f (delta x d) dd = begin
  deltaAppend (fmap f (delta x d)) (fmap f dd) 
  ≡⟨ refl ⟩
  deltaAppend (delta (f x) (fmap f d)) (fmap f dd) 
  ≡⟨ refl ⟩
  delta (f x) (deltaAppend (fmap f d) (fmap f dd))
  ≡⟨ cong (\d -> delta (f x) d) (delta-append-natural-transfomation f d dd) ⟩
  delta (f x) (fmap f (deltaAppend d dd))
  ≡⟨ refl ⟩
  fmap f (deltaAppend (delta x d) dd)



mu-head-delta : {l : Level} {A : Set l} -> (d : Delta (Delta A)) -> mu (headDelta d) ≡ headDelta (mu d)
mu-head-delta (mono (mono x))    = refl
mu-head-delta (mono (delta x (mono xx))) = begin
 mu (headDelta (mono (delta x (mono xx))))
 ≡⟨ refl ⟩
 bind (headDelta (mono (delta x (mono xx)))) id
 ≡⟨ refl ⟩
 bind (delta x (mono xx)) return
 ≡⟨ refl ⟩
 deltaAppend (headDelta (return x)) (bind (mono xx) (tailDelta ∙ return))
 ≡⟨ refl ⟩
 deltaAppend (headDelta (return x)) ((tailDelta ∙ return) xx)
 ≡⟨ refl ⟩
 deltaAppend (headDelta (mono x)) (tailDelta (mono xx))
 ≡⟨ refl ⟩
 deltaAppend (mono x) (mono xx)
 ≡⟨ refl ⟩
 delta x (mono xx)
 ≡⟨ {!!} ⟩
 headDelta (delta x (mono xx))
 ≡⟨ refl ⟩
 headDelta (bind (mono (delta x (mono xx))) id)
 ≡⟨ refl ⟩
 headDelta (mu (mono (delta x (mono xx))))

mu-head-delta (mono (delta x (delta x₁ d))) = {!!}
mu-head-delta (delta d dd) = {!!}

-- 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 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 x)    = refl
functor-law-2 f g (delta x d) = cong (delta (f (g x))) (functor-law-2 f g d)

-- Monad-laws (Category)
{-
-- 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 d)    = refl
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 ⟩
  bind (delta (mu x) (mono (mu d))) id
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (bind (mono (mu d)) tailDelta)
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (tailDelta (mu d))
  ≡⟨ cong (\de -> deltaAppend de (tailDelta (mu d))) (head-delta-natural-transformation {!!} {!!}) ⟩
  deltaAppend (mu (headDelta x)) (tailDelta (mu d))
  ≡⟨ {!!} ⟩
  (mu ∙ mu) (delta x (mono d))

monad-law-1 (delta x (delta d d₁)) = {!!}
-}


{-
monad-law-1 : {l : Level} {A : Set l} -> (d : Delta (Delta (Delta A))) -> ((mu ∙ (fmap mu)) d) ≡ ((mu ∙ mu) d)
monad-law-1 (mono d)    = refl
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) (fmap mu (mono d)))
  ≡⟨ refl ⟩
  mu (delta (mu x) (fmap mu (mono d)))
  ≡⟨ refl ⟩
  mu (delta (mu x) (mono (mu d)))
  ≡⟨ refl ⟩
  bind (delta (mu x) (mono (mu d))) id
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (bind (mono (mu d)) (tailDelta ∙ id))
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (bind (mono (mu d)) (tailDelta))
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (tailDelta (mu d))
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) ((tailDelta ∙ mu) d)
  ≡⟨ refl ⟩
  deltaAppend (headDelta (mu x)) (bind (mono d) (tailDelta ∙ mu))
  ≡⟨ refl ⟩
  bind (delta x (mono d)) mu
  ≡⟨ {!!} ⟩
  mu (deltaAppend (headDelta x) (tailDelta d))
  ≡⟨ refl ⟩
  mu (deltaAppend (headDelta x) (bind (mono d) tailDelta))
  ≡⟨ refl ⟩
  mu (deltaAppend (headDelta (id x)) (bind (mono d) (tailDelta ∙ id)))
  ≡⟨ refl ⟩
  mu (deltaAppend (headDelta x) (bind (mono d) (tailDelta ∙ id)))
  ≡⟨ refl ⟩
  mu (bind (delta x (mono d)) id)
  ≡⟨ refl ⟩
  mu (deltaAppend (headDelta (id x)) (bind  (mono d) (tailDelta ∙ id)))
  ≡⟨ refl ⟩
  mu (mu (delta x (mono d)))
  ≡⟨ refl ⟩
  (mu ∙ mu) (delta x (mono d))

monad-law-1 (delta x (delta xx d)) = {!!}
{-
monad-law-1 (delta x d) = begin
   (mu ∙ fmap mu) (delta x d) 
   ≡⟨ refl ⟩
   mu ((fmap mu) (delta x d))
   ≡⟨ refl ⟩
   mu (delta (mu x) (fmap mu d))
   ≡⟨ refl ⟩
   bind (delta (mu x) (fmap mu d)) id
   ≡⟨ refl ⟩
   deltaAppend (headDelta (mu x)) (bind (fmap mu d) (tailDelta ∙ id))
   ≡⟨ refl ⟩
   deltaAppend (headDelta (mu x)) (bind (fmap mu d) (tailDelta ∙ id))
   ≡⟨ {!!} ⟩
   (mu ∙ mu) (delta x d)

-}
-}

{-
-- 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

-- monad-law-3 : return . f = fmap f . return
monad-law-3 : {l : Level} {A B : Set l} (f : A -> B) (x : A) -> (eta ∙ f) x ≡ (fmap f ∙ eta) x
monad-law-3 f x = refl

-- monad-law-4 : join . fmap (fmap f) = fmap f . join
monad-law-4 : {l ll : Level} {A : Set l} {B : Set ll} (f : A -> B) (s : Delta (Delta A)) ->
              (mu ∙ fmap (fmap f)) s ≡ (fmap f ∙ mu) s
monad-law-4 f (similar lx (similar llx x _ _) ly (similar _ _ lly y)) = refl


-- Monad-laws (Haskell)
-- monad-law-h-1 : return a >>= k  =  k a
monad-law-h-1 : {l ll : Level} {A : Set l} {B : Set ll} ->
                (a : A) -> (k : A -> (Delta B)) ->
                (return a >>= k)  ≡ (k a)
monad-law-h-1 a k = refl



-- monad-law-h-2 : m >>= return  =  m
monad-law-h-2 : {l : Level}{A : Set l} -> (m : Delta A) -> (m >>= return)  ≡ m
monad-law-h-2 (mono x)    = refl
monad-law-h-2 (delta x d) = cong (delta x) (monad-law-h-2 d)




-- monad-law-h-3 : m >>= (\x -> k x >>= h)  =  (m >>= k) >>= h
monad-law-h-3 : {l ll lll : Level} {A : Set l} {B : Set ll} {C : Set lll} ->
                (m : Delta A)  -> (k : A -> (Delta B)) -> (h : B -> (Delta C)) ->
                (m >>= (\x -> k x >>= h)) ≡ ((m >>= k) >>= h)
monad-law-h-3 (mono x) k h    = refl
monad-law-h-3 (delta x d) k h = begin
  (delta x d) >>= (\x -> k x >>= h)
  ≡⟨ refl ⟩
-- (delta x d) >>= f = deltaAppend (headDelta (f x)) (d >>= (tailDelta ∙ f))
  deltaAppend (headDelta ((\x -> k x >>= h) x)) (d >>= (tailDelta ∙ (\x -> k x >>= h)))
  ≡⟨ refl ⟩
  deltaAppend (headDelta (k x >>= h)) (d >>= (tailDelta ∙ (\x -> k x >>= h)))
  ≡⟨ {!!} ⟩
  ((delta x d) >>= k) >>= h

-}