boolalg¶
ai.boolalg
¶
Symbolic Propositional Logic for Knowledge representation.
Propositional logic (PL) is the simplest form of logic where all the statements are made by propositions. A proposition is a declarative statement which is either true or false. It is a technique of knowledge representation in logical and mathematical form.
Example:
```python
from ai import (Symbol, And, Or, Not, Implication)
rain = Symbol("rain")
hagrid = Symbol("hagrid")
dumbledore = Symbol("dumbledore")
knowledge = And(
Implication(Not(rain), hagrid),
Or(hagrid, dumbledore),
Not(And(hagrid, dumbledore)),
dumbledore
)
print(model_check(knowledge, rain))
```
And
¶
Bases: _Sentence
And class implements the properties and behaviour of the (∧) symbol.
In a propositional logic (PL) the AND symbol works similar to the (*)
multiplication operator in algebra that multiplies multiple experssions into
one single value. For example, if we have two symbols with value true and
false (where true is equals to 1 and false is equals to 0) then the
AND operator between these two symbols will result in a new value 0.
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∧ Run'
Source code in ai/boolalg/logic.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
__eq__(other)
¶
__hash__()
¶
__init__(*conjuncts)
¶
Constructs a And instance.
A Symbol instance is used while constructing a And instance as
conjunctions. These symbol will then be evaluated with a (∧) operator
every time a propositional logic model is evaluated. If the value of all the
symbols holds true then the evaluated value of the entire expression will
be true and if the value of any one symbol holds false in the
conjunctions then the entire propositional logic expression evaluates to
false.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conjunct
|
A tuple of conjunctions constructing a |
required |
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∧ Run'
Source code in ai/boolalg/logic.py
__repr__()
¶
add(conjunct)
¶
Appends a conjuction to the current experssion for And.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conjunct
|
_Sentence
|
A conjunction that constructs a |
required |
Example:
>>> from ai.boolalg import (Symbol, And)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'rain ∧ run'
>>> umbrella = Symbol('umbrella')
>>> knowledge.add(umbrella)
>>> knowledge.formula()
'rain ∧ run ∧ umbrella'
Source code in ai/boolalg/logic.py
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the values of the
conjunctions the current expression holds. For example if,
\(P \implies \mathrm{True}\) and \(Q \implies \mathrm{false}\)
in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the current expression. |
Example:
>>> from ai.boolalg import (Symbol, And)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> umbrella = Symbol('umbrella')
>>> knowledge = And(rain, run, umbrella)
>>> knowledge.formula()
'rain ∧ run ∧ umbrella'
>>> model = dict()
>>> model[str(rain)] = True
>>> model[str(umbrella)] = True
>>> model[str(run)] = False
>>> knowledge.evaluate(model)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the conjunctions with the
(∧) operator that can later be joined with other operators and operands
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> from ai.boolalg import Symbol, And
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'rain ∧ run'
Source code in ai/boolalg/logic.py
Biconditional
¶
Bases: _Sentence
Source code in ai/boolalg/logic.py
Implication
¶
Bases: _Sentence
Source code in ai/boolalg/logic.py
Not
¶
Bases: _Sentence
Not class implements the properties and behaviour of the (¬) symbol.
In a propositional logic (PL) the NOT symbol inverts the value of the
symbol it is used with. For example, if the value of the symbol is true then
it will be evaluated to false and vice-versa.
Example:
>>> from logic import (Not, Symbol)
>>> rain = Symbol('Rain')
>>> Not(rain).formula()
'¬Rain'
Source code in ai/boolalg/logic.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
__eq__(other)
¶
__hash__()
¶
__init__(operand)
¶
Constructs a Not instance.
A Symbol instance is used while constructing a Not instance. This symbol
will then be evaluated with a (¬) operator every time a propositional
logic model is evaluated. If the value of the symbol holds true then the
evaluated value of the entire expression will be false and vice-versa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
_Sentence
|
An instance of a |
required |
Example:
>>> from logic import (Not, Symbol)
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> knowledge.formula()
'¬P'
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the value of the current
expression. For example, for a symbol \(¬P \implies \mathrm{True}\) in
a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Dict[str, bool]
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the current expression. |
Example:
>>> from ai import Symbol, Not
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> model_true = dict()
>>> model_true[str(P)] = True
>>> knowledge.evaluate(model_true)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the Symbol with the (¬)
operator that can later be joined with other operators and operands to
complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> P = Symbol('P')
>>> knowledge(Not(P))
>>> knowledge.formula()
'¬P'
Source code in ai/boolalg/logic.py
Or
¶
Bases: _Sentence
Or class implements the properties and behaviour of the (∨) symbol.
In a propositional logic (PL) the OR symbol works similar to the (+)
addition operator in algebra that adds multiple experssions into one single
value. For example, if we have two symbols with value true and false
(where true is equals to 1 and false is equals to 0) then the OR
operator between these two symbols will result in a new value 1.
Note:
Unlike algebra, boolean algebra adds multiple true values into one single
true value which is equals to 1;
\(\mathrm{true} + \mathrm{true} \implies 1\)
Example:
>>> from logic import (Or, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = Or(rain, run)
>>> knowledge.formula()
'Rain ∨ Run'
Source code in ai/boolalg/logic.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
__eq__(other)
¶
__hash__()
¶
__init__(*disjuncts)
¶
Constructs a Or instance.
A Symbol instance is used while constructing a Or instance as
disjunctions. These symbol will then be evaluated with a (∨) operator
every time a propositional logic model is evaluated. If the value of all the
symbols holds true then the evaluated value of the entire expression will
be true and if the value of any all the symbols holds false in the
disjunctions then the entire propositional logic expression evaluates to
false, unless a true symbol sneaks its way into the Or expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disjuncts
|
_Sentence
|
A tuple of disjunctions constructing a |
()
|
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∨ Run'
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the values of the
disjunctions the current expression holds. For example if,
\(P \implies \mathrm{True}\) and \(Q \implies \mathrm{false}\)
in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
|
The evaluated model of the current expression. |
Example:
>>> from ai.boolalg import (Symbol, Or)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> umbrella = Symbol('umbrella')
>>> knowledge = Or(rain, run, umbrella)
>>> knowledge.formula()
'rain ∨ run ∨ umbrella'
>>> model = dict()
>>> model[str(rain)] = True
>>> model[str(umbrella)] = True
>>> model[str(run)] = False
>>> knowledge.evaluate(model)
True
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the disjunctions with the
(∨) operator that can later be joined with other operators and operands
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
|
String representation of the current symbol. |
Example:
>>> from ai.boolalg import Symbol, Or
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = Or(rain, run)
>>> knowledge.formula()
'rain ∨ run'
Source code in ai/boolalg/logic.py
Symbol
¶
Bases: _Sentence
Symbol class creates a symbol for a propositional logic (PL).
A proposition is a declarative statement which is either true or false. The
symbols used in propositional logic determines whether the logic holds true
or turns out to be false.
Example:
>>> from logic import (Symbol, And)
>>> P = Symbol('P')
>>> Q = Symbol('Q')
>>> knowledge = And(P, Q)
>>> knowledge.formula()
'P ∧ Q'
Source code in ai/boolalg/logic.py
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
__eq__(other)
¶
__hash__()
¶
__init__(name)
¶
Constructs a Symbol instance.
A symbol in propositional logic (PL) either holds a truth value or a false value, based on the combination of logical operations these values are then transformed to a result.
Note:
You can use letters, words, digits, or even special characters to contruct symbols as long as they hold some meaning for the viewer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
|
required |
Example:
>>> P = Symbol('P')
>>> Q = Symbol('Q')
>>> R = Symbol('R')
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of a symbol in a propositional logic (PL).
Evaluating a model means evaluating the value of each symbol in the
propositional logic (PL) which either holds True or False.
For example, for a symbol \(P \implies \mathrm{True}\) in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Dict[str, bool]
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the symbol. |
Example:
>>> from ai import Symbol, Not
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> model_true[str(P)] = True
>>> knowledge.evaluate(model_true)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the name of the symbol to be used in the formula.
This function is later used to combine the operators with the operands i.e.,
the symbols which are a Symbol instance. This function returns the string
representation of the symbols which is then later joined with the operators
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> P = Symbol('P')
>>> P.formula()
'P'
Source code in ai/boolalg/logic.py
model_check(knowledge, query)
¶
Checks if knowledge base entails query.
Source code in ai/boolalg/logic.py
logic
¶
And
¶
Bases: _Sentence
And class implements the properties and behaviour of the (∧) symbol.
In a propositional logic (PL) the AND symbol works similar to the (*)
multiplication operator in algebra that multiplies multiple experssions into
one single value. For example, if we have two symbols with value true and
false (where true is equals to 1 and false is equals to 0) then the
AND operator between these two symbols will result in a new value 0.
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∧ Run'
Source code in ai/boolalg/logic.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
__eq__(other)
¶
__hash__()
¶
__init__(*conjuncts)
¶
Constructs a And instance.
A Symbol instance is used while constructing a And instance as
conjunctions. These symbol will then be evaluated with a (∧) operator
every time a propositional logic model is evaluated. If the value of all the
symbols holds true then the evaluated value of the entire expression will
be true and if the value of any one symbol holds false in the
conjunctions then the entire propositional logic expression evaluates to
false.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conjunct
|
A tuple of conjunctions constructing a |
required |
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∧ Run'
Source code in ai/boolalg/logic.py
__repr__()
¶
add(conjunct)
¶
Appends a conjuction to the current experssion for And.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conjunct
|
_Sentence
|
A conjunction that constructs a |
required |
Example:
>>> from ai.boolalg import (Symbol, And)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'rain ∧ run'
>>> umbrella = Symbol('umbrella')
>>> knowledge.add(umbrella)
>>> knowledge.formula()
'rain ∧ run ∧ umbrella'
Source code in ai/boolalg/logic.py
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the values of the
conjunctions the current expression holds. For example if,
\(P \implies \mathrm{True}\) and \(Q \implies \mathrm{false}\)
in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the current expression. |
Example:
>>> from ai.boolalg import (Symbol, And)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> umbrella = Symbol('umbrella')
>>> knowledge = And(rain, run, umbrella)
>>> knowledge.formula()
'rain ∧ run ∧ umbrella'
>>> model = dict()
>>> model[str(rain)] = True
>>> model[str(umbrella)] = True
>>> model[str(run)] = False
>>> knowledge.evaluate(model)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the conjunctions with the
(∧) operator that can later be joined with other operators and operands
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> from ai.boolalg import Symbol, And
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'rain ∧ run'
Source code in ai/boolalg/logic.py
Biconditional
¶
Bases: _Sentence
Source code in ai/boolalg/logic.py
Implication
¶
Bases: _Sentence
Source code in ai/boolalg/logic.py
Not
¶
Bases: _Sentence
Not class implements the properties and behaviour of the (¬) symbol.
In a propositional logic (PL) the NOT symbol inverts the value of the
symbol it is used with. For example, if the value of the symbol is true then
it will be evaluated to false and vice-versa.
Example:
>>> from logic import (Not, Symbol)
>>> rain = Symbol('Rain')
>>> Not(rain).formula()
'¬Rain'
Source code in ai/boolalg/logic.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
__eq__(other)
¶
__hash__()
¶
__init__(operand)
¶
Constructs a Not instance.
A Symbol instance is used while constructing a Not instance. This symbol
will then be evaluated with a (¬) operator every time a propositional
logic model is evaluated. If the value of the symbol holds true then the
evaluated value of the entire expression will be false and vice-versa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operand
|
_Sentence
|
An instance of a |
required |
Example:
>>> from logic import (Not, Symbol)
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> knowledge.formula()
'¬P'
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the value of the current
expression. For example, for a symbol \(¬P \implies \mathrm{True}\) in
a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Dict[str, bool]
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the current expression. |
Example:
>>> from ai import Symbol, Not
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> model_true = dict()
>>> model_true[str(P)] = True
>>> knowledge.evaluate(model_true)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the Symbol with the (¬)
operator that can later be joined with other operators and operands to
complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> P = Symbol('P')
>>> knowledge(Not(P))
>>> knowledge.formula()
'¬P'
Source code in ai/boolalg/logic.py
Or
¶
Bases: _Sentence
Or class implements the properties and behaviour of the (∨) symbol.
In a propositional logic (PL) the OR symbol works similar to the (+)
addition operator in algebra that adds multiple experssions into one single
value. For example, if we have two symbols with value true and false
(where true is equals to 1 and false is equals to 0) then the OR
operator between these two symbols will result in a new value 1.
Note:
Unlike algebra, boolean algebra adds multiple true values into one single
true value which is equals to 1;
\(\mathrm{true} + \mathrm{true} \implies 1\)
Example:
>>> from logic import (Or, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = Or(rain, run)
>>> knowledge.formula()
'Rain ∨ Run'
Source code in ai/boolalg/logic.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
__eq__(other)
¶
__hash__()
¶
__init__(*disjuncts)
¶
Constructs a Or instance.
A Symbol instance is used while constructing a Or instance as
disjunctions. These symbol will then be evaluated with a (∨) operator
every time a propositional logic model is evaluated. If the value of all the
symbols holds true then the evaluated value of the entire expression will
be true and if the value of any all the symbols holds false in the
disjunctions then the entire propositional logic expression evaluates to
false, unless a true symbol sneaks its way into the Or expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disjuncts
|
_Sentence
|
A tuple of disjunctions constructing a |
()
|
Example:
>>> from logic import (And, Symbol)
>>> rain = Symbol('Rain')
>>> run = Symbol('Run')
>>> knowledge = And(rain, run)
>>> knowledge.formula()
'Rain ∨ Run'
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of the current expression i.e., self in the
propositional logic (PL).
Evaluating a model for self means evaluating the values of the
disjunctions the current expression holds. For example if,
\(P \implies \mathrm{True}\) and \(Q \implies \mathrm{false}\)
in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
|
The evaluated model of the current expression. |
Example:
>>> from ai.boolalg import (Symbol, Or)
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> umbrella = Symbol('umbrella')
>>> knowledge = Or(rain, run, umbrella)
>>> knowledge.formula()
'rain ∨ run ∨ umbrella'
>>> model = dict()
>>> model[str(rain)] = True
>>> model[str(umbrella)] = True
>>> model[str(run)] = False
>>> knowledge.evaluate(model)
True
Source code in ai/boolalg/logic.py
formula()
¶
Returns the expression for self that is to be used in the formula.
This function returns a string representation of the disjunctions with the
(∨) operator that can later be joined with other operators and operands
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
|
String representation of the current symbol. |
Example:
>>> from ai.boolalg import Symbol, Or
>>> rain = Symbol('rain')
>>> run = Symbol('run')
>>> knowledge = Or(rain, run)
>>> knowledge.formula()
'rain ∨ run'
Source code in ai/boolalg/logic.py
Symbol
¶
Bases: _Sentence
Symbol class creates a symbol for a propositional logic (PL).
A proposition is a declarative statement which is either true or false. The
symbols used in propositional logic determines whether the logic holds true
or turns out to be false.
Example:
>>> from logic import (Symbol, And)
>>> P = Symbol('P')
>>> Q = Symbol('Q')
>>> knowledge = And(P, Q)
>>> knowledge.formula()
'P ∧ Q'
Source code in ai/boolalg/logic.py
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
__eq__(other)
¶
__hash__()
¶
__init__(name)
¶
Constructs a Symbol instance.
A symbol in propositional logic (PL) either holds a truth value or a false value, based on the combination of logical operations these values are then transformed to a result.
Note:
You can use letters, words, digits, or even special characters to contruct symbols as long as they hold some meaning for the viewer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
|
required |
Example:
>>> P = Symbol('P')
>>> Q = Symbol('Q')
>>> R = Symbol('R')
Source code in ai/boolalg/logic.py
__repr__()
¶
evaluate(model)
¶
Evaluates the value of a symbol in a propositional logic (PL).
Evaluating a model means evaluating the value of each symbol in the
propositional logic (PL) which either holds True or False.
For example, for a symbol \(P \implies \mathrm{True}\) in a propositional logic (PL) —
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Dict[str, bool]
|
A propositional logic model mapping from the symbol name to its truth or false value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The evaluated model of the symbol. |
Example:
>>> from ai import Symbol, Not
>>> P = Symbol('P')
>>> knowledge = Not(P)
>>> model_true[str(P)] = True
>>> knowledge.evaluate(model_true)
False
Source code in ai/boolalg/logic.py
formula()
¶
Returns the name of the symbol to be used in the formula.
This function is later used to combine the operators with the operands i.e.,
the symbols which are a Symbol instance. This function returns the string
representation of the symbols which is then later joined with the operators
to complete the propositional logic (PL).
Returns:
| Type | Description |
|---|---|
str
|
String representation of the current symbol. |
Example:
>>> P = Symbol('P')
>>> P.formula()
'P'
Source code in ai/boolalg/logic.py
model_check(knowledge, query)
¶
Checks if knowledge base entails query.