Awali
Another Weighted Automata library
lift.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 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_ALGOS_LIFT_HH
18 # define AWALI_ALGOS_LIFT_HH
19 
20 # include <map>
21 # include <list>
22 
29 
30 namespace awali { namespace sttc {
31 
32 
33  namespace internal
34  {
35  /*--------------.
36  | lift(types). |
37  `--------------*/
38 
39  template <typename ValueSet>
40  struct context_of
41  {
42  using type = typename ValueSet::context_t;
43  };
44 
45  template <typename Context>
46  struct context_of<sttc::mutable_automaton<Context>>
47  {
48  using type = Context;
49  };
50 
51  template <typename ValueSet>
53 
54  template <typename Context>
57 
58  template <typename Aut>
61 
62  template <typename RatExpSet>
65 
66  // lift(ctx) -> ctx
67  template <typename LabelSet, typename WeightSet>
70  {
71  auto rs_in
74  return {oneset{}, rs_in};
75  }
76 
77  // lift(ratexpset) -> ratexpset
78  template <typename Context>
79  lifted_ratexpset_t<ratexpset<Context>>
81  {
82  return {lift_context(rs.context()), rs.identities()};
83  }
84 
85  template <typename Aut>
86  inline
88  unnormalized_lift(const Aut& a, bool keep_history=true)
89  {
90  using auto_in_t = typename Aut::element_type;
91  using ctx_in_t = context_t_of<auto_in_t>;
92 
93  // Produce RatExps of the same context as the original automaton.
94  using rs_in_t = ratexpset_of<ctx_in_t>;
95  rs_in_t rs_in{get_rat_context(a->context()), rs_in_t::identities_t::trivial};
96 
97  auto ctx_out = internal::lift_context(a->context());
98  using auto_out_t = internal::lifted_automaton_t<auto_in_t>;
99  auto_out_t res = make_shared_ptr<auto_out_t>(ctx_out);
100  std::map<state_t, state_t> map;
101  map[a->pre()] = res->pre();
102  map[a->post()] = res->post();
103  for (auto s: a->states())
104  map[s] = res->add_state();
105 
106  for (auto t: a->all_transitions())
107  if (a->src_of(t) == a->pre())
108  res->add_initial(map[a->dst_of(t)],
109  rs_in.lmul(a->weight_of(t), rs_in.one()));
110  else if (a->dst_of(t) == a->post())
111  res->add_final(map[a->src_of(t)],
112  rs_in.lmul(a->weight_of(t), rs_in.one()));
113  else if (a->context().labelset()->is_one(a->label_of(t)))
114  {
115  res->add_transition
116  (map[a->src_of(t)], map[a->dst_of(t)],
117  {},
118  rs_in.lmul(a->weight_of(t), rs_in.one()));
119  }
120  else
121  res->add_transition
122  (map[a->src_of(t)], map[a->dst_of(t)],
123  {},
124  rs_in.lmul(a->weight_of(t), rs_in.atom(a->label_of(t))));
125  if(keep_history) {
126  auto history = std::make_shared<single_history<Aut>>(a);
127  for(auto p : map)
128  history->add_state(p.second,p.first);
129  res->set_history(history);
130  }
131  return res;
132  }
133  } //internal
134 
135  /*------------------.
136  | lift(automaton). |
137  `------------------*/
138 
139 
156  template <typename Aut>
157  inline
158  internal::lifted_automaton_t<Aut>
159  lift(const Aut& a, bool keep_history=true) {
160  auto aut = internal::unnormalized_lift(a, keep_history);
161  state_t i = aut->add_state();
162  state_t t = aut->add_state();
163  std::list<state_t> li;
164  for(auto tr : aut->initial_transitions()) {
165  aut->new_transition(i, aut->dst_of(tr), {}, aut->weight_of(tr));
166  li.emplace_back(aut->dst_of(tr));
167  }
168  aut->set_initial(i);
169  aut->set_state_name(i, "I");
170 
171  for( auto s : li)
172  aut->unset_initial(s);
173 
174  std::list<state_t> lt;
175  for(auto tr : aut->final_transitions()) {
176  aut->new_transition(aut->src_of(tr), t, {}, aut->weight_of(tr));
177  lt.emplace_back(aut->src_of(tr));
178  }
179  aut->set_final(t);
180  aut->set_state_name(t, "T");
181  for( auto s : lt)
182  aut->unset_final(s);
183  return aut;
184  }
185 
186  /*---------------.
187  | lift(ratexp). |
188  `---------------*/
189 
190  namespace internal
191  {
192  template <typename Exp>
194  typename lifted_context_t<context_t_of<Exp>>::ratexp_t;
195  }
196 
197  template <typename RatExpSet>
198  inline
200  lift(const RatExpSet& rs, const typename RatExpSet::ratexp_t& e)
201  {
202  auto lrs = internal::lift_ratexpset(rs);
203  return lrs.lmul(e, lrs.one());
204  }
205 
206 }}//end of ns awali::stc
207 
208 #endif // !AWALI_ALGOS_LIFT_HH
carries the algebraic settings of automata
Definition: context.hh:40
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:38
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:134
mutable_automaton< lifted_context_t< context_t_of< Aut > >> lifted_automaton_t
Definition: lift.hh:60
internal::lifted_automaton_t< Aut > unnormalized_lift(const Aut &a, bool keep_history=true)
Definition: lift.hh:88
typename ValueSet::context_t type
Definition: lift.hh:42
lifted_context_t< context< LabelSet, WeightSet > > lift_context(const context< LabelSet, WeightSet > &ctx)
Definition: lift.hh:69
typename context_of< ValueSet >::type context_of_t
Definition: lift.hh:52
lifted_ratexpset_t< ratexpset< Context > > lift_ratexpset(const ratexpset< Context > &rs)
Definition: lift.hh:80
typename lifted_context_t< context_t_of< Exp > >::ratexp_t lifted_ratexp_t
Definition: lift.hh:194
Definition: lift.hh:41
@ trivial
Trivial identities only.
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
internal::lifted_automaton_t< Aut > lift(const Aut &a, bool keep_history=true)
Lift labels to weights.
Definition: lift.hh:159
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
ratexp_context_of< Context > get_rat_context(const Context &ctx)
Definition: traits.hh:295
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
Provide a variadic mul on top of a binary mul(), and one().
Definition: weightset.hh:38