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
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 of specialized numerical kernels, and natural language semantics 2
What’s new A variant, but not repetition of 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 3
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! 4
Which language? OCaml, Haskell, Scala, ..., Coq, ... 5
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/ 6
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 7
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 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 8
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 9
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 10
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 11
Recommend
More recommend