Skip to content

Commit 19b7a84

Browse files
committed
First commit
0 parents  commit 19b7a84

8 files changed

+334
-0
lines changed

219px-Ericsson_logo.png

4.74 KB
Loading

Chalmers_logo.png

21.3 KB
Loading

DslTutorial.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Combining Deep and Shallow embeddings for EDSLs
2+
===============================================
3+
4+
Embedded domain specific languages are typically categorized as either
5+
deep or shallow embeddings. In deep embeddings the object language is
6+
implemented by constructing a syntax tree in the host language. This
7+
syntax tree can then be manipulated and evaluated in many different
8+
ways. Shallow embeddings give a direct interpretation of the object
9+
language in the host language which can can give rise to succinct and
10+
fast implementations.
11+
12+
In this tutorial we will advocate a combined approach, using a deeply
13+
embedded core of the object language and model additional language
14+
features using shallow embeddings. This combination gives considerable
15+
flexibility in experimenting with language features. It also enables a
16+
powerful form of partial evaluation for the object language
17+
18+
This tutorial will include several examples of the synergies that come
19+
out of this combined approach. Some of these examples will be taken
20+
from our experience with implementing Feldspar, a language for digital
21+
signal processing designed together with industrial partners.

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tutorialSlides.html: tutorialSlides.md
2+
pandoc tutorialSlides.md -t slidy -s -o tutorialSlides.html
3+
4+
tutorial.pdf: tutorial.tex
5+
pdflatex tutorial.tex
6+
7+
tutorial.tex: tutorial.md Makefile
8+
pandoc tutorial.md -t latex -o tutorial.tex -s --bibliography tutorial.bib

