Skip to content

Commit 9f0cd55

Browse files
committed
Move refinement and singleton types into modules
1 parent 6487cea commit 9f0cd55

File tree

9 files changed

+61
-58
lines changed

9 files changed

+61
-58
lines changed

experiments/idris/fathom.ipkg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package fathom
1515
-- modules to install
1616
modules = Fathom
1717
, Fathom.Base
18+
, Fathom.Data.Sing
19+
, Fathom.Data.Refine
1820
, Fathom.Closed.IndexedInductive
1921
, Fathom.Closed.InductiveRecursive
2022
, Fathom.Closed.InductiveRecursiveCustom

experiments/idris/src/Fathom/Base.idr

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,6 @@ import Data.Colist
55
import Data.List
66

77

8-
------------------
9-
-- USEFUL TYPES --
10-
------------------
11-
12-
13-
||| A value that is refined by a proposition.
14-
|||
15-
||| This is a bit like `(x : A ** B)`, but with the second element erased.
16-
public export
17-
record Refine (0 A : Type) (0 P : A -> Type) where
18-
constructor MkRefine
19-
||| The wrapped value
20-
value : A
21-
||| The proof of the proposition
22-
{auto 0 prf : P value}
23-
24-
||| Refine a value with a proposition
25-
public export
26-
refine : {0 A : Type} -> {0 P : A -> Type} -> (value : A) -> {auto 0 prf : P value} -> Refine A P
27-
refine value = MkRefine { value }
28-
29-
30-
||| Singleton types
31-
|||
32-
||| Inspired by [this type](https://agda.readthedocs.io/en/v2.5.4.1/language/with-abstraction.html#the-inspect-idiom)
33-
||| from the Agda docs.
34-
public export
35-
record Sing {0 A : Type} (x : A) where
36-
constructor MkSing
37-
0 value : A
38-
{auto 0 prf : x = value}
39-
40-
41-
||| Convert a singleton back to its underlying value
42-
public export
43-
sing : {0 A : Type} -> {0 x : A} -> (0 value : A) -> {auto 0 prf : x = value} -> Sing x
44-
sing value = MkSing { value }
45-
46-
||| Convert a singleton back to its underlying value
47-
public export
48-
value : {0 Val : Type} -> {x : Val} -> Sing x -> Val
49-
value (MkSing _) = x
50-
51-
528
---------------------------
539
-- ENCODER/DECODER PAIRS --
5410
---------------------------

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Data.Colist
88
import Data.Vect
99

1010
import Fathom.Base
11+
import Fathom.Data.Sing
1112

1213

1314
-------------------------
@@ -37,7 +38,7 @@ decode End [] = Just ((), [])
3738
decode End (_::_) = Nothing
3839
decode Fail _ = Nothing
3940
decode (Pure x) buffer =
40-
Just (sing x, buffer)
41+
Just (MkSing x, buffer)
4142
decode (Skip f _) buffer = do
4243
(x, buffer') <- decode f buffer
4344
Just ((), buffer')

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import Data.Colist
2626
import Data.Vect
2727

2828
import Fathom.Base
29+
import Fathom.Data.Sing
30+
import Fathom.Data.Refine
2931

3032
-- import Fathom.Open.Record
3133

@@ -87,7 +89,7 @@ decode End [] = Just ((), [])
8789
decode End (_::_) = Nothing
8890
decode Fail _ = Nothing
8991
decode (Pure x) buffer =
90-
Just (sing x, buffer)
92+
Just (MkSing x, buffer)
9193
decode (Skip f _) buffer = do
9294
(x, buffer') <- decode f buffer
9395
Just ((), buffer')
@@ -150,25 +152,25 @@ FormatOf rep = Refine Format (\f => Rep f = rep)
150152

151153

152154
toFormatOf : (f : Format) -> FormatOf (Rep f)
153-
toFormatOf f = refine f
155+
toFormatOf f = MkRefine f
154156

155157

156158
export
157159
either : (cond : Bool) -> (f1 : Format) -> (f2 : Format) -> FormatOf (if cond then Rep f1 else Rep f2)
158-
either True f1 _ = refine f1
159-
either False _ f2 = refine f2
160+
either True f1 _ = MkRefine f1
161+
either False _ f2 = MkRefine f2
160162

161163

162164
export
163165
orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
164166
orPure True f _ = f
165-
orPure False _ def = refine (Pure def)
167+
orPure False _ def = MkRefine (Pure def)
166168

167169

168170
export
169171
orPure' : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
170172
orPure' True f _ = f
171-
orPure' False _ def = refine (Pure def)
173+
orPure' False _ def = MkRefine (Pure def)
172174

173175

174176
foo : (cond : Bool) -> (f : Format) -> Rep f -> Format

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Data.Colist
88
import Data.Vect
99

1010
import Fathom.Base
11+
import Fathom.Data.Sing
12+
import Fathom.Data.Refine
1113

1214

1315
-------------------------
@@ -64,7 +66,7 @@ decode End [] = Just ((), [])
6466
decode End (_::_) = Nothing
6567
decode Fail _ = Nothing
6668
decode (Pure x) buffer =
67-
Just (sing x, buffer)
69+
Just (MkSing x, buffer)
6870
decode (Skip f _) buffer = do
6971
(x, buffer') <- decode f buffer
7072
Just ((), buffer')
@@ -127,25 +129,25 @@ FormatOf rep = Refine Format (\f => Rep f = rep)
127129

128130

129131
toFormatOf : (f : Format) -> FormatOf (Rep f)
130-
toFormatOf f = refine f
132+
toFormatOf f = MkRefine f
131133

132134

133135
export
134136
either : (cond : Bool) -> (f1 : Format) -> (f2 : Format) -> FormatOf (if cond then Rep f1 else Rep f2)
135-
either True f1 _ = refine f1
136-
either False _ f2 = refine f2
137+
either True f1 _ = MkRefine f1
138+
either False _ f2 = MkRefine f2
137139

138140

139141
export
140142
orPure : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
141143
orPure True f _ = f
142-
orPure False _ def = refine (Pure def)
144+
orPure False _ def = MkRefine (Pure def)
143145

144146

145147
export
146148
orPure' : (cond : Bool) -> FormatOf a -> (def : a) -> FormatOf (if cond then a else Sing def)
147149
orPure' True f _ = f
148-
orPure' False _ def = refine (Pure def)
150+
orPure' False _ def = MkRefine (Pure def)
149151

150152

151153
foo : (cond : Bool) -> (f : Format) -> Rep f -> Format
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Fathom.Data.Refine
2+
3+
4+
||| A value that is refined by a proposition.
5+
|||
6+
||| The proof of the proposition is erased at runtime.
7+
|||
8+
||| This is a bit like `(x : A ** B)`, but with the second element erased.
9+
public export
10+
record Refine (0 A : Type) (0 P : A -> Type) where
11+
constructor MkRefine
12+
||| The refined value
13+
val : A
14+
||| The a proof that @val is refined by @P
15+
{auto 0 prf : P val}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Fathom.Data.Sing
2+
3+
4+
||| A singleton type, constrained to be a single value
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).
10+
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}
17+
18+
19+
||| Convert a singleton back to its underlying value restoring it with a value
20+
||| constructed runtime
21+
public export
22+
val : {0 A : Type} -> {x : A} -> Sing x -> A
23+
val (MkSing _) = x

experiments/idris/src/Fathom/Open/Record.idr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Data.Colist
1515
import Data.Vect
1616

1717
import Fathom.Base
18+
import Fathom.Data.Sing
1819

1920

2021
public export
@@ -57,7 +58,7 @@ pure x = MkFormat { Rep, decode, encode } where
5758
Rep = Sing x
5859

5960
decode : Decode Rep BitStream
60-
decode buffer = Just (sing x, buffer)
61+
decode buffer = Just (MkSing x, buffer)
6162

6263
encode : Encode Rep BitStream
6364
encode (MkSing _) buffer = Just buffer

experiments/idris/src/Playground.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Data.Colist
55
import Data.Vect
66

77
import Fathom.Base
8+
import Fathom.Data.Sing
89
import Fathom.Closed.InductiveRecursive as IndRec
910
import Fathom.Closed.IndexedInductive as Indexed
1011
import Fathom.Open.Record as Record

0 commit comments

Comments
 (0)