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
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
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
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
459
460
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
//! Polymorphically typed (Hindley-Milner) First-Order Term Rewriting Systems (no abstraction)
//!
//! Much thanks to:
//! - <https://github.com/rob-smallshire/hindley-milner-python>
//! - <https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system>
//! - (TAPL; Pierce, 2002, ch. 22)
use itertools::Itertools;
use polytype::Context as TypeContext;
use rand::seq::SliceRandom;
use rand::Rng;
use std::fmt;
use std::iter::once;
use term_rewriting::trace::Trace;
use term_rewriting::{Rule, RuleContext, Strategy as RewriteStrategy, Term, TRS as UntypedTRS};
use super::{Lexicon, ModelParams, SampleError, TypeError};
/// Manages the semantics of a term rewriting system.
#[derive(Debug, PartialEq, Clone)]
pub struct TRS {
pub(crate) lex: Lexicon,
// INVARIANT: UntypedTRS.rules ends with lex.background
pub(crate) utrs: UntypedTRS,
pub(crate) ctx: TypeContext,
}
impl TRS {
/// Create a new `TRS` under the given [`Lexicon`]. Any background knowledge
/// will be appended to the given ruleset.
///
/// # Example
///
/// ```
/// use polytype::{ptp, tp};
/// use programinduction::trs::{TRS, Lexicon};
/// use term_rewriting::{Signature, parse_rule};
/// use polytype::Context as TypeContext;
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some("PLUS".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("SUCC".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
/// sig.new_op(0, Some("ZERO".to_string()));
/// ops.push(ptp![int]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "PLUS(x_ ZERO) = x_").expect("parsed rule"),
/// parse_rule(&mut sig, "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_))").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// let lexicon = Lexicon::from_signature(sig, ops, vars, vec![], vec![], false, TypeContext::default());
///
/// let ctx = lexicon.context();
///
/// let trs = TRS::new(&lexicon, rules, &ctx).unwrap();
///
/// assert_eq!(trs.size(), 12);
/// ```
/// [`Lexicon`]: struct.Lexicon.html
pub fn new(
lexicon: &Lexicon,
mut rules: Vec<Rule>,
ctx: &TypeContext,
) -> Result<TRS, TypeError> {
let lexicon = lexicon.clone();
let mut ctx = ctx.clone();
let utrs = {
let lex = lexicon.0.read().expect("poisoned lexicon");
rules.append(&mut lex.background.clone());
let utrs = UntypedTRS::new(rules);
lex.infer_utrs(&utrs, &mut ctx)?;
utrs
};
Ok(TRS {
lex: lexicon,
utrs,
ctx,
})
}
/// The size of the underlying [`term_rewriting::TRS`].
///
/// [`term_rewriting::TRS`]: https://docs.rs/term_rewriting/~0.3/term_rewriting/struct.TRS.html#method.size
pub fn size(&self) -> usize {
self.utrs.size()
}
/// The length of the underlying [`term_rewriting::TRS`].
///
/// [`term_rewriting::TRS`]: https://docs.rs/term_rewriting/~0.3/term_rewriting/struct.TRS.html#method.size
pub fn len(&self) -> usize {
self.utrs.len()
}
/// Is the underlying [`term_rewriting::TRS`] empty?.
///
/// [`term_rewriting::TRS`]: https://docs.rs/term_rewriting/~0.3/term_rewriting/struct.TRS.html#method.size
pub fn is_empty(&self) -> bool {
self.utrs.is_empty()
}
/// A pseudo log prior for a `TRS`: the negative [`size`] of the `TRS`.
///
/// [`size`]: struct.TRS.html#method.size
pub fn pseudo_log_prior(&self) -> f64 {
-(self.size() as f64)
}
/// A log likelihood for a `TRS`: the probability of `data`'s RHSs appearing
/// in [`term_rewriting::Trace`]s rooted at its LHSs.
///
/// [`term_rewriting::Trace`]: https://docs.rs/term_rewriting/~0.3/term_rewriting/trace/struct.Trace.html
pub fn log_likelihood(&self, data: &[Rule], params: ModelParams) -> f64 {
data.iter()
.map(|x| self.single_log_likelihood(x, params))
.sum()
}
/// Compute the log likelihood for a single datum.
fn single_log_likelihood(&self, datum: &Rule, params: ModelParams) -> f64 {
let ll = if let Some(ref rhs) = datum.rhs() {
let mut trace = Trace::new(
&self.utrs,
&datum.lhs,
params.p_observe,
1f64,
params.max_size,
RewriteStrategy::All,
);
trace.rewrites_to(params.max_steps, rhs)
} else {
f64::NEG_INFINITY
};
if ll == f64::NEG_INFINITY {
params.p_partial.ln()
} else {
(1.0 - params.p_partial).ln() + ll
}
}
/// Combine [`pseudo_log_prior`] and [`log_likelihood`], failing early if the
/// prior is `0.0`.
///
/// [`pseudo_log_prior`]: struct.TRS.html#method.pseudo_log_prior
/// [`log_likelihood`]: struct.TRS.html#method.log_likelihood
pub fn posterior(&self, data: &[Rule], params: ModelParams) -> f64 {
let prior = self.pseudo_log_prior();
if prior == f64::NEG_INFINITY {
f64::NEG_INFINITY
} else {
prior + self.log_likelihood(data, params)
}
}
/// Sample a rule and add it to the rewrite system.
///
/// # Example
///
/// ```
/// use polytype::{ptp, tp, Context as TypeContext};
/// use programinduction::trs::{TRS, Lexicon};
/// use rand::{rngs::SmallRng, SeedableRng};
/// use term_rewriting::{Context, RuleContext, Signature, parse_rule};
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some(".".to_string()));
/// ops.push(ptp![0, 1; @arrow[tp!(@arrow[tp!(0), tp!(1)]), tp!(0), tp!(1)]]);
/// sig.new_op(2, Some("PLUS".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("SUCC".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
/// sig.new_op(0, Some("ZERO".to_string()));
/// ops.push(ptp![int]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "PLUS(x_ ZERO) = x_").expect("parsed rule"),
/// parse_rule(&mut sig, "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_))").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// for op in sig.operators() {
/// println!("{:?}/{}", op.name(), op.arity())
/// }
/// for r in &rules {
/// println!("{:?}", r.pretty());
/// }
/// let lexicon = Lexicon::from_signature(sig, ops, vars, vec![], vec![], false, TypeContext::default());
///
/// let mut trs = TRS::new(&lexicon, rules, &lexicon.context()).unwrap();
///
/// assert_eq!(trs.len(), 2);
///
/// let contexts = vec![
/// RuleContext {
/// lhs: Context::Hole,
/// rhs: vec![Context::Hole],
/// }
/// ];
/// let rng = &mut SmallRng::from_seed([1u8; 32]);
/// let atom_weights = (0.5, 0.25, 0.25);
/// let max_size = 50;
///
/// if let Ok(new_trs) = trs.add_rule(&contexts, atom_weights, max_size, rng) {
/// assert_eq!(new_trs.len(), 3);
/// } else {
/// assert_eq!(trs.len(), 2);
/// }
/// ```
pub fn add_rule<R: Rng>(
&self,
contexts: &[RuleContext],
atom_weights: (f64, f64, f64),
max_size: usize,
rng: &mut R,
) -> Result<TRS, SampleError> {
let mut trs = self.clone();
let context = contexts.choose(rng).ok_or(SampleError::OptionsExhausted)?;
let rule = trs.lex.sample_rule_from_context(
rng,
context.clone(),
&mut trs.ctx,
atom_weights,
true,
max_size,
)?;
trs.lex
.0
.write()
.expect("poisoned lexicon")
.infer_rule(&rule, &mut trs.ctx)?;
trs.utrs.push(rule)?;
Ok(trs)
}
/// Delete a rule from the rewrite system if possible. Background knowledge
/// cannot be deleted.
pub fn delete_rule<R: Rng>(&self, rng: &mut R) -> Result<TRS, SampleError> {
let background = &self.lex.0.read().expect("poisoned lexicon").background;
let clauses = self.utrs.clauses();
let deletable: Vec<_> = clauses.iter().filter(|c| !background.contains(c)).collect();
if deletable.is_empty() {
Err(SampleError::OptionsExhausted)
} else {
let mut trs = self.clone();
trs.utrs
.remove_clauses(deletable.choose(rng).ok_or(SampleError::OptionsExhausted)?)?;
Ok(trs)
}
}
/// Move a Rule from one place in the TRS to another at random, excluding the background.
///
/// # Example
///
/// ```
/// use polytype::{ptp, tp, Context as TypeContext};
/// use programinduction::trs::{TRS, Lexicon};
/// use rand::{rngs::SmallRng, SeedableRng};
/// use term_rewriting::{Context, RuleContext, Signature, parse_rule};
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some(".".to_string()));
/// ops.push(ptp![0, 1; @arrow[tp!(@arrow[tp!(0), tp!(1)]), tp!(0), tp!(1)]]);
/// sig.new_op(2, Some("PLUS".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("SUCC".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
/// sig.new_op(0, Some("ZERO".to_string()));
/// ops.push(ptp![int]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "PLUS(x_ ZERO) = x_").expect("parsed rule"),
/// parse_rule(&mut sig, "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_))").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// println!("{:?}", sig.operators());
/// for op in sig.operators() {
/// println!("{:?}/{}", op.name(), op.arity())
/// }
/// for r in &rules {
/// println!("{:?}", r);
/// }
///
/// let ctx = TypeContext::default();
/// let lexicon = Lexicon::from_signature(sig.clone(), ops, vars, vec![], vec![], false, ctx);
///
/// let mut trs = TRS::new(&lexicon, rules, &lexicon.context()).unwrap();
///
/// let pretty_before = trs.to_string();
///
/// let rng = &mut SmallRng::from_seed([1u8; 32]);
///
/// let new_trs = trs.randomly_move_rule(rng).expect("failed when moving rule");
///
/// assert_ne!(pretty_before, new_trs.to_string());
/// assert_eq!(new_trs.to_string(), "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_));\nPLUS(x_ ZERO) = x_;");
/// ```
pub fn randomly_move_rule<R: Rng>(&self, rng: &mut R) -> Result<TRS, SampleError> {
let mut trs = self.clone();
let num_rules = self.len();
let background = &self.lex.0.read().expect("poisoned lexicon").background;
let num_background = background.len();
if num_background < num_rules - 1 {
let i = rng.gen_range(num_background..num_rules);
let mut j = rng.gen_range(num_background..num_rules);
while j == i {
j = rng.gen_range(num_background..num_rules);
}
trs.utrs.move_rule(i, j)?;
Ok(trs)
} else {
Err(SampleError::OptionsExhausted)
}
}
/// Selects a rule from the TRS at random, finds all differences in the LHS and RHS,
/// and makes rules from those differences and inserts them back into the TRS imediately after the background.
///
/// # Example
///
/// ```
/// use polytype::{ptp, tp, Context as TypeContext};
/// use programinduction::trs::{TRS, Lexicon};
/// use rand::{rngs::SmallRng, SeedableRng};
/// use term_rewriting::{Context, RuleContext, Signature, parse_rule};
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some(".".to_string()));
/// ops.push(ptp![0, 1; @arrow[tp!(@arrow[tp!(0), tp!(1)]), tp!(0), tp!(1)]]);
/// sig.new_op(2, Some("PLUS".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("SUCC".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
/// sig.new_op(0, Some("ZERO".to_string()));
/// ops.push(ptp![int]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "SUCC(PLUS(x_ SUCC(y_))) = SUCC(SUCC(PLUS(x_ y_)))").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// for op in sig.operators() {
/// println!("{:?}/{}", op.name(), op.arity())
/// }
/// for r in &rules {
/// println!("{:?}", r.pretty());
/// }
/// let lexicon = Lexicon::from_signature(sig, ops, vars, vec![], vec![], false, TypeContext::default());
///
/// let mut trs = TRS::new(&lexicon, rules, &lexicon.context()).unwrap();
///
/// assert_eq!(trs.len(), 1);
///
/// let rng = &mut SmallRng::from_seed([1u8; 32]);
///
/// if let Ok(new_trs) = trs.local_difference(rng) {
/// assert_eq!(new_trs.len(), 2);
/// let display_str = format!("{}", new_trs);
/// assert_eq!(display_str, "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_));\nSUCC(PLUS(x_ SUCC(y_))) = SUCC(SUCC(PLUS(x_ y_)));");
/// } else {
/// assert_eq!(trs.len(), 1);
/// }
/// ```
pub fn local_difference<R: Rng>(&self, rng: &mut R) -> Result<TRS, SampleError> {
let mut trs = self.clone();
let num_rules = self.len();
let background = &self.lex.0.read().expect("poisoned lexicon").background;
let num_background = background.len();
if num_rules > num_background {
let idx = rng.gen_range(num_background..num_rules);
let new_rules = TRS::local_difference_helper(&trs.utrs.rules[idx]);
if !new_rules.is_empty() {
trs.utrs.remove_idx(idx)?;
trs.utrs.inserts_idx(num_background, new_rules)?;
return Ok(trs);
}
}
Err(SampleError::OptionsExhausted)
}
/// Given a rule that has similar terms in the lhs and rhs,
/// returns a list of rules where each similarity is removed one at a time
fn local_difference_helper(rule: &Rule) -> Vec<Rule> {
if let Some(rhs) = rule.rhs() {
TRS::find_differences(&rule.lhs, &rhs)
.into_iter()
.filter_map(|(lhs, rhs)| Rule::new(lhs, vec![rhs]))
.collect_vec()
} else {
vec![]
}
}
// helper for local difference, finds differences in the given lhs and rhs recursively
fn find_differences(lhs: &Term, rhs: &Term) -> Vec<(Term, Term)> {
if lhs == rhs {
return vec![];
}
match (lhs, rhs) {
(Term::Variable(_), _) => vec![], // Variable can't be head of rule
(
Term::Application {
op: lop,
args: largs,
},
Term::Application {
op: rop,
args: rargs,
},
) if lop == rop && !largs.is_empty() => largs
.iter()
.zip(rargs)
.flat_map(|(l, r)| TRS::find_differences(l, r))
.chain(once((lhs.clone(), rhs.clone())))
.collect_vec(),
_ => vec![(lhs.clone(), rhs.clone())],
}
}
/// Selects a rule from the TRS at random, swaps the LHS and RHS if possible and inserts the resulting rules
/// back into the TRS imediately after the background.
///
/// # Example
///
/// ```
/// use polytype::{ptp, tp, Context as TypeContext};
/// use programinduction::trs::{TRS, Lexicon};
/// use rand::{rngs::SmallRng, SeedableRng};
/// use term_rewriting::{Context, RuleContext, Signature, parse_rule};
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some(".".to_string()));
/// ops.push(ptp![0, 1; @arrow[tp!(@arrow[tp!(0), tp!(1)]), tp!(0), tp!(1)]]);
/// sig.new_op(2, Some("PLUS".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("SUCC".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
/// sig.new_op(0, Some("ZERO".to_string()));
/// ops.push(ptp![int]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "PLUS(x_ SUCC(y_)) = SUCC(PLUS(x_ y_)) | PLUS(SUCC(x_) y_)").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// for op in sig.operators() {
/// println!("{:?}/{}", op.name(), op.arity())
/// }
/// for r in &rules {
/// println!("{:?}", r.pretty());
/// }
///
/// let lexicon = Lexicon::from_signature(sig, ops, vars, vec![], vec![], false, TypeContext::default());
///
/// let mut trs = TRS::new(&lexicon, rules, &lexicon.context()).unwrap();
///
/// assert_eq!(trs.len(), 1);
///
/// let rng = &mut SmallRng::from_seed([1u8; 32]);
///
/// if let Ok(new_trs) = trs.swap_lhs_and_rhs(rng) {
/// assert_eq!(new_trs.len(), 2);
/// let display_str = format!("{}", new_trs);
/// assert_eq!(display_str, "SUCC(PLUS(x_ y_)) = PLUS(x_ SUCC(y_));\nPLUS(SUCC(x_) y_) = PLUS(x_ SUCC(y_));");
/// } else {
/// assert_eq!(trs.len(), 1);
/// }
///
///
/// let mut sig = Signature::default();
///
/// let mut ops = vec![];
/// sig.new_op(2, Some(".".to_string()));
/// ops.push(ptp![0, 1; @arrow[tp!(@arrow[tp!(0), tp!(1)]), tp!(0), tp!(1)]]);
/// sig.new_op(2, Some("A".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int), tp!(int)]]);
/// sig.new_op(1, Some("B".to_string()));
/// ops.push(ptp![@arrow[tp!(int), tp!(int)]]);
///
/// let rules = vec![
/// parse_rule(&mut sig, "A(x_ y_) = B(x_ )").expect("parsed rule"),
/// ];
///
/// let vars = vec![
/// ptp![int],
/// ptp![int],
/// ptp![int],
/// ];
///
/// let lexicon = Lexicon::from_signature(sig, ops, vars, vec![], vec![], false, TypeContext::default());
///
/// let mut trs = TRS::new(&lexicon, rules, &lexicon.context()).unwrap();
///
/// assert!(trs.swap_lhs_and_rhs(rng).is_err());
/// ```
pub fn swap_lhs_and_rhs<R: Rng>(&self, rng: &mut R) -> Result<TRS, SampleError> {
let num_rules = self.len();
let num_background = self
.lex
.0
.read()
.expect("poisoned lexicon")
.background
.len();
if num_background <= num_rules {
let idx = rng.gen_range(num_background..num_rules);
let mut trs = self.clone();
let new_rules = TRS::swap_rule_helper(&trs.utrs.rules[idx])?;
trs.utrs.remove_idx(idx)?;
trs.utrs.inserts_idx(num_background, new_rules)?;
Ok(trs)
} else {
Err(SampleError::OptionsExhausted)
}
}
/// returns a vector of a rules with each rhs being the lhs of the original
/// rule and each lhs is each rhs of the original.
fn swap_rule_helper(rule: &Rule) -> Result<Vec<Rule>, SampleError> {
let rules = rule
.clauses()
.iter()
.filter_map(TRS::swap_clause_helper)
.collect_vec();
if rules.is_empty() {
Err(SampleError::OptionsExhausted)
} else {
Ok(rules)
}
}
/// Swap lhs and rhs iff the rule is deterministic and swap is a valid rule.
fn swap_clause_helper(rule: &Rule) -> Option<Rule> {
rule.rhs()
.and_then(|rhs| Rule::new(rhs, vec![rule.lhs.clone()]))
}
}
impl fmt::Display for TRS {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let true_len = self.utrs.len()
- self
.lex
.0
.read()
.expect("poisoned lexicon")
.background
.len();
let trs_str = self
.utrs
.rules
.iter()
.take(true_len)
.map(|r| format!("{};", r.display()))
.join("\n");
write!(f, "{}", trs_str)
}
}