<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>A.I. programming in Prolog and Assembler</title>
	<atom:link href="http://prologsource.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://prologsource.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 02 Jun 2011 12:41:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='prologsource.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>A.I. programming in Prolog and Assembler</title>
		<link>http://prologsource.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://prologsource.wordpress.com/osd.xml" title="A.I. programming in Prolog and Assembler" />
	<atom:link rel='hub' href='http://prologsource.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ultra-Fast Hybrid Genetic Algorithm in Assembly Language for the Travelling Salesman Problem (DLL for LPA Prolog)</title>
		<link>http://prologsource.wordpress.com/2009/05/08/ultra-fast-hybrid-genetic-algorithm-in-assembly-language-for-the-travelling-salesman-problem-dll-for-lpa-prolog/</link>
		<comments>http://prologsource.wordpress.com/2009/05/08/ultra-fast-hybrid-genetic-algorithm-in-assembly-language-for-the-travelling-salesman-problem-dll-for-lpa-prolog/#comments</comments>
		<pubDate>Fri, 08 May 2009 16:51:35 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Logic Programming]]></category>
		<category><![CDATA[LPA Win-Prolog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[DNA]]></category>
		<category><![CDATA[Dynamic-link library]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Genetic]]></category>
		<category><![CDATA[Genetic Programming]]></category>
		<category><![CDATA[Implementations]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Prolog]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/?p=84</guid>
		<description><![CDATA[UPDATE (8 Oct. 2009): new HD YouTube video about this project: You can also watch a (slightly dated) slide-show about this algorithm here: http://omadeon.com/tsp You don&#8217;t need PowerPoint to see this, but the PowerPoint presentation can also be downloaded here: http://omadeon.com/tsp/ogtsp.ppt I am currently using this algorithm&#8217;s DLL inside a large-scale Scheduling Application for Multiple [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=84&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3><span style="color:#ff0000;">UPDATE</span> (8 Oct. 2009): new HD <span style="color:#ff0000;">YouTube video</span> about this project:</h3>
<p style="text-align:center;"><span style="text-align:center; display: block;"><a href="http://prologsource.wordpress.com/2009/05/08/ultra-fast-hybrid-genetic-algorithm-in-assembly-language-for-the-travelling-salesman-problem-dll-for-lpa-prolog/"><img src="http://img.youtube.com/vi/HvE5Ue971Cs/2.jpg" alt="" /></a></span></p>
<h3>You can also watch a (slightly dated) slide-show about this algorithm here:</h3>
<p><a href="http://omadeon.com/tsp/index.html"><strong>http://omadeon.com/tsp</strong></a></p>
<ul>
<li>
<h4>You don&#8217;t need <a class="zem_slink" title="Microsoft PowerPoint" rel="wikipedia" href="http://en.wikipedia.org/wiki/Microsoft_PowerPoint">PowerPoint</a> to see this, but the PowerPoint presentation can also be downloaded here: <a href="http://omadeon.com/tsp/ogtsp.ppt"><strong>http://omadeon.com/tsp/ogtsp.ppt</strong></a></h4>
</li>
</ul>
<p>I am currently using this algorithm&#8217;s <strong><a class="zem_slink" title="Dynamic-link library" rel="wikipedia" href="http://en.wikipedia.org/wiki/Dynamic-link_library">DLL</a></strong> inside a large-scale <strong><a class="zem_slink" title="Scheduling (computing)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Scheduling_%28computing%29">Scheduling</a> Application</strong> for <strong>Multiple Tasks</strong>, in <a href="http://www.lpa.co.uk"><strong>LPA WIN-Prolog</strong></a>. Some screen-shots from the program&#8217;s main window are shown in the following <strong><a class="zem_slink" title="Graphics Interchange Format" rel="wikipedia" href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format">animated GIF</a>:</strong></p>
<div id="attachment_102" class="wp-caption aligncenter" style="width: 450px"><img class="size-full wp-image-102" title="xscr2" src="http://prologsource.files.wordpress.com/2009/05/xscr2.gif?w=440&#038;h=503" alt="O.G.T.S.P. Algorithm sample screen-shots" width="440" height="503" /><p class="wp-caption-text">O.G.T.S.P. Algorithm sample screen-shots</p></div>
<h6 class="zemanta-related-title" style="font-size:1em;">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://prologsource.wordpress.com/2009/02/15/assembly-language-for-visual-prolog-meta-programming/">Assembly Language for Visual Prolog Meta-programming</a> (prologsource.wordpress.com)</li>
<li class="zemanta-article-ul-li"><a href="http://blog.dbtune.org/post/2009/01/29/Prolog-message-queue">Prolog message queue</a> (dbtune.org)</li>
<li class="zemanta-article-ul-li"><a href="http://propella.blogspot.com/2009/04/prolog-in-haskell.html"> TAKASHI&#8217;S WORKSPACE: A Prolog In Haskell </a> (propella.blogspot.com)</li>
<li class="zemanta-article-ul-li"><a href="http://www.5min.com/Video/Introduction-of-the-Microsoft-Office-PowerPoint-2007-80717125?sid=247"> Introduction of the Microsoft Office PowerPoint 2007 </a> (5min.com)</li>
</ul>
<div class="zemanta-pixie" style="margin-top:10px;height:15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/035bb6e7-10ce-44c3-a2d8-11e68f44bd58/"><img class="zemanta-pixie-img" style="border:medium none;float:right;" src="http://img.zemanta.com/reblog_e.png?x-id=035bb6e7-10ce-44c3-a2d8-11e68f44bd58" alt="Reblog this post [with Zemanta]" /></a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=84&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2009/05/08/ultra-fast-hybrid-genetic-algorithm-in-assembly-language-for-the-travelling-salesman-problem-dll-for-lpa-prolog/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>

		<media:content url="http://prologsource.files.wordpress.com/2009/05/xscr2.gif" medium="image">
			<media:title type="html">xscr2</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/reblog_e.png?x-id=035bb6e7-10ce-44c3-a2d8-11e68f44bd58" medium="image">
			<media:title type="html">Reblog this post [with Zemanta]</media:title>
		</media:content>
	</item>
		<item>
		<title>Assembly Language for Visual Prolog Meta-programming</title>
		<link>http://prologsource.wordpress.com/2009/02/15/assembly-language-for-visual-prolog-meta-programming/</link>
		<comments>http://prologsource.wordpress.com/2009/02/15/assembly-language-for-visual-prolog-meta-programming/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 15:09:36 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[ASM for Visual Prolog]]></category>
		<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[Visual Prolog]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2009/02/15/assembly-language-for-visual-prolog-meta-programming/</guid>
		<description><![CDATA[(This is an experimental repost &#8211; for testing purposes, using  Scribefire. Original post is here). Back in 2005, while working in large-scale programming projects for data-mining in G.I.S. and Hydrology, I wrote a Prolog interpreter called &#8220;G.I.S. Prolog&#8220;, equipped with many extra predicates (such as functions to locate points inside polygons, etc). The G.I.S. Prolog [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=79&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>(This is an experimental repost &#8211; for testing purposes, using  <strong>Scribefire</strong>. Original post is <a href="http://prologsource.wordpress.com/2008/07/20/asm-for-visual-prolog-meta-programming/">here</a>).<span id="more-79"></span></p>
<div>
<div>Back in <strong>2005</strong>, while working in large-scale programming projects for <a href="http://omadeon.com/alc" target="_blank"><strong>data-mining</strong></a> in <strong>G.I.S.</strong> and Hydrology, I wrote a <strong>Prolog interpreter</strong> called <strong>&#8220;<a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a>&#8220;</strong>, equipped with many extra predicates (such as functions to locate points inside polygons, etc).</div>
<p>The <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog interpreter</a></strong> was originally based on  the &#8220;<strong>PIE interpreter</strong>&#8221; (included as <em>free source-code</em> in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong>) but it ended up <em>enhanced</em> with many extra predicates, as well as an improved core-level <em>inference mechanism.</em></div>
<div>Ever since I started using the <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong> compilers (and the <strong>PDC Prolog</strong> compilers preceding them) I was fascinated by the possibilities of implementing additional <strong>ISO-Prolog functionality</strong> in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong> through <strong>Assembly Language</strong> and <strong>&#8216;C&#8217;</strong>. Of course, such attempts are inherently <em>limited</em> by the <em>internal design</em> of <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong> compilers. So, the only way to implement ISO-Prolog functionality in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog </strong></a></strong>is to extend the &#8220;<strong>PIE Interpreter</strong>&#8221; (and <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> as its offspring).</p>
<p>A multitude of extra predicates, implemented in pure <strong>Assembly language</strong>, became available through <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> for easy <em>immediate experimentation:</em> Coding in <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> produced <em>immediate</em> results, without any need for (often tedious) <em>EXE-file</em> compilation. Code modifications could therefore be done very quickly and most mistakes were (semi-)automatically <em>corrected</em> by the interpreter&#8217;s own <em>enhanced</em> error-checking capabilities.</div>
<div>Recently, I discovered some <strong>Assembly language techniques</strong> to enhance <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> even further, potentially valuable for a multitude of other purposes. They also have an intrinsic <em>fascination </em>in themselves, as <em>general tools for <strong>Prolog meta-programming.</strong></em></div>
<div>E.g. here is an <strong>Assembly language predicate</strong>, that takes as inputs another (external) predicate&#8217;s memory-address and a (Visual Prolog-) argument-list, and calls <em>this </em>(external) predicate, using the (arbitrary-length-) list of N arguments, as <em>arguments of <strong>&#8220;arity N&#8221;:</strong></em></div>
<p><strong><br />
</strong><span style="font-family:Courier New;"><strong><em>apply_func(PRED, [Arg,Arg2,...]) &lt;=&gt; PRED(Arg1,Arg2,&#8230;)</em></strong><strong></strong></span></p>
<div>Now, in <strong>ISO-Prolog</strong> there is a standard predicate known as <strong>&#8220;univ&#8221;</strong>, written as &#8220;<strong>=..</strong>&#8220;, which turns a list like <span style="font-family:Courier New;"><strong>[PRED, ARG1, ARG2, ARG3...]</strong></span> into a predicate call such as <span style="font-family:Courier New;"><strong>PRED(ARG1,ARG2,&#8230;).</strong></span> However, this does <em>not </em>exist in Visual Prolog, which sacrifices such &#8220;luxuries&#8221; for speed (which is  the reason I also often use <strong>ISO-Prolog compilers</strong>, such as <a href="http://lpa.co.uk" target="_blank"><strong>LPA-WinProlog</strong></a> and <a href="http://www.swi-prolog.com" target="_blank"><strong>SWI-Prolog</strong></a>).</div>
<div>Anyway&#8230; The code you are about to see can be useful <em>more generally,</em> as an example of <strong>Prolog meta-programming implemented in Assembly Language. </strong>The <em>only</em> difference between the way it works for Visual Prolog and the way it might work for another Prolog (or -indeed- ANY programming language, using a &#8216;C&#8217;-calling convention) is the Visual-Prolog-specific structure of a LIST, which in Visual Prolog has a  different form than in all other languages. If you understand Assembly Language and intend to use this code for other (meta-programming) tasks, all you have to do is modify just a couple of lines in the code that follows. However, before you (even begin to) look at the Assembly Language Code, the following simple definitions in Visual Prolog (5.*) are a prerequisite for easier understanding:</div>
<p><strong>GLOBAL DOMAINS<br />
dom_iii = determ INTEGER (INTEGER,INTEGER,INTEGER) -(i,i,i) language c % &lt;&#8211; example domain</strong></p>
<p><strong>GLOBAL PREDICATES<br />
apply_func(DWORD,ListDomain) -(i,i) language c  % where arg-1 is a predicate-domain, such as &#8220;dom_iii&#8221;<br />
</strong></p>
<div>After you compile the Assembly language code, you could create a simple &#8220;EasyWin&#8221; Visual Prolog project, with the following ines:</div>
<p><strong>PREDICATES<br />
func2dword(dom_iii,DWORD) % converts a predicate to a doubleword memory-address<br />
</strong><br />
<strong>CLAUSES<br />
func2dword(FUNC,DW):- DW = cast(dword,FUNC).</strong></p>
<p><strong>GOAL<br />
func2dword(add3,DW),<br />
Out = apply_func(DW,[10,20,30]),<br />
write(&#8220;result = &#8220;,Out), nl, readchar(_).</strong></p>
<p><strong></strong>OK, so here is the Assembly language code:</p>
<p><span style="font-family:Courier New;">; ======================= _apply_func.asm ==========================<br />
; Code for TASM 5 Assembler, command-line call for compilation:<br />
; C:\TASM\BIN\TASM32.EXE /p /z /w2 /m2 /ml _apply_func.asm</span></p>
<p><span style="font-family:Courier New;">IDEAL<br />
P586<br />
MODEL    flat<br />
CODESEG<br />
ALIGN 4</span></p>
<p><span style="font-family:Courier New;"> public _apply_func    ; (i,i)</span></p>
<p><span style="font-family:Courier New;">PROC _apply_func near<br />
ARG    func:dword, list:dword<br />
LOCAL    fcnt:dword<br />
ENTER 4,0<br />
push    esi        ;<br />
push    edi        ;<br />
push    ebx        ;<br />
mov    ecx,[func]  ; function&#8230;&#8230;&#8230;&#8230; ARG 1<br />
mov    esi,[list]  ; list&#8230;&#8230;&#8230;&#8230;&#8230;. ARG 2<br />
xor    ebx,ebx     ; make EBX=0<br />
mov    [fcnt],ebx  ; initialize local variable &#8216;fcnt&#8217; to 0<br />
lodsd              ; load the first list-element&#8217;s &#8220;element-flag&#8221;<br />
dec    al          ; decrement it, to check if it was equal to 1<br />
jnz short @@x1     ; exit if not so (i.e. if it was the end of the list)<br />
; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- else&#8230;<br />
@@L1:                  ; loop to read the (Visual-Prolog-) argument-list<br />
inc    ebx        ; increment ebx (counter for number_of_arguments)<br />
lodsd             ; load next list-element (argument of function)<br />
push    eax       ; push it into the stack (for later function-call)<br />
lodsd             ; load the pointer to the next list-element in EAX<br />
mov    esi,eax    ; now ESI = (pointer-to-) next list-element<br />
lodsd             ; load element-flag of next list-element<br />
dec    al         ; decrement it, to check if it was a 1<br />
jz short @@L1     ; if so, not yet the end of the list, so repeat!<br />
; ====================== else&#8230;<br />
@@x1:<br />
call   ecx         ; call the (external) function (given in ARG-1)<br />
mov    ecx,[fcnt]  ; get the function&#8217;s number of arguments from fcnt<br />
@@L2:<br />
jcxz @@x2          ; if the called function had NO arguments, exit<br />
; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- else&#8230;<br />
@@L3:                  ; loop to POP function-arguments, after the call<br />
pop        edx     ; recover next argument from the stack<br />
mov    ecx,[fcnt]  ; recover remaining arguments_counter<br />
dec        ecx     ; decrement the remaining number_of_arguments<br />
mov    [fcnt],ecx  ; store it again, in the local variable fcnt<br />
jnz short @@L3     ; if not zeroed, continue popping arguments&#8230;<br />
; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
@@x2:<br />
pop        ebx     ;<br />
pop        edi     ;<br />
pop        esi     ;<br />
LEAVE<br />
ENDP _apply_func</span></p>
<p><span style="font-family:Courier New;"> end</span></p>
<div class="zemanta-pixie" style="margin-top:10px;height:15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/1551b7a9-c32b-4e92-80c2-0e2a6ccdd52f/"><img class="zemanta-pixie-img" style="border:medium none;float:right;" src="http://img.zemanta.com/reblog_e.png?x-id=1551b7a9-c32b-4e92-80c2-0e2a6ccdd52f" alt="Reblog this post [with Zemanta]" /></a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/79/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/79/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/79/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=79&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2009/02/15/assembly-language-for-visual-prolog-meta-programming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/reblog_e.png?x-id=1551b7a9-c32b-4e92-80c2-0e2a6ccdd52f" medium="image">
			<media:title type="html">Reblog this post [with Zemanta]</media:title>
		</media:content>
	</item>
		<item>
		<title>Assembly Language for Visual Prolog Meta-programming</title>
		<link>http://prologsource.wordpress.com/2008/07/20/asm-for-visual-prolog-meta-programming/</link>
		<comments>http://prologsource.wordpress.com/2008/07/20/asm-for-visual-prolog-meta-programming/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 10:56:25 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[ASM for Visual Prolog]]></category>
		<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Prolog compilers]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[Visual Prolog]]></category>
		<category><![CDATA[Compiler]]></category>
		<category><![CDATA[Implementations]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/?p=18</guid>
		<description><![CDATA[Image via Wikipedia Back in 2005, while working in large-scale programming projects for data-mining in G.I.S. and Hydrology, I wrote a Prolog interpreter called &#8220;G.I.S. Prolog&#8220;, equipped with many extra predicates (such as functions to locate points inside polygons, etc).The G.I.S. Prolog interpreter was originally based on the &#8220;PIE interpreter&#8221; (included as free source-code in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=18&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="float:right;display:block;margin:1em;"><a href="http://en.wikipedia.org/wiki/Image:Visual_Prolog_IDE_Screenshot.png"><img style="border:medium none;display:block;" src="http://upload.wikimedia.org/wikipedia/en/thumb/8/84/Visual_Prolog_IDE_Screenshot.png/202px-Visual_Prolog_IDE_Screenshot.png" alt="Visual Prolog Integrated..." /></a><span class="zemanta-img-attribution">Image via <a href="http://en.wikipedia.org/wiki/Image:Visual_Prolog_IDE_Screenshot.png">Wikipedia</a></span></div>
<div>Back in <strong>2005</strong>, while working in large-scale programming projects for <a href="http://omadeon.com/alc" target="_blank"><strong>data-mining</strong></a> in <strong>G.I.S.</strong> and Hydrology, I wrote a <strong><a class="zem_slink" title="Prolog" rel="wikipedia" href="http://en.wikipedia.org/wiki/Prolog">Prolog</a> interpreter</strong> called <strong>&#8220;<a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a>&#8220;</strong>, equipped with many extra predicates (such as functions to locate points inside polygons, etc).The <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog interpreter</a></strong> was originally based on  the &#8220;<strong>PIE interpreter</strong>&#8221; (included as <em>free <a class="zem_slink" title="Source code" rel="wikipedia" href="http://en.wikipedia.org/wiki/Source_code">source-code</a></em> in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong>) but it ended up <em>enhanced</em> with many extra predicates, as well as an improved core-level <em>inference mechanism.</em></div>
<div style="text-align:justify;">Ever since I started using the <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong> <a class="zem_slink" title="Compiler" rel="wikipedia" href="http://en.wikipedia.org/wiki/Compiler">compilers</a> (and the <strong>PDC Prolog</strong> compilers preceding them) I was fascinated by the possibilities of implementing additional <strong>ISO-Prolog functionality</strong> in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a> </strong>through <strong><a class="zem_slink" title="Assembly language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Assembly_language">Assembly Language</a></strong> and <strong>&#8216;C&#8217;</strong>. Of course, such attempts are inherently <em>limited</em> by the <em>internal design</em> of <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a></strong> compilers. So, the only way to implement ISO-Prolog functionality in <strong><a href="http://www.visual-prolog.com/" target="_blank"><strong>Visual Prolog</strong></a> </strong>is to extend the &#8220;<strong>PIE Interpreter</strong>&#8221; (and <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> as its offspring). A multitude of extra predicates, implemented in pure <strong>Assembly language</strong>, became available through <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> for easy <em>immediate experimentation:</em> Coding in <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> produced <em>immediate</em> results, without any need for (often tedious) <em><a class="zem_slink" title="EXE" rel="wikipedia" href="http://en.wikipedia.org/wiki/EXE">EXE-file</a></em> compilation. Code modifications could therefore be done very quickly and most mistakes were (semi-)automatically <em>corrected</em> by the interpreter&#8217;s own <em>enhanced</em> error-checking capabilities.</div>
<p style="text-align:justify;">Recently, I discovered some <strong>Assembly language techniques</strong> to enhance <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong> even further, potentially valuable for a multitude of other purposes. They also have an intrinsic <em>fascination </em>in themselves, as <em>general tools for <strong>Prolog <a class="zem_slink" title="Metaprogramming" rel="wikipedia" href="http://en.wikipedia.org/wiki/Metaprogramming">meta-programming</a>.</strong></em></p>
<p style="text-align:justify;">E.g. here is an <strong>Assembly language predicate</strong>, that takes as inputs another (external) predicate&#8217;s memory-address and a (<a class="zem_slink" title="Visual Prolog" rel="homepage" href="http://www.visual-prolog.com/">Visual Prolog</a>-) argument-list, and calls <em>this </em>(external) predicate, using the (arbitrary-length-) list of N arguments, as <em>arguments of <strong>&#8220;arity N&#8221;:</strong></em><br />
<strong><br />
</strong><span style="font-family:Courier New;"><span style="color:#ff0000;"><strong><em>apply_func(PRED, [Arg,Arg2,...]) <span style="color:#000000;">&lt;=&gt; </span>PRED(Arg1,Arg2,&#8230;)</em></strong></span></span></p>
<p style="text-align:justify;">Now, in <strong>ISO-Prolog</strong> there is a standard predicate known as <strong>&#8220;univ&#8221;</strong>, written as &#8220;<strong>=..</strong>&#8220;, which turns a list like <span style="font-family:Courier New;"><strong>[PRED,ARG1,ARG2,ARG3...]</strong></span> into a predicate call such as <span style="font-family:Courier New;"><strong>PRED(ARG1,ARG2,&#8230;).</strong></span> However, this does <em>not </em>exist in Visual Prolog, which sacrifices such &#8220;luxuries&#8221; for speed (which is  the reason I also often use <strong>ISO-Prolog compilers</strong>, such as <a href="http://lpa.co.uk" target="_blank"><strong>LPA-WinProlog</strong></a> and <a href="http://www.swi-prolog.com" target="_blank"><strong>SWI-Prolog</strong></a>).</p>
<p style="text-align:justify;">Anyway&#8230; The code you are about to see can be useful <em>more generally,</em> as an example of <strong>Prolog meta-programming, implemented in Assembly Language. </strong>The <em>only</em> difference between the way it works for Visual Prolog and the way it might work for another Prolog (or -indeed- ANY <a class="zem_slink" title="Programming language" rel="wikipedia" href="http://en.wikipedia.org/wiki/Programming_language">programming language</a>, using a &#8216;C&#8217;-calling convention) is the Visual-Prolog-specific structure of a LIST, which in Visual Prolog has a  different form than in all other languages. If you understand Assembly Language and intend to use this code for other (meta-programming) tasks, all you have to do is modify just a couple of lines in the code that follows. However, before you (even begin to) look at the Assembly Language Code, the following simple definitions in Visual Prolog (5.*) are a prerequisite for easier understanding:</p>
<pre><strong>GLOBAL DOMAINS
dom_iii = determ INTEGER (INTEGER,INTEGER,INTEGER)
</strong><strong><strong> 	</strong></strong><strong>-(i,i,i) language c </strong>% &lt;-- example domain</pre>
<pre><strong>GLOBAL PREDICATES
apply_func(DWORD,ListDomain) -(i,i) language c
</strong>% where arg-1 is a predicate-domain, such as "dom_iii"</pre>
<p>After you compile the Assembly language code, you could create a simple &#8220;EasyWin&#8221; Visual Prolog project, with the following ines:</p>
<pre><strong>PREDICATES
 func2dword(dom_iii,DWORD) </strong>
 % converts a predicate to a doubleword/address<strong>

 add3: dom_iii</strong></pre>
<pre><strong>CLAUSES
func2dword(FUNC,DW):- DW = cast(dword,FUNC).

add3(_X,_Y,_Z,Out):- Out = _X+_Y+_Z,
</strong><strong><strong> 	</strong></strong><strong>write("out = ",Out), nl, !
 	;
 	write("error!\n"), Out=-1, !.</strong></pre>
<pre><strong>GOAL
func2dword(add3,DW),
Out = apply_func(DW,[10,20,30]),
write("result = ",Out), nl, readchar(_).

</strong>%This program should produce "<strong>result = 60</strong>" (sum of [10,20,30]).<strong></strong></pre>
<p>OK, so here is the <strong><span style="color:#ff0000;">Assembly language code</span>:</strong></p>
<pre><span style="color:#000080;">; ==================== _apply_func.asm =====================
; Code for TASM 5 Assembler, command-line call for compilation:
; C:\TASM\BIN\TASM32.EXE /p /z /w2 /m2 /ml _apply_func.asm</span></pre>
<pre><span style="color:#000080;">IDEAL
P586
MODEL    flat
CODESEG
ALIGN 4</span></pre>
<pre><span style="color:#000080;">public _apply_func    ; (i,i)</span></pre>
<pre><span style="color:#000080;">PROC _apply_func near
ARG    func:dword, list:dword
LOCAL    fcnt:dword
ENTER 4,0
push    esi        ;
push    edi        ;
push    ebx        ;
push</span><span><span style="color:#000080;">    ecx        ;
</span></span><span style="color:#000080;">mov    ecx,[func]  ; function............ ARG 1
mov    esi,[list]  ; list................ ARG 2
xor    ebx,ebx     ; make EBX=0
mov    [fcnt],ebx  ; initialize local variable 'fcnt' to 0
lodsd              ; load the 1st list-element's "element-flag"
dec    al          ; decrement it, to check if it was a 1
jnz short @@x1     ; exit if not (i.e. if it's the list's end)
; ----------------- else...
@@L1:             ; loop to read the (Visual-Prolog-) arg-list
inc    ebx        ; increment ebx (counter for number_of_args)
lodsd             ; load next list-element (arg. of function)
push    eax       ; push it into the stack (for a function-call)
lodsd             ; load the pointer to next list-element in EAX
mov    esi,eax    ; now ESI = (pointer-to-) next list-element
lodsd             ; load element-flag of next list-element
dec    al         ; decrement it, to check if it was a 1
jz short @@L1     ; if so, not yet the list's end, so repeat!
; ================= else...
@@x1:
mov</span><span><span style="color:#000080;">    [fcnt],ebx</span></span><span><span style="color:#000080;">  ; store the number_of_args in local var. 'fcnt'
</span></span><span style="color:#000080;">call   ecx         ; call the (external) function (given in ARG-1)
mov    ecx,[fcnt]  ; get the function's number of args from 'fcnt'
@@L2:
jcxz @@x2          ; if the called function had NO args, exit
; ------------------ else...
@@L3:              ; loop to POP function-args after the call
pop        edx     ; recover next argument from the stack
dec        ecx     ; decrement the remaining number_of_args
jnz short @@L3     ; if not zeroed, continue popping args...
; ------------------
@@x2:
</span><span><span style="color:#000080;">pop        ecx     ;
</span></span><span style="color:#000080;">pop        ebx     ;
pop        edi     ;
pop        esi     ;
LEAVE
ENDP _apply_func</span></pre>
<pre><span style="color:#000080;">end

</span></pre>
<p><span style="color:#ff0000;"><strong><span>NOTES:</span></strong></span></p>
<ul>
<li><span></span> A <em>not-so-obvious </em>advantage of this code is that <em>any </em>Prolog interpeters written on the basis of Visual Prolog&#8217;s &#8220;PIE engine&#8221; (such as <strong><a href="http://omadeon.com/gis_prolog" target="_blank">G.I.S. Prolog</a></strong>) make extensive use of calls such as this, inside their inference engine; using Prolog-lists of arguments to be called by turning them into proper predicate calls of arity=N (where N is the size of the list). So, an <strong>Assembly language implementation</strong> of such a <em>calling mechanism </em>can <em>speed up</em> such an interpreter considerably, especially inside <em>recursive calls </em>or <em>loops,</em> which call <em>other predicates</em> repeatedly countless times&#8230;</li>
<li>Another <em>not-so-obvious</em> advantage is that -in this way- we managed to&#8230;<em> trick </em>Visual Prolog into doing &#8220;forbidden&#8221; predicate calls, such as <strong>PRED(arg1,arg2,&#8230;.), </strong>where both the predicate&#8217;s functor and the arguments may appear as static data, stored in a <strong>Visual Prolog <em>facts&#8217; database.</em></strong></li>
<li>Don&#8217;t ask me (<em>yet</em>) how to implement such <em>tricks</em> in Visual Prolog 6.* or 7.*; I still use the version 5.* compilers a lot, because of their speed, as well as robustness in <em>foreign language calls.</em><strong><em><br />
</em></strong></li>
</ul>
<h6 class="zemanta-related-title" style="font-size:1em;">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://lifehacker.com/398611/how-to-compile-software-from-source-code">How to Compile Software From Source Code [Command Line]</a></li>
<li class="zemanta-article-ul-li"><a href="http://hardware.slashdot.org/article.pl?sid=08/07/02/1833221&amp;from=rss">Intel Says to Prepare For &#8220;Thousands of Cores&#8221;</a></li>
<li class="zemanta-article-ul-li"><a href="http://www.computerworld.com/action/article.do?command=viewArticleBasic&amp;articleId=314087&amp;source=rss_topic18">Back to Basics: Algorithms</a></li>
<li class="zemanta-article-ul-li"><a href="http://morepypy.blogspot.com/2008/06/hi-all-some-news-from-jit-front.html">JIT in Prolog</a></li>
<li class="zemanta-article-ul-li"><a href="http://www.paulgraham.com/icad.html">Revenge of the Nerds</a></li>
<li class="zemanta-article-ul-li"><a href="http://www.regdeveloper.co.uk/2008/01/16/concept_programming/">Dip into concept programming</a></li>
<li class="zemanta-article-ul-li"><a href="http://blogoscoped.com/archive/2008-06-12-n48.html">Google Code Search Shows Function Outline</a></li>
<li class="zemanta-article-ul-li"><a href="http://www.hackaday.com/2008/07/14/ard-e-cheap-arduino-robot-platform/">Ard-e: Cheap Arduino robot platform</a></li>
<li class="zemanta-article-ul-li"><a href="http://www.webmonkey.com/tutorial/Get_Started_With_Movable_Type">Get Started With Movable Type</a></li>
</ul>
<div class="zemanta-pixie" style="margin-top:10px;height:15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/3527d736-d818-4bb2-96b9-2aba73d5d361/"><img class="zemanta-pixie-img" style="border:medium none;float:right;" src="http://img.zemanta.com/reblog_e.png?x-id=3527d736-d818-4bb2-96b9-2aba73d5d361" alt="Zemanta Pixie" /></a></div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=18&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2008/07/20/asm-for-visual-prolog-meta-programming/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/wikipedia/en/thumb/8/84/Visual_Prolog_IDE_Screenshot.png/202px-Visual_Prolog_IDE_Screenshot.png" medium="image">
			<media:title type="html">Visual Prolog Integrated...</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/reblog_e.png?x-id=3527d736-d818-4bb2-96b9-2aba73d5d361" medium="image">
			<media:title type="html">Zemanta Pixie</media:title>
		</media:content>
	</item>
		<item>
		<title>DreamProver: A visual theorem prover for &#8220;Multiple Form Logic&#8221; (etc.) in LPA Win-Prolog 4.6</title>
		<link>http://prologsource.wordpress.com/2007/10/14/dreamprover/</link>
		<comments>http://prologsource.wordpress.com/2007/10/14/dreamprover/#comments</comments>
		<pubDate>Sun, 14 Oct 2007 23:46:13 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[Formal Logic]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Logic Programming]]></category>
		<category><![CDATA[LPA Win-Prolog]]></category>
		<category><![CDATA[Multiple Form Logic Theory And Research]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Prolog compilers]]></category>
		<category><![CDATA[Theorem Proving Software]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[Boolean Algebra]]></category>
		<category><![CDATA[theorem proving]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/10/14/dreamprover/</guid>
		<description><![CDATA[Image via Wikipedia Visual DreamProver 1.0 is a new theorem-proving program, developed in LPA Win-Prolog 4.6, with multi-coloured graphics displays of (potentially unlimited) Logic expressions, theorem proofs and deductions in Multiple Form Logic, in the primary algebra of &#8220;Laws of Form&#8220;, in Boolean Algebra and in a variety of other logic systems (to a large [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=8&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="float:right;display:block;margin:1em;"><a href="http://commons.wikipedia.org/wiki/Image:Develstages.svg"><img style="border:medium none;display:block;" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Develstages.svg/202px-Develstages.svg.png" alt="Chart showing the stages in the software relea..." /></a><span class="zemanta-img-attribution">Image via <a href="http://commons.wikipedia.org/wiki/Image:Develstages.svg">Wikipedia</a></span></div>
<p align="justify"><strong><span style="color:#ff0000;">Visual DreamProver 1.0 </span>is a new theorem-proving program, developed in LPA Win-Prolog 4.6, with multi-coloured graphics displays of (potentially unlimited) Logic expressions, theorem proofs and deductions in <a title="The Multiple Form Logic site" href="http://omadeon.com/logic" target="_blank">Multiple Form Logic</a>, in the primary algebra of &#8220;<span style="text-decoration:underline;"><a title="OMADEON bookmarks about Laws of Form" href="http://del.icio.us/omadeon/LoF" target="_blank">Laws of Form</a></span>&#8220;, in <a class="zem_slink" title="Boolean algebra (structure)" rel="wikipedia" href="http://en.wikipedia.org/wiki/Boolean_algebra_%28structure%29">Boolean Algebra</a> and in a variety of other <a class="zem_slink" title="Formal system" rel="wikipedia" href="http://en.wikipedia.org/wiki/Formal_system">logic systems</a> (to a large extent used-defined). Here is an <a class="zem_slink" title="Graphics Interchange Format" rel="wikipedia" href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format">animated GIF</a> slide-show of DreamProver&#8217;s visual display. It offers  unlimited control of size, colour, shape and content for all Logic Expressions and all theorem proofs:</strong></p>
<p style="text-align:center;"><a title="LARGER DISPLAY (800 * 627 pixel slide-show)" href="http://prologsource.files.wordpress.com/2007/10/dreamprover800x627.gif"><img src="http://prologsource.files.wordpress.com/2007/10/dreamprover430x.gif?w=468" alt="dreamprover430x.gif" /></a></p>
<p align="center"><span style="color:#ff0000;"><strong>(Click on this image for a <em>better quality animated GIF</em>, of size 450Kb)</strong></span></p>
<p align="justify"><strong>Although DreamProver is still at the &#8220;<a class="zem_slink" title="Software release life cycle" rel="wikipedia" href="http://en.wikipedia.org/wiki/Software_release_life_cycle">alpha stage</a>&#8220;, I decided to publish a preliminary first report about its features and capabilities, to a large extent already working, to a lesser extent requiring minor debugging and final extensions, before release. I am also doing this for the benefit (and amusement) of a friendly innov</strong><strong>ative company: &#8220;<a class="zem_slink" title="Logic Programming Associates" rel="wikipedia" href="http://en.wikipedia.org/wiki/Logic_Programming_Associates">Logic Programming Associates</a> Ltd&#8221;, where I worked for a short pleasant  period of a few weeks, some years ago (in 2001). LPA are the creators of the LPA Win-Prolog compiler. I hope that LPA continues a long tradition of innovative success through the latest version of their compiler, which also has MIDI (music) programming capabilities (featured in a <span style="text-decoration:underline;"><a title="LPA Win-Prolog (blog article)" href="http://prologsource.wordpress.com/2007/09/22/lpaprolog/">recent posting, here</a></span>). </strong></p>
<p align="justify"><strong>I am also&#8230; officially requesting, after the release of DreamProver (and the ensuing free promotion of  LPA&#8217;s amazing compiler) a small&#8230; personal favour: -A legitimate free copy of their newest LPA Win-Prolog 4.7 compiler!  <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </strong><em> (as my license for using version 4.6 ends on the last day of 2007).</em></p>
<p align="justify">DreamProver is particularly suited for the display of so-called &#8220;Boundary Logic Systems&#8221; (first created by <a class="zem_slink" title="G. Spencer-Brown" rel="wikipedia" href="http://en.wikipedia.org/wiki/G._Spencer-Brown">George Spencer Brown</a> in &#8220;Laws of Form&#8221; and then extended by various people in various ways &#8211; including my own &#8220;Multiple Form Logic&#8221; system). However, its (almost unlimited) potential allows the display of many other logic systems, including Parse-Trees of used-defined grammars, since both the shapes and the data-structures they represent can be redefined &#8220;on the fly&#8221;. In the display shown above, only a small example of a logic expression is used, mainly to demonstrate graphics capabilities. However, if -for example- the Grammar of a subset of English is used, instead of a Logic Expression, the ensuing graphic display of coloured shapes resembles a tree which is symmetric with respect to a &#8220;horizon&#8221; line in the middle.</p>
<p align="justify">The <a class="zem_slink" title="Data structure" rel="wikipedia" href="http://en.wikipedia.org/wiki/Data_structure">data-structure</a> for this unusual kind of tree-representation is relatively simple, straight-forward and documented (in the final release of DreamProver). It is separate from the internal Logic representation but related to it through specific user-defined rules: Both the &#8220;productions&#8221; and the &#8220;leaves&#8221; of such a grammar tree are user-defined in shape and content. The only difference between other kinds of systems and those built-in (as regards the current first version of DreamProver) is that the other systems do not include internal Proof Algorithms and automated deductions, and can only be fed from the results of such processes (through external third-party software). Before final release it is hoped that the input-expressions in other systems are expressed in standard <a class="zem_slink" title="XML" rel="wikipedia" href="http://en.wikipedia.org/wiki/XML">XML</a>, so as to make the software useful to almost any researcher or developer, in <em>any</em> topic that includes parsed tree-expressions. The ultimate goal is also to develop a kind of Universal DreamProver library, available under a professional license to developers for a small fee (that might help sustain this work and pay for the effort of future upgrades). However, the current version of DreamProver is likely to appear as Open Source in the near future. <strong><em>Keep in touch!</em></strong></p>
<p align="justify">
<p align="justify">
<p style="text-align:center;">
<p align="justify">
<h6 class="zemanta-related-title" style="font-size:1em;">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://wonkette.com/371601/breaking-internet-creates-obvious-political-gif">BREAKING: Internet Creates Obvious Political .GIF! [It's Like Magic!]</a></li>
</ul>
<div class="zemanta-pixie" style="margin-top:10px;height:15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/fa98e4d2-26ab-42b5-bb77-8ee07bbf7eb9/"><img class="zemanta-pixie-img" style="border:medium none;float:right;" src="http://img.zemanta.com/reblog_e.png?x-id=fa98e4d2-26ab-42b5-bb77-8ee07bbf7eb9" alt="Zemanta Pixie" /></a></div>
<p><a rel="sync" href="http://www.sync.gr/claim/f6iQji7qloYG"></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=8&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/10/14/dreamprover/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Develstages.svg/202px-Develstages.svg.png" medium="image">
			<media:title type="html">Chart showing the stages in the software relea...</media:title>
		</media:content>

		<media:content url="http://prologsource.files.wordpress.com/2007/10/dreamprover430x.gif" medium="image">
			<media:title type="html">dreamprover430x.gif</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/reblog_e.png?x-id=fa98e4d2-26ab-42b5-bb77-8ee07bbf7eb9" medium="image">
			<media:title type="html">Zemanta Pixie</media:title>
		</media:content>
	</item>
		<item>
		<title>reading/writing/sorting Prolog variables, using the original variable-names (LPA Prolog code)</title>
		<link>http://prologsource.wordpress.com/2007/09/27/varnamesort/</link>
		<comments>http://prologsource.wordpress.com/2007/09/27/varnamesort/#comments</comments>
		<pubDate>Thu, 27 Sep 2007 14:17:48 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[LPA Win-Prolog]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/09/27/varnamesort/</guid>
		<description><![CDATA[Typically, Prolog variables are assigned by the compiler &#8220;internal names&#8221;, so that it is impossible to sort them using their original names (those provided by the user or by an input file). However, in LPA Win-Prolog it is possible to preserve the original variable names, as well as sort them on the basis of these [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=7&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Typically, Prolog variables are assigned by the compiler &#8220;internal names&#8221;, so that it is  impossible to sort them using their original names (those provided by the user or by an input file).</p>
<p>However, in LPA Win-Prolog it is possible to preserve the original variable names, as well as sort them on the basis of these names. Finally it is also possible to retrieve the variables with their original names. Some examples follow:</p>
<p>1) LPA Prolog has predicates handling variable names (eread/2 and eprint/2). E.g. if you want Prolog to read a term with variables, preserving user-given variable names and then printing them out, you can use code <em>like this:</em></p>
<pre><strong>| ?- eread(EXP,Vars), nl, eprint(EXP,Vars), nl, nl, nl.</strong></pre>
<pre><strong>|: this(THING,V,X,W).</strong></pre>
<pre><strong>this(THING,V,X,W)</strong></pre>
<pre><strong>EXP = this(_38218,_38220,_38226,_38232) ,</strong></pre>
<pre><strong>Vars = [('THING',_38218),('V',_38220),('X',_38226),('W',_38232)]</strong></pre>
<pre><strong>| ?-  </strong></pre>
<pre></pre>
<p>In LPA Prolog, eread/2 reads a term with variables and the it produces a <em>list of names-and-variables</em> [(Name,Var)|...] as a second argument. This has elements like (&#8216;THING&#8217;,_38218) -in this example- where &#8216;THING&#8217; is the <em>user-defined</em> variable <em>name</em> and _38218 is an &#8220;internal variable code&#8221;, generated by the compiler. The second predicate eprint/2 is similar: If the second argument of eprint/2 is bound to the names-and-variables-list (extracted previously) then the result is a printout of the expression <em>as it was originally entered</em>, preserving <em>all the user-defined variable names. </em></p>
<p>Now, here is another example, only slightly more complicated. This time, it uses a small source-code file, which you can create easily, by copying and pasting the code that follows into a <em>text file</em> (of extension &#8216;.pl&#8217;).</p>
<p>The program asks the user to type a list (consisting of variables and constants) and then it is required to sort the variables, finally printing-out the resulting list with Variables sorted, but with names <em>exactly the same as those </em><em>typed </em><em>originally.</em> So, here is a piece of code that reads such a list from a user-prompt, sorts the list with respect to its variables, and then prints out the resulting list, with <em>all the variables sorted:</em></p>
<pre><strong><font color="#000080">test:- repeat, write('type a list of variables and constants:'), nl,</font></strong></pre>
<pre><strong><font color="#000080">    eread(List,VL), sort(VL,VLsorted), sort(List,List1), varsort(List1,VLsorted,ListOut),</font></strong></pre>
<pre><strong><font color="#000080">    write('sorted w.r.t. variables: '), eprint(ListOut,VL), nl, </font></strong></pre>
<pre><strong><font color="#000080">    write('press escape to exit, any other key to go on: '), get0(CHAR), CHAR = -1, !.</font></strong></pre>
<pre><font color="#000080"><strong>removevar(V,[A|L],L):- V == A, !.
removevar(V,[A|L],[A|Lx]):- removevar(V,L,Lx).

varsort([],_,[]):- !.
varsort(L,[],L):- !.
varsort(L,[(_Name,V)|VL],[V|XL]):-  removevar(V,L,L2), !, varsort(L2,[(_,V)|VL],XL).
varsort(L,[_|VL],XL):- !, varsort(L,VL,XL).</strong></font></pre>
<p>This piece of code is<em> complete</em>; you can copy it, paste it to a file ando compile it in LPA Win-Prolog. After doing this, type &#8216;test&#8217; at the Prolog prompt. You will get a dialogue such as <em>this:</em></p>
<pre><font color="#000080"><strong>| ?- test1.
type a list of variables and constants:
|: [A,D,C,2,B,34,X,F,D].
sorted w.r.t. variables: [A,B,C,D,F,X,2,34]
press escape to exit, any other key to go on: |:
yes</strong></font></pre>
<pre><font color="#000080"><strong>| ?-</strong></font></pre>
<p>The main predicate &#8216;test&#8217; calls &#8216;eread/2&#8242;, which reads user- (or file-) input expressions and parses them into <em>Prolog terms</em> <em>with variables</em>. Then the variable-name-list is <em>sorted</em>. The original list is also sorted, and then the sorted variable names&#8217; list is used to rearrange the sorted original list a second time, this time sorting it as regards <em>variable names.</em></p>
<p>A very crucial predicate here is &#8216;removevar/3&#8242;, which is similar to the built-in predicate &#8216;remove/3&#8242;, removing an element of a list and producing the list without this element each time it&#8217;s called. However, unlike the built-in predicate &#8216;remove/3&#8242;, this one only deletes list-elements which are &#8220;identical to the first argument&#8221;, without doing any unification. I.e. it is capable of removing variables, if they exist in the list (of arg 2) but does not confuse them with other variables through unification.</p>
<p>Another new predicate in the code above is &#8216; varsort&#8217;. It assumes that arg-1 contains an input-list (a mixture of variables and constants) and it makes use of the sorted list of variable names, to rearrange the list so that all the variable names are sorted, in the third (output-)argument:</p>
<pre><font><strong><font color="#000080"><strong>[A,D,C,2,B,34,X,F,D] becomes </strong></font></strong></font><font><strong><font color="#000080"><strong>[A,B,C,D,F,X,2,34]</strong></font></strong></font> .</pre>
<p>As an exercise, if you have some Prolog experience you can now try to write a similar piece of code that works on ANY kind of Prolog term, <em>not just a list</em>. If you do write such code, you can also send it as a comment in this posting, or an e-mail to omadeon@hotmail.com (with your name and any other details you&#8217;d like to see included), and I will publish it in this blog (<em>with</em> your name or alias) exactly as you&#8217;ve written it (provided of course that&#8230; it works <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=7&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/09/27/varnamesort/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>
	</item>
		<item>
		<title>LPA Win-Prolog: A professional Prolog compiler with unique features</title>
		<link>http://prologsource.wordpress.com/2007/09/22/lpaprolog/</link>
		<comments>http://prologsource.wordpress.com/2007/09/22/lpaprolog/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 18:38:54 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[Logic Programming]]></category>
		<category><![CDATA[LPA Win-Prolog]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Prolog compilers]]></category>
		<category><![CDATA[software reviews]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/09/22/lpaprolog/</guid>
		<description><![CDATA[In early 2001, I had the pleasure of working closely together with a friendly bunch of people, the creators of LPA Win-Prolog, for a a period of a few months: This company, who made LPA Win-Prolog is &#8220;Logic Programming Associates&#8220;, a group of dedicated developers and computer scientists led by Brian Steel, who also happens [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=6&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">In early 2001, I had the pleasure of working closely together with a friendly bunch of people, the creators of <u><a href="http://www.lpa.co.uk" title="Home Page of LPA Win-Prolog" target="_blank"><strong>LPA Win-Prolog</strong></a></u>, for a a period of a few months: This company, who made  <u><a href="http://www.lpa.co.uk" title="Home Page of LPA Win-Prolog" target="_blank"><strong>LPA Win-Prolog</strong></a></u> is &#8220;<strong>Logic Programming Associates</strong>&#8220;, a group of dedicated developers and computer scientists led by <strong>Brian Steel</strong>, who also happens to be a <em>musician</em> and an <em>orchestra conductor</em>. A long time ago (in the eighties) Brian Steel had caused quite a stir in the so-called &#8220;home computer&#8221; industry, by writing  the first Prolog compiler that could run on a <strong>ZX Spectrum</strong>, a machine with only <strong>48Kb</strong> of RAM and a <strong>Z80</strong> 8-bit processor.  At that embryonic stage of the computer industry&#8217;s evolution, it was considered impossible to cram a <em>working</em> Prolog compiler in <em>only so</em> little RAM and in <em>such a slow computer.</em> However, Brian Steel was<em> also</em> an Assembly Language programmer (just like me -although <em>long before my time</em>). He still writes very efficient <strong>Assembly Language code</strong> (today for Intel Pentiums &#8211; 32-bit and 64-bit code) which empowers <em>today&#8217;s</em> LPA compiler with a <em>tremendous speed</em>, compared to its rivals. Brian also thought deeply about the best way to implement certain commonly needed operations (such as string search) and so he set out to improve the <strong>ISO-prolog-compatible LPA compiler</strong> with  special and <em>unique</em> instructions that increase its speed and efficiency even more (e.g. the multi-faceted predicate &#8220;<strong>find/3</strong>&#8220;).</p>
<p align="justify">Well, I am <em>still</em> using <strong>LPA Win-Prolog</strong>, ever since that happy period of a few months I spent in the UK, back in 2001, as an <em>employee of LPA Ltd</em>. In fact, I was <em>forced </em>to return to Greece because of a bad accident (a broken tendon in a foot), otherwise I&#8217;d <em>rather</em> stay in the UK and work with <strong>LPA</strong>&#8230; forever! BTW, <strong>Brian Steel </strong>is <em>also</em> -like me- a <em>winter swimmer</em>, in the English sea (in Cornwall) making it even <em>more</em> apealling for me -at the time- to&#8230; <em>follow his example</em>. <img src='http://s2.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p align="justify">The <em>latest</em> versions of <strong>LPA Prolog</strong> (version 4.6 is the one I use at the moment) are full of extra goodies, such as a <strong>coloured syntax editor</strong>, a nice <strong>dialog editor</strong> (for the visual design of menus, windows, dialog boxes, etc), and so on. (Not to mention several good extra packages, included in the compiler, such as Flex, Datamite, Proweb, Chimaira Agents, and so on; you can read all about them in <a href="http://www.lpa.co.uk" title="LPA Win-Prolog's site" target="_blank"><strong>LPA Prolog&#8217;s site, here</strong></a>).</p>
<p align="justify">My only complaint is that (at the moment) the LPA package does <em>not </em>include a <a href="http://del.icio.us/omadeon/Constraints" title="Omadeon's bookmarks about Constraints programming" target="_blank"><strong>Constraints programming extension</strong></a>, (such as <strong> CLP</strong>, <strong>CLP/fd</strong>, <strong>CHR</strong>, etc). However, I plan to adapt some open source code for such extensions and include it inside LPA Prolog, in the near future. Unless -of course- this innovative company has <em>already </em>embarked on a similar project, adding to their compiler <em>Constraints handling extensions</em>. Finally, the very latest (recently announced) <em>next</em> version of LPA Prolog includes something very special, which is useful to musically inclined programmers and hobbyists: A <strong>midi interface</strong>!</p>
<p align="justify">I was probably among those people who <em>first</em> proposed to Brian Steel -back in 2001- that a midi interface would prove to be very popular, as well as a way to expand LPA&#8217;s customer base <em>even further.</em> At the time (2001), most of their customers were academics, universities and serious professional people. Today (2007) my guess is that their clients begin to be <em>also</em> musically oriented hobbyists and computer-literate composers. I -for one- (as a composer and remixer) <em>can&#8217;t wait</em> to get my hands on this new version of their compiler, since it&#8217;s the best way to experiment with <a href="http://del.icio.us/omadeon/L-systems" title="L-systems personal bookmarks" target="_blank"><strong>L-systems</strong></a> for computer music generation, Prolog-based <strong>grammars</strong> for parsing and analysing existing MIDI music pieces, and so on.</p>
<p align="justify">However, now that the MIDI music Logic Programming client-base of LPA Prolog is beginniing to grow, the issue of Constraints extensions can no longer be neglected. Some of the best work in musical Artificial Intelligence is already using Constraints programming methods.</p>
<p align="justify">In this blog, I hope to write quite often about programming projects and experiments with  <u><a href="http://www.lpa.co.uk" title="Home Page of LPA Win-Prolog" target="_blank"><strong>LPA Win-Prolog</strong></a></u>, including my own (public domain<em>) source-code</em>. In addition, you can browse some professional projects implemented with LPA Prolog in recent years (2001-2005), in my <u><a href="http://www.omadeon.com/sourcecode" title="Source code and programming projects by Omadeon" target="_blank"><strong>source-code and programming projects&#8217; page</strong></a></u>.</p>
<p align="justify">Finally, there are some rather unusual web-pages I wrote a few years ago, with tips and free code to <strong><em>combine</em> </strong><u><a href="http://www.lpa.co.uk" title="Home Page of LPA Win-Prolog" target="_blank"><strong>LPA Win-Prolog</strong></a></u>, with&#8230; one of its rivals (<u><a href="http://www.visual-prolog.com" title="Home Page of Visual Prolog"><strong>PDC/Visual Prolog</strong></a></u>). At the time, it became evident that it was perfectly possible to combine the best features of both these professional compilers, so the title of the main web-page about this work was &#8220;<u><a href="http://www.omadeon.com/sourcecode/twoprologs.html" title="A Tale of Two Prologs (Visual Prolog + LPA Prolog together)" target="_blank"><strong>A tale of Two Prologs</strong></a></u>&#8220;.<u> </u></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=6&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/09/22/lpaprolog/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading EXCEL CSV-files as Prolog Clauses (SWI-Prolog source-code)</title>
		<link>http://prologsource.wordpress.com/2007/09/21/csv2swiprolog/</link>
		<comments>http://prologsource.wordpress.com/2007/09/21/csv2swiprolog/#comments</comments>
		<pubDate>Fri, 21 Sep 2007 09:18:08 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[Conversions]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[SWI-Prolog]]></category>
		<category><![CDATA[Comma-separated values]]></category>
		<category><![CDATA[csv files]]></category>
		<category><![CDATA[Open source]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/09/21/csv2swiprolog/</guid>
		<description><![CDATA[Image via Wikipedia If you need to convert into Prolog terms &#8220;raw data&#8221; supplied in EXCEL csv-files, read on! The source code in this posting will read any CSV file, converting each semicolon-delimited line (or record) of the CSV file into a Prolog clause, asserted in RAM. It is also possible to use the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=5&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="display:block;margin:1em;">
<div>
<dl class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://en.wikipedia.org/wiki/Image:CsvDelimited001.svg"><img title="stylized depiction of a csv text file" src="http://upload.wikimedia.org/wikipedia/en/thumb/3/38/CsvDelimited001.svg/113px-CsvDelimited001.svg.png" alt="stylized depiction of a csv text file" width="113" height="113" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution">Image via <a href="http://en.wikipedia.org/wiki/Image:CsvDelimited001.svg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p style="text-align:justify;">If you need to<strong> convert into Prolog terms &#8220;raw data&#8221; supplied in EXCEL csv-files</strong>, read on! The source code in this posting will read <em>any </em><strong><a class="zem_slink" title="Comma-separated values" rel="wikipedia" href="http://en.wikipedia.org/wiki/Comma-separated_values">CSV</a> file</strong>, converting each  <em>semicolon-delimited</em> line (or <strong>record</strong>)  of the <strong>CSV file</strong> into a <em><strong>Prolog clause</strong>, asserted in RAM.</em> It is also possible to use the same code to read data <em>deliberately provided </em>(e.g. by another application) as a CSV-file, but which is <em>specifically intended </em>for use as a <em><strong>set of Prolog clauses</strong>.</em></p>
<p style="text-align:justify;">This code also uses a couple of <em><span style="color:#ff0000;">specification predicates</span>:</em> <strong>time_field_type/1</strong>, <strong> field1_as_<a class="zem_slink" title="Functor" rel="wikipedia" href="http://en.wikipedia.org/wiki/Functor">functor</a>/1, and</strong><span style="color:#000080;"> <span style="color:#000000;"><strong>conv_csvhead/2</strong></span>.</span> These predicates control the <strong><em>behaviour of the conversion process</em></strong>, as follows:</p>
<p><strong>time_field_type/1 :</strong></p>
<ul>
<li><span style="color:#000080;">time_field_type(0). </span>In this case, time-fields in the CSV file (of the  form &#8220;HH:MM&#8221; or &#8220;HH:MM:SS&#8230;&#8221;) are translated into minutes, ignoring seconds or hundredths of a second.</li>
<li><span style="color:#000080;">time_field_type(1). </span> In this case, time-fields in the CSV file (of the  form &#8220;HH:MM&#8221; or &#8220;HH:MM:SS&#8230;&#8221;) are translated into seconds, ignoring hundredths of a second.</li>
<li><span style="color:#000080;">time_field_type(2). </span> In this case, time-fields in the CSV file are kept as they are, as atoms (e.g. &#8217;03:35&#8242;, &#8217;12:45:20&#8242;, etc).</li>
</ul>
<p><strong>field1_as_functor/1:</strong></p>
<ul style="text-align:justify;">
<li><span style="color:#000080;">field1_as_functor(0):</span> Each line in the CSV-file is interpreted as a <em>prolog clause,</em> where the functor of the clause is the first field of the record, and the other fields are arguments.</li>
<li><span style="color:#000080;">field1_as_functor(foo)</span> (where &#8216;foo&#8217; can be <em>any </em>atom): Each line in the CSV file is interpreted as a prolog clause, where the functor of the clause is foo (or any atom supplied as 1st argument to field1_as_functor/1) and all the fields are arguments.</li>
</ul>
<p><span style="color:#000080;"><span style="color:#000000;"><strong>conv_csvhead/2:</strong></span></span></p>
<ul style="text-align:justify;">
<li>This predicate is used to <em>convert </em>the contents of the <em>first field </em>(of the CSV-file) into a (user-defined) internal Prolog representation. <span style="color:#000000;">I<strong><em>t is used only if &#8220;</em></strong></span><span style="color:#000080;"><strong><em><span style="color:#000000;">time_field_type(0)&#8221; exists.</span> </em></strong>For example, to convert records where the first field is a Prolog functor &#8216;job&#8217; but the actual contents of this field are &#8216;j&#8217; (for brevvity), using a definition &#8220;conv_csvhead(j,job)&#8221; will convert each &#8216;j&#8217; into a functor &#8216;job&#8217;.<span style="color:#000000;"><em> (Use of conv_csvhead/2 is optional; in the default case, it does nothing!)</em></span></span></li>
</ul>
<p>Finally,<strong> some notes:</strong></p>
<ul style="text-align:justify;">
<li>The main predicate to call is &#8220;<span style="color:#ff0000;"><strong>loaddb(CSVfile)</strong></span>&#8220;, where CSVfile can be e.g. &#8220;test.csv&#8221;.</li>
<li>Provision has been taken for <em>special fields</em> which contain <strong><em>Lists of items, <a class="zem_slink" title="Delimiter-separated values" rel="wikipedia" href="http://en.wikipedia.org/wiki/Delimiter-separated_values">comma-delimited</a>.</em></strong> In EXCEL these fields will appear as longish strings, but this code was written to <em>parse them as <strong>Prolog atom-lists</strong>. </em>(<em>Comment-out</em> this section if you don&#8217;t need it).</li>
<li>The <em>only </em>type of field that is currently <em>not </em>converted into any meaningful internal representation is DATE. Dates are converted to atoms, just as they appear, without parsing their actual contents. (As an exercise, you can re-use parts of the same code to parse date-fields!) <span style="color:#000080;"><em>The honest reason for this omission is that&#8230; I <strong>didn&#8217;t need dates</strong> (in an application I am developing, for which this code was also written).</em></span></li>
</ul>
<p style="text-align:justify;"><strong>The source-code follows.</strong> There are useful comments inside this code. You can just copy and paste what follows from this point onwards, into a text file saved for compilation by SWI-Prolog, ending in &#8220;.pl&#8221;:<span id="more-5"></span></p>
<pre><span style="color:#000080;">% ReadingG CSV-files (EXCEL); converting to Prolog clauses</span></pre>
<pre><span style="color:#000080;">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span></pre>
<pre><span style="color:#000080;">%% <strong>Open-source SWI-Prolog code</strong> </span><span style="color:#000080;">by OMADEON (c)2007/2009.
%% Blogs:</span><span style="color:#000080;">
%%%%%%%% <a href="http://omadeon.wordpress.com">http://prologsource.wordpress.com</a>
</span><span style="color:#000080;">%%%%%%%% <a href="http://omadeon.wordpress.com">http://omadeon.wordpress.com</a></span>
<span style="color:#000080;">% time_field_type/1 defines the type of the TIME-fields:</span></pre>
<pre><span style="color:#000080;">% <strong>Cases:</strong></span></pre>
<pre><span style="color:#000080;">% (0): minute-accuracy,</span></pre>
<pre><span style="color:#000080;">% (1): seconds-accuracy,</span></pre>
<pre><span style="color:#000080;">% (N): time preserved <strong>as is</strong> (atom, e.g. '03:30');</span><span style="color:#000080;"> N &gt; 1 </span></pre>
<pre><span style="color:#000080;">time_field_type(0).</span></pre>
<pre><span style="color:#000080;">%time_field_type(1).</span></pre>
<pre><span style="color:#000080;">%time_field_type(2).</span></pre>
<pre><span style="color:#000080;">
% Reading CSV-file, converting to prolog-clauses:</span></pre>
<pre><span style="color:#000080;"><span style="color:#ff0000;"><strong>loaddb(File)</strong></span>:-
     open(File, read, Stream),</span><span style="color:#000080;">
     read_line_to_codes(Stream, T0),</span><span style="color:#000080;">
     parse_line(T0,Stream),
     close(Stream).

% user-specific conversion of 1st field to functor:</span></pre>
<pre><span style="color:#000080;">%conv_csvhead('j','job'):- !. % activate <em>if really needed!</em></span></pre>
<pre><span style="color:#000080;">% default-case, when the 1st field is an internal functor</span></pre>
<pre><span style="color:#000080;">conv_csvhead(X,X):- !.</span></pre>
<pre><span style="color:#000080;">
% specifies if first field of each record is Prolog functor</span></pre>
<pre><span style="color:#000080;">field1_is_functor(1). % it <em>is</em> a functor</span></pre>
<pre><span style="color:#000080;">%field1_is_functor(foo). % NOT a functor; arg1 IS the functor!</span></pre>
<pre><span style="color:#000080;">
% end of file</span></pre>
<pre><span style="color:#000080;">parse_line(end_of_file,_):- !.</span></pre>
<pre><span style="color:#000080;">% end of input-stream</span></pre>
<pre><span style="color:#000080;">parse_line([],_):- !.</span></pre>
<pre><span style="color:#000080;">
% current line is parsed; next line follows</span></pre>
<pre><span style="color:#000080;">parse_line(CODES,Stream):-
     field1_is_functor(1), !,
     csvparse(CODES,XL),
     XL = [First|XXL],
     </span><span style="color:#000080;">conv_csvhead(First,Head),
     TERM =.. [Head|XXL],
     </span><span style="color:#000080;">assert(TERM),  % assert prolog-clause
     read_line_to_codes(Stream,T2), </span><span style="color:#000080;">
     parse_line(T2,Stream).

% current line is parsed; next line follows</span></pre>
<pre><span style="color:#000080;">parse_line(CODES,Stream):-
     </span><span style="color:#000080;">field1_is_functor(Head), !,
     csvparse(CODES,XL),
     TERM =.. [Head|XL],
     </span><span style="color:#000080;">assert(TERM), %%% assert prolog-clause
     read_line_to_codes(Stream,T2),
     parse_line(T2,Stream).</span></pre>
<pre><span style="color:#000080;">
% error message (rarely invoked; included "just in case")</span></pre>
<pre><span style="color:#000080;">parse_line(Term,Stream):- </span><span style="color:#000080;">
     format(user_error, 'Bad term: ~p~n', [Term]),</span><span style="color:#000080;">
     read_line_to_codes(Stream,T2),
     parse_line(T2,Stream).
csvparse2([],_,[[]]):-!.</span></pre>
<pre><span style="color:#000080;">csvparse2(Inp,Sep,X):-</span><span style="color:#000080;">
     csvparse(Inp,Sep,X), !.

csvparse2a([],[[]]):-!.</span></pre>
<pre><span style="color:#000080;">csvparse2a(Inp,X):- csvparse(Inp,X), !.</span></pre>
<pre><span style="color:#000080;">
% parsing a line delimited by a semicolon(;)</span></pre>
<pre><span style="color:#000080;">csvparse([],[]):-!.</span></pre>
<pre><span style="color:#000080;">csvparse(CODES,[FirstOut|More]):-
     append(First,[59|RestCODES],CODES),
     !,
     conv_codes(First,FirstOut),
     csvparse2a(RestCODES,More).</span></pre>
<pre><span style="color:#000080;">csvparse(Last,[OutX]):- conv_codes(Last,OutX), !.</span></pre>
<pre><span style="color:#000080;">
% parsing line delimited by given separator-char:</span></pre>
<pre><span style="color:#000080;">csvparse([],_,[]):-!.</span></pre>
<pre><span style="color:#000080;">csvparse(CODES,SEPc,[FirstOut|More]):-
     append(First,[SEPc|RestCODES],CODES), !,
     conv_codes(First,FirstOut),
     csvparse2(RestCODES,SEPc,More).</span></pre>
<pre><span style="color:#000080;">csvparse(Last,_,[OutX]):-
     </span><span style="color:#000080;">conv_codes(Last,OutX), !.</span></pre>
<pre><span style="color:#000080;">
% check that input-list is only digits or dot(.)</span></pre>
<pre><span style="color:#000080;">are_digits([C]):- C &gt;= 46, C =&lt; 57, C =\= 47, !.</span></pre>
<pre><span style="color:#000080;">are_digits([C|CL]):-
      C &gt;= 46, C =&lt; 57, C =\= 47,
      !, are_digits(CL).</span></pre>
<pre><span style="color:#000080;">
% read string enclosed in double_quotes, e.g. "the"</span></pre>
<pre><span style="color:#000080;">conv_codes([34|Rest],AtomX):-
     </span><span style="color:#000080;">append(MAINx,[34],Rest),
     +\memberchk(44,MAINx),
     atom_codes(AtomX,MAINx), !.</span></pre>
<pre><span style="color:#000080;">
% reading an integer or decimal number</span></pre>
<pre><span style="color:#000080;">conv_codes(CL,Nx):-
     are_digits(CL),
     atom_codes(Ax,CL),</span><span style="color:#000080;">
     atom_number(Ax,Nx), !.

% read comma-delimited list in dup-quotes, e.g. "a,b,cd"</span></pre>
<pre><span style="color:#000080;">conv_codes([34|Rest],LISTx):-</span><span style="color:#000080;">
     append(MAINx,[34],Rest),
     memberchk(44,MAINx),</span><span style="color:#000080;">
     csvparse(MAINx,44,LISTx), !.

% time_field of the form "HH:MM"</span></pre>
<pre><span style="color:#000080;">conv_codes([C1,C2,58,C3,C4],OUTx):-
    </span><span style="color:#000080;">time_field_type(TYP),
    atom_codes(A1,[C1,C2]),
    atom_codes(A2,[C3,C4]),
    atom_number(A1,N1x),</span><span style="color:#000080;">
    atom_number(A2,N2x),
  </span><span style="color:#000080;">  (TYP = 0 -&gt;</span><span style="color:#000080;">       
      OUTx is N2x + 60*N1x % only minutes-accuracy
      ;</span></pre>
<pre><span style="color:#000080;">      ( TYP = 1 -&gt;</span></pre>
<pre><span style="color:#000080;">        ( OUTx is 60*(N2x + 60*N1x) ) % seconds-accuracy</span></pre>
<pre><span style="color:#000080;">        ;</span></pre>
<pre><span style="color:#000080;">        atom_codes(OUTx,[C1,C2,58,C3,C4]) % time=string</span></pre>
<pre><span style="color:#000080;">      )
    ), !.</span></pre>
<pre><span style="color:#000080;">
% time_field of the form "HH:MM:SS..."</span></pre>
<pre><span style="color:#000080;">conv_codes([C1,C2,58,C3,C4,58,C5,C6|CL],OUTx):-
    time_field_type(TYP),
    atom_codes(A1,[C1,C2]),
    atom_codes(A2,[C3,C4]),
    atom_number(A1,N1x),
    atom_number(A2,N2x),</span><span style="color:#000080;">
    MINSx is N2x + 60*N1x,
    ( TYP = 0</span></pre>
<pre><span style="color:#000080;">      -&gt; OUTx = MINSx % if only minutes-accuracy required</span></pre>
<pre><span style="color:#000080;">        ;</span></pre>
<pre><span style="color:#000080;">        ( TYP = 1</span></pre>
<pre><span style="color:#000080;">          -&gt; </span><span style="color:#000080;">% use this if seconds-accuracy is required</span></pre>
<pre><span style="color:#000080;">          ( atom_codes(A3,[C5,C6]),
            </span><span style="color:#000080;">atom_number(A3,SECS),
            OUTx is SECS + 60*MINSx</span></pre>
<pre><span style="color:#000080;">          )</span></pre>
<pre><span style="color:#000080;">          ;</span></pre>
<pre><span style="color:#000080;">          atom_codes(OUTx,[C1,C2,58,C3,C4,58,C5,C6|CL])
          % time=string</span></pre>
<pre><span style="color:#000080;">        )</span></pre>
<pre><span style="color:#000080;">    ), !.</span></pre>
<pre><span style="color:#000080;">
% a comma-delimited list</span></pre>
<pre><span style="color:#000080;">conv_codes(MAIN,LISTx):-
     memberchk(44,MAIN),
     csvparse(MAIN,44,LISTx), !.</span></pre>
<pre><span style="color:#000080;">
% remaining case: just an atom
% (including case of time=string):</span></pre>
<pre><span style="color:#000080;">conv_codes(MAIN,ATOMx):-
     atom_codes(ATOMx,MAIN), !.</span></pre>
<div class="zemanta-pixie" style="margin-top:10px;height:15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/1ad1fea8-cf56-44f5-8dbd-9b49e18cf146/"><img class="zemanta-pixie-img" style="border:medium none;float:right;" src="http://img.zemanta.com/reblog_e.png?x-id=1ad1fea8-cf56-44f5-8dbd-9b49e18cf146" alt="Reblog this post [with Zemanta]" /></a></div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=5&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/09/21/csv2swiprolog/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/wikipedia/en/thumb/3/38/CsvDelimited001.svg/113px-CsvDelimited001.svg.png" medium="image">
			<media:title type="html">stylized depiction of a csv text file</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/reblog_e.png?x-id=1ad1fea8-cf56-44f5-8dbd-9b49e18cf146" medium="image">
			<media:title type="html">Reblog this post [with Zemanta]</media:title>
		</media:content>
	</item>
		<item>
		<title>SWI-Prolog source code: Converting hours-and-minutes to integers (e.g. for use in CLP)</title>
		<link>http://prologsource.wordpress.com/2007/09/20/swi-timeconv/</link>
		<comments>http://prologsource.wordpress.com/2007/09/20/swi-timeconv/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 17:31:04 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[CLP]]></category>
		<category><![CDATA[Conversions]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[SWI-Prolog]]></category>
		<category><![CDATA[time-predicates]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/09/20/swi-timeconv/</guid>
		<description><![CDATA[This short posting is about a useful piece of SWI-Prolog code I keep (re-)using, ever since I wrote it. It is a predicate that converts time (in an EXCEL-compatible format &#8216;HH:MM&#8217; or &#8216;HH:MM:SS&#8217;) to integers expressing minutes only, e.g. integers suitable for use in CLP applications (Constraints Logic Programming over finite integer domains). I am [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=4&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">This short posting is about a <strong><em>useful piece of SWI-Prolog code</em></strong> I keep (re-)using, ever since I wrote it. It is a predicate that converts<strong> time </strong>(in an EXCEL-compatible format &#8216;<strong>HH:MM&#8217;</strong> or &#8216;<strong>HH:MM:SS&#8217;)</strong> to <strong><em>integers</em></strong> expressing minutes only, e.g. integers suitable for use in <strong>CLP applications</strong> (<em>Constraints Logic Programming over finite integer domains</em>). I am developing a serious <strong>CLP application</strong>, during the last few weeks and I regard the following <em>(bi-directional) </em><strong>conversion code</strong> <em>as <strong>indispensable:</strong></em></p>
<pre><strong>%%% Conversion of Hours-and-minutes to integers <em>a<font color="#ff0000">nd vice-versa</font> </em>(e.g. for CLP problems)</strong></pre>
<pre><strong>%%% converts number of minutes to a valid time-string e.g. '03:05':
</strong></pre>
<pre><strong>mins2hourmin(MINS,OUTX):- nonvar(MINS), MINS &gt; 0,
    Hours is MINS // 60, Minsx is MINS mod 60,
    num2str2(Hours,S1x), num2str2(Minsx,S2x),
    swritef(OUTX,'%w:%w',[S1x,S2x]), !
    ;
    MINS = 0, OUTX = '00:00', !.
mins2hourmin(HMINx,HRi):- nonvar(HRi),
    sub_atom(HRi,0,2,_,A1x), atom_number(A1x,HOURx),
    sub_atom(HRi,3,2,_,A2x), atom_number(A2x,MINSx),
    HMINx is MINSx + 60*HOURx, !.
mins2hourmin(MINS1,OUTX):- nonvar(MINS1), MINS1 &lt; 0,
    MINS is -MINS1,
    Hours is MINS // 60, Minsx is MINS mod 60,
    num2str2(Hours,S1x), num2str2(Minsx,S2x),
    swritef(OUTX,'-%w:%w',[S1x,S2x]), !.
</strong></pre>
<pre><strong>%%</strong> the same predicate operating on <strong><em>Lists of time-entities: </em></strong></pre>
<pre><strong>mins2hourmin_list([],[]):- !.
mins2hourmin_list([M|ML],[X|XL]):- mins2hourmin(M,X), !, mins2hourmin_list(ML,XL).</strong></pre>
<pre></pre>
<pre></pre>
<pre><strong>%%% an auxilliary predicate for mins2hourmin/2:
num2str2(N,Sx):- N &gt;= 10, swritef(Sx,'%w',[N]), !
    ;
    swritef(Sx,'0%w',[N]), !.
</strong></pre>
<pre></pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=4&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/09/20/swi-timeconv/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>
	</item>
		<item>
		<title>Bridging gaps between Prologs (SWI-Prolog predicates implented in LPA Win-Prolog)</title>
		<link>http://prologsource.wordpress.com/2007/09/20/lpaprolog-swiprolog/</link>
		<comments>http://prologsource.wordpress.com/2007/09/20/lpaprolog-swiprolog/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 16:36:12 +0000</pubDate>
		<dc:creator>Omadeon</dc:creator>
				<category><![CDATA[Code Conversion]]></category>
		<category><![CDATA[LPA Win-Prolog]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[SWI-Prolog]]></category>

		<guid isPermaLink="false">http://prologsource.wordpress.com/2007/09/20/lpaprolog-swiprolog/</guid>
		<description><![CDATA[Today I spent too much time trying to force a SWI-Prolog project (of timetable scheduling, custom-made for a specific company) to run in a different compiler: LPA Win-Prolog. I needed badly to use certain graphics routines and other goodies of LPA Prolog (a commercial compiler), entire volumes of them in fact. So, I ended up [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=3&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I spent too much time trying to force a <strong>SWI-Prolog project</strong> (of timetable scheduling, <em>custom-made for a specific company</em>) to run in a different compiler: <strong>LPA Win-Prolog</strong>. I needed badly to use certain graphics routines and other goodies of <strong>LPA Prolog</strong> (a commercial compiler), entire volumes of them in fact. So, I ended up writing code in <strong>LPA Prolog</strong> that implements some quite common <strong>SWI-Prolog</strong> predicates. Here are some of them:</p>
<p>The <strong>SWI-Prolog predicate &#8216;between/3&#8242;</strong> generates (<em>non-deterministically</em>) a number, ranging from a minimum value to a maximum Value (as &#8216;bound&#8217; 1st and 2nd arguments). I.e., in the <strong>SWI-Prolog</strong> <em>console:</em></p>
<pre><font color="#000080"><strong>?- between(1,3,X).</strong></font></pre>
<pre><font color="#000080"><strong>X = 1 ;</strong></font></pre>
<pre><font color="#000080"><strong>X = 2 ;</strong></font></pre>
<pre><font color="#000080"><strong>X = 3 ;
</strong></font></pre>
<p>Well, here is an <strong>LPA Win-Prolog</strong> implementation of this predicate (also valid in any <em>other </em><strong>ISO-compatible Prolog</strong>):</p>
<pre><strong><font color="#000080">between(Min,_,Min).</font></strong></pre>
<pre><strong><font color="#000080">between(Min,Max,Out):- M2 is Min+1, M2 =&lt; Max, between(M2,Max,Out).</font></strong></pre>
<p>OK, This was an easy example, while most probably the same code <em>already exists </em>in elementary Prolog textbooks. Here are <em>some other </em>(not-so-obvious) examples:</p>
<p>In <strong>LPA Prolog</strong> there are some <em>very special, very efficient unique commands</em>, like &#8216;<strong><font color="#ff0000">find/3</font>&#8216;</strong>, which operates on &#8216;input streams&#8217; to locate (sub-)strings inside them. Now, the so-called &#8216;input stream&#8217; can itself (effectively) be <em>just another string</em> (turned into a stream through the special LPA command <strong>&#8216;&lt;~&#8217;</strong>). The use of this predicate, &#8216;find/3&#8242; to write quickly <em>efficient</em> code for non-deterministic search (of substrings inside larger strings) is a natural happy consequence. E.g.</p>
<pre><font color="#000080"><strong>findsubs(SubSTR,STR,Px):- len(SubSTR,Len), findrep(SubSTR,Len,Px) &lt;~ STR.

findrep(_,Len,Px):- inpos(EndP), EndP &gt; 0, Px is EndP-Len.
findrep(SubSTR,Len,Px):- find(SubSTR,0,Sx), +Sx=``, findrep(SubSTR,Len,Px). </strong></font></pre>
<p>I wrote this code <em>after</em> understanding the (well-documented) similar non-deterministic predicate &#8216;replace&#8217; (<em>not </em>a built-in command but given as simple <em>source-code</em> in <strong>LPA-Prolog&#8217;s Reference Quide, page 226</strong>). The reason I wrote it is because I needed it as a sub-predicate to implement of <strong>SWI-Prolog</strong>&#8216;s superb predicate &#8216;<font color="#ff0000"><strong>sub_atom</strong></font>&#8216;. (This was <em>already</em> used <em>-alas in several places-</em> inside the SWI-Prolog project, which was to be <em>converted into </em><strong>LPA WIn-Prolοg</strong>).So, here is the resulting <strong>LPA-Prolog implementation of &#8216;<font color="#ff0000">sub_atom</font>&#8216;,</strong>  valid for (almost) all possible flow-patterns (at least those used in my project, plus a few more):</p>
<pre><font color="#000080">/*********************************************************************************************</font></pre>
<pre><font color="#000080">(a description of sub_atom/5 from SWI-Prolog's Open Source documentation):</font></pre>
<pre><font color="#000080"><strong>sub atom(+Atom, ?Before, ?Len, ?After, ?Sub)</strong></font></pre>
<pre><font color="#000080">    ISO predicate for breaking atoms. It maintains the following relation: Sub is a sub-atom of Atom
    that starts at Before, has Len characters and Atom contains After characters after the match.
?- sub_atom(abc, 1, 1, A, S).
 A = 1, S = b
   The implementation minimises non-determinism and creation of atoms. This is a very flexible
   predicate that can do search, prefix- and suffix-matching, etc.
**********************************************************************************************/</font></pre>
<pre><font color="#000080"><strong>% HERE is my LPA Win-Prolog (exact) implementation of 'sub_atom/5':</strong></font></pre>
<pre><font color="#000080">%%%%%%%%%% (also valid in most other ISO-ish Prologs) %%%%%%%%%%%%</font></pre>
<pre><font color="#000080">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</font></pre>
<pre><font color="#000080">% (i,i,i,o,o)
sub_atom(Atom,Start,Len,LenAfterX,SubX):-
   nonvar(Atom), nonvar(Start), nonvar(Len), var(LenAfterX), var(SubX),
   cat(Lx,Atom,[Start,Len]), Lx = [_,SubX,End], len(End,LenAfterX).</font></pre>
<pre><font color="#000080">% (i,o,i,i,o)
sub_atom(Atom,Start,Len,LenAfter,SubX):-
   nonvar(Atom), nonvar(Len), nonvar(LenAfter), var(Start), var(SubX),
   len(Atom,LenTotal), Start is LenTotal - LenAfter - Len,
   cat(Lx,Atom,[Start,Len]), Lx = [_,SubX,_].</font></pre>
<pre><font color="#000080"><strong>% (i,o,o,o,i)  (effectively a non-deterministic search-function)</strong>
sub_atom(Atom,Startx,SubLenx,LenAfterX,Sub):-
   nonvar(Atom), nonvar(Sub), var(SubLenx), var(LenAfterx), var(Startx),
   len(Sub,SubLenx),  atom_string(Atom,STR), atom_string(Sub,SubSTR), len(STR,Slen),
   </font><font color="#000080"><strong>findrep</strong>(SubSTR,SubLenx,Startx) &lt;~ STR, LenAfterX is Slen-Startx-SubLenx.</font></pre>
<pre><font color="#000080">% where '<strong>findrep/3</strong>' was written previously as a part of '<strong>findsubs</strong>' (top of this post).</font></pre>
<pre><font color="#000080">%(i,i,o,o,o)
sub_atom(Atom,Start,Len,LenAfterX,SubX):-
   nonvar(Atom), nonvar(Start), var(Len), var(LenAfterX), var(SubX),
  cat(Lx,Atom,[Start]), Lx = [_,End], len(End,Len2),
  between(0,Len2,Lenx), cat(Lxx,End,[Lenx]),
  Lxx = [SubX,End2], len(End2,LenAfterX), Len=Lenx.</font></pre>
<pre><font color="#000080">%(i,o,o,o,o)
sub_atom(Atom,Start,Len,LenAfterX,SubX):-
   nonvar(Atom), var(Start), var(Len), var(LenAfterX), var(SubX),
   len(Atom,Len1), between(0,Len1,Pos),
  cat(Lx,Atom,[Pos]), Lx = [_,End], len(End,Len2),
  between(0,Len2,Lenx), cat(Lxx,End,[Lenx]),
  Lxx = [SubX,End2], len(End2,LenAfterX), Len=Lenx, Start=Pos.</font></pre>
<pre><font color="#000080">%(i,o,i,o,o)
sub_atom(Atom,Start,Len,LenAfterX,SubX):-
   nonvar(Atom), var(Start), nonvar(Len), var(LenAfterX), var(SubX),
   len(Atom,Len1), LenPre is Len1-Len, between(0,LenPre,Pos),
  cat(Lx,Atom,[Pos,Len]), Lx = [_,Mid,End],
  SubX = Mid, LenAfterX is Len1-Pos-Len, Start=Pos.</font></pre>
<p><font color="#000080"><strong> </strong></font></p>
<pre><font color="#000080">%%%%%%%%%% (end of code) %%%%%%%%</font></pre>
<p>Well,<em> that&#8217;s it,</em> for the moment, I&#8217;m afraid.</p>
<p>I <em>must </em>go back to my project now, but (rest assured) <em>I will be coming back here, soon, again and again, and again&#8230; </em></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/prologsource.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/prologsource.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/prologsource.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/prologsource.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/prologsource.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=prologsource.wordpress.com&amp;blog=1757696&amp;post=3&amp;subd=prologsource&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://prologsource.wordpress.com/2007/09/20/lpaprolog-swiprolog/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f9afd16e93a892136d3962baf4b32746?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">omadeon</media:title>
		</media:content>
	</item>
	</channel>
</rss>
