Imporved cleaning of recursive primitives

parent abda3590
......@@ -19,24 +19,21 @@ Version 0.4.1
* Fixed some compiler bugs
* Fixed bug in stall
Version 0.5, TODO
* Improved parsing tool (guile-log)
Version 0.5,
* Improved parsing tool
- Factorized and enabled for customized streams flow e.g.
current indentation level in python
- f-cons and similar tools for functional AST creation.
- Debug features
* Debug features
* Tabling or memoisation
* rational datastructures
* scheme Keyword objects and logic values
* GC of prolog variables
* Attributed variables
* corouttines
* tracing
* Better compilation error handling
* Tablating (all) DONE
* Circular datastructures (all) DONE
* Improved documentation (all)
* Keyword objects and logic values (prolog) DONE
* GC of prolog variables (all) DONE
* Attributed variables (all) DONE
* corouttines (prolog) DONE
* Debugging facilities (prolog) DONE
* Better error messages (prolog) DONE
* Better compilation errors (prolog) DONE
* Use guile variables when possible (prolog) DONE
Version 0.6, TODO
* GC of the (almost) unreachable tail of a stream (all)
......@@ -46,6 +43,7 @@ Version 0.6, TODO
* Ordinary hash maps (all)
* Faster compilation (prolog)
* Better compilation errors (prolog)
* Improved documentation (all)
* Improved matcher (prolog)
* vectors (prolog)
* structs (prolog)
......@@ -1270,7 +1270,9 @@ Finally the last section conserns the ability to compile the dynamic function fo
@node rational data
@chapter Memoizing
it is possible to perform most operation in guile-log using ratioanl datastructures, The printer supports printing recursive data structures in guile-log and prolog. Generally the handling of ratoinal datstructures are expensive, therefore a mechanism to turn this feture on and off have been designed.
it is possible to perform most operation in guile-log using ratioanl datastructures, The printer supports printing recursive data structures in guile-log and prolog. Generally the handling of rational datstructures are expensive, therefore mechanisms to turn this feture on and off have been designed. Yoy may use the @code{.rec} interactive directive. Or you can use the @code{-functorize(recursive)} functor to tell that the function shall be evaluated with recursives on. Or in code use
@code{prolog recursive(Code)}.
@node memoizing
@chapter Memoizing
......
This source diff could not be displayed because it is too large. You can view the blob instead.
<html lang="en">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>acumulators/generators - Preliminary Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Preliminary Manual">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="guile_002dlog.html#guile_002dlog" title="guile-log">
<link rel="next" href="postpone.html#postpone" title="postpone">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<title>Preliminary Manual: acumulators/generators</title>
<meta name="description" content="Preliminary Manual: acumulators/generators">
<meta name="keywords" content="Preliminary Manual: acumulators/generators">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index.html#Index" rel="index" title="Index">
<link href="index.html#Top" rel="up" title="Top">
<link href="postpone.html#postpone" rel="next" title="postpone">
<link href="guile_002dlog.html#guile_002dlog" rel="prev" title="guile-log">
<style type="text/css">
<!--
This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body>
<div class="node">
<a name="acumulators%2fgenerators"></a>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="acumulators_002fgenerators"></a>
<div class="header">
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="postpone.html#postpone">postpone</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="guile_002dlog.html#guile_002dlog">guile-log</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
Next: <a href="postpone.html#postpone" accesskey="n" rel="next">postpone</a>, Previous: <a href="guile_002dlog.html#guile_002dlog" accesskey="p" rel="prev">guile-log</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Acumulators_002fgenerators"></a>
<h2 class="chapter">3 Acumulators/generators</h2>
<p>In the guile-log chapter we defined the basic framework to fold a sequence of solutions into a variable. With this we can support e.g prolog's bagof and similar constructs. In the module <code>(guile-log logic collects)</code>, we have defined a few specialisations of the fold operator in order to standardize these common abstractions.
<p>In the guile-log chapter we defined the basic framework to fold a sequence of solutions into a variable. With this we can support e.g prolog&rsquo;s bagof and similar constructs. In the module <code>(guile-log logic collects)</code>, we have defined a few specialisations of the fold operator in order to standardize these common abstractions.
</p>
<a name="Acumulators-API"></a>
<h3 class="section">3.1 Acumulators API</h3>
<p>To explain the Api we have a <code>gl-thunk</code>, e.g. a guile-log lambda with zero arguments. <code>it</code>, will be the pattern to store. when it successes typically this variable will be copied and added to the resulting data structure which is <code>list, sum, ...</code>. There is a possibility to keep some elements fixed and this is the <code>-2</code> forms with the variable <code>fixed</code> naming the variable that needs to be fixated. e.g. first all solutions for the first found representation of the variable <code>fixed</code> will be found and added, then the then all solutions with the next found variant of the <code>fixed</code> will be used and so on, very much like how bagof in prolog is working. The <code>-step</code> versions of the acumulators will produce produce all the intermediate acumulator solutions.
</p>
<p><a name="index-g_t_003ccollect_003e-143"></a><a name="index-g_t_003ccollect_002d2_003e-144"></a><a name="index-g_t_003ccollect_002dstep_003e-145"></a><a name="index-g_t_003ccollect_002dstep_002d2_003e-146"></a>The following forms will cons all solutions to a list e.g. prodice lists in
<a name="index-_003ccollect_003e"></a>
<a name="index-_003ccollect_002d2_003e"></a>
<a name="index-_003ccollect_002dstep_003e"></a>
<a name="index-_003ccollect_002dstep_002d2_003e"></a>
<p>The following forms will cons all solutions to a list e.g. prodice lists in
reverse order.
<p><code>G.L. (&lt;collect&gt; gl-thunk it list)</code>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed list)</code>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it list)</code>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed list)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
<p><a name="index-g_t_003csum_003e-147"></a><a name="index-g_t_003csum_002d2_003e-148"></a><a name="index-g_t_003csum_002dstep_003e-149"></a><a name="index-g_t_003csum_002dstep_002d2_003e-150"></a>The following forms will sum all solutions.
<p><code>G.L. (&lt;collect&gt; gl-thunk it sum)</code>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed sum)</code>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it sum)</code>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed sum)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;
<p><a name="index-g_t_003cprod_003e-151"></a><a name="index-g_t_003cprod_002d2_003e-152"></a><a name="index-g_t_003cprod_002dstep_003e-153"></a><a name="index-g_t_003cprod_002dstep_002d2_003e-154"></a>The following forms will take the product of all solutins
<p><code>G.L. (&lt;collect&gt; gl-thunk it prod)</code>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed prod)</code>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it prod)</code>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed prod)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
<p><a name="index-g_t_003cmax_003e-155"></a><a name="index-g_t_003cmax_002d2_003e-156"></a><a name="index-g_t_003cmax_002dstep_003e-157"></a><a name="index-g_t_003cmax_002dstep_002d2_003e-158"></a>The following forms will take the max of all solutions.
<p><code>G.L. (&lt;max&gt; gl-thunk it max)</code>
<p><code>G.L. (&lt;max-2&gt; gl-thunk it fixed max)</code>
<p><code>G.L. (&lt;max-step&gt; gl-thunk it max)</code>
<p><code>G.L. (&lt;max-step-2&gt; gl-thunk it fixed max)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
<p><a name="index-g_t_003cmin_003e-159"></a><a name="index-g_t_003cmin_002d2_003e-160"></a><a name="index-g_t_003cmin_002dstep_003e-161"></a><a name="index-g_t_003cmin_002dstep_002d2_003e-162"></a>The following forms will take the max of all solutions.
<p><code>G.L. (&lt;min&gt; gl-thunk it min)</code>
<p><code>G.L. (&lt;min-2&gt; gl-thunk it fixed min)</code>
<p><code>G.L. (&lt;min-step&gt; gl-thunk it min)</code>
<p><code>G.L. (&lt;min-step-2&gt; gl-thunk it fixed min)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
<p><a name="index-g_t_003csoland_003e-163"></a><a name="index-g_t_003csoland_002d2_003e-164"></a><a name="index-g_t_003csoland_002dstep_003e-165"></a><a name="index-g_t_003csoland_002dstep_002d2_003e-166"></a>The following forms will take the and of all operations returning the last one
</p>
<p><code>G.L. (&lt;collect&gt; gl-thunk it list)</code>
</p>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed list)</code>
</p>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it list)</code>
</p>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed list)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<a name="index-_003csum_003e"></a>
<a name="index-_003csum_002d2_003e"></a>
<a name="index-_003csum_002dstep_003e"></a>
<a name="index-_003csum_002dstep_002d2_003e"></a>
<p>The following forms will sum all solutions.
</p>
<p><code>G.L. (&lt;collect&gt; gl-thunk it sum)</code>
</p>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed sum)</code>
</p>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it sum)</code>
</p>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed sum)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;-
</p>
<a name="index-_003cprod_003e"></a>
<a name="index-_003cprod_002d2_003e"></a>
<a name="index-_003cprod_002dstep_003e"></a>
<a name="index-_003cprod_002dstep_002d2_003e"></a>
<p>The following forms will take the product of all solutins
</p>
<p><code>G.L. (&lt;collect&gt; gl-thunk it prod)</code>
</p>
<p><code>G.L. (&lt;collect-2&gt; gl-thunk it fixed prod)</code>
</p>
<p><code>G.L. (&lt;collect-step&gt; gl-thunk it prod)</code>
</p>
<p><code>G.L. (&lt;collect-step-2&gt; gl-thunk it fixed prod)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<a name="index-_003cmax_003e"></a>
<a name="index-_003cmax_002d2_003e"></a>
<a name="index-_003cmax_002dstep_003e"></a>
<a name="index-_003cmax_002dstep_002d2_003e"></a>
<p>The following forms will take the max of all solutions.
</p>
<p><code>G.L. (&lt;max&gt; gl-thunk it max)</code>
</p>
<p><code>G.L. (&lt;max-2&gt; gl-thunk it fixed max)</code>
</p>
<p><code>G.L. (&lt;max-step&gt; gl-thunk it max)</code>
</p>
<p><code>G.L. (&lt;max-step-2&gt; gl-thunk it fixed max)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<a name="index-_003cmin_003e"></a>
<a name="index-_003cmin_002d2_003e"></a>
<a name="index-_003cmin_002dstep_003e"></a>
<a name="index-_003cmin_002dstep_002d2_003e"></a>
<p>The following forms will take the max of all solutions.
</p>
<p><code>G.L. (&lt;min&gt; gl-thunk it min)</code>
</p>
<p><code>G.L. (&lt;min-2&gt; gl-thunk it fixed min)</code>
</p>
<p><code>G.L. (&lt;min-step&gt; gl-thunk it min)</code>
</p>
<p><code>G.L. (&lt;min-step-2&gt; gl-thunk it fixed min)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<a name="index-_003csoland_003e"></a>
<a name="index-_003csoland_002d2_003e"></a>
<a name="index-_003csoland_002dstep_003e"></a>
<a name="index-_003csoland_002dstep_002d2_003e"></a>
<p>The following forms will take the and of all operations returning the last one
as a solution.
<p><code>G.L. (&lt;soland&gt; gl-thunk it and)</code>
<p><code>G.L. (&lt;soland-2&gt; gl-thunk it fixed and)</code>
<p><code>G.L. (&lt;soland-step&gt; gl-thunk it and)</code>
<p><code>G.L. (&lt;soland-step-2&gt; gl-thunk it fixed and)</code>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
<p><a name="index-g_t_003csolor_003e-167"></a><a name="index-g_t_003csolor_002d2_003e-168"></a><a name="index-g_t_003csolor_002dstep_003e-169"></a><a name="index-g_t_003csolor_002dstep_002d2_003e-170"></a>The following forms will take the or of all operations returning the first solution.
<p><code>G.L. (&lt;solor&gt; gl-thunk it or)</code>
<p><code>G.L. (&lt;solor-2&gt; gl-thunk it fixed or)</code>
<p><code>G.L. (&lt;solor-step&gt; gl-thunk it or)</code>
<p><code>G.L. (&lt;solor-step-2&gt; gl-thunk it fixed or)</code>
</p>
<p><code>G.L. (&lt;soland&gt; gl-thunk it and)</code>
</p>
<p><code>G.L. (&lt;soland-2&gt; gl-thunk it fixed and)</code>
</p>
<p><code>G.L. (&lt;soland-step&gt; gl-thunk it and)</code>
</p>
<p><code>G.L. (&lt;soland-step-2&gt; gl-thunk it fixed and)</code>
</p>
<p>&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<a name="index-_003csolor_003e"></a>
<a name="index-_003csolor_002d2_003e"></a>
<a name="index-_003csolor_002dstep_003e"></a>
<a name="index-_003csolor_002dstep_002d2_003e"></a>
<p>The following forms will take the or of all operations returning the first solution.
</p>
<p><code>G.L. (&lt;solor&gt; gl-thunk it or)</code>
</p>
<p><code>G.L. (&lt;solor-2&gt; gl-thunk it fixed or)</code>
</p>
<p><code>G.L. (&lt;solor-step&gt; gl-thunk it or)</code>
</p>
<p><code>G.L. (&lt;solor-step-2&gt; gl-thunk it fixed or)</code>
</p>
<a name="Local-Non-backtracking-Genrators-API"></a>
<h3 class="section">3.2 Local Non backtracking Genrators API</h3>
<p>This tool is found directly in the module <code>(logic guile-log)</code>,
</p>
<p><code>G.L. (&lt;with-generators&gt; ((var init) ...) code ...)</code>, This will bound the variables <code>var ...</code> initiated to <code>init ...</code> inside <code>code ...</code>.
</p>
<p>These variables can then be changed by using <code>set!</code> and if the updating is functional, the state of the variables will be managed and store and restore will be managed in such a way that jumping below the initiation with <code>&lt;with-generators&gt;</code> and back will produce the same values, but inside this form it will not be restored to the old value. Especially a simple generator can be applied by using,
</p>
<p><code>G.L. (&lt;next-generator-value&gt; sum var val)</code>, e.g. perform <code>var &lt;- (sum var val)</code> each time this line is executed and no backtracking will be done.
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</p>
<p><code>G.L. (&lt;with-generators&gt; ((var init) ...) code ...)</code>, This will bound the variables <code>var ...</code> initiated to <code>init ...</code> inside <code>code ...</code>.
<p>These variables can then be changed by using <code>set!</code> and if the updating is functional, the state of the variables will be managed and store and restore will be managed in such a way that jumping below the initiation with <code>&lt;with-generators&gt;</code> and back will produce the same values, but inside this form it will not be restored to the old value. Especially a simple generator can be applied by using,
<hr>
<div class="header">
<p>
Next: <a href="postpone.html#postpone" accesskey="n" rel="next">postpone</a>, Previous: <a href="guile_002dlog.html#guile_002dlog" accesskey="p" rel="prev">guile-log</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<p><code>G.L. (&lt;next-generator-value&gt; sum var val)</code>, e.g. perform <code>var &lt;- (sum var val)</code> each time this line is executed and no backtracking will be done.
&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&ndash;
</body></html>
</body>
</html>
<html lang="en">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>dynamic-functions - Preliminary Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Preliminary Manual">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="dynamics.html#dynamics" title="dynamics">
<link rel="next" href="rational-data.html#rational-data" title="rational data">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<title>Preliminary Manual: dynamic-functions</title>
<meta name="description" content="Preliminary Manual: dynamic-functions">
<meta name="keywords" content="Preliminary Manual: dynamic-functions">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index.html#Index" rel="index" title="Index">
<link href="index.html#Top" rel="up" title="Top">
<link href="rational-data.html#rational-data" rel="next" title="rational data">
<link href="dynamics.html#dynamics" rel="prev" title="dynamics">
<style type="text/css">
<!--
This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body>
<div class="node">
<a name="dynamic-functions"></a>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="dynamic_002dfunctions"></a>
<div class="header">
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="rational-data.html#rational-data">rational data</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="dynamics.html#dynamics">dynamics</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
Next: <a href="rational-data.html#rational-data" accesskey="n" rel="next">rational data</a>, Previous: <a href="dynamics.html#dynamics" accesskey="p" rel="prev">dynamics</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Dynamic-functions"></a>
<h2 class="chapter">6 Dynamic functions</h2>
<p>An interesting category of functionality in prolog is their dynamic function framework. They are a generalization of databases and allow to do matching in a scalable manner through indexing. Guile log also support a full indexer for a simplified matcher. And for all matched clauses they will be executed in order acording to how the database is setuped. One can add clauses to the head or to the end, remove clauses according to a pattern and also store states for later rewinding. The state guarding mechanism is either cheap (weak) e.g. it will not remember states as long as the action is above in the stack, else if it backtracks below the stack, state will be stored and restored at reentrence. We also support strong state storage at e.g. postpone support.
<p>To use the api use <code>(use-modules (logic guile-log functional-database))</code>.
<p>An interesting category of functionality in prolog is their dynamic function framework. They are a generalization of databases and allow to do matching in a scalable manner through indexing. Guile log also support a full indexer for a simplified matcher. And for all matched clauses they will be executed in order acording to how the database is setuped. One can add clauses to the head or to the end, remove clauses according to a pattern and also store states for later rewinding. The state guarding mechanism is either cheap (weak) e.g. it will not remember states as long as the action is above in the stack, else if it backtracks below the stack, state will be stored and restored at reentrence. We also support strong state storage at e.g. postpone support.
</p>
<p>To use the api use <code>(use-modules (logic guile-log functional-database))</code>.
</p>
<a name="Api-1"></a>
<h3 class="section">6.1 Api</h3>
<p><a name="index-define_002ddynamic-176"></a><a name="index-g_t_003cpush_002ddynamic_003e-177"></a><a name="index-g_t_003clambda_002ddyn_003e-178"></a><a name="index-g_t_003cappend_002ddynamic_003e-179"></a><a name="index-g_t_003cremove_002ddynamic_003e-180"></a><code>SCM (define-dynamic f)</code>, this will make a dynamic function object and bind it to <code>f</code>
<p><code>G.L. (&lt;push-dynamic&gt; f lambda-dyn)</code>, push a match onto the head of dynamic function <code>f</code>. <code>lambda-dyn</code> is a special lambda typically created through,
<p><code>SCM (&lt;lambda-dyn&gt; pat ... code)</code>, here <code>pat ...</code> will be simplified patterns only supporting <code>quote,unquote</code> all else directive is not allowed. <code>code</code> is evaluated in guile-log mode.
<p>Similarly we can add code to the end of the dynamic function, e.g.
<p><code>G.L. (&lt;append-dynamic&gt; f lambda-dyn)</code>.
<p>Also to remove elements one can use,
<p><code>G.L. (&lt;remove-dynamic&gt; (f a ...))</code>, where <code>f</code> is the dynamic function and <code>a ...</code> the patterns for the arguments that, which matches will be retracted.
<p><a name="index-g_t_003cwith_002ddynamic_002dfunctions_003e-181"></a><a name="index-g_t_003cguard_002ddynamic_002dfunctions_003e-182"></a>The previous set of API is typically what you get to use in prolog. The
<a name="index-define_002ddynamic"></a>
<a name="index-_003cpush_002ddynamic_003e"></a>
<a name="index-_003clambda_002ddyn_003e"></a>
<a name="index-_003cappend_002ddynamic_003e"></a>
<a name="index-_003cremove_002ddynamic_003e"></a>
<p><code>SCM (define-dynamic f)</code>, this will make a dynamic function object and bind it to <code>f</code>
</p>
<p><code>G.L. (&lt;push-dynamic&gt; f lambda-dyn)</code>, push a match onto the head of dynamic function <code>f</code>. <code>lambda-dyn</code> is a special lambda typically created through,
</p>
<p><code>SCM (&lt;lambda-dyn&gt; pat ... code)</code>, here <code>pat ...</code> will be simplified patterns only supporting <code>quote,unquote</code> all else directive is not allowed. <code>code</code> is evaluated in guile-log mode.
</p>
<p>Similarly we can add code to the end of the dynamic function, e.g.
</p>
<p><code>G.L. (&lt;append-dynamic&gt; f lambda-dyn)</code>.
</p>
<p>Also to remove elements one can use,
</p>
<p><code>G.L. (&lt;remove-dynamic&gt; (f a ...))</code>, where <code>f</code> is the dynamic function and <code>a ...</code> the patterns for the arguments that, which matches will be retracted.
</p>
<a name="index-_003cwith_002ddynamic_002dfunctions_003e"></a>
<a name="index-_003cguard_002ddynamic_002dfunctions_003e"></a>
<p>The previous set of API is typically what you get to use in prolog. The
downside is that they posses problem w.r.t. leaking data and therefor the next ideom is preferable to use in order to make sure both that one undo any changes to the dynamic variable, but also at reinstation of a state inside the form, it will resore to the same state as when control left the inner of the ideom e.g. The design is to favor fast lookup and adding and removing information is a bit expensive.
</p>
<p><code>G.L. (&lt;with-dynamic-functions&gt; (f ...) code ...)</code>, for this form <code>f ...</code>, will be weakly guarded throughout the form e.g. if control leaves the form then the state is stored if needed and at reinstation of state inside <code>code ...</code>, the old state of the dynamic variable will be reinstated. But inside the form state of the dynamic functions is not saved or restored.
</p>
<p>A stronger form of guard can be used in case one would like to make sure that the dynamic functions state can be reinstated in case of e.g. postpone is used, then use,
</p>
<p><code>G.L. (&lt;guard-dynamic-functions&gt; (f ...) code ...)</code>,
</p>
<p>Finally the last section conserns the ability to compile the dynamic function for as fast lookup as possible, use
</p>
<a name="index-_003cdynamic_002dcompile_003e"></a>
<p><code>G.L. (&lt;dynamic-compile&gt; f ...)</code>, comiles the dynamic functions <code>f ...</code> for fast lookup. This ideom takes time and if used freely can cause a high memory consumption.
</p>
<hr>
<div class="header">
<p>
Next: <a href="rational-data.html#rational-data" accesskey="n" rel="next">rational data</a>, Previous: <a href="dynamics.html#dynamics" accesskey="p" rel="prev">dynamics</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<p><code>G.L. (&lt;with-dynamic-functions&gt; (f ...) code ...)</code>, for this form <code>f ...</code>, will be weakly guarded throughout the form e.g. if control leaves the form then the state is stored if needed and at reinstation of state inside <code>code ...</code>, the old state of the dynamic variable will be reinstated. But inside the form state of the dynamic functions is not saved or restored.
<p>A stronger form of guard can be used in case one would like to make sure that the dynamic functions state can be reinstated in case of e.g. postpone is used, then use,
<p><code>G.L. (&lt;guard-dynamic-functions&gt; (f ...) code ...)</code>,
<p>Finally the last section conserns the ability to compile the dynamic function for as fast lookup as possible, use
<p><a name="index-g_t_003cdynamic_002dcompile_003e-183"></a><code>G.L. (&lt;dynamic-compile&gt; f ...)</code>, comiles the dynamic functions <code>f ...</code> for fast lookup. This ideom takes time and if used freely can cause a high memory consumption.
</body></html>
</body>
</html>
<html lang="en">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>dynamics - Preliminary Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Preliminary Manual">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="postpone.html#postpone" title="postpone">
<link rel="next" href="dynamic_002dfunctions.html#dynamic_002dfunctions" title="dynamic-functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<title>Preliminary Manual: dynamics</title>
<meta name="description" content="Preliminary Manual: dynamics">
<meta name="keywords" content="Preliminary Manual: dynamics">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Index.html#Index" rel="index" title="Index">
<link href="index.html#Top" rel="up" title="Top">
<link href="dynamic_002dfunctions.html#dynamic_002dfunctions" rel="next" title="dynamic-functions">
<link href="postpone.html#postpone" rel="prev" title="postpone">
<style type="text/css">
<!--
This is a preliminary manual of guile-log package for guile-2.0
Copyright (C) 2012 Stefan Israelsson Tampe-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body>
<div class="node">
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="dynamics"></a>
<div class="header">
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="dynamic_002dfunctions.html#dynamic_002dfunctions">dynamic-functions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="postpone.html#postpone">postpone</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
Next: <a href="dynamic_002dfunctions.html#dynamic_002dfunctions" accesskey="n" rel="next">dynamic-functions</a>, Previous: <a href="postpone.html#postpone" accesskey="p" rel="prev">postpone</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Dynamics"></a>
<h2 class="chapter">5 Dynamics</h2>
<p>The main work has been to enable delimeted continuation in guile-log. To do this is not easy because we want to support interleaving, postponing, store/restore, zip like construct as well as acumulation all in one theoretically and computationally sound environment. It's not just to store and restore a stack as in scheme. Guile log is targetted to be effective reinstating states and keep a huge number of states in memory, to do this we note 1. we store a tree of states to compress the overall memory need and keep consing to a lower degree. 2. reinstating state information multiple times is cheap because there is no consing the second time for the main stack (we keep another dynamic stack where we need to reinstate new datastructures at each reinstation. All this means that we must reinstate the same datastructure. Also generally the cc and p fields at the in and out of the generated continuation needs to be patched and in order to support all of guile-log seamlessly one need to make use of guarded variables. In all stackless dynamics like kanren employ is not well suited because this is a nonfunctional modification. The resulting continuation will work almost as one would expect, but one can imagine problematics if the evaluation of the continuation returns a failure thunk intended to jump back into the continuation without normal backtracking. This is not a serious flaw and we do not try to add functionality to support this more than that we would implement new versions of the frameworks, looking at that source code it would be clear how to proceed.
<p>The main work has been to enable delimeted continuation in guile-log. To do this is not easy because we want to support interleaving, postponing, store/restore, zip like construct as well as acumulation all in one theoretically and computationally sound environment. It&rsquo;s not just to store and restore a stack as in scheme. Guile log is targetted to be effective reinstating states and keep a huge number of states in memory, to do this we note 1. we store a tree of states to compress the overall memory need and keep consing to a lower degree. 2. reinstating state information multiple times is cheap because there is no consing the second time for the main stack (we keep another dynamic stack where we need to reinstate new datastructures at each reinstation. All this means that we must reinstate the same datastructure. Also generally the cc and p fields at the in and out of the generated continuation needs to be patched and in order to support all of guile-log seamlessly one need to make use of guarded variables. In all stackless dynamics like kanren employ is not well suited because this is a nonfunctional modification. The resulting continuation will work almost as one would expect, but one can imagine problematics if the evaluation of the continuation returns a failure thunk intended to jump back into the continuation without normal backtracking. This is not a serious flaw and we do not try to add functionality to support this more than that we would implement new versions of the frameworks, looking at that source code it would be clear how to proceed.
</p>
<a name="Api"></a>
<h3 class="section">5.1 Api</h3>
<p><a name="index-g_t_003cprompt_003e-173"></a><a name="index-g_t_003cabort_003e-174"></a><a name="index-g_t_003ccatch_003e-175"></a>
<code>G.L. (&lt;prompt&gt; tag data thunk handler)</code>, This will evaluate the guile-log version of a thunk and continue just as with any other thunk. But if an abort with a tag that matches the prompt tag or if the prompt tag is true, any abort will be catched. data will be guile-log dynamic data that will be captured at the abort and transferd back to the prompts context. The handler is of the form
<p><code>(handler tag next kk args ...)</code>, <code>tag</code> is the abort tag, next is a guile-log thunk, that evaluated will try the next handler in the stack. kk is a meta continuation. typically to get the continuaiton <code>k</code>, you do
<p><code>(let ((k (kk))) ...)</code>
<p>But we can also do
<p><code>(let ((k (kk new-handler))) ...)</code>, in cases where one want to use a new
handler for the continuation which is a common pattern in producing generators.
Technically this is not needed, but doing so means the difference between dog
<a name="index-_003cprompt_003e"></a>
<a name="index-_003cabort_003e"></a>
<a name="index-_003ccatch_003e"></a>
<p><code>G.L. (&lt;prompt&gt; tag data thunk handler)</code>, This will evaluate the guile-log version of a thunk and continue just as with any other thunk. But if an abort with a tag that matches the prompt tag or if the prompt tag is true, any abort will be catched. data will be guile-log dynamic data that will be captured at the abort and transferd back to the prompts context. The handler is of the form
</p>
<p><code>(handler tag next kk args ...)</code>, <code>tag</code> is the abort tag, next is a guile-log thunk, that evaluated will try the next handler in the stack. kk is a meta continuation. typically to get the continuaiton <code>k</code>, you do
</p>
<p><code>(let ((k (kk))) ...)</code>
</p>
<p>But we can also do
</p>
<p><code>(let ((k (kk new-handler))) ...)</code>, in cases where one want to use a new
handler for the continuation which is a common pattern in producing generators.
Technically this is not needed, but doing so means the difference between dog
slow code and performant code. Finally we can use,
</p>
<p><code>(let ((k (kk tag new-handler))) ...)</code>, with the obvious interpretation. The return continuation k will have a few arguments that represents the arguments used in the continuation of the abort.
<p><code>(let ((k (kk tag new-handler))) ...)</code>, with the obvious interpretation. The return continuation k will have a few arguments that represents the arguments used in the continuation of the abort.
</p>
<p>The arguments <code>(args ...)</code> are the arguments returned by the abort.
</p>
<p>The arguments <code>(args ...)</code> are the arguments returned by the abort.