Skip to content

Commit 7ddf57d

Browse files
author
jared
committed
Updated purescript.md and haskell.md to go "all the way" with using
opaque types from Prelude.
1 parent 538186e commit 7ddf57d

File tree

3 files changed

+31
-108
lines changed

3 files changed

+31
-108
lines changed

docs/examples/Document.lbf

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Document
22

3-
-- We need the opaque Char type
4-
import Prelude (Char)
3+
-- Importing types
4+
import Prelude (Text, List, Set, Bytes)
55

66
-- Author
77
sum Author = Ivan | Jovan | Savo
@@ -12,7 +12,7 @@ sum Reviewer = Bob | Alice
1212
-- Document
1313
record Document a = {
1414
author : Author,
15-
reviewers : List Reviewer,
15+
reviewers : Set Reviewer,
1616
content : Chapter a
1717
}
1818

@@ -23,20 +23,7 @@ record Chapter a = {
2323
}
2424

2525
-- Some actual content
26-
sum RichContent = Image Image String | Gif Gif String | Text String
27-
28-
sum Image = FunnyImage | BoringImage
29-
30-
sum Gif = FunnyGif | InspiringGif
26+
sum RichContent = Image Bytes | Gif Bytes | Text Text
3127

3228
-- Rich document
33-
3429
prod RichDocument = (Document RichContent)
35-
36-
-- # Some basic types
37-
38-
-- ## We need a list type
39-
sum List a = Nil | Cons a (List a)
40-
41-
-- ## String
42-
prod String = (List Char)

docs/haskell.md

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Let's now use `lbf-prelude-to-haskell` to process the [Document.lbf](examples/Do
2424
```purescript
2525
module Document
2626
27-
-- We need the opaque Char type
28-
import Prelude (Char)
27+
-- Importing types
28+
import Prelude (Text, List, Set, Bytes)
2929
3030
-- Author
3131
sum Author = Ivan | Jovan | Savo
@@ -36,7 +36,7 @@ sum Reviewer = Bob | Alice
3636
-- Document
3737
record Document a = {
3838
author : Author,
39-
reviewers : List Reviewer,
39+
reviewers : Set Reviewer,
4040
content : Chapter a
4141
}
4242
@@ -47,23 +47,10 @@ record Chapter a = {
4747
}
4848
4949
-- Some actual content
50-
sum RichContent = Image Image String | Gif Gif String | Text String
51-
52-
sum Image = FunnyImage | BoringImage
53-
54-
sum Gif = FunnyGif | InspiringGif
50+
sum RichContent = Image Bytes | Gif Bytes | Text Text
5551
5652
-- Rich document
57-
5853
prod RichDocument = (Document RichContent)
59-
60-
-- # Some basic types
61-
62-
-- ## We need a list type
63-
sum List a = Nil | Cons a (List a)
64-
65-
-- ## String
66-
prod String = (List Char)
6754
```
6855

6956
```shell
@@ -84,13 +71,9 @@ The outputted Haskell module in `autogen/LambdaBuffers/Document.hs`:
8471
module LambdaBuffers.Document (Author(..)
8572
, Chapter(..)
8673
, Document(..)
87-
, Gif(..)
88-
, Image(..)
89-
, List(..)
9074
, Reviewer(..)
9175
, RichContent(..)
92-
, RichDocument(..)
93-
, String(..)) where
76+
, RichDocument(..)) where
9477

9578
import qualified LambdaBuffers.Prelude
9679
import qualified Prelude
@@ -99,27 +82,19 @@ import qualified Prelude
9982
data Author = Author'Ivan | Author'Jovan | Author'Savo deriving Prelude.Show
10083

10184
data Chapter a = Chapter { chapter'content :: a
102-
, chapter'subChapters :: List (Chapter a)} deriving Prelude.Show
85+
, chapter'subChapters :: LambdaBuffers.Prelude.List (Chapter a)} deriving Prelude.Show
10386

10487
data Document a = Document { document'author :: Author
105-
, document'reviewers :: List Reviewer
88+
, document'reviewers :: LambdaBuffers.Prelude.Set Reviewer
10689
, document'content :: Chapter a} deriving Prelude.Show
10790

108-
data Gif = Gif'FunnyGif | Gif'InspiringGif deriving Prelude.Show
109-
110-
data Image = Image'FunnyImage | Image'BoringImage deriving Prelude.Show
111-
112-
data List a = List'Nil | List'Cons a (List a) deriving Prelude.Show
113-
11491
data Reviewer = Reviewer'Bob | Reviewer'Alice deriving Prelude.Show
11592

116-
data RichContent = RichContent'Image Image String
117-
| RichContent'Gif Gif String
118-
| RichContent'Text String deriving Prelude.Show
93+
data RichContent = RichContent'Image LambdaBuffers.Prelude.Bytes
94+
| RichContent'Gif LambdaBuffers.Prelude.Bytes
95+
| RichContent'Text LambdaBuffers.Prelude.Text deriving Prelude.Show
11996

12097
newtype RichDocument = RichDocument (Document RichContent) deriving Prelude.Show
121-
122-
newtype String = String (List LambdaBuffers.Prelude.Char) deriving Prelude.Show
12398
```
12499

