| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An either-or-both data type |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.These where |
| 10 |
|
| 11 |
open import Level |
| 12 |
open import Data.Maybe.Base using (Maybe; just; nothing; maybe′) |
| 13 |
open import Data.Sum.Base using (_⊎_; [_,_]′) |
| 14 |
open import Function.Base using (const; _∘′_; id; constᵣ) |
| 15 |
|
| 16 |
|
| 17 |
------------------------------------------------------------------------ |
| 18 |
-- Re-exporting the datatype and its operations |
| 19 |
|
| 20 |
open import Data.These.Base public |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
a b : Level |
| 25 |
A : Set a |
| 26 |
B : Set b |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- Additional operations |
| 30 |
|
| 31 |
-- projections |
| 32 |
|
| 33 |
fromThis : These A B → Maybe A |
| 34 |
fromThis = fold just (const nothing) (const ∘′ just) |
| 35 |
|
| 36 |
fromThat : These A B → Maybe B |
| 37 |
fromThat = fold (const nothing) just (const just) |
| 38 |
|
| 39 |
leftMost : These A A → A |
| 40 |
leftMost = fold id id const |
| 41 |
|
| 42 |
rightMost : These A A → A |
| 43 |
rightMost = fold id id constᵣ |
| 44 |
|
| 45 |
mergeThese : (A → A → A) → These A A → A |
| 46 |
mergeThese = fold id id |
| 47 |
|
| 48 |
-- deletions |
| 49 |
|
| 50 |
deleteThis : These A B → Maybe (These A B) |
| 51 |
deleteThis = fold (const nothing) (just ∘′ that) (const (just ∘′ that)) |
| 52 |
|
| 53 |
deleteThat : These A B → Maybe (These A B) |
| 54 |
deleteThat = fold (just ∘′ this) (const nothing) (const ∘′ just ∘′ this) |