Lecture 2 Domain Specific Embedded Languages

Slides:



Advertisements
Similar presentations
Chapter 14 Graph class design Bjarne Stroustrup
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Chapter 8 A Module of Regions. The Region Data Type  A region represents an area on the two-dimensional Cartesian plane.  It is represented by a tree-like.
Chapter 14 Graph class design John Keyser’s Modifications of Slides by Bjarne Stroustrup
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
CS 599 – Spatial and Temporal Databases Realm based Spatial data types: The Rose Algebra Ralf Hartmut Guting Markus Schneider.
Informationsteknologi Wednesday, November 7, 2007Computer Graphics - Class 51 Today’s class Geometric objects and transformations.
HCI 530 : Seminar (HCI) Damian Schofield. HCI 530: Seminar (HCI) Transforms –Two Dimensional –Three Dimensional The Graphics Pipeline.
Interpreter for ZOOM-I Andrew Deren Initial Presentation - SE690 5/30/03
1/18 CS 693/793 Lecture 09 Special Topics in Domain Specific Languages CS 693/793-1C Spring 2004 Mo, We, Fr 10:10 – 11:00 CH 430.
Outline for Today More math… Finish linear algebra: Matrix composition
Math for CSLecture 11 Mathematical Methods for Computer Science Lecture 1.
Transformations I CS5600Computer Graphics by Rich Riesenfeld 27 February 2002 Lecture Set 5.
OOP Languages: Java vs C++
Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.
COMP 175: Computer Graphics March 24, 2015
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Object Oriented Programming Some Interesting Genes.
Advanced Functional Programming 2010
Computer Graphics 2D Transformations
CSE341: Programming Languages Lecture 22 OOP vs
Eigenfaces (for Face Recognition)
dr Robert Kowalczyk WMiI UŁ
Theory of Computation Lecture 4: Programs and Computable Functions II
Object-Oriented Modeling with UML
The role of abstractions
Variables and Arithmetic Operators in JavaScript
Image Warping (Geometric Transforms)
Introduction to Computer Graphics CS 445 / 645
AFP - Lecture 2 Domain Specific Embedded Languages
Abstract Classes and Inheritence Operator over-loading
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
Object Oriented Practices
Lecture 13 Writing Classes Richard Gesick.
Lesson 2: Building Blocks of Programming
PROGRAMMING IN HASKELL
COMP 175: Computer Graphics February 9, 2016
Object Oriented Programming (OOP) LAB # 8
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Enumerations & Annotations
Enumerations & Annotations
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
More Image Manipulation
Matlab tutorial course
COMP 175: Computer Graphics February 21, 2016
2D transformations (a.k.a. warping)
3D Transformation CS380: Computer Graphics Sung-Eui Yoon (윤성의)
Variables Title slide variables.
Iteration Abstraction
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Enumerations & Annotations
Hierarchical Modeling & Constructive Solid Geometry
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 22 OOP vs
Linear Algebra A gentle introduction
Theory of Computation Lecture 4: Programs and Computable Functions II
you get to solve puzzles!
CSE341: Programming Languages Lecture 22 OOP vs
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CSE341: Programming Languages Lecture 22 OOP vs
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Lecture 2 Domain Specific Embedded Languages Patrik Jansson 2010 (slides by Norell and Bernardy)

Anatomy of a DSEL A set of types modelling concepts in the domain newtype Signal a = Signal (Time -> a) A set of types modelling concepts in the domain Constructor functions constructing elements of these types Combinators combining and modifying elements Run functions making observations of the elements constS :: a -> Signal a timeS :: Signal Time ($$) :: Signal (a -> b) -> Signal a -> Signal b mapS :: (a -> b) -> Signal a -> Signal b sample :: Signal a -> (Time -> a)

Primitive and Derived operations A primitive operation is defined exploiting the definitions of the involved types A derived operation can be defined purely in terms of other operations timeS :: Signal Time timeS = Signal (\t -> t) Try to keep the set of primitive operations as small as possible! (Why?) mapS :: (a -> b) -> Signal a -> Signal b mapS f s = constS f $$ s

Think about… Compositionality Answer: Awkwardly! addS x y = mapS (\t -> sample x t + sample y t) timeS Compositionality Combining elements into more complex ones should be easy and natural Abstraction The user shouldn’t have to know (or be allowed to exploit) the underlying implementation of your types. Suppose we didn’t have ($$) in our Signal language. How would you define addS x y = constS (+) $$ x $$ y Changing implementation shouldn’t break user code!

Implementation of a DSEL Shallow embedding Represent elements by their semantics (what observations they support) Constructor functions and combinators do most of the work, run functions for free Deep embedding Represent elements by how they are constructed Most of the work done by the run functions, constructor functions and combinators for free Or something in between… Is the signal library a deep or shallow embedding?

A deep embedding of Signals Generalized Algebraic Datatype (GADT). More on these in another lecture. data Signal a where ConstS :: a -> Signal a TimeS :: Signal Time (:$$) :: Signal (a -> b) -> Signal a -> Signal b constS = ConstS timeS = TimeS ($$) = (:$$) sample :: Signal a -> (Time -> a) sample (ConstS x) = const x sample TimeS = id sample (f :$$ x) = \t -> sample f t $ sample x t -- Start of derived operations mapS :: (a -> b) -> Signal a -> Signal b mapS f x = constS f $$ x Simple constructors and combinators. All the work happens in the run function. Derived operations are unaffected by implementation style.

