Upload
skills-matter
View
577
Download
1
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
F# CODE QUOTATIONS: CODE-AS-DATAJACK 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 1011. 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