Skip to content

Commit

Permalink
Update gh-pages
Browse files Browse the repository at this point in the history
Store path: /nix/store/6244lmlgkdfb1ga3wn8h1ndf70r45pgg-lambda-buffers-book
  • Loading branch information
hercules-ci[bot] committed Oct 26, 2023
1 parent 99eff2b commit 76e38c5
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 429 deletions.
29 changes: 5 additions & 24 deletions examples/Document.lbf
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Document

-- Importing types
import Prelude (Text, List, Set, Bytes)

-- Author
sum Author = Ivan | Jovan | Savo

Expand All @@ -9,7 +12,7 @@ sum Reviewer = Bob | Alice
-- Document
record Document a = {
author : Author,
reviewers : List Reviewer,
reviewers : Set Reviewer,
content : Chapter a
}

Expand All @@ -20,29 +23,7 @@ record Chapter a = {
}

-- Some actual content
sum RichContent = Image Image String | Gif Gif String | Text String

sum Image = FunnyImage | BoringImage

sum Gif = FunnyGif | InspiringGif
sum RichContent = Image Bytes | Gif Bytes | Text Text

-- Rich document

prod RichDocument = (Document RichContent)

-- # Some basic types

-- ## We need a list type
sum List a = Nil | Cons a (List a)

-- ## We need a Char type that is either a letter, number or punctuation
sum Char = Letter Letter | Number Number | Punctuation Punctuation

sum Letter = A | B | C

sum Number = Num0 | Num1 | Num2

sum Punctuation = Dot | Question

-- ## String
prod String = (List Char)
146 changes: 43 additions & 103 deletions haskell.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,22 @@ <h1 class="menu-title">Lambda Buffers documentation</h1>
<h1 id="lambdabuffers-to-haskell"><a class="header" href="#lambdabuffers-to-haskell">LambdaBuffers to Haskell</a></h1>
<p>Let's take a look at how LambdaBuffers modules map into Haskell modules and how
LambdaBuffers type definitions map into Haskell type definitions.</p>
<p>Note that in this chapter we work with a 'pure' LambdaBuffers module, no
<code>opaque</code>s or type clasess, to demonstrate how pure type definition mapping
works.</p>
<p>We'll use the <code>lbf-to-haskell</code> CLI tool which is just a convenient wrapper over
<p>We'll use the <code>lbf-prelude-to-haskell</code> CLI tool which is just a convenient wrapper over
the raw <code>lbf</code> CLI. We can get this tool by either loading the LambdaBuffers Nix
environment that comes packaged with all the CLI tools:</p>
<pre><code class="language-shell">$ nix develop github:mlabs-haskell/lambda-buffers#lb
$ lb&lt;tab&gt;
lbc lbf lbg lbg-purescript lbg-haskelll lbf-to-purescript lbf-to-haskell
$ lbf&lt;tab&gt;
lbf lbf-plutus-to-purescript lbf-prelude-to-purescript
lbf-plutus-to-haskell lbf-prelude-to-haskell
</code></pre>
<p>Or we can simply just refer directly to the <code>lbf-to-haskell</code> CLI by <code>nix run github:mlabs-haskell/lambda-buffers#lbf-to-haskell</code>.</p>
<p>Or we can simply just refer directly to the <code>lbf-prelude-to-haskell</code> CLI by <code>nix run github:mlabs-haskell/lambda-buffers#lbf-prelude-to-haskell</code>.</p>
<p>In this chapter, we're going to use the latter option.</p>
<p>Let's now use <code>lbf-to-haskell</code> to process the</p>
<p><a href="examples/Document.lbf">Document.lbf</a> schema</p>
<p>Let's now use <code>lbf-prelude-to-haskell</code> to process the <a href="examples/Document.lbf">Document.lbf</a> schema.</p>
<pre><code class="language-purescript">module Document

-- Importing types
import Prelude (Text, List, Set, Bytes)

