Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ratexpset.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_RATEXPSET_HH
2 # define VCSN_CORE_RAT_RATEXPSET_HH
3 
4 # include <set>
5 # include <string>
6 
8 # include <vcsn/core/rat/printer.hh>
9 # include <vcsn/core/rat/ratexp.hh>
10 # include <vcsn/ctx/context.hh>
11 # include <vcsn/labelset/labelset.hh>
13 # include <vcsn/labelset/oneset.hh>
14 # include <vcsn/misc/raise.hh>
15 # include <vcsn/misc/star_status.hh>
16 # include <vcsn/weightset/b.hh>
17 # include <vcsn/weightset/q.hh>
18 # include <vcsn/weightset/r.hh>
19 # include <vcsn/weightset/z.hh>
20 # include <vcsn/weightset/zmin.hh>
21 
22 namespace vcsn
23 {
24  namespace rat
25  {
28  template <typename Context>
29  class ratexpset_impl
30  {
31  public:
33  using context_t = Context;
37  using labelset_ptr = typename context_t::labelset_ptr;
38  using weightset_ptr = typename context_t::weightset_ptr;
40  using weight_t = typename weightset_t::value_t;
44  //
45  // See http://stackoverflow.com/questions/15537023 to know why we
46  // add the vcsn::rat:: part: GCC wants it, Clang does not care,
47  // both are right.
48 # define DEFINE(Type) \
49  using Type ## _t = vcsn::rat::Type<context_t>
66 # undef DEFINE
67 
69  using value_t = typename node_t::value_t;
71  using type_t = typename node_t::type_t;
73  using values_t = typename node_t::values_t;
74 
75  template <type_t Type>
77  template <type_t Type>
79 
80  using word_t = self_type;
82 
83  public:
85  static std::string sname();
87  std::string vname(bool full = true) const;
89  static self_type make(std::istream& is);
90 
94  ratexpset_impl(const context_t& ctx,
95  identities_t identities); // FIXME: make this optional again?
96 
100  bool open(bool o) const;
101 
103  const context_t& context() const;
104 
106  identities_t identities() const;
107  bool is_series() const;
108 
110  const labelset_ptr& labelset() const;
112  const weightset_ptr& weightset() const;
113 
115  static value_t special()
116  {
117  return atom(labelset_t::special());
118  }
119 
121  static bool is_special(value_t v)
122  {
123  return equals(special(), v);
124  }
125 
127  bool is_letter(value_t) const
128  {
129  return false;
130  }
131 
133  bool is_zero(value_t v) const ATTRIBUTE_PURE;
134 
136  static bool is_one(value_t v) ATTRIBUTE_PURE;
137 
139  static constexpr bool is_commutative()
140  {
141  return false;
142  }
143 
145  static constexpr bool is_idempotent()
146  {
147  // FIXME: well, the truth is that we are idempotent if the
148  // weightset is, _and_ we apply ACI to addition.
149  return weightset_t::is_idempotent();
150  }
151 
153  static constexpr bool show_one()
154  {
155  return false;
156  }
157 
159  static constexpr bool has_one()
160  {
161  return true;
162  }
163 
165  static constexpr bool is_ratexpset()
166  {
167  return true;
168  }
169 
171  static constexpr bool is_free()
172  {
173  return false;
174  }
175 
177  static constexpr star_status_t star_status()
178  {
180  }
181 
182  template <typename GenSet>
183  value_t conv(const letterset<GenSet>& ls,
184  typename letterset<GenSet>::value_t v) const;
185  value_t conv(b, typename b::value_t v) const;
186  value_t conv(const z& ws, typename z::value_t v) const;
187  value_t conv(const q& ws, typename q::value_t v) const;
188  value_t conv(const r& ws, typename r::value_t v) const;
189  value_t conv(const zmin& ws, typename zmin::value_t v) const;
190  template <typename Ctx2>
192  typename ratexpset_impl<Ctx2>::value_t v) const;
193 
195  static bool less_than(value_t l, value_t r);
196 
198  static bool equals(value_t l, value_t r);
199 
201  static size_t hash(const value_t& l);
202 
204  static auto atom(const label_t& v)
205  -> value_t;
206 
207  // Concrete type implementation.
208  value_t zero() const;
209  static value_t one();
210  value_t add(value_t l, value_t r) const;
211  value_t mul(value_t l, value_t r) const;
212  value_t concat(value_t l, value_t r) const;
213  value_t conjunction(value_t l, value_t r) const;
214  value_t shuffle(value_t l, value_t r) const;
215  value_t ldiv(value_t l, value_t r) const;
216  value_t rdiv(value_t l, value_t r) const;
217  value_t star(value_t e) const;
219  value_t complement(value_t e) const;
221  value_t transposition(value_t e) const;
223  value_t rmul(value_t e, const weight_t& w) const;
225  value_t lmul(const weight_t& w, value_t e) const;
227  value_t transpose(value_t e) const;
228 
230  word_t word(label_t l) const
231  {
232  return l;
233  }
234 
236  template <typename... Args>
237  value_t letter_class(Args&&... chars) const;
238 
240  value_t conv(std::istream& is) const;
241 
243  value_t conv(self_type, value_t v) const;
244 
246  std::set<value_t> convs(std::istream&) const
247  {
248  raise(vname(), ": ranges not implemented");
249  }
250 
251  std::ostream& print(const value_t v, std::ostream& o,
252  symbol format = symbol{"text"}) const;
253 
254  std::ostream&
255  print_set(std::ostream& o, symbol format = symbol{"text"}) const
256  {
257  if (format == "latex")
258  {
259  o << "\\mathsf{";
260  switch (identities())
261  {
263  o << "RatE";
264  break;
266  o << "Series";
267  break;
268  default:
269  assert(false);
270  };
271  o << "}[";
272  context().print_set(o, format);
273  o << ']';
274  }
275  else if (format == "text")
276  o << vname();
277  else
278  raise("invalid format: ", format);
279  return o;
280  }
281 
282  private:
283  void require_weightset_commutativity() const;
286  typename values_t::iterator i) const;
287  value_t insert_in_sum_series_(const sum_t& addends, value_t r) const;
288  value_t merge_sum_series_(const sum_t& addends1, value_t aa2) const;
294  value_t mul_series_(value_t l, value_t r) const;
295  value_t mul_(value_t l, value_t r, bool series) const;
296  bool is_unweighted_nonsum_(value_t v) const;
297  bool is_nonsum_(value_t v) const;
298  value_t mul_atoms_(const label_t& l, const label_t& r) const;
300  value_t mul_atoms_(const label_t& l, const label_t& r,
301  std::true_type) const;
303  value_t mul_atoms_(const label_t& l, const label_t& r,
304  std::false_type) const;
309 
313  template <type_t Type>
314  void gather_(values_t& res, value_t v) const;
315 
320  template <type_t Type>
321  values_t gather_(value_t l, value_t r) const;
322 
324  value_t concat_(value_t l, value_t r, std::true_type) const;
326  value_t concat_(value_t l, value_t r, std::false_type) const;
327 
329  template <typename LabelSet_, typename... Args>
330  value_t letter_class_(const Args&&... chars, std::true_type) const;
331 
333  template <typename LabelSet_>
334  value_t
335  letter_class_(std::set<std::pair<typename LabelSet_::letter_t,
336  typename LabelSet_::letter_t>> chars,
337  bool accept,
338  std::false_type) const;
339 
340  private:
343  };
344  } // rat::
345 
346  namespace detail
347  {
349  template <typename Ctx>
351  {
353  static type value(const type& ls)
354  {
355  return ls;
356  }
357  };
358 
360  template <typename Ctx>
361  struct law_traits<ratexpset<Ctx>>
362  {
364  static type value(const type& ls)
365  {
366  return ls;
367  }
368  };
369 
371  template <typename Ctx1, typename Ctx2>
372  struct join_impl<ratexpset<Ctx1>, ratexpset<Ctx2>>
373  {
375 
376  static type join(const ratexpset<Ctx1>& lhs, const ratexpset<Ctx2>& rhs)
377  {
378  return {vcsn::join(lhs.context(), rhs.context()),
379  vcsn::join(lhs.identities(), rhs.identities())};
380  }
381  };
382 
384  // FIXME: what about the other labelsets?
385  template <typename GenSet1, typename Ctx2>
386  struct join_impl<letterset<GenSet1>, ratexpset<Ctx2>>
387  {
391 
392  static type join(const letterset<GenSet1>& a, const ratexpset<Ctx2>& b)
393  {
394  return {context_t{vcsn::join(a, *b.labelset()), *b.weightset()},
395  b.identities()};
396  }
397  };
398 
399  // B. FIXME: screams for refactoring!
400  template <typename Context>
401  struct join_impl<b, ratexpset<Context>>
402  {
404  static type join(const b&, const ratexpset<Context>& rhs)
405  {
406  return rhs;
407  }
408  };
409 
410  // Z.
411  template <typename Context>
412  struct join_impl<z, ratexpset<Context>>
413  {
417  static type join(const z& ws, const ratexpset<Context>& rs)
418  {
419  return {context_t{*rs.labelset(), vcsn::join(ws, *rs.weightset())},
420  rs.identities()};
421  }
422  };
423 
424  // Q.
425  template <typename Context>
426  struct join_impl<q, ratexpset<Context>>
427  {
431  static type join(const q& ws, const ratexpset<Context>& rs)
432  {
433  return {context_t{*rs.labelset(), vcsn::join(ws, *rs.weightset())},
434  rs.identities()};
435  }
436  };
437 
438  // R.
439  template <typename Context>
440  struct join_impl<r, ratexpset<Context>>
441  {
445  static type join(const r& ws, const ratexpset<Context>& rs)
446  {
447  return {context_t{*rs.labelset(), vcsn::join(ws, *rs.weightset())},
448  rs.identities()};
449  }
450  };
451 
452  // Zmin.
453  template <typename Context>
454  struct join_impl<zmin, ratexpset<Context>>
455  {
459  static type join(const zmin& ws, const ratexpset<Context>& rs)
460  {
461  return {context_t{*rs.labelset(), vcsn::join(ws, *rs.weightset())},
462  rs.identities()};
463  }
464  };
465 
466  }
467 
469  template <typename Context>
472  {
473  return {ctx, identities};
474  }
475 
477  template <typename Ctx1, typename Ctx2>
478  inline
479  auto
482  {
483  return {meet(a.context(), b.context()),
484  meet(a.identities(), b.identities())};
485  }
486 
487 } // namespace vcsn
488 
489 # include <vcsn/core/rat/ratexpset.hxx>
490 
491 #endif // !VCSN_CORE_RAT_RATEXPSET_HH
492 
493 // This is ugly, yet I don't know how to address this circular
494 // dependency another way: ratexpset.hxx uses is-valid-ratexp.hh,
495 // which, of course, also uses ratexpset.hh.
496 //
497 // So let's have ratexpset.hh first accept a forward declaration (via
498 // algos/fwd.hh), then provide here the needed definition. Do not
499 // leave this inside the CPP guard.
500 
static self_type make(std::istream &is)
Build from the description in is.
Definition: ratexpset.hxx:77
std::ostream & print_set(std::ostream &o, symbol format=symbol{"text"}) const
Definition: ratexpset.hh:255
labelset_t_of< context_t > labelset_t
Definition: ratexpset.hh:34
static auto atom(const label_t &v) -> value_t
Build a label.
Definition: ratexpset.hxx:125
typename node_t::values_t values_t
A list (vector) of ratexps.
Definition: ratexpset.hh:73
An inner node with multiple children.
Definition: fwd.hh:123
value_t concat_(value_t l, value_t r, std::true_type) const
If labelset is wordset.
Definition: ratexpset.hxx:565
The abstract parameterized, root for all rational expression types.
Definition: fwd.hh:80
typename node_t::type_t type_t
Type tag for AST classes.
Definition: ratexpset.hh:71
std::string vname(bool full=true) const
Dynamic description key.
Definition: ratexpset.hxx:55
static type join(const b &, const ratexpset< Context > &rhs)
Definition: ratexpset.hh:404
ratexpset< Context > make_ratexpset(const Context &ctx, rat::identities identities)
Shorthand to ratexpset constructor.
Definition: ratexpset.hh:471
decltype(join(std::declval< ValueSets >()...)) join_t
The type of the join of the ValueSets.
Definition: join.hh:61
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
value_t complement(value_t e) const
Add a complement operator.
Definition: ratexpset.hxx:618
value_t nontrivial_mul_series_(value_t l, value_t r) const
Definition: ratexpset.hxx:433
value_t insert_in_sum_series_(const sum_t &addends, value_t r) const
Definition: ratexpset.hxx:216
typename node_t::value_t value_t
A ratexp (a shared pointer to a tree).
Definition: ratexpset.hh:69
static constexpr bool show_one()
When used as WeightSet.
Definition: ratexpset.hh:153
bool is_unweighted_nonsum_(value_t v) const
Definition: ratexpset.hxx:352
static value_t one()
Definition: ratexpset.hxx:139
#define DEFINE(Type)
Type of ratexps.
Definition: ratexpset.hh:48
The LAW from a LAL.
Definition: labelset.hh:62
std::ostream & print(const value_t v, std::ostream &o, symbol format=symbol{"text"}) const
Definition: ratexpset.hxx:833
static bool less_than(value_t l, value_t r)
Whether l < r.
Definition: ratexpset.hxx:733
vcsn::rat::sum< context_t > sum_t
Definition: ratexpset.hh:63
value_t merge_sum_series_(const sum_t &addends1, value_t aa2) const
Definition: ratexpset.hxx:251
static type join(const letterset< GenSet1 > &a, const ratexpset< Ctx2 > &b)
Definition: ratexpset.hh:392
void require_weightset_commutativity() const
Definition: ratexpset.hxx:36
weight_t possibly_implicit_lweight_(value_t e) const
Definition: ratexpset.hxx:289
Trivial identities only.
static type value(const type &ls)
Definition: ratexpset.hh:364
void gather_(values_t &res, value_t v) const
Push v in res, applying associativity if possible.
static constexpr bool is_idempotent()
When used as WeightSet.
Definition: ratexpset.hh:145
value_t mul_unweighted_nontrivial_products_(value_t a, value_t b) const
Definition: ratexpset.hxx:391
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: pair.hh:214
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
Definition: symbol.hh:24
value_t lmul(const weight_t &w, value_t e) const
Left-multiplication by a weight.
Definition: ratexpset.hxx:655
typename context_t::labelset_ptr labelset_ptr
Definition: ratexpset.hh:37
variadic_mul_mixin< detail::b_impl > b
Definition: fwd.hh:38
static type join(const r &ws, const ratexpset< Context > &rs)
Definition: ratexpset.hh:445
value_t mul_atoms_(const label_t &l, const label_t &r) const
Definition: ratexpset.hxx:373
bool open(bool o) const
Whether unknown letters should be added, or rejected.
Definition: ratexpset.hxx:94
typename weightset_t::value_t weight_t
Definition: ratexpset.hh:40
typename context_t::weightset_ptr weightset_ptr
Definition: ratexpset.hh:38
label_t_of< context_t > label_t
Definition: ratexpset.hh:39
value_t nontrivial_mul_expressions_(value_t l, value_t r) const
Definition: ratexpset.hxx:427
static constexpr star_status_t star_status()
When used as WeightSet.
Definition: ratexpset.hh:177
const weightset_ptr & weightset() const
Accessor to the weightset.
Definition: ratexpset.hxx:120
static value_t special()
When used as a LabelSet.
Definition: ratexpset.hh:115
value_t star(value_t e) const
Definition: ratexpset.hxx:602
value_t transpose(value_t e) const
The transposed of this rational expression.
Definition: ratexpset.hxx:843
ratexpset< Context > self_type
Definition: ratexpset.hh:32
static bool equals(value_t l, value_t r)
Whether l == r.
Definition: ratexpset.hxx:751
static constexpr bool has_one()
When used as WeightSet.
Definition: ratexpset.hh:159
value_t letter_class(Args &&...chars) const
A ratexp matching one character amongst chars.
Trivial identities plus series identities.
value_t unwrap_possible_lweight_(value_t e) const
Definition: ratexpset.hxx:298
static type join(const ratexpset< Ctx1 > &lhs, const ratexpset< Ctx2 > &rhs)
Definition: ratexpset.hh:376
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
ratexpset_impl(const context_t &ctx, identities_t identities)
Constructor.
Definition: ratexpset.hxx:27
std::set< value_t > convs(std::istream &) const
Read a range of ratexps.
Definition: ratexpset.hh:246
A typed ratexp set.
Definition: fwd.hh:161
static type join(const zmin &ws, const ratexpset< Context > &rs)
Definition: ratexpset.hh:459
static type join(const q &ws, const ratexpset< Context > &rs)
Definition: ratexpset.hh:431
auto meet(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< meet_t< Ctx1, Ctx2 >>
The meet of two ratexpsets.
Definition: ratexpset.hh:480
letter_t value_t
Definition: letterset.hh:33
type_t type_ignoring_lweight_(value_t e) const
Definition: ratexpset.hxx:283
value_t transposition(value_t e) const
Add a transposition operator.
Definition: ratexpset.hxx:631
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
bool is_letter(value_t) const
When used as a LabelSet.
Definition: ratexpset.hh:127
value_t mul_products_(value_t a, value_t b) const
Definition: ratexpset.hxx:408
static std::string sname()
Static description key.
Definition: ratexpset.hxx:49
rat::type_t type_t
The possible types of ratexps.
Definition: ratexp.hh:28
star_status_t
Definition: star_status.hh:6
value_t concat(value_t l, value_t r) const
Definition: ratexpset.hxx:551
A structure that implements the computation of join(V1, V2).
Definition: join.hh:19
value_t add_nonzero_series_(value_t l, value_t r) const
Definition: ratexpset.hxx:260
const context_t & context() const
Accessor to the context.
Definition: ratexpset.hxx:100
value_t shuffle(value_t l, value_t r) const
Definition: ratexpset.hxx:530
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
value_t mul_expressions_(value_t l, value_t r) const
Definition: ratexpset.hxx:316
static constexpr bool is_commutative()
When used as WeightSet.
Definition: ratexpset.hh:139
bool is_nonsum_(value_t v) const
Definition: ratexpset.hxx:367
const identities_t identities_
Definition: ratexpset.hh:342
An inner node implementing a weight.
Definition: fwd.hh:145
std::vector< value_t > values_t
Definition: ratexp.hh:91
value_t rmul(value_t e, const weight_t &w) const
Right-multiplication by a weight.
Definition: ratexpset.hxx:682
identities_t identities() const
Accessor to the identities set.
Definition: ratexpset.hxx:105
The root from which to derive the final node types.
Definition: fwd.hh:81
An inner node.
Definition: fwd.hh:83
value_t mul_series_(value_t l, value_t r) const
Definition: ratexpset.hxx:322
bool less_than_ignoring_weight_(value_t l, value_t r) const
Definition: ratexpset.hxx:191
value_t conjunction(value_t l, value_t r) const
Definition: ratexpset.hxx:464
static size_t hash(const value_t &l)
Hash l.
Definition: ratexpset.hxx:757
value_t zero() const
Definition: ratexpset.hxx:133
std::shared_ptr< const node_t > value_t
A ratexp usable with value semantics.
Definition: ratexp.hh:90
value_t remove_from_sum_series_(values_t addends, typename values_t::iterator i) const
Definition: ratexpset.hxx:197
static bool is_one(value_t v) ATTRIBUTE_PURE
When used as WeightSet.
Definition: ratexpset.hxx:727
value_t rdiv(value_t l, value_t r) const
Definition: ratexpset.hxx:523
Implementation of labels are letters.
Definition: fwd.hh:9
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
value_t letter_class_(const Args &&...chars, std::true_type) const
If labelset is oneset.
const labelset_ptr & labelset() const
Accessor to the labelset.
Definition: ratexpset.hxx:115
static type join(const z &ws, const ratexpset< Context > &rs)
Definition: ratexpset.hh:417
identities
A ratexpset can implement several different sets of identities on expressions.
Definition: identities.hh:17
static constexpr bool is_ratexpset()
When used as WeightSet.
Definition: ratexpset.hh:165
value_t mul(value_t l, value_t r) const
Definition: ratexpset.hxx:307
value_t mul_(value_t l, value_t r, bool series) const
Definition: ratexpset.hxx:328
value_t add(value_t l, value_t r) const
Definition: ratexpset.hxx:173
The smallest nullableset which includes LabelSet.
Definition: labelset.hh:22
value_t conv(const letterset< GenSet > &ls, typename letterset< GenSet >::value_t v) const
value_t ldiv(value_t l, value_t r) const
Definition: ratexpset.hxx:505
static constexpr bool is_free()
When used as WeightSet.
Definition: ratexpset.hh:171
variadic_mul_mixin< detail::r_impl > r
Definition: fwd.hh:42
word_t word(label_t l) const
Make a `word' out of a ratexp.
Definition: ratexpset.hh:230
static bool is_special(value_t v)
When used as a LabelSet.
Definition: ratexpset.hh:121
weightset_t_of< context_t > weightset_t
Definition: ratexpset.hh:35
bool is_zero(value_t v) const ATTRIBUTE_PURE
When used as WeightSet.
Definition: ratexpset.hxx:721