terms 0.1.3

Tree terms and patterns data structures
Documentation
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
//use std::rc::Arc;
use std::sync::Arc;
use std::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::collections::hash_map::DefaultHasher;
use std::sync::atomic::{self, AtomicU64};
use std::fmt;
use crate::Term;

// pub trait Meta<F>: Clone + Eq + Sized + fmt::Debug {
//	 /// Gives the sub-patterns needed to recognize the given symbol.
//	 fn matches(&self, index: usize, f: &F, arity: usize) -> Option<(Vec<Self>, Self)>;
// }
//
// impl<F> Meta<F> for () {
//	 fn matches(&self, _index: usize, _f: &F, arity: usize) -> Option<(Vec<Self>, Self)> {
//		 let mut sub_patterns = Vec::with_capacity(arity);
//		 sub_patterns.resize(arity, ());
//		 Some((sub_patterns, ()))
//	 }
// }

/// Any object that can act like a pattern. Such as a term.
pub trait PatternLike<F, X>: Sized {
	fn kind(&self) -> PatternLikeKind<F, X, Self>;
}

pub enum PatternLikeKind<'a, F, X, T: PatternLike<F, X>> {
	Cons(&'a F, &'a [T]),
	Var(&'a X)
}

pub trait Spawnable: Sized {
	fn spawn() -> Self;
}

pub struct Pattern<F, X> {
	kind: PatternKind<F, X>,
	hash: AtomicU64
}

impl<F, X> PatternLike<F, X> for Pattern<F, X> {
	fn kind(&self) -> PatternLikeKind<F, X, Self> {
		match &self.kind {
			PatternKind::Cons(f, subs) => PatternLikeKind::Cons(&f, &subs),
			PatternKind::Var(x) => PatternLikeKind::Var(&x)
		}
	}
}

impl<F: fmt::Debug, X: fmt::Debug> fmt::Debug for Pattern<F, X> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match &self.kind {
			PatternKind::Cons(g, subs) => write!(f, "{:?}({:?})", g, subs),
			PatternKind::Var(x) => write!(f, "{:?}", x)
		}
	}
}

impl<F: fmt::Display, X: fmt::Display> fmt::Display for Pattern<F, X> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match &self.kind {
			PatternKind::Cons(g, subs) => {
				g.fmt(f)?;
				match subs.split_first() {
					Some((head, tail)) => {
						write!(f, "(")?;
						head.fmt(f)?;
						for e in tail.iter() {
							write!(f, ", ")?;
							e.fmt(f)?;
						}
						write!(f, ")")
					},
					None => Ok(())
				}
			},
			PatternKind::Var(x) => x.fmt(f)
		}
	}
}

/// A pattern.
pub enum PatternKind<F, X> {
	Cons(F, Arc<Vec<Pattern<F, X>>>),
	Var(X)
}

impl<F, X> Pattern<F, X> {
	pub fn kind(&self) -> &PatternKind<F, X> {
		&self.kind
	}

	pub fn into_kind(self) -> PatternKind<F, X> {
		self.kind
	}

	pub fn cons(f: F, subs: Vec<Self>) -> Self where F: Clone, X: Clone {
		Pattern {
			kind: PatternKind::Cons(f, Arc::new(subs)),
			hash: AtomicU64::new(0)
		}
	}

	pub fn from_slice(f: F, subs: &[Self]) -> Self where F: Clone, X: Clone {
		Pattern {
			kind: PatternKind::Cons(f, Arc::new(subs.iter().map(|p| p.clone()).collect())),
			hash: AtomicU64::new(0)
		}
	}

	pub fn var(x: X) -> Self {
		Pattern {
			kind: PatternKind::Var(x),
			hash: AtomicU64::new(0)
		}
	}

	pub fn symbol(&self) -> Option<&F> {
		match &self.kind {
			PatternKind::Cons(f, _) => Some(f),
			_ => None
		}
	}

	pub fn sub_patterns(&self) -> Option<&Vec<Self>> {
		match &self.kind {
			PatternKind::Cons(_, list) => Some(list),
			_ => None
		}
	}

	// /// Beware that this may change the pattern hash!
	// pub fn sub_patterns_mut(&mut self) -> Option<&mut Vec<Arc<Self>>> {
	//	 self.hash.set(0);
	//	 match &mut self.kind {
	//		 PatternKind::Cons(_, ref mut list) => Some(list),
	//		 _ => None
	//	 }
	// }

