This document summarizes code quotations in F# and their uses, including for meta-programming, code transformation, testing frameworks, type providers, and data binding. Key points include: code quotations allow treating code as data; F# supports full language quotations unlike C# expression trees; quotations enable composing and decomposing code; and quotations are essential for type providers to access and represent types from other sources. Examples are provided for constructing and splicing quotations, implementing type providers, and using quotations for GUI input validation and data binding.
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 @>
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