Awali
Another Weighted Automata library
are_equivalent.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_ARE_EQUIVALENT_HH
18 # define AWALI_ALGOS_ARE_EQUIVALENT_HH
19 
20 #include <awali/common/priority.hh>
21 
22 #include <awali/sttc/algos/accessible.hh> // is_useless
29 #include <awali/sttc/algos/sum.hh>
31 
32 namespace awali {
33  namespace sttc {
34 
35  /*,---------------------------------------.
36  | are_equivalent(automaton, automaton). |
37  `---------------------------------------'*/
38 
39  namespace internal {
40  //Definitions of internal functions `are_equivalent_`
41 
43  template <typename Aut1, typename Aut2, typename P>
44  auto
45  are_equivalent_(const Aut1& aut1, const Aut2& aut2, priority::FIVE<P>)
46  -> typename std::enable_if<(labelset_t_of<Aut1>::is_free()
47  && std::is_same<weightset_t_of<Aut1>, b>::value
49  && std::is_same<weightset_t_of<Aut2>, b>::value),
50  bool>::type
51  {
52  return ( is_useless(difference(aut1, aut2))
53  && is_useless(difference(aut2, aut1)));
54  }
55 
56 
58  template <typename Aut1, typename Aut2, typename T>
59  auto
60  are_equivalent_(const Aut1& aut1, const Aut2& aut2, priority::FOUR<T>)
61  -> decltype(aut2->weightset()->sub,
62  aut2->weightset()->zero,
63  aut2->weightset()->one,
64  bool())
65  {
66  const auto& ws2 = *aut2->weightset();
67  // d = aut1 U -aut2.
68  auto d = sum(aut1, left_mult(aut2, ws2.sub(ws2.zero(), ws2.one())));
69  return is_empty(reduce(d));
70  }
71 
72 
73  template <typename Aut1, typename Aut2, typename P>
74  bool
75  are_equivalent_(const Aut1& aut1, const Aut2& aut2, priority::ONE<P>)
76  {
77  raise("Function are_equivalent is only supported for weightsets with subtraction and for boolean automata over a free label-sets with no epsilon-transitions allowed.");
78  }
79 
80  } // end of namespace awali::sttc::internal
81 
92  template <typename Aut0, typename Aut2>
93  bool are_equivalent(const Aut0& aut1, const Aut2& aut2)
94  {
96  }
97 
98 
99  /*,-----------------------------------.
100  | difference(automaton, automaton). |
101  `-----------------------------------'*/
102 
111  template <typename Lhs, typename Rhs>
112  typename Lhs::element_type::automaton_nocv_t
113  difference(const Lhs& aut1, const Rhs& aut2)
114  {
115  // Meet complement()'s requirements.
116  auto a = aut2;
117  if (!is_deterministic(a))
118  a = complete(determinize(a,false));
119  else if (!is_complete(a))
120  a = complete(a);
121  return product(aut1, complement(a));
122  }
123 
124 
125  }
126 }//end of ns awali::stc
127 
128 #endif // !AWALI_ALGOS_ARE_EQUIVALENT_HH
The Boolean semring.
Definition: b.hh:38
static constexpr TOP< void > value
Definition: priority.hh:93
Definition: priority.hh:52
auto are_equivalent_(const Aut1 &aut1, const Aut2 &aut2, priority::FIVE< P >) -> typename std::enable_if<(labelset_t_of< Aut1 >::is_free() &&std::is_same< weightset_t_of< Aut1 >, b >::value &&labelset_t_of< Aut2 >::is_free() &&std::is_same< weightset_t_of< Aut2 >, b >::value), bool >::type
Check equivalence between Boolean automata on a free labelset.
Definition: are_equivalent.hh:45
bool is_empty(const Aut &aut) ATTRIBUTE_PURE
Test whether the automaton has no state.
Definition: accessible.hh:365
auto complement(const Aut &aut, bool keep_history=true) -> decltype(copy(aut))
Complementation of a deterministic complete automaton.
Definition: complement.hh:79
auto complete(const Aut &aut, bool keep_history=true) -> decltype(copy(aut, keep_history))
Completion of a deterministic automaton.
Definition: complete.hh:90
bool are_equivalent(const Aut0 &aut1, const Aut2 &aut2)
Tests if aut1 and aut2 are equivalent.
Definition: are_equivalent.hh:93
auto product(const Lhs &lhs, const Rhs &rhs, bool keep_history=true) -> decltype(join_automata(lhs, rhs))
Definition: product.hh:394
bool is_useless(const Aut &aut)
Test whether the automaton has useful states.
Definition: accessible.hh:321
bool is_deterministic(const Aut &aut)
tests whether the automaton is deterministic
Definition: determinize.hh:210
bool is_complete(const Aut &aut)
Whether aut is complete.
Definition: is_complete.hh:28
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
auto determinize(const Aut &a, bool keep_history=true) -> mutable_automaton< context_t_of< Aut >>
Determinization of the automaton.
Definition: determinize.hh:41
auto to_lal(const Aut &aut, direction_t dir=BACKWARD, bool prune=true, bool keep_history=true) -> typename internal::dispatch_lal_lan< Aut, labelset_t_of< Aut >>::l_automaton_t
Definition: lal_lan_conversion.hh:87
Aut reduce(const Aut &input)
Definition: reduce.hh:619
Lhs::element_type::automaton_nocv_t difference(const Lhs &aut1, const Rhs &aut2)
Computes an automaton that accepts every word accepted by aut1 which is not accepted by aut2.
Definition: are_equivalent.hh:113
Aut::element_type::automaton_nocv_t left_mult(const Aut &aut, const weight_t_of< Aut > &w, bool standard=false)
Definition: left_mult.hh:125
Main namespace of Awali.
Definition: ato.hh:22