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
#![feature(trait_alias)]

extern crate terms;

use std::fmt;
use std::hash::{Hash, Hasher};
use std::cmp::{PartialOrd, Ord, Ordering};
pub use terms::Ranked;

mod state;
mod label;
mod language;
mod inter;
mod utils;
pub mod bottom_up;
pub mod alternating;

pub use state::*;
pub use label::*;
pub use language::*;
pub use inter::*;

pub use utils::*;

#[cfg(not(debug_assertions))]
pub trait Symbol = Hash + Clone + Eq;

#[cfg(debug_assertions)]
pub trait Symbol = Hash + Clone + Eq + fmt::Display + fmt::Debug;

pub struct Rank<T>(pub T, pub usize);

impl<T> Ranked for Rank<T> {
    fn arity(&self) -> usize {
        self.1
    }
}

impl<T: Ord> Ord for Rank<T> {
    fn cmp(&self, other: &Rank<T>) -> Ordering {
        match self.1.cmp(&other.1) {
            Ordering::Equal => self.0.cmp(&other.0),
            ord => ord
        }
    }
}

impl<T: PartialOrd> PartialOrd for Rank<T> {
    fn partial_cmp(&self, other: &Rank<T>) -> Option<Ordering> {
        match self.1.cmp(&other.1) {
            Ordering::Equal => self.0.partial_cmp(&other.0),
            ord => Some(ord)
        }
    }
}

impl<T: Eq> Eq for Rank<T> {}

impl<T: PartialEq> PartialEq for Rank<T> {
    fn eq(&self, other: &Rank<T>) -> bool {
        self.1 == other.1 && self.0 == other.0
    }
}

impl<T: Hash> Hash for Rank<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
        self.1.hash(state);
    }
}

impl<T: Copy> Copy for Rank<T> {}

impl<T: Clone> Clone for Rank<T> {
    fn clone(&self) -> Rank<T> {
        Rank(self.0.clone(), self.1)
    }
}

impl<T: fmt::Display> fmt::Display for Rank<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}:{}", self.0, self.1)
    }
}

impl<T: fmt::Debug> fmt::Debug for Rank<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}:{}", self.0, self.1)
    }
}

// Sorted instances.
pub trait SortedWith<T> {
    fn sort(&self) -> &T;
}

impl<A, B> SortedWith<B> for (A, B) {
    fn sort(&self) -> &B {
        &self.1
    }
}

pub struct Sorted<X, T>(pub X, pub T);

impl<X, T> Sorted<X, T> {
    pub fn new(x: X, t: T) -> Sorted<X, T> {
        Sorted(x, t)
    }
}

impl<X, T> SortedWith<T> for Sorted<X, T> {
    fn sort(&self) -> &T {
        &self.1
    }
}

impl<X: Ord, T: Ord> Ord for Sorted<X, T> {
    fn cmp(&self, other: &Sorted<X, T>) -> Ordering {
        match self.1.cmp(&other.1) {
            Ordering::Equal => self.0.cmp(&other.0),
            ord => ord
        }
    }
}

impl<X: PartialOrd, T: PartialOrd> PartialOrd for Sorted<X, T> {
    fn partial_cmp(&self, other: &Sorted<X, T>) -> Option<Ordering> {
        match self.1.partial_cmp(&other.1) {
            Some(Ordering::Equal) => self.0.partial_cmp(&other.0),
            Some(ord) => Some(ord),
            None => None
        }
    }
}

impl<X: Eq, T: Eq> Eq for Sorted<X, T> {}

impl<X:PartialEq, T: PartialEq> PartialEq for Sorted<X, T> {
    fn eq(&self, other: &Sorted<X, T>) -> bool {
        self.1 == other.1 && self.0 == other.0
    }
}

impl<X: Hash, T: Hash> Hash for Sorted<X, T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
        self.1.hash(state);
    }
}

impl<X: Copy, T: Copy> Copy for Sorted<X, T> {}

impl<X: Clone, T: Clone> Clone for Sorted<X, T> {
    fn clone(&self) -> Sorted<X, T> {
        Sorted(self.0.clone(), self.1.clone())
    }
}

impl<X: fmt::Display, T: fmt::Display> fmt::Display for Sorted<X, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}:{}", self.0, self.1)
    }
}

impl<X: fmt::Debug, T: fmt::Debug> fmt::Debug for Sorted<X, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}:{:?}", self.0, self.1)
    }
}