Note that there are some explanatory texts on larger screens.

plurals
  1. POEmulating Prolog backtracking in F#
    primarykey
    data
    text
    <p>I am currently involved in a project to develop an application able to consider a set of nodes and connections and find the shortest path (a common and well-known issue) between two nodes (on allowed connections). Well I don't really have to build an application from zero, but just need to "convert" a Prolog pre-existing application in f#. I thought I bit about it and finally asked myself one question: "Instead of developing a special purpose solution and implementing new algorithms specifically binded to this problem, can I create a program able to accept facts like Prolog and use them to make queries or something similar?".</p> <p>By doing so I would create a set of facts (like in Prolog) and then use them to make queries. So, considering now this new issue (converting Prolog in F#) I need to find a way to create facts like these:</p> <pre><code>myfact1(el1, el2,..., eln). myfact2(el1, el2,..., elm). ... myfactk(el1, el2,..., elp). </code></pre> <p>to something in a similar syntax like:</p> <pre><code>fact factname1: el1 el2 ... eln; fact factname2: el1 el2 ... elm; ... fact factnamek: el1 el2 ... elp; </code></pre> <p>I know that F# is very well for parsing, so I think that parsing this would probably not be a problem.</p> <p>OK! Now that it is parsed, I should define an algorithm that, while parsing the code, stores all facts in some sort of knowledge (nothing more than a table). In order to make then all needed associations.</p> <p>For example a solution might be a hashtable that considers all associations</p> <pre><code>factname1 -&gt; el1 factname1 -&gt; el2 ... factname1 -&gt; eln factname2 -&gt; el1 factnale2 -&gt; el2 ... factname2 -&gt; elm factname3 -&gt; el1 ... ... factnamek -&gt; el1 factnamek -&gt; el2 ... factnamek -&gt; elp </code></pre> <p>By doing so I will always be able to solve queries. For example consider the following Prolog fact</p> <pre><code>mother(A, B) % This means that A is mother of B mother(C, D) </code></pre> <p>In F# I create</p> <p>fact mother: A B; fact mother: C D;</p> <p>My hashtable is:</p> <p>mother -> A | B mother -> C | D</p> <p>The first col is the fact name and the second is the value (here a tuple).</p> <p>If I want to search: "who is the mother of B" --> I search for mother, and look for value, I find B, I look in the tuple and discover A!</p> <p>Well it seems working. But facts are easy to implement. What about rules? For example rule parents:</p> <pre><code>parents(A, B, C) :- mother(A, C), father (B, C) </code></pre> <p>In F# using my syntax? Well I came up with this idea:</p> <pre><code>rule parents: A, B, C =&gt; mother A C and father B C </code></pre> <p>When my parser encounters a rule, what should it do? I would like to create some sort of record in a table like I did and be able, later, to make queries in order to specify a subject and get its parents or specify a father and get all sons and so on... What would you do?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload