Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global variable in prolog

I have a list that I create as follows:

tab([(top,left),(top,middle),(top,right),(center,left),(center,middle),
     (center,right),(bottom,left),(bottom,middle),(bottom,right)]).

I wish to create a global variable AllPosition that is a tab. So I did the following:

tab(AllPos).

Is this right?

Then I have to follow problem: I have a function that receives one of the pair in tab. That I wish to remove. So I did this:

place(Line, Column, Tab) :-
AllPos \== [_,_] /*while AllPos isn't empty - not sur if this is done this way*/ -> (member((Line,Column), AllPos) -> (erase(AllPos, (Line,Column), AllPos)).

where erase(List, Element, NewList) erases the element Element from List and creates a new list NewList equal to List but without Element. Both functions member and erase are working.

The thing is... As you might have noticed I use AllPoseverywhere. That's because I want to, I want to modify it so I can use it later (after having removed some elements from it), in another function. Is my logic right? Will I be able to use modified AllPos in another function? Thanks

like image 390
FriedRike Avatar asked May 18 '12 15:05

FriedRike


People also ask

How do you define a global variable in Prolog?

Global variables are associations between names (atoms) and terms. They differ in various ways from storing information using assert/1 or recorda/3. The value lives on the Prolog (global) stack. This implies that lookup time is independent of the size of the term.

How do you create a variable in Prolog?

A variable in Prolog is a string of letters, digits, and underscores ( _ ) beginning either with a capital letter or with an underscore. Examples: X , Sister , _ , _thing , _y47 , First_name , Z2 The variable _ is used as a "don't-care" variable, when we don't mind what value the variable has.

Can you assign variables in Prolog?

In PROLOG variables can be substituted with certain values. This means that a variable can be assigned some value or be bound to it. The assignment can be annulled as a result of backtracking and then a new value can be assigned to the variable.

What is the scope of a variable in Prolog?

The scope of a variable is a rule or fact, while the scope of a constant is the whole program. For example, "George is funny" can be represented as a Prolog fact: funny(george).


1 Answers

In SWI-Prolog you can use: b_setval(name, value) and b_getval(name, value). And in case you don't want the values change back in case of backtracking, you can make them actual global by using: nb_setval(name, value) and nb_getval(name, value).

Thus for example if you have a program and you want to check how often it went through a certain path, you can use:

recursive(100).
recursive(X) :- add, Y is X + 1, recursive(Y).

add :- nb_getval(counter, C), CNew is C + 1, nb_setval(counter, CNew).

testRecursion
:-
    % Set counter to zero
    nb_setval(counter, 0),

    % Run some code with 'add'
    recursive(0), !,

    % Print the results
    nb_getval(counter, CounterValue),
    write('Steps: '), writeln(CounterValue).

This is good for some experimental cases, but in general you will want to avoid global variables in Prolog because Prolog means programming in logic.

like image 55
Yeti Avatar answered Oct 13 '22 18:10

Yeti