13
F# CODE QUOTATIONS: CODE-AS-DATA JACK PAPPAS, DMITRY MOROZOV SEPTEMBER 19, 2013

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

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

Page 1: Dmitry mozorov on code quotations code as-data for f#

F# CODE QUOTATIONS: CODE-AS-DATAJACK PAPPAS, DMITRY MOROZOV

SEPTEMBER 19, 2013

Page 2: Dmitry mozorov on code quotations code as-data for f#

SOURCES

•http://tinyurl.com/fs-code-quotations

Page 3: Dmitry mozorov on code quotations code as-data for f#

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.

Page 4: Dmitry mozorov on code quotations code as-data for f#

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

Page 5: Dmitry mozorov on code quotations code as-data for f#

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 @>

Page 6: Dmitry mozorov on code quotations code as-data for f#

TYPE PROVIDERS IMPLEMENTATION

• Quotations is an essential tool for Type Providers development

Page 7: Dmitry mozorov on code quotations code as-data for f#

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

Page 8: Dmitry mozorov on code quotations code as-data for f#

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)

Page 9: Dmitry mozorov on code quotations code as-data for f#

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

Page 10: Dmitry mozorov on code quotations code as-data for f#

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")

Page 11: Dmitry mozorov on code quotations code as-data for f#

DATA BINDING MICRO DSL

• Scrap your boilerplate

• F# provides great tools to build internal DSLs

Page 12: Dmitry mozorov on code quotations code as-data for f#

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)

Page 13: Dmitry mozorov on code quotations code as-data for f#

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