1use crate::agent::A_NATURAL_PERSON;
21use crate::NQuin;
22
23pub const JURAL_CLAIM: u8 = 0x30;
26pub const JURAL_DUTY: u8 = 0x31;
27pub const JURAL_PRIVILEGE: u8 = 0x32;
28pub const JURAL_NO_RIGHT: u8 = 0x33;
29pub const JURAL_POWER: u8 = 0x34;
30pub const JURAL_LIABILITY: u8 = 0x35;
31pub const JURAL_IMMUNITY: u8 = 0x36;
32pub const JURAL_DISABILITY: u8 = 0x37;
33
34const CONTENT_MASK: u64 = 0x7FFF_FFFF_FFFF_FF00;
36
37pub const fn correlative(pos: u8) -> u8 {
40 match pos {
41 JURAL_CLAIM => JURAL_DUTY,
42 JURAL_DUTY => JURAL_CLAIM,
43 JURAL_PRIVILEGE => JURAL_NO_RIGHT,
44 JURAL_NO_RIGHT => JURAL_PRIVILEGE,
45 JURAL_POWER => JURAL_LIABILITY,
46 JURAL_LIABILITY => JURAL_POWER,
47 JURAL_IMMUNITY => JURAL_DISABILITY,
48 JURAL_DISABILITY => JURAL_IMMUNITY,
49 other => other,
50 }
51}
52
53pub const fn jural_opposite(pos: u8) -> u8 {
55 match pos {
56 JURAL_CLAIM => JURAL_NO_RIGHT,
57 JURAL_NO_RIGHT => JURAL_CLAIM,
58 JURAL_DUTY => JURAL_PRIVILEGE,
59 JURAL_PRIVILEGE => JURAL_DUTY,
60 JURAL_POWER => JURAL_DISABILITY,
61 JURAL_DISABILITY => JURAL_POWER,
62 JURAL_IMMUNITY => JURAL_LIABILITY,
63 JURAL_LIABILITY => JURAL_IMMUNITY,
64 other => other,
65 }
66}
67
68#[inline]
70pub const fn is_jural_position(pos: u8) -> bool {
71 matches!(
72 pos,
73 JURAL_CLAIM
74 | JURAL_DUTY
75 | JURAL_PRIVILEGE
76 | JURAL_NO_RIGHT
77 | JURAL_POWER
78 | JURAL_LIABILITY
79 | JURAL_IMMUNITY
80 | JURAL_DISABILITY
81 )
82}
83
84#[inline]
86pub const fn is_first_order(pos: u8) -> bool {
87 matches!(
88 pos,
89 JURAL_CLAIM | JURAL_DUTY | JURAL_PRIVILEGE | JURAL_NO_RIGHT
90 )
91}
92
93pub const fn position_name(pos: u8) -> Option<&'static str> {
95 Some(match pos {
96 JURAL_CLAIM => "Claim",
97 JURAL_DUTY => "Duty",
98 JURAL_PRIVILEGE => "Privilege",
99 JURAL_NO_RIGHT => "No-Right",
100 JURAL_POWER => "Power",
101 JURAL_LIABILITY => "Liability",
102 JURAL_IMMUNITY => "Immunity",
103 JURAL_DISABILITY => "Disability",
104 _ => return None,
105 })
106}
107
108#[inline]
109pub const fn jural_position(predicate: u64) -> u8 {
111 (predicate & 0xFF) as u8
112}
113
114#[inline]
115pub const fn jural_content(predicate: u64) -> u64 {
117 predicate & CONTENT_MASK
118}
119
120pub fn compile_jural_quin(
123 holder: u64,
124 position: u8,
125 content_path: u64,
126 counterparty: u64,
127 frame: u64,
128) -> NQuin {
129 let predicate = ((content_path << 8) & CONTENT_MASK) | (position as u64);
130 let parity = holder ^ predicate ^ counterparty ^ frame;
131 NQuin {
132 subject: holder,
133 predicate,
134 object: counterparty,
135 context: frame,
136 metadata: 0,
137 parity,
138 }
139}
140
141pub fn correlative_quin(rel: &NQuin) -> NQuin {
144 let pos = jural_position(rel.predicate);
145 let predicate = jural_content(rel.predicate) | (correlative(pos) as u64);
146 let holder = rel.object; let counterparty = rel.subject;
148 let frame = rel.context;
149 let parity = holder ^ predicate ^ counterparty ^ frame;
150 NQuin {
151 subject: holder,
152 predicate,
153 object: counterparty,
154 context: frame,
155 metadata: 0,
156 parity,
157 }
158}
159
160pub fn jural_correlativity_holds(rel: &NQuin, graph: &[NQuin]) -> bool {
162 let expected = correlative_quin(rel);
163 graph.iter().any(|q| {
164 q.subject == expected.subject
165 && q.object == expected.object
166 && q.predicate == expected.predicate
167 && q.context == expected.context
168 })
169}
170
171pub fn find_unmet_correlatives(rels: &[NQuin], out: &mut [NQuin]) -> usize {
176 let mut n = 0usize;
177 for rel in rels {
178 if !is_jural_position(jural_position(rel.predicate)) {
179 continue;
180 }
181 if !jural_correlativity_holds(rel, rels) {
182 if n >= out.len() {
183 break;
184 }
185 out[n] = correlative_quin(rel);
186 n += 1;
187 }
188 }
189 n
190}
191
192pub fn personhood_category_error(
203 holder_class: u64,
204 content_is_np_exclusive: bool,
205 position: u8,
206 tbox: &[NQuin],
207) -> bool {
208 if !content_is_np_exclusive {
209 return false;
210 }
211 if !matches!(position, JURAL_CLAIM | JURAL_PRIVILEGE | JURAL_IMMUNITY) {
213 return false;
214 }
215 !crate::modalities::dl::check_subsumption_quin(holder_class, A_NATURAL_PERSON, tbox)
217}
218
219#[inline]
224pub const fn is_second_order(pos: u8) -> bool {
225 matches!(
226 pos,
227 JURAL_POWER | JURAL_LIABILITY | JURAL_IMMUNITY | JURAL_DISABILITY
228 )
229}
230
231pub fn jural_chain_links(upstream: &NQuin, downstream: &NQuin) -> bool {
237 is_second_order(jural_position(upstream.predicate))
238 && is_jural_position(jural_position(downstream.predicate))
239 && upstream.object == downstream.subject
240}
241
242pub fn jural_chain_pivot(upstream: &NQuin, downstream: &NQuin) -> Option<u64> {
244 if jural_chain_links(upstream, downstream) {
245 Some(upstream.object)
246 } else {
247 None
248 }
249}
250
251pub fn jural_chain_valid(rels: &[NQuin]) -> bool {
254 rels.windows(2).all(|w| jural_chain_links(&w[0], &w[1]))
255}
256
257#[derive(Debug, Clone, Copy, PartialEq, Eq)]
261pub enum CollisionResolution {
262 FirstPrevails,
264 SecondPrevails,
266 RequiresHumanReview,
269 NoCollision,
271}
272
273pub fn jural_collision(a: &NQuin, b: &NQuin) -> bool {
277 a.subject == b.subject
278 && a.context == b.context
279 && is_jural_position(jural_position(a.predicate))
280 && jural_content(a.predicate) == jural_content(b.predicate)
281 && jural_opposite(jural_position(a.predicate)) == jural_position(b.predicate)
282}
283
284pub fn resolve_collision(
290 a: &NQuin,
291 b: &NQuin,
292 a_nonderogable: bool,
293 b_nonderogable: bool,
294) -> CollisionResolution {
295 if !jural_collision(a, b) {
296 return CollisionResolution::NoCollision;
297 }
298 match (a_nonderogable, b_nonderogable) {
299 (true, false) => CollisionResolution::FirstPrevails,
300 (false, true) => CollisionResolution::SecondPrevails,
301 _ => CollisionResolution::RequiresHumanReview,
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 use super::*;
308 use crate::q_hash;
309
310 #[test]
311 fn correlatives_are_involutive_and_paired() {
312 for &p in &[
313 JURAL_CLAIM,
314 JURAL_DUTY,
315 JURAL_PRIVILEGE,
316 JURAL_NO_RIGHT,
317 JURAL_POWER,
318 JURAL_LIABILITY,
319 JURAL_IMMUNITY,
320 JURAL_DISABILITY,
321 ] {
322 assert_eq!(correlative(correlative(p)), p, "{:?}", position_name(p));
324 assert_eq!(jural_opposite(jural_opposite(p)), p);
325 assert_ne!(correlative(p), p);
327 assert_ne!(jural_opposite(p), p);
328 }
329 assert_eq!(correlative(JURAL_CLAIM), JURAL_DUTY);
330 assert_eq!(correlative(JURAL_POWER), JURAL_LIABILITY);
331 assert_eq!(correlative(JURAL_IMMUNITY), JURAL_DISABILITY);
332 assert_eq!(jural_opposite(JURAL_CLAIM), JURAL_NO_RIGHT);
333 }
334
335 #[test]
336 fn claim_implies_correlative_duty() {
337 let (alice, bob, frame) = (q_hash("alice"), q_hash("bob"), q_hash("nda"));
338 let content = q_hash("q42:repayLoan");
339 let claim = compile_jural_quin(alice, JURAL_CLAIM, content, bob, frame);
341 let duty = correlative_quin(&claim);
343 assert_eq!(jural_position(duty.predicate), JURAL_DUTY);
344 assert_eq!(duty.subject, bob);
345 assert_eq!(duty.object, alice);
346 assert_eq!(
347 jural_content(duty.predicate),
348 jural_content(claim.predicate)
349 );
350 assert_eq!(duty.context, frame);
351 assert_eq!(
353 duty.parity,
354 duty.subject ^ duty.predicate ^ duty.object ^ duty.context
355 );
356 }
357
358 #[test]
359 fn correlativity_holds_when_duty_is_present() {
360 let (alice, bob, frame) = (q_hash("alice"), q_hash("bob"), q_hash("nda"));
361 let content = q_hash("q42:repayLoan");
362 let claim = compile_jural_quin(alice, JURAL_CLAIM, content, bob, frame);
363 let duty = correlative_quin(&claim);
364 assert!(jural_correlativity_holds(&claim, &[claim, duty]));
366 assert!(!jural_correlativity_holds(&claim, &[claim]));
368 }
369
370 #[test]
371 fn unmet_correlative_duty_is_made_legible() {
372 let (alice, state, frame) = (q_hash("alice"), q_hash("state"), q_hash("icescr"));
373 let content = q_hash("q42:adequateHousing");
374 let claim = compile_jural_quin(alice, JURAL_CLAIM, content, state, frame);
376 let mut out = [NQuin::default(); 4];
377 let n = find_unmet_correlatives(&[claim], &mut out);
378 assert_eq!(n, 1, "the missing duty must be surfaced");
379 assert_eq!(jural_position(out[0].predicate), JURAL_DUTY);
380 assert_eq!(
381 out[0].subject, state,
382 "the State is the would-be duty-bearer"
383 );
384 assert_eq!(out[0].object, alice);
385
386 let duty = correlative_quin(&claim);
388 assert_eq!(find_unmet_correlatives(&[claim, duty], &mut out), 0);
389 }
390
391 #[test]
392 fn corporate_person_claiming_human_only_right_is_category_error() {
393 let np = A_NATURAL_PERSON;
395 let legal = q_hash("https://ns.webcivics.net/values/LegalPerson");
396 let corp = q_hash("https://ns.webcivics.net/values/CorporatePerson");
397 let agent = q_hash("https://ns.webcivics.net/values/Agent");
398 let sub = q_hash("http://www.w3.org/2000/01/rdf-schema#subClassOf");
399 let e = |s: u64, o: u64| NQuin {
400 subject: s,
401 predicate: sub,
402 object: o,
403 context: 0,
404 metadata: 0,
405 parity: 0,
406 };
407 let tbox = [e(corp, legal), e(legal, agent), e(np, agent)];
408
409 assert!(personhood_category_error(corp, true, JURAL_CLAIM, &tbox));
411 assert!(!personhood_category_error(np, true, JURAL_CLAIM, &tbox));
413 assert!(!personhood_category_error(corp, false, JURAL_CLAIM, &tbox));
415 assert!(!personhood_category_error(corp, true, JURAL_DUTY, &tbox));
417 }
418
419 #[test]
420 fn multi_party_chain_a_power_over_b_duty_to_c() {
421 let (a, b, c, frame) = (q_hash("A"), q_hash("B"), q_hash("C"), q_hash("frame"));
422 let content = q_hash("q42:performService");
423 let a_power = compile_jural_quin(a, JURAL_POWER, content, b, frame);
425 let b_duty = compile_jural_quin(b, JURAL_DUTY, content, c, frame);
426 assert!(
427 jural_chain_links(&a_power, &b_duty),
428 "A's power over B governs B's duty to C"
429 );
430 assert_eq!(
431 jural_chain_pivot(&a_power, &b_duty),
432 Some(b),
433 "the pivot is B"
434 );
435 assert!(jural_chain_valid(&[a_power, b_duty]));
436
437 let a_claim = compile_jural_quin(a, JURAL_CLAIM, content, b, frame);
439 assert!(!jural_chain_links(&a_claim, &b_duty));
440 let x_duty = compile_jural_quin(q_hash("X"), JURAL_DUTY, content, c, frame);
442 assert!(!jural_chain_links(&a_power, &x_duty));
443 assert_eq!(jural_chain_pivot(&a_power, &x_duty), None);
444 }
445
446 #[test]
447 fn colliding_rights_resolve_by_non_derogability_else_human_review() {
448 let (holder, cp, frame) = (q_hash("holder"), q_hash("counter"), q_hash("frame"));
449 let content = q_hash("q42:speak");
450 let duty = compile_jural_quin(holder, JURAL_DUTY, content, cp, frame);
452 let privilege = compile_jural_quin(holder, JURAL_PRIVILEGE, content, cp, frame);
453 assert!(
454 jural_collision(&duty, &privilege),
455 "Duty vs Privilege over same content = collision"
456 );
457
458 assert_eq!(
460 resolve_collision(&duty, &privilege, true, false),
461 CollisionResolution::FirstPrevails
462 );
463 assert_eq!(
464 resolve_collision(&duty, &privilege, false, true),
465 CollisionResolution::SecondPrevails
466 );
467 assert_eq!(
469 resolve_collision(&duty, &privilege, true, true),
470 CollisionResolution::RequiresHumanReview
471 );
472 assert_eq!(
473 resolve_collision(&duty, &privilege, false, false),
474 CollisionResolution::RequiresHumanReview
475 );
476
477 let duty2 = compile_jural_quin(holder, JURAL_DUTY, content, cp, frame);
479 assert!(!jural_collision(&duty, &duty2));
480 assert_eq!(
481 resolve_collision(&duty, &duty2, true, false),
482 CollisionResolution::NoCollision
483 );
484 }
485}