Debian Codegraph @

src/Codata/Sized/Thunk.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The Thunk wrappers for sized codata, copredicates and corelations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --sized-types #-}
8
9 module Codata.Sized.Thunk where
10
11 open import Size using (Size; ∞; Size<_; SizedSet)
12 open import Relation.Unary.Sized using (_⇒_; ∀[_])
13
14 ------------------------------------------------------------------------
15 -- Basic types.
16
17 record Thunk {ℓ} (F : SizedSet ℓ) (i : Size) : Set ℓ where
18 coinductive
19 field force : {j : Size< i} → F j
20 open Thunk public
21
22 Thunk^P : ∀ {f p} {F : SizedSet f} (P : Size → F ∞ → Set p)
23 (i : Size) (tf : Thunk F ∞) → Set p
24 Thunk^P P i tf = Thunk (λ i → P i (tf .force)) i
25
26 Thunk^R : ∀ {f g r} {F : SizedSet f} {G : SizedSet g}
27 (R : Size → F ∞ → G ∞ → Set r)
28 (i : Size) (tf : Thunk F ∞) (tg : Thunk G ∞) → Set r
29 Thunk^R R i tf tg = Thunk (λ i → R i (tf .force) (tg .force)) i
30
31 ------------------------------------------------------------------------
32 -- Syntax
33
34 Thunk-syntax : ∀ {ℓ} → SizedSet ℓ → Size → Set ℓ
35 Thunk-syntax = Thunk
36
37 syntax Thunk-syntax (λ j → e) i = Thunk[ j < i ] e
38
39 ------------------------------------------------------------------------
40 -- Basic functions.
41
42 -- Thunk is a functor
43 module _ {p q} {P : SizedSet p} {Q : SizedSet q} where
44
45 map : ∀[ P ⇒ Q ] → ∀[ Thunk P ⇒ Thunk Q ]
46 map f p .force = f (p .force)
47
48 -- Thunk is a comonad
49 module _ {p} {P : SizedSet p} where
50
51 extract : ∀[ Thunk P ] → P ∞
52 extract p = p .force
53
54 duplicate : ∀[ Thunk P ⇒ Thunk (Thunk P) ]
55 duplicate p .force .force = p .force
56
57 module _ {p q} {P : SizedSet p} {Q : SizedSet q} where
58
59 infixl 1 _<*>_
60 _<*>_ : ∀[ Thunk (P ⇒ Q) ⇒ Thunk P ⇒ Thunk Q ]
61 (f <*> p) .force = f .force (p .force)
62
63 -- We can take cofixpoints of functions only making Thunk'd recursive calls
64 module _ {p} (P : SizedSet p) where
65
66 cofix : ∀[ Thunk P ⇒ P ] → ∀[ P ]
67 cofix f = f λ where .force → cofix f