SLIDE 1
From the Tagless-Final Cookbook
Embedding and Optimizing (Hardware) Domain-Specific Languages in the Typed Final Style http://okmij.org/ftp/tagless-final/course/index.html Metaprogramming Summer School 2019 Dagstuhl, August 13-15, 2019
SLIDE 2 2
Tagless-Final
A general method of embedding (often typed) domain-specific languages (DSL) in a typed functional language such as Haskell, OCaml, Scala and Coq
◮ An alternative to the (‘deep’) embedding as a (G)ADT ◮ Centered around interpreters ◮ Extensible ◮ Executable denotational semantics
It has been used to implement extensible DSLs in the domains of
language-integrated queries, non-deterministic and probabilistic programming, delimited continuations, computability theory, stream processing, hardware description languages, generation
- f specialized numerical kernels, and natural language
semantics
SLIDE 3 3
What’s new
A variant, but not repetition
- f an earlier course (CUFP 2015, Metaprogramming Summer
School 2016)
The same DSL of combinational circuits
but developed at a faster pace and to a larger extent
Semantic rather than syntactic
Focus on denotations rather than term rewriting
SLIDE 4
4
Goals
◮ Introduce the Tagless-final style on a familiar example: combinational circuits ◮ Show off the features of the approach and design choice ◮ Introduce various optimizations Optimizing EDSL in the typed final style is not only possible: it is modular and systematic You can do it!
SLIDE 5
5
Which language?
OCaml, Haskell, Scala, ..., Coq, ...
SLIDE 6
6
Overview
Interactivity
◮ Please do ask questions ◮ I will ask questions ◮ Interactive writing of code (me vs. OCaml) ◮ Several exercises to do in class (and homework) ◮ Work alone or in group ◮ Installed OCaml? http://try.ocamlpro.com/
SLIDE 7
7
Problems
◮ A DSL for basic logical circuits (AND/OR/NOT) ◮ Various interpreters ◮ Compiler to NAND circuit ◮ Simplification and other transformers ◮ Conversion to CNF in one easy step ◮ From gates to circuits ◮ Circuit optimization, composionally ◮ Adding Gates (higher-order), reusing previous transformation rules Real-life application: efficient language-integrated query The web page of the approach (Tutorials, applications, etc.) http://okmij.org/ftp/tagless-final/course/index.html Similar, in spirit tutorial http://okmij.org/ftp/ tagless-final/course/optimizations.html
SLIDE 8
8
Main ideas
◮ Multiple interpretations: write once, interpret many times ◮ Extensibility ◮ Types
◮ typed implementation language ◮ typed object language ◮ typed optimization rules ◮ connections with logic
◮ ‘Final’
◮ everything is in lower-case ◮ prefer elimination over introduction ◮ connections to denotational semantics
SLIDE 9
8
Main ideas
◮ Multiple interpretations: write once, interpret many times ◮ Extensibility ◮ Types
◮ typed implementation language ◮ typed object language ◮ typed optimization rules ◮ connections with logic
◮ Denotational
◮ seek meaning ◮ algebras ◮ evaluation rather than rewriting
SLIDE 10
9
Compositionality
The meaning of a complex expression is determined by its structure and the meanings of its constituents.
http://plato.stanford.edu/entries/compositionality/ eval (Add e1 e2) = eval e1 + eval e2 ◮ Evaluators and other interpreters are compositional ◮ Denotational semantics must be compositional ◮ Compositionality is modularity ◮ Compositionality is context-insensitivity ◮ Bottom-up reconstruction of meaning ◮ Compositional processing is fold
SLIDE 11
10
More problems, homework
◮ The problem noted in the source code files (search for QUIZ) ◮ Selective inlining (combine only those circuits when doing so will not lead to duplication) ◮ Sharing ◮ AND X X X ◮ Implement various simplifications
SLIDE 12
11
Why Tagless Final style?
Thinking about meaning helps ◮ Algebraic perspective: focus on operations, on what we need to do (and how to add more: extensibility) ◮ Denotational perspective: focus on what we eventually want: eye on the prize rather than on word shuffling http://okmij.org/ftp/tagless-final/ http://okmij.org/ftp/tagless-final/cookbook.html http://okmij.org/ftp/tagless-final/Algebra.html http://okmij.org/ftp/tagless-final/semantics.html