	pub fn get(&self, i: usize) -> Option<&Self> {
		match &self.kind {
			PatternKind::Cons(_, list) => list.get(i),
			_ => None
		}
	}

	pub fn as_cons(&self) -> Option<(&F, &Vec<Self>)> {
		match &self.kind {
			PatternKind::Cons(f, list) => Some((f, list)),
			_ => None
		}
	}

	pub fn as_term(&self) -> Option<Term<F>> where F: Clone {
		match &self.kind {
			PatternKind::Var(_) => None,
			PatternKind::Cons(f, sub_patterns) => {
				let mut sub_terms = Vec::with_capacity(sub_patterns.len());
				for sub in sub_patterns.iter() {
					match sub.as_term() {
						Some(term) => sub_terms.push(term),
						None => return None
					}
				}

				Some(Term::new(f.clone(), sub_terms))
			}
		}
	}

	pub fn variables(&self) -> UniqueVariables<X> where X: PartialEq {
		UniqueVariables::new(self)
	}

	pub fn map_variables<Y, M>(&self, g: &M) -> Pattern<F, Y> where M: Fn(&X) -> Pattern<F, Y>, F: Clone {
		let kind = match &self.kind {
			PatternKind::Var(x) => {
				((*g)(x)).kind
			},
			PatternKind::Cons(f, sub_patterns) => {
				let mapped_sub_patterns = sub_patterns.iter().map(|sub| sub.map_variables(g)).collect();
				PatternKind::Cons(f.clone(), Arc::new(mapped_sub_patterns))
			}
		};

		Pattern {
			kind: kind,
			hash: AtomicU64::new(0)
		}
	}

	pub fn try_map_variables<Y, M>(&self, g: &M) -> Option<Pattern<F, Y>> where M: Fn(&X) -> Option<Pattern<F, Y>>, F: Clone {
		let kind = match &self.kind {
			PatternKind::Var(x) => {
				match (*g)(x) {
					Some(p) => p.kind,
					None => return None
				}
			},
			PatternKind::Cons(f, sub_patterns) => {
				let mut mapped_sub_patterns = Vec::with_capacity(sub_patterns.len());
				for sub in sub_patterns.iter() {
					match sub.try_map_variables(g) {
						Some(mapped_sub) => mapped_sub_patterns.push(mapped_sub),
						None => return None
					}
				}
				PatternKind::Cons(f.clone(), Arc::new(mapped_sub_patterns))
			}
		};

		Some(Pattern {
			kind: kind,
			hash: AtomicU64::new(0)
		})
	}

	/// Find a renaming from X -> Y so that both patterns are equals.
	pub fn renaming<Z: Clone, W: Clone, Y: AsRef<W>>(&self, other: &Pattern<F, Y>, renaming: &mut HashMap<Z, W>) -> bool where X: AsRef<Z> + PartialOrd<Y>, Z: Hash + Eq, W: Eq, F: PartialEq {
		match (self.kind(), other.kind()) {
			(PatternKind::Cons(f1, subs1), PatternKind::Cons(f2, subs2)) if f1 == f2 && subs1.len() == subs2.len() => {
				for i in 0..subs1.len() {
					let a = &subs1[i];
					let b = &subs2[i];

					if !a.renaming(b, renaming) {
						return false
					}
				}

				true
			},
			(PatternKind::Var(x), PatternKind::Var(y)) => {
				let z = x.as_ref();
				if let Some(w) = renaming.get(z) {
					if w == y.as_ref() {
						true
					} else {
						false
					}
				} else {
					if x <= y {
						renaming.insert(z.clone(), y.as_ref().clone());
						true
					} else {
						false
					}
				}
			},
			_ => false
		}
	}
}

impl<F, X> From<PatternKind<F, X>> for Pattern<F, X> {
	fn from(kind: PatternKind<F, X>) -> Pattern<F, X> {
		Pattern {
			kind: kind,
			hash: AtomicU64::new(0)
		}
	}
}

pub enum UniqueVariables<'a, X: PartialEq> {
	Var(&'a X, bool),
	Cons(Box<Vec<UniqueVariables<'a, X>>>, Vec<&'a X>)
}

