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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use std::fmt;
use std::collections::{hash_set, hash_map, HashSet, HashMap};
use terms::{
    PatternLike,
    PatternLikeKind
};

use crate::{
    utils::combinations,
    Symbol,
    State,
    Label,
    Ranked,
    NoLabel,
    Labeled,
    Language,
    //LanguageState,
    ConfigurationIterator
};

pub mod macros;
pub mod search;
pub mod width_search;

pub use search::*;
pub use width_search::*;

/// Tree automaton configuration.
#[derive(Hash, PartialEq, Eq, Clone, Debug)]
pub struct Configuration<F, Q: State>(pub F, pub Vec<Q>);

impl<F, Q: State> Configuration<F, Q> {
    pub fn symbol(&self) -> &F {
        &self.0
    }

    pub fn states(&self) -> &[Q] {
        &self.1
    }

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

    pub fn signature(&self) -> (&F, usize) {
        (&self.0, self.1.len())
    }

    pub fn map<R: State, M>(&self, g: M) -> Configuration<F, R> where M: Fn(&Q) -> R, F: Clone {
        let states = self.1.iter().map(|q| g(q)).collect();
        Configuration(self.0.clone(), states)
    }
}

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

pub type Configurations<'a, F, Q, L> = hash_set::Iter<'a, Labeled<Configuration<F, Q>, L>>;

pub struct Transifions<'a, F, Q: State, L: Label> {
    it: hash_map::Iter<'a, Q, HashSet<Labeled<Configuration<F, Q>, L>>>,
    current: Option<(&'a Q, Configurations<'a, F, Q, L>)>
}

impl<'a, F, Q: State, L: Label> Transifions<'a, F, Q, L> {
    fn new(aut: &'a Automaton<F, Q, L>) -> Transifions<'a, F, Q, L> {
        Transifions {
            it: aut.state_configurations.iter(),
            current: None
        }
    }
}

impl<'a, F, Q: State, L: Label> Iterator for Transifions<'a, F, Q, L> {
    type Item = (&'a Configuration<F, Q>, &'a L, &'a Q);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.current {
                Some((q, ref mut configurations)) => {
                    match configurations.next() {
                        Some((configuration, label)) => {
                            return Some((configuration, label, q))
                        },
                        None => self.current = None
                    }
                },
                None => {
                    match self.it.next() {
                        Some((q, configurations)) => self.current = Some((q, configurations.iter())),
                        None => return None
                    }
                }
            }
        }
    }
}

/// Tree automaton.
#[derive(Clone)]
pub struct Automaton<F, Q: State, L: Label> {
    /// An empty set of labeled states used to return an empty iterator.
    dummy_states: HashSet<Labeled<Q, L>>,

    /// An empty set of labeled configurations used to return an empty iterator.
    dummy_configurations: HashSet<Labeled<Configuration<F, Q>, L>>,

    /// For each configuration t, associates the set of states such that t -> q.
    configuration_states: HashMap<Configuration<F, Q>, HashSet<Labeled<Q, L>>>,

    /// For each state q, associates the set of configurations such that t -> q.
    state_configurations: HashMap<Q, HashSet<Labeled<Configuration<F, Q>, L>>>,

    // /// For each configuration, associate every known labeled version.
    // labeled_configurations: HashMap<Configuration<F, Q>, HashSet<Labeled<Configuration<F, Q>, L>>>,

    /// Final states of the automaton.
    final_states: HashSet<Q>
}

pub type States<'a, F, Q, L> = hash_map::Keys<'a, Q, HashSet<Labeled<Configuration<F, Q>, L>>>;

impl<F: Symbol, Q: State, L: Label> Language<F> for Automaton<F, Q, L> {
    // ...
}

impl<F: Symbol, Q: State, L: Label> Automaton<F, Q, L> {
    // type Configuration = Configuration<F, Q>;

