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
use crate::lterm::LTerm;
use crate::lvalue::LValue;
use crate::state::constraint::store::ConstraintStore;
use crate::state::constraint::Constraint;
use crate::user::UserState;
use std::fmt;
use std::ops::Deref;
use std::rc::Rc;

#[derive(Clone, Debug)]
pub struct LResult<U: UserState>(pub Rc<LTerm>, pub Rc<ConstraintStore<U>>);

impl<U: UserState> LResult<U> {
    /// Check if the wrapped LTerm is an Any-variable with constraints such that it cannot be
    /// the `exception`.
    pub fn is_any_except<T>(&self, exception: &T) -> bool
    where
        T: PartialEq<LTerm>,
    {
        if self.0.is_any() {
            // result is an `any` variable, see if it has the expected constraint
            for constraint in self.constraints() {
                if let Constraint::Tree(tree) = constraint {
                    for (cu, cv) in tree.smap_ref().iter() {
                        if &self.0 == cu && exception == &**cv
                            || &self.0 == cv && exception == &**cu
                        {
                            return true;
                        }
                    }
                }
            }
        }

        false
    }

    /// Check if the wrapped LTerm is constrained by any constraint.
    pub fn is_constrained(&self) -> bool {
        self.constraints().any(|_| true)
    }

    /// Returns iterator to constraints that refer to the wrapped LTerm.
    pub fn constraints<'a>(&'a self) -> impl Iterator<Item = &'a Constraint<U>> {
        let anyvars = self.0.anyvars();
        self.1.relevant(&anyvars)
    }
}

impl<U: UserState> Deref for LResult<U> {
    type Target = Rc<LTerm>;

    fn deref(&self) -> &Rc<LTerm> {
        &self.0
    }
}

impl<U: UserState> fmt::Display for LResult<U> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)?;
        if self.is_constrained() {
            write!(f, "  where  {{ ")?;
            self.1.display_relevant(&self.0, f)?;
            write!(f, " }}")
        } else {
            write!(f, "")
        }
    }
}

impl<U: UserState> PartialEq<LTerm> for LResult<U> {
    fn eq(&self, other: &LTerm) -> bool {
        &*self == other
    }
}

impl<U: UserState> PartialEq<LResult<U>> for LTerm {
    fn eq(&self, other: &LResult<U>) -> bool {
        &*other == self
    }
}

impl<U: UserState> PartialEq<Rc<LTerm>> for LResult<U> {
    fn eq(&self, other: &Rc<LTerm>) -> bool {
        &self.0 == other
    }
}

impl<U: UserState> PartialEq<LResult<U>> for Rc<LTerm> {
    fn eq(&self, other: &LResult<U>) -> bool {
        &other.0 == self
    }
}

impl<U: UserState> PartialEq<LValue> for LResult<U> {
    fn eq(&self, other: &LValue) -> bool {
        match self.as_ref() {
            LTerm::Val(v) => v == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for LValue {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(v) => v == self,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<bool> for LResult<U> {
    fn eq(&self, other: &bool) -> bool {
        match self.as_ref() {
            LTerm::Val(LValue::Bool(x)) => x == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for bool {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(LValue::Bool(x)) => x == self,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<isize> for LResult<U> {
    fn eq(&self, other: &isize) -> bool {
        match self.as_ref() {
            LTerm::Val(LValue::Number(x)) => x == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for isize {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(LValue::Number(x)) => x == self,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<char> for LResult<U> {
    fn eq(&self, other: &char) -> bool {
        match self.as_ref() {
            LTerm::Val(LValue::Char(x)) => x == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for char {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(LValue::Char(x)) => x == self,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<String> for LResult<U> {
    fn eq(&self, other: &String) -> bool {
        match self.as_ref() {
            LTerm::Val(LValue::String(x)) => x == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for String {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(LValue::String(x)) => x == self,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<&str> for LResult<U> {
    fn eq(&self, other: &&str) -> bool {
        match self.as_ref() {
            LTerm::Val(LValue::String(x)) => x == other,
            _ => false,
        }
    }
}

impl<U: UserState> PartialEq<LResult<U>> for &str {
    fn eq(&self, other: &LResult<U>) -> bool {
        match other.as_ref() {
            LTerm::Val(LValue::String(x)) => x == self,
            _ => false,
        }
    }
}