RefAPI.agda 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. module Issue784.RefAPI where
  2. open import Issue784.Values public
  3. open import Issue784.Context -- that should be private
  4. open import Issue784.Transformer public
  5. open import Data.List public using (List; []; _∷_; [_])
  6. open import Data.Nat
  7. open import Relation.Binary.PropositionalEquality.TrustMe
  8. private
  9. postulate
  10. _seq_ : ∀ {a b} {A : Set a} {B : Set b} → A → B → B
  11. nativeRef : (A : Set) → Set
  12. -- create new reference and initialize it with passed value
  13. nativeNew-ℕ : ℕ → nativeRef ℕ
  14. -- increment value in place
  15. nativeInc-ℕ : nativeRef ℕ → Unit
  16. nativeGet-ℕ : nativeRef ℕ → ℕ
  17. nativeFree-ℕ : nativeRef ℕ → Unit
  18. data Exact {ℓ} {A : Set ℓ} : A → Set ℓ where
  19. exact : ∀ a → Exact a
  20. getExact : ∀ {ℓ} {A : Set ℓ} {a : A} → Exact a → A
  21. getExact (exact a) = a
  22. ≡-exact : ∀ {ℓ} {A : Set ℓ} {a : A} (e : Exact a) → getExact e ≡ a
  23. ≡-exact (exact a) = refl
  24. Ref-ℕ : ℕ → Set
  25. Ref-ℕ a = Σ[ native ∈ nativeRef ℕ ] nativeGet-ℕ native ≡ a
  26. private
  27. -- making these private to avoid further using
  28. -- which may lead to inconsistency
  29. proofNew-ℕ : ∀ a → Ref-ℕ a
  30. proofNew-ℕ a = nativeNew-ℕ a , trustMe
  31. proofInc-ℕ : ∀ {a} → Ref-ℕ a → Ref-ℕ (suc a)
  32. proofInc-ℕ (r , _) = (nativeInc-ℕ r) seq (r , trustMe)
  33. proofGet-ℕ : ∀ {a} → Ref-ℕ a → Exact a
  34. proofGet-ℕ (r , p) = ≡-elim′ Exact p (exact $ nativeGet-ℕ r)
  35. proofFree-ℕ : {a : ℕ} → Ref-ℕ a → Unit
  36. proofFree-ℕ (r , _) = nativeFree-ℕ r
  37. new-ℕ : ∀ a n → Transformer! [] [(n , Unique (Ref-ℕ a))]
  38. new-ℕ a n ctx nr-v []⊆v nr-v∪n = context w , (≡⇒≋ $ ≡-trans p₁ p₂) where
  39. v = Context.get ctx
  40. w = v ∪ [(n , Unique (Ref-ℕ a) , unique (proofNew-ℕ a))]
  41. p₁ : types (v ∪ [(n , Unique (Ref-ℕ a) , _)]) ≡ types v ∪ [(n , Unique (Ref-ℕ a))]
  42. p₁ = t-x∪y v [(n , Unique (Ref-ℕ a) , _)]
  43. p₂ : types v ∪ [(n , Unique (Ref-ℕ a))] ≡ types v ∖∖ [] ∪ [(n , Unique (Ref-ℕ a))]
  44. p₂ = ≡-cong (λ x → x ∪ [(n , Unique (Ref-ℕ a))]) (≡-sym $ t∖[]≡t $ types v)
  45. inc-ℕ : ∀ {a} n → Transformer! [(n , Unique (Ref-ℕ a))] [(n , Unique (Ref-ℕ (suc a)))]
  46. inc-ℕ {a} n ctx nr-v n⊆v nr-v∪n = context w , (≡⇒≋ $ ≡-trans p₁ p₂) where
  47. v = Context.get ctx
  48. r = unique ∘ proofInc-ℕ ∘ Unique.get ∘ getBySignature ∘ n⊆v $ here refl
  49. w = v ∖∖ [ n ] ∪ [(n , Unique (Ref-ℕ $ suc a) , r)]
  50. p₁ : types (v ∖∖ [ n ] ∪ [(n , Unique (Ref-ℕ $ suc a) , r)]) ≡ types (v ∖∖ [ n ]) ∪ [(n , Unique (Ref-ℕ $ suc _))]
  51. p₁ = t-x∪y (v ∖∖ [ n ]) _
  52. p₂ : types (v ∖∖ [ n ]) ∪ [(n , Unique (Ref-ℕ $ suc a))] ≡ types v ∖∖ [ n ] ∪ [(n , Unique (Ref-ℕ $ suc _))]
  53. p₂ = ≡-cong (λ x → x ∪ [(n , Unique (Ref-ℕ $ suc a))]) (t-x∖y v [ n ])
  54. get-ℕ : (r n : String) {r≢!n : r s-≢! n} {a : ℕ} →
  55. Transformer ([(r , Unique (Ref-ℕ a))] , nr-[a]) ((r , Unique (Ref-ℕ a)) ∷ [(n , Pure (Exact a))] , (x≢y⇒x∉l⇒x∉y∷l (s-≢!⇒≢? r≢!n) λ()) ∷ nr-[a])
  56. get-ℕ r n {a = a} ctx nr-v h⊆v _ = context w , ≋-trans p₁ (≋-trans p₂ p₃) where
  57. v = Context.get ctx
  58. pr : Pure (Exact a)
  59. pr = pure ∘ proofGet-ℕ ∘ Unique.get ∘ getBySignature ∘ h⊆v $ here refl
  60. w = v ∪ [(n , Pure (Exact a) , pr)]
  61. p₁ : types (v ∪ [(n , Pure (Exact a) , pr)]) ≋ types v ∪ [(n , Pure (Exact a))]
  62. p₁ = ≡⇒≋ $ t-x∪y v [(n , Pure (Exact a) , pr)]
  63. p₂ : types v ∪ [(n , Pure (Exact a))] ≋ (types v ∖∖ [ r ] ∪ [(r , Unique (Ref-ℕ _))]) ∪ [(n , Pure (Exact a))]
  64. p₂ = x≋x̀⇒x∪y≋x̀∪y (≋-trans g₁ g₂) [(n , Pure (Exact a))] where
  65. g₁ : types v ≋ [(r , Unique (Ref-ℕ _))] ∪ types v ∖∖ [ r ]
  66. g₁ = t₁⊆t₂⇒t₂≋t₁∪t₂∖nt₁ nr-[a] (nr-x⇒nr-t-x nr-v) h⊆v
  67. g₂ : [(r , Unique (Ref-ℕ _))] ∪ types v ∖∖ [ r ] ≋ types v ∖∖ [ r ] ∪ [(r , Unique (Ref-ℕ _))]
  68. g₂ = ∪-sym [(r , Unique (Ref-ℕ _))] (types v ∖∖ [ r ])
  69. p₃ : (types v ∖∖ [ r ] ∪ [(r , Unique (Ref-ℕ a))]) ∪ [(n , Pure (Exact a))] ≋ types v ∖∖ [ r ] ∪ ((r , Unique (Ref-ℕ _)) ∷ [(n , Pure (Exact a))])
  70. p₃ = ≡⇒≋ $ ∪-assoc (types v ∖∖ [ r ]) [(r , Unique (Ref-ℕ _))] [(n , Pure (Exact a))]
  71. free-ℕ : (h : String) {a : ℕ} → Transformer! [(h , Unique (Ref-ℕ a))] []
  72. free-ℕ h ctx nr-v h⊆v _ = u seq (context w , ≡⇒≋ p) where
  73. v = Context.get ctx
  74. u = proofFree-ℕ ∘ Unique.get ∘ getBySignature ∘ h⊆v $ here refl
  75. w = v ∖∖ [ h ]
  76. p : types (v ∖∖ [ h ]) ≡ types v ∖∖ [ h ] ∪ []
  77. p = ≡-trans (t-x∖y v [ h ]) (≡-sym $ x∪[]≡x (types v ∖∖ [ h ]))