Data Model
Data model for the typed-logic framework.
This module defines the core classes and structures used to represent logical constructs such as sentences, terms, predicates, and theories. It is based on the Common Logic Interchange Format (CLIF) and the Common Logic Standard (CL), with additions to make working with simple type systems easier.
Logical axioms are called sentences which organized into theories., which can be loaded into a solver.
While one of the goals of typed-logic is to be able to write logic intuitively in Python, this data model is independent of the mapping from the Python language to the logic language; it can be used independently of the python syntax.
Here is an example:
>>> from typedlogic import Term, Forall, Implies
>>> x = Variable('x')
>>> y = Variable('y')
>>> pdef = PredicateDefinition(predicate='FriendOf',
... arguments={'x': 'str', 'y': 'str'}),
>>> theory = Theory(
... name="My theory",
... predicate_definitions=[pdef],
... )
>>> s = Forall([x, y],
... Implies(Term('friend_of', x, y),
... Term('friend_of', y, x)))
>>> theory.add(s)
PredicateDefinition
dataclass
Defines the name and arguments of a predicate.
>>> pdef = PredicateDefinition(predicate='FriendOf',
... arguments={'x': 'str', 'y': 'str'})
The arguments are mappings between variable names and types. You can use either base types (e.g. 'str', 'int', 'float') or custom types.
Custom types should be defined in the theory's type_definitions
attribute.
>>> pdef = PredicateDefinition(predicate='FriendOf',
... arguments={'x': 'Person', 'y': 'Person'})
>>> theory = Theory(
... name="My theory",
... type_definitions={'Person': 'str'},
... predicate_definitions=[pdef],
... )
classDiagram
class PredicateDefinition {
+String predicate
+Dict arguments
+String description
+Dict metadata
}
PredicateDefinition --> "*" PredicateDefinition : parents
Source code in src/typedlogic/datamodel.py
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 |
|
from_class(python_class)
classmethod
Create a predicate definition from a python class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicate_class
|
|
required |
Returns:
Type | Description |
---|---|
PredicateDefinition
|
|
Source code in src/typedlogic/datamodel.py
97 98 99 100 101 102 103 104 105 106 107 108 |
|
Variable
dataclass
A variable in a logical sentence.
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = Forall([x, y],
... Implies(Term('friend_of', x, y),
... Term('friend_of', y, x)))
Variables can have domains (types) specified:
>>> x = Variable('x', domain='str')
>>> y = Variable('y', domain='str')
>>> z = Variable('y', domain='int')
>>> xa = Variable('xa', domain='int')
>>> ya = Variable('ya', domain='int')
>>> s = Forall([x, y, z],
... Implies(And(Term('ParentOf', x, y),
... Term('Age', x, xa),
... Term('Age', y, ya)),
... Term('OlderThan', x, y)))
The domains should be either base types or defined types in the theory's `type_definitions` attribute.
Source code in src/typedlogic/datamodel.py
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 |
|
Sentence
Bases: ABC
Base class for logical sentences.
Do not use this class directly; use one of the subclasses instead.
Model:
classDiagram
Sentence <|-- Term
Sentence <|-- BooleanSentence
Sentence <|-- QuantifiedSentence
Sentence <|-- Extension
Source code in src/typedlogic/datamodel.py
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 197 198 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 |
|
annotations: Dict[str, Any]
property
Annotations for the sentence.
Annotations are always logically silent, but can be used to store metadata or other information.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
|
add_annotation(key, value)
Add an annotation to the sentence
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
|
required |
value
|
Any
|
|
required |
Returns:
Type | Description |
---|---|
|
Source code in src/typedlogic/datamodel.py
232 233 234 235 236 237 238 239 240 241 242 |
|
Term
Bases: Sentence
An atomic part of a sentence.
A ground term is a term with no variables:
>>> t = Term('FriendOf', 'Alice', 'Bob')
>>> t
FriendOf(Alice, Bob)
>>> t.values
('Alice', 'Bob')
>>> t.is_ground
True
Keyword argument based initialization is also supported:
>>> t = Term('FriendOf', dict(about='Alice', friend='Bob'))
>>> t.values
('Alice', 'Bob')
>>> t.positional
False
Mappings:
- Corresponds to AtomicSentence in Common Logic
Source code in src/typedlogic/datamodel.py
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 303 304 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 |
|
is_constant
property
Returns:
Type | Description |
---|---|
True if the term is a constant (zero arguments) |
is_ground
property
Returns:
Type | Description |
---|---|
True if none of the arguments are variables |
values: Tuple[Any, ...]
property
Representation of the arguments of the term as a fixed-position tuples
Returns:
Type | Description |
---|---|
Tuple[Any, ...]
|
|
variables: List[Variable]
property
Returns:
Type | Description |
---|---|
List[Variable]
|
All of the arguments that are variables |
make_keyword_indexed(keywords)
Convert positional arguments to keyword arguments
Source code in src/typedlogic/datamodel.py
348 349 350 351 352 353 354 |
|
Extension
Bases: Sentence
, ABC
Use this class for framework-specific extensions.
An example of this is the Fact
class which subclasses Extension, and is intended to be
subclasses by domain-specific classes representing predicate definitions, whose instances
map to terms.
Source code in src/typedlogic/datamodel.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
to_model_object()
abstractmethod
Convert the extension to a standard model object.
Returns:
Type | Description |
---|---|
Sentence
|
|
Source code in src/typedlogic/datamodel.py
384 385 386 387 388 389 390 391 |
|
BooleanSentence
dataclass
Bases: Sentence
, ABC
Base class for sentences that are boolean expressions
Corresponds to BooleanSentence in CL
classDiagram
BooleanSentence <|-- And
BooleanSentence <|-- Or
BooleanSentence <|-- Not
BooleanSentence <|-- Xor
BooleanSentence <|-- ExactlyOne
BooleanSentence <|-- Implication
BooleanSentence <|-- Implied
BooleanSentence <|-- Iff
BooleanSentence <|-- NegationAsFailure
BooleanSentence --> "*" Sentence : operands
Source code in src/typedlogic/datamodel.py
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 |
|
And
dataclass
Bases: BooleanSentence
A conjunction of sentences.
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = And(Term('friend_of', x, y), Term('friend_of', y, x))
You can also use syntactic sugar:
>>> s = Term('friend_of', x, y) & Term('friend_of', y, x)
Note however that precedence rules for &
are different from and
.
In the context of a pylog program, you can also use and
:
assert FriendOf(x, y) & FriendOf(y, x)
As in CL, And()
means True
Source code in src/typedlogic/datamodel.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 |
|
Or
dataclass
Bases: BooleanSentence
A disjunction of sentences.
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = Or(Term('friend_of', x, y), Term('friend_of', y, x))
>>> s.operands[0]
friend_of(?x, ?y)
You can also use syntactic sugar:
>>> s = Term('friend_of', x, y) | Term('friend_of', y, x)
Note however that precedence rules for |
are different from or
.
In the context of a pylog program, you can also use or
:
assert FriendOf(x, y) | FriendOf(y, x)
As in CL, Or()
means False
Source code in src/typedlogic/datamodel.py
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 |
|
Not
dataclass
Bases: BooleanSentence
A complement of a sentence
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = Not(Term('friend_of', x, y))
>>> s.negated
friend_of(?x, ?y)
You can also use syntactic sugar:
>>> s = ~Term('friend_of', x, y)
In the context of a pylog program, you can also use not
:
assert not FriendOf(x, y)
This SHOULD be interpreted as strict negation, not as failure.
Source code in src/typedlogic/datamodel.py
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 |
|
negated: Sentence
property
Xor
Bases: BooleanSentence
An exclusive or of sentences
Source code in src/typedlogic/datamodel.py
567 568 569 570 571 572 573 |
|
ExactlyOne
dataclass
Bases: BooleanSentence
Exactly one of the sentences is true
>>> x = Variable('x')
>>> s = ExactlyOne(Term('likes', x, "root beer"), Term('likes', x, "marmite"))
Source code in src/typedlogic/datamodel.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
|
Implication
dataclass
Bases: BooleanSentence
, ABC
An abstract grouping of sentences with an implication operator.
classDiagram
Implication <|-- Implies
Implication <|-- Implied
Implication <|-- Iff
Source code in src/typedlogic/datamodel.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
Implies
dataclass
Bases: Implication
An if-then implication.
Corresponds to Implication in CommonLogic
>>> x = Variable('x')
>>> s = Iff(Term('likes', x, "root beer"), ~Term('likes', x, "marmite"))
You can also use syntactic sugar:
>>> s = Term('likes', x, "root beer") >> ~Term('likes', x, "marmite")
Source code in src/typedlogic/datamodel.py
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
|
Implied
dataclass
Bases: Implication
An implication of the form consequent <- antecedent
Inverse of Implies
Source code in src/typedlogic/datamodel.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
|
Iff
dataclass
Bases: Implication
An equivalence of sentences
Corresponds to Biconditional in CommonLogic.
>>> x = Variable('x')
>>> s = Iff(Term('likes', x, "jaffa cakes"), Term('likes', x, "marmite"))
Source code in src/typedlogic/datamodel.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 |
|
NegationAsFailure
dataclass
Bases: BooleanSentence
A negated sentence, interpreted via negation as failure semantics.
Source code in src/typedlogic/datamodel.py
726 727 728 729 730 731 732 733 734 735 736 737 |
|
QuantifiedSentence
dataclass
Bases: Sentence
, ABC
A sentence with a logical quantifier.
classDiagram
QuantifiedSentence <|-- Forall
QuantifiedSentence <|-- Exists
QuantifiedSentence --> "*" Variable : variables
Source code in src/typedlogic/datamodel.py
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 |
|
Forall
dataclass
Bases: QuantifiedSentence
Universal quantifier.
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = Forall([x, y], Implies(Term('friend_of', x, y), Term('friend_of', y, x)))
Source code in src/typedlogic/datamodel.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
|
Exists
dataclass
Bases: QuantifiedSentence
Existential quantifier.
>>> x = Variable('x')
>>> y = Variable('y')
>>> s = ~Exists([x, y], And(Term('friend_of', x, y), Term('enemy_of', x, y)))
Source code in src/typedlogic/datamodel.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
|
SentenceGroup
dataclass
A logical grouping of related sentences with common documentation.
One way to collect these is via a decorated python function.
```mermaid classDiagram class SentenceGroup { +String name +SentenceGroupType group_type +String docstring +Dict annotations } SentenceGroup --> "*" Sentence : sentences
Source code in src/typedlogic/datamodel.py
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
|
Theory
dataclass
A collection of predicate definitions and sentences.
Analogous to a Text in CommonLogic.
classDiagram
class Theory {
+String name
+Dict constants
+Dict type_definitions
+List predicate_definitions
+List sentence_groups
+List ground_terms
+Dict annotations
}
Theory --> "*" DefinedType : type_definitions
Theory --> "*" PredicateDefinition : predicate_definitions
Theory --> "*" Term : ground_terms
Theory --> "*" SentenceGroup : sentence_groups
Source code in src/typedlogic/datamodel.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 |
|
sentences: List[Sentence]
property
goals: List[Sentence]
property
add(sentence)
Add a sentence to the theory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sentence
|
Sentence
|
|
required |
Returns:
Type | Description |
---|---|
|
Source code in src/typedlogic/datamodel.py
929 930 931 932 933 934 935 936 937 938 939 940 |
|
remove(sentence, strict=False)
Remove a sentence to the theory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sentence
|
Sentence
|
|
required |
strict
|
|
False
|
Returns:
Type | Description |
---|---|
|
Source code in src/typedlogic/datamodel.py
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 |
|
unroll_type(typ)
Unroll a defined type into its components
Parameters:
Name | Type | Description | Default |
---|---|---|---|
typ
|
DefinedType
|
|
required |
Returns:
Type | Description |
---|---|
List[str]
|
|
Source code in src/typedlogic/datamodel.py
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 |
|
NotInProfileError
Bases: ValueError
Raised when a sentence is not in some profile
Source code in src/typedlogic/datamodel.py
1023 1024 1025 1026 1027 1028 |
|
term(predicate, *args, **kwargs)
Create a term object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicate
|
Union[str, Type[Extension], Extension]
|
|
required |
args
|
|
()
|
|
kwargs
|
|
{}
|
Returns:
Type | Description |
---|---|
Term
|
Term object |
Source code in src/typedlogic/datamodel.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
|
not_provable(predicate)
Function for Negation as Failure
Source code in src/typedlogic/datamodel.py
741 742 743 |
|