]> Witch of Git - mukan/blob - src/term.rs
Make the Bind iterator for AND goals fair
[mukan] / src / term.rs
1 use std::{fmt, sync::Arc};
2
3 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
4 pub struct Var(pub(crate) u32);
5
6 #[derive(Clone, PartialEq, Eq)]
7 pub enum Term {
8 Var(Var),
9 Pair(Arc<Term>, Arc<Term>),
10 Lit(i32),
11 }
12
13 impl From<&Term> for Term {
14 fn from(term: &Term) -> Term {
15 term.clone()
16 }
17 }
18
19 impl From<i32> for Term {
20 fn from(i: i32) -> Term {
21 Term::Lit(i)
22 }
23 }
24
25 impl From<Var> for Term {
26 fn from(var: Var) -> Term {
27 Term::Var(var)
28 }
29 }
30
31 impl<T, U> From<(T, U)> for Term
32 where
33 T: Into<Term>,
34 U: Into<Term>,
35 {
36 fn from((t, u): (T, U)) -> Term {
37 Term::Pair(Arc::new(t.into()), Arc::new(u.into()))
38 }
39 }
40
41 impl fmt::Display for Term {
42 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
43 match self {
44 Term::Var(v) => write!(fmt, "{:?}", v),
45 Term::Pair(l, r) => write!(fmt, "({}, {})", *l, *r),
46 Term::Lit(i) => write!(fmt, "{}", i),
47 }
48 }
49 }
50
51 impl fmt::Debug for Term {
52 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
53 write!(fmt, "{}", self)
54 }
55 }
56
57 impl fmt::Debug for Var {
58 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
59 write!(fmt, "x{}", self.0)
60 }
61 }