dynamic_002dfunctions.html 6.48 KB
Newer Older
1 2 3 4
<!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 -->
5
<!-- Created by GNU Texinfo 6.1, http://www.gnu.org/software/texinfo/ -->
6
<head>
7 8 9 10 11 12 13 14 15 16 17 18 19 20
<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">
21
<!--
22
a.summary-letter {text-decoration: none}
23 24
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
25 26 27 28 29 30 31
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
32
kbd {font-style: oblique}
33 34 35 36 37 38 39 40
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}
41 42 43
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
44 45 46 47 48
ul.no-bullet {list-style: none}
-->
</style>


49
</head>
50

51
<body lang="en">
52
<a name="dynamic_002dfunctions"></a>
53
<div class="header">
54
<p>
55
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>
56
</div>
57 58
<hr>
<a name="Dynamic-functions"></a>
59
<h2 class="chapter">6 Dynamic functions</h2>
60 61 62 63 64
<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>
65
<h3 class="section">6.1 Api</h3>
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
<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 
89
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
</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>
107 108 109



110 111
</body>
</html>