Skip to content

Commit 93055a4

Browse files
committed
Implement a simpler singleton type
1 parent 01a2852 commit 93055a4

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

experiments/idris/src/Fathom/Closed/InductiveRecursiveCustom.idr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ simple_glyph =
188188
x => ?todo4
189189

190190
-- repeat' : Bits8
191-
-- repeat' with (MkSing flag)
192-
-- repeat' | MkSing 0 {prf} = rewrite sym prf in repeat
193-
-- repeat' | MkSing x {prf} = ?todo4
191+
-- repeat' with (MkSingEq flag)
192+
-- repeat' | MkSingEq 0 {prf} = rewrite sym prf in repeat
193+
-- repeat' | MkSingEq x {prf} = ?todo4
194194

195195
-- repeat' : Bits8
196-
-- repeat' = case MkSing flag of
197-
-- MkSing 0 {prf} => ?todo3
198-
-- MkSing x {prf} => ?todo4
196+
-- repeat' = case MkSingEq flag of
197+
-- MkSingEq 0 {prf} => ?todo3
198+
-- MkSingEq x {prf} => ?todo4
199199
in
200200
Pure (repeat' + 1))
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
11
module Fathom.Data.Sing
22

33

4-
||| A singleton type, constrained to be a single value
4+
||| A type constrained to a single value
55
|||
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.
108
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
1711

1812

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.
2114
public export
2215
val : {0 A : Type} -> {x : A} -> Sing x -> A
2316
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

Comments
 (0)