|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | +[//]: # (This file is auto-generated. Please do not modify it yourself.) |
| 5 | + |
| 6 | +# asserta/1 |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +`asserta/1` is a predicate that asserts a clause into the database as the first clause of the predicate. |
| 11 | + |
| 12 | +## Signature |
| 13 | + |
| 14 | +```text |
| 15 | +asserta(+Clause) |
| 16 | +``` |
| 17 | + |
| 18 | +Where: |
| 19 | + |
| 20 | +- Clause is the clause to assert into the database. |
| 21 | + |
| 22 | +## Examples |
| 23 | + |
| 24 | +### Assert a fact into the database |
| 25 | + |
| 26 | +This scenario demonstrates the process of asserting a new fact into a Prolog database. In Prolog, asserting a fact means |
| 27 | +adding a new piece of information or *knowledge* into the database, allowing it to be referenced in subsequent queries. |
| 28 | +This is particularly useful when you want to dynamically extend the knowledge base with facts or rules based on conditions |
| 29 | +or interactions during runtime. |
| 30 | + |
| 31 | +Here are the steps of the scenario: |
| 32 | + |
| 33 | +- **Given** the program: |
| 34 | + |
| 35 | +``` prolog |
| 36 | +assert_fact :- asserta(father(john, pete)). |
| 37 | +``` |
| 38 | + |
| 39 | +- **Given** the query: |
| 40 | + |
| 41 | +``` prolog |
| 42 | +assert_fact, father(X, Y). |
| 43 | +``` |
| 44 | + |
| 45 | +- **When** the query is run |
| 46 | +- **Then** the answer we get is: |
| 47 | + |
| 48 | +``` yaml |
| 49 | +height: 42 |
| 50 | +gas_used: 3977 |
| 51 | +answer: |
| 52 | + has_more: false |
| 53 | + variables: ["X", "Y"] |
| 54 | + results: |
| 55 | + - substitutions: |
| 56 | + - variable: X |
| 57 | + expression: john |
| 58 | + - variable: 'Y' |
| 59 | + expression: pete |
| 60 | +``` |
| 61 | +
|
| 62 | +### Only dynamic predicates can be asserted |
| 63 | +
|
| 64 | +This scenario demonstrates that only dynamic predicates can be asserted. In Prolog, dynamic predicates are those that can be |
| 65 | +modified during runtime. This is in contrast to static predicates, which are fixed and cannot be modified. |
| 66 | +
|
| 67 | +Here are the steps of the scenario: |
| 68 | +
|
| 69 | +- **Given** the program: |
| 70 | +
|
| 71 | +``` prolog |
| 72 | +parent(jane, alice). |
| 73 | +``` |
| 74 | + |
| 75 | +- **Given** the query: |
| 76 | + |
| 77 | +``` prolog |
| 78 | +asserta(parent(john, alice)). |
| 79 | +``` |
| 80 | + |
| 81 | +- **When** the query is run |
| 82 | +- **Then** the answer we get is: |
| 83 | + |
| 84 | +``` yaml |
| 85 | +height: 42 |
| 86 | +gas_used: 3975 |
| 87 | +answer: |
| 88 | + has_more: false |
| 89 | + results: |
| 90 | + - error: "error(permission_error(modify,static_procedure,parent/2),asserta/1)" |
| 91 | +``` |
| 92 | +
|
| 93 | +### Show that the fact is asserted at the beginning of the database |
| 94 | +
|
| 95 | +This scenario demonstrates that the asserta/1 predicate adds the fact to the beginning of the database. This means that |
| 96 | +the fact is the first fact to be matched when a query is run. |
| 97 | +
|
| 98 | +This is in contrast to the assertz/1 predicate, which adds the fact to the end of the database. |
| 99 | +
|
| 100 | +Here are the steps of the scenario: |
| 101 | +
|
| 102 | +- **Given** the program: |
| 103 | +
|
| 104 | +``` prolog |
| 105 | +:- dynamic(parent/2). |
| 106 | + |
| 107 | +parent(jane, alice). |
| 108 | + |
| 109 | +assert_fact :- asserta(parent(john, alice)). |
| 110 | +``` |
| 111 | + |
| 112 | +- **Given** the query: |
| 113 | + |
| 114 | +``` prolog |
| 115 | +assert_fact, parent(X, alice). |
| 116 | +``` |
| 117 | + |
| 118 | +- **When** the query is run (limited to 2 solutions) |
| 119 | +- **Then** the answer we get is: |
| 120 | + |
| 121 | +``` yaml |
| 122 | +height: 42 |
| 123 | +gas_used: 3977 |
| 124 | +answer: |
| 125 | + has_more: false |
| 126 | + variables: ["X"] |
| 127 | + results: |
| 128 | + - substitutions: |
| 129 | + - variable: X |
| 130 | + expression: john |
| 131 | + - substitutions: |
| 132 | + - variable: X |
| 133 | + expression: jane |
| 134 | +``` |
| 135 | +
|
| 136 | +### Shows a simple counter example |
| 137 | +
|
| 138 | +This scenario demonstrates a simple counter example using the `asserta/1` and `retract/1` predicates. |
| 139 | +In this example, we represent the value of the counter as a dynamic predicate `counter/1` that is asserted and retracted |
| 140 | +to each time the value of the counter is incremented or decremented. |
| 141 | + |
| 142 | +Here are the steps of the scenario: |
| 143 | + |
| 144 | +- **Given** the program: |
| 145 | + |
| 146 | +``` prolog |
| 147 | +:- dynamic(counter/1). |
| 148 | +
|
| 149 | +counter(0). |
| 150 | +
|
| 151 | +increment_counter :- retract(counter(X)), Y is X + 1, asserta(counter(Y)). |
| 152 | +decrement_counter :- retract(counter(X)), Y is X - 1, asserta(counter(Y)). |
| 153 | +``` |
| 154 | + |
| 155 | +- **Given** the query: |
| 156 | + |
| 157 | +``` prolog |
| 158 | +counter(InitialValue), increment_counter, increment_counter, counter(IncrementedValue), decrement_counter, counter(DecrementedValue). |
| 159 | +``` |
| 160 | + |
| 161 | +- **When** the query is run |
| 162 | +- **Then** the answer we get is: |
| 163 | + |
| 164 | +``` yaml |
| 165 | +height: 42 |
| 166 | +gas_used: 3989 |
| 167 | +answer: |
| 168 | + has_more: false |
| 169 | + variables: ["InitialValue", "IncrementedValue", "DecrementedValue"] |
| 170 | + results: |
| 171 | + - substitutions: |
| 172 | + - variable: InitialValue |
| 173 | + expression: 0 |
| 174 | + - variable: IncrementedValue |
| 175 | + expression: 2 |
| 176 | + - variable: DecrementedValue |
| 177 | + expression: 1 |
| 178 | +``` |
0 commit comments