Tutorial.hs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{-# LANGUAGE GADTs #-}
2+
module Tutorial where
3+
4+
data FunC a where
5+
LitI :: Int -> FunC Int
6+
LitB :: Bool -> FunC Bool
7+
Arr :: FunC Int -> (FunC Int -> a) -> FunC (Array Int a)
8+
While :: (s -> FunC Bool) -> (s -> s) -> s -> FunC s
9+
If :: FunC Bool -> FunC a -> FunC a -> FunC a
10+
Pair :: FunC a -> FunC b -> FunC (a,b)
11+
Prim :: String -> a -> FunC a
12+
13+
class Syntactic a where
14+
type Internal a
15+
toFunC :: a -> FunC (Internal a)
16+
fromFunC :: FunC (Internal a) -> a
17+
18+
-- Shallow Embeddings
19+
20+
-- Complex Numbers
21+
22+
data Complex = Complex (FunC Float) (FunC Float)
23+
24+
instance Syntactic Complex where
25+
type Internal (Complex) = (Float,Float)
26+
fromFunC (Complex i r) = Pair (i,r)
27+
toFunC p = Complex (fst p) (snd p)
28+
29+
-- Vectors
30+
31+
data Vector a where
32+
Indexed :: FunC Int -> (FunC Int -> a) -> Vector a
33+
34+
instance Syntactic (Vector a) where
35+
type Internal (Vector a) = Array Int a
36+
fromFunC (Vector l ix) = Arr l ix
37+
toFunC arr = Vector (length arr) (\ix -> arr ! ix)
38+
39+
-- Interesting variation: statically sized vectors
40+
41+
data SVector a where
42+
SVector :: Int -> (FunC Int -> a) -> SVector a
43+
44+
-- Streams
45+
46+
data Stream a where
47+
Stream :: (s -> (a,s)) -> s -> Stream a
48+
49+
-- Finite stream (sequential vectors)
50+
51+
data FStream a where
52+
FStream :: FunC Int -> (s -> (a,s)) -> s -> FStream a
53+
54+
data SFStream a where
55+
SFStream :: Int -> (s -> (a,s)) -> s -> SFStream a

cimer.png

18.2 KB
Loading

tutorial.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
% Combining Deep and Shallow Embeddings
2+
% Josef Svenningsson
3+
%
4+
5+
6+
# Introduction
7+
8+
The advantages of embedding domain specific languages are well known.
9+
It helps the language implementor(s) focus on language design by
10+
leveraging the parser and (when applicable) the type checker of the
11+
host language.
12+
13+
When embedding a language there are several techniques to choose from
14+
which are typically categorized as *shallow* and *deep*
15+
embeddings. These techniques have their individual merits and are
16+
often considered separate.
17+
18+
In this tutorial we will show that much can be gained from combining
19+
deep and shallow embeddings. More concretely we will show the benefits
20+
of starting with a deep embedding and adding language constructs which
21+
are implemented using shallow embeddings on top of the deep embedding.
22+
23+
## Terminology
24+
25+
Before getting to the meat of this tutorial we will establish some
26+
terminology.
27+
28+
We will use *object language* to refer to the DSL which is being
29+
embedded. In this tutorial we will see two examples of object
30+
languages. The term *host language* will be used to refer to the
31+
language which the object language is embedded in. We will use Haskell
32+
as our host language throughout this tutorial.
33+
34+
# Contrast Deep and Shallow Embeddings
35+
36+
Before delving into the subject of combining deep and shallow
37+
embeddings let be precise about what those terms mean and the way that
38+
we will use them in tutorial.
39+
40+
Running example contrasting deep and shallow
41+
42+
Simple arithmetic language
43+
44+
~~~
45+
(+) :: SInt -> SInt -> SInt
46+
(*) :: SInt -> SInt -> SInt
47+
5 :: SInt
48+
~~~
49+
50+
# Example Embedding
51+
52+
# Deep Embedding
53+
54+
# Shallow Embedding
55+
56+
# Main example
57+
58+
# Functional C -- Deep Embedding
59+
60+
# Various Shallow Embeddings
61+
62+
# Vectors
63+
64+
# Sequential Vectors
65+
66+
# Streams
67+
68+
# Pairs
69+
70+
# Complex Numbers
71+
72+
A good example of where we experimented with a feature using the shallow
73+
embedding but then included it in the deep embedding.
74+
75+
76+
A good example of where we experimented with a feature using the shallow
77+
embedding but then included it in the deep embedding.
78+
79+
80+
# Fixed Point Numbers
81+
82+
# Type Class for seamless mixing
83+
84+
A type class which summarizes all the language features -- both shallowly
85+
and deeply embedded
86+
87+
# Partial Evaluation
88+
89+
The host language is effectively the meta language of the object language.
90+
91+
# Other hybrid approaches
92+
93+
There are other approaches to combining deep and shallow embeddings, not to be
94+
confused with the one presented in this tutorial.
95+
96+
In the implementation of Kansas Lava they store shallow values in the
97+
syntax tree for easy access [@Farmer:10:WhatsTheMatter].
98+
99+
In the Feldspar project we do something similar but
100+
101+
## Finally Tagless
102+
103+
The Finally Tagless approach is an alternative method to embed
104+
languages. It has a lot of the same advantages as the approach we have
105+
presented in this tutorial and can in some cases even be considered
106+
superiour.
107+
108+
109+
Why haven't I said anything about the Finally Tagless approach. The
110+
Finally Tagless approach solves some of the same problems that I've
111+
talked about in this tutorial.
112+
113+
* Problems with weird types involving type variables and type constraints
114+
* Not nice for end users who are not themselves functional programmers
115+
* It doesn't mix well with observable sharing
116+
117+
# References

tutorialSlides.md

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
% Combining Deep and Shallow Embeddings: A Tutorial
2+
% Josef Svenningsson
3+
%
4+
5+
# Combining Deep and Shallow Embeddings
6+
7+
# Background
8+
9+
The ideas I'm going to present in this tutorial have been used
10+
extensively in the development of Feldspar.
11+
12+
Feldspar is a domain specific language
13+
14+
Feldspar is a collaboration between:
15+
16+
---- ----
17+
Chalmers University of Technology ![Chalmers logo]
18+
ELTE University ![ELTE logo]
19+
Ericsson ![Ericsson logo]
20+
---- ----
21+
22+
[Chalmers logo]: Chalmers_logo.png
23+
[ELTE logo]: cimer.png
24+
[Ericsson logo]: 219px-Ericsson_logo.png
25+
26+
# Contrast Deep and Shallow Embeddings
27+
28+
Running example contrasting deep and shallow
29+
30+
Simple arithmetic language
31+
32+
~~~
33+
(+) :: SInt -> SInt -> SInt
34+
(*) :: SInt -> SInt -> SInt
35+
5 :: SInt
36+
~~~
37+
38+
# Example Embedding
39+
40+
# Deep Embedding
41+
42+
# Shallow Embedding
43+
44+
# Hybrid approaches
45+
46+
In Kansas Lava they use a hybrid approach where the deep embedding contains
47+
a shallow embedding.
48+
49+
~~~
50+
data Signal a = Signal (Stream a) (D a)
51+
52+
data Stream a = a :~ Stream a
53+
54+
type D a = AST
55+
~~~
56+
57+
# Conclusion: Deep and Shallow embeddings
58+
59+
Deep and Shallow embeddings are two important ways to embed a target language
60+
in a host language
61+
62+
These are certainly not the only ways to embed a language, but they will be
63+
the focus in this tutorial.
64+
65+
# Main example
66+
67+
# Functional C -- Deep Embedding
68+
69+
Our example language for demonstrating our main point is a small
70+
functional flavored C.
71+
72+
It is inspired by the core language used in Feldspar, which has served us well
73+
for writing embedded software.
74+
75+
76+
# Various Shallow Embeddings
77+
78+
# Vectors
79+
80+
# Sequential Vectors
81+
82+
# Streams
83+
84+
# Pairs
85+
86+
# Complex Numbers
87+
88+
A good example of where we experimented with a feature using the shallow
89+
embedding but then included it in the deep embedding.
90+
91+
92+
A good example of where we experimented with a feature using the shallow
93+
embedding but then included it in the deep embedding.
94+
95+
96+
# Fixed Point Numbers
97+
98+
# Type Class for seamless mixing
99+
100+
A type class which summarizes all the language features -- both shallowly
101+
and deeply embedded
102+
103+
# Partial Evaluation
104+
105+
The host language is effectively the meta language of the object language.
106+
107+
Haskell values are static.
108+
109+
DSL values are dynamic.
110+
111+
Should I show the Binding Time module?
112+
113+
# Analysis and optimization of shallow embeddings
114+
115+
We can optimize vectors even further if we know something about their shape.
116+
117+
Show example with `(++)`.
118+
119+
120+
[Final:ish slide]
121+
# Finally Tagless
122+
123+
Why haven't I said anything about the Finally Tagless approach. The
124+
Finally Tagless approach solves some of the same problems that I've
125+
talked about in this tutorial.
126+
127+
* Problems with weird types involving type variables and type constraints
128+
* Not nice for end users who are not themselves functional programmers
129+
* It doesn't mix well with observable sharing
130+
131+
# Thanks
132+
133+
Thanks!

0 commit comments

Comments
 (0)