3
3
Let's take a look at how LambdaBuffers modules map into Haskell modules and how
4
4
LambdaBuffers type definitions map into Haskell type definitions.
5
5
6
- Note that in this chapter we work with a 'pure' LambdaBuffers module, no
7
- ` opaque ` s or type clasess, to demonstrate how pure type definition mapping
8
- works.
9
-
10
- We'll use the ` lbf-to-haskell ` CLI tool which is just a convenient wrapper over
6
+ We'll use the ` lbf-prelude-to-haskell ` CLI tool which is just a convenient wrapper over
11
7
the raw ` lbf ` CLI. We can get this tool by either loading the LambdaBuffers Nix
12
8
environment that comes packaged with all the CLI tools:
13
9
14
10
``` shell
15
11
$ nix develop github:mlabs-haskell/lambda-buffers#lb
16
- $ lb< tab>
17
- lbc lbf lbg lbg-purescript lbg-haskelll lbf-to-purescript lbf-to-haskell
12
+ $ lbf< tab>
13
+ lbf lbf-plutus-to-purescript lbf-prelude-to-purescript
14
+ lbf-plutus-to-haskell lbf-prelude-to-haskell
18
15
```
19
16
20
- Or we can simply just refer directly to the ` lbf-to-haskell ` CLI by `nix run
21
- github: mlabs-haskell /lambda-buffers#lbf-to-haskell`.
17
+ Or we can simply just refer directly to the ` lbf-prelude- to-haskell ` CLI by `nix run
18
+ github: mlabs-haskell /lambda-buffers#lbf-prelude- to-haskell`.
22
19
23
20
In this chapter, we're going to use the latter option.
24
21
25
- Let's now use ` lbf-to-haskell ` to process the
26
-
27
- [ Document.lbf] ( examples/Document.lbf ) schema
22
+ Let's now use ` lbf-prelude-to-haskell ` to process the [ Document.lbf] ( examples/Document.lbf ) schema.
28
23
29
24
``` purescript
30
25
module Document
31
26
27
+ -- Importing types
28
+ import Prelude (Text, List, Set, Bytes)
29
+
32
30
-- Author
33
31
sum Author = Ivan | Jovan | Savo
34
32
@@ -38,7 +36,7 @@ sum Reviewer = Bob | Alice
38
36
-- Document
39
37
record Document a = {
40
38
author : Author,
41
- reviewers : List Reviewer,
39
+ reviewers : Set Reviewer,
42
40
content : Chapter a
43
41
}
44
42
@@ -49,43 +47,19 @@ record Chapter a = {
49
47
}
50
48
51
49
-- 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
57
51
58
52
-- Rich document
59
-
60
53
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
- -- ## We need a Char type that is either a letter, number or punctuation
68
- sum Char = Letter Letter | Number Number | Punctuation Punctuation
69
-
70
- sum Letter = A | B | C
71
-
72
- sum Number = Num0 | Num1 | Num2
73
-
74
- sum Punctuation = Dot | Question
75
-
76
- -- ## String
77
- prod String = (List Char)
78
54
```
79
55
80
56
``` shell
81
- $ nix run github:mlabs-haskell/lambda-buffers#lbf-to-haskell -- Document.lbf
57
+ $ nix run github:mlabs-haskell/lambda-buffers#lbf-prelude- to-haskell -- Document.lbf
82
58
$ find autogen/
83
59
autogen/
84
- autogen/build.json
85
60
autogen/LambdaBuffers
86
61
autogen/LambdaBuffers/Document.hs
87
- $ ghc autogen/LambdaBuffers/Document.hs
88
- [1 of 1] Compiling LambdaBuffers.Document ( autogen/LambdaBuffers/Document.hs, autogen/LambdaBuffers/Document.o )
62
+ autogen/build.json
89
63
```
90
64
91
65
As we can see the ` autogen ` directory has been created that contains the generated Haskell modules.
@@ -94,90 +68,57 @@ Note the `autogen/build.json` file as it contains all the necessary Hackage depe
94
68
The outputted Haskell module in ` autogen/LambdaBuffers/Document.hs ` :
95
69
96
70
``` haskell
97
- module LambdaBuffers.Document
98
- ( Author (.. ),
99
- Chapter (.. ),
100
- Char (.. ),
101
- Document (.. ),
102
- Gif (.. ),
103
- Image (.. ),
104
- Letter (.. ),
105
- List (.. ),
106
- Number (.. ),
107
- Punctuation (.. ),
108
- Reviewer (.. ),
109
- RichContent (.. ),
110
- RichDocument (.. ),
111
- String (.. ),
112
- )
113
- where
114
-
71
+ module LambdaBuffers.Document (Author (.. )
72
+ , Chapter (.. )
73
+ , Document (.. )
74
+ , Reviewer (.. )
75
+ , RichContent (.. )
76
+ , RichDocument (.. )) where
77
+
78
+ import qualified LambdaBuffers.Prelude
115
79
import qualified Prelude
116
80
117
- data Author = Author'Ivan | Author'Jovan | Author'Savo deriving (Prelude.Show )
118
-
119
- data Chapter a = Chapter
120
- { chapter'content :: a ,
121
- chapter'subChapters :: List (Chapter a )
122
- }
123
- deriving (Prelude.Show )
124
-
125
- data Char
126
- = Char'Letter Letter
127
- | Char'Number Number
128
- | Char'Punctuation Punctuation
129
- deriving (Prelude.Show )
130
-
131
- data Document a = Document
132
- { document'author :: Author ,
133
- document'reviewers :: List Reviewer ,
134
- document'content :: Chapter a
135
- }
136
- deriving (Prelude.Show )
137
81
138
- data Gif = Gif'FunnyGif | Gif'InspiringGif deriving ( Prelude.Show )
82
+ data Author = Author'Ivan | Author'Jovan | Author'Savo deriving Prelude.Show
139
83
140
- data Image = Image'FunnyImage | Image'BoringImage deriving (Prelude.Show )
84
+ data Chapter a = Chapter { chapter'content :: a
85
+ , chapter'subChapters :: LambdaBuffers.Prelude. List (Chapter a )} deriving Prelude.Show
141
86
142
- data Letter = Letter'A | Letter'B | Letter'C deriving (Prelude.Show )
87
+ data Document a = Document { document'author :: Author
88
+ , document'reviewers :: LambdaBuffers.Prelude. Set Reviewer
89
+ , document'content :: Chapter a } deriving Prelude.Show
143
90
144
- data List a = List'Nil | List'Cons a ( List a ) deriving ( Prelude.Show )
91
+ data Reviewer = Reviewer'Bob | Reviewer'Alice deriving Prelude.Show
145
92
146
- data Number = Number'Num0 | Number'Num1 | Number'Num2 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
147
96
148
- data Punctuation
149
- = Punctuation'Dot
150
- | Punctuation'Question
151
- deriving (Prelude.Show )
152
-
153
- data Reviewer = Reviewer'Bob | Reviewer'Alice deriving (Prelude.Show )
154
-
155
- data RichContent
156
- = RichContent'Image Image String
157
- | RichContent'Gif Gif String
158
- | RichContent'Text String
159
- deriving (Prelude.Show )
97
+ newtype RichDocument = RichDocument (Document RichContent ) deriving Prelude.Show
98
+ ```
160
99
161
- newtype RichDocument = RichDocument (Document RichContent ) deriving (Prelude.Show )
100
+ We can compile the code with the following commands.
101
+ Note the dev shell `dev- prelude- haskell` as it includes the `LambdaBuffers. Prelude ` dependency.
162
102
163
- newtype String = String (List Char ) deriving (Prelude.Show )
103
+ ```shell
104
+ $ nix develop github: mlabs- haskell/ lambda- buffers# dev- prelude- haskell
105
+ $ ghc autogen/ LambdaBuffers / Document. hs
106
+ [1 of 1 ] Compiling LambdaBuffers. Document ( autogen/ LambdaBuffers / Document. hs, autogen/ LambdaBuffers / Document. o )
164
107
```
165
108
166
109
## Sum types
167
110
168
- The types `Author `, `Reviewer `, `RichContent `, `Image `, `Gif `, `List `, `Char `,
169
- `Letter `, `Number ` and `Punctuation ` have been declared as sum types in the
170
- 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.
171
112
172
- As we can see, notihing too surprising here, all the `sum` types become `data`
173
- in haskell .
113
+ As we can see, nothing too surprising here, all the ` sum ` types become ` data `
114
+ in Haskell .
174
115
175
116
The only thing to notice is that the type name was prepended with ` ' ` (single
176
117
quote) to the defined constructor names as to make sure they are unique.
177
118
178
119
## Product types
179
120
180
- 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
181
122
LamdaBuffers schema using the ` prod ` keyword.
182
123
183
124
They become Haskell ` newtype ` if they have a single type in their body, otherwise they are ` data ` .
@@ -195,4 +136,4 @@ type in their body, otherwise they are `data`.
195
136
Also like with product types, the constructor has the same name as the type.
196
137
197
138
The field names, similar to sum constructor names, are prepended with the
198
- 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.
0 commit comments