Awali
Another Weighted Automata library
f2.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_F2_HH
18 # define AWALI_WEIGHTSET_F2_HH
19 
20 # include <cassert>
21 # include <ostream>
22 
24 #include <awali/utils/hash.hh>
25 #include <awali/sttc/misc/raise.hh>
26 #include <awali/common/enums.hh>
29 
30 namespace awali {
31  namespace sttc {
36  class f2
37  {
38  public:
39  using self_type = f2;
40 
41  static std::string sname()
42  {
43  return "f2";
44  }
45 
46  std::string vname(bool = true) const
47  {
48  return sname();
49  }
50 
52  static f2 make(std::istream& is)
53  {
54  eat(is, sname());
55  return {};
56  }
57 
58  using value_t = bool;
59  using finite_t = bool;
60  static const finite_t finite=true;
61 
62  static value_t
63  zero()
64  {
65  return false;
66  }
67 
68  static value_t
69  one()
70  {
71  return true;
72  }
73 
74  static value_t
75  add(const value_t l, const value_t r)
76  {
77  return l ^ r;
78  }
79 
80  static value_t
81  sub(const value_t l, const value_t r)
82  {
83  return l ^ r;
84  }
85 
86  static value_t
87  mul(const value_t l, const value_t r)
88  {
89  return l && r;
90  }
91 
92  static value_t
93  rdiv(const value_t l, const value_t r)
94  {
95  require(!is_zero(r), "div: division by zero");
96  return l;
97  }
98 
99  static value_t
100  ldiv(const value_t l, const value_t r)
101  {
102  return rdiv(r, l);
103  }
104 
105  static value_t
106  star(const value_t v)
107  {
108  if (!v)
109  return true;
110  else
111  throw std::domain_error("f2: star: invalid value: 1");
112  }
113 
114  static value_t
115  plus(const value_t v)
116  {
117  if (!v)
118  return false;
119  else
120  throw std::domain_error("f2: star: invalid value: 1");
121  }
122 
123  static bool
124  equals(const value_t l, const value_t r)
125  {
126  return l == r;
127  }
128 
130  static bool less_than(value_t lhs, value_t rhs)
131  {
132  return lhs < rhs;
133  }
134 
135  constexpr static bool is_special(value_t)
136  {
137  return false;
138  }
139 
140  static bool
141  is_zero(const value_t v)
142  {
143  return !v;
144  }
145 
146  static bool
147  is_one(const value_t v)
148  {
149  return v;
150  }
151 
152  static constexpr bool is_commutative_semiring() { return true; }
153 
154  static constexpr bool show_one() { return false; }
155 
156  static constexpr
158 
159  static value_t
161  {
162  return v;
163  }
164 
165  static size_t hash(value_t v)
166  {
167  return utils::hash_value(v);
168  }
169 
170  static value_t
172  {
173  return v;
174  }
175 
176  static value_t
177  conv(std::istream& stream)
178  {
179  int i;
180  if ((stream >> i) && (i == 0 || i == 1))
181  return i;
182  else
183  sttc::fail_reading(stream, sname() + ": invalid value");
184  }
185 
186  static value_t
187  parse(const std::string & s, size_t& p) {
188  if ((p>=4)&&(!s.compare(p-4,4,"true"))) {
189  p -= 4;
190  return true;
191  }
192  if ((p>=5)&&(!s.compare(p-5,5,"false"))) {
193  p -= 5;
194  return false;
195  }
196  if(s[--p] == '0')
197  return false;
198  if(s[p] == '1')
199  return true;
200  p++;
201  throw parse_exception("Wrong F2 value");
202  }
203 
204  static std::ostream&
205  print(const value_t v, std::ostream& o,
206  const std::string& = "text")
207  {
208  return o << (v ? '1' : '0');
209  }
210 
211  std::ostream&
212  print_set(std::ostream& o, const std::string& format = "text") const
213  {
214  if (format == "latex")
215  o << "\\mathbb{F}_2";
216  else if (format == "text")
217  o << "F2";
218  else
219  raise("invalid format: ", format);
220  return o;
221  }
222 
223 
224  template<unsigned version = version::fsm_json>
226  const
227  {
228  version::check_fsmjson<version>();
229  switch (version) {
230  case 0: /* Never occurs due to above check. */
231  case 1:
232  default:
233  json::object_t* obj = new json::object_t();
234  obj->push_back("semiring", new json::string_t("F2"));
235  return (obj);
236  }
237  }
238 
239  template<unsigned version = version::fsm_json>
241  const
242  {
243  version::check_fsmjson<version>();
244  switch (version) {
245  case 0: /* Never occurs due to above check. */
246  case 1:
247  default:
248  return new json::int_t(v);
249  }
250  }
251 
252  template<unsigned version = version::fsm_json>
254  const
255  {
256  version::check_fsmjson<version>();
257  switch (version) {
258  case 0: /* Never occurs due to above check. */
259  case 1:
260  default:
261  return p->to_bool();
262  }
263  }
264 
265  };
266 
267  inline f2 join(const f2&, const f2&) { return {}; }
268 
269 
270 }}//end of ns awali::stc
271 
272 #endif // !AWALI_WEIGHTSET_F2_HH
Definition: node.hh:488
Definition: node.hh:193
virtual bool to_bool() const
Coerces this node_t to bool.
Definition: node.hh:306
Definition: node.hh:367
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:529
The field F2.
Definition: f2.hh:37
static value_t conv(self_type, value_t v)
Definition: f2.hh:171
static value_t plus(const value_t v)
Definition: f2.hh:115
static bool is_one(const value_t v)
Definition: f2.hh:147
static value_t sub(const value_t l, const value_t r)
Definition: f2.hh:81
static constexpr bool is_commutative_semiring()
Definition: f2.hh:152
bool value_t
Definition: f2.hh:58
static f2 make(std::istream &is)
Build from the description in is.
Definition: f2.hh:52
static value_t star(const value_t v)
Definition: f2.hh:106
static size_t hash(value_t v)
Definition: f2.hh:165
json::node_t * to_json() const
Definition: f2.hh:225
static value_t add(const value_t l, const value_t r)
Definition: f2.hh:75
constexpr static bool is_special(value_t)
Definition: f2.hh:135
static value_t conv(std::istream &stream)
Definition: f2.hh:177
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: f2.hh:130
std::string vname(bool=true) const
Definition: f2.hh:46
static bool is_zero(const value_t v)
Definition: f2.hh:141
static const finite_t finite
Definition: f2.hh:60
static value_t one()
Definition: f2.hh:69
value_t value_from_json(json::node_t const *p) const
Definition: f2.hh:253
static bool equals(const value_t l, const value_t r)
Definition: f2.hh:124
static constexpr bool show_one()
Definition: f2.hh:154
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: f2.hh:212
static value_t transpose(const value_t v)
Definition: f2.hh:160
static value_t zero()
Definition: f2.hh:63
json::node_t * value_to_json(value_t v) const
Definition: f2.hh:240
static value_t mul(const value_t l, const value_t r)
Definition: f2.hh:87
static std::ostream & print(const value_t v, std::ostream &o, const std::string &="text")
Definition: f2.hh:205
static constexpr star_status_t star_status()
Definition: f2.hh:157
static value_t ldiv(const value_t l, const value_t r)
Definition: f2.hh:100
static std::string sname()
Definition: f2.hh:41
static value_t parse(const std::string &s, size_t &p)
Definition: f2.hh:187
static value_t rdiv(const value_t l, const value_t r)
Definition: f2.hh:93
bool finite_t
Definition: f2.hh:59
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
@ NON_STARRABLE
Definition: enums.hh:166
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