~ubuntu-branches/ubuntu/raring/agda-stdlib/raring

« back to all changes in this revision

Viewing changes to src/Category/Monad/Partiality/All.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-04-10 10:30:20 UTC
  • mfrom: (2.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130410103020-bcspfz3whyy5iafu
Tags: 0.7-1
* [6d52289] Imported Upstream version 0.7
* [54104d0] Update Depends and Build-Depends for this version, compatible
  with Agda 2.3.2
* [b3ddce4] No need for the .install file to be executable (thanks lintian)
* [a9a6cb7] Standards-Version → 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- The Agda standard library
 
3
--
 
4
-- An All predicate for the partiality monad
 
5
------------------------------------------------------------------------
 
6
 
 
7
module Category.Monad.Partiality.All where
 
8
 
 
9
open import Category.Monad
 
10
open import Category.Monad.Partiality as Partiality using (_⊥; ⇒≈)
 
11
open import Coinduction
 
12
open import Function
 
13
open import Level
 
14
open import Relation.Binary using (_Respects_; IsEquivalence)
 
15
open import Relation.Binary.PropositionalEquality as P using (_≡_)
 
16
 
 
17
open Partiality._⊥
 
18
open Partiality.Equality using (Rel)
 
19
open Partiality.Equality.Rel
 
20
private
 
21
  open module E {a} {A : Set a} = Partiality.Equality (_≡_ {A = A})
 
22
    using (_≅_; _≳_)
 
23
  open module M {f} = RawMonad (Partiality.monad {f = f})
 
24
    using (_>>=_)
 
25
 
 
26
------------------------------------------------------------------------
 
27
-- All, along with some lemmas
 
28
 
 
29
-- All P x means that if x terminates with the value v, then P v
 
30
-- holds.
 
31
 
 
32
data All {a p} {A : Set a} (P : A → Set p) : A ⊥ → Set (a ⊔ p) where
 
33
  now   : ∀ {v} (p : P v)             → All P (now v)
 
34
  later : ∀ {x} (p : ∞ (All P (♭ x))) → All P (later x)
 
35
 
 
36
-- Bind preserves All in the following way:
 
37
 
 
38
_>>=-cong_ : ∀ {ℓ p q} {A B : Set ℓ} {P : A → Set p} {Q : B → Set q}
 
39
               {x : A ⊥} {f : A → B ⊥} →
 
40
             All P x → (∀ {x} → P x → All Q (f x)) →
 
41
             All Q (x >>= f)
 
42
now p   >>=-cong f = f p
 
43
later p >>=-cong f = later (♯ (♭ p >>=-cong f))
 
44
 
 
45
-- All respects all the relations, given that the predicate respects
 
46
-- the underlying relation.
 
47
 
 
48
respects :
 
49
  ∀ {k a p ℓ} {A : Set a} {P : A → Set p} {_∼_ : A → A → Set ℓ} →
 
50
  P Respects _∼_ → All P Respects Rel _∼_ k
 
51
respects resp (now    x∼y) (now   p) = now (resp x∼y p)
 
52
respects resp (later  x∼y) (later p) = later (♯ respects resp (♭ x∼y) (♭ p))
 
53
respects resp (laterˡ x∼y) (later p) =          respects resp    x∼y  (♭ p)
 
54
respects resp (laterʳ x≈y) p         = later (♯ respects resp    x≈y     p)
 
55
 
 
56
respects-flip :
 
57
  ∀ {k a p ℓ} {A : Set a} {P : A → Set p} {_∼_ : A → A → Set ℓ} →
 
58
  P Respects flip _∼_ → All P Respects flip (Rel _∼_ k)
 
59
respects-flip resp (now    x∼y) (now   p) = now (resp x∼y p)
 
60
respects-flip resp (later  x∼y) (later p) = later (♯ respects-flip resp (♭ x∼y) (♭ p))
 
61
respects-flip resp (laterˡ x∼y) p         = later (♯ respects-flip resp    x∼y     p)
 
62
respects-flip resp (laterʳ x≈y) (later p) =          respects-flip resp    x≈y  (♭ p)
 
63
 
 
64
-- "Equational" reasoning.
 
65
 
 
66
module Reasoning {a p ℓ}
 
67
                 {A : Set a} {P : A → Set p}
 
68
                 {_∼_ : A → A → Set ℓ}
 
69
                 (resp : P Respects flip _∼_) where
 
70
 
 
71
  infix  2 finally
 
72
  infixr 2 _≡⟨_⟩_ _∼⟨_⟩_
 
73
 
 
74
  _≡⟨_⟩_ : ∀ x {y} → x ≡ y → All P y → All P x
 
75
  _ ≡⟨ P.refl ⟩ p = p
 
76
 
 
77
  _∼⟨_⟩_ : ∀ {k} x {y} → Rel _∼_ k x y → All P y → All P x
 
78
  _ ∼⟨ x∼y ⟩ p = respects-flip resp (⇒≈ x∼y) p
 
79
 
 
80
  -- A cosmetic combinator.
 
81
 
 
82
  finally : (x : A ⊥) → All P x → All P x
 
83
  finally _ p = p
 
84
 
 
85
  syntax finally x p = x ⟨ p ⟩
 
86
 
 
87
-- "Equational" reasoning with _∼_ instantiated to propositional
 
88
-- equality.
 
89
 
 
90
module Reasoning-≡ {a p} {A : Set a} {P : A → Set p}
 
91
  = Reasoning {P = P} {_∼_ = _≡_} (P.subst P ∘ P.sym)
 
92
 
 
93
------------------------------------------------------------------------
 
94
-- An alternative, but equivalent, formulation of All
 
95
 
 
96
module Alternative {a p : Level} where
 
97
 
 
98
  infixr 2 _≅⟨_⟩P_ _≳⟨_⟩P_
 
99
  infix  2 _⟨_⟩P
 
100
 
 
101
  -- All "programs".
 
102
 
 
103
  data AllP {A : Set a} (P : A → Set p) : A ⊥ → Set (suc (a ⊔ p)) where
 
104
    now         : ∀ {x} (p : P x) → AllP P (now x)
 
105
    later       : ∀ {x} (p : ∞ (AllP P (♭ x))) → AllP P (later x)
 
106
    _>>=-congP_ : ∀ {B : Set a} {Q : B → Set p} {x f}
 
107
                  (p-x : AllP Q x) (p-f : ∀ {v} → Q v → AllP P (f v)) →
 
108
                  AllP P (x >>= f)
 
109
    _≅⟨_⟩P_     : ∀ x {y} (x≅y : x ≅ y) (p : AllP P y) → AllP P x
 
110
    _≳⟨_⟩P_     : ∀ x {y} (x≳y : x ≳ y) (p : AllP P y) → AllP P x
 
111
    _⟨_⟩P       : ∀ x (p : AllP P x) → AllP P x
 
112
 
 
113
  private
 
114
 
 
115
    -- WHNFs.
 
116
 
 
117
    data AllW {A} (P : A → Set p) : A ⊥ → Set (suc (a ⊔ p)) where
 
118
      now   : ∀ {x} (p : P x) → AllW P (now x)
 
119
      later : ∀ {x} (p : AllP P (♭ x)) → AllW P (later x)
 
120
 
 
121
    -- A function which turns WHNFs into programs.
 
122
 
 
123
    program : ∀ {A} {P : A → Set p} {x} → AllW P x → AllP P x
 
124
    program (now   p) = now      p
 
125
    program (later p) = later (♯ p)
 
126
 
 
127
    -- Functions which turn programs into WHNFs.
 
128
 
 
129
    trans-≅ : ∀ {A} {P : A → Set p} {x y : A ⊥} →
 
130
              x ≅ y → AllW P y → AllW P x
 
131
    trans-≅ (now P.refl) (now   p) = now p
 
132
    trans-≅ (later  x≅y) (later p) = later (_ ≅⟨ ♭ x≅y ⟩P p)
 
133
 
 
134
    trans-≳ : ∀ {A} {P : A → Set p} {x y : A ⊥} →
 
135
              x ≳ y → AllW P y → AllW P x
 
136
    trans-≳ (now P.refl) (now   p) = now p
 
137
    trans-≳ (later  x≳y) (later p) = later (_ ≳⟨ ♭ x≳y ⟩P p)
 
138
    trans-≳ (laterˡ x≳y)        p  = later (_ ≳⟨   x≳y ⟩P program p)
 
139
 
 
140
    mutual
 
141
 
 
142
      _>>=-congW_ : ∀ {A B} {P : A → Set p} {Q : B → Set p} {x f} →
 
143
                    AllW P x → (∀ {v} → P v → AllP Q (f v)) →
 
144
                    AllW Q (x >>= f)
 
145
      now   p >>=-congW p-f = whnf (p-f p)
 
146
      later p >>=-congW p-f = later (p >>=-congP p-f)
 
147
 
 
148
      whnf : ∀ {A} {P : A → Set p} {x} → AllP P x → AllW P x
 
149
      whnf (now   p)           = now p
 
150
      whnf (later p)           = later (♭ p)
 
151
      whnf (p-x >>=-congP p-f) = whnf p-x >>=-congW p-f
 
152
      whnf (_ ≅⟨ x≅y ⟩P p)     = trans-≅ x≅y (whnf p)
 
153
      whnf (_ ≳⟨ x≳y ⟩P p)     = trans-≳ x≳y (whnf p)
 
154
      whnf (_ ⟨ p ⟩P)          = whnf p
 
155
 
 
156
  -- AllP P is sound and complete with respect to All P.
 
157
 
 
158
  sound : ∀ {A} {P : A → Set p} {x} → AllP P x → All P x
 
159
  sound = λ p → soundW (whnf p)
 
160
    where
 
161
    soundW : ∀ {A} {P : A → Set p} {x} → AllW P x → All P x
 
162
    soundW (now   p) = now p
 
163
    soundW (later p) = later (♯ sound p)
 
164
 
 
165
  complete : ∀ {A} {P : A → Set p} {x} → All P x → AllP P x
 
166
  complete (now   p) = now p
 
167
  complete (later p) = later (♯ complete (♭ p))