clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
aggregate.hh
Go to the documentation of this file.
1 // {{{ GPL License
2 
3 // This file is part of gringo - a grounder for logic programs.
4 // Copyright (C) 2013 Roland Kaminski
5 
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 // }}}
20 
21 #ifndef _GRINGO_INPUT_AGGREGATE_HH
22 #define _GRINGO_INPUT_AGGREGATE_HH
23 
24 #include <gringo/input/literal.hh>
25 #include <gringo/safetycheck.hh>
26 #include <list>
27 
28 namespace Gringo {
29 
30 namespace Ground {
31 using ULitVec = std::vector<ULit>;
32 struct Statement;
33 using UStm = std::unique_ptr<Statement>;
34 using UStmVec = std::vector<UStm>;
35 }
36 
37 namespace Input {
38 
39 // {{{ declaration of auxiliary types
40 
41 typedef std::pair<ULit, ULitVec> CondLit;
42 typedef std::vector<CondLit> CondLitVec;
43 typedef std::pair<UTermVec, ULitVec> BodyAggrElem;
44 typedef std::vector<BodyAggrElem> BodyAggrElemVec;
45 
46 typedef std::tuple<UTermVec, ULit, ULitVec> HeadAggrElem;
47 typedef std::vector<HeadAggrElem> HeadAggrElemVec;
48 
50 typedef std::unique_ptr<BodyAggregate> UBodyAggr;
51 typedef std::vector<UBodyAggr> UBodyAggrVec;
52 
54 typedef std::unique_ptr<HeadAggregate> UHeadAggr;
55 typedef std::vector<UHeadAggr> UHeadAggrVec;
56 
57 // }}}
58 
59 // {{{ declaration of AssignLevel
60 
61 struct AssignLevel {
62  typedef std::unordered_map<FWString, unsigned> BoundSet;
63 
64  void add(VarTermBoundVec &vars);
66  void assignLevels();
67  void assignLevels(unsigned level, BoundSet const &bound);
68  virtual ~AssignLevel();
69 
70  std::list<AssignLevel> childs;
71  std::unordered_map<FWString, std::vector<VarTerm*>> occurr;
72 };
73 
74 // }}}
75 // {{{ declaration of CheckLevel
76 
77 struct CheckLevel {
78  struct Ent {
79  bool operator<(Ent const &) const;
80  };
82  using VarMap = std::unordered_map<FWString, SC::VarNode *>;
83 
84  CheckLevel(Location const &loc, Printable const &p);
86  SC::VarNode &var(VarTerm &var);
87  bool check();
88  ~CheckLevel();
89 
91  Printable const &p;
93  SC::EntNode *current = 0;
95 };
96 using ChkLvlVec = std::vector<CheckLevel>;
97 
98 // }}}
99 // {{{ declaration of ToGroundArg
100 
101 using CreateLit = std::function<void (Ground::ULitVec &, bool)>;
102 using CreateStm = std::function<Ground::UStm (Ground::ULitVec &&)>;
103 using CreateStmVec = std::vector<CreateStm>;
104 using CreateBody = std::pair<CreateLit, CreateStmVec>;
105 using CreateBodyVec = std::vector<CreateBody>;
106 using CreateHead = std::pair<CreateStm, CreateBody>;
107 
108 struct ToGroundArg {
109  ToGroundArg(unsigned &auxNames, PredDomMap &domains);
110  FWString newId(bool increment = true);
112  UTerm newId(UTermVec &&global, Location const &loc, bool increment = true);
113  template <class T>
114  UTerm newId(T const &x) {
116  x.collect(vars);
117  return newId(getGlobal(vars), x.loc());
118  }
119  ~ToGroundArg();
120 
121  unsigned &auxNames;
123 };
124 
125 // }}}
126 
127 // {{{ declaration of BodyAggregate
128 
129 struct BodyAggregate : Printable, Hashable, Locatable, Clonable<BodyAggregate>, Comparable<BodyAggregate> {
131  virtual void unpool(UBodyAggrVec &x, bool beforeRewrite) = 0;
134  virtual void simplify(Projections &project, Term::DotsMap &dots, Term::ScriptMap &scripts, unsigned &auxNum) = 0;
137  virtual void assignLevels(AssignLevel &lvl) = 0;
138  virtual bool check(ChkLvlVec &lvl) const = 0;
141  virtual void rewriteArithmetics(Term::ArithmeticsMap &arith, Literal::AssignVec &assign, unsigned &auxNum) = 0;
147  virtual bool rewriteAggregates(UBodyAggrVec &aggr) = 0;
150  virtual void removeAssignment() = 0;
151  virtual bool isAssignment() const = 0;
155  virtual void collect(VarTermBoundVec &vars) const = 0;
156  virtual bool hasPool(bool beforeRewrite) const = 0;
157  virtual void replace(Defines &dx) = 0;
158  virtual CreateBody toGround(ToGroundArg &x, Ground::UStmVec &stms) const = 0;
159  virtual ~BodyAggregate() { }
160 };
161 
162 // }}}
163 
164 // {{{ declaration of HeadAggregate
165 
166 struct HeadAggregate : Printable, Hashable, Locatable, Clonable<HeadAggregate>, Comparable<HeadAggregate> {
167 
169  virtual void unpool(UHeadAggrVec &x, bool beforeRewrite) = 0;
172  virtual void simplify(Projections &project, Term::DotsMap &dots, Term::ScriptMap &scripts, unsigned &auxNum) = 0;
175  virtual void assignLevels(AssignLevel &lvl) = 0;
176  virtual bool check(ChkLvlVec &lvl) const = 0;
179  virtual void rewriteArithmetics(Term::ArithmeticsMap &arith, unsigned &auxNum) = 0;
180  virtual UHeadAggr rewriteAggregates(UBodyAggrVec &aggr) = 0;
181  virtual UHeadAggr shiftHead(UBodyAggrVec &aggr) = 0;
185  virtual void collect(VarTermBoundVec &vars) const = 0;
186  virtual bool hasPool(bool beforeRewrite) const = 0;
187  virtual void replace(Defines &dx) = 0;
188  virtual CreateHead toGround(ToGroundArg &x, bool external) const = 0;
189  virtual Value isEDB() const;
190  virtual ~HeadAggregate() { }
191 };
192 
193 // }}}
194 
195 } } // namespace Input Gringo
196 
198 GRINGO_HASH(Gringo::Input::HeadAggregate)
199 
200 #endif // _GRINGO_INPUT_AGGREGATE_HH
Definition: comparable.hh:29
std::vector< ULit > ULitVec
Definition: literal.hh:61
Definition: aggregate.hh:77
Definition: locatable.hh:30
virtual UHeadAggr shiftHead(UBodyAggrVec &aggr)=0
AssignLevel & subLevel()
Definition: aggregate.cc:32
std::vector< UHeadAggr > UHeadAggrVec
Definition: aggregate.hh:55
std::unordered_map< FWString, SC::VarNode * > VarMap
Definition: aggregate.hh:82
virtual ~BodyAggregate()
Definition: aggregate.hh:159
CheckLevel(Location const &loc, Printable const &p)
Definition: aggregate.cc:54
FWString newId(bool increment=true)
Definition: aggregate.cc:85
std::unordered_map< FWString, unsigned > BoundSet
Definition: aggregate.hh:62
std::unique_ptr< Term > UTerm
Definition: statement.hh:32
Definition: literal.hh:52
Definition: value.hh:113
std::unordered_map< FWString, std::vector< VarTerm * > > occurr
Definition: aggregate.hh:71
virtual bool check(ChkLvlVec &lvl) const =0
Definition: printable.hh:30
std::unique_ptr< Statement > UStm
Definition: statement.hh:33
Scripts & scripts
Definition: literals.cc:126
Printable const & p
Definition: aggregate.hh:91
std::pair< CreateLit, CreateStmVec > CreateBody
Definition: aggregate.hh:104
virtual void assignLevels(AssignLevel &lvl)=0
std::vector< CondLit > CondLitVec
Definition: aggregate.hh:42
std::vector< std::unordered_map< UTerm, UTerm, value_hash< UTerm >, value_equal_to< UTerm >>> ArithmeticsMap
Definition: term.hh:121
std::vector< std::pair< UTerm, UTerm >> AssignVec
Definition: literal.hh:66
unsigned & auxNames
Definition: aggregate.hh:121
std::vector< HeadAggrElem > HeadAggrElemVec
Definition: aggregate.hh:47
virtual void rewriteArithmetics(Term::ArithmeticsMap &arith, unsigned &auxNum)=0
virtual ~HeadAggregate()
Definition: aggregate.hh:190
Definition: hashable.hh:36
Definition: aggregate.hh:129
virtual void collect(VarTermBoundVec &vars) const =0
virtual bool rewriteAggregates(UBodyAggrVec &aggr)=0
std::vector< BodyAggrElem > BodyAggrElemVec
Definition: aggregate.hh:44
virtual void replace(Defines &dx)=0
std::pair< UTermVec, ULitVec > BodyAggrElem
Definition: aggregate.hh:43
virtual void collect(VarTermBoundVec &vars) const =0
std::pair< CreateStm, CreateBody > CreateHead
Definition: aggregate.hh:106
std::vector< std::tuple< UTerm, UTerm, UTerm >> DotsMap
Type that stores for each rewritten DotsTerm the associated variable and the lower and upper bound...
Definition: term.hh:117
Definition: locatable.hh:51
Definition: aggregate.hh:108
virtual void unpool(UHeadAggrVec &x, bool beforeRewrite)=0
Unpool the aggregate and aggregate elements.
std::vector< UBodyAggr > UBodyAggrVec
Definition: aggregate.hh:51
virtual bool check(ChkLvlVec &lvl) const =0
std::vector< std::pair< VarTerm *, bool >> VarTermBoundVec
Definition: term.hh:106
std::function< void(Ground::ULitVec &, bool)> CreateLit
Definition: aggregate.hh:101
SC::EntNode * current
Definition: aggregate.hh:93
virtual void unpool(UBodyAggrVec &x, bool beforeRewrite)=0
Unpool the aggregate and aggregate elements.
ToGroundArg(unsigned &auxNames, PredDomMap &domains)
Definition: aggregate.cc:82
virtual bool isAssignment() const =0
VarMap vars
Definition: aggregate.hh:94
SC::VarNode & var(VarTerm &var)
Definition: aggregate.cc:56
Definition: term.hh:454
bool check()
Definition: aggregate.cc:61
Bound & bound
Definition: output.cc:122
UTermVec getGlobal(VarTermBoundVec const &vars)
Definition: aggregate.cc:89
std::pair< ULit, ULitVec > CondLit
Definition: aggregate.hh:41
virtual CreateHead toGround(ToGroundArg &x, bool external) const =0
virtual void rewriteArithmetics(Term::ArithmeticsMap &arith, Literal::AssignVec &assign, unsigned &auxNum)=0
virtual void removeAssignment()=0
std::vector< CheckLevel > ChkLvlVec
Definition: aggregate.hh:96
std::function< Ground::UStm(Ground::ULitVec &&)> CreateStm
Definition: aggregate.hh:102
virtual void simplify(Projections &project, Term::DotsMap &dots, Term::ScriptMap &scripts, unsigned &auxNum)=0
virtual ~AssignLevel()
Definition: aggregate.cc:48
virtual void simplify(Projections &project, Term::DotsMap &dots, Term::ScriptMap &scripts, unsigned &auxNum)=0
UTerm assign
Definition: literals.cc:59
Definition: term.hh:53
std::unique_ptr< BodyAggregate > UBodyAggr
Definition: aggregate.hh:49
Definition: clonable.hh:29
Definition: aggregate.hh:61
std::unique_ptr< HeadAggregate > UHeadAggr
Definition: aggregate.hh:53
Definition: aggregate.hh:166
void add(VarTermBoundVec &vars)
Definition: aggregate.cc:29
virtual void replace(Defines &dx)=0
int x
Definition: utility.cc:65
std::vector< unsigned > vars
Definition: statements.cc:1282
Definition: unique_list.hh:157
Definition: aggregate.hh:78
~ToGroundArg()
Definition: aggregate.cc:103
std::tuple< UTermVec, ULit, ULitVec > HeadAggrElem
Definition: aggregate.hh:46
#define GRINGO_HASH(T)
Definition: hashable.hh:29
virtual CreateBody toGround(ToGroundArg &x, Ground::UStmVec &stms) const =0
void assignLevels()
Definition: aggregate.cc:36
virtual Value isEDB() const
Definition: aggregates.cc:725
PredDomMap & domains
Definition: aggregate.hh:122
SC dep
Definition: aggregate.hh:92
Location loc
Definition: aggregate.hh:90
std::vector< CreateBody > CreateBodyVec
Definition: aggregate.hh:105
std::vector< UTerm > UTermVec
Definition: statement.hh:57
~CheckLevel()
Definition: aggregate.cc:77
std::vector< std::tuple< UTerm, FWString, UTermVec >> ScriptMap
Definition: term.hh:118
std::list< AssignLevel > childs
Definition: aggregate.hh:70
UTerm newId(T const &x)
Definition: aggregate.hh:114
virtual bool hasPool(bool beforeRewrite) const =0
std::unique_ptr< DotsTerm > dots(UTerm &&left, UTerm &&right)
Definition: term_helper.hh:51
std::vector< UStm > UStmVec
Definition: statement.hh:35
virtual UHeadAggr rewriteAggregates(UBodyAggrVec &aggr)=0
std::vector< CreateStm > CreateStmVec
Definition: aggregate.hh:103
virtual bool hasPool(bool beforeRewrite) const =0
bool operator<(Ent const &) const
Definition: aggregate.cc:53
virtual void assignLevels(AssignLevel &lvl)=0