Vcsn  2.1
Be Rational
others.cc
Go to the documentation of this file.
3 #include <vcsn/dyn/algos.hh>
4 #include <vcsn/dyn/automaton.hh>
5 #include <vcsn/dyn/context.hh>
6 #include <vcsn/dyn/registries.hh>
8 #include <vcsn/dyn/translate.hh>
9 #include <vcsn/misc/builtins.hh>
10 #include <vcsn/misc/getargs.hh>
11 
12 namespace vcsn
13 {
14  namespace dyn
15  {
16 
17  /*---------------.
18  | conjunction. |
19  `---------------*/
20 
21  // Implement the binary case on top of the variadic one, to avoid
22  // compiling it twice.
23  automaton
24  conjunction(const automaton& lhs, const automaton& rhs, bool lazy)
25  {
26  auto auts = std::vector<automaton>{lhs, rhs};
27  return conjunction(auts, lazy);
28  }
29 
30 
31  /*---------.
32  | focus. |
33  `---------*/
34 
35  static
37  {
38  return integral_constant{symbol("std::integral_constant<unsigned, "
39  + std::to_string(tape) + '>')};
40  }
41 
42  REGISTRY_DEFINE(focus);
43  automaton
44  focus(const automaton& aut, unsigned tape)
45  {
46  auto t = to_integral_constant(tape);
47  return detail::focus_registry().call(aut, t);
48  }
49 
50 
51  /*----------------.
52  | infiltration. |
53  `----------------*/
54 
55  automaton
56  infiltration(const automaton& lhs, const automaton& rhs)
57  {
58  auto auts = std::vector<automaton>{lhs, rhs};
59  return infiltration(auts);
60  }
61 
62 
63  /*-------------------------.
64  | lift(automaton, tapes). |
65  `-------------------------*/
66 
67  REGISTRY_DEFINE(lift_automaton);
68  automaton
69  lift(const automaton& aut,
70  const std::vector<unsigned>& tapes, vcsn::rat::identities ids)
71  {
72  std::string signame;
73  for (auto t : tapes)
74  {
75  if (!signame.empty())
76  signame += ", ";
77  signame += ("std::integral_constant<unsigned, "
78  + std::to_string(t) + '>');
79  }
80  auto t = integral_constant{symbol("const std::tuple<" + signame + ">&")};
81  signature sig;
82  sig.sig.emplace_back(vname(aut));
83  sig.sig.emplace_back("vcsn::rat::identities");
84  for (const auto& t: tapes)
85  sig.sig.emplace_back("std::integral_constant<unsigned, "
86  + std::to_string(t) + '>');
87  return detail::lift_automaton_registry().call(sig, aut, ids, t);
88  }
89 
90 
91  /*---------------.
92  | make_context. |
93  `---------------*/
94 
95  REGISTRY_DEFINE(make_context);
96  context
97  make_context(const std::string& n)
98  {
100  std::string full_name = ast::normalize_context(n, true);
101  if (!detail::make_context_registry().get0({sname}))
102  compile(sname);
103  return detail::make_context_registry().call({sname}, full_name);
104  }
105 
106 
107  /*---------.
108  | project. |
109  `---------*/
110 
111  REGISTRY_DEFINE(project);
112  automaton
113  project(const automaton& aut, unsigned tape)
114  {
115  auto t = to_integral_constant(tape);
116  return detail::project_registry().call(aut, t);
117  }
118 
119  REGISTRY_DEFINE(project_context);
120  context
121  project(const context& ctx, unsigned tape)
122  {
123  auto t = to_integral_constant(tape);
124  return detail::project_context_registry().call(ctx, t);
125  }
126 
127 
128  /*-----------.
129  | shuffle. |
130  `-----------*/
131 
132  automaton
133  shuffle(const automaton& lhs, const automaton& rhs)
134  {
135  auto auts = std::vector<automaton>{lhs, rhs};
136  return shuffle(auts);
137  }
138 
139 
140  /*----------------.
141  | to_automaton. |
142  `----------------*/
143 
144  automaton
145  to_automaton(const expression& exp, const std::string& algo)
146  {
147  enum class algo_t
148  {
149  derivation,
150  expansion,
151  standard,
152  thompson,
153  zpc,
154  zpc_compact,
155  };
156  static const auto map = std::map<std::string, algo_t>
157  {
158  {"derivation", algo_t::derivation},
159  {"expansion", algo_t::expansion},
160  {"derived_term", algo_t::expansion},
161  {"auto", algo_t::expansion},
162  {"standard", algo_t::standard},
163  {"thompson", algo_t::thompson},
164  {"zpc", algo_t::zpc},
165  {"zpc_compact", algo_t::zpc_compact},
166  };
167  switch (getargs("to_automaton: algorithm", map, algo))
168  {
169  case algo_t::expansion:
170  return strip(derived_term(exp));
171  case algo_t::derivation:
172  return strip(derived_term(exp, "derivation"));
173  case algo_t::standard:
174  return standard(exp);
175  case algo_t::thompson:
176  return thompson(exp);
177  case algo_t::zpc:
178  return zpc(exp);
179  case algo_t::zpc_compact:
180  return zpc(exp, "compact");
181  }
183  }
184 
185 
186  /*---------.
187  | tuple. |
188  `---------*/
189 
190  expression
191  tuple(const expression& lhs, const expression& rhs)
192  {
193  auto auts = std::vector<expression>{lhs, rhs};
194  return tuple(auts);
195  }
196 
197  std::string type(const automaton& a)
198  {
199  return a->vname();
200  }
201  }
202 }
automaton project(const automaton &aut, unsigned tape)
Keep a single tape from a multiple-tape automaton.
Definition: others.cc:113
automaton strip(const automaton &a)
The automaton in a with its metadata layers removed.
Definition: strip.hh:51
automaton standard(const automaton &a)
A standardized a.
Definition: standard.hh:141
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:41
polynomial derivation(const expression &exp, const label &lbl, bool breaking=false)
Derive exp with respect to s.
Definition: derivation.hh:337
automaton lift(const automaton &aut, const std::vector< unsigned > &tapes={}, rat::identities ids={})
Lift some tapes of the transducer, or turn an automaton into a spontaneous automaton.
Definition: others.cc:69
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:197
C::mapped_type getargs(const std::string &kind, const C &map, const std::string &key)
Find a correspondance in a map.
Definition: getargs.hh:21
std::string normalize_context(const std::string &ctx, bool full=true)
A context, normalized.
automaton lift_automaton(const automaton &aut, vcsn::rat::identities ids, integral_constant)
Bridge.
Definition: lift.hh:260
std::string to_string(identities i)
Wrapper around operator<<.
Definition: identities.cc:17
symbol vname(T &t)
Definition: name.hh:101
context< project_labelset< Tape, labelset_t_of< Context >>, weightset_t_of< Context >> project_context
The type of the resulting apparent context when keeping only tape Tape.
Definition: tupleset.hh:1194
A simple placeholder for integral constants.
Definition: name.hh:198
automaton conjunction(const automaton &lhs, const automaton &rhs, bool lazy=false)
The conjunction (aka synchronized product) of automata.
Definition: others.cc:24
automaton derived_term(const expression &exp, const std::string &algo="auto")
The derived-term automaton of exp.
rat::expression_polynomial_t< ExpSet > derivation(const ExpSet &rs, const typename ExpSet::value_t &e, label_t_of< ExpSet > a, bool breaking=false)
Derive an expression wrt to a letter.
Definition: derivation.hh:267
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:80
static integral_constant to_integral_constant(unsigned tape)
Definition: others.cc:36
automaton shuffle(const automaton &lhs, const automaton &rhs)
The shuffle product of automata lhs and rhs.
Definition: others.cc:133
void compile(const std::string &ctx)
Compile, and load, a DSO with instantiations for ctx.
Definition: translate.cc:380
Aut zpc(const context_t_of< Aut > &ctx, const ExpSet &rs, const typename ExpSet::value_t &r, const std::string &algo="auto")
Build a ZPC automaton from an expression.
Definition: zpc.hh:335
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
std::shared_ptr< const detail::expansion_base > expansion
Definition: expansion.hh:73
static identities ids(const driver &d)
Get the identities of the driver.
Definition: parse.cc:87
automaton thompson(const expression &e)
The Thompson automaton of e.
Definition: thompson.hh:208
automaton focus(const automaton &aut, unsigned tape)
Focus on a specific tape of a tupleset automaton.
Definition: others.cc:44
automaton to_automaton(const expression &exp, const std::string &algo="auto")
An automaton denoting the language of exp.
Definition: others.cc:145
context make_context(const std::string &name)
Build a context from its name.
Definition: others.cc:97
automaton zpc(const expression &exp, const std::string &algo="auto")
The ZPC automaton of exp.
Definition: zpc.hh:358
symbol sname()
Definition: name.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:21
Aut thompson(const context_t_of< Aut > &ctx, const ExpSet &rs, const typename ExpSet::value_t &r)
Build a Thompson automaton from an expression.
Definition: thompson.hh:176
Signature of a function call.
Definition: signature.hh:15
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:78
auto standard(const Aut &aut) -> decltype(copy(aut))
Definition: standard.hh:116
automaton infiltration(const automaton &lhs, const automaton &rhs)
The infiltration of automata lhs and rhs.
Definition: others.cc:56
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
expression tuple(const expression &lhs, const expression &rhs)
Build a two-tape expression from two expressions.
Definition: others.cc:191