Next: , Previous: , Up: prolog   [Index]

16.8 closures in prolog

Closures is an interesting construct a schemer can’t stay away from and we support theme in guile-log. There is a complication with logic programs though, when we are comparing closures we compare code, they may differ, but still produce the same logic hence when there is a high demand to find all solutions, e.g. the predicate is closed, we may produce the wrong answer. Unfourtunately it’s impossible to verify if the closures logically are the same or not and hence we will need to in some circumstances throw an error when such a unification is under action. Also when unifying closures we need to open up the closures and unify the state as well. We also support that quoted writes can read in the closures correctly with the straightforward logic of the logic beeing defined by the term. Also when we assert*() code that includes closures they will be compiled and handled just as with writes, e.g. the state of the code will be the world and connection outside that world will be lost e.g. in

 f(X) :- Y=_,asserta(g :- {var(Y)=1}.),g.

Y will be unbound although the var(Y) refers to the outside variable Y.

The semantic of the closure creation is basically through code inside curly brackets that defined the closure and it is placed at the code segments in prolog and the generated closure objects that can be treated just as with normal data. To define a closure that is closed used two curly braces e.g. {{...def...}}. The definition can be normal prolog definitions, and the last predicate will name and sellect the code that will represent the closure. All other functions will represent local names and will not be reachable e.g. in

f :- Y=_, h({{ a(X) :- X=v(Y).
               b(X) :- Z=[v[Y],1],a(Z).

Then a is local and b will create a parent name of the closure which will be defined in the current module. The closure is closed and hence if h unifies the closure with another closure an error (that’s controllable) may be thrown or it may backtrack. There is a possibility to force a name on a closure by prefixing the curly braces with a named symbol. Then the code with the one associated with matching name in the definition will be used unless the closure is a one liner annonymous construction, then it will name the annonymous code. Else for an annonymous closure if no name is supplied as a prefix a automatically generated symbol will be used instead. There is two annonymous versions of one liners that can be used, for closures with arguments use e.g. clos1{A,B|...code...} and with no argument just use clos2{...code...}. To refere to a variable in the closest outer context, where it is defined, use var[X], or v[X]. Do not refere to global variables, that will throw an error, for that use scm[X] in which case the logic is flawed, don’t practice this for logic programs.

To control the behavior of unification of closed closures, we will have the following api found in [use-modules (logic guile-log prolog closed)].,

prolog error_at_closed_p_handle(Handle), this predicate fetches a handle that represent the predicate of issuing a fail at non matching unification of the codes part of a closed closure. This handle can be guarded by the ideoms in the section about dynamic features so it is really simple to restrict the value to just one part of the code. For a true value of the handle, error will happen at a code missmatch in a unification with at least one closed closure, and else if false, backtracking will result.

prolog close_error_true/0 close_error_false/0, use these funcitons to turn on/off the throwing of error at unification missmatcehs of closed closures code part.

To reflect the closure we have from [use-modules (logic guile-log guile-prolog closures)], and api.

prolog closure_p(Cl), true if Cl is a closure.

prolog closure_state_ref(Cl,L), will expose the state of the closure Cl into L.

prolog closure_is_closed(Cl), true if Cl is a closed closure.

prolog closure_code_ref(Cl,F), will expose the code of the closure Cl into F.

Next: , Previous: , Up: prolog   [Index]