Awali
Another Weighted Automata library
transpose.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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(maybe, e)
117  {
118  res_ = rs_.star(transpose(e.sub()));
119  }
120 
122  {
123  res_ = rs_.star(transpose(e.sub()));
124  }
125 
126  AWALI_RAT_VISIT(complement, e)
127  {
128  res_ = rs_.complement(transpose(e.sub()));
129  }
130 
131  AWALI_RAT_VISIT(transposition, e)
132  {
133  // Don't stack indefinitly transpositions on top of
134  // transitions. Not only is this useless, it would also break
135  // the involution as r.transpose().transpose() would not be r,
136  // but "r{T}{T}". On the other hand, if "(abc){T}".tranpose()
137  // return "abc", we also lose the involution.
138  //
139  // So rather, don't stack more that two transpositions:
140  //
141  // (abc){T}.transpose() => (abc){T}{T}
142  // (abc){T}{T}.transpose() => (abc){T}
143  //
144  // Do the same with ldiv, for the same reasons: involution.
145  //
146  // (E\F).transpose() => (E\F){T}
147  // (E\F){T}.transpose() => (E\F)
148  /*
149  if (e.sub()->kind == rat::type_t::transposition
150  || e.sub()->kind == rat::type_t::ldiv)
151  res_ = e.sub();
152  else
153  res_ = rs_.transposition(e.shared_from_this());
154  */
155  }
156 
158  {
159  // There is nothing we can do here but leaving an explicit
160  // transposition.
161  res_ = rs_.transposition(e.shared_from_this());
162  }
163 
164  AWALI_RAT_VISIT(lweight, e)
165  {
166  res_ = rs_.rmul(transpose(e.sub()),
167  rs_.weightset()->transpose(e.weight()));
168  }
169 
170  AWALI_RAT_VISIT(rweight, e)
171  {
172  res_ = rs_.lmul(rs_.weightset()->transpose(e.weight()),
173  transpose(e.sub()));
174  }
175 
176 
177  private:
178  ratexpset_t rs_;
179  ratexp_t res_;
180  };
181  }
182 
183  template <class RatExpSet>
184  typename RatExpSet::ratexp_t
185  transpose(const RatExpSet& rs, const typename RatExpSet::ratexp_t& v)
186  {
187  return rs.transpose(v);
188  }
189 
190 }}//end of ns awali::stc
191 
192 #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:157
AWALI_RAT_VISIT(transposition, e)
Definition: transpose.hh:131
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(maybe, e)
Definition: transpose.hh:116
AWALI_RAT_VISIT(complement, e)
Definition: transpose.hh:126
AWALI_RAT_VISIT(rweight, e)
Definition: transpose.hh:170
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
AWALI_RAT_VISIT(plus, e)
Definition: transpose.hh:121
ratexp_t operator()(const ratexp_t &e)
Definition: transpose.hh:51
AWALI_RAT_VISIT(lweight, e)
Definition: transpose.hh:164
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