Awali
Another Weighted Automata library
star_normal_form.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_STAR_NORMAL_FORM_HH
18 # define AWALI_ALGOS_STAR_NORMAL_FORM_HH
19 
22 #include <awali/sttc/ctx/fwd.hh>
23 #include <awali/sttc/misc/raise.hh>
24 
25 namespace awali { namespace sttc {
26 
27  namespace rat
28  {
29 
30  /*---------------------------.
31  | star_normal_form(ratexp). |
32  `---------------------------*/
33 
40  template <typename RatExpSet>
42  : public RatExpSet::const_visitor
43  {
44  public:
45  using ratexpset_t = RatExpSet;
46  using ratexp_t = typename ratexpset_t::value_t;
49  using weight_t = typename weightset_t::value_t;
50 
51  using super_type = typename RatExpSet::const_visitor;
52 
53  constexpr static const char* me() { return "star_normal_form"; }
54 
56  enum operation_t { dot, box };
57 
59  : rs_(rs)
60  {}
61 
62  ratexp_t
63  operator()(const ratexp_t& v)
64  {
65  operation_ = dot;
66  v->accept(*this);
67  return std::move(res_);
68  }
69 
71  {
72  res_ = rs_.zero();
73  }
74 
76  {
77  res_ = operation_ == box ? rs_.zero() : rs_.one();
78  }
79 
81  {
82  res_ = rs_.atom(v.value());
83  }
84 
85  // Plain traversal for sums.
87  {
88  v.head()->accept(*this);
89  ratexp_t res = res_;
90  for (auto c: v.tail())
91  {
92  c->accept(*this);
93  res = rs_.add(res, res_);
94  }
95  res_ = std::move(res);
96  }
97 
103 
105  {
106  if (operation_ == box)
107  box_of(v);
108  else
109  dot_of(v);
110  }
111 
113  void box_of(const prod_t& v)
114  {
115  if (std::any_of(std::begin(v), std::end(v),
116  [this](const ratexp_t& n)
117  {
118  return ws_.is_zero(constant_term(rs_, n));
119  }))
120  {
121  // Some factor has a null constant-term.
122  operation_ = dot;
123  dot_of(v);
124  operation_ = box;
125  }
126  else
127  {
128  // All the factors have a non null constant-term.
129  v.head()->accept(*this);
130  ratexp_t res = res_;
131  for (auto c: v.tail())
132  {
133  c->accept(*this);
134  res = rs_.add(res, res_);
135  }
136  res_ = std::move(res);
137  }
138  }
139 
141  void dot_of(const prod_t& v)
142  {
143  v.head()->accept(*this);
144  ratexp_t res = res_;
145  for (auto c: v.tail())
146  {
147  c->accept(*this);
148  res = rs_.mul(res, res_);
149  }
150  res_ = std::move(res);
151  }
152 
154  {
155  if (operation_ == dot)
156  {
157  operation_ = box;
158  v.sub()->accept(*this);
159  res_ = rs_.star(res_);
160  res_ = rs_.lmul(ws_.star(constant_term(rs_, v.sub())), res_);
161  operation_ = dot;
162  }
163  else
164  {
165  v.sub()->accept(*this);
166  }
167  }
168 
170  {
171  v.sub()->accept(*this);
172  res_ = rs_.lmul(v.weight(), std::move(res_));
173  }
174 
176  {
177  v.sub()->accept(*this);
178  res_ = rs_.rmul(std::move(res_), v.weight());
179  }
180 
181  private:
182  ratexpset_t rs_;
184  weightset_t ws_ = *rs_.weightset();
186  ratexp_t res_;
188  operation_t operation_ = dot;
189  };
190 
191  }//end of ns rat
192 
194  template <typename RatExpSet>
195  typename RatExpSet::value_t
196  star_normal_form(const RatExpSet& rs, const typename RatExpSet::value_t& e)
197  {
199  return star_normal_form(e);
200  }
201 
202 }}//end of ns awali::stc
203 
204 #endif // !AWALI_ALGOS_STAR_NORMAL_FORM_HH
The semiring of complex numbers.
Definition: c.hh:44
The semiring of Natural numbers.
Definition: n.hh:34
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: star_normal_form.hh:43
AWALI_RAT_VISIT(lweight, v)
Definition: star_normal_form.hh:169
ratexp_t operator()(const ratexp_t &v)
Definition: star_normal_form.hh:63
context_t_of< ratexpset_t > context_t
Definition: star_normal_form.hh:47
AWALI_RAT_VISIT(sum, v)
Definition: star_normal_form.hh:86
typename weightset_t::value_t weight_t
Definition: star_normal_form.hh:49
AWALI_RAT_VISIT(one,)
Definition: star_normal_form.hh:75
RatExpSet ratexpset_t
Definition: star_normal_form.hh:45
void dot_of(const prod_t &v)
Handling of a product by the dot operator.
Definition: star_normal_form.hh:141
AWALI_RAT_VISIT(rweight, v)
Definition: star_normal_form.hh:175
star_normal_form_visitor(const ratexpset_t &rs)
Definition: star_normal_form.hh:58
typename ratexpset_t::value_t ratexp_t
Definition: star_normal_form.hh:46
weightset_t_of< context_t > weightset_t
Definition: star_normal_form.hh:48
AWALI_RAT_VISIT(star, v)
Definition: star_normal_form.hh:153
constexpr static const char * me()
Definition: star_normal_form.hh:53
AWALI_RAT_VISIT(atom, v)
Definition: star_normal_form.hh:80
void box_of(const prod_t &v)
Handling of a product by the box operator.
Definition: star_normal_form.hh:113
AWALI_RAT_VISIT(zero,)
Definition: star_normal_form.hh:70
typename RatExpSet::const_visitor super_type
Definition: star_normal_form.hh:51
operation_t
The type of the operator.
Definition: star_normal_form.hh:56
@ dot
Definition: star_normal_form.hh:56
@ box
Definition: star_normal_form.hh:56
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
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
weight_t_of< RatExpSet > constant_term(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e)
Definition: constant_term.hh:155
RatExpSet::value_t star_normal_form(const RatExpSet &rs, const typename RatExpSet::value_t &e)
Star_Normal_Forming a typed ratexp shared_ptr.
Definition: star_normal_form.hh:196
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
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73