[Next] [Up] [Previous] [Contents]
Next: Defining New Types Up: Tutorial Previous: Unification and Freshness   Contents

Lists and Namespaces

$ \alpha $Prolog has a small list library. To import the list library, do:
?- #use "list.apl".
This indicates that "list.apl" defines a namespace "List" which contains several predicates defining common list operations. To refer to list predicates within the namespace, we prefix them with "List.".

For example, to append two lists we can do

?- List.append([1,2],[3,4],X).
Yes. 
X = [1,2,3,4]
Of course, as usual in logic programming we can also run predicates ``backwards'' and nondeterministically:
?- List.append(X,Y,[1,2,3,4]).
Yes.
X = [] 
Y = [1,2,3,4] 
;
Yes.
X = [1] 
Y = [2,3,4] 
;
...
or solve for unknowns:
?- List.append([1|Y],Y,[1,X,4]).
Yes.
X = 4 
Y = [4]

Finally, we can ``open'' a namespace, which makes all of its identifiers (including other namespaces) bound locally so that we don't have to refer to them using explicitly qualified names. For example:

?- #open List.
?- append([1|Y],Y,[1,4,4]).
Yes.
Y = [4]
Warning: Using "open" incautiously can result in strange behavior. It shadows any previous identifier declarations in the current namespace with new ones; however, existing definitions that referred to the old identifiers will not change. Thus, had we already defined our own version of "append" (possibly having a different type signature), after opening "List" we can no longer refer to this definition by name. However had we also defined a predicate "foo" in terms of the shadowed "append", "foo" would continue to work.

In the rest of this tutorial we assume that "List" has been opened.


[Next] [Up] [Previous] [Contents]
Next: Defining New Types Up: Tutorial Previous: Unification and Freshness   Contents
James Cheney 2003-10-23