Global Variables in Prolog

As you can see in many of the files already posted to this website, variables in Prolog must start with a capital letter. But a much more important (and interesting!) fact about Prolog variables is that they are all local variables. If we look at our superhero knowledgebase (the wildcard assignment), Y1 and Y2 from older are exclusive to older, and cannot be used in any other rule. While there is a lot you can do with this, it is nice to be able to compute with global variables in Prolog. So, we can implement a global variable abstract data type (ADT) in Prolog by introducing rules that assert and retract facts to the knowledgebase. This page will outline how to do this in a non-destructive manner, meaning the values bound to variables will not be overridden when operations are performed with them.

In order to do this, you must add an additional parameter to any operation we want to make. So,

add(Variable,Number)

retract(binding(Variable,Value)),
NewValue is Value + Number,
assert(binding(Variable,NewValue)).

becomes

add(Variable1,Variable2,NewVariable)

binding(Variable1,N1), binding(Variable2,N2),
NewValue is N1 + N2,
assert(binding(NewVariable,NewValue)).

If we do this with all of the methods from our (gv1.pl) file, then we are able to perform basic arithmetic without destroying the values that are bound to Variable1 and Variable2. Read below for what the new functions look like, and what they do. An important note is that all of the arithmetic functions take in global variables, meaning that declare(VariableName,VariableValue). has been run.

declare(Variable,Value)
Asserts the fact binding(Variable,Value) to the KB if there is not a binding fact with that variable does not already exist in the KB. If there is such a fact, it retracts it (removes it from the KB), and asserts binding(Variable,Value). The purpose of this program is to create an interface for using this in a way that preserves the binding facts that are already in the KB.

bindings
Displays all of the binding facts that are in the KB in the form Variable -> Value. This is an easy way to make sure that variables are not losing their bindings, and that new variables with new bindings are asserted.

add(Variable1,Variable2,Result)
Adds the value bound to Variable1 and the value bound to Variable2, and binds the result to the value of Result. Both Variable1 and Variable2 must be global variables bound to numbers in order for this function to work.

sub(Variable1,Variable2,Result)
Subtracts the value bound to Variable2 from the value bound to Variable1, and binds the result to the value of Result. Both Variable1 and Variable2 must be global variables bound to numbers in order for this function to work.

mul(Variable1,Variable2,Result)
Multiplies the value bound to Variable1 and the value bound to Variable2, and binds the result to the value of Result. Both Variable1 and Variable2 must be global variables bound to numbers in order for this function to work.

div(Variable1,Variable2,Result)
Divides the value bound to Variable1 and the value bound to Variable2, and binds the result to the value of Result. Both Variable1 and Variable2 must be global variables bound to numbers in order for this function to work.

pow(Variable1,Variable2,Result)
Raises the value bound to Variable1 to the value bound to Variable2, and binds the result to the value of Result. Both Variable1 and Variable2 must be global variables bound to numbers in order for this function to work.