Vaucanson  1.4.1
krat_exp_pattern.hh
1 // krat_exp_pattern.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2011 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH
18 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH
19 
21 # include <vaucanson/algebra/implementation/series/series.hh>
22 # include <vaucanson/algebra/implementation/series/rat/dispatch_visitor.hh>
23 
24 namespace vcsn {
25 
26  namespace algebra {
27 
28  /*---------.
29  | BinaryOp |
30  `---------*/
32 
35  template <class T, class U>
36  struct BinaryOp
37  {
38  typedef T lhs_node_type;
39  typedef U rhs_node_type;
40 
41  BinaryOp();
42  BinaryOp(const BinaryOp& b);
43  BinaryOp(const T& lhs, const U& rhs);
44  T& lhs();
45  const T& lhs() const;
46  U& rhs();
47  const U& rhs() const;
48 
49  private:
50  T lhs_;
51  U rhs_;
52  };
53 
54  /*--------.
55  | UnaryOp |
56  `--------*/
58 
61  template <class T>
62  struct UnaryOp
63  {
64  typedef T value_type;
65 
66  UnaryOp();
67  UnaryOp(const UnaryOp& b);
68  UnaryOp(const T& node);
69  T& value();
70  const T& value() const;
71 
72  private:
73  T node_;
74  };
75 
76  /*------.
77  | Value |
78  `------*/
80 
83  template <class T>
84  struct Value
85  {
86  typedef T value_type;
87 
88  Value();
89  Value(const Value& v);
90  Value(const T& v);
91  const T& value() const;
92  T& value();
93 
94  private:
95  T v_;
96  };
97 
98  /*---------------.
99  | GenericMatcher |
100  `---------------*/
101 
109  template <class Self, class T, class U, class F>
111  {
112  typedef U return_type;
113 
121  U
122  match(const T& ast);
123 
124  protected:
125  GenericMatcher();
126  };
127 
128 #define DecBinaryOp(N, T, U) \
129 struct N : public vcsn::algebra::BinaryOp<T, U> \
130 { \
131  N(const T& lhs, const U& rhs) : vcsn::algebra::BinaryOp<T, U>(lhs, rhs) \
132  {} \
133 };
134 
135 #define DecUnaryOp(N, T) \
136 struct N : public vcsn::algebra::UnaryOp<T> \
137 { \
138  N(const T& node) : vcsn::algebra::UnaryOp<T>(node) \
139  {} \
140 };
141 
142 #define DecLeaf(N, U) \
143  struct N : public vcsn::algebra::Value<U> \
144  { \
145  N(const U& v) : \
146  vcsn::algebra::Value<U>(v) \
147  {} \
148  };
149 
150 #define DecFinalLeaf(N) \
151  struct N \
152  { \
153  N() \
154  {} \
155  };
156 
157 #define MATCH__(N, Lhs, Rhs) \
158 return_type \
159 match_node##N(const N& p____) \
160 { \
161  typename N::lhs_node_type Lhs = p____.lhs(); \
162  typename N::rhs_node_type Rhs = p____.rhs();
163 
164 #define MATCH_(N, Val) \
165 return_type \
166 match_node##N(const N& p____) \
167 { \
168  typename N::value_type Val(p____.value());
169 
170 #define MATCH(N) \
171 return_type \
172 match_node##N(const N&) \
173 {
174 
175 #define END }
176 
177 
178  template <class Self, class T, class U, class F>
179  struct KRatExpMatcher : public GenericMatcher<Self, T, U, F>
180  {
181  typedef U return_type;
182  typedef typename T::semiring_elt_value_t semiring_elt_value_t;
183  typedef typename T::monoid_elt_value_t monoid_elt_value_t;
184 
185  DecBinaryOp(Product, T, T);
186  DecBinaryOp(Sum, T, T);
187  DecUnaryOp(Star, T);
188  DecBinaryOp(LeftWeight, semiring_elt_value_t, T);
189  DecBinaryOp(RightWeight, T, semiring_elt_value_t);
190  DecLeaf(Constant, monoid_elt_value_t);
191  DecFinalLeaf(One);
192  DecFinalLeaf(Zero);
193 
194  protected:
195  KRatExpMatcher() {}
196  };
197 
198 #define INHERIT_CONSTRUCTORS(Self, T, U, F) \
199  typedef ::vcsn::algebra::KRatExpMatcher<Self, T, U, F> krat_exp_matcher_t; \
200  typedef typename krat_exp_matcher_t::Product Product; \
201  typedef typename krat_exp_matcher_t::Sum Sum; \
202  typedef typename krat_exp_matcher_t::Star Star; \
203  typedef typename krat_exp_matcher_t::LeftWeight LeftWeight; \
204  typedef typename krat_exp_matcher_t::RightWeight RightWeight; \
205  typedef typename krat_exp_matcher_t::Constant Constant; \
206  typedef typename krat_exp_matcher_t::One One; \
207  typedef typename krat_exp_matcher_t::Zero Zero;
208 
209  template <class Self, class Series, class T, class Dispatch>
210  struct KRatExpIdentity : vcsn::algebra::KRatExpMatcher<
211  Self,
212  T,
213  Element<Series, T>,
214  Dispatch
215  >
216  {
217  typedef Self self_t;
218  typedef Element<Series, T> return_type;
219  typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
220  typedef typename semiring_elt_t::value_t semiring_elt_value_t;
221  typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
222  typedef typename monoid_elt_t::set_t monoid_t;
223  typedef typename monoid_t::alphabet_t alphabet_t;
224  typedef typename alphabet_t::letter_t letter_t;
225  INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
226 
227  KRatExpIdentity(const Element<Series, T>& exp) :
228  exp_(exp)
229  {}
230 
231  MATCH__(Product, lhs, rhs)
232  {
233  return this->match(lhs) * this->match(rhs);
234  }
235  END
236 
237  MATCH__(Sum, lhs, rhs)
238  {
239  return this->match(lhs) + this->match(rhs);
240  }
241  END
242 
243  MATCH_(Star, e)
244  {
245  return_type r (this->match(e));
246  r.star();
247  return r;
248  }
249  END
250 
251  MATCH__(LeftWeight, w, e)
252  {
253  semiring_elt_t welt (exp_.structure().semiring(), w);
254  return welt * this->match(e);
255  }
256  END
257 
258  MATCH__(RightWeight, e, w)
259  {
260  semiring_elt_t welt (exp_.structure().semiring(), w);
261  return this->match(e) * welt;
262  }
263  END
264 
265  MATCH_(Constant, m)
266  {
267  monoid_elt_t melt (exp_.structure().monoid(), m);
268  return Element<Series, T> (exp_.structure(), melt);
269  }
270  END
271 
272  MATCH(Zero)
273  {
274  return zero_as<T>::of(exp_.structure());
275  }
276  END
277 
278  MATCH(One)
279  {
280  return identity_as<T>::of(exp_.structure());
281  }
282  END
283 
284  protected:
285  Element<Series, T> exp_;
286  };
287 
288 
289  } // algebra
290 
291 } // vcsn
292 
293 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
294 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hxx>
295 # endif // VCSN_USE_INTERFACE_ONLY
296 
297 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH