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