Awali
Another Weighted Automata library
noo.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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_WEIGHTSET_NOO_HH
18 # define AWALI_WEIGHTSET_NOO_HH
19 
20 # include <ostream>
21 # include <string>
22 # include <sstream>
23 # include <limits>
24 
25 #include <awali/sttc/misc/raise.hh>
26 #include <awali/common/enums.hh>
31 
32 namespace awali {
33  namespace sttc {
38  class noo
39  {
40  public:
41  using self_type = noo;
42 
43  static std::string sname()
44  {
45  return "noo";
46  }
47 
48  std::string vname(bool = true) const
49  {
50  return sname();
51  }
52 
54  static noo make(std::istream& is)
55  {
56  eat(is, sname());
57  return {};
58  }
59 
60  using value_t = unsigned ;
61 
62  constexpr static value_t
63  zero()
64  {
65  return 0u;
66  }
67 
68  constexpr static value_t
70  {
72  }
73 
74  constexpr static value_t
75  one()
76  {
77  return 1u;
78  }
79 
80  static value_t
81  add(const value_t l, const value_t r)
82  {
83  if(is_infinity(l) || is_infinity(r))
84  return infinity();
85  return l + r;
86  }
87 
88  static value_t
89  sub(const value_t l, const value_t r)
90  {
91  require(l>=r, "N: sub: invalid subtraction: ", l , '-', r);
92  require(!is_infinity(r), "N: sub: invalid subtraction: ", l , '-', r);
93  if(is_infinity(l))
94  return l;
95  return l - r;
96  }
97 
98  static value_t
99  mul(const value_t l, const value_t r)
100  {
101  if(is_zero(l) || is_zero(r))
102  return zero();
103  if(is_infinity(l) || is_infinity(r))
104  return infinity();
105  return l * r;
106  }
107 
108  static value_t
109  rdiv(const value_t l, const value_t r)
110  {
111  require(!is_zero(r), "div: division by zero");
112  if(is_infinity(r))
113  return zero();
114  require(!(l % r),
115  "N: div: invalid division: ", l, '/', r);
116  return l / r;
117  }
118 
119  static value_t
120  ldiv(const value_t l, const value_t r)
121  {
122  return rdiv(r, l);
123  }
124 
125  value_t
126  star(const value_t v) const
127  {
128  if (is_zero(v))
129  return one();
130  return infinity();
131  }
132 
133  value_t
134  plus(const value_t v) const
135  {
136  if (is_zero(v))
137  return zero();
138  return infinity();
139  }
140 
141  constexpr static bool is_special(value_t)
142  {
143  return false;
144  }
145 
146  static bool
147  is_zero(const value_t v)
148  {
149  return v == 0u;
150  }
151 
152  static bool
154  {
155  return v == infinity();
156  }
157 
158  static bool
159  is_one(const value_t v)
160  {
161  return v == 1u;
162  }
163 
164  static bool
165  equals(const value_t l, const value_t r)
166  {
167  return l == r;
168  }
169 
171  static bool less_than(value_t lhs, value_t rhs)
172  {
173  return lhs < rhs;
174  }
175 
176  static constexpr bool is_commutative_semiring() { return true; }
177 
178  static constexpr bool show_one() { return false; }
179  static constexpr star_status_t star_status() { return star_status_t::STARRABLE; }
180 
181  static value_t
183  {
184  return v;
185  }
186 
187  static size_t hash(value_t v)
188  {
189  return utils::hash_value(v);
190  }
191 
192  static value_t
194  {
195  return v;
196  }
197 
198  static value_t
200  {
201  // Conversion from bool to int.
202  return v;
203  }
204 
205  value_t
206  parse(const std::string & s, size_t& p) const {
207  if(s[p-1]=='o'){
208  --p;
209  if(s[p-1]=='o')
210  --p;
211  else
212  throw parse_exception("Wrong N-oo value");
213  return infinity();
214  }
215  size_t i=p;
216  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
217  ;
218  if(i==p)
219  throw parse_exception("Wrong N value");
220  std::istringstream st(s.substr(i, p-i));
221  value_t x;
222  st >> x;
223  p=i;
224  return x;
225  }
226 
227  static value_t
228  conv(std::istream& stream)
229  {
230  if(stream.peek()=='o') {
231  stream.get();
232  if(stream.get()=='o')
233  return infinity();
234  sttc::fail_reading(stream, sname() + ": invalid value");
235  }
236  value_t res;
237  if (stream >> res)
238  return res;
239  else
240  sttc::fail_reading(stream, sname() + ": invalid value");
241  }
242 
243  static std::ostream&
244  print(const value_t v, std::ostream& o,
245  const std::string& fmt = "text")
246  {
247  if(is_infinity(v)) {
248  if(fmt == "json")
249  return o<< "\"oo\"";
250  else
251  return o << "oo";
252  }
253  return o << v;
254  }
255 
256  std::ostream&
257  print_set(std::ostream& o, const std::string& format = "text") const
258  {
259  if (format == "latex")
260  o << "\\mathbb{N}_\\infty";
261  else if (format == "text")
262  o << "N-oo";
263  else
264  raise("invalid format: ", format);
265  return o;
266  }
267 
268  template<unsigned version = version::fsm_json>
269  static json::node_t*
271  {
272  version::check_fsmjson<version>();
273  switch (version) {
274  case 0: /* Never occurs due to above check. */
275  case 1:
276  default:
277  return new json::object_t("semiring",new json::string_t("N-oo"));
278  }
279  }
280 
281  template<unsigned version = version::fsm_json>
283  const
284  {
285  version::check_fsmjson<version>();
286  switch (version) {
287  case 0: /* Never occurs due to above check. */
288  case 1:
289  default:
290  if (is_infinity(v))
291  return new json::string_t("oo");
292  else
293  return new json::int_t(v);
294  }
295  }
296 
297  template<unsigned version = version::fsm_json>
299  const
300  {
301  version::check_fsmjson<version>();
302  switch (version) {
303  case 0: /* Never occurs due to above check. */
304  case 1:
305  default:
306  try {
307  int i = p->to_int();
308  if(i>= 0)
309  return i;
310  break;
311  } catch (json::coercion_exception&) {}
312  try {
313  if (p->to_string() == "oo")
314  return zero();
315  } catch (json::coercion_exception&) {}
316  }
317  std::stringstream ss;
318  ss << "[noo] At position " << p->path_to_root()
319  << " node cannot be coerced to a N-oo value.";
320  throw json::coercion_exception(ss.str());
321  }
322 
323  };
324 
325  inline noo join(const noo&, const noo&) { return {}; }
326 
327  inline noo join(const b&, const noo&) { return {}; }
328  inline noo join(const noo&, const b&) { return {}; }
329 
330 }}//end of ns awali::stc
331 
332 #endif // !AWALI_WEIGHTSET_NOO_HH
Exception used when trying to coerce a node to a given type.
Definition: node.hh:143
Definition: node.hh:488
Definition: node.hh:193
path_t path_to_root() const
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:321
Definition: node.hh:367
Definition: node.hh:529
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of extended natural numbers.
Definition: noo.hh:39
static std::ostream & print(const value_t v, std::ostream &o, const std::string &fmt="text")
Definition: noo.hh:244
std::string vname(bool=true) const
Definition: noo.hh:48
value_t parse(const std::string &s, size_t &p) const
Definition: noo.hh:206
static value_t mul(const value_t l, const value_t r)
Definition: noo.hh:99
static bool is_one(const value_t v)
Definition: noo.hh:159
static json::node_t * to_json()
Definition: noo.hh:270
static bool equals(const value_t l, const value_t r)
Definition: noo.hh:165
static value_t conv(std::istream &stream)
Definition: noo.hh:228
value_t star(const value_t v) const
Definition: noo.hh:126
static value_t sub(const value_t l, const value_t r)
Definition: noo.hh:89
constexpr static value_t one()
Definition: noo.hh:75
constexpr static value_t infinity()
Definition: noo.hh:69
static bool is_infinity(const value_t v)
Definition: noo.hh:153
json::node_t * value_to_json(value_t v) const
Definition: noo.hh:282
static value_t conv(b, b::value_t v)
Definition: noo.hh:199
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: noo.hh:171
static value_t conv(self_type, value_t v)
Definition: noo.hh:193
constexpr static value_t zero()
Definition: noo.hh:63
constexpr static bool is_special(value_t)
Definition: noo.hh:141
static value_t ldiv(const value_t l, const value_t r)
Definition: noo.hh:120
static bool is_zero(const value_t v)
Definition: noo.hh:147
value_t value_from_json(json::node_t const *p) const
Definition: noo.hh:298
static value_t add(const value_t l, const value_t r)
Definition: noo.hh:81
static size_t hash(value_t v)
Definition: noo.hh:187
value_t plus(const value_t v) const
Definition: noo.hh:134
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: noo.hh:257
static value_t transpose(const value_t v)
Definition: noo.hh:182
static noo make(std::istream &is)
Build from the description in is.
Definition: noo.hh:54
static std::string sname()
Definition: noo.hh:43
static constexpr bool is_commutative_semiring()
Definition: noo.hh:176
static constexpr bool show_one()
Definition: noo.hh:178
static constexpr star_status_t star_status()
Definition: noo.hh:179
static value_t rdiv(const value_t l, const value_t r)
Definition: noo.hh:109
unsigned value_t
Definition: noo.hh:60
The semiring of floating Numbers.
Definition: r.hh:35
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ STARRABLE
The star of every element exists.
Definition: enums.hh:165
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, std::string explanation)
Throw an exception after failing to read from is.
Definition: stream.hh:93
auto join(const ratexpset< Ctx1 > &a, const ratexpset< Ctx2 > &b) -> ratexpset< join_t< Ctx1, Ctx2 >>
The union of two ratexpsets.
Definition: ratexpset.hh:449
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
void require(bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:55
std::size_t hash_value(const T &v)
Definition: hash.hh:76
ATTRIBUTE_CONST int max(int a, int b)
Definition: arith.hh:54
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26