functional programing at work - haskell and domain specific

Download Report

Transcript functional programing at work - haskell and domain specific

600.429
FUNCTIONAL PROGRAMING AT WORK HASKELL AND DOMAIN SPECIFIC LANGUAGES
Dr. John Peterson
Western State Colorado University
Homework 4
It’s up – we’ll discuss it soon! I even added it to
Blackboard :-)
Don’t delay in installing Euterpea – there seem to
be Mac issues. You can work on the assignment
even if it’s not installed.
There will be a turn-in later this week to kick off the
DSL implementation project. Come talk to me
about projects. Talk to other students about
teaming up.
Domain Specific Languages
What is a DSL?
How do we build them?
How is a DSL different from an API?
Why is Haskell a good vehicle for DSLs?
DSL Design Space
• Front end: how do we express programs in
this language?
• Custom: write a parser / preprocessor
• Embedded: add vocabulary to an existing
language. Use language extension constructs
(special syntax, overloading, monads)
DSL Design Space
How do we express the semantics of the
language?
• Embedded: within the host language, often
augmented by libraries
• Translated: generate code in a different
language (usually source code)
DSL Correctness
• No correctness issues beyond those in the
embedded part of the language. The type
vocabulary is embedded in standard Haskell
• Custom type checking – the type system of the
DSL requires a user-written type checker
• Extended types – the type system requires
extensions to the Haskell type system
Haskell Evaluation and the DSL
How does the lambda in Haskell relate to the DSL?
• It doesn’t – the language has a custom syntax and
there is no Lambda in the language
• Integrated – the Haskell lambda calculus is part of
the evaluation mechanism in the language
• Expanded – the language doesn’t have lambda in
its semantics; the lambdas in the input are
expanded away (lambda as “cpp”)
DSL Semantics
How are semantic properties expressed or used?
• Embedded in Haskell semantics
• As rewrite rules – domain specific rewrites
augment ordinary equational reasoning
• Custom – program semantics are not related
to Haskell at all
• Custom Functional – as above, except that the
underlying lambda calculus reasoning is valid
DSL Design Space
Program Transformation
• None – the program is run “as written”
• Lambda calculus – transformations are
possible in the “lambda” part of the language
• Tree rewrite – domain-specific rules are
applied to the program in an AST (Abstract
Syntax Tree) for optimization or code
generation
Euterpea – A Music DSL
See the reading for
documentation. Note that this
teaches Haskell as well as the
music language. This language
has two components: a note level
and a sound level. We’ll work at
the note level for now.
Overview
• Syntax: embedded in standard Haskell
• Code generation: programs are compiled to Midi files
• Semantics: the book discusses the difference between
the Haskell level of the semantics and the music level
but the implementation does not rely on these
semantics
• Semantics: theses are “custom” - not related to Haskell
• Types: embedded in the Haskell type system
• Lambda: expanded away
• Transformation: programs are transformed to MIDI
semantics during code generation
A Simple DSL Formula
• Start with domain-specific objects (musical notes)
as data declarations
• Add domain-specific combinators (:+: and :=:) and
transformations (transpose, instrument)
• Write utilities which make the language easier to
use (note, rest)
• Add an interpretation function (play) which
creates values in the underlying domain (midi
files) – this makes use of transformations that are
validated by domain-level semantics
Code Time
Off to Haskell!