    /// Create a new empty automaton.
    pub fn new() -> Automaton<F, Q, L> {
        Automaton {
            dummy_states: HashSet::new(),
            dummy_configurations: HashSet::new(),
            configuration_states: HashMap::new(),
            state_configurations: HashMap::new(),
            // labeled_configurations: HashMap::new(),
            final_states: HashSet::new()
        }
    }

    /// Return the number of states in the automaton.
    pub fn len(&self) -> usize {
        self.state_configurations.len()
    }

    pub fn states(&self) -> States<F, Q, L> {
        self.state_configurations.keys()
    }

    /// Return an iterator to the final states of the automaton.
    pub fn final_states(&self) -> hash_set::Iter<Q> {
        self.final_states.iter()
    }

    /// Set the given state a final state.
    /// Return `true` if the state was not already final.
    /// Return `false` if the state was already a final state.
    pub fn set_final(&mut self, q: Q) -> bool {
        self.final_states.insert(q)
    }

    /// Checks if the given state is in the automaton.
    /// Return true if at least one configuration is attached to the state in the automaton.
    pub fn includes(&self, q: &Q) -> bool {
        self.state_configurations.get(q).is_some()
    }

    pub fn transitions(&self) -> Transifions<F, Q, L> {
        Transifions::new(self)
    }

    /// Return an iterator over the configurations connected to the given state.
    pub fn configurations_for_state(&self, q: &Q) -> Configurations<F, Q, L> {
        match self.state_configurations.get(q) {
            Some(confs) => confs.iter(),
            None => self.dummy_configurations.iter()
        }
    }

    // /// Return an iterator over the configurations compatible with the given configuration.
    // pub fn configurations_for<'a>(&'a self, source: &'a Configuration<F, Q>) -> Configurations<F, Q, L> {
    //     // match source.kind() {
    //     //     PatternKind::Var(q) => self.configurations_for_state(q),
    //     //     _ =>
    //     // }
    //     self.labeled_configurations(source)
    // }

    // /// Return an iterator over the known labeled versions of the given configuration.
    // pub fn labeled_configurations(&self, conf: &Configuration<F, Q>) -> Configurations<F, Q, L> {
    //     match self.labeled_configurations.get(conf) {
    //         Some(confs) => confs.iter(),
    //         None => self.dummy_configurations.iter()
    //     }
    // }

    /// Return an iterator over the states connected to the given configuration.
    pub fn states_for_configuration(&self, conf: &Configuration<F, Q>) -> hash_set::Iter<Labeled<Q, L>> {
        match self.configuration_states.get(conf) {
            Some(states) => states.iter(),
            None => self.dummy_states.iter()
        }
    }

    /// Add a new transition to the automaton.
    pub fn add(&mut self, conf: Configuration<F, Q>, label: L, state: Q) {
        match self.configuration_states.get_mut(&conf) {
            Some(states) => {
                states.insert((state.clone(), label.clone()));
            },
            None => {
                let mut states = HashSet::new();
                states.insert((state.clone(), label.clone()));
                self.configuration_states.insert(conf.clone(), states);
            }
        }

        // match self.labeled_configurations.get_mut(&conf) {
        //     Some(labeled_confs) => {
        //         labeled_confs.insert((conf.clone(), label.clone()));
        //     },
        //     None => {
        //         let mut labeled_confs = HashSet::new();
        //         labeled_confs.insert((conf.clone(), label.clone()));
        //         self.labeled_configurations.insert(conf.clone(), labeled_confs);
        //     }
        // }

        match self.state_configurations.get_mut(&state) {
            Some(configurations) => {
                configurations.insert((conf, label));
            },
            None => {
                let mut configurations = HashSet::new();
                configurations.insert((conf, label));
                self.state_configurations.insert(state, configurations);
            }
        }
    }

