Alex Walker

Freebase Type Provider with F#

Freebase is a graph database.

To use the freebase type provider you need at least mono version 3, if you already have mono installed check the version

    mono --version

If you don’t have mono installed or the version you have is not 3 or higher you will need to download and install it Download mono 3

Get NuGet. See Install NuGet on OS X

Create a folder called “planets”

    mkdir planets
    cd planets

Use NuGet to get the freebase type provider, I am using version 1.6.137

    nuget install FSharpx.TypeProviders.Freebase -Version 1.6.137

If you want to use the latest version omit the -Version option

    nuget install FSharpx.TypeProviders.Freebase

You will need to reference the type provider dll, determine what the path is, it will change as new versions of the library are released, use the following command

    ls

The output will look something like this

    FSharpx.TypeProviders.Freebase.1.6.137

The free base type provider library is stored in FSharpx.TypeProviders.Freebase.1.6.137/lib/40

To see the contents of the folder use the following command

    ls FSharpx.TypeProviders.Freebase.1.6.137/lib/40

The freebase type provider library is called FSharpx.TypeProviders.Freebase.dll

Create an f# script file

    mate planets.fsx

Add a reference to the freebase type provider in the f# script

    #r "FSharpx.TypeProviders.Freebase.1.6.137/lib/40/FSharpx.TypeProviders.Freebase.dll"
 

Get the data context

    let ctx = FSharpx.TypeProviders.Freebase.FreebaseData.GetDataContext()
 

Loop through the planets and print their names

    for e in ctx.``Science and Technology``.Astronomy.Planets do
      printfn "%s" e.Name

The full script should look like the following planets.fsx

    #r "FSharpx.TypeProviders.Freebase.1.6.137/lib/40/FSharpx.TypeProviders.Freebase.dll"

    let ctx = FSharpx.TypeProviders.Freebase.FreebaseData.GetDataContext()

    for e in ctx.``Science and Technology``.Astronomy.Planets do
      printfn "%s" e.Name

Create a folder in which to store the compiled script

    mkdir bin

Copy the freebase type provider into the bin folder

     cp FSharpx.TypeProviders.Freebase.1.6.137/lib/40/FSharpx.TypeProviders.Freebase.dll bin

Compile the script into an executable

    fsharpc planets.fsx -o:bin/planets.exe

Execute with the following statement

    mono bin/planets.exe

The output should look like the following

    Earth
    Venus
    Mars
    Mercury
    Jupiter
    Neptune
    Saturn
    Uranus
Install NuGet on OS X

Make sure that Mono has been installed

http://www.go-mono.com/mono-downloads/download.html

Download “NuGet Command Line” from codeplex

http://nuget.codeplex.com/releases

Move the NuGet.exe to /usr/local/bin

    mv ~/Downloads/NuGet.exe /usr/local/bin/

Create a file and call it nuget

    mate /usr/local/bin/nuget

Insert the following

    #!/bin/sh
    exec mono --runtime=v4.0.30319 --gc=sgen /usr/local/bin/NuGet.exe "$@"

Mark the file as executable

    sudo chmod +x /usr/local/bin/nuget

Update nuget to the latest version

    sudo nuget update -self

Test nuget

    nuget

Install a package

    cd ~/
    mkdir nugettest
    cd nugettest
    nuget install nunit