Awali
Another Weighted Automata library
b.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_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 value_t plus(value_t v) {
90  return v;
91  }
92 
93  static bool equals(value_t l, value_t r)
94  {
95  return l == r;
96  }
97 
99  static bool less_than(value_t lhs, value_t rhs)
100  {
101  return lhs < rhs;
102  }
103 
104  constexpr static bool is_special(value_t) {
105  return false;
106  }
107 
108  static bool is_zero(value_t v) {
109  return !v;
110  }
111 
112  static bool is_one(value_t v) {
113  return v;
114  }
115 
116  constexpr static bool is_commutative_semiring() { return true; }
117 
118  constexpr static bool show_one() { return false; }
119 
120  constexpr static star_status_t star_status() {
122  }
123 
125  return v;
126  }
127 
128  static size_t hash(value_t v) {
129  return utils::hash_value(v);
130  }
131 
133  return v;
134  }
135 
136  static value_t conv(std::istream& is) {
137  int i;
138  if (is >> i)
139  {
140  if (i == 0 || i == 1)
141  return i;
142  else
143  fail_reading(is,
144  sname() + ": invalid value: " + std::to_string(i));
145  }
146  else
147  fail_reading(is, sname() + ": invalid value");
148  }
149 
150  static value_t parse(const std::string & s, size_t& p) {
151  if ((p>=4)&&(!s.compare(p-4,4,"true"))) {
152  p -= 4;
153  return true;
154  }
155  if ((p>=5)&&(!s.compare(p-5,5,"false"))) {
156  p -= 5;
157  return false;
158  }
159  if(s[--p] == '0')
160  return false;
161  if(s[p] == '1')
162  return true;
163  throw parse_exception("Wrong Boolean value");
164  }
165 
166  static std::ostream& print(value_t v, std::ostream& o,
167  const std::string& format = "text") {
168  if (format == "latex")
169  o << (v ? "\\top" : "\\bot");
170  else
171  o << (v? "true" : "false");
172  return o;
173  }
174 
175  static std::ostream& print_set(std::ostream& o, const std::string& format = "text") {
176  if (format == "latex")
177  o << "\\mathbb{B}";
178  else if (format == "text")
179  o << "B";
180  else
181  raise("invalid format: ", format);
182  return o;
183  }
184 
185 
186  template<unsigned version = version::fsm_json>
187  static
189  {
190  switch (version) {
191  case 0:
192  throw parse_exception("[B] Unsupported fsm-json version:"
193  + std::to_string(version));
194  case 1:
195  default:
196  return new json::object_t("semiring", new json::string_t("B"));
197  }
198  }
199 
200 
201  template<unsigned version = version::fsm_json>
202  static
204  {
205  switch (version) {
206  case 0:
207  throw parse_exception("[B] Unsupported fsm-json version:"
208  + std::to_string(version));
209  case 1:
210  default:
211  return (new json::bool_t(v));
212  }
213  }
214 
215  template<unsigned version = version::fsm_json>
217  switch (version) {
218  case 0:
219  throw parse_exception("[B] Unsupported fsm-json version:"
220  + std::to_string(version));
221  case 1:
222  default:
223  return p->to_bool();
224  }
225  }
226  };
227 
228  inline b join(const b&, const b&) { return {}; }
229 
230 }}//end of namespaces awali::sttc, and awali
231 
232 
233 #endif // !AWALI_WEIGHTSET_B_HH
Definition: node.hh:555
Definition: node.hh:193
virtual bool to_bool() const
Coerces this node_t to bool.
Definition: node.hh:306
Definition: node.hh:367
Definition: node.hh:529
The Boolean semring.
Definition: b.hh:38
static size_t hash(value_t v)
Definition: b.hh:128
static json::node_t * to_json()
Definition: b.hh:188
static value_t conv(std::istream &is)
Definition: b.hh:136
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 value_t plus(value_t v)
Definition: b.hh:89
static bool is_one(value_t v)
Definition: b.hh:112
constexpr static bool is_special(value_t)
Definition: b.hh:104
static value_t one()
Definition: b.hh:64
static value_t conv(self_type, value_t v)
Definition: b.hh:132
static std::ostream & print(value_t v, std::ostream &o, const std::string &format="text")
Definition: b.hh:166
static bool equals(value_t l, value_t r)
Definition: b.hh:93
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:99
static value_t parse(const std::string &s, size_t &p)
Definition: b.hh:150
constexpr static bool show_one()
Definition: b.hh:118
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:203
constexpr static bool is_commutative_semiring()
Definition: b.hh:116
static bool is_zero(value_t v)
Definition: b.hh:108
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:216
static std::ostream & print_set(std::ostream &o, const std::string &format="text")
Definition: b.hh:175
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:124
constexpr static star_status_t star_status()
Definition: b.hh:120
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
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: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
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26