Domain-Specific Embedded-Language Design

280
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
280
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Domain-Specific Embedded-Language Design

  1. 1. Little Languages for Big Applications Paul Hudak Department of Computer Science Yale University Copyright © 2003, Paul Hudak, All rights reserved. Keiburtz Bash December 5, 2003
  2. 2. Is “Higher Level” Better? <ul><li>A programming language can be viewed as an interface to an abstract machine . </li></ul><ul><li>When is one general-purpose language higher-level than another? </li></ul><ul><li>Assembly language is just the right abstraction for a CPU. </li></ul><ul><li>Why do some languages better match some applications than others? </li></ul>
  3. 3. We Need Domain Specificity <ul><li>A domain-specific language (or DSL) is a language that precisely captures a domain semantics; no more, and no less. </li></ul><ul><li>We also need domain specific: </li></ul><ul><ul><li>specifications (starting point!) </li></ul></ul><ul><ul><li>optimizations and transformations </li></ul></ul><ul><ul><li>software tools </li></ul></ul><ul><ul><li>type systems, aspects, constraints, etc. </li></ul></ul>
  4. 4. <ul><li>Programs in the target domain are: </li></ul><ul><ul><li>more concise </li></ul></ul><ul><ul><li>quicker to write </li></ul></ul><ul><ul><li>easier to maintain </li></ul></ul><ul><ul><li>easier to reason about </li></ul></ul><ul><ul><li>can often be written by non-programmers </li></ul></ul>Advantages of DSL Approach Contribute to higher programmer productivity Dominant cost in large SW systems Verification, transform- ation, optimization Helps bridge gap between developer and user These are the same arguments in favor of any high-level language. But in addition:
  5. 5. The Bottom Line
  6. 6. DSL’s Allow Faster Prototyping specify specify test design build test design build With DSL Without DSL Using the “Spiral Model” of Software Development
  7. 7. Why Study DSL’s? <ul><li>Ok, so perhaps DSL’s are useful. </li></ul><ul><li>But why should programming language researchers be interested in DSL’s? </li></ul><ul><ul><li>To have an impact on the real world. </li></ul></ul><ul><ul><li>The chances of a general purpose language succeeding are slim, no matter how good it is. </li></ul></ul><ul><ul><li>DSL design and implementation is a source of new and interesting problems. </li></ul></ul><ul><ul><li>It is also fun! </li></ul></ul><ul><li>In the remainder of the talk I will concentrate on the latter two points. </li></ul>
  8. 8. DSL’s Embedded in Haskell <ul><li>Graphics/Animation (Fran, w/Microsoft) </li></ul><ul><li>Robotics (Frob) </li></ul><ul><li>Graphical user interfaces (Fruit) </li></ul><ul><li>Computer Vision (Fvision) </li></ul><ul><li>Computer Music (Haskore) </li></ul><ul><li>Sound Synthesis (Hsound) </li></ul><ul><li>Dance/choreography (Haskanotation) </li></ul><ul><li>Educations (Pan, Pan#, w/Microsoft) </li></ul><ul><li>Scripting languages for the WWW </li></ul><ul><li>Scripting COM objects </li></ul><ul><li>Hardware description / VLSI layout </li></ul><ul><li>Parsing/pretty printing </li></ul><ul><li>GUI’s </li></ul><ul><li>Financial contracts </li></ul><ul><li>Theater lighting </li></ul>At Yale: Elsewhere:
  9. 9. A Case Study: FRP Functional Programming FRP / Yampa Functions, types, etc. (Haskell) Continuous behaviors and discrete reactivity Specialized languages Fran FVision Graphics, Robotics, GUIs, Vision Applications Fruit Frob
  10. 10. Behaviors <ul><li>Continuous behaviors capture any time-varying quantity, whether: </li></ul><ul><ul><li>input (sonar, temperature, video, etc.), </li></ul></ul><ul><ul><li>output (actuator voltage, velocity vector, etc.), or </li></ul></ul><ul><ul><li>intermediate values internal to a program. </li></ul></ul><ul><li>Operations on behaviors include: </li></ul><ul><ul><li>Generic operations such as arithmetic, integration, differentiation, and time-transformation. </li></ul></ul><ul><ul><li>Domain-specific operations such as edge-detection and filtering for vision, scaling and rotation for animation and graphics, etc. </li></ul></ul>
  11. 11. Events <ul><li>Discrete event streams include user input as well as domain-specific sensors, asynchronous messages, interrupts, etc. </li></ul><ul><li>They also include tests for dynamic constraints on behaviors (temperature too high, level too low, etc.) </li></ul><ul><li>Operations on event streams include: </li></ul><ul><ul><li>Mapping, filtering, reduction, etc. </li></ul></ul><ul><ul><li>Reactive behavior modification (next slide). </li></ul></ul>
  12. 12. An Example from Graphics (Fran) A single animation example that demonstrates key aspects of FRP: growFlower = stretch size flower where size = 1 + integral bSign bSign = 0 `until` (lbp ==> -1 `until` lbr ==> bSign) .|. (rbp ==> 1 `until` rbr ==> bSign) Link to animation
  13. 13. Fran Also Supports 3D spiralTurn = turn3 zVector3 (pi*time) (unionGs (map ball [1 .. n])) where n = 40 ball i = withColorG color (move3 motion (stretch3 0.1 sphereLowRes )) where motion = vector3Spherical 1.5 (10*phi) phi phi = pi * fromInt i / fromInt n color = colorHSL (2*phi) 0.5 0.5 Link to animation
  14. 14. The Semantics of Fran <ul><li>Denotational semantics [Elliott,Hudak,ICFP98] </li></ul><ul><ul><li>at [[b]] t : instantaneous value of behavior b at time t. </li></ul></ul><ul><ul><li>occ [[e]] t : presence of event e at time t. </li></ul></ul><ul><ul><li>Domain (cpo) of time T , with partial elements > t that denote “a time that is at least t”. </li></ul></ul><ul><li>Stream-based operational semantics [SOE (Hudak2000) and Wan,Hudak,PLDI2000] </li></ul><ul><ul><li>Streams represent behaviors and events. </li></ul></ul><ul><ul><li>Compositional semantics via stream transformers. </li></ul></ul><ul><ul><li>Leads naturally to concrete implementation. </li></ul></ul>Theorem : In the limit, as sample time goes to zero, the stream-based semantics is faithful to the denotational semantics [PLDI2000].
  15. 15. Lambda in Motion Controlling Robots with Haskell
  16. 16. Motivation <ul><li>Mobile robot control is hard! </li></ul><ul><li>Prototyping is essential: repeated experimentation required. </li></ul><ul><li>Must deal with uncertainty: imprecise sensors, unknown environment, moving obstacles, mechanical problems. </li></ul><ul><li>Need to compose solved sub-problems. </li></ul><ul><li>Reliability needed – programs must recover from errors and deploy alternative strategies to meet goals. </li></ul>
  17. 17. Our Solution: Frob <ul><li>Frob = FRP + robot controller + robot/vision library </li></ul><ul><li>Programming robots is a lot like programming an animation! </li></ul><ul><li>… except that: </li></ul><ul><ul><li>The robot doesn’t always do what you want it to do. </li></ul></ul><ul><ul><li>Error / anomalous conditions are more common. </li></ul></ul><ul><ul><li>Real-time issues are more dominant. </li></ul></ul><ul><ul><li>Robots are a lot slower than graphics hardware. </li></ul></ul><ul><ul><li>Feedback/control is essential. </li></ul></ul>
  18. 18. Differential Drive Mobile Robot θ l v l v r y x Our software includes a robot simulator . Its robots are differential drive, and we refer to them as simbots .
  19. 19. An Example from Robotics <ul><li>The equations governing the x position of a differential drive robot are: </li></ul><ul><li>The corresponding FRP code is: x = (1/2) * (integral ((vr + vl) * cos theta) theta = (1/l) * (integral (vr - vl)) (Note the lack of explicit time.) </li></ul>
  20. 20. Nico: A One-year Old Android <ul><li>Yale’s Humonoid Robotics Lab </li></ul><ul><li>(Brian Scassellati, Ganghua Sun) </li></ul><ul><li>Nico is a child-size robot for the study of: </li></ul><ul><li>Social interactions </li></ul><ul><li>A theory of mind (AI) </li></ul><ul><li>Clinical studies of autistic children </li></ul>
  21. 21. Dance <ul><li>A language for controlling humanoid robots . </li></ul><ul><li>Borrows ideas from FRP and Labanotation , a notation for human dance. </li></ul><ul><li>Motions at abstract level: </li></ul><ul><ul><li>Each body part controlled separately. </li></ul></ul><ul><ul><li>Positions “forward”, “back”, “left”, “right”, etc. </li></ul></ul><ul><ul><li>Time steps like those in musical score. </li></ul></ul>
  22. 22. Haskore <ul><li>Motivation: </li></ul><ul><li>Traditional music notation has many limitations: </li></ul><ul><li>Unable to express a composer’s intentions . </li></ul><ul><li>Biased toward music that is humanly performable . </li></ul><ul><li>Unable to express notions of algorithmic composition . Haskore (“Haskell” + “Score”) is a DSL for computer music composition. </li></ul>
  23. 23. Basic Haskore Design type Pitch = (NoteName, Octave) data NoteName = Cf | C | Cs | Df | D | Ds | Ef | E | Es | Ff | F | Fs | Gf | G | Gs | Af | A | As | Bf | B | Bs type Octave = Int data Music = Note Pitch Dur -- a note atomic | Rest Dur -- a rest / objects | Music :+: Music -- sequential composition | Music :=: Music -- parallel composition | Tempo Int Int Music -- scale the tempo | Trans Int Music -- transposition | Instr IName Music -- instrument label Type Dur = Float
  24. 24. Example For convenience: c oct dur = Note (C, oct) dur … qn = 0.25 … Then a C-major scale can be define as: cmaj = c 3 qn :+: d 3 qn :+: e 3 qn :+: c 4 qn :+: … Sound file
  25. 25. Simple Transformations Add a parallel line 7 notes (semitones) higher: pfifth m = m :=: Trans 7 m cmaj5 = pfifth cmaj Repeat a phrase, but faster: againFaster m = m :+: (Tempo 2 m) cmaj5twice = againFaster cmaj5 cmaj5four = againFaster cmaj5twice Sound file Sound file Sound file
  26. 26. More Examples A function to recursively apply transformations f (to elements in a sequence) and g (to accumulated phrases): rep :: (Music -> Music) -> (Music -> Music) -> Integer -> Music -> Music rep f g 0 m = Rest 0 rep f g n m = m :=: g (rep f g (n-1) (f m)) An example using &quot;rep&quot; three times, recursively, to create a &quot;cascade&quot; of sounds. run = rep (Trans 5) (delay tn) 8 (c 4 tn) cascade = rep (Trans 4) (delay en) 8 run cascades = rep id (delay sn) 2 cascade waterfall = cascades :+: revM cascades Sound file Sound file Sound file Sound file
  27. 27. Common Operations <ul><li>Previous work on: </li></ul><ul><ul><li>Haskore : a library for computer music composition. </li></ul></ul><ul><ul><li>Fran : a language for functional reactive animation. </li></ul></ul><ul><ul><li>Dance : a language for humanoid robots. </li></ul></ul><ul><li>has revealed striking similarities at the highest level. </li></ul><ul><li>In particular, notions of: </li></ul><ul><ul><li>Sequential composition </li></ul></ul><ul><ul><li>Parallel composition </li></ul></ul><ul><ul><li>Temporal properties (duration, etc.) </li></ul></ul><ul><ul><li>Point-wise operations (scaling, transposing, etc.) </li></ul></ul><ul><li>Questions: </li></ul><ul><ul><li>Can these notions be captured in a single unified framework ? </li></ul></ul><ul><ul><li>How do we give meaning to these structures? </li></ul></ul><ul><ul><li>How do we manipulate and reason about them? </li></ul></ul>
  28. 28. Polymorphic Temporal Media <ul><li>Captures music, animation, sound files, video clips , … </li></ul><ul><li>Supports: </li></ul><ul><li>Syntactic (structural) operations and properties (map, fold, etc.) </li></ul><ul><li>Temporal operations and properties (duration, take, drop, etc.) </li></ul><ul><li>Semantic operations and properties (sequential and parallel composition) </li></ul><ul><li>In addition, we can define an Axiomatic Semantics that is both sound and complete. </li></ul>
  29. 29. Polymorphic Media <ul><li>Define an algebraic data type: data Media a = Prim a -- base media | Media a :+: Media a -- sequential composition | Media a :=: Media a -- parallel composition We refer to T in Media T as the base media type . </li></ul><ul><li>So: </li></ul><ul><ul><li>Prim x is a media value from the base media type. </li></ul></ul><ul><ul><li>m1 :+: m2 is media value m1 followed in time by m2 . </li></ul></ul><ul><ul><li>m1 :=: m2 is media value m1 occurring simultaneously with m2 . </li></ul></ul>
  30. 30. Example 1: Music <ul><li>For music media , Note is the base media type: type Music = Media Note data Note = Rest Dur | Note Pitch Dur type Dur = Real type Pitch = (NoteName, Octave) type Octave = Int data NoteName = Cf | C | Cs | Df | D | Ds | Ef | E | Es | Ff | F | Fs | Gf | G | Gs | Af | A | As | Bf | B | Bs </li></ul>
  31. 31. Example 2: Animation <ul><li>For animation media , Anim is the base media type: type Animation = Media Anim type Anim = (Dur, Time -> Picture) type Time = Real type Dur = Real data Picture = EmptyPic | Circle Radius Point | Square Length Point | Polygon [Point] Point </li></ul>
  32. 32. Semantics <ul><li>Consider these two expressions: m1 :+: (m2 :+: m3) (m1 :+: m2) :+: m3 </li></ul><ul><li>Intuition tells us that these represent the same media value; i.e. (:+:) should be associative . There are in fact several other examples of this. </li></ul><ul><li>What we need is an interpretation of media values that somehow gives meaning to them. </li></ul><ul><li>And we wish to do this in a polymorphic way. </li></ul>
  33. 33. The Meaning of Media <ul><li>We use type classes to structure meanings: class Combine b where concatM :: b -> b -> b merge :: b -> b -> b zero :: Dur -> b class Combine b => Meaning a b where meaning :: a -> b </li></ul><ul><li>Intuitively, an instance Meaning T1 T2 means that T1 can be given meaning in terms of T2 . instance Meaning a b => Meaning (Media a) b where meaning = foldM meaning concatM merge </li></ul>
  34. 34. Semantic Equivalence <ul><li>Definition: m1, m2 :: Media T are equivalent , written m1 === m2 , if and only if meaning m1 = meaning m2 . </li></ul><ul><li>Example : We take the meaning of music to be a pair: the duration , and a sequence of events , where each event marks the start-time, pitch, and duration of a single note: data Event = Event Time Pitch Dur type Time = Ratio Int type Performance = (Dur, [Event]) </li></ul><ul><li>This corresponds well to low-level representations of music such as Midi and Csound. </li></ul><ul><li>Animation can be handled in an analogous way. </li></ul>
  35. 35. <ul><li>Define A as the axiomatic semantics given by the nine axioms: (1) associativity of (:+:) m1 :+: (m2 :+: m3) === (m1 :+: m2) :+: m3 (2) associative of (:=:) m1 :=: (m2 :=: m3) === (m1 :=: m2) :=: m3 (3) commutativity of (:=:) m1 :=: m2 === m2 :=: m1 (4) left (sequential) zero none 0 :+: m === m (5) right (sequential) zero m :+: none 0 === m (6) left (parallel) zero none d :=: m === m, if d = dur m (7) right (parallel) zero m :=: none d === m, if d = dur m (8) additivity of none none d1 :+: none d2 === none (d1+d2) (9) serial/parallel axiom: (m1 :+: m2) :=: (m3 :+: m4) === (m1 :=: m3) :+: (m2 :=: m4), if dur m1 = dur m3 and dur m2 = dur m4 plus the reflexive, symmetric, and transitive axioms implied by (===) being an equivalence relation, and the substitution axioms implied by (===) being a congruence relation. </li></ul>An Axiomatic Semantics
  36. 36. The Serial/Parallel Axiom <ul><li>Suppose dur m1 = dur m3 and dur m2 = dur m4 . </li></ul><ul><li>Then, intuitively, these two phrases should be equivalent: (m1 :+: m2) :=: (m3 :+: m4) (m1 :=: m3) :+: (m2 :=: m4) </li></ul><ul><li>Or, graphically: </li></ul><ul><li>This is a critical axiom to many proofs. </li></ul>=== m1 m3 m2 m4 m1 m3 m2 m4
  37. 37. Soundness <ul><li>We write “ A |- m1 = m2 ” iff m1 === m2 is provable from the axioms in A . </li></ul><ul><li>Theorem: The axiomatic semantics A is sound . That is, for all m1, m2 :: Media T : A |- m1 = m2  m1 === m2 Proof: By induction on the derivation, and validity of the axioms. </li></ul>
  38. 38. Completeness <ul><li>In what sense are the axioms complete ? That is, if two media values are equivalent, can we always prove it from the axioms? </li></ul><ul><li>The answer is “yes, if…” </li></ul><ul><li>Definition : A well-formed media term m :: Media T is in normal form iff it is of the form: none d, d >=0 --- or --- (none d 11 :+: Prim x 1 :+: none d 12 ) :=: . . . (none d n1 :+: Prim x n :+: none d n2 ), n >= 1, where for all (1 <= i <= n), d i1 + d i2 + dur x i = dur m, and for all (1 <= i < n), (d i1 ,x i ,di2) <= d (i+1)1 , x i+1 , d (i+1)2 </li></ul><ul><li>We denote the set of media normal forms as MediaNF T . </li></ul>
  39. 39. Normalization <ul><li>Lemma : Any m : Media T can be transformed into a media normal-form using only the axioms of A . </li></ul><ul><li>Proof : Define this normalization function : normalize :: (Ord (Media a), Temporal a) => Media a -> Media a normalize m = sortM (norm (dur m) 0 m) norm :: (Ord (Media a), Temporal a) => Dur -> Time -> Media a -> Media a norm d t m | isNone m = m norm d t (Prim x) = none t :+: Prim x :+: none (d-t-dur x) norm d t (m1 :+: m2) = norm d t m1 :=: norm d (t+dur m1) m2 norm d t (m1 :=: m2) = norm d t m1 :=: norm d t m2 and establish it’s validity using only the axioms of A . </li></ul>
  40. 40. Completeness, cont’d <ul><li>Theorem: The axiomatic semantics A is complete , that is, for all m1, m2 :: Media T : m1 === m2  A |- m1 = m2 if and only if the normal forms in MediaNF T are unique . </li></ul>
  41. 41. Example 1 <ul><li>Elements of MusicNF = MediaNF Note are unique . </li></ul><ul><li>To see why, note that each normal form m: (none d 11 :+: Prim x 1 :+: none d 12 ) :=: (none d 21 :+: Prim x 2 :+: none d 22 ) :=: . . . (none d n1 :+: Prim x n :+: none d n2 ) corresponds uniquely to an interpretation: (dur m, [ Event d 11 p 1 (dur x 1 ), Event d 21 p 2 (dur x 2 ), . . . Event d n1 p n (dur x n ) ]) </li></ul>
  42. 42. Example 2 <ul><li>Elements of AnimationNF = MediaNF Anim are not unique . </li></ul><ul><li>This is because there are more equivalences: ball :=: ball === ball takeM d (Prim x) :+: dropM d (Prim x) === Prim x Both of these imply more equivalences than the axioms alone can establish. </li></ul><ul><li>Solution: add “domain-specific” axioms to regain completeness. </li></ul>
  43. 43. How FL Research Can Help <ul><li>Formal semantics . </li></ul><ul><li>Testing and verification . </li></ul><ul><li>Implementation techniques (compilers, interpreters, run-time systems) </li></ul><ul><li>Language design (abstraction mechanisms, types, etc.) </li></ul><ul><li>Functional programming techniques </li></ul>
  44. 44. Glove by Tom Makucevich with help from Paul Hudak Composed using Haskore and rendered using csound. Animated by Paul Hudak in Fran, and John Peterson in Pan.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×