Awali
Another Weighted Automata library
f2.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_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 bool
115  equals(const value_t l, const value_t r)
116  {
117  return l == r;
118  }
119 
121  static bool less_than(value_t lhs, value_t rhs)
122  {
123  return lhs < rhs;
124  }
125 
126  constexpr static bool is_special(value_t)
127  {
128  return false;
129  }
130 
131  static bool
132  is_zero(const value_t v)
133  {
134  return !v;
135  }
136 
137  static bool
138  is_one(const value_t v)
139  {
140  return v;
141  }
142 
143  static constexpr bool is_commutative_semiring() { return true; }
144 
145  static constexpr bool show_one() { return false; }
146 
147  static constexpr
149 
150  static value_t
152  {
153  return v;
154  }
155 
156  static size_t hash(value_t v)
157  {
158  return utils::hash_value(v);
159  }
160 
161  static value_t
163  {
164  return v;
165  }
166 
167  static value_t
168  conv(std::istream& stream)
169  {
170  int i;
171  if ((stream >> i) && (i == 0 || i == 1))
172  return i;
173  else
174  sttc::fail_reading(stream, sname() + ": invalid value");
175  }
176 
177  static value_t
178  parse(const std::string & s, size_t& p) {
179  if ((p>=4)&&(!s.compare(p-4,4,"true"))) {
180  p -= 4;
181  return true;
182  }
183  if ((p>=5)&&(!s.compare(p-5,5,"false"))) {
184  p -= 5;
185  return false;
186  }
187  if(s[--p] == '0')
188  return false;
189  if(s[p] == '1')
190  return true;
191  p++;
192  throw parse_exception("Wrong F2 value");
193  }
194 
195  static std::ostream&
196  print(const value_t v, std::ostream& o,
197  const std::string& = "text")
198  {
199  return o << (v ? '1' : '0');
200  }
201 
202  std::ostream&
203  print_set(std::ostream& o, const std::string& format = "text") const
204  {
205  if (format == "latex")
206  o << "\\mathbb{F}_2";
207  else if (format == "text")
208  o << "F2";
209  else
210  raise("invalid format: ", format);
211  return o;
212  }
213 
214 
215  template<unsigned version = version::fsm_json>
217  const
218  {
219  switch (version) {
220  case 0:
221  throw parse_exception("[f2] Unsupported fsm-json version:"
222  + std::to_string(version));
223  case 1:
224  default:
225  json::object_t* obj = new json::object_t();
226  obj->push_back("semiring", new json::string_t("F2"));
227  return (obj);
228  }
229  }
230 
231  template<unsigned version = version::fsm_json>
233  const
234  {
235  switch (version) {
236  case 0:
237  throw parse_exception("[f2] Unsupported fsm-json version:"
238  + std::to_string(version));
239  case 1:
240  default:
241  return new json::int_t(v);
242  }
243  }
244 
245  template<unsigned version = version::fsm_json>
247  const
248  {
249  switch (version) {
250  case 0:
251  case 1:
252  default:
253  return p->to_bool();
254  }
255  }
256 
257  };
258 
259  inline f2 join(const f2&, const f2&) { return {}; }
260 
261 
262 }}//end of ns awali::stc
263 
264 #endif // !AWALI_WEIGHTSET_F2_HH
Definition: node.hh:483
Definition: node.hh:191
virtual bool to_bool() const
Coerces this node_t to bool.
Definition: node.hh:304
Definition: node.hh:365
object_t * push_back(std::string key, node_t *node)
Definition: node.hh:526
The field F2.
Definition: f2.hh:37
static value_t conv(self_type, value_t v)
Definition: f2.hh:162
static bool is_one(const value_t v)
Definition: f2.hh:138
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:143
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:156
json::node_t * to_json() const
Definition: f2.hh:216
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:126
static value_t conv(std::istream &stream)
Definition: f2.hh:168
static bool less_than(value_t lhs, value_t rhs)
Whether lhs < rhs.
Definition: f2.hh:121
std::string vname(bool=true) const
Definition: f2.hh:46
static bool is_zero(const value_t v)
Definition: f2.hh:132
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:246
static bool equals(const value_t l, const value_t r)
Definition: f2.hh:115
static constexpr bool show_one()
Definition: f2.hh:145
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: f2.hh:203
static value_t transpose(const value_t v)
Definition: f2.hh:151
static value_t zero()
Definition: f2.hh:63
json::node_t * value_to_json(value_t v) const
Definition: f2.hh:232
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:196
static constexpr star_status_t star_status()
Definition: f2.hh:148
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:178
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:161
@ NON_STARRABLE
Definition: enums.hh:164
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