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

extern crate terms;
extern crate tree_automata as ta;

use std::ops::Index;
use std::cmp::{PartialOrd, Ord, Ordering};
use std::slice::SliceIndex;
use std::fmt;
use terms::Term;
use ta::Ranked;

pub mod convolution;
pub mod pattern;

pub use convolution::Convolution;
pub use pattern::ConvolutedPattern;

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct Convoluted<T>(pub Vec<MaybeBottom<T>>);

impl<T> Convoluted<T> {
    pub fn iter(&self) -> std::slice::Iter<MaybeBottom<T>> {
        self.0.iter()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn signature(&self) -> u32 {
        let mut signature: u32 = 0;
        for t in &self.0 {
            signature <<= 1;
            if let MaybeBottom::Some(_) = t {
                signature |= 1;
            }
        }

        signature
    }
}

impl<T, I> Index<I> for Convoluted<T> where I: SliceIndex<[MaybeBottom<T>]> {
    type Output = I::Output;

    fn index(&self, i: I) -> &I::Output {
        self.0.index(i)
    }
}

impl<T: Ord> Ord for Convoluted<T> {
    fn cmp(&self, other: &Convoluted<T>) -> Ordering {
        match self.0.len().cmp(&other.0.len()) {
            Ordering::Equal => {
                for (i, a) in self.0.iter().enumerate() {
                    let b = &other.0[i];
                    match a.cmp(b) {
                        Ordering::Equal => (),
                        ord => return ord
                    }
                }

                Ordering::Equal
            },
            ord => ord
        }
    }
}

impl<T: Ord> PartialOrd for Convoluted<T> {
    fn partial_cmp(&self, other: &Convoluted<T>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum MaybeBottom<T> {
    Bottom,
    Some(T)
}

impl<T: PartialEq> PartialEq<T> for MaybeBottom<T> {
    fn eq(&self, other: &T) -> bool {
        match self {
            MaybeBottom::Some(t) => t == other,
            _ => false
        }
    }
}

impl<T: Ord> Ord for MaybeBottom<T> {
    fn cmp(&self, other: &MaybeBottom<T>) -> Ordering {
        match (self, other) {
            (MaybeBottom::Some(a), MaybeBottom::Some(b)) => a.cmp(b),
            (MaybeBottom::Some(_), _) => Ordering::Greater,
            (MaybeBottom::Bottom, MaybeBottom::Bottom) => Ordering::Equal,
            _ => Ordering::Less
        }
    }
}

impl<T: Ord> PartialOrd for MaybeBottom<T> {
    fn partial_cmp(&self, other: &MaybeBottom<T>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

pub trait Relation<F: Ranked> {
    /// Check if the relation contains the given terms.
    /// The number of term must be equal to the arity of the relation.
    fn contains(&self, terms: &[&Term<F>]) -> bool;
}

impl<T: fmt::Display> fmt::Display for MaybeBottom<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MaybeBottom::Bottom => write!(f, "⟘"),
            MaybeBottom::Some(t) => t.fmt(f)
        }
    }
}

impl<T: fmt::Display> fmt::Display for Convoluted<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0.split_first() {
            Some((head, tail)) => {
                head.fmt(f)?;
                for e in tail.iter() {
                    write!(f, "⊗")?;
                    e.fmt(f)?;
                }
                Ok(())
            },
            None => Ok(())
        }
    }
}