Vcsn  2.4
Be Rational
project.hh
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace vcsn
6 {
7  namespace rat
8  {
14  template <typename InExpSet, typename OutExpSet, size_t Tape>
16  : public InExpSet::const_visitor
17  {
18  public:
19  using in_expressionset_t = InExpSet;
20  using out_expressionset_t = OutExpSet;
21  using super_t = typename in_expressionset_t::const_visitor;
23 
25  using in_expression_t = typename in_expressionset_t::value_t;
26  using out_expression_t = typename out_expressionset_t::value_t;
27  template <type_t Type>
28  using unary_t = typename super_t::template unary_t<Type>;
29  template <type_t Type>
30  using variadic_t = typename super_t::template variadic_t<Type>;
31 
33  const out_expressionset_t& out_rs)
34  : in_rs_{in_rs}
35  , out_rs_{out_rs}
36  {}
37 
39  : project_impl{in_rs,
40  in_rs.template project<Tape>()}
41  {}
42 
46  {
47  return rec_(v);
48  }
49 
50  private:
53  {
54  v->accept(*this);
55  return res_;
56  }
57 
59  template <exp::type_t Type, typename Fun>
60  void
61  rec_(const unary_t<Type>& v, Fun&& fun)
62  {
63  // FIXME: C++17: invoke.
64  res_ = (out_rs_.*fun)(rec_(v.sub()));
65  }
66 
68  template <exp::type_t Type, typename Fun>
69  void
70  rec_(const variadic_t<Type>& v, Fun&& fun)
71  {
72  // Be sure to apply the identities: merely projecting on tape
73  // 0 in `a|b+a|c` would result in `a+a`.
74  auto res = rec_(v.head());
75  for (const auto& c: v.tail())
76  // FIXME: C++17: invoke.
77  res = (out_rs_.*fun)(res, rec_(c));
78  res_ = std::move(res);
79  }
80 
88  VCSN_RAT_VISIT(one,) { res_ = out_rs_.one(); }
89  using bin_t =
91  (ors_t::*)(const out_expression_t&, const out_expression_t&) const;
93  static_cast<bin_t>(&ors_t::mul)); }
97  VCSN_RAT_VISIT(zero,) { res_ = out_rs_.zero(); }
98 
100  {
101  res_ = out_rs_.atom(std::get<Tape>(v.value()));
102  }
103 
105  {
106  res_ = out_rs_.lweight(v.weight(), rec_(v.sub()));
107  }
108 
110  {
111  res_ = out_rs_.rweight(rec_(v.sub()), v.weight());
112  }
113 
114  /*---------.
115  | tuple. |
116  `---------*/
117 
118  using tuple_t = typename super_t::tuple_t;
119 
120  template <typename = void>
121  struct visit_tuple
122  {
125  {
126  return std::get<Tape>(v.sub());
127  }
129  };
130 
131  void visit(const tuple_t& v, std::true_type) override
132  {
133  detail::static_if<in_context_t::is_lat>
134  ([this](auto&& v){ res_ = visit_tuple<decltype(v)>{*this}(v); })
135  (v);
136  }
137 
144  };
145 
146 
152  template <size_t Tape, typename InExpSet, typename OutExpSet>
153  auto
154  project(const InExpSet& in_rs, const OutExpSet& out_rs,
155  const typename InExpSet::value_t& v)
156  -> typename OutExpSet::value_t
157  {
158  auto p = project_impl<InExpSet, OutExpSet, Tape>{in_rs, out_rs};
159  return p(v);
160  }
161 
166  template <size_t Tape, typename InExpSet>
167  auto
168  project(const InExpSet& in_rs, const typename InExpSet::value_t& v)
169  -> typename InExpSet::template project_t<Tape>::value_t
170  {
171  return project<Tape>(in_rs, in_rs.template project<Tape>(), v);
172  }
173 } // namespace rat
174 } // namespace vcsn
VCSN_RAT_VISIT(rweight, v)
Definition: project.hh:109
out_expression_t rec_(const in_expression_t &v)
Easy recursion.
Definition: project.hh:52
VCSN_RAT_VISIT(transposition, v)
Definition: project.hh:96
void visit(const tuple_t &v, std::true_type) override
Definition: project.hh:131
typename super_t::template unary_t< Type > unary_t
Definition: project.hh:28
return res
Definition: multiply.hh:398
An inner node implementing a weight.
Definition: expression.hh:255
typename in_expressionset_t::const_visitor super_t
Definition: project.hh:21
auto project(const InExpSet &in_rs, const OutExpSet &out_rs, const typename InExpSet::value_t &v) -> typename OutExpSet::value_t
Project a rational expression.
Definition: project.hh:154
out_expression_t res_
Output value, under construction.
Definition: project.hh:143
context_t_of< in_expressionset_t > in_context_t
Definition: project.hh:24
VCSN_RAT_VISIT(ldivide, v)
Definition: project.hh:87
void rec_(const variadic_t< Type > &v, Fun &&fun)
Factor the handling of n-ary operations.
Definition: project.hh:70
typename out_expressionset_t::value_t out_expression_t
Definition: project.hh:26
typename super_t::tuple_t tuple_t
Definition: project.hh:118
const in_expressionset_t & in_rs_
expressionset to decode the input value.
Definition: project.hh:139
typename super_t::template variadic_t< Type > variadic_t
Definition: project.hh:30
out_expressionset_t ors_t
Definition: project.hh:81
out_expression_t operator()(const tuple_t &v)
Entry point.
Definition: project.hh:124
Definition: a-star.hh:8
InExpSet in_expressionset_t
Definition: project.hh:19
VCSN_RAT_VISIT(conjunction, v)
Definition: project.hh:85
OutExpSet out_expressionset_t
Definition: project.hh:20
VCSN_RAT_VISIT(lweight, v)
Definition: project.hh:104
out_expression_t(ors_t::*)(const out_expression_t &, const out_expression_t &) const bin_t
Definition: project.hh:91
Functor to project a rational expression.
Definition: project.hh:15
VCSN_RAT_VISIT(atom, v)
Definition: project.hh:99
VCSN_RAT_VISIT(shuffle, v)
Definition: project.hh:94
typename in_expressionset_t::value_t in_expression_t
Definition: project.hh:25
An inner node with multiple children.
Definition: expression.hh:118
return v
Definition: multiply.hh:361
VCSN_RAT_VISIT(infiltrate, v)
Definition: project.hh:86
project_impl(const in_expressionset_t &in_rs, const out_expressionset_t &out_rs)
Definition: project.hh:32
VCSN_RAT_VISIT(compose, v)
Definition: project.hh:84
out_expression_t operator()(const in_expression_t &v)
Entry point: print v.
Definition: project.hh:45
project_impl(const in_expressionset_t &in_rs)
Definition: project.hh:38
out_expressionset_t out_rs_
expressionset to build the output value.
Definition: project.hh:141
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
void rec_(const unary_t< Type > &v, Fun &&fun)
Factor the handling of unary operations.
Definition: project.hh:61
VCSN_RAT_VISIT(star, v)
Definition: project.hh:95
VCSN_RAT_VISIT(complement, v)
Definition: project.hh:83