SlideShare uma empresa Scribd logo
1 de 35
Combinators, DSLs, HTML and F# Robert Pickering, ALTI
About the Presenter 2 ,[object Object]
Oldest F# user outside of Microsoft
Written a book about F# (now in its second edition) ,[object Object]
I have 3 catsContact me: robert@strangelights.com http://strangelights.com/blog
What is a Combinator? A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
Combinatory Logic in Computing In computer science, combinatory logic is used as a simplified model of computation, used in computability theory and proof theory. Despite its simplicity, combinatory logic captures many essential features of computation. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
Combinator Library "A combinator library offers functions (the combinators) that combine functions together to make bigger functions"[1]. These kinds of libraries are particularly useful for allowing domain-specific programming languages to be easily embedded into a general purpose language by defining a few primitive functions for the given domain. Souce: Wikipedia http://en.wikipedia.org/wiki/Combinator_library [1] “A History of Haskell” Hudak, Hughes, Peyton Jones, Wadler
History of Haskell: Combinator Libraries What is a combinatorlibrary? The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
History of Haskell: Combinator Libraries What is a combinatorlibrary?The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
History of Haskell: Combinator Libraries Another productive way to think of a combinator library is as a domain-specific language (DSL) for describing values of a particular type.
What is a Domain Specific Language? A programming language tailored for a particular application domain, which captures precisely the semantics of the application domain -- no more, no less. A DSL allows one to develop software for a particular application domain quickly, and effectively, yielding programs that are easy to understand, reason about, and maintain. Hudak
Combinatorsvs DSLs Combinartor libraries are a special case of DSLs Sometimes called DSELs (Domain Specific Embed languages) DSELs have several advantages: ,[object Object]
Inherit compilers and tools.
Uniform “look and feel” across many DSLs
DSLs integrated with full programming language, and with each other.DSELs one disadvantage: ,[object Object],[object Object]
The Anatomy of a DSL Syntax Tree type Expression =     | Add of Expression * Expression     | Subtract of Expression * Expression     | Multiply of Expression * Expression     | Constant ofint     | Parameter of string with staticmember (+) (x, y) = Add(x, y) staticmember (-) (x, y) = Subtract(x, y) staticmember (*) (x, y) = Multiply(x, y) moduleNumericLiteralN =  letFromZero() = Constant 0 letFromOne() = Constant 1 let FromInt32 = Constant letparam = Parameter Combinators
The Anatomy of a DSL letexpr = (1N + 2N) * (5N - 2N) valexpr : Expression =   Multiply (Add (Constant 1,Constant 2),            Subtract (Constant 5,Constant 2))
The Anatomy of a DSL Expressions now have an abstract tree like representation: ,[object Object]
Add
Constant 1
Constant 2
Subtract
Constant 5
Constant 2
This can then be evaluated
Or we can preform more advanced analysis ,[object Object]
The Anatomy of a DSL
The Anatomy of a DSL letrecsimplifyExpressionexp = let simpIfPoss op exp1 exp2 = letexp' = op (simplifyExpression exp1,  simplifyExpressionexp2) ifexp' = expthenexp' elsesimplifyExpressionexp' matchexpwith     | Multiply(Constant 0, Constant _) -> Constant 0     | Multiply(Constant _, Constant 0) -> Constant 0     | Multiply(Constant n1, Constant n2) -> Constant (n1 * n2)     | Add(Constant n1, Constant n2) -> Constant (n1 + n2)     | Subtract(Constant n1, Constant n2) -> Constant (n1 - n2)     | Multiply(exp1, exp2) ->simpIfPoss Multiply exp1 exp2     | Add(exp1, exp2) ->simpIfPoss Add exp1 exp2     | Subtract(exp1, exp2) ->simpIfPoss Add exp1 exp2     | Constant _ | Parameter _ ->exp
The Anatomy of a DSL
Why a DSL for HTML? <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <h1>Hello world!</h1> <formid="form1"> <inputtype="text"name="name"/>     </form> </body> </html>
Why a DSL for HTML ? HTML has a tree like structure We need be able to generate and manipulate this tree in an abstract, type safe way
Why a DSL for HTML? ,[object Object]
Tag: head
Tag: title

