pythen 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
docs/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ """
2
+ PYTHEN Documentation
3
+
4
+ This directory contains comprehensive documentation for the PYTHEN framework:
5
+
6
+ - getting_started.md: A beginner's guide to using PYTHEN
7
+ - api_reference.md: Detailed API documentation
8
+ - rule_syntax.md: Complete guide to the rule syntax
9
+ - philosophy.md: The design philosophy behind PYTHEN
10
+ """
examples/__init__.py ADDED
@@ -0,0 +1,19 @@
1
+ """
2
+ PYTHEN Examples
3
+
4
+ This package contains end-to-end examples demonstrating PYTHEN's application
5
+ to various legal domains:
6
+
7
+ - gdpr_article17: GDPR Article 17 - Right to Erasure
8
+ - contract_law: Contract Law - Voidable Contracts
9
+
10
+ Each example includes realistic scenarios and demonstrates how to:
11
+ 1. Define legal rules with conditions and exceptions
12
+ 2. Evaluate rules against facts
13
+ 3. Trace and explain evaluation results
14
+ """
15
+
16
+ from . import gdpr_article17
17
+ from . import contract_law
18
+
19
+ __all__ = ["gdpr_article17", "contract_law"]
@@ -0,0 +1,262 @@
1
+ """
2
+ Contract Law Example - Voidable Contracts
3
+
4
+ This example demonstrates PYTHEN's application to contract law,
5
+ specifically determining when a contract is voidable.
6
+
7
+ This shows how PYTHEN can model complex legal doctrines with
8
+ multiple conditions and exceptions.
9
+ """
10
+
11
+ from pythen import RuleTreeEvaluator, print_rules
12
+
13
+
14
+ # Define the rule set for voidable contracts
15
+ VOIDABLE_CONTRACT_RULES = [
16
+ {
17
+ "p": "contract_voidable",
18
+ "op": "ANY",
19
+ "conditions": [
20
+ "party_lacks_capacity",
21
+ "contract_induced_by_fraud",
22
+ "contract_induced_by_duress",
23
+ "contract_induced_by_undue_influence",
24
+ "contract_induced_by_misrepresentation"
25
+ ],
26
+ "exceptions": [
27
+ "contract_for_necessities",
28
+ "contract_ratified",
29
+ "contract_performed",
30
+ "party_waived_right"
31
+ ]
32
+ },
33
+ {
34
+ "p": "party_lacks_capacity",
35
+ "op": "ANY",
36
+ "conditions": [
37
+ "party_is_minor",
38
+ "party_is_mentally_incapacitated",
39
+ "party_is_intoxicated"
40
+ ],
41
+ "exceptions": []
42
+ },
43
+ {
44
+ "p": "party_is_minor",
45
+ "op": "ALL",
46
+ "conditions": ["party_age_below_18"],
47
+ "exceptions": []
48
+ },
49
+ {
50
+ "p": "party_is_mentally_incapacitated",
51
+ "op": "ALL",
52
+ "conditions": ["party_lacks_mental_capacity"],
53
+ "exceptions": []
54
+ },
55
+ {
56
+ "p": "contract_induced_by_fraud",
57
+ "op": "ALL",
58
+ "conditions": [
59
+ "false_statement_made",
60
+ "statement_material",
61
+ "party_relied_on_statement",
62
+ "party_suffered_damage"
63
+ ],
64
+ "exceptions": []
65
+ },
66
+ {
67
+ "p": "contract_induced_by_duress",
68
+ "op": "ALL",
69
+ "conditions": [
70
+ "threat_made",
71
+ "threat_unlawful",
72
+ "threat_induced_contract"
73
+ ],
74
+ "exceptions": []
75
+ },
76
+ {
77
+ "p": "contract_induced_by_undue_influence",
78
+ "op": "ALL",
79
+ "conditions": [
80
+ "relationship_of_trust",
81
+ "excessive_pressure_applied",
82
+ "party_did_not_act_freely"
83
+ ],
84
+ "exceptions": []
85
+ },
86
+ {
87
+ "p": "contract_induced_by_misrepresentation",
88
+ "op": "ALL",
89
+ "conditions": [
90
+ "false_representation_made",
91
+ "representation_material",
92
+ "party_relied_on_representation"
93
+ ],
94
+ "exceptions": []
95
+ },
96
+ {
97
+ "p": "contract_for_necessities",
98
+ "op": "ALL",
99
+ "conditions": ["goods_are_necessities"],
100
+ "exceptions": []
101
+ }
102
+ ]
103
+
104
+
105
+ def example_1_minor_contract():
106
+ """
107
+ Example 1: Contract with a minor
108
+
109
+ Scenario: A 16-year-old purchases a video game console.
110
+ The contract is voidable because the party is a minor.
111
+ However, if the goods are necessities, the contract may be enforceable.
112
+ """
113
+ print("\n" + "="*70)
114
+ print("EXAMPLE 1: Contract with a Minor")
115
+ print("="*70)
116
+
117
+ print("\nScenario A: Non-necessity goods (video game console)")
118
+ facts_a = ["party_age_below_18"]
119
+
120
+ evaluator = RuleTreeEvaluator(VOIDABLE_CONTRACT_RULES, enable_trace=True)
121
+ result_a = evaluator.evaluate(facts_a, "contract_voidable")
122
+
123
+ print(f"Facts: {facts_a}")
124
+ print(f"Is contract voidable? {result_a}")
125
+
126
+ print("\n" + "-"*70)
127
+ print("\nScenario B: Necessity goods (food, clothing)")
128
+ facts_b = ["party_age_below_18", "goods_are_necessities"]
129
+
130
+ result_b = evaluator.evaluate(facts_b, "contract_voidable")
131
+
132
+ print(f"Facts: {facts_b}")
133
+ print(f"Is contract voidable? {result_b}")
134
+ print("\nExplanation:")
135
+ print(evaluator.explain(facts_b, "contract_voidable"))
136
+
137
+
138
+ def example_2_fraud():
139
+ """
140
+ Example 2: Contract induced by fraud
141
+
142
+ Scenario: A seller misrepresents the condition of a car.
143
+ The buyer discovers the fraud and seeks to void the contract.
144
+ """
145
+ print("\n" + "="*70)
146
+ print("EXAMPLE 2: Contract Induced by Fraud")
147
+ print("="*70)
148
+
149
+ facts = [
150
+ "false_statement_made",
151
+ "statement_material",
152
+ "party_relied_on_statement",
153
+ "party_suffered_damage"
154
+ ]
155
+
156
+ evaluator = RuleTreeEvaluator(VOIDABLE_CONTRACT_RULES, enable_trace=True)
157
+ result = evaluator.evaluate(facts, "contract_voidable")
158
+
159
+ print(f"\nFacts: {facts}")
160
+ print(f"Is contract voidable? {result}")
161
+ print("\nExplanation:")
162
+ print(evaluator.explain(facts, "contract_voidable"))
163
+
164
+
165
+ def example_3_duress():
166
+ """
167
+ Example 3: Contract induced by duress
168
+
169
+ Scenario: A person is threatened with violence unless they sign a contract.
170
+ The contract is voidable due to duress.
171
+ """
172
+ print("\n" + "="*70)
173
+ print("EXAMPLE 3: Contract Induced by Duress")
174
+ print("="*70)
175
+
176
+ facts = [
177
+ "threat_made",
178
+ "threat_unlawful",
179
+ "threat_induced_contract"
180
+ ]
181
+
182
+ evaluator = RuleTreeEvaluator(VOIDABLE_CONTRACT_RULES, enable_trace=True)
183
+ result = evaluator.evaluate(facts, "contract_voidable")
184
+
185
+ print(f"\nFacts: {facts}")
186
+ print(f"Is contract voidable? {result}")
187
+ print("\nExplanation:")
188
+ print(evaluator.explain(facts, "contract_voidable"))
189
+
190
+
191
+ def example_4_ratification():
192
+ """
193
+ Example 4: Contract with ratification
194
+
195
+ Scenario: A minor enters into a contract, but later ratifies it
196
+ (affirms it after reaching majority). The contract is no longer voidable.
197
+ """
198
+ print("\n" + "="*70)
199
+ print("EXAMPLE 4: Contract Ratification")
200
+ print("="*70)
201
+
202
+ facts = [
203
+ "party_age_below_18",
204
+ "contract_ratified" # Exception: contract has been ratified
205
+ ]
206
+
207
+ evaluator = RuleTreeEvaluator(VOIDABLE_CONTRACT_RULES, enable_trace=True)
208
+ result = evaluator.evaluate(facts, "contract_voidable")
209
+
210
+ print(f"\nFacts: {facts}")
211
+ print(f"Is contract voidable? {result}")
212
+ print("\nExplanation:")
213
+ print(evaluator.explain(facts, "contract_voidable"))
214
+
215
+
216
+ def example_5_mental_incapacity():
217
+ """
218
+ Example 5: Contract with mentally incapacitated party
219
+
220
+ Scenario: A person with dementia signs a contract.
221
+ The contract is voidable due to lack of mental capacity.
222
+ """
223
+ print("\n" + "="*70)
224
+ print("EXAMPLE 5: Mental Incapacity")
225
+ print("="*70)
226
+
227
+ facts = [
228
+ "party_lacks_mental_capacity"
229
+ ]
230
+
231
+ evaluator = RuleTreeEvaluator(VOIDABLE_CONTRACT_RULES, enable_trace=True)
232
+ result = evaluator.evaluate(facts, "contract_voidable")
233
+
234
+ print(f"\nFacts: {facts}")
235
+ print(f"Is contract voidable? {result}")
236
+ print("\nExplanation:")
237
+ print(evaluator.explain(facts, "contract_voidable"))
238
+
239
+
240
+ def main():
241
+ """Run all examples."""
242
+ print("\n" + "="*70)
243
+ print("PYTHEN: Contract Law - Voidable Contracts Examples")
244
+ print("="*70)
245
+
246
+ # Print the rule set
247
+ print_rules(VOIDABLE_CONTRACT_RULES)
248
+
249
+ # Run examples
250
+ example_1_minor_contract()
251
+ example_2_fraud()
252
+ example_3_duress()
253
+ example_4_ratification()
254
+ example_5_mental_incapacity()
255
+
256
+ print("\n" + "="*70)
257
+ print("Examples completed!")
258
+ print("="*70 + "\n")
259
+
260
+
261
+ if __name__ == "__main__":
262
+ main()
@@ -0,0 +1,217 @@
1
+ """
2
+ GDPR Article 17 - Right to Erasure Example
3
+
4
+ This example demonstrates PYTHEN's application to GDPR Article 17 (Right to be Forgotten).
5
+ It shows how to model complex legal rules with conditions and exceptions.
6
+
7
+ Reference: https://gdpr-info.eu/art-17-gdpr/
8
+ """
9
+
10
+ from pythen import RuleTreeEvaluator, print_rules
11
+
12
+
13
+ # Define the rule set for GDPR Article 17
14
+ GDPR_ARTICLE_17_RULES = [
15
+ {
16
+ "p": "art17_erasure_applicable",
17
+ "op": "ANY",
18
+ "conditions": [
19
+ "no_longer_necessary",
20
+ "consent_withdrawn",
21
+ "object_to_processing",
22
+ "processing_unlawful",
23
+ "child_data_collected"
24
+ ],
25
+ "exceptions": [
26
+ "freedom_of_expression",
27
+ "legal_obligation",
28
+ "public_interest_archiving_research",
29
+ "legal_claims"
30
+ ]
31
+ },
32
+ {
33
+ "p": "no_longer_necessary",
34
+ "op": "ALL",
35
+ "conditions": ["data_not_needed_for_purpose"],
36
+ "exceptions": []
37
+ },
38
+ {
39
+ "p": "consent_withdrawn",
40
+ "op": "ALL",
41
+ "conditions": ["consent_was_basis", "consent_is_withdrawn"],
42
+ "exceptions": []
43
+ },
44
+ {
45
+ "p": "object_to_processing",
46
+ "op": "ANY",
47
+ "conditions": ["objection_no_overriding_grounds", "objection_to_direct_marketing"],
48
+ "exceptions": []
49
+ },
50
+ {
51
+ "p": "processing_unlawful",
52
+ "op": "ALL",
53
+ "conditions": ["processing_is_unlawful"],
54
+ "exceptions": []
55
+ },
56
+ {
57
+ "p": "child_data_collected",
58
+ "op": "ALL",
59
+ "conditions": ["data_collected_from_child"],
60
+ "exceptions": []
61
+ },
62
+ {
63
+ "p": "objection_no_overriding_grounds",
64
+ "op": "ALL",
65
+ "conditions": ["objection_made", "no_overriding_grounds"],
66
+ "exceptions": []
67
+ }
68
+ ]
69
+
70
+
71
+ def example_1_consent_withdrawal():
72
+ """
73
+ Example 1: User withdraws consent for direct marketing
74
+
75
+ Scenario: A user previously gave consent for marketing emails.
76
+ They now withdraw that consent and request erasure.
77
+ """
78
+ print("\n" + "="*70)
79
+ print("EXAMPLE 1: Consent Withdrawal for Direct Marketing")
80
+ print("="*70)
81
+
82
+ facts = [
83
+ "consent_was_basis",
84
+ "consent_is_withdrawn",
85
+ "data_not_needed_for_purpose"
86
+ ]
87
+
88
+ evaluator = RuleTreeEvaluator(GDPR_ARTICLE_17_RULES, enable_trace=True)
89
+ result = evaluator.evaluate(facts, "art17_erasure_applicable")
90
+
91
+ print(f"\nFacts: {facts}")
92
+ print(f"\nShould data be erased? {result}")
93
+ print("\nExplanation:")
94
+ print(evaluator.explain(facts, "art17_erasure_applicable"))
95
+
96
+
97
+ def example_2_with_exception():
98
+ """
99
+ Example 2: Legal obligation exception
100
+
101
+ Scenario: User requests erasure, but data must be retained for legal claims.
102
+ """
103
+ print("\n" + "="*70)
104
+ print("EXAMPLE 2: Legal Obligation Exception")
105
+ print("="*70)
106
+
107
+ facts = [
108
+ "consent_was_basis",
109
+ "consent_is_withdrawn",
110
+ "legal_claims" # Exception: legal claims pending
111
+ ]
112
+
113
+ evaluator = RuleTreeEvaluator(GDPR_ARTICLE_17_RULES, enable_trace=True)
114
+ result = evaluator.evaluate(facts, "art17_erasure_applicable")
115
+
116
+ print(f"\nFacts: {facts}")
117
+ print(f"\nShould data be erased? {result}")
118
+ print("\nExplanation:")
119
+ print(evaluator.explain(facts, "art17_erasure_applicable"))
120
+
121
+
122
+ def example_3_child_data():
123
+ """
124
+ Example 3: Child data erasure
125
+
126
+ Scenario: Data of a child was collected without proper consent.
127
+ The child (or parent) requests erasure.
128
+ """
129
+ print("\n" + "="*70)
130
+ print("EXAMPLE 3: Child Data Erasure")
131
+ print("="*70)
132
+
133
+ facts = [
134
+ "data_collected_from_child"
135
+ ]
136
+
137
+ evaluator = RuleTreeEvaluator(GDPR_ARTICLE_17_RULES, enable_trace=True)
138
+ result = evaluator.evaluate(facts, "art17_erasure_applicable")
139
+
140
+ print(f"\nFacts: {facts}")
141
+ print(f"\nShould data be erased? {result}")
142
+ print("\nExplanation:")
143
+ print(evaluator.explain(facts, "art17_erasure_applicable"))
144
+
145
+
146
+ def example_4_freedom_of_expression():
147
+ """
148
+ Example 4: Freedom of expression exception
149
+
150
+ Scenario: User requests erasure, but data is needed for freedom of expression.
151
+ """
152
+ print("\n" + "="*70)
153
+ print("EXAMPLE 4: Freedom of Expression Exception")
154
+ print("="*70)
155
+
156
+ facts = [
157
+ "processing_is_unlawful",
158
+ "freedom_of_expression" # Exception: freedom of expression
159
+ ]
160
+
161
+ evaluator = RuleTreeEvaluator(GDPR_ARTICLE_17_RULES, enable_trace=True)
162
+ result = evaluator.evaluate(facts, "art17_erasure_applicable")
163
+
164
+ print(f"\nFacts: {facts}")
165
+ print(f"\nShould data be erased? {result}")
166
+ print("\nExplanation:")
167
+ print(evaluator.explain(facts, "art17_erasure_applicable"))
168
+
169
+
170
+ def example_5_complex_scenario():
171
+ """
172
+ Example 5: Complex scenario with multiple conditions and exceptions
173
+
174
+ Scenario: User objects to processing, but there are overriding grounds.
175
+ """
176
+ print("\n" + "="*70)
177
+ print("EXAMPLE 5: Complex Scenario - Objection with Overriding Grounds")
178
+ print("="*70)
179
+
180
+ facts = [
181
+ "objection_made",
182
+ "no_overriding_grounds", # Actually, there ARE overriding grounds
183
+ "legal_obligation" # Exception: legal obligation
184
+ ]
185
+
186
+ evaluator = RuleTreeEvaluator(GDPR_ARTICLE_17_RULES, enable_trace=True)
187
+ result = evaluator.evaluate(facts, "art17_erasure_applicable")
188
+
189
+ print(f"\nFacts: {facts}")
190
+ print(f"\nShould data be erased? {result}")
191
+ print("\nExplanation:")
192
+ print(evaluator.explain(facts, "art17_erasure_applicable"))
193
+
194
+
195
+ def main():
196
+ """Run all examples."""
197
+ print("\n" + "="*70)
198
+ print("PYTHEN: GDPR Article 17 - Right to Erasure Examples")
199
+ print("="*70)
200
+
201
+ # Print the rule set
202
+ print_rules(GDPR_ARTICLE_17_RULES)
203
+
204
+ # Run examples
205
+ example_1_consent_withdrawal()
206
+ example_2_with_exception()
207
+ example_3_child_data()
208
+ example_4_freedom_of_expression()
209
+ example_5_complex_scenario()
210
+
211
+ print("\n" + "="*70)
212
+ print("Examples completed!")
213
+ print("="*70 + "\n")
214
+
215
+
216
+ if __name__ == "__main__":
217
+ main()
pythen/__init__.py ADDED
@@ -0,0 +1,43 @@
1
+ """
2
+ PYTHEN: A Flexible Framework for Legal Reasoning in Python
3
+
4
+ PYTHEN is a Python-based framework for defeasible legal reasoning that combines
5
+ the power of logic programming with the accessibility and flexibility of Python.
6
+
7
+ Key Features:
8
+ - Simple JSON-based rule syntax
9
+ - Support for both conjunctive (ALL) and disjunctive (ANY) conditions
10
+ - Explicit exception handling for defeasible reasoning
11
+ - Easy integration with LLMs for autoformalization
12
+ - Comprehensive documentation and examples
13
+
14
+ Example:
15
+ >>> from pythen import RuleTreeEvaluator
16
+ >>> rules = [
17
+ ... {
18
+ ... "p": "erasure_applicable",
19
+ ... "op": "ANY",
20
+ ... "conditions": ["no_longer_necessary", "consent_withdrawn"],
21
+ ... "exceptions": ["legal_obligation"]
22
+ ... }
23
+ ... ]
24
+ >>> evaluator = RuleTreeEvaluator(rules)
25
+ >>> facts = ["consent_withdrawn"]
26
+ >>> result = evaluator.evaluate(facts, "erasure_applicable")
27
+ >>> print(result) # True
28
+ """
29
+
30
+ __version__ = "0.1.0"
31
+ __author__ = "PYTHEN Contributors"
32
+ __license__ = "MIT"
33
+
34
+ from .evaluator import RuleTreeEvaluator
35
+ from .utils import validate_rule, validate_rules, log_message, RuleValidationError
36
+
37
+ __all__ = [
38
+ "RuleTreeEvaluator",
39
+ "validate_rule",
40
+ "validate_rules",
41
+ "log_message",
42
+ "RuleValidationError",
43
+ ]