Awali
Another Weighted Automata library
noo.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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  constexpr static bool is_special(value_t)
134  {
135  return false;
136  }
137 
138  static bool
139  is_zero(const value_t v)
140  {
141  return v == 0u;
142  }
143 
144  static bool
146  {
147  return v == infinity();
148  }
149 
150  static bool
151  is_one(const value_t v)
152  {
153  return v == 1u;
154  }
155 
156  static bool
157  equals(const value_t l, const value_t r)
158  {
159  return l == r;
160  }
161 
163  static bool less_than(value_t lhs, value_t rhs)
164  {
165  return lhs < rhs;
166  }
167 
168  static constexpr bool is_commutative_semiring() { return true; }
169 
170  static constexpr bool show_one() { return false; }
171  static constexpr star_status_t star_status() { return star_status_t::STARRABLE; }
172 
173  static value_t
175  {
176  return v;
177  }
178 
179  static size_t hash(value_t v)
180  {
181  return utils::hash_value(v);
182  }
183 
184  static value_t
186  {
187  return v;
188  }
189 
190  static value_t
192  {
193  // Conversion from bool to int.
194  return v;
195  }
196 
197  value_t
198  parse(const std::string & s, size_t& p) const {
199  if(s[p-1]=='o'){
200  --p;
201  if(s[p-1]=='o')
202  --p;
203  else
204  throw parse_exception("Wrong N-oo value");
205  return infinity();
206  }
207  size_t i=p;
208  for(; i>0 && s[i-1]>='0' && s[i-1]<='9'; --i)
209  ;
210  if(i==p)
211  throw parse_exception("Wrong N value");
212  std::istringstream st(s.substr(i, p-i));
213  value_t x;
214  st >> x;
215  p=i;
216  return x;
217  }
218 
219  static value_t
220  conv(std::istream& stream)
221  {
222  if(stream.peek()=='o') {
223  stream.get();
224  if(stream.get()=='o')
225  return infinity();
226  sttc::fail_reading(stream, sname() + ": invalid value");
227  }
228  value_t res;
229  if (stream >> res)
230  return res;
231  else
232  sttc::fail_reading(stream, sname() + ": invalid value");
233  }
234 
235  static std::ostream&
236  print(const value_t v, std::ostream& o,
237  const std::string& fmt = "text")
238  {
239  if(is_infinity(v))
240  if(fmt == "json")
241  return o<< "\"oo\"";
242  else
243  return o << "oo";
244  return o << v;
245  }
246 
247  std::ostream&
248  print_set(std::ostream& o, const std::string& format = "text") const
249  {
250  if (format == "latex")
251  o << "\\mathbb{N}_\\infty";
252  else if (format == "text")
253  o << "N-oo";
254  else
255  raise("invalid format: ", format);
256  return o;
257  }
258 
259  template<unsigned version = version::fsm_json>
260  static json::node_t*
262  {
263  switch (version) {
264  case 0:
265  throw parse_exception("[zz] Unsupported fsm-json version:"
266  + std::to_string(version));
267  case 1:
268  default:
269  return new json::object_t("semiring",new json::string_t("N-oo"));
270  }
271  }
272 
273  template<unsigned version = version::fsm_json>
275  const
276  {
277  switch (version) {
278  case 0:
279  throw parse_exception("[zz] Unsupported fsm-json version:"
280  + std::to_string(version));
281  case 1:
282  default:
283  if (is_infinity(v))
284  return new json::string_t("oo");
285  else
286  return new json::int_t(v);
287  }
288  }
289 
290  template<unsigned version = version::fsm_json>
292  const
293  {
294  switch (version) {
295  case 0:
296  throw parse_exception("[zz] Unsupported fsm-json version:"
297  + std::to_string(version));
298  case 1:
299  default:
300  try {
301  int i = p->to_int();
302  if(i>= 0)
303  return i;
304  break;
305  } catch (json::coercion_exception&) {}
306  try {
307  if (p->to_string() == "oo")
308  return zero();
309  } catch (json::coercion_exception&) {}
310  }
311  std::stringstream ss;
312  ss << "[noo] At position " << p->path_to_root()
313  << " node cannot be coerced to a N-oo value.";
314  throw json::coercion_exception(ss.str());
315  }
316 
317  };
318 
319  inline noo join(const noo&, const noo&) { return {}; }
320 
321  inline noo join(const b&, const noo&) { return {}; }
322  inline noo join(const noo&, const b&) { return {}; }
323 
324 }}//end of ns awali::stc
325 
326 #endif // !AWALI_WEIGHTSET_NOO_HH
Exception used when trying to coerce a node to a given type.
Definition: exceptions.hh:83
Definition: node.hh:483
Definition: node.hh:191
path_t path_to_root() const
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:329
virtual int to_int() const
Coerces this node_t to int.
Definition: node.hh:319
Definition: node.hh:365
Definition: node.hh:526
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:236
std::string vname(bool=true) const
Definition: noo.hh:48
value_t parse(const std::string &s, size_t &p) const
Definition: noo.hh:198
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:151
static json::node_t * to_json()
Definition: noo.hh:261
static bool equals(const value_t l, const value_t r)
Definition: noo.hh:157
static value_t conv(std::istream &stream)
Definition: noo.hh:220
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:145
json::node_t * value_to_json(value_t v) const
Definition: noo.hh:274
static value_t conv(b, b::value_t v)
Definition: noo.hh:191
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: noo.hh:163
static value_t conv(self_type, value_t v)
Definition: noo.hh:185
constexpr static value_t zero()
Definition: noo.hh:63
constexpr static bool is_special(value_t)
Definition: noo.hh:133
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:139
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:179
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: noo.hh:248
static value_t transpose(const value_t v)
Definition: noo.hh:174
static noo make(std::istream &is)
Build from the description in is.
Definition: noo.hh:54
static std::string sname()
Definition: noo.hh:43
value_t value_from_json(json::node_t *p) const
Definition: noo.hh:291
static constexpr bool is_commutative_semiring()
Definition: noo.hh:168
static constexpr bool show_one()
Definition: noo.hh:170
static constexpr star_status_t star_status()
Definition: noo.hh:171
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:34
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:161
@ STARRABLE
The star of every element exists.
Definition: enums.hh:163
std::string to_string(identities i)
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:448
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