33Let's take a look at how LambdaBuffers modules map into Haskell modules and how
44LambdaBuffers type definitions map into Haskell type definitions.
55
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
117the raw ` lbf ` CLI. We can get this tool by either loading the LambdaBuffers Nix
128environment that comes packaged with all the CLI tools:
139
1410``` shell
1511$ 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
1815```
1916
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`.
2219
2320In this chapter, we're going to use the latter option.
2421
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.
2823
2924``` purescript
3025module Document
3126
27+ -- Importing types
28+ import Prelude (Text, List, Set, Bytes)
29+
3230-- Author
3331sum Author = Ivan | Jovan | Savo
3432
@@ -38,7 +36,7 @@ sum Reviewer = Bob | Alice
3836-- Document
3937record Document a = {
4038 author : Author,
41- reviewers : List Reviewer,
39+ reviewers : Set Reviewer,
4240 content : Chapter a
4341 }
4442
@@ -49,43 +47,19 @@ 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-
6053prod 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)
7854```
7955
8056``` 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
8258$ find autogen/
8359autogen/
84- autogen/build.json
8560autogen/LambdaBuffers
8661autogen/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
8963```
9064
9165As 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
9468The outputted Haskell module in ` autogen/LambdaBuffers/Document.hs ` :
9569
9670``` 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
11579import qualified Prelude
11680
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 )
13781
138- data Gif = Gif'FunnyGif | Gif'InspiringGif deriving ( Prelude.Show )
82+ data Author = Author'Ivan | Author'Jovan | Author'Savo deriving Prelude.Show
13983
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
14186
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
14390
144- data List a = List'Nil | List'Cons a ( List a ) deriving ( Prelude.Show )
91+ data Reviewer = Reviewer'Bob | Reviewer'Alice deriving Prelude.Show
14592
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
14796
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+ ```
16099
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.
162102
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 )
164107```
165108
166109## Sum types
167110
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.
171112
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 .
174115
175116The only thing to notice is that the type name was prepended with ` ' ` (single
176117quote) to the defined constructor names as to make sure they are unique.
177118
178119## Product types
179120
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
181122LamdaBuffers schema using the ` prod ` keyword.
182123
183124They 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`.
195136Also like with product types, the constructor has the same name as the type.
196137
197138The 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