|
1 | 1 | module Fathom.Data.Sing |
2 | 2 |
|
3 | 3 |
|
4 | | -||| A singleton type, constrained to be a single value |
| 4 | +||| A type constrained to a single value |
5 | 5 | ||| |
6 | | -||| The underlying value and the proof are both erased at runtime, as they can |
7 | | -||| be converted back to the index by reconstructing the value as required. |
8 | | -||| |
9 | | -||| Inspired by the singleton type [found in Adga’s documentation](https://agda.readthedocs.io/en/v2.5.4.1/language/with-abstraction.html#the-inspect-idiom). |
| 6 | +||| The underlying value is erased at runtime, as it can be converted back to |
| 7 | +||| the index by reconstructing the value as required. |
10 | 8 | public export |
11 | | -record Sing {0 A : Type} (x : A) where |
12 | | - constructor MkSing |
13 | | - ||| The underlying value of the singleton (erased at run-time) |
14 | | - 0 val : A |
15 | | - ||| A proof that @val is the same as the indexed value (erased at run-time) |
16 | | - {auto 0 prf : x = val} |
| 9 | +data Sing : {0 A : Type} -> (x : A) -> Type where |
| 10 | + MkSing : {0 A : Type} -> (0 x : A) -> Sing x |
17 | 11 |
|
18 | 12 |
|
19 | | -||| Convert a singleton back to its underlying value restoring it with a value |
20 | | -||| constructed runtime |
| 13 | +||| Reconstruct a singleton with a runtime value. |
21 | 14 | public export |
22 | 15 | val : {0 A : Type} -> {x : A} -> Sing x -> A |
23 | 16 | val (MkSing _) = x |
| 17 | + |
| 18 | + |
| 19 | +||| Update the value contained in a singleton with a function. |
| 20 | +export |
| 21 | +map : {0 A, B : Type} -> {0 x : A} -> (f : A -> B) -> Sing x -> Sing (f x) |
| 22 | +map f (MkSing y) = MkSing (f y) |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +namespace SingEq |
| 27 | + -- NOTE: Unsure if this representation is actually needed? |
| 28 | + |
| 29 | + ||| A type constrained to be a single value, with an associated equality proof. |
| 30 | + ||| |
| 31 | + ||| The underlying value and the proof are both erased at runtime, as they can |
| 32 | + ||| be converted back to the index by reconstructing the value as required. |
| 33 | + ||| |
| 34 | + ||| Inspired by the singleton type [found in Adga’s documentation](https://agda.readthedocs.io/en/v2.5.4.1/language/with-abstraction.html#the-inspect-idiom). |
| 35 | + public export |
| 36 | + record SingEq {0 A : Type} (x : A) where |
| 37 | + constructor MkSingEq |
| 38 | + ||| The underlying value of the singleton (erased at run-time) |
| 39 | + 0 val : A |
| 40 | + ||| A proof that @val is the same as the indexed value (erased at run-time) |
| 41 | + {auto 0 prf : x = val} |
| 42 | + |
| 43 | + |
| 44 | + ||| Convert a singleton back to its underlying value restoring it with a value |
| 45 | + ||| constructed runtime |
| 46 | + public export |
| 47 | + val : {0 A : Type} -> {x : A} -> SingEq x -> A |
| 48 | + val (MkSingEq _) = x |
| 49 | + |
| 50 | + |
| 51 | + ||| Update the value contained in a singleton with a function. |
| 52 | + export |
| 53 | + map : {0 A, B : Type} -> {0 x : A} -> (f : A -> B) -> SingEq x -> SingEq (f x) |
| 54 | + map f (MkSingEq y {prf}) = MkSingEq (f y) {prf = cong f prf} |
| 55 | + |
| 56 | + |
| 57 | +withEq : {0 A : Type} -> {0 x : A} -> Sing x -> SingEq x |
| 58 | +withEq (MkSing x) = MkSingEq x |
| 59 | + |
| 60 | +withoutEq : {0 A : Type} -> {0 x : A} -> SingEq x -> Sing x |
| 61 | +withoutEq {x} (MkSingEq _) = MkSing x |
0 commit comments