125100
We can compile the code with the following commands.
@@ -133,7 +108,7 @@ $ ghc autogen/LambdaBuffers/Document.hs
133108

134109
## Sum types
135110

136-
The types `Author`, `Reviewer`, `RichContent`, `Image`, `Gif`, and `List` have been declared as sum types in the LamdaBuffers schema using the `sum` keyword.
111+
The types `Author`, `Reviewer`, and `RichContent` have been declared as sum types in the LamdaBuffers schema using the `sum` keyword.
137112

138113
As we can see, nothing too surprising here, all the `sum` types become `data`
139114
in Haskell.
@@ -143,7 +118,7 @@ quote) to the defined constructor names as to make sure they are unique.
143118

144119
## Product types
145120

146-
The types `RichDocument` and `String` have been declared as product types in the
121+
The type `RichDocument` have been declared as a product type in the
147122
LamdaBuffers schema using the `prod` keyword.
148123

149124
They become Haskell `newtype` if they have a single type in their body, otherwise they are `data`.
@@ -161,4 +136,4 @@ type in their body, otherwise they are `data`.
161136
Also like with product types, the constructor has the same name as the type.
162137

163138
The field names, similar to sum constructor names, are prepended with the
164-
lowercased named of the type with a single quote (`'`) to maintain uniqueness.
139+
lowercased name of the type with a single quote (`'`) to maintain uniqueness.

docs/purescript.md

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ github:mlabs-haskell/lambda-buffers#lbf-prelude-to-purescript`.
1919

2020
In this chapter, we're going to use the latter option.
2121

22-
Let's now use `lbf-prelude-to-purescript` to process the
23-
24-
[Document.lbf](examples/Document.lbf) schema
22+
Let's now use `lbf-prelude-to-purescript` to process the [Document.lbf](examples/Document.lbf) schema
2523

2624
```purescript
2725
module Document
2826
29-
-- We need the opaque Char type
30-
import Prelude (Char)
27+
-- Importing types
28+
import Prelude (Text, List, Set, Bytes)
3129
3230
-- Author
3331
sum Author = Ivan | Jovan | Savo
@@ -38,7 +36,7 @@ sum Reviewer = Bob | Alice
3836
-- Document
3937
record Document a = {
4038
author : Author,
41-
reviewers : List Reviewer,
39+
reviewers : Set Reviewer,
4240
content : Chapter a
4341
}
4442
@@ -49,23 +47,10 @@ record Chapter a = {
4947
}
5048
5149
-- Some actual content
52-
sum RichContent = Image Image String | Gif Gif String | Text String
53-
54-
sum Image = FunnyImage | BoringImage
55-
56-
sum Gif = FunnyGif | InspiringGif
50+
sum RichContent = Image Bytes | Gif Bytes | Text Text
5751
5852
-- Rich document
59-
6053
prod RichDocument = (Document RichContent)
61-
62-
-- # Some basic types
63-
64-
-- ## We need a list type
65-
sum List a = Nil | Cons a (List a)
66-
67-
-- ## String
68-
prod String = (List Char)
6954
```
7055

