Awali
Another Weighted Automata library
b.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_B_HH
18 # define AWALI_WEIGHTSET_B_HH
19 
20 # include <cassert>
21 # include <ostream>
22 # include <string>
23 
25 #include <awali/utils/hash.hh>
26 #include <awali/common/parse_exception.cc>
27 #include <awali/sttc/misc/raise.hh>
29 #include <awali/common/enums.hh>
32 #include <awali/common/json/node.cc>
33 #include <awali/common/version.hh>
34 
35 namespace awali {
36 namespace sttc {
38  class b {
39  public:
40  using self_type = b;
41 
42  static std::string sname() {
43  return "b";
44  }
45 
46  static std::string vname(bool = true) {
47  return sname();
48  }
49 
51  static b make(std::istream& is) {
52  eat(is, sname());
53  return {};
54  }
55 
56  using value_t = bool;
57  using finite_t = bool;
58  static const finite_t finite=true;
59 
60  static value_t zero() {
61  return false;
62  }
63 
64  static value_t one() {
65  return true;
66  }
67 
68  static value_t add(value_t l, value_t r) {
69  return l || r;
70  }
71 
72  static value_t mul(value_t l, value_t r) {
73  return l && r;
74  }
75 
76  static value_t rdiv(value_t l, value_t r) {
77  require(!is_zero(r), "div: division by zero");
78  return l;
79  }
80 
81  static value_t ldiv(value_t l, value_t r) {
82  return rdiv(r, l);
83  }
84 
85  static value_t star(value_t) {
86  return one();
87  }
88 
89  static bool equals(value_t l, value_t r)
90  {
91  return l == r;
92  }
93 
95  static bool less_than(value_t lhs, value_t rhs)
96  {
97  return lhs < rhs;
98  }
99 
100  constexpr static bool is_special(value_t) {
101  return false;
102  }
103 
104  static bool is_zero(value_t v) {
105  return !v;
106  }
107 
108  static bool is_one(value_t v) {
109  return v;
110  }
111 
112  constexpr static bool is_commutative_semiring() { return true; }
113 
114  constexpr static bool show_one() { return false; }
115 
116  constexpr static star_status_t star_status() {
118  }
119 
121  return v;
122  }
123 
124  static size_t hash(value_t v) {
125  return utils::hash_value(v);
126  }
127 
129  return v;
130  }
131 
132  static value_t conv(std::istream& is) {
133  int i;
134  if (is >> i)
135  {
136  if (i == 0 || i == 1)
137  return i;
138  else
139  fail_reading(is,
140  sname() + ": invalid value: " + std::to_string(i));
141  }
142  else
143  fail_reading(is, sname() + ": invalid value");
144  }
145 
146  static value_t parse(const std::string & s, size_t& p) {
147  if ((p>=4)&&(!s.compare(p-4,4,"true"))) {
148  p -= 4;
149  return true;
150  }
151  if ((p>=5)&&(!s.compare(p-5,5,"false"))) {
152  p -= 5;
153  return false;
154  }
155  if(s[--p] == '0')
156  return false;
157  if(s[p] == '1')
158  return true;
159  throw parse_exception("Wrong Boolean value");
160  }
161 
162  static std::ostream& print(value_t v, std::ostream& o,
163  const std::string& format = "text") {
164  if (format == "latex")
165  o << (v ? "\\top" : "\\bot");
166  else
167  o << (v? "true" : "false");
168  return o;
169  }
170 
171  static std::ostream& print_set(std::ostream& o, const std::string& format = "text") {
172  if (format == "latex")
173  o << "\\mathbb{B}";
174  else if (format == "text")
175  o << "B";
176  else
177  raise("invalid format: ", format);
178  return o;
179  }
180 
181 
182  template<unsigned version = version::fsm_json>
183  static
185  {
186  switch (version) {
187  case 0:
188  throw parse_exception("[B] Unsupported fsm-json version:"
189  + std::to_string(version));
190  case 1:
191  default:
192  return new json::object_t("semiring", new json::string_t("B"));
193  }
194  }
195 
196 
197  template<unsigned version = version::fsm_json>
198  static
200  {
201  switch (version) {
202  case 0:
203  throw parse_exception("[B] Unsupported fsm-json version:"
204  + std::to_string(version));
205  case 1:
206  default:
207  return (new json::bool_t(v));
208  }
209  }
210 
211  template<unsigned version = version::fsm_json>
213  switch (version) {
214  case 0:
215  throw parse_exception("[B] Unsupported fsm-json version:"
216  + std::to_string(version));
217  case 1:
218  default:
219  return p->to_bool();
220  }
221  }
222  };
223 
224  inline b join(const b&, const b&) { return {}; }
225 
226 }}//end of namespaces awali::sttc, and awali
227 
228 
229 #endif // !AWALI_WEIGHTSET_B_HH
Definition: node.hh:555
Definition: node.hh:191
virtual bool to_bool() const
Coerces this node_t to bool.
Definition: node.hh:304
Definition: node.hh:365
Definition: node.hh:526
The Boolean semring.
Definition: b.hh:38
static size_t hash(value_t v)
Definition: b.hh:124
static json::node_t * to_json()
Definition: b.hh:184
static value_t conv(std::istream &is)
Definition: b.hh:132
static value_t star(value_t)
Definition: b.hh:85
static value_t add(value_t l, value_t r)
Definition: b.hh:68
static const finite_t finite
Definition: b.hh:58
static bool is_one(value_t v)
Definition: b.hh:108
constexpr static bool is_special(value_t)
Definition: b.hh:100
static value_t one()
Definition: b.hh:64
static value_t conv(self_type, value_t v)
Definition: b.hh:128
static std::ostream & print(value_t v, std::ostream &o, const std::string &format="text")
Definition: b.hh:162
static bool equals(value_t l, value_t r)
Definition: b.hh:89
static value_t zero()
Definition: b.hh:60
static value_t rdiv(value_t l, value_t r)
Definition: b.hh:76
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: b.hh:95
static value_t parse(const std::string &s, size_t &p)
Definition: b.hh:146
constexpr static bool show_one()
Definition: b.hh:114
static value_t ldiv(value_t l, value_t r)
Definition: b.hh:81
static json::node_t * value_to_json(value_t v)
Definition: b.hh:199
constexpr static bool is_commutative_semiring()
Definition: b.hh:112
static bool is_zero(value_t v)
Definition: b.hh:104
static std::string vname(bool=true)
Definition: b.hh:46
bool finite_t
Definition: b.hh:57
static b make(std::istream &is)
Build from the description in is.
Definition: b.hh:51
bool value_t
Definition: b.hh:56
static value_t value_from_json(json::node_t const *p)
Definition: b.hh:212
static std::ostream & print_set(std::ostream &o, const std::string &format="text")
Definition: b.hh:171
static std::string sname()
Definition: b.hh:42
static value_t mul(value_t l, value_t r)
Definition: b.hh:72
static value_t transpose(value_t v)
Definition: b.hh:120
constexpr static star_status_t star_status()
Definition: b.hh:116
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: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
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26