Mais conteúdo relacionado

Mais procurados

Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisEelco Visser
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Richard Thomson
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Omar Abdelhafith
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional ProgrammingJordan Parmer
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
S1 DML Syntax and Invocation
S1 DML Syntax and InvocationS1 DML Syntax and Invocation
S1 DML Syntax and InvocationArvind Surve
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurSiddharth Mathur
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Avelin Huo
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurSiddharth Mathur
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library FunctionDeepak Singh
 

Mais procurados (18)

Hadoop map reduce concepts
Hadoop map reduce conceptsHadoop map reduce concepts
Hadoop map reduce concepts
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
 
basics of c++
basics of c++basics of c++
basics of c++
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
R language
R languageR language
R language
 
F# 101
F# 101F# 101
F# 101
 
Intro to Functional Programming
Intro to Functional ProgrammingIntro to Functional Programming
Intro to Functional Programming
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
S1 DML Syntax and Invocation
S1 DML Syntax and InvocationS1 DML Syntax and Invocation
S1 DML Syntax and Invocation
 
Animate
AnimateAnimate
Animate
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathur
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
 
Modern C++
Modern C++Modern C++
Modern C++
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathur
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 

Semelhante a Combinators, DSLs, HTML and F#

AD215 - Practical Magic with DXL
AD215 - Practical Magic with DXLAD215 - Practical Magic with DXL
AD215 - Practical Magic with DXLStephan H. Wissel
 
Tech Days Paris Intoduction F# and Collective Intelligence
Tech Days Paris Intoduction F# and Collective IntelligenceTech Days Paris Intoduction F# and Collective Intelligence
Tech Days Paris Intoduction F# and Collective IntelligenceRobert Pickering
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#Talbott Crowell
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick offAndrea Gangemi
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010Satish Verma
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUSkills Matter
 
project 1Assignment 1.pdf .docx
project 1Assignment 1.pdf                                .docxproject 1Assignment 1.pdf                                .docx
project 1Assignment 1.pdf .docxwkyra78
 
Convention-Based Syntactic Descriptions
Convention-Based Syntactic DescriptionsConvention-Based Syntactic Descriptions
Convention-Based Syntactic DescriptionsRay Toal
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta ProgrammingReggie Meisler
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perlmegakott
 
XPath - XML Path Language
XPath - XML Path LanguageXPath - XML Path Language
XPath - XML Path Languageyht4ever
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonTristan Penman
 
NEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator PresentationNEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator Presentationaskankit
 

Semelhante a Combinators, DSLs, HTML and F# (20)

AD215 - Practical Magic with DXL
AD215 - Practical Magic with DXLAD215 - Practical Magic with DXL
AD215 - Practical Magic with DXL
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
PDF Localization
PDF  LocalizationPDF  Localization
PDF Localization
 
Tech Days Paris Intoduction F# and Collective Intelligence
Tech Days Paris Intoduction F# and Collective IntelligenceTech Days Paris Intoduction F# and Collective Intelligence
Tech Days Paris Intoduction F# and Collective Intelligence
 
Exploring SharePoint with F#
Exploring SharePoint with F#Exploring SharePoint with F#
Exploring SharePoint with F#
 
F sharp - an overview
F sharp - an overviewF sharp - an overview
F sharp - an overview
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
 
Visual Studio .NET2010
Visual Studio .NET2010Visual Studio .NET2010
Visual Studio .NET2010
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPU
 
F# and the DLR
F# and the DLRF# and the DLR
F# and the DLR
 
project 1Assignment 1.pdf .docx
project 1Assignment 1.pdf                                .docxproject 1Assignment 1.pdf                                .docx
project 1Assignment 1.pdf .docx
 
Convention-Based Syntactic Descriptions
Convention-Based Syntactic DescriptionsConvention-Based Syntactic Descriptions
Convention-Based Syntactic Descriptions
 
Practical Meta Programming
Practical Meta ProgrammingPractical Meta Programming
Practical Meta Programming
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
XPath - XML Path Language
XPath - XML Path LanguageXPath - XML Path Language
XPath - XML Path Language
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
 
NEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator PresentationNEOOUG 2010 Oracle Data Integrator Presentation
NEOOUG 2010 Oracle Data Integrator Presentation
 
