My first disappointment on F# type system.

Today I found that there are examples of code that correct for C# and could not be compiled in F#. I was very surprised and upset.

I continued playing with Neo4jClient and tried to implement more complex model. I have found that it  is not possible to define F# type for cross entity relationship. To make such relationship I need to define type that implements two interfaces, like this:

type FollowRelationship(target) =
    inherit Relationship(target)
    interface IRelationshipAllowingSourceNode<Person>
    interface IRelationshipAllowingTargetNode<Company>

    override this.RelationshipTypeKey
        with get() = "follow"

But F# compiler does not allow the creation of¬†such type. I’ve got the¬†following compilation error:

This type implements or inherits the same interface at different generic instantiations ‘IRelationshipAllowingParticipantNode’ and ‘IRelationshipAllowingParticipantNode’. This is not permitted in this version of F#.

It happens because IRelationshipAllowingSourceNode and IRelationshipAllowingTargetNode inherited from a single generic interface IRelationshipAllowingParticipantNode and F# does not allow to implement the same interface in different generic instantiations.

Here is an implementation of these interfaces from Neo4jClient source code.

public interface IRelationshipAllowingParticipantNode<out TNode>
{
}
public interface IRelationshipAllowingSourceNode<out TNode>
    : IRelationshipAllowingParticipantNode<TNode>
{
}
public interface IRelationshipAllowingTargetNode<out TNode>
    : IRelationshipAllowingParticipantNode<TNode>
{
}

As I found, there is actually no way to do it in F#. An only option is to write such types in C#. We have a similar question about this on StackOverflow: “Implementing the same interface at different generic instantiations“.

May be it is not a real constrain of F#, but it adds a noise to C#/F# integration. It is means that not all C# design patterns are integrable with F#.

It can be one more answer to

Microsoft Patents related to SharePoint 2013 Search

Extremely useful to understand what is under the hood of SharePoint 2013 Search.

Insights into search black magic

While investigating relevancy calculation in new SharePoint 2013, I did a research and it turned out that there are a lot publicly available patents which cover search in SharePoint, most of them are done in scope of Microsoft Research programs, according to names of inventors. Although there is no direct evidence that it was implemented exactly as described in patents, I created an Excel spreadsheet which mimiques logic described in patents which actual values from SharePoint ‚Äď and numbers match!

I hope most curious of you will find it helpful to deep dive into Enterprise Search relevancy and better understand what happens behind the curtain.

Enterprise relevancy ranking using a neural network

Internal ranking model representation schema

Techniques to perform relative ranking for search results

Ranking and providing search results based in part on a number of click-through features

Document length as…

View original post 28 more words

Using Neo4j Graph DB With F#

Neo4j_logo

Today, I have found Neo4j and could do nothing but play with it. It looks extremely attractive(for example, for enterprise social data).

Neo4j is an open-source, high-performance, enterprise-grade NOSQL graph database.

Neo4j stores data in nodes connected by directed, typed relationships with properties on both, also known as a Property Graph.

First good news, Neo4j has REST API and .NET client library that is available on NuGet. Let’s download it and reference from our script.

#r "System.Net.Http.dll"
#r "System.Net.Http.WebRequest.dll"
#r "Neo4jClient.dll"
#r "Newtonsoft.Json.dll"

open System
open Neo4jClient
open System.Linq

Than, we model a twitter data with following and friendship relationships. Let’s define a Person entity, we need to do nothing more than define a new .NET type.

[<CLIMutable>]
type Person = { Name:string; Twitter:string }

We also need to define relationships. Let’s start from following relationship that is simple and does not store any extra data. To define such relationship we need to write new type that inherits¬†Relationship then implement¬†two interfaces which constrain types of source and target entities that can be connected by this relationship. The last step is to define¬†RelationshipTypeKey property that helps us to identify this relationship in graph.

type FollowRelationship(target) =
    inherit Relationship(target)
    interface IRelationshipAllowingSourceNode<Person>
    interface IRelationshipAllowingTargetNode<Person>

    override this.RelationshipTypeKey
        with get() = "follows"

We also can define relationship that store any extra data. To do so, we need to create a new class for metadata and pass it into Relationship constructor.

[<CLIMutable>]
type KnowsData = { Details:string }

type KnowsRelationship(target, data) =
    inherit Relationship(target, data)
    interface IRelationshipAllowingSourceNode<Person>
    interface IRelationshipAllowingTargetNode<Person>

    override this.RelationshipTypeKey
        with get() = "knows"

So, we are ready to build a graph. Let’s setup a connection to DB and create data.

let client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();

let createPerson person =
    client.Create person

let pA = createPerson { Name = "Person A"; Twitter="tA"}
let pB = createPerson { Name = "Person B"; Twitter="tB"}
let pC = createPerson { Name = "Person C"; Twitter="tC"}
let pD = createPerson { Name = "Person D"; Twitter="tD"}

