Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. module Universal where
  2.  
  3. open import Relation.Binary.PropositionalEquality
  4. open import Relation.Binary
  5. open Relation.Binary.PropositionalEquality.≡-Reasoning
  6. open import Agda.Primitive
  7. open import Data.Nat hiding (_⊔_)
  8. open import Data.Unit hiding (setoid)
  9. open import Data.Bool
  10. open import Data.Nat.Properties
  11. open import Data.Product hiding (map)
  12. open import Data.Vec hiding (drop; map)
  13. open import Data.Fin hiding (_+_)
  14. open import Data.List hiding (zipWith)
  15.  
  16. data HVec {a} : (n : ℕ) → List (Set a) → Set a where
  17. hnil : HVec 0 []
  18. hcons : {A : Set a} → {n : ℕ} → {l : List (Set a)} →
  19. (x : A) → HVec n l → HVec (suc n) (A ∷ l)
  20.  
  21.  
  22. hLookup : {a : Level} → ∀ {len l} → HVec {a} len l → (n : Fin (length l)) → (lookup n (fromList l))
  23. hLookup hnil ()
  24. hLookup (hcons x xs) zero = x
  25. hLookup (hcons x xs) (suc n) = hLookup xs n
  26.  
  27. opSignature : ∀ {a} → Set a → ℕ → Set a
  28. opSignature A zero = A
  29. opSignature A (suc n) = A → (opSignature A n)
  30.  
  31. apply : ∀ {a} → {A : Set a} → (n : ℕ) → (f : opSignature A n) →
  32. (v : Vec A n) → A
  33. apply zero f [] = f
  34. apply (suc n) f (x ∷ l) = apply n (f x) l
  35.  
  36. data WellDefined {a b} {A : Set a} (_==_ : A → A → Set b) (n : ℕ) (f : opSignature A n) : Set (a ⊔ b) where
  37. wd : ((vx vy : Vec A n) →
  38. (p : let t = toList (zipWith _==_ vx vy) in HVec (length t) t) →
  39. (apply n f vx == apply n f vy)) →
  40. WellDefined _==_ n f
  41.  
  42. respect : ∀ {a b} → {A : Set a} → (_==_ : A → A → Set b) →
  43. (ns : List ℕ) → HVec (length ns) (map (opSignature A) ns) →
  44. List (Set (a ⊔ b))
  45. respect _ [] _ = []
  46. respect _==_ (n ∷ ns) (hcons f hfs) = (WellDefined _==_ n f) ∷ (respect _==_ ns hfs)
  47.  
  48. open Agda.Primitive
  49.  
  50. record Universal {a b} (S : Setoid a b) (Signature : List ℕ) : Set (lsuc (a ⊔ b)) where
  51. open Setoid S
  52.  
  53. n = length Signature
  54.  
  55. opTypes = map (opSignature Carrier) Signature
  56.  
  57. field
  58. Operators : HVec n opTypes
  59.  
  60. wdTypes = respect _≈_ Signature Operators
  61.  
  62. field
  63. WellDef : HVec n wdTypes
  64.  
  65. record Monoid {a b} (S : Setoid a b) : Set (lsuc (a ⊔ b)) where
  66. open Setoid S
  67.  
  68. field
  69. {{Uni}} : Universal S (2 ∷ 0 ∷ [])
  70.  
  71. open Universal Uni
  72.  
  73. _●_ = hLookup Operators zero
  74. ε = hLookup Operators (suc zero)
  75.  
  76. field
  77. assoc : (x y z : Carrier) → (x ● (y ● z)) ≈ ((x ● y) ● z)
  78. lunit : (x : Carrier) → (ε ● x) ≈ x
  79. runit : (x : Carrier) → (x ● ε) ≈ x
  80.  
  81. plusAssoc : (x y z : ℕ) → x + (y + z) ≡ (x + y) + z
  82. plusAssoc zero y z = refl
  83. plusAssoc (suc x) y z = cong suc (plusAssoc x y z)
  84.  
  85. plusZero : (x : ℕ) → x + 0 ≡ x
  86. plusZero zero = refl
  87. plusZero (suc n) = cong suc (plusZero n)
  88.  
  89. plusWellDef : WellDefined _≡_ 2 _+_
  90. plusWellDef = wd f where
  91. f : (vx vy : Vec ℕ 2) →
  92. let t = toList (zipWith _≡_ vx vy) in HVec (length t) t →
  93. apply 2 _+_ vx ≡ apply 2 _+_ vy
  94. f (a ∷ b ∷ []) (c ∷ d ∷ []) (hcons pac (hcons pbd hnil)) =
  95. begin
  96. a + b
  97. ≡⟨ cong (λ x → x + b) pac ⟩
  98. c + b
  99. ≡⟨ cong (λ x → c + x) pbd ⟩
  100. c + d
  101.  
  102. zeroWellDef : WellDefined _≡_ 0 0
  103. zeroWellDef = wd f where
  104. f : (vx vy : Vec ℕ 0) →
  105. let t = toList (zipWith _≡_ vx vy) in HVec (length t) t →
  106. apply 0 0 vx ≡ apply 0 0 vy
  107. f [] [] _ = refl
  108.  
  109. instance
  110. natPlus : Monoid (setoid ℕ)
  111. natPlus = record
  112. {
  113. Uni = record
  114. {
  115. Operators = hcons _+_ (hcons 0 hnil);
  116. WellDef = hcons plusWellDef (hcons zeroWellDef hnil)
  117. };
  118. assoc = plusAssoc;
  119. lunit = λ x → refl;
  120. runit = plusZero
  121. }
Add Comment
Please, Sign In to add comment