Vcsn  2.2
Be Rational
expand.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vcsn/ctx/fwd.hh>
5 #include <vcsn/dyn/expression.hh>
6 
7 #include <vcsn/algos/derivation.hh> // expression_polynomialset_t.
8 
9 namespace vcsn
10 {
11 
12  namespace rat
13  {
14 
15  /*----------------------.
16  | expand(expression). |
17  `----------------------*/
18 
20  template <typename ExpSet>
22  : public ExpSet::const_visitor
23  {
24  public:
25  using expressionset_t = ExpSet;
26  using expression_t = typename expressionset_t::value_t;
29  using weight_t = typename weightset_t::value_t;
30 
33 
34  using super_t = typename ExpSet::const_visitor;
35 
36  constexpr static const char* me() { return "expand"; }
37 
39  : rs_(rs)
40  {}
41 
44  {
45  v->accept(*this);
46  return ps_.to_label(res_);
47  }
48 
49  private:
52  {
53  e->accept(*this);
54  return res_;
55  }
56 
58  {
59  res_ = ps_.zero();
60  }
61 
63  {
64  res_ = polynomial_t{{rs_.one(), ws_.one()}};
65  }
66 
68  {
69  res_ = polynomial_t{{rs_.atom(v.value()), ws_.one()}};
70  }
71 
73  {
74  polynomial_t res = ps_.zero();
75  for (auto c: v)
76  res = ps_.add(res, expand(c));
77  res_ = std::move(res);
78  }
79 
81  {
82  auto res = expand(v.head());
83  for (auto c: v.tail())
84  {
85  polynomial_t sum = ps_.zero();
86  for (const auto& l: res)
87  for (const auto& r: expand(c))
88  ps_.add_here(sum,
89  rs_.conjunction(label_of(l), label_of(r)),
90  ws_.mul(weight_of(l), weight_of(r)));
91  res = sum;
92  }
93  res_ = std::move(res);
94  }
95 
101 
102  using tuple_t = typename super_t::tuple_t;
103  virtual void visit(const tuple_t&, std::true_type) override
104  {
105  raise(me(), ": tuple is not supported");
106  }
107 
109  {
110  polynomial_t res = ps_.one();
111  for (auto c: v)
112  res = ps_.mul(res, expand(c));
113  res_ = std::move(res);
114  }
115 
117  {
118  // Recurse, but make it a star.
119  v.sub()->accept(*this);
120  res_ = polynomial_t{{rs_.star(ps_.to_label(res_)), ws_.one()}};
121  }
122 
124  {
125  v.sub()->accept(*this);
126  res_ = ps_.lmul(v.weight(), std::move(res_));
127  }
128 
130  {
131  v.sub()->accept(*this);
132  res_ = ps_.rmul(std::move(res_), v.weight());
133  }
134 
135  private:
138  weightset_t ws_ = *rs_.weightset();
143  };
144 
145  } // rat::
146 
148  template <typename ExpSet>
149  inline
150  typename ExpSet::value_t
151  expand(const ExpSet& rs, const typename ExpSet::value_t& e)
152  {
154  return expand(e);
155  }
156 
157  namespace dyn
158  {
159  namespace detail
160  {
162  template <typename ExpSet>
163  inline
164  expression
165  expand(const expression& exp)
166  {
167  const auto& e = exp->as<ExpSet>();
168  return make_expression(e.expressionset(),
169  ::vcsn::expand(e.expressionset(),
170  e.expression()));
171  }
172  }
173  }
174 
175 } // vcsn::
expression expand(const expression &exp)
Bridge.
Definition: expand.hh:165
context_t_of< expressionset_t > context_t
Definition: expand.hh:27
#define VCSN_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:68
typename polynomialset_t::value_t polynomial_t
Definition: expand.hh:32
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
Definition: a-star.hh:8
static constexpr const char * me()
Definition: expand.hh:36
STL namespace.
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
An inner node with multiple children.
Definition: expression.hh:118
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
Definition: wet.hh:154
ExpSet::value_t expand(const ExpSet &rs, const typename ExpSet::value_t &e)
Expand a typed expression.
Definition: expand.hh:151
weightset_t ws_
Shorthand to the weightset.
Definition: expand.hh:138
expand_visitor(const expressionset_t &rs)
Definition: expand.hh:38
typename super_t::tuple_t tuple_t
Definition: expand.hh:102
auto rs
Definition: lift.hh:151
typename expressionset_t::value_t expression_t
Definition: expand.hh:26
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
polynomial_t expand(const expression_t &e)
Syntactic sugar: recursive call to this visitor.
Definition: expand.hh:51
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
Definition: wet.hh:146
An inner node implementing a weight.
Definition: expression.hh:264
expression_t operator()(const expression_t &v)
Definition: expand.hh:43
virtual void visit(const tuple_t &, std::true_type) override
Definition: expand.hh:103
weightset_t_of< expressionset_t > weightset_t
Definition: expand.hh:28
VCSN_RAT_VISIT(conjunction, v)
Definition: expand.hh:80
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:92
VCSN_RAT_VISIT(lweight, v)
Definition: expand.hh:123
polynomial_t res_
The result.
Definition: expand.hh:142
expression_polynomialset_t< ExpSet > make_expression_polynomialset(const ExpSet &rs)
From a ExpSet to its polynomialset.
Definition: split.hh:33
expressionset_t rs_
Definition: expand.hh:136
typename weightset_t::value_t weight_t
Definition: expand.hh:29
VCSN_RAT_VISIT(rweight, v)
Definition: expand.hh:129
polynomialset_t ps_
Polynomialset of expressions.
Definition: expand.hh:140
value_t mul(const Ts &...ts) const
A variadic multiplication.
Definition: weightset.hh:36
typename ExpSet::const_visitor super_t
Definition: expand.hh:34
expression make_expression(const ExpSet &rs, const typename ExpSet::value_t &r)
Definition: expression.hh:97