forked from agda/agda-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase.agda
270 lines (189 loc) · 7.43 KB
/
Base.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
------------------------------------------------------------------------
-- The Agda standard library
--
-- Simple combinators working solely on and with functions
------------------------------------------------------------------------
-- The contents of this module is also accessible via the `Function`
-- module. See `Function.Strict` for strict versions of these
-- combinators.
{-# OPTIONS --cubical-compatible --safe #-}
module Function.Base where
open import Level using (Level)
private
variable
a b c d e : Level
A : Set a
B : Set b
C : Set c
D : Set d
E : Set e
------------------------------------------------------------------------
-- Some simple functions
id : A → A
id x = x
const : A → B → A
const x = λ _ → x
constᵣ : A → B → B
constᵣ _ = id
------------------------------------------------------------------------
-- Operations on dependent functions
-- These are functions whose output has a type that depends on the
-- value of the input to the function.
infixr 9 _∘_ _∘₂_
infixl 8 _ˢ_
infixl 0 _|>_
infix 0 case_returning_of_ case_return_of_
infixr -1 _$_
-- Composition
_∘_ : ∀ {A : Set a} {B : A → Set b} {C : {x : A} → B x → Set c} →
(∀ {x} (y : B x) → C y) → (g : (x : A) → B x) →
((x : A) → C (g x))
f ∘ g = λ x → f (g x)
{-# INLINE _∘_ #-}
_∘₂_ : ∀ {A₁ : Set a} {A₂ : A₁ → Set d}
{B : (x : A₁) → A₂ x → Set b}
{C : {x : A₁} → {y : A₂ x} → B x y → Set c} →
({x : A₁} → {y : A₂ x} → (z : B x y) → C z) →
(g : (x : A₁) → (y : A₂ x) → B x y) →
((x : A₁) → (y : A₂ x) → C (g x y))
f ∘₂ g = λ x y → f (g x y)
-- Flipping order of arguments
flip : ∀ {A : Set a} {B : Set b} {C : A → B → Set c} →
((x : A) (y : B) → C x y) → ((y : B) (x : A) → C x y)
flip f = λ y x → f x y
{-# INLINE flip #-}
-- Application - note that _$_ is right associative, as in Haskell.
-- If you want a left associative infix application operator, use
-- RawFunctor._<$>_ from Effect.Functor.
_$_ : ∀ {A : Set a} {B : A → Set b} →
((x : A) → B x) → ((x : A) → B x)
f $ x = f x
{-# INLINE _$_ #-}
-- Flipped application (aka pipe-forward)
_|>_ : ∀ {A : Set a} {B : A → Set b} →
(a : A) → (∀ a → B a) → B a
_|>_ = flip _$_
{-# INLINE _|>_ #-}
-- The S combinator - written infix as in Conor McBride's paper
-- "Outrageous but Meaningful Coincidences: Dependent type-safe syntax
-- and evaluation".
_ˢ_ : ∀ {A : Set a} {B : A → Set b} {C : (x : A) → B x → Set c} →
((x : A) (y : B x) → C x y) →
(g : (x : A) → B x) →
((x : A) → C x (g x))
f ˢ g = λ x → f x (g x)
{-# INLINE _ˢ_ #-}
-- Converting between implicit and explicit function spaces.
_$- : ∀ {A : Set a} {B : A → Set b} → ((x : A) → B x) → ({x : A} → B x)
f $- = f _
{-# INLINE _$- #-}
λ- : ∀ {A : Set a} {B : A → Set b} → ({x : A} → B x) → ((x : A) → B x)
λ- f = λ x → f
{-# INLINE λ- #-}
-- Case expressions (to be used with pattern-matching lambdas, see
-- README.Case).
case_returning_of_ : ∀ {A : Set a} (x : A) (B : A → Set b) →
((x : A) → B x) → B x
case x returning B of f = f x
{-# INLINE case_returning_of_ #-}
------------------------------------------------------------------------
-- Non-dependent versions of dependent operations
-- Any of the above operations for dependent functions will also work
-- for non-dependent functions but sometimes Agda has difficulty
-- inferring the non-dependency. Primed (′ = \prime) versions of the
-- operations are therefore provided below that sometimes have better
-- inference properties.
infixr 9 _∘′_ _∘₂′_
infixl 0 _|>′_
infix 0 case_of_
infixr -1 _$′_
-- Composition
_∘′_ : (B → C) → (A → B) → (A → C)
f ∘′ g = _∘_ f g
_∘₂′_ : (C → D) → (A → B → C) → (A → B → D)
f ∘₂′ g = _∘₂_ f g
-- Flipping order of arguments
flip′ : (A → B → C) → (B → A → C)
flip′ = flip
-- Application
_$′_ : (A → B) → (A → B)
_$′_ = _$_
-- Flipped application (aka pipe-forward)
_|>′_ : A → (A → B) → B
_|>′_ = _|>_
-- Case expressions (to be used with pattern-matching lambdas, see
-- README.Case).
case_of_ : A → (A → B) → B
case x of f = case x returning _ of f
{-# INLINE case_of_ #-}
------------------------------------------------------------------------
-- Operations that are only defined for non-dependent functions
infixl 1 _⟨_⟩_
infixl 0 _∋_
-- Binary application
_⟨_⟩_ : A → (A → B → C) → B → C
x ⟨ f ⟩ y = f x y
-- In Agda you cannot annotate every subexpression with a type
-- signature. This function can be used instead.
_∋_ : (A : Set a) → A → A
A ∋ x = x
-- Conversely it is sometimes useful to be able to extract the
-- type of a given expression.
typeOf : {A : Set a} → A → Set a
typeOf {A = A} _ = A
-- Construct an element of the given type by instance search.
it : {A : Set a} → {{A}} → A
it {{x}} = x
------------------------------------------------------------------------
-- Composition of a binary function with other functions
infixr 0 _-⟪_⟫-_ _-⟨_⟫-_
infixl 0 _-⟪_⟩-_
infixr 1 _-⟨_⟩-_ ∣_⟫-_ ∣_⟩-_
infixl 1 _on_ _on₂_ _-⟪_∣ _-⟨_∣
-- Two binary functions
_-⟪_⟫-_ : (A → B → C) → (C → D → E) → (A → B → D) → (A → B → E)
f -⟪ _*_ ⟫- g = λ x y → f x y * g x y
-- A single binary function on the left
_-⟪_∣ : (A → B → C) → (C → B → D) → (A → B → D)
f -⟪ _*_ ∣ = f -⟪ _*_ ⟫- constᵣ
-- A single binary function on the right
∣_⟫-_ : (A → C → D) → (A → B → C) → (A → B → D)
∣ _*_ ⟫- g = const -⟪ _*_ ⟫- g
-- A single unary function on the left
_-⟨_∣ : (A → C) → (C → B → D) → (A → B → D)
f -⟨ _*_ ∣ = f ∘₂ const -⟪ _*_ ∣
-- A single unary function on the right
∣_⟩-_ : (A → C → D) → (B → C) → (A → B → D)
∣ _*_ ⟩- g = ∣ _*_ ⟫- g ∘₂ constᵣ
-- A binary function and a unary function
_-⟪_⟩-_ : (A → B → C) → (C → D → E) → (B → D) → (A → B → E)
f -⟪ _*_ ⟩- g = f -⟪ _*_ ⟫- ∣ constᵣ ⟩- g
-- A unary function and a binary function
_-⟨_⟫-_ : (A → C) → (C → D → E) → (A → B → D) → (A → B → E)
f -⟨ _*_ ⟫- g = f -⟨ const ∣ -⟪ _*_ ⟫- g
-- Two unary functions
_-⟨_⟩-_ : (A → C) → (C → D → E) → (B → D) → (A → B → E)
f -⟨ _*_ ⟩- g = f -⟨ const ∣ -⟪ _*_ ⟫- ∣ constᵣ ⟩- g
-- A single binary function on both sides
_on₂_ : (C → C → D) → (A → B → C) → (A → B → D)
_*_ on₂ f = f -⟪ _*_ ⟫- f
-- A single unary function on both sides
_on_ : (B → B → C) → (A → B) → (A → A → C)
_*_ on f = f -⟨ _*_ ⟩- f
------------------------------------------------------------------------
-- DEPRECATED NAMES
------------------------------------------------------------------------
-- Please use the new names as continuing support for the old names is
-- not guaranteed.
-- Version 1.4
_-[_]-_ = _-⟪_⟫-_
{-# WARNING_ON_USAGE _-[_]-_
"Warning: Function._-[_]-_ was deprecated in v1.4.
Please use _-⟪_⟫-_ instead."
#-}
-- Version 2.0
case_return_of_ = case_returning_of_
{-# WARNING_ON_USAGE case_return_of_
"case_return_of_ was deprecated in v2.0.
Please use case_returning_of_ instead."
#-}