KEMBAR78
F# Type Providers in Depth | PPTX
F# Type Providers in Depth

          Tomas Petricek
 @tomaspetricek | http://tomasp.net
F# 3.0 in Visual Studio 11
The Problem
   Meta-data                    <s:complexType name="Session">
                                <s:sequence>
                                  <s:element minOccurs="0" maxOccurs="1"

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

    F# type                                  name="SessionKey" type="s:string" />
                                  <s:element minOccurs="0" maxOccurs="1"
                                             name="ChosenDeliverySlotInfo"
  declaration                                type="s:string" />
                                  <s:element minOccurs="0" maxOccurs="1"
                                             name="CustomerMessageOfTheDay"
                                             type="s:string" />
type Session =                    <s:element minOccurs="0" maxOccurs="1"
                                             name="CustomerForename"
  { CustomerID : int                         type="s:string" />
    CustomerName : string       </s:sequence>
                                </s:complexType>
    BranchNumber : int
    SessionKey : string
    ChosenDeliverySlotInfo : string
    CustomerMessageOfTheDay : string
    CustomerForName : string }
The Problem
Levels of Data Access

Expression Scale       Program Scale
  Dynamic typing        Code generation


            Internet Scale
             Type providers
Type providers change
 how you think about
   programming!
DEMO: WORLD BANK
What is a Type Provider?
How are Type Providers used?


              Type provider

    IDE                    Compiler
 IntelliSense for     Type-Check     Compile using
Generated Types     Imported Types   Type Provider
Type Providers for .NET

Hiding code generation
 Web Services (WSDL)
 SQL databases (LINQ to Entities)

Erasing provided types
 REST services (World Bank)
 Schematized web (Freebase)
DEMO: WSDL, SQL, ODATA
Queries in F#

Can be turned to quotations
     query { for movie in netflix.Titles do
             where (movie.Name.Contains(search))
             select movie }



Extensible query language
     query { for index in Numbers do
             reverse
             takeWhile index > 10 }
Implementing Type Providers

 public interface ITypeProvider
 {
    Type[] GetTypes();

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

     event EventHandler Invalidate;
 }
Design Considerations

Choosing the right view
  Provide the right projection
  Inferring structure from data

Schema structure
  Refreshing the schema
  Caching of online schema
DEMO: XML TYPE PROVIDER
Summary

The ease of dynamic languages
  Generate types from schema
  No actual types are needed

With the static typing benefits
  Checked at compile-time
  Full IntelliSense support
Compile-Time vs. Run-time

Type Provider



      Runtime API      Provider

                                     Accesses
                                                 Data
                    Generates                   source
             Uses                    Accesses

                     Provided code
Executable
Compile-Time vs. Run-time

Type Provider



      Runtime API      Provider

                                     Accesses
                                                 Data
                    Generates                   source


                     Provided code
Executable
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 ])

F# Type Providers in Depth

  • 1.
    F# Type Providersin Depth Tomas Petricek @tomaspetricek | http://tomasp.net
  • 3.
    F# 3.0 inVisual Studio 11
  • 4.
    The Problem Meta-data <s:complexType name="Session"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" information 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" F# type name="SessionKey" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="ChosenDeliverySlotInfo" declaration type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="CustomerMessageOfTheDay" type="s:string" /> type Session = <s:element minOccurs="0" maxOccurs="1" name="CustomerForename" { CustomerID : int type="s:string" /> CustomerName : string </s:sequence> </s:complexType> BranchNumber : int SessionKey : string ChosenDeliverySlotInfo : string CustomerMessageOfTheDay : string CustomerForName : string }
  • 5.
  • 6.
    Levels of DataAccess Expression Scale Program Scale Dynamic typing Code generation Internet Scale Type providers
  • 7.
    Type providers change how you think about programming!
  • 9.
  • 10.
    What is aType Provider?
  • 11.
    How are TypeProviders used? Type provider IDE Compiler IntelliSense for Type-Check Compile using Generated Types Imported Types Type Provider
  • 12.
    Type Providers for.NET Hiding code generation Web Services (WSDL) SQL databases (LINQ to Entities) Erasing provided types REST services (World Bank) Schematized web (Freebase)
  • 13.
  • 14.
    Queries in F# Canbe turned to quotations query { for movie in netflix.Titles do where (movie.Name.Contains(search)) select movie } Extensible query language query { for index in Numbers do reverse takeWhile index > 10 }
  • 15.
    Implementing Type Providers public interface ITypeProvider { Type[] GetTypes(); Expression GetInvokerExpression ( MethodBase method, ParameterExpression[] params ); event EventHandler Invalidate; }
  • 16.
    Design Considerations Choosing theright view Provide the right projection Inferring structure from data Schema structure Refreshing the schema Caching of online schema
  • 17.
  • 18.
    Summary The ease ofdynamic languages Generate types from schema No actual types are needed With the static typing benefits Checked at compile-time Full IntelliSense support
  • 19.
    Compile-Time vs. Run-time TypeProvider Runtime API Provider Accesses Data Generates source Uses Accesses Provided code Executable
  • 20.
    Compile-Time vs. Run-time TypeProvider Runtime API Provider Accesses Data Generates source Provided code Executable
  • 21.
    Structure of aSimple 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 ])