f# type providers in depth

21
F# Type Providers in Depth Tomas Petricek @tomaspetricek | http://tomasp.net

Upload: tomas-petricek

Post on 14-Jun-2015

3.813 views

Category:

Technology


0 download

DESCRIPTION

Talk slides from the Functional Programming eXchange 2012 on F# type providers. Examples include WorldBank data, SQL, OData and WSDL as well as structure inference for XML files.

TRANSCRIPT

Page 1: F# Type Providers in Depth

F# Type Providers in Depth

Tomas Petricek@tomaspetricek | http://tomasp.net

Page 2: F# Type Providers in Depth
Page 3: F# Type Providers in Depth

F# 3.0 in Visual Studio 11

Page 4: F# Type Providers in Depth

The Problem

<s:complexType name="Session"><s:sequence> <s:element minOccurs="0" maxOccurs="1" name="CustomerID" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerName" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="BranchNumber" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="SessionKey" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="ChosenDeliverySlotInfo" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerMessageOfTheDay" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerForename" type="s:string" /></s:sequence></s:complexType>

type Session = { CustomerID : int CustomerName : string BranchNumber : int SessionKey : string ChosenDeliverySlotInfo : string CustomerMessageOfTheDay : string CustomerForName : string }

Meta-data information

≠F# type

declaration

Page 5: F# Type Providers in Depth

The Problem

Page 6: F# Type Providers in Depth

Levels of Data Access

Expression ScaleDynamic typing

Program ScaleCode generation

Internet ScaleType providers

Page 7: F# Type Providers in Depth

Type providers change how you think about

programming!

Page 8: F# Type Providers in Depth
Page 9: F# Type Providers in Depth

DEMO: WORLD BANK

Page 10: F# Type Providers in Depth

What is a Type Provider?

Page 11: F# Type Providers in Depth

How are Type Providers used?

Type provider

IDE

IntelliSense for Generated Types

Compiler

Type-Check Imported Types

Compile using Type Provider

Page 12: F# Type Providers in Depth

Type Providers for .NET

Hiding code generationWeb Services (WSDL)SQL databases (LINQ to Entities)

Erasing provided typesREST services (World Bank)Schematized web (Freebase)

Page 13: F# Type Providers in Depth

DEMO: WSDL, SQL, ODATA

Page 14: F# Type Providers in Depth

Queries in F#

Can be turned to quotations

Extensible query language

query { for movie in netflix.Titles do where (movie.Name.Contains(search)) select movie }

query { for index in Numbers do reverse takeWhile index > 10 }

Page 15: F# Type Providers in Depth

Implementing Type Providers

public interface ITypeProvider { Type[] GetTypes();

Expression GetInvokerExpression ( MethodBase method, ParameterExpression[] params );

event EventHandler Invalidate;}

Page 16: F# Type Providers in Depth

Design Considerations

Choosing the right viewProvide the right projectionInferring structure from data

Schema structureRefreshing the schemaCaching of online schema

Page 17: F# Type Providers in Depth

DEMO: XML TYPE PROVIDER

Page 18: F# Type Providers in Depth

Summary

The ease of dynamic languagesGenerate types from schemaNo actual types are needed

With the static typing benefitsChecked at compile-timeFull IntelliSense support

Page 19: F# Type Providers in Depth

Compile-Time vs. Run-time

Type Provider

Data source

Executable

Runtime API

Provided code

Generates

Uses Accesses

Accesses

Provider

Page 20: F# Type Providers in Depth

Compile-Time vs. Run-time

Type Provider

Executable

Generates

Accesses

ProviderRuntime API

Provided code

Data source

Page 21: F# Type Providers in Depth

Structure of a Simple Provider [<TypeProvider>]type SampleTypeProvider(config: TypeProviderConfig) = inherit TypeProviderForNamespaces() // Define new type Samples.GeneratedType let thisAssembly = Assembly.GetExecutingAssembly() let providedType = ProvidedTypeDefinition( ... ) do // Add property 'Hello' that just returns a string ProvidedProperty ( "Hello", typeof<string>, IsStatic = true, GetterCode = fun args -> <@@ Runtime.lookup "Hello" @@>) |> providedType.AddMember // Register the type with the compiler this.AddNamespace(namespaceName, [ providedType ])