    /// Add new transitions in the automaton by adding and normalizing the given configuration,
    /// label and state.
    pub fn add_normalized<P: PatternLike<F, Q>, N>(&mut self, pattern: &P, normalizer: &mut N) -> Q
    where N: FnMut(&Configuration<F, Q>) -> Labeled<Q, L> {
        match pattern.kind() {
            PatternLikeKind::Cons(f, l) => {
                let mut normalized_l = Vec::with_capacity(l.len());
                for sub_conf in l.iter() {
                    let q = match sub_conf.kind() {
                        PatternLikeKind::Var(q) => q.clone(),
                        _ => {
                            self.add_normalized(sub_conf, normalizer)
                        }
                    };
                    normalized_l.push(q);
                }

                let normalized_conf = Configuration(f.clone(), normalized_l);

                if let Some((state, _)) = self.states_for_configuration(&normalized_conf).next() {
                    state.clone()
                } else {
                    let (state, label) = (*normalizer)(&normalized_conf);
                    self.add(normalized_conf, label, state.clone());
                    state
                }
            },
            PatternLikeKind::Var(_) => {
                panic!("epsilon transitions are not allowed!")
            }
        }
    }

    // /// Find a run in the automaton that recognizes the given pattern.
    // pub fn find<X>(&self, pattern: Pattern<F, X>) -> Option<Configuration<F, Labeled<Q, L>>> {
    //     panic!("TODO")
    // }

    /// Automata common configurations.
    pub fn common_configurations<'a>(
        automata: &'a [&'a Automaton<F, Q, L>],
        positions: &'a [Q]
    ) -> CommonConfigurations<'a, F, Q, L> {
        CommonConfigurations::new(automata, positions)
    }

    // / Return an iterator over all the synchronized runs between the given automata,
    // / starting from the given positions, matching the given patterns and with the given
    // / constraints.
    // pub fn synchronized_runs<'a, P: pattern::Meta<F>>(
    //     automata: &'a [&'a Automaton<F, Q, L>],
    //     positions: &'a [Configuration<F, Q>],
    //     patterns: &'a [P],
    //     constraints: &'a P::Constraints
    // ) -> SynchronizedRuns<'a, F, Q, L, P>
    // where P: Clone, P::Constraints: Clone
    // {
    //     SynchronizedRuns::new(automata, positions, patterns, constraints)
    // }

    /// Return an iterator over the representative terms of the automaton.
    /// The representatives terms are all the terms recognized by the automaton *without cycle*.
    /// Together they trigger every transition of the automaton.
    pub fn representatives(&self) -> Representatives<F, Q, L> {
        Representatives::new(self)
    }

    /// Complement the automaton.
    /// This will invert the set of final and non-final states.
    /// If the automaton is complete, then `self` becomes its own complement.
    pub fn complement(&mut self) {
        let states: HashSet<Q> = self.states().cloned().collect();
        let new_final_states = states.difference(&self.final_states).cloned().collect();
        self.final_states = new_final_states;
    }

    /// Return the alphabet on which this automaton is defined.
    pub fn alphabet(&self) -> HashSet<F> {
        let mut alphabet = HashSet::new();
        for (Configuration(f, _), _, _) in self.transitions() {
            alphabet.insert(f.clone());
        }

        alphabet
    }

    pub fn map_states<R: State, M>(&self, g: M) -> Automaton<F, R, L> where M: Fn(&Q) -> R {
        let mut configuration_states: HashMap<Configuration<F, R>, HashSet<Labeled<R, L>>> = HashMap::new();
        for (conf, states) in self.configuration_states.iter() {
            let conf = conf.map(|q| g(q));
            let states: HashSet<Labeled<R, L>> = states.iter().map(|(q, l)| (g(q), l.clone())).collect();
            match configuration_states.get_mut(&conf) {
                Some(conf_states) => {
                    conf_states.extend(states.into_iter());
                },
                None => {
                    configuration_states.insert(conf, states);
                }
            }
        }

        let mut state_configurations: HashMap<R, HashSet<Labeled<Configuration<F, R>, L>>> = HashMap::new();
        for (state, confs) in self.state_configurations.iter() {
            let state = g(state);
            let confs: HashSet<Labeled<Configuration<F, R>, L>> = confs.iter().map(|(conf, l)| (conf.map(|q| g(q)), l.clone())).collect();
            match state_configurations.get_mut(&state) {
                Some(state_confs) => {
                    state_confs.extend(confs.into_iter());
                },
                None => {
                    state_configurations.insert(state, confs);
                }
            }
        }

        let mut final_states = HashSet::new();
        for q in self.final_states.iter() {
            final_states.insert(g(q));
        }

        Automaton {
            dummy_states: HashSet::new(),
            dummy_configurations: HashSet::new(),
            configuration_states: configuration_states,
            state_configurations: state_configurations,
            final_states: final_states
        }
    }
}