C-PROGRAM
C-PROGRAMC-PROGRAM
C-PROGRAM
 
Learn C
Learn CLearn C
Learn C
 

Combinators, DSLs, HTML and F#

  • 1. Combinators, DSLs, HTML and F# Robert Pickering, ALTI
  • 2.
  • 3. Oldest F# user outside of Microsoft
  • 4.
  • 5. I have 3 catsContact me: robert@strangelights.com http://strangelights.com/blog
  • 6. What is a Combinator? A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
  • 7. Combinatory Logic in Computing In computer science, combinatory logic is used as a simplified model of computation, used in computability theory and proof theory. Despite its simplicity, combinatory logic captures many essential features of computation. Source: Wikipedia, http://en.wikipedia.org/wiki/Combinatory_Logic
  • 8. Combinator Library "A combinator library offers functions (the combinators) that combine functions together to make bigger functions"[1]. These kinds of libraries are particularly useful for allowing domain-specific programming languages to be easily embedded into a general purpose language by defining a few primitive functions for the given domain. Souce: Wikipedia http://en.wikipedia.org/wiki/Combinator_library [1] “A History of Haskell” Hudak, Hughes, Peyton Jones, Wadler
  • 9. History of Haskell: Combinator Libraries What is a combinatorlibrary? The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
  • 10. History of Haskell: Combinator Libraries What is a combinatorlibrary?The reader will search in vain for a definition of this heavily used term, but the key idea is this: a combinator library offers functions (the combinators) that combine functions together to make bigger functions.
  • 11. History of Haskell: Combinator Libraries Another productive way to think of a combinator library is as a domain-specific language (DSL) for describing values of a particular type.
  • 12. What is a Domain Specific Language? A programming language tailored for a particular application domain, which captures precisely the semantics of the application domain -- no more, no less. A DSL allows one to develop software for a particular application domain quickly, and effectively, yielding programs that are easy to understand, reason about, and maintain. Hudak
  • 13.
  • 15. Uniform “look and feel” across many DSLs
  • 16.
  • 17. The Anatomy of a DSL Syntax Tree type Expression = | Add of Expression * Expression | Subtract of Expression * Expression | Multiply of Expression * Expression | Constant ofint | Parameter of string with staticmember (+) (x, y) = Add(x, y) staticmember (-) (x, y) = Subtract(x, y) staticmember (*) (x, y) = Multiply(x, y) moduleNumericLiteralN = letFromZero() = Constant 0 letFromOne() = Constant 1 let FromInt32 = Constant letparam = Parameter Combinators
  • 18. The Anatomy of a DSL letexpr = (1N + 2N) * (5N - 2N) valexpr : Expression = Multiply (Add (Constant 1,Constant 2), Subtract (Constant 5,Constant 2))
  • 19.
  • 20. Add
  • 26. This can then be evaluated
  • 27.
  • 28. The Anatomy of a DSL
  • 29. The Anatomy of a DSL letrecsimplifyExpressionexp = let simpIfPoss op exp1 exp2 = letexp' = op (simplifyExpression exp1, simplifyExpressionexp2) ifexp' = expthenexp' elsesimplifyExpressionexp' matchexpwith | Multiply(Constant 0, Constant _) -> Constant 0 | Multiply(Constant _, Constant 0) -> Constant 0 | Multiply(Constant n1, Constant n2) -> Constant (n1 * n2) | Add(Constant n1, Constant n2) -> Constant (n1 + n2) | Subtract(Constant n1, Constant n2) -> Constant (n1 - n2) | Multiply(exp1, exp2) ->simpIfPoss Multiply exp1 exp2 | Add(exp1, exp2) ->simpIfPoss Add exp1 exp2 | Subtract(exp1, exp2) ->simpIfPoss Add exp1 exp2 | Constant _ | Parameter _ ->exp
  • 30. The Anatomy of a DSL
  • 31. Why a DSL for HTML? <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <h1>Hello world!</h1> <formid="form1"> <inputtype="text"name="name"/> </form> </body> </html>
  • 32. Why a DSL for HTML ? HTML has a tree like structure We need be able to generate and manipulate this tree in an abstract, type safe way
  • 33.
  • 42.
  • 43. DSLs for Working with HTML #light – DSL for working with HTML, by SadekDrobi: http://sharplight.codeplex.com/ F# Web Tools – Tool kit for Ajax programing, by TomášPetříček: http://www.codeplex.com/fswebtools WebSharper – DSL for HTML with F# to JavaScript translator, by IntelliFactory: http://www.intellifactory.com/products/wsp
  • 44. Hello world Paragraph Element [<JavaScript>] let Main () = let welcome = P [Text "Welcome"] Div [ welcome Input [Type "Button"; Value "Click me!"] |> On Events.Click (fun e -> welcome.Text <- "Hello, world!") ] Input Button Element Client Side Event Handler
  • 45. How Does it Work? At compile time an extra step takes place: Functions & types are translated into JavaScript Compiler attempts to optimize JavaScript code Compiler tries to preserve binding names F# functions are compiled using the F# quotations system
  • 46. Aside: Quotations in F# openMicrosoft.FSharp.Quotations.Patterns openMicrosoft.FSharp.Quotations.DerivedPatterns let quotation = <@ 1 + 1 @> // a function to interpret very simple quotations letrecinterpretQuotationexp = matchexpwith | Value (x, typ) whentyp = typeof<int> ->printfn"%d" (x :?> int) | SpecificCall <@ (+) @> (_, _, [l;r]) ->interpretQuotation l printfn"+" interpretQuotation r | _ ->printfn"not supported" // test the function interpretQuotation quotation
  • 47. Formlets– Compositional Forms Text Input Element [<JavaScript>] let input label err = Controls.Input"" |> Validator.IsNotEmpty err |> Controls.Enhance.WithLabel label [<JavaScript>] letinputInt label err = Controls.Input"" |> Validator.IsInt err |> Controls.Enhance.WithLabellabel Add Validation Element Add Label Element
  • 48. Formlets – Compositional Forms [<JavaScriptType>] typeBasicInfo = { Name : string; Age : int } [<JavaScript>] letBasicInfoForm () : Formlet<BasicInfo> = Formlet.Yield (fun name age -> { Name = name; Age = age |> int }) <*> input "Name""Please enter your name" <*> inputInt"Age""Please enter a valid age" Construct form result Construct and compose input elements
  • 49. Formlets – Compositional Forms Configuration options letconf = { Render.FormConfiguration.Defaultwith Header = "Sign up" } Render.FormThenResponse conf (ContactForms.BasicInfoForm()) (fun info ->Div [ P [ "Thanks " + info.Name ] P [ "You are " + string info.Age + " years old" ] ]) Form and config Function that constructs result
  • 51. Flowlets – Composing Formlets Flowlet.Flowlet.Do { let! info = ContactForms.BasicInfoForm() |> Flowlet.OfFormlet conf1 let! contact = ContactForms.ContactInfoForm() |> Flowlet.OfFormlet conf2 return info, contact }
  • 52. Flowlets – Composing Formlets Flowlet.Run (fun (info, contact) -> letresult = matchcontact with | ContactForms.ContactVia.Address address -> "the address: " + address.Street + ", " + address.City + ", " + address.Country | ContactForms.ContactVia.Phone phone -> "the phone number: " + phone letresults = FieldSet [ Legend [Text "Sign-up summary"] P ["Hi " + info.Name + "!" |> Text] P ["You are " + string info.Age + " years old" |> Text] P ["Your preferred contact method is via " + result |> Text] ] steps -< [ results ] |> ignore)
  • 53. Wrapping It Up Combinator libraries/DSL are an excellent way of tackling many problems Especially if that problem involves some kind of tree manipulation F# provides the features you need to implement combinator libraries
  • 54. Further F# Info MSDN Center: http://msdn.microsoft.com/fsharp/ User’s forums: http://cs.hubfs.net/forums http://stackoverflow.com/tags/F%23 Blogs (there are lots of others) : http://blogs.msnd.com/dsyme http://strangelights.com/blog Websites : http://code.msdn.microsoft.com/fsharpsamples http://code.google.com/hosting/search?q=label:fsharp http://codeplex.com/Project/ProjectDirectory.aspx?TagName=F%23
  • 55. Books