File: assignvars.lgt

package info (click to toggle)
yap 5.1.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,124 kB
  • ctags: 14,650
  • sloc: ansic: 122,796; perl: 22,545; sh: 3,768; java: 1,277; makefile: 1,191; xml: 739; tcl: 624; lisp: 142; awk: 9
file content (114 lines) | stat: -rw-r--r-- 2,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

/*
This file contains an adaptation to Logtalk of code for logical assignment 
of Prolog terms developed by Nobukuni Kino. For more information, please 
consult the URL http://www.kprolog.com/en/logical_assignment/

As a derivative work, this file is licensed under the Open Software License 
version 2.1 (http://opensource.org/licenses/osl-2.1.php).
*/


:-op(100, xfx, '<=').
:-op(100, xfx, '=>').


:- category(assignvars).


	:- info([
		version is 1.0,
		author is 'Nobukuni Kino and Paulo Moura',
		date is 2005/1/7,
		comment is 'Assignable variables (supporting logical, backtracable assignement of non-variable terms).']).


	:- public(assignable/1).
	:- mode(assignable(-assignvar), one).
	:- info(assignable/1, [
		comment is 'Makes Variable an assignable variable. Initial state will be empty.',
		argnames is ['Variable'],
		exceptions is [
			'Variable is not a variable' - type_error(variable, 'Variable')]]).

	:- public(assignable/2).
	:- mode(assignable(-assignvar, @nonvar), one).
	:- info(assignable/2, [
		comment is 'Makes Variable an assignable variable and sets its initial state to Value.',
		argnames is ['Variable', 'Value'],
		exceptions is [
			'Variable is not a variable' - type_error(variable, 'Variable'),
			'Value is not instantiated' - instantiation_error]]).

	:- public((<=)/2).
	:- mode(<=(?assignvar, @nonvar), one).
	:- info((<=)/2, [
		comment is 'Sets the state of the assignable variable Variable to Value (initializing the variable if needed).',
		argnames is ['Variable', 'Value'],
		exceptions is [
			'Value is not instantiated' - instantiation_error]]).

	:- public((=>)/2).
	:- mode(=>(+assignvar, ?nonvar), zero_or_one).
	:- info((=>)/2, [
		comment is 'Unifies Value with the current state of the assignable variable Variable.',
		argnames is ['Variable', 'Value'],
		exceptions is [
			'Variable is not instantiated' - instantiation_error]]).


	:-op(100, xfx, <=).
	:-op(100, xfx, =>).


	assignable(Assig) :-
		nonvar(Assig),
		self(Self),
		sender(Sender),
		throw(error(type_error(variable, Assig), Self::assignable(Assig), Sender)).

	assignable([_| _]).


	assignable(Assig, Init) :-
		nonvar(Assig),
		self(Self),
		sender(Sender),
		throw(error(type_error(variable, Assig), Self::assignable(Assig, Init), Sender)).

	assignable(Assig, Init) :-
		var(Init),
		self(Self),
		sender(Sender),
		throw(error(instantiation_error, Self::assignable(Assig, Init), Sender)).

	assignable([_, Init| _], Init).


	Assig <= Value :-
		var(Value),
		self(Self),
		sender(Sender),
		throw(error(instantiation_error, Self::Assig <= Value, Sender)).

	[_| Tail] <= Value :-
		nonvar(Tail) ->
			Tail <= Value
			;
			Tail = [Value| _].


	Assig => Value :-
		var(Assig),
		self(Self),
		sender(Sender),
		throw(error(instantiation_error, Self::Assig => Value, Sender)).

	[Current| Tail] => Value :-
		nonvar(Tail) ->
			Tail => Value
			;
			Current = Value.


:- end_category.