| 1 | |
|---|
| 2 | /* |
|---|
| 3 | This file contains an adaptation to Logtalk of code for logical assignment |
|---|
| 4 | of Prolog terms developed by Nobukuni Kino. For more information, please |
|---|
| 5 | consult the URL http://www.kprolog.com/en/logical_assignment/ |
|---|
| 6 | |
|---|
| 7 | As a derivative work, this file is licensed under the Open Software License |
|---|
| 8 | version 2.1 (http://opensource.org/licenses/osl-2.1.php). |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | :-op(100, xfx, '<='). |
|---|
| 13 | :-op(100, xfx, '=>'). |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | :- category(assignvars). |
|---|
| 17 | |
|---|
| 18 | :- info([ |
|---|
| 19 | version is 1.0, |
|---|
| 20 | author is 'Nobukuni Kino and Paulo Moura', |
|---|
| 21 | date is 2005/1/7, |
|---|
| 22 | comment is 'Assignable variables (supporting logical, backtracable assignement of non-variable terms).']). |
|---|
| 23 | |
|---|
| 24 | :- public(assignable/1). |
|---|
| 25 | :- mode(assignable(-assignvar), one). |
|---|
| 26 | :- info(assignable/1, [ |
|---|
| 27 | comment is 'Makes Variable an assignable variable. Initial state will be empty.', |
|---|
| 28 | argnames is ['Variable'], |
|---|
| 29 | exceptions is [ |
|---|
| 30 | 'Variable is not a variable' - type_error(variable, 'Variable')]]). |
|---|
| 31 | |
|---|
| 32 | :- public(assignable/2). |
|---|
| 33 | :- mode(assignable(-assignvar, @nonvar), one). |
|---|
| 34 | :- info(assignable/2, [ |
|---|
| 35 | comment is 'Makes Variable an assignable variable and sets its initial state to Value.', |
|---|
| 36 | argnames is ['Variable', 'Value'], |
|---|
| 37 | exceptions is [ |
|---|
| 38 | 'Variable is not a variable' - type_error(variable, 'Variable'), |
|---|
| 39 | 'Value is not instantiated' - instantiation_error]]). |
|---|
| 40 | |
|---|
| 41 | :- public((<=)/2). |
|---|
| 42 | :- mode(<=(?assignvar, @nonvar), one). |
|---|
| 43 | :- info((<=)/2, [ |
|---|
| 44 | comment is 'Sets the state of the assignable variable Variable to Value (initializing the variable if needed).', |
|---|
| 45 | argnames is ['Variable', 'Value'], |
|---|
| 46 | exceptions is [ |
|---|
| 47 | 'Value is not instantiated' - instantiation_error]]). |
|---|
| 48 | |
|---|
| 49 | :- public((=>)/2). |
|---|
| 50 | :- mode(=>(+assignvar, ?nonvar), zero_or_one). |
|---|
| 51 | :- info((=>)/2, [ |
|---|
| 52 | comment is 'Unifies Value with the current state of the assignable variable Variable.', |
|---|
| 53 | argnames is ['Variable', 'Value'], |
|---|
| 54 | exceptions is [ |
|---|
| 55 | 'Variable is not instantiated' - instantiation_error]]). |
|---|
| 56 | |
|---|
| 57 | :-op(100, xfx, <=). |
|---|
| 58 | :-op(100, xfx, =>). |
|---|
| 59 | |
|---|
| 60 | assignable(Assig) :- |
|---|
| 61 | nonvar(Assig), |
|---|
| 62 | self(Self), |
|---|
| 63 | sender(Sender), |
|---|
| 64 | throw(error(type_error(variable, Assig), Self::assignable(Assig), Sender)). |
|---|
| 65 | assignable([_| _]). |
|---|
| 66 | |
|---|
| 67 | assignable(Assig, Init) :- |
|---|
| 68 | nonvar(Assig), |
|---|
| 69 | self(Self), |
|---|
| 70 | sender(Sender), |
|---|
| 71 | throw(error(type_error(variable, Assig), Self::assignable(Assig, Init), Sender)). |
|---|
| 72 | assignable(Assig, Init) :- |
|---|
| 73 | var(Init), |
|---|
| 74 | self(Self), |
|---|
| 75 | sender(Sender), |
|---|
| 76 | throw(error(instantiation_error, Self::assignable(Assig, Init), Sender)). |
|---|
| 77 | assignable([_, Init| _], Init). |
|---|
| 78 | |
|---|
| 79 | Assig <= Value :- |
|---|
| 80 | var(Value), |
|---|
| 81 | self(Self), |
|---|
| 82 | sender(Sender), |
|---|
| 83 | throw(error(instantiation_error, Self::Assig <= Value, Sender)). |
|---|
| 84 | |
|---|
| 85 | [_| Tail] <= Value :- |
|---|
| 86 | nonvar(Tail) -> |
|---|
| 87 | Tail <= Value |
|---|
| 88 | ; |
|---|
| 89 | Tail = [Value| _]. |
|---|
| 90 | |
|---|
| 91 | Assig => Value :- |
|---|
| 92 | var(Assig), |
|---|
| 93 | self(Self), |
|---|
| 94 | sender(Sender), |
|---|
| 95 | throw(error(instantiation_error, Self::Assig => Value, Sender)). |
|---|
| 96 | |
|---|
| 97 | [Current| Tail] => Value :- |
|---|
| 98 | nonvar(Tail) -> |
|---|
| 99 | Tail => Value |
|---|
| 100 | ; |
|---|
| 101 | Current = Value. |
|---|
| 102 | |
|---|
| 103 | :- end_category. |
|---|