Vcsn  2.1
Be Rational
b.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/misc/format.hh>
8 #include <vcsn/misc/escape.hh>
9 #include <vcsn/misc/functional.hh> // hash_value
10 #include <vcsn/misc/raise.hh>
11 #include <vcsn/misc/star-status.hh>
12 #include <vcsn/misc/stream.hh>
13 #include <vcsn/misc/symbol.hh>
14 #include <vcsn/weightset/fwd.hh>
16 
17 namespace vcsn
18 {
19  namespace detail
20  {
21  class b_impl
22  {
23  public:
24  using self_t = b;
25 
26  static symbol sname()
27  {
28  static symbol res("b");
29  return res;
30  }
31 
33  static b make(std::istream& is)
34  {
35  eat(is, sname());
36  return {};
37  }
38 
39  using value_t = bool;
40 
41  static value_t
42  zero()
43  {
44  return false;
45  }
46 
47  static value_t
48  one()
49  {
50  return true;
51  }
52 
53  static value_t
54  add(const value_t l, const value_t r)
55  {
56  return l || r;
57  }
58 
59  // This is highly debatable. Was introduced to allow the division
60  // of polynomials. It should rather be handled there.
61  static value_t
62  sub(const value_t l, const value_t r)
63  {
64  return l && !r;
65  }
66 
67  static value_t
68  mul(const value_t l, const value_t r)
69  {
70  return l && r;
71  }
72 
73  static value_t
74  lgcd(const value_t l, const value_t r)
75  {
76  require(!is_zero(l), sname(), ": lgcd: invalid lhs: zero");
77  require(!is_zero(r), sname(), ": lgcd: invalid rhs: zero");
78  return one();
79  }
80 
81  static value_t
82  rgcd(const value_t l, const value_t r)
83  {
84  return lgcd(l, r);
85  }
86 
87  static value_t
88  rdiv(const value_t l, const value_t r)
89  {
90  require(!is_zero(r), "div: division by zero");
91  return l;
92  }
93 
94  static value_t
95  ldiv(const value_t l, const value_t r)
96  {
97  return rdiv(r, l);
98  }
99 
100  static value_t
101  star(const value_t)
102  {
103  return one();
104  }
105 
106  static bool
107  equal(const value_t l, const value_t r)
108  {
109  return l == r;
110  }
111 
113  static bool less(const value_t lhs, const value_t rhs)
114  {
115  return lhs < rhs;
116  }
117 
118  constexpr static bool is_special(const value_t)
119  {
120  return false;
121  }
122 
123  static bool
125  {
126  return !v;
127  }
128 
129  static bool
130  is_one(const value_t v)
131  {
132  return v;
133  }
134 
135  static constexpr bool is_commutative() { return true; }
136  static constexpr bool is_idempotent() { return true; }
137 
138  static constexpr bool show_one() { return false; }
139  static constexpr star_status_t star_status()
140  {
142  }
143 
144  static value_t
146  {
147  return v;
148  }
149 
150  static size_t hash(const value_t v)
151  {
152  return hash_value(v);
153  }
154 
155  static value_t
157  {
158  return v;
159  }
160 
161  static value_t
162  conv(std::istream& is, bool = true)
163  {
164  int i;
165  if (is >> i)
166  {
167  require(i == 0 || i == 1,
168  sname(), ": invalid value: ", i);
169  return i;
170  }
171  else
172  raise(sname(), ": invalid value: ", is);
173  }
174 
175  static std::ostream&
176  print(const value_t v, std::ostream& o,
177  format fmt = {})
178  {
179  if (fmt == format::latex)
180  o << (v ? "\\top" : "\\bot");
181  else
182  o << (v ? '1' : '0');
183  return o;
184  }
185 
186  std::ostream&
187  print_set(std::ostream& o, format fmt = {}) const
188  {
189  if (fmt == format::latex)
190  o << "\\mathbb{B}";
191  else if (fmt == format::text)
192  o << sname();
193  else
194  raise("invalid format: ", fmt);
195  return o;
196  }
197  };
198 
199  /*-------.
200  | join. |
201  `-------*/
202 
203  VCSN_JOIN_SIMPLE(b, b);
204  } // detail::
205 
206 }
static value_t one()
Definition: b.hh:48
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:33
bool value_t
Definition: b.hh:39
static value_t conv(self_t, const value_t v)
Definition: b.hh:156
static symbol sname()
Definition: b.hh:26
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static value_t mul(const value_t l, const value_t r)
Definition: b.hh:68
static value_t sub(const value_t l, const value_t r)
Definition: b.hh:62
static bool equal(const value_t l, const value_t r)
Definition: b.hh:107
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
static value_t star(const value_t)
Definition: b.hh:101
static bool less(const value_t lhs, const value_t rhs)
Whether lhs < rhs.
Definition: b.hh:113
weightset_mixin< detail::b_impl > b
Definition: fwd.hh:48
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
static value_t conv(std::istream &is, bool=true)
Definition: b.hh:162
static size_t hash(const value_t v)
Definition: b.hh:150
static value_t transpose(const value_t v)
Definition: b.hh:145
static constexpr bool is_idempotent()
Definition: b.hh:136
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:63
static constexpr bool is_special(const value_t)
Definition: b.hh:118
static bool is_one(const value_t v)
Definition: b.hh:130
static value_t zero()
Definition: b.hh:42
An input/output format.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
always valid.
Definition: star-status.hh:17
static bool is_zero(const value_t v)
Definition: b.hh:124
static constexpr bool is_commutative()
Definition: b.hh:135
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:75
static std::ostream & print(const value_t v, std::ostream &o, format fmt={})
Definition: b.hh:176
static value_t lgcd(const value_t l, const value_t r)
Definition: b.hh:74
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
static value_t rgcd(const value_t l, const value_t r)
Definition: b.hh:82
static value_t add(const value_t l, const value_t r)
Definition: b.hh:54
static value_t rdiv(const value_t l, const value_t r)
Definition: b.hh:88
static value_t ldiv(const value_t l, const value_t r)
Definition: b.hh:95
star_status_t
Definition: star-status.hh:5
VCSN_JOIN_SIMPLE(b, b)
static constexpr bool show_one()
Definition: b.hh:138
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: b.hh:187
static constexpr star_status_t star_status()
Definition: b.hh:139