-- Author
sum Author = Ivan | Jovan | Savo

Expand All @@ -172,7 +172,7 @@ <h1 id="lambdabuffers-to-haskell"><a class="header" href="#lambdabuffers-to-hask
-- Document
record Document a = {
author : Author,
reviewers : List Reviewer,
reviewers : Set Reviewer,
content : Chapter a
}

Expand All @@ -183,123 +183,63 @@ <h1 id="lambdabuffers-to-haskell"><a class="header" href="#lambdabuffers-to-hask
}

-- Some actual content
sum RichContent = Image Image String | Gif Gif String | Text String

sum Image = FunnyImage | BoringImage

sum Gif = FunnyGif | InspiringGif
sum RichContent = Image Bytes | Gif Bytes | Text Text

-- Rich document

prod RichDocument = (Document RichContent)

-- # Some basic types

-- ## We need a list type
sum List a = Nil | Cons a (List a)

-- ## We need a Char type that is either a letter, number or punctuation
sum Char = Letter Letter | Number Number | Punctuation Punctuation

sum Letter = A | B | C

sum Number = Num0 | Num1 | Num2

sum Punctuation = Dot | Question

-- ## String
prod String = (List Char)
</code></pre>
<pre><code class="language-shell">$ nix run github:mlabs-haskell/lambda-buffers#lbf-to-haskell -- Document.lbf
<pre><code class="language-shell">$ nix run github:mlabs-haskell/lambda-buffers#lbf-prelude-to-haskell -- Document.lbf
$ find autogen/
autogen/
autogen/build.json
autogen/LambdaBuffers
autogen/LambdaBuffers/Document.hs
$ ghc autogen/LambdaBuffers/Document.hs
[1 of 1] Compiling LambdaBuffers.Document ( autogen/LambdaBuffers/Document.hs, autogen/LambdaBuffers/Document.o )
autogen/build.json
</code></pre>
<p>As we can see the <code>autogen</code> directory has been created that contains the generated Haskell modules.
Note the <code>autogen/build.json</code> file as it contains all the necessary Hackage dependencies the generated module needs in order to be properly compiled by GHC.</p>
<p>The outputted Haskell module in <code>autogen/LambdaBuffers/Document.hs</code>:</p>
<pre><code class="language-haskell">module LambdaBuffers.Document
( Author (..),
Chapter (..),
Char (..),
Document (..),
Gif (..),
Image (..),
Letter (..),
List (..),
Number (..),
Punctuation (..),
Reviewer (..),
RichContent (..),
RichDocument (..),
String (..),
)
where

<pre><code class="language-haskell">module LambdaBuffers.Document (Author(..)
, Chapter(..)
, Document(..)
, Reviewer(..)
, RichContent(..)
, RichDocument(..)) where

import qualified LambdaBuffers.Prelude
import qualified Prelude

data Author = Author'Ivan | Author'Jovan | Author'Savo deriving (Prelude.Show)

data Chapter a = Chapter
{ chapter'content :: a,
chapter'subChapters :: List (Chapter a)
}
deriving (Prelude.Show)

data Char
= Char'Letter Letter
| Char'Number Number
| Char'Punctuation Punctuation
deriving (Prelude.Show)

data Document a = Document
{ document'author :: Author,
document'reviewers :: List Reviewer,
document'content :: Chapter a
}
deriving (Prelude.Show)

data Gif = Gif'FunnyGif | Gif'InspiringGif deriving (Prelude.Show)

data Image = Image'FunnyImage | Image'BoringImage deriving (Prelude.Show)
data Author = Author'Ivan | Author'Jovan | Author'Savo deriving Prelude.Show

