view CCCGraph.agda @ 829:6c5cfb9b333e

fix deduction theorem
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 13 Jul 2019 00:18:17 +0900
parents d1569e80fe0b
children 232cea484067
line wrap: on
line source

open import Level
open import Category 
module CCCgraph where

open import HomReasoning
open import cat-utility
open import Data.Product renaming (_×_ to _/\_  ) hiding ( <_,_> )
open import Category.Constructions.Product
open  import  Relation.Binary.PropositionalEquality hiding ( [_] )
open import CCC

open Functor

--   ccc-1 : Hom A a 1 ≅ {*}
--   ccc-2 : Hom A c (a × b) ≅ (Hom A c a ) × ( Hom A c b )
--   ccc-3 : Hom A a (c ^ b) ≅ Hom A (a × b) c

open import Category.Sets

------------------------------------------------------
-- Sets is a CCC
------------------------------------------------------

postulate extensionality : { c₁ c₂ ℓ : Level} ( A : Category c₁ c₂ ℓ ) → Relation.Binary.PropositionalEquality.Extensionality c₂ c₂

data One {l : Level}  : Set l where
  OneObj : One   -- () in Haskell ( or any one object set )

sets : {l : Level } → CCC (Sets {l})
sets {l} = record {
         1  = One
       ; ○ = λ _ → λ _ → OneObj
       ; _∧_ = _∧_
       ; <_,_> = <,>
       ; π = π
       ; π' = π'
       ; _<=_ = _<=_
       ; _* = _*
       ; ε = ε
       ; isCCC = isCCC
  } where
         1 : Obj Sets 
         1 = One 
         ○ : (a : Obj Sets ) → Hom Sets a 1
         ○ a = λ _ → OneObj
         _∧_ : Obj Sets → Obj Sets → Obj Sets
         _∧_ a b =  a /\  b
         <,> : {a b c : Obj Sets } → Hom Sets c a → Hom Sets c b → Hom Sets c ( a ∧ b)
         <,> f g = λ x → ( f x , g x )
         π : {a b : Obj Sets } → Hom Sets (a ∧ b) a
         π {a} {b} =  proj₁ 
         π' : {a b : Obj Sets } → Hom Sets (a ∧ b) b
         π' {a} {b} =  proj₂ 
         _<=_ : (a b : Obj Sets ) → Obj Sets
         a <= b  = b → a
         _* : {a b c : Obj Sets } → Hom Sets (a ∧ b) c → Hom Sets a (c <= b)
         f * =  λ x → λ y → f ( x , y )
         ε : {a b : Obj Sets } → Hom Sets ((a <= b ) ∧ b) a
         ε {a} {b} =  λ x → ( proj₁ x ) ( proj₂ x )
         isCCC : CCC.IsCCC Sets 1 ○ _∧_ <,> π π' _<=_ _* ε
         isCCC = record {
               e2  = e2
             ; e3a = λ {a} {b} {c} {f} {g} → e3a {a} {b} {c} {f} {g}
             ; e3b = λ {a} {b} {c} {f} {g} → e3b {a} {b} {c} {f} {g}
             ; e3c = e3c
             ; π-cong = π-cong
             ; e4a = e4a
             ; e4b = e4b
             ; *-cong = *-cong
           } where
                e2 : {a : Obj Sets} {f : Hom Sets a 1} → Sets [ f ≈ ○ a ]
                e2 {a} {f} = extensionality Sets ( λ x → e20 x )
                  where
                        e20 : (x : a ) → f x ≡ ○ a x
                        e20 x with f x
                        e20 x | OneObj = refl
                e3a : {a b c : Obj Sets} {f : Hom Sets c a} {g : Hom Sets c b} →
                    Sets [ ( Sets [  π  o ( <,> f g)  ] ) ≈ f ]
                e3a = refl
                e3b : {a b c : Obj Sets} {f : Hom Sets c a} {g : Hom Sets c b} →
                    Sets [ Sets [ π' o ( <,> f g ) ] ≈ g ]
                e3b = refl
                e3c : {a b c : Obj Sets} {h : Hom Sets c (a ∧ b)} →
                    Sets [ <,> (Sets [ π o h ]) (Sets [ π' o h ]) ≈ h ]
                e3c = refl
                π-cong : {a b c : Obj Sets} {f f' : Hom Sets c a} {g g' : Hom Sets c b} →
                    Sets [ f ≈ f' ] → Sets [ g ≈ g' ] → Sets [ <,> f g ≈ <,> f' g' ]
                π-cong refl refl = refl
                e4a : {a b c : Obj Sets} {h : Hom Sets (c ∧ b) a} →
                    Sets [ Sets [ ε o <,> (Sets [ h * o π ]) π' ] ≈ h ]
                e4a = refl
                e4b : {a b c : Obj Sets} {k : Hom Sets c (a <= b)} →
                    Sets [ (Sets [ ε o <,> (Sets [ k o π ]) π' ]) * ≈ k ]
                e4b = refl
                *-cong : {a b c : Obj Sets} {f f' : Hom Sets (a ∧ b) c} →
                    Sets [ f ≈ f' ] → Sets [ f * ≈ f' * ]
                *-cong refl = refl

module ccc-from-graph where

------------------------------------------------------
--  CCC generated from a graph
------------------------------------------------------

   open import Relation.Binary.PropositionalEquality renaming ( cong to ≡-cong ) hiding ( [_] )
   open import graph
   open graphtocat 

   open Graph

   data Objs (G : Graph {Level.zero} {Level.zero} ) : Set where    -- formula
      atom : (vertex G) → Objs G
      ⊤ : Objs G
      _∧_ : Objs G → Objs G → Objs G
      _<=_ : Objs G → Objs G → Objs G

   data Arrow (G : Graph ) :  Objs G → Objs G → Set where  --- proof
      arrow : {a b : vertex G} →  (edge G) a b → Arrow G (atom a) (atom b)
      ○ : (a : Objs G ) → Arrow G a ⊤
      π : {a b : Objs G } → Arrow G ( a ∧ b ) a
      π' : {a b : Objs G } → Arrow G ( a ∧ b ) b
      ε : {a b : Objs G } → Arrow G ((a <= b) ∧ b ) a
      <_,_> : {a b c : Objs G } → Arrow G c a → Arrow G c b → Arrow G c (a ∧ b)
      _* : {a b c : Objs G } → Arrow G (c ∧ b ) a → Arrow G c ( a <= b )

   data one {v : Level} {S : Set v} : Set v where
             elm : (x : S ) → one 

   iso→ : {v : Level} {S : Set v} → one → S
   iso→ (elm x) = x

   data iso← {v : Level} {S : Set v} : (one {v} {S} ) → S → Set v where
       elmeq : {x : S} →  iso←  ( elm x ) x

   iso-left : {v : Level} {S : Set v} → (x : one {v} {S} ) → (a : S ) → iso← x a → iso→ x ≡ a
   iso-left (elm x) .x elmeq = refl

   iso-right : {v : Level} {S : Set v} → (x : one {v} {S} ) → iso← x ( iso→ x ) 
   iso-right (elm x) = elmeq

   record SM {v : Level} : Set (suc v)  where
      field
        graph : Graph  {v} {v}
        sobj : vertex graph → Set
        smap : { a b : vertex graph } → edge graph a b  → sobj a → sobj b

   open SM

   -- positive intutionistic calculus
   PL : (G : SM) → Graph
   PL G = record { vertex = Objs (graph G) ; edge = Arrow (graph G) }
   DX : (G : SM) → Category  Level.zero Level.zero Level.zero   
   DX G = GraphtoCat (PL G)

   -- open import Category.Sets
   -- postulate extensionality : { c₁ c₂ ℓ : Level} ( A : Category c₁ c₂ ℓ ) → Relation.Binary.PropositionalEquality.Extensionality c₂ c₂

   fobj : {G : SM} ( a  : Objs (graph G) ) → Set
   fobj {G} (atom x) = sobj G x
   fobj {G} (a ∧ b) = (fobj {G} a ) /\ (fobj {G} b )
   fobj {G} (a <= b) = fobj {G} b → fobj {G} a
   fobj ⊤ = One
   amap : {G : SM} { a b : Objs (graph G) } → Arrow (graph G) a b → fobj {G} a → fobj {G} b
   amap {G} (arrow x) = smap G x
   amap (○ a) _ = OneObj
   amap π ( x , _) = x
   amap π'( _ , x) = x
   amap ε ( f , x ) = f x
   amap < f , g > x = (amap f x , amap g x)
   amap (f *) x = λ y → amap f ( x , y )
   fmap : {G : SM} { a b : Objs (graph G) } → Hom (DX G) a b → fobj {G} a → fobj {G} b
   fmap {G} {a} (id a) = λ z → z
   fmap {G} (next x f ) = Sets [ amap {G} x o fmap f ]

   --   CS is a map from Positive logic to Sets
   --    Sets is CCC, so we have a cartesian closed category generated by a graph
   --       as a sub category of Sets

   CS : (G : SM ) → Functor (DX G) (Sets {Level.zero})
   FObj (CS G) a  = fobj a
   FMap (CS G) {a} {b} f = fmap {G} {a} {b} f
   isFunctor (CS G) = isf where
       _++_ = Category._o_ (DX G)
       ++idR = IsCategory.identityR ( Category.isCategory ( DX G ) )
       distr : {a b c : Obj (DX G)}  { f : Hom (DX G) a b } { g : Hom (DX G) b c } → (z : fobj {G} a ) → fmap (g ++ f) z ≡ fmap g (fmap f z)
       distr {a} {b} {c} {f} {next {b} {d} {c} x g} z = adistr (distr {a} {b} {d} {f} {g} z ) x where
          adistr : fmap (g ++ f) z ≡ fmap g (fmap f z) →
              ( x : Arrow (graph G) d c ) → fmap ( next x (g ++ f) ) z  ≡ fmap ( next x g ) (fmap f z )
          adistr eq x = cong ( λ k → amap x k ) eq
       distr {a} {b} {b} {f} {id b} z =  refl
       isf : IsFunctor (DX G) Sets fobj fmap 
       IsFunctor.identity isf = extensionality Sets ( λ x → refl )
       IsFunctor.≈-cong isf refl = refl
       IsFunctor.distr isf {a} {b} {c} {g} {f} = extensionality Sets ( λ z → distr {a} {b} {c} {g} {f} z ) 

------------------------------------------------------
--  Cart     Category of CCC and CCC preserving Functor
------------------------------------------------------

---
---  SubCategoy SC F A is a category with Obj = FObj F, Hom = FMap 
---
---     CCC ( SC (CS G)) Sets   have to be proved
---  SM can be eliminated if we have
---    sobj (a : vertex g ) → {a}              a set have only a
---    smap (a b : vertex g ) → {a} → {b}


record CCCObj { c₁ c₂ ℓ  : Level} : Set (suc (c₁ ⊔ c₂ ⊔ ℓ)) where
   field
     cat : Category c₁ c₂ ℓ
     ccc : CCC cat
 
open CCCObj 
 
record CCCMap  {c₁ c₂ ℓ : Level} (A B : CCCObj {c₁} {c₂} {ℓ} ) : Set (suc (c₁ ⊔ c₂ ⊔ ℓ )) where
   field
     cmap : Functor (cat A ) (cat B )
     ccf :  CCC (cat A) → CCC (cat B)

open import Category.Cat

open  CCCMap
open import Relation.Binary.Core

Cart : {c₁ c₂ ℓ : Level} → Category (suc (c₁ ⊔ c₂ ⊔ ℓ)) (suc (c₁ ⊔ c₂ ⊔ ℓ))(suc (c₁ ⊔ c₂ ⊔ ℓ))
Cart {c₁} {c₂} {ℓ} = record {
    Obj = CCCObj {c₁} {c₂} {ℓ}
  ; Hom = CCCMap
  ; _o_ = λ {A} {B} {C} f g → record { cmap = (cmap f) ○ ( cmap g ) ; ccf = λ _ → ccf f ( ccf g (ccc A )) }
  ; _≈_ = λ {a} {b} f g → cmap f ≃ cmap g
  ; Id  = λ {a} → record { cmap = identityFunctor ; ccf = λ x → x }
  ; isCategory = record {
     isEquivalence = λ {A} {B} → record {
          refl = λ {f} →  let open ≈-Reasoning (CAT) in refl-hom {cat A} {cat B} {cmap f} 
        ; sym = λ {f} {g}  → let open ≈-Reasoning (CAT) in sym-hom {cat A} {cat B} {cmap f} {cmap g} 
        ; trans = λ {f} {g} {h} → let open ≈-Reasoning (CAT) in trans-hom {cat A} {cat B} {cmap f} {cmap g} {cmap h}  }
     ; identityL = λ {x} {y} {f} → let open ≈-Reasoning (CAT) in idL {cat x} {cat y} {cmap f} {_} {_}
     ; identityR = λ {x} {y} {f} → let open ≈-Reasoning (CAT) in idR {cat x} {cat y} {cmap f}
     ; o-resp-≈ = λ {x} {y} {z} {f} {g} {h} {i}  → IsCategory.o-resp-≈ ( Category.isCategory CAT) {cat x}{cat y}{cat z} {cmap f} {cmap g} {cmap h} {cmap i}
     ; associative =  λ {a} {b} {c} {d} {f} {g} {h} → let open ≈-Reasoning (CAT) in assoc {cat a} {cat b} {cat c} {cat d} {cmap f} {cmap g} {cmap h}
   }} 

------------------------------------------------------
--  Grph     Category of Graph and Graph mapping
------------------------------------------------------

open import graph
open Graph

record GMap {v v' : Level} (x y : Graph {v} {v'} )  : Set (suc (v ⊔ v') ) where
  field
   vmap : vertex x → vertex y
   emap : {a b : vertex x} → edge x a b → edge y (vmap a) (vmap b)

open GMap

open import Relation.Binary.HeterogeneousEquality using (_≅_;refl ) renaming ( sym to ≅-sym ; trans to ≅-trans ; cong to ≅-cong )

data [_]_==_ {c₁ c₂ } (C : Graph {c₁} {c₂} ) {A B : vertex C} (f : edge C A B)
     : ∀{X Y : vertex C} → edge C X Y → Set (suc (c₁ ⊔ c₂ )) where
  mrefl : {g : edge C A B} → (eqv : f ≡ g ) → [ C ] f == g

_=m=_ : ∀ {c₁ c₂ } {C D : Graph {c₁} {c₂} } 
    → (F G : GMap C D) → Set (suc (c₂ ⊔ c₁))
_=m=_ {C = C} {D = D} F G = ∀{A B : vertex C} → (f : edge C A B) → [ D ] emap F f == emap G f

_&_ :  {v v' : Level} {x y z : Graph {v} {v'}} ( f : GMap y z ) ( g : GMap x y ) → GMap x z
f & g = record { vmap = λ x →  vmap f ( vmap g x ) ; emap = λ x → emap f ( emap g x ) }

Grph : {v v' : Level} → Category (suc (v ⊔ v')) (suc (v ⊔ v')) (suc ( v ⊔ v'))
Grph {v} {v'} = record {
    Obj = Graph {v} {v'}
  ; Hom = GMap {v} {v'}
  ; _o_ = _&_
  ; _≈_ = _=m=_
  ; Id  = record { vmap = λ y → y ; emap = λ f → f }
  ; isCategory = record {
       isEquivalence = λ {A} {B} →  ise 
     ; identityL = λ e → mrefl refl
     ; identityR =  λ e → mrefl refl
     ; o-resp-≈ = m--resp-≈ 
     ; associative = λ e → mrefl refl
   }}  where
       msym : {v v' : Level} {x y : Graph {v} {v'} }  { f g : GMap x y } → f =m= g → g =m= f
       msym {_} {_} {x} {y} f=g f = lemma ( f=g f ) where
            lemma  : ∀{a b c d} {f : edge y a b} {g : edge y c d} → [ y ] f == g → [ y ] g == f
            lemma (mrefl Ff≈Gf) = mrefl  (sym  Ff≈Gf)
       mtrans : {v v' : Level} {x y : Graph {v} {v'} }  { f g h : GMap x y } → f =m= g → g =m= h → f =m= h
       mtrans {_} {_} {x} {y} f=g g=h f = lemma ( f=g f ) ( g=h f ) where
           lemma : ∀{a b c d e f} {p : edge y a b} {q : edge y c d} → {r : edge y e f}  → [ y ] p == q → [ y ] q == r → [ y ] p == r
           lemma (mrefl eqv) (mrefl eqv₁) = mrefl ( trans eqv  eqv₁ )
       ise : {v v' : Level} {x y : Graph {v} {v'}}  → IsEquivalence {_} {suc v ⊔ suc v' } {_} ( _=m=_  {v} {v'}  {x} {y}) 
       ise  = record {
          refl =  λ f → mrefl refl
        ; sym = msym
        ; trans = mtrans
          }
       m--resp-≈ : {v v' : Level} {A B C : Graph {v} {v'} }  
           {f g : GMap A B} {h i : GMap B C} → f =m= g → h =m= i → ( h & f ) =m= ( i & g )
       m--resp-≈ {_} {_} {A} {B} {C} {f} {g} {h} {i} f=g h=i e =
          lemma (emap f e) (emap g e) (emap i (emap g e)) (f=g e) (h=i ( emap g e )) where
            lemma : {a b c d : vertex B } {z w : vertex C } (ϕ : edge B a b) (ψ : edge B c d) (π : edge C z w) →
                [ B ] ϕ  == ψ → [ C ] (emap h ψ) == π → [ C ] (emap h ϕ) == π
            lemma _ _ _ (mrefl refl) (mrefl refl) = mrefl refl

------------------------------------------------------
--- CCC → Grph  Forgetful functor
------------------------------------------------------

≃-cong : {c₁ c₂ ℓ : Level}  (B : Category c₁ c₂ ℓ ) → {a b a' b' : Obj B }
      → { f f'   : Hom B a b }
      → { g g' : Hom B a' b' }
      → [_]_~_ B f g → B [ f ≈ f' ] → B [ g ≈ g' ] → [_]_~_ B f' g'
≃-cong B {a} {b} {a'} {b'} {f} {f'} {g} {g'}  (refl {g2} eqv) f=f' g=g' = let open ≈-Reasoning B in refl {_} {_} {_} {B} {a'} {b'} {f'} {g'} ( begin
             f'
          ≈↑⟨ f=f' ⟩
             f
          ≈⟨ eqv  ⟩
             g
          ≈⟨ g=g' ⟩
             g'
          ∎  )
  
fobj : {c₁ c₂ ℓ : Level} → Obj (Cart {c₁} {c₂} {ℓ} )  → Obj (Grph {c₁} {c₂})
fobj a = record { vertex = Obj (cat a) ; edge = Hom (cat a) }
fmap : {c₁ c₂ ℓ : Level} → {a b : Obj (Cart {c₁} {c₂} {ℓ} ) } → Hom (Cart {c₁} {c₂} {ℓ} ) a b → Hom (Grph {c₁} {c₂}) ( fobj a ) ( fobj b )
fmap f =  record { vmap = FObj (cmap f) ; emap = FMap (cmap f) }

UX : {c₁ c₂ ℓ : Level} → ( ≈-to-≡ : (A : Category c₁ c₂ ℓ ) →  {a b : Obj A} → {f g : Hom A a b} → A [ f ≈ g ] → f ≡ g  )
    → Functor (Cart {c₁} {c₂} {ℓ} ) (Grph {c₁} {c₂})
FObj (UX {c₁} {c₂} {ℓ} ≈-to-≡  ) a = fobj a
FMap (UX ≈-to-≡)  f =  fmap f
isFunctor (UX {c₁} {c₂} {ℓ}  ≈-to-≡)  = isf where
  -- if we don't need ≈-cong ( i.e.   f ≈ g → UX f =m= UX g ), all lemmas are not necessary
  open import HomReasoning
  isf : IsFunctor (Cart {c₁} {c₂} {ℓ} ) (Grph {c₁} {c₂}) fobj fmap
  IsFunctor.identity isf {a} {b} {f} e = mrefl refl 
  IsFunctor.distr isf f = mrefl refl
  IsFunctor.≈-cong isf {a} {b} {f} {g} eq {x} {y} e = lemma (extensionality Sets ( λ z → lemma4 (
               ≃-cong (cat b) (eq (id1 (cat a) z)) (IsFunctor.identity (Functor.isFunctor (cmap f))) (IsFunctor.identity (Functor.isFunctor (cmap g)))
          ))) (eq e) where
      lemma4 : {x y : vertex (fobj b) } →  [_]_~_ (cat b)  (id1 (cat b) x) (id1 (cat b) y) → x ≡ y
      lemma4 (refl eqv) = refl 
      lemma : vmap (fmap f) ≡ vmap (fmap g) → [ cat b ] FMap (cmap f) e ~ FMap (cmap g) e → [ fobj b ] emap (fmap f) e == emap (fmap g) e
      lemma refl (refl eqv) = mrefl ( ≈-to-≡ (cat b) eqv )


---   
---   open ccc-from-graph
---   
---   sm : {v : Level } → Graph {v} → SM {v}
---   SM.graph (sm g) = g
---   SM.sobj (sm  g) = {!!}
---   SM.smap (sm  g) = {!!}
---   
---   HX : {v : Level } ( x : Obj (Grph {v}) ) → Obj (Grph {v})
---   HX {v} x = {!!} -- FObj UX ( record { cat = Sets {v} ;  ccc = sets } )
---   
---   
---   
---