impl<'a, X: PartialEq> UniqueVariables<'a, X> {
	pub fn new<F>(pattern: &'a Pattern<F, X>) -> UniqueVariables<'a, X> {
		match &pattern.kind {
			PatternKind::Var(x) => UniqueVariables::Var(x, false),
			PatternKind::Cons(_, sub_patterns) => {
				let iterators = sub_patterns.iter().map(|sub| {
					sub.variables()
				}).collect();

				UniqueVariables::Cons(Box::new(iterators), Vec::new())
			}
		}
	}
}

impl<'a, X: PartialEq> Iterator for UniqueVariables<'a, X> {
	type Item = &'a X;

	fn next(&mut self) -> Option<&'a X> {
		match self {
			UniqueVariables::Var(x, visited) => {
				if *visited {
					None
				} else {
					*visited = true;
					Some(x)
				}
			},
			UniqueVariables::Cons(ref mut iterators, ref mut visited) => {
				for it in iterators.iter_mut() {
					loop {
						match it.next() {
							Some(x) => {
								if !visited.contains(&x) {
									visited.push(x);
									return Some(x)
								}
							},
							None => break
						}
					}
				}
				None
			}
		}
	}
}

// impl<F: Clone + Eq + fmt::Debug, X: Eq + fmt::Debug + Clone + Spawnable> Meta<F> for Pattern<F, X> {
//	 fn matches(&self, _index: usize, f: &F, arity: usize) -> Option<(Vec<Self>, Self)> {
//		 match &self.kind {
//			 PatternKind::Cons(g, list) if g == f && list.len() == arity => {
//				 let sub_patterns = list.iter().map(|p| (**p).clone()).collect();
//				 Some((sub_patterns, self.clone()))
//			 },
//			 PatternKind::Var(_) => {
//				 let mut sub_patterns = Vec::with_capacity(arity);
//				 for i in 0..arity {
//					 sub_patterns.push(X::spawn().into())
//				 }
//				 Some((sub_patterns, self.clone()))
//			 },
//			 _ => None
//		 }
//	 }
// }

impl<F: Clone, X: Clone> Clone for Pattern<F, X> {
	fn clone(&self) -> Pattern<F, X> {
		let kind = match &self.kind {
			PatternKind::Cons(f, l) => PatternKind::Cons(f.clone(), l.clone()),
			PatternKind::Var(x) => PatternKind::Var(x.clone())
		};
		Pattern {
			kind: kind,
			hash: AtomicU64::new(self.hash.load(atomic::Ordering::Relaxed))
		}
	}
}

impl<F: PartialEq, X: PartialEq> PartialEq for Pattern<F, X> {
	fn eq(&self, other: &Pattern<F, X>) -> bool {
		match (&self.kind, &other.kind) {
			(PatternKind::Cons(f1, subs1), PatternKind::Cons(f2, subs2)) => {
				f1 == f2 && subs1 == subs2
			},
			(PatternKind::Var(x1), PatternKind::Var(x2)) => x1 == x2,
			_ => false
		}
	}
}

impl<F: PartialEq + Eq, X: PartialEq + Eq> Eq for Pattern<F, X> {}

impl<F: Hash, X: Hash> Hash for Pattern<F, X> {
	fn hash<H: Hasher>(&self, state: &mut H) {
		let mut h = self.hash.load(atomic::Ordering::Relaxed);
		if h == 1 { // hash is beeing computed by another thread.
			loop {
				h = self.hash.load(atomic::Ordering::Relaxed);
				if h != 1 {
					break;
				}
			}
		}
		if h == 0 {
			//self.hash.set(1); // set to 1 to avoid loops.
			self.hash.store(1, atomic::Ordering::Relaxed);

			let mut hasher = DefaultHasher::new();
			match &self.kind {
				PatternKind::Cons(f, l) => {
					f.hash(&mut hasher);
					for sub in l.iter() {
						Pattern::<F, X>::hash(sub, &mut hasher)
					}
				},
				PatternKind::Var(x) => {
					x.hash(&mut hasher)
				}
			}
			h = hasher.finish();
			if h <= 1 { // just to be sure...
				h = 2;
			}
			self.hash.store(h, atomic::Ordering::Relaxed);
		}
		h.hash(state)
	}
}

impl<F, X> From<X> for Pattern<F, X> {
	fn from(x: X) -> Self {
		Pattern {
			kind: PatternKind::Var(x),
			hash: AtomicU64::new(0)
		}
	}
}