data Letter = Letter'A | Letter'B | Letter'C deriving (Prelude.Show)
data Chapter a = Chapter { chapter'content :: a
, chapter'subChapters :: LambdaBuffers.Prelude.List (Chapter a)} deriving Prelude.Show

data List a = List'Nil | List'Cons a (List a) deriving (Prelude.Show)
data Document a = Document { document'author :: Author
, document'reviewers :: LambdaBuffers.Prelude.Set Reviewer
, document'content :: Chapter a} deriving Prelude.Show

data Number = Number'Num0 | Number'Num1 | Number'Num2 deriving (Prelude.Show)
data Reviewer = Reviewer'Bob | Reviewer'Alice deriving Prelude.Show

data Punctuation
= Punctuation'Dot
| Punctuation'Question
deriving (Prelude.Show)
data RichContent = RichContent'Image LambdaBuffers.Prelude.Bytes
| RichContent'Gif LambdaBuffers.Prelude.Bytes
| RichContent'Text LambdaBuffers.Prelude.Text deriving Prelude.Show

data Reviewer = Reviewer'Bob | Reviewer'Alice deriving (Prelude.Show)

data RichContent
= RichContent'Image Image String
| RichContent'Gif Gif String
| RichContent'Text String
deriving (Prelude.Show)

newtype RichDocument = RichDocument (Document RichContent) deriving (Prelude.Show)

newtype String = String (List Char) deriving (Prelude.Show)
newtype RichDocument = RichDocument (Document RichContent) deriving Prelude.Show
</code></pre>
<p>We can compile the code with the following commands.
Note the dev shell <code>dev-prelude-haskell</code> as it includes the <code>LambdaBuffers.Prelude</code> dependency.</p>
<pre><code class="language-shell">$ nix develop github:mlabs-haskell/lambda-buffers#dev-prelude-haskell
$ ghc autogen/LambdaBuffers/Document.hs
[1 of 1] Compiling LambdaBuffers.Document ( autogen/LambdaBuffers/Document.hs, autogen/LambdaBuffers/Document.o )
</code></pre>
<h2 id="sum-types"><a class="header" href="#sum-types">Sum types</a></h2>
<p>The types <code>Author</code>, <code>Reviewer</code>, <code>RichContent</code>, <code>Image</code>, <code>Gif</code>, <code>List</code>, <code>Char</code>,
<code>Letter</code>, <code>Number</code> and <code>Punctuation</code> have been declared as sum types in the
LamdaBuffers schema using the <code>sum</code> keyword.</p>
<p>As we can see, notihing too surprising here, all the <code>sum</code> types become <code>data</code>
in haskell.</p>
<p>The types <code>Author</code>, <code>Reviewer</code>, and <code>RichContent</code> have been declared as sum types in the LamdaBuffers schema using the <code>sum</code> keyword.</p>
<p>As we can see, nothing too surprising here, all the <code>sum</code> types become <code>data</code>
in Haskell.</p>
<p>The only thing to notice is that the type name was prepended with <code>'</code> (single
quote) to the defined constructor names as to make sure they are unique.</p>
<h2 id="product-types"><a class="header" href="#product-types">Product types</a></h2>
<p>The types <code>RichDocument</code> and <code>String</code> have been declared as product types in the
<p>The type <code>RichDocument</code> have been declared as a product type in the
LamdaBuffers schema using the <code>prod</code> keyword.</p>
<p>They become Haskell <code>newtype</code> if they have a single type in their body, otherwise they are <code>data</code>.</p>
<p>Note that the constructor has the same name as the type.</p>
Expand All @@ -310,7 +250,7 @@ <h2 id="record-types"><a class="header" href="#record-types">Record types</a></h
type in their body, otherwise they are <code>data</code>.</p>
<p>Also like with product types, the constructor has the same name as the type.</p>
<p>The field names, similar to sum constructor names, are prepended with the
lowercased named of the type with a single quote (<code>'</code>) to maintain uniqueness.</p>
lowercased name of the type with a single quote (<code>'</code>) to maintain uniqueness.</p>

</main>

Expand Down
Loading

0 comments on commit 76e38c5

Please sign in to comment.