Awali
Another Weighted Automata library
transpose.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_CORE_RAT_TRANSPOSE_HH
18 # define AWALI_CORE_RAT_TRANSPOSE_HH
19 
21 
22 namespace awali { namespace sttc
23 {
24 
25  namespace internal
26  {
27 
28  /*--------------------.
29  | transpose(ratexp). |
30  `--------------------*/
31 
33  template <class RatExpSet>
34  class transposer
35  : public RatExpSet::const_visitor
36  {
37  public:
38  using ratexpset_t = RatExpSet;
42  using ratexp_t = typename context_t::ratexp_t;
43  using super_type = typename ratexpset_t::const_visitor;
44 
46  : rs_{rs}
47  , res_{}
48  {}
49 
50  ratexp_t
51  operator()(const ratexp_t& e)
52  {
53  e->accept(*this);
54  return std::move(res_);
55  }
56 
57  ratexp_t
58  transpose(const ratexp_t& e)
59  {
60  ratexp_t res;
61  std::swap(res_, res);
62  e->accept(*this);
63  std::swap(res_, res);
64  return res;
65  }
66 
68  {
69  res_ = rs_.zero();
70  }
71 
73  {
74  res_ = rs_.one();
75  }
76 
77  AWALI_RAT_VISIT(atom, e)
78  {
79  res_ = rs_.atom(rs_.labelset()->transpose(e.value()));
80  }
81 
83  {
84  res_ = rs_.zero();
85  for (auto v: e)
86  res_ = rs_.add(res_, transpose(v));
87  }
88 
89  AWALI_RAT_VISIT(conjunction, e)
90  {
91  res_ = transpose(e.head());
92  for (auto v: e.tail())
93  res_ = rs_.conjunction(res_, transpose(v));
94  }
95 
96  AWALI_RAT_VISIT(shuffle, e)
97  {
98  // FIXME: that should be easy to factor.
99  res_= transpose(e.head());
100  for (auto v: e.tail())
101  res_ = rs_.shuffle(res_, transpose(v));
102  }
103 
105  {
106  res_ = rs_.one();
107  for (auto v: e)
108  res_ = rs_.mul(transpose(v), res_);
109  }
110 
112  {
113  res_ = rs_.star(transpose(e.sub()));
114  }
115 
116  AWALI_RAT_VISIT(complement, e)
117  {
118  res_ = rs_.complement(transpose(e.sub()));
119  }
120 
121  AWALI_RAT_VISIT(transposition, e)
122  {
123  // Don't stack indefinitly transpositions on top of
124  // transitions. Not only is this useless, it would also break
125  // the involution as r.transpose().transpose() would not be r,
126  // but "r{T}{T}". On the other hand, if "(abc){T}".tranpose()
127  // return "abc", we also lose the involution.
128  //
129  // So rather, don't stack more that two transpositions:
130  //
131  // (abc){T}.transpose() => (abc){T}{T}
132  // (abc){T}{T}.transpose() => (abc){T}
133  //
134  // Do the same with ldiv, for the same reasons: involution.
135  //
136  // (E\F).transpose() => (E\F){T}
137  // (E\F){T}.transpose() => (E\F)
138  /*
139  if (e.sub()->kind == rat::type_t::transposition
140  || e.sub()->kind == rat::type_t::ldiv)
141  res_ = e.sub();
142  else
143  res_ = rs_.transposition(e.shared_from_this());
144  */
145  }
146 
148  {
149  // There is nothing we can do here but leaving an explicit
150  // transposition.
151  res_ = rs_.transposition(e.shared_from_this());
152  }
153 
154  AWALI_RAT_VISIT(lweight, e)
155  {
156  res_ = rs_.rmul(transpose(e.sub()),
157  rs_.weightset()->transpose(e.weight()));
158  }
159 
160  AWALI_RAT_VISIT(rweight, e)
161  {
162  res_ = rs_.lmul(rs_.weightset()->transpose(e.weight()),
163  transpose(e.sub()));
164  }
165 
166 
167  private:
168  ratexpset_t rs_;
169  ratexp_t res_;
170  };
171  }
172 
173  template <class RatExpSet>
174  typename RatExpSet::ratexp_t
175  transpose(const RatExpSet& rs, const typename RatExpSet::ratexp_t& v)
176  {
177  return rs.transpose(v);
178  }
179 
180 }}//end of ns awali::stc
181 
182 #endif // !AWALI_CORE_RAT_TRANSPOSE_HH
Definition: transpose.hh:36
weightset_t_of< context_t > weightset_t
Definition: transpose.hh:40
ratexp_t transpose(const ratexp_t &e)
Definition: transpose.hh:58
context_t_of< ratexpset_t > context_t
Definition: transpose.hh:39
AWALI_RAT_VISIT(zero,)
Definition: transpose.hh:67
typename context_t::ratexp_t ratexp_t
Definition: transpose.hh:42
AWALI_RAT_VISIT(star, e)
Definition: transpose.hh:111
AWALI_RAT_VISIT(ldiv, e)
Definition: transpose.hh:147
AWALI_RAT_VISIT(transposition, e)
Definition: transpose.hh:121
AWALI_RAT_VISIT(sum, e)
Definition: transpose.hh:82
weight_t_of< context_t > weight_t
Definition: transpose.hh:41
AWALI_RAT_VISIT(atom, e)
Definition: transpose.hh:77
AWALI_RAT_VISIT(shuffle, e)
Definition: transpose.hh:96
RatExpSet ratexpset_t
Definition: transpose.hh:38
AWALI_RAT_VISIT(one,)
Definition: transpose.hh:72
AWALI_RAT_VISIT(complement, e)
Definition: transpose.hh:116
AWALI_RAT_VISIT(rweight, e)
Definition: transpose.hh:160
typename ratexpset_t::const_visitor super_type
Definition: transpose.hh:43
AWALI_RAT_VISIT(conjunction, e)
Definition: transpose.hh:89
transposer(const ratexpset_t &rs)
Definition: transpose.hh:45
AWALI_RAT_VISIT(prod, e)
Definition: transpose.hh:104
ratexp_t operator()(const ratexp_t &e)
Definition: transpose.hh:51
AWALI_RAT_VISIT(lweight, e)
Definition: transpose.hh:154
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
AutOut transpose(Aut &aut, bool keep_history=true)
Definition: transpose.hh:79
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22