7156
```shell
@@ -86,13 +71,9 @@ The outputted Purescript module in `autogen/LambdaBuffers/Document.hs`:
8671
module LambdaBuffers.Document (Author(..)
8772
, Chapter(..)
8873
, Document(..)
89-
, Gif(..)
90-
, Image(..)
91-
, List(..)
9274
, Reviewer(..)
9375
, RichContent(..)
94-
, RichDocument(..)
95-
, String(..)) where
76+
, RichDocument(..)) where
9677
9778
import LambdaBuffers.Prelude as LambdaBuffers.Prelude
9879
import Data.Generic.Rep as Data.Generic.Rep
@@ -106,43 +87,29 @@ derive instance Data.Generic.Rep.Generic Author _
10687
instance Data.Show.Show Author where
10788
show = Data.Show.Generic.genericShow
10889
109-
newtype Chapter a = Chapter { content :: a, subChapters :: List (Chapter a)}
90+
newtype Chapter a = Chapter { content :: a
91+
, subChapters :: LambdaBuffers.Prelude.List (Chapter a)}
11092
derive instance Data.Newtype.Newtype (Chapter a) _
11193
derive instance Data.Generic.Rep.Generic (Chapter a) _
11294
instance (Data.Show.Show a) => Data.Show.Show (Chapter a) where
11395
show = Data.Show.Generic.genericShow
11496
11597
newtype Document a = Document { author :: Author
116-
, reviewers :: List Reviewer
98+
, reviewers :: LambdaBuffers.Prelude.Set Reviewer
11799
, content :: Chapter a}
118100
derive instance Data.Newtype.Newtype (Document a) _
119101
derive instance Data.Generic.Rep.Generic (Document a) _
120102
instance (Data.Show.Show a) => Data.Show.Show (Document a) where
121103
show = Data.Show.Generic.genericShow
122104
123-
data Gif = Gif'FunnyGif | Gif'InspiringGif
124-
derive instance Data.Generic.Rep.Generic Gif _
125-
instance Data.Show.Show Gif where
126-
show = Data.Show.Generic.genericShow
127-
128-
data Image = Image'FunnyImage | Image'BoringImage
129-
derive instance Data.Generic.Rep.Generic Image _
130-
instance Data.Show.Show Image where
131-
show = Data.Show.Generic.genericShow
132-
133-
data List a = List'Nil | List'Cons a (List a)
134-
derive instance Data.Generic.Rep.Generic (List a) _
135-
instance (Data.Show.Show a) => Data.Show.Show (List a) where
136-
show = Data.Show.Generic.genericShow
137-
138105
data Reviewer = Reviewer'Bob | Reviewer'Alice
139106
derive instance Data.Generic.Rep.Generic Reviewer _
140107
instance Data.Show.Show Reviewer where
141108
show = Data.Show.Generic.genericShow
142109
143-
data RichContent = RichContent'Image Image String
144-
| RichContent'Gif Gif String
145-
| RichContent'Text String
110+
data RichContent = RichContent'Image LambdaBuffers.Prelude.Bytes
111+
| RichContent'Gif LambdaBuffers.Prelude.Bytes
112+
| RichContent'Text LambdaBuffers.Prelude.Text
146113
derive instance Data.Generic.Rep.Generic RichContent _
147114
instance Data.Show.Show RichContent where
148115
show = Data.Show.Generic.genericShow
@@ -152,17 +119,11 @@ derive instance Data.Newtype.Newtype RichDocument _
152119
derive instance Data.Generic.Rep.Generic RichDocument _
153120
instance Data.Show.Show RichDocument where
154121
show = Data.Show.Generic.genericShow
155-
156-
newtype String = String (List LambdaBuffers.Prelude.Char)
157-
derive instance Data.Newtype.Newtype String _
158-
derive instance Data.Generic.Rep.Generic String _
159-
instance Data.Show.Show String where
160-
show = Data.Show.Generic.genericShow
161122
```
162123

163124
## Sum types
164125

165-
The types `Author`, `Reviewer`, `RichContent`, `Image`, `Gif`, and `List` have been declared as sum types in the LamdaBuffers schema using the `sum` keyword.
126+
The types `Author`, `Reviewer`, and `RichContent` have been declared as sum types in the LamdaBuffers schema using the `sum` keyword.
166127

167128
As we can see, nothing too surprising here, all the `sum` types become `data`
168129
in Purescript.
@@ -172,7 +133,7 @@ quote) to the defined constructor names as to make sure they are unique.
172133

173134
## Product types
174135

175-
The types `RichDocument` and `String` have been declared as product types in the
136+
The type `RichDocument` have been declared as a product type in the
176137
LamdaBuffers schema using the `prod` keyword.
177138

178139
They become Purescript `newtype` if they have a single type in their body, otherwise they are `data`.

0 commit comments

Comments
 (0)