impl<F: Symbol, Q: State> Automaton<F, Q, NoLabel> {
    /// Complete the language with the given automaton.
    /// Each state of `self` must be mappable into a state of `lang`, and each state of `lang`
    /// must be transformed into a dead state of `self`.
    pub fn complete_with<'a, A: Iterator<Item=&'a F>, R: State>(&mut self, alphabet: A, lang: &Automaton<F, R, NoLabel>) where F: 'a + Ranked, R: From<Q>, Q: From<R> {
        let mut states: Vec<Q> = self.states().map(|q| (*q).clone()).collect();
        states.extend(lang.states().map(|r| r.clone().into()));

        for f in alphabet {
            let indexes: Vec<usize> = (0..f.arity()).collect();
            for states in combinations(&indexes, |_| states.clone().into_iter()) {
                let conf = Configuration(f.clone(), states.clone());
                match self.states_for_configuration(&conf).next() {
                    Some(_) => (),
                    None => {
                        let parent_states: Vec<R> = states.iter().map(|q| (*q).clone().into()).collect();
                        if let Some((r, _)) = lang.states_for_configuration(&Configuration(f.clone(), parent_states)).next() {
                            self.add(conf, NoLabel, (*r).clone().into());
                        }
                    }
                }
            }
        }
    }
}

// fn map<'a, F: 'a + Clone, Q: 'a + State, L: 'a + Label>(conf: &'a Labeled<Configuration<F, Q>, L>) -> Labeled<Configuration<F, Q>, L> {
//     conf.clone()
// }

pub struct CloneConfigurations<'a, F: Symbol, Q: State, L: Label> {
    it: Configurations<'a, F, Q, L>
}

impl<'a, F: Symbol, Q: State, L: Label> Iterator for CloneConfigurations<'a, F, Q, L> {
    type Item = Configuration<F, Q>;

    fn next(&mut self) -> Option<Configuration<F, Q>> {
        match self.it.next() {
            Some((conf, _)) => Some(conf.clone()),
            None => None
        }
    }
}

impl<'a, F: Symbol, Q: State, L: Label> ConfigurationIterator<'a, F, Q> for CloneConfigurations<'a, F, Q, L> {
    // ...
}

// impl<F: Symbol, Q: State, L: Label> LanguageState<F, Automaton<F, Q, L>> for Q {
//     // type Configurations<'a> = std::iter::Map<Configurations<'a, F, Q, L>, fn(&'a Labeled<Configuration<F, Q>, L>) -> Configuration<F, Q>> where L: 'a;
//
//     fn configurations<'a>(&'a self, lang: &'a Automaton<F, Q, L>) -> Box<ConfigurationIterator<'a, F, Q> + 'a> {
//         Box::new(CloneConfigurations {
//             it: lang.configurations_for_state(self)
//         })
//     }
// }
//

impl<F: Symbol + fmt::Display, Q: State + fmt::Display, L: Label + fmt::Display> fmt::Display for Automaton<F, Q, L> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (conf, label, q) in self.transitions() {
            write!(f, "{} -{}-> {}\n", conf, label, q)?;
        }
        write!(f, "final states: ")?;
        for q in self.final_states() {
            write!(f, "{} ", q)?;
        }
        Ok(())
    }
}