SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Term.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Attribute.h"
4 #include "Common.h"
5 #include "Lexicon.h"
6 #include "Identifier.h"
7 #include "SExpression.h"
8 #include "Sort.h"
9 #include "theories/Theories.h"
10 #include <boost/spirit/home/qi/nonterminal/grammar.hpp>
11 
12 namespace smtrat {
13 namespace parser {
14 
15 struct QualifiedIdentifierParser: public qi::grammar<Iterator, Identifier(), Skipper> {
16  QualifiedIdentifierParser(): QualifiedIdentifierParser::base_type(main, "qualified identifier") {
17  main =
18  identifier[qi::_val = qi::_1]
19  | (qi::lit("(") >> "as" >> identifier >> sort >> ")")[qi::_val = px::bind(&QualifiedIdentifierParser::checkQualification, px::ref(*this), qi::_1, qi::_2)]
20  ;
21  }
22 
23  Identifier checkQualification(const Identifier& identifier, const carl::Sort&) const {
24  ///@todo Check what can be checked here.
25  SMTRAT_LOG_DEBUG("smtrat.parser", "Qualified identifier: " << identifier << " with sort " << sort);
26  return identifier;
27  }
28 
32 };
33 
34 struct SortedVariableParser: public qi::grammar<Iterator, std::pair<std::string, carl::Sort>(), Skipper> {
35  SortedVariableParser(): SortedVariableParser::base_type(main, "sorted variable") {
36  main = qi::lit("(") >> symbol >> sort >> ")" ;
37  }
40  qi::rule<Iterator, std::pair<std::string, carl::Sort>(), Skipper> main;
41 };
42 
43 struct TermParser: public qi::grammar<Iterator, types::TermType(), Skipper> {
46  main =
47  specconstant[qi::_val = px::bind(&Converter::template convert<types::ConstType>, &converter, qi::_1)]
48  | (qi::lit("(") >> "forall" >> "(" >> (+sortedvariable)[px::bind(&TermParser::declareQuantifiedVariable, px::ref(*this), qi::_1)] >> ")" >> main >> ")")[qi::_val = px::bind(&Theories::quantifiedTerm, px::ref(*theories), qi::_1, qi::_2, true)]
49  | (qi::lit("(") >> "exists" >> "(" >> (+sortedvariable)[px::bind(&TermParser::declareQuantifiedVariable, px::ref(*this), qi::_1)] >> ")" >> main >> ")")[qi::_val = px::bind(&Theories::quantifiedTerm, px::ref(*theories), qi::_1, qi::_2, false)]
50  | qualifiedidentifier[qi::_val = px::bind(&Theories::resolveSymbol, px::ref(*theories), qi::_1)]
51  | (qi::lit("(") >> termop >> ")")[qi::_val = qi::_1]
52  ;
53  termop =
54  (qi::lit("!") >> main >> +attribute)[qi::_val = px::bind(&Theories::annotateTerm, px::ref(*theories), qi::_1, qi::_2)]
55  | (qi::lit("let")[px::bind(&Theories::pushExpressionScope, px::ref(*theories), 1)] >> "(" >> +binding >> ")" >> main[qi::_val = qi::_1])[px::bind(&Theories::popExpressionScope, px::ref(*theories), 1)]
56  | (qualifiedidentifier >> +main)[qi::_val = px::bind(&Theories::functionCall, px::ref(*theories), qi::_1, qi::_2)]
57  ;
58  binding = (qi::lit("(") >> symbol >> main >> ")")[px::bind(&Theories::handleLet, px::ref(*theories), qi::_1, qi::_2)];
59  }
60 
61  void declareQuantifiedVariable(const std::vector<std::pair<std::string, carl::Sort>>& vars) {
62  SMTRAT_LOG_DEBUG("smtrat.parser", "Declare quantified variables: " << vars);
63  for(const auto& var: vars) {
64  //TODO: It might be that the variable has been declared before -> is this the right way to handle this?
65  if(theories->isVariableDeclared(var.first)) continue ;
66  theories->declareVariable(var.first, var.second);
67  }
68  }
69 
77  qi::rule<Iterator, Skipper> binding;
80 };
81 
82 }
83 }
int var(Lit p)
Definition: SolverTypes.h:64
carl::mpl_variant_of< TermTypes >::type TermType
Variant type for all terms.
Definition: TheoryTypes.h:160
PositionIteratorType Iterator
Definition: Common.h:49
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
Class to create the formulas for axioms.
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
Identifier checkQualification(const Identifier &identifier, const carl::Sort &) const
Definition: Term.h:23
qi::rule< Iterator, Identifier(), Skipper > main
Definition: Term.h:31
qi::rule< Iterator, std::pair< std::string, carl::Sort >), Skipper > main
Definition: Term.h:40
Converter converter
Definition: Term.h:76
Theories * theories
Definition: Term.h:70
SpecConstantParser specconstant
Definition: Term.h:72
AttributeParser attribute
Definition: Term.h:75
conversion::VariantVariantConverter< types::TermType > Converter
Definition: Term.h:44
qi::rule< Iterator, types::TermType(), Skipper > termop
Definition: Term.h:78
qi::rule< Iterator, Skipper > binding
Definition: Term.h:77
QualifiedIdentifierParser qualifiedidentifier
Definition: Term.h:73
qi::rule< Iterator, types::TermType(), Skipper > main
Definition: Term.h:79
TermParser(Theories *theories)
Definition: Term.h:45
SortedVariableParser sortedvariable
Definition: Term.h:74
void declareQuantifiedVariable(const std::vector< std::pair< std::string, carl::Sort >> &vars)
Definition: Term.h:61
SymbolParser symbol
Definition: Term.h:71
The Theories class combines all implemented theories and provides a single interface to interact with...
Definition: Theories.h:28
void handleLet(const std::string &symbol, const types::TermType &term)
Definition: Theories.h:237
types::TermType functionCall(const Identifier &identifier, const std::vector< types::TermType > &arguments)
Definition: Theories.h:337
types::TermType resolveSymbol(const Identifier &identifier) const
Definition: Theories.h:174
void declareVariable(const std::string &name, const carl::Sort &sort)
Definition: Theories.h:108
const auto & annotateTerm(const types::TermType &term, const std::vector< Attribute > &attributes)
Definition: Theories.h:213
void popExpressionScope(std::size_t n)
Definition: Theories.h:203
types::TermType quantifiedTerm(const std::vector< std::pair< std::string, carl::Sort >> &vars, const types::TermType &term, bool universal)
Definition: Theories.h:412
bool isVariableDeclared(const std::string &name) const
Definition: Theories.h:425
void pushExpressionScope(std::size_t n)
Definition: Theories.h:200