SlideShare a Scribd company logo
F# CODE QUOTATIONS:
CODE-AS-DATA
JACK PAPPAS, DMITRY MOROZOV
SEPTEMBER 19, 2013
SOURCES
•http://tinyurl.com/fs-code-
quotations
RECAP
• First appeared in Lisp: '(one two three) vs (one two three). Hence the
name.
• Code as Data (meta-programming, Language Oriented Programming)
• Transformation (to the same or other language)
• Explicit construction and evaluation
• Numerous usage examples:
• F# query expressions , LINQ
• Foq/Moq/Unquote - testing frameworks
• Type providers development
• F# to GPU (Jack's work)
• WebSharper, FunScript, M-Brace, etc.
F# QUOTATIONS VS C# EXPRESSION
TREES
• C# doesn't require caller to have explicit quotation
• F# quotations literals support full language (almost) as opposed to C#
expr. trees
• F# ReflectedDefinitionAttribute - ability to quote programming
language entities: methods, classes, modules
• Composition: F# splicing vs C# OOP
• Decomposition: F# active patterns vs Visitor pattern
QUOTATIONS CONSTRUCTION
• Quotation literals
• Example: <@ 1 + 2 @>
• Explicit construction using Quotations.Expr type factory methods
• Example:
• let op_Add = Type.GetType("Microsoft.FSharp.Core.Operators,
FSharp.Core").GetMethod("op_Addition").MakeGenericMethod(typeof<int>,
typeof<int>, typeof<int>)
• Expr.Cast<int>(Expr.Call(op_Add, [Expr.Value 1; Expr.Value 2]))
• Splicing
• let x = <@ 1 @> in <@ %x + 2 @>
TYPE PROVIDERS IMPLEMENTATION
• Quotations is an essential tool for Type Providers development
TYPE PROVIDER 101
1. Create project using “F# Type Provider Template” by Tao Liu
2. Change “Debug” settings “Start” section to open same solution
within another instance of IDE
3. Replace ProvidedTypes-head.* with the latest from
http://fsharp3sample.codeplex.com/
4. Congratulations! You have skeleton of a working type provider
5. Now pick a data source, develop an new Type Provider and become
F# ninja
SQL COMMAND TYPE PROVIDER
• Dapper on sterioids
• Use SQL Data Tools to develop functional data access layer
• FUNCTIONS, VIEWS, TABLES
• CROSS APPLY operator (monadic Bind, C# SelectMany)
GUI INPUT VALIDATION
• INotifyDataErrorInfo - WPF 4.5, SL, WinRT
• IDataErrorInfo - WPF 4.0 or less, WinForms (partially)
• Statically-typed
• Would be verbose without F# language features:
• Active patterns
• Explicit member constraints
• Partial application
DATA BINDING
• Making implicit dependency explicit (no magic strings)
• Leveraging F#/.NET type system
• Leveraging IDE/compiler
type IView<'Events, 'Model> =
inherit IObservable<'Events>
abstract SetBindings : 'Model -> unit
<@ textBox.Text <- model.Name @>
textBox.SetBinding(TextBox.TextProperty, "Name")
DATA BINDING MICRO DSL
• Scrap your boilerplate
• F# provides great tools to build internal DSLs
DERIVED PROPERTIES
• Scrap your boilerplate
• F# pure magic
• ReflectedDefinitionAttribute
• Quotation
• Active patterns (a lot)
• WPF black magic (MultiBinding)
• C# way of doing things
• http://knockoutcs.com/index.html
• IL rewriting (PostSharp) or weaving (http://github.com/Fody/PropertyChanged)
WPF WITH F#?
• F# MVC for WPF
• GitHub project:
http://github.com/dmitry-a-morozov/fsharp-wpf-mvc-series
• Wiki:
http://github.com/dmitry-a-morozov/fsharp-wpf-mvc-series/wiki
• F# great general purpose language
• Go thru the framework code base – it’s great language tutorial
• Hard things easy impossible things possible

More Related Content

ODP
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
PPTX
Creating Domain Specific Languages in F#
PPTX
Domain Specific Languages: The Functional Way
PPTX
How F# Learned to Stop Worrying and Love the Data
PPTX
Introduction to F#
PPTX
F# Tutorial @ QCon
PPT
F# Eye for the C# Guy
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
Creating Domain Specific Languages in F#
Domain Specific Languages: The Functional Way
How F# Learned to Stop Worrying and Love the Data
Introduction to F#
F# Tutorial @ QCon
F# Eye for the C# Guy

What's hot (19)

PPTX
Introduction to F# 3.0
PPTX
C# language
PPTX
History of F#, and the ML family of languages.
PPT
Trends in Programming Technology you might want to keep an eye on af Bent Tho...
PDF
C# 9 and 10 - What's cool?
PDF
Swift vs. Language X
PPTX
c# usage,applications and advantages
PPTX
Procedure oriented programming
KEY
Remix Your Language Tooling (JSConf.eu 2012)
PDF
Create Your Own Language
PPT
F# and the DLR
PDF
LIL Presentation
PPTX
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
KEY
Using Aspects for Language Portability (SCAM 2010)
PDF
The Spoofax Language Workbench (SPLASH 2010)
PPTX
C programming language
PPTX
PPTX
C & C++ Training in Ambala ! BATRA COMPUTER CENTRE
PPTX
C vs c++
Introduction to F# 3.0
C# language
History of F#, and the ML family of languages.
Trends in Programming Technology you might want to keep an eye on af Bent Tho...
C# 9 and 10 - What's cool?
Swift vs. Language X
c# usage,applications and advantages
Procedure oriented programming
Remix Your Language Tooling (JSConf.eu 2012)
Create Your Own Language
F# and the DLR
LIL Presentation
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
Using Aspects for Language Portability (SCAM 2010)
The Spoofax Language Workbench (SPLASH 2010)
C programming language
C & C++ Training in Ambala ! BATRA COMPUTER CENTRE
C vs c++
Ad

Similar to Dmitry mozorov on code quotations code as-data for f# (20)

PPTX
Introduction to f#
PDF
"Introduction to F#" - South Dakota Code Camp, November 5, 2011
PPTX
Introduction to F#
PPTX
F# for functional enthusiasts
PPTX
Sharper tools with F#
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PDF
Madrid F# Meetup: Introduction to F#
PPT
Accessing loosely structured data from F# and C#
PDF
F# for Scala developers
PPTX
Loving data with F#
PPTX
Valdis Iljuconoks - Loving data with F#
PDF
Functional Design Patterns (DevTernity 2018)
PDF
Domain Driven Design with the F# type System -- NDC London 2013
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PPTX
F# for startups
PPTX
F# for startups v2
PPTX
TechDaysNL 2015 - DDD with F#
PPTX
Can F# make us better as .NET developers?
PPTX
Information-rich programming in F# (ML Workshop 2012)
Introduction to f#
"Introduction to F#" - South Dakota Code Camp, November 5, 2011
Introduction to F#
F# for functional enthusiasts
Sharper tools with F#
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
Madrid F# Meetup: Introduction to F#
Accessing loosely structured data from F# and C#
F# for Scala developers
Loving data with F#
Valdis Iljuconoks - Loving data with F#
Functional Design Patterns (DevTernity 2018)
Domain Driven Design with the F# type System -- NDC London 2013
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
F# for startups
F# for startups v2
TechDaysNL 2015 - DDD with F#
Can F# make us better as .NET developers?
Information-rich programming in F# (ML Workshop 2012)
Ad

More from Skills Matter (20)

PDF
5 things cucumber is bad at by Richard Lawrence
ODP
Patterns for slick database applications
PDF
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
ODP
Oscar reiken jr on our success at manheim
PDF
Cukeup nyc ian dees on elixir, erlang, and cucumberl
PDF
Cukeup nyc peter bell on getting started with cucumber.js
PDF
Agile testing & bdd e xchange nyc 2013 jeffrey davidson & lav pathak & sam ho...
ODP
Progressive f# tutorials nyc rachel reese & phil trelford on try f# from zero...
ODP
Progressive f# tutorials nyc don syme on keynote f# in the open source world
PDF
Agile testing & bdd e xchange nyc 2013 gojko adzic on bond villain guide to s...
PDF
A poet's guide_to_acceptance_testing
PDF
Russ miles-cloudfoundry-deep-dive
KEY
Serendipity-neo4j
PDF
Simon Peyton Jones: Managing parallelism
PDF
Plug 20110217
PDF
Lug presentation
PPT
I went to_a_communications_workshop_and_they_t
PDF
Plug saiku
PDF
Huguk lily
PDF
Bootstrapping a-devops-matter
5 things cucumber is bad at by Richard Lawrence
Patterns for slick database applications
Scala e xchange 2013 haoyi li on metascala a tiny diy jvm
Oscar reiken jr on our success at manheim
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc peter bell on getting started with cucumber.js
Agile testing & bdd e xchange nyc 2013 jeffrey davidson & lav pathak & sam ho...
Progressive f# tutorials nyc rachel reese & phil trelford on try f# from zero...
Progressive f# tutorials nyc don syme on keynote f# in the open source world
Agile testing & bdd e xchange nyc 2013 gojko adzic on bond villain guide to s...
A poet's guide_to_acceptance_testing
Russ miles-cloudfoundry-deep-dive
Serendipity-neo4j
Simon Peyton Jones: Managing parallelism
Plug 20110217
Lug presentation
I went to_a_communications_workshop_and_they_t
Plug saiku
Huguk lily
Bootstrapping a-devops-matter

Recently uploaded (20)

PPTX
Tartificialntelligence_presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Tartificialntelligence_presentation.pptx
NewMind AI Weekly Chronicles - August'25-Week II
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
SOPHOS-XG Firewall Administrator PPT.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Programs and apps: productivity, graphics, security and other tools
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
A comparative analysis of optical character recognition models for extracting...
Group 1 Presentation -Planning and Decision Making .pptx
MIND Revenue Release Quarter 2 2025 Press Release
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm

Dmitry mozorov on code quotations code as-data for f#

  • 1. F# CODE QUOTATIONS: CODE-AS-DATA JACK PAPPAS, DMITRY MOROZOV SEPTEMBER 19, 2013
  • 3. RECAP • First appeared in Lisp: '(one two three) vs (one two three). Hence the name. • Code as Data (meta-programming, Language Oriented Programming) • Transformation (to the same or other language) • Explicit construction and evaluation • Numerous usage examples: • F# query expressions , LINQ • Foq/Moq/Unquote - testing frameworks • Type providers development • F# to GPU (Jack's work) • WebSharper, FunScript, M-Brace, etc.
  • 4. F# QUOTATIONS VS C# EXPRESSION TREES • C# doesn't require caller to have explicit quotation • F# quotations literals support full language (almost) as opposed to C# expr. trees • F# ReflectedDefinitionAttribute - ability to quote programming language entities: methods, classes, modules • Composition: F# splicing vs C# OOP • Decomposition: F# active patterns vs Visitor pattern
  • 5. QUOTATIONS CONSTRUCTION • Quotation literals • Example: <@ 1 + 2 @> • Explicit construction using Quotations.Expr type factory methods • Example: • let op_Add = Type.GetType("Microsoft.FSharp.Core.Operators, FSharp.Core").GetMethod("op_Addition").MakeGenericMethod(typeof<int>, typeof<int>, typeof<int>) • Expr.Cast<int>(Expr.Call(op_Add, [Expr.Value 1; Expr.Value 2])) • Splicing • let x = <@ 1 @> in <@ %x + 2 @>
  • 6. TYPE PROVIDERS IMPLEMENTATION • Quotations is an essential tool for Type Providers development
  • 7. TYPE PROVIDER 101 1. Create project using “F# Type Provider Template” by Tao Liu 2. Change “Debug” settings “Start” section to open same solution within another instance of IDE 3. Replace ProvidedTypes-head.* with the latest from http://fsharp3sample.codeplex.com/ 4. Congratulations! You have skeleton of a working type provider 5. Now pick a data source, develop an new Type Provider and become F# ninja
  • 8. SQL COMMAND TYPE PROVIDER • Dapper on sterioids • Use SQL Data Tools to develop functional data access layer • FUNCTIONS, VIEWS, TABLES • CROSS APPLY operator (monadic Bind, C# SelectMany)
  • 9. GUI INPUT VALIDATION • INotifyDataErrorInfo - WPF 4.5, SL, WinRT • IDataErrorInfo - WPF 4.0 or less, WinForms (partially) • Statically-typed • Would be verbose without F# language features: • Active patterns • Explicit member constraints • Partial application
  • 10. DATA BINDING • Making implicit dependency explicit (no magic strings) • Leveraging F#/.NET type system • Leveraging IDE/compiler type IView<'Events, 'Model> = inherit IObservable<'Events> abstract SetBindings : 'Model -> unit <@ textBox.Text <- model.Name @> textBox.SetBinding(TextBox.TextProperty, "Name")
  • 11. DATA BINDING MICRO DSL • Scrap your boilerplate • F# provides great tools to build internal DSLs
  • 12. DERIVED PROPERTIES • Scrap your boilerplate • F# pure magic • ReflectedDefinitionAttribute • Quotation • Active patterns (a lot) • WPF black magic (MultiBinding) • C# way of doing things • http://knockoutcs.com/index.html • IL rewriting (PostSharp) or weaving (http://github.com/Fody/PropertyChanged)
  • 13. WPF WITH F#? • F# MVC for WPF • GitHub project: http://github.com/dmitry-a-morozov/fsharp-wpf-mvc-series • Wiki: http://github.com/dmitry-a-morozov/fsharp-wpf-mvc-series/wiki • F# great general purpose language • Go thru the framework code base – it’s great language tutorial • Hard things easy impossible things possible