let follows target source =
    client.CreateRelationship(source, FollowRelationship target)

pB |> follows pA
pC |> follows pA
pD |> follows pB
pD |> follows pC

let knows target details source =
  client.CreateRelationship(source,KnowsRelationship(target,{Details=details}))

pB |> knows pC "colleagues"

We have built the graph. It’s time to make a query to search something. Neo4j supports a special graph oriented query language – Cypher¬†that looks readable even for analysts, not only for programmers. Neo4jClient has a LINQ-like query builder for Cypher.

let pAfollowers =
    client.Cypher
        .Start("n", pA)
        .Match("n<-[r:follows]-e")
        .Return<Person>("e")
        .Results
        .Select(fun x -> x.Name)

If you execute this query, you will see a list of followers of “Person A“. It is¬†[“Person B”; “Person C”].

Neo4j has a cool web UI, where you can not only maintain server, but browse, edit and query your data.

Let’s open Data Browser tab, click on the Layout button and add a new layout. Here we can define rules for how we want to show entities based on their internal properties values. Please fill the form according to the picture below.

Neo4j_Layout

After that save the layout, open Data browse tab one more time and search entity by id 2. You should see the following beautiful graph.

Neo4j_graph

Have a good time playing with graphs! ūüôā

Using Neo4j with F# ‚Äď Cypher 2.0”¬†is a new version of this demo that is updated to VS2013 and Cypher 2.0. Thanks to¬†Chris Skardon.

F# Weekly #12, 2013

FsharpMajor– “What can C# do that F# cannot?”
-“NullReferenceException :-)”
Tomas Petricek.

Welcome to F# Weekly,

Using this weekly, I would like  to introduce Nando de Freitas for everyone who interested in machine learning. He is a machine learning professor at UBC. He has an excellent YouTube channel with lots of interesting lectures. Follow him if you are  interested.

A roundup of F# content from this past week:

News

Videos

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #11

“Why F#?” by F# Weekly

WhyFsharp

“To be, or not to be”
William Shakespeare

Why people choose F#? ¬†This is¬†actually an interesting question. Everyone, who does not use it yet, should find a reason that will force him to try F# or even start use it in production. New posts like ‘Why F#’ have begun to appear more often. It is a good time to collect all thoughts in one ¬†post and give a way to think about F# one more time.

The latest thoughts about F#:

  1. Does the language you choose make a difference?”¬†by Simon Cousins.
  2. F# end to end”¬†by Colin Bull.
  3. Why bugs don‚Äôt like F#”¬†by Simon Cousins.
  4. Why F#?”¬†by Dave Fancher. (Hacker News)
  5. F#: Already Engineered for Testability” by¬†Jack Fox.
  6. Using F# for teching” by¬†F# Software Foundation.
  7. Why use F#?” by¬†FsharpForFunAndProfit.com

Pay attention¬†to the¬†publishing¬†date –¬†things might have changed.

A little bit earlier thoughts:

  1. Why F# is the language for data mining” by¬†Yin Zhu.
  2. Why F#? with Richard Minerich and Phillip Trelford”¬†by Scott Hanselman.
  3. Microsoft’s F#: 10 Reasons Why It’s a Hot Programming Language for Developers” by eWeek.
  4. F# is Greater than C#” by Aaron Erickson.
  5. Why F#?” by Richard Minerich.
  6. The Unheralded Benefits of the F# Programming Language” by Aaron Erickson
  7. F# in the Enterprise” by Visual F# Team.
  8. F# in the Enterprise” by Simon Cousins.
  9. Nine reasons to use F#” by Brian.
  10. Why not F#?” by Fredrik Holmstrom.
  11. Why F#?” by Joel Pobar.
  12. Why F#?” by Mike James.
  13. Why F# *ROCKS!*” by M. David Peterson.
  14. Why F#?” by Carey Cilyok.
  15. Why F#” by 2#4u.

Some discussions:

  1. Why F# Why Not C#?
  2. Why should I use F#?
  3. When & Where do we use F#?
  4. Why is F# so special?
  5. In what areas might the use of F# be more appropriate than C#?
  6. What are the benefits of using C# vs F# or F# vs C#?
  7. Anyone Actually Using F# in Production?
  8. What’s wrong with F#?
  9. What is the case against F#?
  10. Why should a .net developer learn F#?

If you still do not use F#, it is a good time to read stories from the others and think one more time.

P.S. If you know any other interesting posts/discussions about why people like or dislike F#, please, leave a link in comments. Thanks.

F# Weekly #11, 2013

Welcome to F# Weekly,

One more week passed by with an¬†excellent event “Functional Programming Exchange 2013“.¬†All tracks are recorded and available on the conference page.¬†But this is not the only event in F# universe, so see details¬†below:

News

Videos

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #10