Skip to content

Commit b438b73

Browse files
andy-morriscwgoes
andauthored
Records (#6)
* Add Data instances for simple AST * ExtXXX records take an extra String for labels if a constructor is a record * Make the extension constructor a record if all others are * Clean up warnings in examples * Generate record patsyns for record constructors * Add record examples in examples/ & docs Co-authored-by: Christopher Goes <cwgoes@pluranimity.org>
1 parent fa102a8 commit b438b73

File tree

7 files changed

+208
-54
lines changed

7 files changed

+208
-54
lines changed

examples/deriv/Deriv.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extendA "A" [] [t|T|] $ defaultExtA {
99

1010
extendB "B" [] [t|T|] $ defaultExtB
1111

12+
main :: IO ()
1213
main = print $
1314
(A (B "") 5 ["a", "b"]) ==
1415
(A (B "") 5 ["a", "b", "c"] :: A String)

examples/param/Param.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ do an <- newName "a"
99
typeTX = [("Extra", a)]
1010
}
1111

12+
main :: IO ()
1213
main = pure ()

examples/qualified/Qualified.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ QualifiedBase.extendT "T" [] [t|()|] $ QualifiedBase.defaultExtT {
44
QualifiedBase.typeTX = [("C", [t|Char|])]
55
}
66

7+
main :: IO ()
78
main = pure ()

examples/record/Record.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Extensible
2+
import qualified RecordBase as R
3+
import qualified RecordBase
4+
5+
6+
data WithString
7+
8+
R.extendRec "Rec" [] [t|WithString|] $
9+
R.defaultExtRec {
10+
R.typeR1 = Ann ("label1", [t|String|]),
11+
R.typeR2 = Ann ("label2", [t|String|]),
12+
R.typeRecX = [("R3", "contents", [t|Int|])]
13+
}
14+
15+
main :: IO ()
16+
main = print (R1 {beep = True, boop = 4, label1 = "hello"} :: Rec)
17+
-- type annotation needed until the pat syns get signatures :/

examples/record/RecordBase.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module RecordBase where
2+
3+
import Extensible
4+
5+
6+
extensible [d|
7+
data Rec
8+
= R1 { beep :: Bool, boop :: Int }
9+
| R2 { inner :: Rec }
10+
deriving Show
11+
|]

extensible-data.cabal

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ common example
4444
default-extensions:
4545
TemplateHaskell, TypeFamilies, PatternSynonyms, ConstraintKinds,
4646
StandaloneDeriving, FlexibleContexts, UndecidableInstances
47-
ghc-options: -Wno-unused-top-binds -Wno-missing-pattern-synonym-signatures
47+
ghc-options:
48+
-Wno-unused-top-binds
49+
-Wno-missing-pattern-synonym-signatures
50+
-Wno-unused-imports
4851
if flag(dump-splices)
4952
ghc-options: -ddump-splices -fforce-recomp
5053
if !flag(build-examples)
@@ -85,3 +88,9 @@ executable deriv
8588
hs-source-dirs: examples/deriv
8689
main-is: Deriv.hs
8790
other-modules: DerivBase
91+
92+
executable record
93+
import: deps, example
94+
hs-source-dirs: examples/record
95+
main-is: Record.hs
96+
other-modules: RecordBase

0 commit comments

Comments
 (0)