{"id":1192,"date":"2012-09-13T09:24:41","date_gmt":"2012-09-13T09:24:41","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1192"},"modified":"2012-09-25T10:13:44","modified_gmt":"2012-09-25T10:13:44","slug":"graph-type-providers-in-fsharpx","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/09\/13\/graph-type-providers-in-fsharpx\/","title":{"rendered":"Graph type providers in FSharpx"},"content":{"rendered":"<p>After the official Visual Studio 2012 launch yesterday I think it\u2019s a good idea to announce two new type providers which are based on the DGMLTypeProvider from the <a href=\"http:\/\/fsharp3sample.codeplex.com\/\">F# 3.0 Sample Pack<\/a>.<\/p>\n<h5>Synchronous and asynchronous state machine<\/h5>\n<p>The first one is only a small extension to the DGMLTypeProvider <a href=\"http:\/\/apollo13cn.blogspot.de\/2012\/09\/dgml-type-provider-explore-design.html\">by Tao<\/a>. which allows to generate state machines from <a href=\"http:\/\/en.wikipedia.org\/wiki\/DGML\">DGML<\/a> files. The extension is simply that you can choose between the original async state machine and a synchronous version, which allows easier testing.<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_4.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_thumb_4.png\" width=\"534\" height=\"250\" \/><\/a><\/p>\n<p>If you want the async version, which performs all state transitions asynchronously, you only have to write <strong>AsyncStateMachine<\/strong> instead of <strong>StateMachine.<\/strong><\/p>\n<h5>State machine as a network of types<\/h5>\n<p>The generated state machine performs only valid state transitions, but we can go one step further and model the state transitions as compile time restrictions:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_5.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_thumb_5.png\" width=\"518\" height=\"377\" \/><\/a><\/p>\n<p>As you can see the compiler knows that we are in State2 and allows only the transitions to State3 and State4.<\/p>\n<p>If you write labels on the edges of the graph the type provider will generate the method names based on the edge label. In the following sample I\u2019ve created a small finite-state machine which allows to check a binary number if it has an even or odd number of zeros:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_3.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/image_thumb_3.png\" width=\"537\" height=\"422\" \/><\/a><\/p>\n<p>As you can see in this case the compiler has already calculated that <em>10100<\/em> has an odd number of zeros \u2013 no need to run the test <img style=\"border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none\" class=\"wlEmoticon wlEmoticon-winkingsmile\" alt=\"Zwinkerndes Smiley\" src=\"http:\/\/www.navision-blog.de\/images\/Graph-type-provider-in-FSharpx_973F\/wlEmoticon-winkingsmile.png\" \/>.<\/p>\n<p>This stuff is already part of the <a href=\"http:\/\/nuget.org\/packages\/FSharpx.TypeProviders.Graph\">FSharpx.TypeProviders.Graph<\/a> package on nuget so please check it out and give feedback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After the official Visual Studio 2012 launch yesterday I think it\u2019s a good idea to announce two new type providers which are based on the DGMLTypeProvider from the F# 3.0 Sample Pack. Synchronous and asynchronous state machine The first one is only a small extension to the DGMLTypeProvider by Tao. which allows to generate state [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,448,8,34],"tags":[664,609,614],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1192"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1192"}],"version-history":[{"count":11,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1192\/revisions"}],"predecessor-version":[{"id":1208,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1192\/revisions\/1208"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}