Deep vs. Shallow Like in the Signal example A shallow embedding (when it works out) is often more elegant When there is an obvious semantics, shallow embeddings usually work out nicely A deep embedding is easier to extend Adding new operations Adding new run functions Adding optimizations Working out the type might be very difficult... Most of the time you get a mix between deep and shallow! Deep embedding may give you an easier start More on this in another lecture.

Case Study: A language for Shapes Step 1: Design the interface Unit circle and unit square. Use translate and scale to get more interesting circles and rectangles. type Shape -- Constructor functions empty :: Shape circle :: Shape square :: Shape -- Combinators translate :: Vec -> Shape -> Shape scale :: Vec -> Shape -> Shape rotate :: Angle -> Shape -> Shape Union :: Shape -> Shape -> Shape intersect :: Shape -> Shape -> Shape difference :: Shape -> Shape -> Shape -- Run functions inside :: Point -> Shape -> Bool

Interface, continued Think about primitive/derived operations No obvious derived operations Sometimes introducing additional primitives makes the language nicer We need a language for working with matrices! invert :: Shape -> Shape transform :: Matrix -> Shape -> Shape scale :: Vec -> Shape -> Shape scale v = transform (matrix (vecX v) 0 0 (vecY v)) rotate :: Angle -> Shape -> Shape rotate a = transform (matrix (cos a) (-sin a) (sin a) (cos a)) difference :: Shape -> Shape -> Shape difference a b = a `intersect` invert b Do you remember you linear algebra course?

Side track: A matrix library type Matrix type Vector type Point -- Constructor functions point :: Double -> Double -> Point vec :: Double -> Double -> Vec matrix :: Double -> Double -> Double -> Double -> Matrix -- Combinators mulPt :: Matrix -> Point -> Point mulVec :: Matrix -> Vec -> Vec inv :: Matrix -> Matrix subtract :: Point -> Vec -> Point -- Run functions ptX, ptY :: Point -> Double vecX, vecY :: Vec -> Double This should do for our purposes.

Shallow embedding What are the observations we can make of a shape? inside :: Point -> Shape -> Bool So, let’s go for newtype Shape = Shape (Point -> Bool) inside :: Point -> Shape -> Bool inside p (Shape f) = f p In general, it’s not this easy. In most cases you need to generalize the type of the run function a little to get a compositional shallow embedding.

Shallow embedding, cont. If we picked the right implementation the operations should now be easy to implement Trick: move the point instead of the shape empty = Shape $ \p -> False circle = Shape $ \p -> ptX p ^ 2 + ptY p ^ 2 <= 1 square = Shape $ \p -> abs (ptX p) <= 1 && abs (ptY p) <= 1 transform m a = Shape $ \p -> mulPt (inv m) p `inside` a translate v a = Shape $ \p -> subtract p v `inside` a union a b = Shape $ \p -> inside p a || inside p b intersect a b = Shape $ \p -> inside p a && inside p b invert a = Shape $ \p -> not (inside p a)

Deep embedding Representation is easy, just make a datatype of the primitive operations data Shape where -- Constructor functions Empty :: Shape Circle :: Shape Square :: Shape -- Combinators Translate :: Vec → Shape -> Shape Transform :: Matrix -> Shape -> Shape Union :: Shape -> Shape -> Shape Intersect :: Shape -> Shape -> Shape Invert :: Shape -> Shape empty = Empty; circle = Circle; ...

Deep embedding … the same datatype without GADT notation: data Shape = Empty | Circle | Square | Translate Vec Shape | Transform Matrix Shape | Union Shape Shape | Intersect Shape Shape | Invert Shape empty = Empty circle = Circle translate = Translate transform = Transform union = Union intersect = Intersect invert = Invert

Deep embedding, cont. All the work happens in the run function: inside :: Point -> Shape -> Bool p `inside` Empty = False p `inside` Circle = ptX p ^ 2 + ptY p ^ 2 <= 1 p `inside` Square = abs (ptX p) <= 1 && abs (ptY p) <= 1 p `inside` Translate v a = subtract p v `inside` a p `inside` Transform m a = mulPt (inv m) p `inside` a p `inside` Union a b = inside p a || inside p b p `inside` Intersect a b = inside p a && inside p b p `inside` Invert a = not (inside p a)

Abstraction! module Shape ( module Matrix , Shape , empty, circle, square , translate, transform, scale, rotate , union, intersect, difference, invert , inside ) where import Matrix … It might be nice to re-export the matrix library Hide the implementation of Shape The interface is the same for both deep and shallow embedding. No visible difference to the user!

More interesting run function: render to ASCII-art module Render where import Shape data Window = Window { bottomLeft :: Point , topRight :: Point , resolution :: (Int, Int) } defaultWindow :: Window pixels :: Window -> [[Point]] render :: Window -> Shape -> String render win a = unlines $ map (concatMap putPixel) (pixels win) where putPixel p | p `inside` a = “[]” | otherwise = “ ”

Some action Go live! module Animate where import Shape import Render import Signal animate :: Window -> Time -> Time -> Signal Shape -> IO () Go live!

Discussion Adding coloured shapes Go back and discuss what changes would need to be made Bad shallow implementations Looking at the render run function we might decide to go for Discuss the problems with this implementation Other questions/comments..? newtype Shape = Shape (Window -> String)

Summary Different kinds of operations Implementation styles Remember constructor functions / combinators / run functions primitive / derived Implementation styles Shallow – representation given by semantics Deep – representation given by operations Remember Compositionality Abstraction