-
Notifications
You must be signed in to change notification settings - Fork 2
/
IsBounded.agda
71 lines (55 loc) · 2.14 KB
/
IsBounded.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{-# OPTIONS --rewriting #-}
open import Algebra.Cost
-- Upper bound on the cost of a computation.
module Calf.Data.IsBounded (costMonoid : CostMonoid) where
open CostMonoid costMonoid
open import Calf.Prelude
open import Calf.CBPV
open import Calf.Directed
open import Calf.Step costMonoid
open import Calf.Data.IsBoundedG costMonoid
IsBounded : (A : tp⁺) → cmp (F A) → ℂ → Set
IsBounded A e c = IsBoundedG A e (step⋆ c)
bound/relax : {c c' : ℂ} → c ≤ c' → {e : cmp (F A)} → IsBounded A e c → IsBounded A e c'
bound/relax h {e = e} = boundg/relax (step-monoˡ-≤⁻ (ret triv) h) {e = e}
bound/ret : {A : tp⁺} (a : val A) → IsBounded A (ret a) zero
bound/ret a = ≤⁻-refl
bound/step : {A : tp⁺} (c : ℂ) {c' : ℂ} (e : cmp (F A)) →
IsBounded A e c' →
IsBounded A (step (F A) c e) (c + c')
bound/step c {c'} e h = boundg/step c {b = step⋆ c'} e h
bound/bind/const : {e : cmp (F A)} {f : val A → cmp (F B)}
(c d : ℂ) →
IsBounded A e c →
((a : val A) → IsBounded B (f a) d) →
IsBounded B (bind {A} (F B) e f) (c + d)
bound/bind/const {e = e} {f} c d he hf =
let open ≤⁻-Reasoning cost in
begin
bind cost e (λ v → bind cost (f v) (λ _ → ret triv))
≲⟨ bind-monoʳ-≤⁻ e hf ⟩
bind cost e (λ _ → step⋆ d)
≡⟨⟩
bind cost (bind cost e λ _ → ret triv) (λ _ → step⋆ d)
≲⟨ bind-monoˡ-≤⁻ (λ _ → step⋆ d) he ⟩
bind cost (step⋆ c) (λ _ → step⋆ d)
≡⟨⟩
step⋆ (c + d)
∎
module Legacy where
open import Calf.Data.Product
open import Calf.Data.Equality
legacy : {e : cmp (F A)} {c : ℂ} →
val (Σ⁺ ℂ⁺ λ c' → meta⁺ (c' ≤ c) ×⁺ Σ⁺ A λ a → e ≡⁺[ U (F A) ] step (F A) c' (ret a)) →
IsBounded A e c
legacy {A} {e} {c} (c' , c'≤c , a , e≡step-ret) =
let open ≤⁻-Reasoning cost in
begin
bind cost e (λ _ → ret triv)
≡⟨ cong (λ e → bind cost e (λ _ → ret triv)) e≡step-ret ⟩
bind cost (step (F A) c' (ret a)) (λ _ → ret triv)
≡⟨⟩
step⋆ c'
≲⟨ step-monoˡ-≤⁻ (ret triv) c'≤c ⟩
step⋆ c
∎