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, Lan...
F# QUOTATIONS VS C# EXPRESSION
TREES
• C# doesn't require caller to have explicit quotation
• F# quotations literals suppo...
QUOTATIONS CONSTRUCTION
• Quotation literals
• Example: <@ 1 + 2 @>
• Explicit construction using Quotations.Expr type fac...
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” sectio...
SQL COMMAND TYPE PROVIDER
• Dapper on sterioids
• Use SQL Data Tools to develop functional data access layer
• FUNCTIONS, ...
GUI INPUT VALIDATION
• INotifyDataErrorInfo - WPF 4.5, SL, WinRT
• IDataErrorInfo - WPF 4.0 or less, WinForms (partially)
...
DATA BINDING
• Making implicit dependency explicit (no magic strings)
• Leveraging F#/.NET type system
• Leveraging IDE/co...
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 (...
WPF WITH F#?
• F# MVC for WPF
• GitHub project:
http://github.com/dmitry-a-morozov/fsharp-wpf-mvc-series
• Wiki:
http://gi...
Upcoming SlideShare
Loading in …5
×

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

1,550 views

Published on

Code Quotations: Code-as-Data for F#
This tutorial will cover F# Code Quotations in-depth. You'll learn what Code Quotations are, how to use them, and where to apply them in your applications. We'll work through several real-world examples to highlight the important features -- and potential pitfalls -- of Code Quotations.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,550
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. F# CODE QUOTATIONS: CODE-AS-DATA JACK PAPPAS, DMITRY MOROZOV SEPTEMBER 19, 2013
  2. 2. SOURCES •http://tinyurl.com/fs-code- quotations
  3. 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. 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. 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. 6. TYPE PROVIDERS IMPLEMENTATION • Quotations is an essential tool for Type Providers development
  7. 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. 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. 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. 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. 11. DATA BINDING MICRO DSL • Scrap your boilerplate • F# provides great tools to build internal DSLs
  12. 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. 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

×