1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! IR-level term type.
/// An IR-level term, possibly annotated with reasoning metadata.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[allow(missing_docs)]
pub enum IrTerm {
// --- Primitive terms ---
Var(String),
Atom(String),
Number(i64),
/// Extension: the AST has `Term::Float(f64)` which would lose information as `Number(i64)`.
Float(f64),
/// A dense tensor / embedding value (e.g., from `#[1.0, 0.5, -0.3]`).
Tensor(Vec<f32>),
Structure {
name: String,
args: Vec<IrTerm>,
},
/// Cut (`!`): commits to the current clause, removing all choice points back
/// to the enclosing predicate's cut barrier.
Cut,
// --- Annotated terms ---
/// Type-constrained term
Typed {
term: Box<IrTerm>,
ty: String,
},
/// Probabilistic fact annotation
Probabilistic {
term: Box<IrTerm>,
prob: f64,
},
/// Temporal scoping annotation
Temporal {
term: Box<IrTerm>,
start: i128,
end: i128,
},
/// Modal reasoning annotation
///
/// Carries a structured `ModalAnnotation` containing operator and optional agent.
Modal {
term: Box<IrTerm>,
annotation: crate::ModalAnnotation,
},
/// Neural predicate reference
Neural {
term: Box<IrTerm>,
model_id: String,
},
/// Differentiable logic annotation
Differentiable {
term: Box<IrTerm>,
grad_id: String,
},
/// Neural predicate with gradient tracking (bidirectional).
/// Combines neural model execution with differentiable gradient flow.
DiffNeural {
term: Box<IrTerm>,
model_id: String,
grad_id: String,
},
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ModalAnnotation;
use std::f64::consts::PI;
#[test]
fn test_primitive_variants() {
assert_eq!(IrTerm::Var("X".into()), IrTerm::Var("X".into()));
assert_eq!(IrTerm::Atom("foo".into()), IrTerm::Atom("foo".into()));
assert_eq!(IrTerm::Number(42), IrTerm::Number(42));
assert_eq!(IrTerm::Float(PI), IrTerm::Float(PI));
assert_eq!(
IrTerm::Structure {
name: "f".into(),
args: vec![IrTerm::Atom("a".into())]
},
IrTerm::Structure {
name: "f".into(),
args: vec![IrTerm::Atom("a".into())]
},
);
}
#[test]
fn test_annotated_variants() {
let base = Box::new(IrTerm::Atom("rain".into()));
let typed = IrTerm::Typed {
term: base.clone(),
ty: "bool".into(),
};
let prob = IrTerm::Probabilistic {
term: base.clone(),
prob: 0.8,
};
let temporal = IrTerm::Temporal {
term: base.clone(),
start: 0,
end: 100,
};
let modal = IrTerm::Modal {
term: base.clone(),
annotation: ModalAnnotation::necessity(),
};
let neural = IrTerm::Neural {
term: base.clone(),
model_id: "m1".into(),
};
let diff = IrTerm::Differentiable {
term: base.clone(),
grad_id: "g1".into(),
};
assert_eq!(
typed,
IrTerm::Typed {
term: base.clone(),
ty: "bool".into()
}
);
assert_eq!(
prob,
IrTerm::Probabilistic {
term: base.clone(),
prob: 0.8
}
);
assert_eq!(
temporal,
IrTerm::Temporal {
term: base.clone(),
start: 0,
end: 100
}
);
assert_eq!(
modal,
IrTerm::Modal {
term: base.clone(),
annotation: ModalAnnotation::necessity(),
}
);
assert_eq!(
neural,
IrTerm::Neural {
term: base.clone(),
model_id: "m1".into()
}
);
assert_eq!(
diff,
IrTerm::Differentiable {
term: base.clone(),
grad_id: "g1".into()
}
);
}
#[test]
fn test_primitive_ron_roundtrip() {
let terms = vec![
IrTerm::Var("X".into()),
IrTerm::Atom("foo".into()),
IrTerm::Number(42),
IrTerm::Float(1.5),
IrTerm::Structure {
name: "f".into(),
args: vec![IrTerm::Number(1)],
},
];
for term in &terms {
let s = ron::to_string(term).unwrap();
let back: IrTerm = ron::from_str(&s).unwrap();
assert_eq!(term, &back);
}
}
#[test]
#[ignore = "RON does not support i128 serialization"]
fn test_annotated_ron_roundtrip() {
let base = Box::new(IrTerm::Atom("rain".into()));
let terms = vec![
IrTerm::Typed {
term: base.clone(),
ty: "bool".into(),
},
IrTerm::Probabilistic {
term: base.clone(),
prob: 0.9,
},
IrTerm::Temporal {
term: base.clone(),
start: 10,
end: 20,
},
IrTerm::Modal {
term: base.clone(),
annotation: ModalAnnotation::necessity(),
},
IrTerm::Neural {
term: base.clone(),
model_id: "net1".into(),
},
IrTerm::Differentiable {
term: base.clone(),
grad_id: "grad1".into(),
},
];
for term in &terms {
let s = ron::to_string(term).unwrap();
let back: IrTerm = ron::from_str(&s).unwrap();
assert_eq!(term, &back);
}
}
}