Conference PaperPDF Available

DSL implementation in MetaOCaml, template Haskell, and C++

Authors:

Abstract

A wide range of domain-specific languages (DSLs) has been implemented successfully by embedding them in general purpose lan- guages. This paper reviews embedding, and summarizes how two alter- native techniques—staged interpreters and templates—can be used to overcome the limitations of embedding. Both techniques involve a form of generative programming. The paper reviews and compares three pro- gramming languages that have special support for generative program- ming. Two of these languages (MetaOCaml and Template Haskell) are research languages, while the third (C++) is already in wide industrial use. The paper identifies several dimensions that can serve as a basis for comparing generative languages.
A preview of the PDF is not available
... Programming languages with powerful compile-time metaprogramming support provide a direct solution to DSL embedding [Czarnecki et al. 2004]. MetaML [Sheard 1999], MetaOCaml [Calcagno et al. 2003;Kiselyov 2014], and Template Haskell [Sheard and Jones 2002] extend existing programming languages with macro systems that enable DSL code generation at compile time. ...
Preprint
Full-text available
Fluent API is an object-oriented pattern for smart and elegant embedded DSLs. As fluent API designs typically rely on function overloading, they are hard to realize in functional programming languages. We show how to write functional fluent APIs using parametric polymorphism and unification instead of overloading. Our designs support all regular and deterministic context-free DSLs and beyond.
... Among the key tools, DSLs provide a compact representation that raises the level of abstraction for specific problems and hence enables the manipulation of programs [129]- [132]. C++, Haskell, MetaOCaml, and Scala are often used as host languages to embed DSLs [135]. The SEJITS [136] specializer specializes kernels to low-level implementations. ...
Article
Full-text available
In this paper, we address the question of how to automatically map computational kernels to highly efficient code for a wide range of computing platforms and establish the correctness of the synthesized code. More specifically, we focus on two fundamental problems that software developers are faced with: performance portability across the ever-changing landscape of parallel platforms and correctness guarantees for sophisticated floating-point code. The problem is approached as follows: We develop a formal framework to capture computational algorithms, computing platforms, and program transformations of interest, using a unifying mathematical formalism we call operator language (OL). Then we cast the problem of synthesizing highly optimized computational kernels for a given machine as a strongly constrained optimization problem that is solved by search and a multistage rewriting system. Since all rewrite steps are semantics preserving, our approach establishes equivalence between the kernel specification and the synthesized program. This approach is implemented in the SPIRAL system, and we demonstrate it with a selection of computational kernels from the signal and image processing domain, software-defined radio, and robotic vehicle control. Our target platforms range from mobile devices, desktops, and server multicore processors to large-scale high-performance and supercomputing systems, and we demonstrate performance comparable to expertly hand-tuned code across kernels and platforms.
Article
Fluent API is an object-oriented pattern for elegant APIs and embedded DSLs. A smart fluent API can enforce the API protocol or DSL syntax at compile time. Since fluent API implementations typically rely on overloading function names, they are hard to realize in functional programming languages. This work shows how functional fluent APIs can be implemented in the absence of name overloading, by relying on parametric polymorphism and Hindley-Milner type inference. The implementation supports fluent API protocols in the regular- and deterministic context-free language classes, and even beyond.
Chapter
Online judges are Web-based platforms where people can solve programming challenges and have their solutions automatically evaluated, in real time. They can be used for teaching, self study, or recruitment purposes. Online judges are a great resource for students in particular, as a means of practicing for algorithmic competitions, exams, and interviews. Numerous computer science departments from institutions around the world try to integrate online judges into their teaching systems, as a solution for automatic assessment. Some have even developed custom judges, and published papers which elaborate the implementation details and review the impact on students’ performance. Unfortunately, none of them clarifies the methods used to achieve language-agnostic judges. The aim of this paper is to fill in this gap, by surveying different approaches of designing a judge which supports multiple programming languages.
Chapter
Internal DSLs are a special kind of DSLs that use an existing programming language as their host. To build them successfully, knowledge regarding how to modify the host language is essential. In this chapter, the author contributes six DSL design principles and 21 DSL design patterns. DSL Design principles provide guidelines that identify specific design goals to shape the syntax and semantic of a DSL. DSL design patterns express proven knowledge about recurring DSL design challenges, their solution, and their connection to each other – forming a rich vocabulary that developers can use to explain a DSL design and share their knowledge. The chapter presents design patterns grouped into foundation patterns (which provide the skeleton of the DSL consisting of objects and methods), notation patterns (which address syntactic variations of host language expressions), and abstraction patterns (which provide the domain-specific abstractions as extensions or even modifications of the host language semantics).
Chapter
With the emergence of new technologies and their use in different areas, new experiences emerge. In the context of the use of IoT in educational contexts, the potential of hypersituation has been considered by several authors as the greatest potential of these technologies for this field of study. However, despite several references to this fact, this term still lacks further conceptualization and the drawing of guidelines to achieve it. Thus, this paper aims to present an interpretation and definition of the term hypersituation indicating potentials, challenges, and ways to achieve it.
Article
Program generation is indispensable. We propose a novel unification of two existing metaprogramming techniques: multi-stage programming and hygienic generative macros. The former supports runtime code generation and execution in a type-safe manner while the latter offers compile-time code generation. In this work we draw upon a long line of research on metaprogramming, starting with Lisp, MetaML and MetaOCaml. We provide direct support for quotes, splices and top-level splices, all regulated uniformly by a level-counting Phase Consistency Principle. Our design enables the construction and combination of code values for both expressions and types. Moreover, code generation can happen either at runtime à la MetaML or at compile time, in a macro fashion, à la MacroML. We provide an implementation of our design in Scala and we present two case studies. The first implements the Hidden Markov Model, Shonan Challenge for HPC. The second implements the staged streaming library Strymonas.
Article
The λ-calculus is considered a useful mathematical tool in the study of programming languages, since programs can be identified with λ-terms. However, if one goes further and uses βη-conversion to prove equivalence of programs, then a gross simplification is introduced (programs are identified with total functions from values to values) that may jeopardise the applicability of theoretical results. In this paper we introduce calculi, based on a categorical semantics for computations, that provide a correct basis for proving equivalence of programs for a wide range of notions of computation.
Book
"This book teaches functional programming using Haskell, the most popular purely functional language. The emphasis is on functional programming as a way of thinking and problem solving, using Haskell as a vehicle for expressing solutions. Rather than using conventional examples from mathematics, which are commonly found in other programming language books, this tutorial uses examples drawn from multimedia applications, including graphics, animation, and computer music, thus rewarding the reader with working programs for inherently more interesting applications. The author also teaches how to reason about functional programs, using a very simple process of calculation."--Jacket.
Article
Lava is a tool to assist circuit designers in specifying, designing, verifying and implementing hardware. It is a collection of Haskell modules. The system design exploits functional programming language features, such as monads and type classes, to provide multiple interpretations of circuit descriptions. These interpretations implement standard circuit analyses such as simulation, formal verification and the generation of code for the production of real circuits.Lava also uses polymorphism and higher order functions to provide more abstract and general descriptions than are possible in traditional hardware description languages. Two Fast Fourier Transform circuit examples illustrate this.