Dukales

mixed partial derivative

Nov 28th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.10 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <insituc/transform/derivator/intrinsics.hpp>
  4.  
  5. #include <insituc/base_types.hpp>
  6. #include <insituc/ast/ast.hpp>
  7. #include <insituc/ast/compare.hpp>
  8. #include <insituc/utility/reversed_container.hpp>
  9.  
  10. #include <deque>
  11. #include <stack>
  12. #include <algorithm>
  13. #include <iterator>
  14. #include <functional>
  15. #include <exception>
  16. #include <stdexcept>
  17. #include <map>
  18. #include <limits>
  19. #include <experimental/optional>
  20.  
  21. namespace insituc
  22. {
  23. namespace transform
  24. {
  25.  
  26. struct derivator
  27. {
  28.  
  29.     derivator()
  30.         : derive_statement_(*this)
  31.         , derive_expression_(*this)
  32.         , derive_intrinsic_()
  33.     { ; }
  34.  
  35. private :
  36.  
  37.     struct context
  38.     {
  39.  
  40.         struct trailing_wrt
  41.         {
  42.  
  43.             size_type block_;
  44.             size_type offset_;
  45.  
  46.         };
  47.  
  48.         using tail_type = std::deque< trailing_wrt >;
  49.         using head_type = std::deque< tail_type >;
  50.  
  51.         ast::symbols wrts_;
  52.  
  53.         head_type wrts_blocks_;
  54.         std::deque< size_type > wrts_offsets_;
  55.  
  56.         context() = default;
  57.  
  58.         context(ast::symbols const & _wrts)
  59.         {
  60.             push(_wrts);
  61.         }
  62.  
  63.         size_type
  64.         offset(trailing_wrt const & _trailing_wrt) const
  65.         {
  66.             if (_trailing_wrt.block_ == std::numeric_limits< size_type >::max()) {
  67.                 assert(_trailing_wrt.offset_ == 0);
  68.                 return 0;
  69.             } else {
  70.                 return wrts_offsets_[_trailing_wrt.block_] + _trailing_wrt.offset_;
  71.             }
  72.         }
  73.  
  74.         size_type
  75.         prev_arity() const
  76.         {
  77.             if (wrts_offsets_.empty()) {
  78.                 return 1;
  79.             } else {
  80.                 return wrts_offsets_.back();
  81.             }
  82.         }
  83.  
  84.         size_type
  85.         arity() const
  86.         {
  87.             if (wrts_offsets_.empty()) {
  88.                 return 1;
  89.             } else {
  90.                 return wrts_offsets_.back() + wrts_blocks_.back().size();
  91.             }
  92.         }
  93.  
  94.         ast::symbol const &
  95.         wrt() const
  96.         {
  97.             assert(!wrts_.empty());
  98.             return wrts_.back();
  99.         }
  100.  
  101.         void
  102.         push(ast::symbol const & _wrt)
  103.         {
  104.             assert(wrts_.size() == wrts_blocks_.size());
  105.             assert(wrts_offsets_.size() == wrts_blocks_.size());
  106.             wrts_offsets_.push_back(arity());
  107.             wrts_blocks_.push_back(tail(wrts_blocks_, _wrt));
  108.             wrts_.push_back(_wrt);
  109.         }
  110.  
  111.         void
  112.         push(ast::symbol && _wrt)
  113.         {
  114.             assert(wrts_.size() == wrts_blocks_.size());
  115.             assert(wrts_offsets_.size() == wrts_blocks_.size());
  116.             wrts_offsets_.push_back(arity());
  117.             wrts_blocks_.push_back(tail(wrts_blocks_, _wrt));
  118.             wrts_.push_back(std::move(_wrt));
  119.         }
  120.  
  121.         void
  122.         push(ast::symbols const & _wrts)
  123.         {
  124.             for (ast::symbol const & wrt_ : _wrts) {
  125.                 push(wrt_);
  126.             }
  127.         }
  128.  
  129.         void
  130.         pop()
  131.         {
  132.             assert(!wrts_.empty());
  133.             assert(wrts_.size() == wrts_blocks_.size());
  134.             assert(wrts_offsets_.size() == wrts_blocks_.size());
  135.             wrts_.pop_back();
  136.             wrts_offsets_.pop_back();
  137.             wrts_blocks_.pop_back();
  138.         }
  139.  
  140.         void
  141.         clear()
  142.         {
  143.             wrts_.clear();
  144.             wrts_offsets_.clear();
  145.             wrts_blocks_.clear();
  146.         }
  147.  
  148.     private :
  149.  
  150.         tail_type
  151.         tail(head_type const & _head, ast::symbol const & _wrt) const
  152.         {
  153.             tail_type tail_;
  154.             size_type block_ = _head.size();
  155.             while (0 < block_) {
  156.                 --block_;
  157.                 size_type offset_ = _head[block_].size();
  158.                 while (0 < offset_) {
  159.                     --offset_;
  160.                     tail_.push_front({block_, offset_});
  161.                 }
  162.                 if (_wrt == wrts_[block_]) {
  163.                     return tail_;
  164.                 }
  165.             }
  166.             tail_.push_front({std::numeric_limits< size_type >::max(), 0});
  167.             return tail_;
  168.         }
  169.  
  170.     };
  171.  
  172.     context external_context_;
  173.     std::deque< std::reference_wrapper< ast::symbol const > > local_variables_;
  174.     std::deque< ast::identifier > callies_; // should die-in-use or be cleared after each derive_function call by user code
  175.  
  176.     ast::entry_definition
  177.     derive_function(ast::entry_definition const & _entry_definition)
  178.     try {
  179.         assert(local_variables_.empty());
  180.         ast::entry_definition entry_definition_;
  181.         entry_definition_.entry_name_ = _entry_definition.entry_name_;
  182.         entry_definition_.entry_name_.derive(external_context_.wrt());
  183.         if (!_entry_definition.argument_list_.lvalues_.empty()) { // if not nullary
  184.             entry_definition_.argument_list_.lvalues_ = derive_lvalues(_entry_definition.argument_list_.lvalues_);
  185.         }
  186.         entry_definition_.body_.statements_ = derive_statements(_entry_definition.body_.statements_);
  187.         local_variables_.clear();
  188.         return entry_definition_;
  189.     } catch (std::exception const &) {
  190.         local_variables_.clear();
  191.         throw;
  192.     }
  193.  
  194.     ast::lvalues
  195.     derive_lvalues(ast::lvalues const & _lvalues)
  196.     {
  197.         assert(!_lvalues.empty());
  198.         ast::lvalues lvalues_ = _lvalues;
  199.         assert((lvalues_.size() % external_context_.prev_arity()) == 0);
  200.         size_type const arity_ = lvalues_.size() / external_context_.prev_arity();
  201.         for (size_type i = 0; i < arity_; ++i) {
  202.             ast::identifier const & lvalue_ = _lvalues[i];
  203.             assert(lvalue_.is_original());
  204.             local_variables_.emplace_back(lvalue_.symbol_);
  205.         }
  206.         for (typename context::trailing_wrt const & position_ : external_context_.wrts_blocks_.back()) {
  207.             size_type const offset_ = arity_ * external_context_.offset(position_);
  208.             for (size_type i = 0; i < arity_; ++i) {
  209.                 ast::identifier const & lvalue_ = _lvalues[offset_ + i];
  210.                 lvalues_.push_back(lvalue_);
  211.                 lvalues_.back().derive(external_context_.wrt());
  212.             }
  213.         }
  214.         assert(!lvalues_[arity_].is_original());
  215.         return lvalues_;
  216.     }
  217.  
  218.     ast::rvalues
  219.     derive_rvalues(ast::rvalues const & _rvalues, context const & _context)
  220.     {
  221.         assert(!_rvalues.empty());
  222.         ast::rvalues rvalues_ = _rvalues;
  223.         assert((rvalues_.size() % _context.prev_arity()) == 0);
  224.         size_type const arity_ = rvalues_.size() / _context.prev_arity();
  225.         for (typename context::trailing_wrt const & position_ : _context.wrts_blocks_.back()) {
  226.             size_type const offset_ = arity_ * _context.offset(position_);
  227.             for (size_type i = 0; i < arity_; ++i) {
  228.                 ast::rvalue const & rvalue_ = _rvalues[offset_ + i];
  229.                 rvalues_.push_back(derive_operand(rvalue_));
  230.             }
  231.         }
  232.         return rvalues_;
  233.     }
  234.  
  235.     ast::rvalues
  236.     derive_rvalues(ast::rvalues const & _rvalues)
  237.     {
  238.         return derive_rvalues(_rvalues, external_context_);
  239.     }
  240.  
  241.     struct derivator_of_statement
  242.     {
  243.  
  244.         using result_type = ast::statement;
  245.  
  246.         derivator_of_statement(derivator & _derivator)
  247.             : derivator_(_derivator)
  248.         { ; }
  249.  
  250.         template< typename type >
  251.         result_type
  252.         operator () (type const & _value) const
  253.         {
  254.             return derivator_.derive_statement(_value);
  255.         }
  256.  
  257.     private :
  258.  
  259.         derivator & derivator_;
  260.  
  261.     } const derive_statement_;
  262.  
  263.     typename derivator_of_statement::result_type
  264.     derive_statement(ast::empty const &)
  265.     {
  266.         throw std::runtime_error("empty statement not allowed");
  267.     }
  268.  
  269.     typename derivator_of_statement::result_type
  270.     derive_statement(ast::statement const & _statement)
  271.     {
  272.         return _statement.apply_visitor(derive_statement_);
  273.     }
  274.  
  275.     typename derivator_of_statement::result_type
  276.     derive_statement(ast::variable_declaration const & _variable_declaration)
  277.     {
  278.         ast::variable_declaration variable_declaration_;
  279.         variable_declaration_.lhs_.lvalues_ = derive_lvalues(_variable_declaration.lhs_.lvalues_);
  280.         variable_declaration_.rhs_.rvalues_ = derive_rvalues(_variable_declaration.rhs_.rvalues_);
  281.         return std::move(variable_declaration_);
  282.     }
  283.  
  284.     typename derivator_of_statement::result_type
  285.     derive_statement(ast::return_statement const & _return_statement)
  286.     {
  287.         ast::return_statement return_statement_;
  288.         for (ast::rvalue const & rvalue_ : _return_statement.return_value_.rvalues_) { // piecewise
  289.             return_statement_.return_value_.rvalues_.push_back(derive_operand(rvalue_));
  290.         }
  291.         return std::move(return_statement_);
  292.     }
  293.  
  294.     typename derivator_of_statement::result_type
  295.     derive_statement(ast::assignment const &)
  296.     {
  297.         throw std::runtime_error("can't infer a simple analitycal representation for the derivative of an assignment statement");
  298.     }
  299.  
  300.     typename derivator_of_statement::result_type
  301.     derive_statement(ast::statement_block const & _statement_block)
  302.     {
  303.         return ast::statement_block{derive_statements(_statement_block.statements_)};
  304.     }
  305.  
  306.     ast::statements
  307.     derive_statements(ast::statements const & _statements)
  308.     {
  309.         size_type const frame_used_ = local_variables_.size();
  310.         ast::statements statements_;
  311.         for (ast::statement const & statement_ : _statements) {
  312.             statements_.push_back(derive_statement(statement_));
  313.         }
  314.         assert(!(local_variables_.size() < frame_used_));
  315.         while (frame_used_ < local_variables_.size()) {
  316.             local_variables_.pop_back();
  317.         }
  318.         return statements_;
  319.     }
  320.  
  321.     struct derivator_of_expression
  322.     {
  323.  
  324.         using result_type = ast::operand;
  325.  
  326.         derivator_of_expression(derivator & _derivator)
  327.             : derivator_(_derivator)
  328.         { ; }
  329.  
  330.         template< typename type >
  331.         result_type
  332.         operator () (type const & _value) const
  333.         {
  334.             return derivator_.derive_expression(_value);
  335.         }
  336.  
  337.     private :
  338.  
  339.         derivator & derivator_;
  340.  
  341.     } const derive_expression_;
  342.  
  343.     typename derivator_of_expression::result_type
  344.     derive_operand(ast::operand const & _operand)
  345.     {
  346.         return _operand.apply_visitor(derive_expression_);
  347.     }
  348.  
  349.     typename derivator_of_expression::result_type
  350.     derive_expression(ast::empty const &)
  351.     {
  352.         throw std::runtime_error("empty expression cannot be derived");
  353.     }
  354.  
  355.     typename derivator_of_expression::result_type
  356.     derive_expression(G const &)
  357.     {
  358.         return ast::constant::zero;
  359.     }
  360.  
  361.     typename derivator_of_expression::result_type
  362.     derive_expression(ast::constant const &)
  363.     {
  364.         return ast::constant::zero;
  365.     }
  366.  
  367.     typename derivator_of_expression::result_type
  368.     derive_expression(ast::intrinsic_invocation const & _intrinsic_invocation)
  369.     {
  370.         ast::rvalues darguments_;
  371.         for (ast::rvalue const & rvalue_ : _intrinsic_invocation.argument_list_.rvalues_) { // piecewise
  372.             darguments_.push_back(derive_operand(rvalue_));
  373.         }
  374.         return ast::rvalue_list{derive_intrinsic_(_intrinsic_invocation.intrinsic_, _intrinsic_invocation.argument_list_.rvalues_, std::move(darguments_))};
  375.     }
  376.  
  377.     typename derivator_of_expression::result_type
  378.     derive_expression(ast::entry_substitution const & _entry_substitution)
  379.     {
  380.         ast::entry_substitution entry_substitution_;
  381.         entry_substitution_.entry_name_ = _entry_substitution.entry_name_;
  382.         entry_substitution_.entry_name_.derive(external_context_.wrt());
  383.         callies_.push_back(entry_substitution_.entry_name_);
  384.         if (!_entry_substitution.argument_list_.rvalues_.empty()) {
  385.             context context_(entry_substitution_.entry_name_.wrts_);
  386.             entry_substitution_.argument_list_.rvalues_ = derive_rvalues(_entry_substitution.argument_list_.rvalues_, context_);
  387.         }
  388.         return entry_substitution_;
  389.     }
  390.  
  391.     typename derivator_of_expression::result_type
  392.     derive_expression(ast::identifier const & _identifier)
  393.     {
  394.         ast::symbol const & wrt_ = external_context_.wrt();
  395.         auto const lrend = std::crend(local_variables_);
  396.         if (std::find(std::crbegin(local_variables_), lrend, _identifier.symbol_) != lrend) {
  397.             ast::identifier identifier_ = _identifier;
  398.             identifier_.derive(wrt_);
  399.             return std::move(identifier_);
  400.         } else {
  401.             assert(_identifier.is_original());
  402.             if (_identifier.symbol_ == wrt_)  {
  403.                 return ast::constant::one;
  404.             } else {
  405.                 return ast::constant::zero;
  406.             }
  407.         }
  408.     }
  409.  
  410.     typename derivator_of_expression::result_type
  411.     derive_expression(ast::unary_expression const & _unary_expression)
  412.     {
  413.         switch (_unary_expression.operator_) {
  414.         case ast::unary::plus : {
  415.             return derive_operand(_unary_expression.operand_);
  416.         }
  417.         case ast::unary::minus : {
  418.             return ast::unary_expression{ast::unary::minus, derive_operand(_unary_expression.operand_)};
  419.         }
  420.         }
  421.     }
  422.  
  423.     typename derivator_of_expression::result_type
  424.     derive_binary(ast::operand_cref const & _lhs,
  425.                   ast::operand && _dlhs,
  426.                   ast::binary const _operator,
  427.                   ast::operand_cref const & _rhs,
  428.                   ast::operand && _drhs) const
  429.     {
  430.         using B = ast::binary_expression;
  431.         using I = ast::intrinsic_invocation;
  432.         switch (_operator) {
  433.         case ast::binary::add : {
  434.             return B{std::move(_dlhs), ast::binary::add, std::move(_drhs)};
  435.         }
  436.         case ast::binary::sub : {
  437.             return B{std::move(_dlhs), ast::binary::sub, std::move(_drhs)};
  438.         }
  439.         case ast::binary::mul : {
  440.             return B{B{std::move(_dlhs), ast::binary::mul, _rhs}, ast::binary::add, B{_lhs, ast::binary::mul, std::move(_drhs)}};
  441.         }
  442.         case ast::binary::div : {
  443.             return B{B{std::move(_dlhs), ast::binary::div, _rhs}, ast::binary::sub, B{B{_lhs, ast::binary::mul, std::move(_drhs)}, ast::binary::div, I{ast::intrinsic::sqr, ast::make_rvalue_list(_rhs)}}};
  444.         }
  445.         case ast::binary::mod : { // http://math.stackexchange.com/questions/672610/
  446.             return B{std::move(_dlhs), ast::binary::sub, B{std::move(_drhs), ast::binary::mul, I{ast::intrinsic::trunc, ast::make_rvalue_list(B{_lhs, ast::binary::div, _rhs})}}};
  447.         }
  448.         case ast::binary::pow : { // functional power rule
  449.             return B{B{_lhs, ast::binary::pow, _rhs}, ast::binary::mul, B{B{std::move(_dlhs), ast::binary::mul, B{_rhs, ast::binary::div, _lhs}}, ast::binary::add, B{std::move(_drhs), ast::binary::mul, I{ast::intrinsic::ln, ast::make_rvalue_list(_lhs)}}}};
  450.         }
  451.         }
  452.     }
  453.  
  454.     typename derivator_of_expression::result_type
  455.     derive_expression(ast::expression const & _expression)
  456.     {
  457.         if (_expression.rest_.empty()) {
  458.             return derive_operand(_expression.first_);
  459.         } else if (_expression.rest_.size() == 1) {
  460.             ast::operation const & rhs_ = _expression.rest_.back();
  461.             return derive_binary(std::cref(_expression.first_),
  462.                                  derive_operand(_expression.first_),
  463.                                  rhs_.operator_,
  464.                                  std::cref(rhs_.operand_),
  465.                                  derive_operand(rhs_.operand_));
  466.         } else {
  467.             throw std::logic_error("transform to binary tree first");
  468.         }
  469.     }
  470.  
  471.     typename derivator_of_expression::result_type
  472.     derive_expression(ast::binary_expression const & _binary_expression)
  473.     {
  474.         return derive_binary(std::cref(_binary_expression.lhs_),
  475.                              derive_operand(_binary_expression.lhs_),
  476.                              _binary_expression.operator_,
  477.                              std::cref(_binary_expression.rhs_),
  478.                              derive_operand(_binary_expression.rhs_));
  479.     }
  480.  
  481.     typename derivator_of_expression::result_type
  482.     derive_expression(ast::rvalue_list const & _rvalue_list)
  483.     {
  484.         ast::rvalue_list rvalue_list_;
  485.         for (ast::rvalue const & rvalue_ : _rvalue_list.rvalues_) {
  486.             rvalue_list_.rvalues_.push_back(derive_operand(rvalue_));
  487.         }
  488.         return std::move(rvalue_list_);
  489.     }
  490.  
  491.     typename derivator_of_expression::result_type
  492.     derive_expression(ast::operand_cref const & _operand_cref)
  493.     {
  494.         return derive_operand(_operand_cref.get());
  495.     }
  496.  
  497.     derivator_of_intrinsic const derive_intrinsic_;
  498.  
  499. public :
  500.  
  501.     struct incremental
  502.     {
  503.  
  504.         incremental(derivator & _derivator)
  505.             : derivator_(_derivator)
  506.         { ; }
  507.  
  508.         void
  509.         enter()
  510.         {
  511.             assert(derivatives_.empty());
  512.         }
  513.  
  514.         boolean_type
  515.         push(ast::program const & _primitive, ast::symbol const & _wrt)
  516.         {
  517.             if (derivator_.external_context_.wrts_.empty()) {
  518.                 ancestors_.emplace(_primitive);
  519.             } else {
  520.                 ancestors_.emplace(derivatives_.back());
  521.             }
  522.             derivator_.external_context_.push(_wrt);
  523.             derivatives_.emplace_back();
  524.             return true;
  525.         }
  526.  
  527.         boolean_type
  528.         append(ast::symbol const & _wrt)
  529.         {
  530.             derivator_.external_context_.pop();
  531.             derivator_.external_context_.push(_wrt);
  532.             derivatives_.emplace_back();
  533.             return true;
  534.         }
  535.  
  536.         void
  537.         pop()
  538.         {
  539.             derivator_.external_context_.pop();
  540.             assert(!ancestors_.empty());
  541.             ancestors_.pop();
  542.         }
  543.  
  544.         template< typename filter >
  545.         boolean_type
  546.         derive(filter && _filter)
  547.         {
  548.             ast::entries & entries_ = derivatives_.back().entries_;
  549.             for (ast::entry_definition const & entry_definition_ : ancestors_.top().get().entries_) {
  550.                 try {
  551.                     entries_.push_back(std::forward< filter >(_filter)(derivator_.derive_function(entry_definition_)));
  552.                 } catch (std::exception const &) {
  553.                     derivator_.callies_.clear();
  554.                     throw;
  555.                 }
  556.                 derivator_.callies_.clear();
  557.             }
  558.             return true;
  559.         }
  560.  
  561.         ast::program
  562.         leave()
  563.         {
  564.             ast::program result_;
  565.             for (ast::program & derivative_ : derivatives_) {
  566.                 result_.entries_.splice(std::cend(result_.entries_), std::move(derivative_.entries_));
  567.             }
  568.             derivatives_.clear();
  569.             assert(ancestors_.empty());
  570.             return result_;
  571.         }
  572.  
  573.     private :
  574.  
  575.         derivator & derivator_;
  576.  
  577.         using program_cref = std::reference_wrapper< ast::program const >;
  578.  
  579.         std::deque< ast::program > derivatives_;
  580.         std::stack< program_cref const > ancestors_;
  581.  
  582.     };
  583.  
  584.     struct dependent_recursive
  585.     {
  586.  
  587.         dependent_recursive(derivator & _derivator)
  588.             : derivator_(_derivator)
  589.         { ; }
  590.  
  591.         ast::program derivatives_;
  592.  
  593.     private :
  594.  
  595.         derivator & derivator_;
  596.  
  597.         struct is_function_name
  598.         {
  599.  
  600.             boolean_type
  601.             operator () (ast::entry_definition const & _entry_definition) const
  602.             {
  603.                 return (symbol_ == _entry_definition.entry_name_.symbol_) && (wrts_ == _entry_definition.entry_name_.wrts_);
  604.             }
  605.  
  606.             ast::symbol const & symbol_;
  607.             ast::symbols const & wrts_;
  608.  
  609.         };
  610.  
  611.         struct empty {};
  612.  
  613.         using entry_definition_cref = std::reference_wrapper< ast::entry_definition const >;
  614.         using entry_definition_opt_cref = std::experimental::optional< entry_definition_cref >;
  615.  
  616.         template< typename filter >
  617.         entry_definition_opt_cref
  618.         derive(ast::program const & _primitives, ast::symbol const & _symbol, ast::symbols && _wrts, filter && _filter)
  619.         {
  620.             is_function_name const is_function_name_{_symbol, _wrts};
  621.             if (_wrts.empty()) {
  622.                 assert(derivator_.external_context_.wrts_.empty());
  623.                 for (ast::entry_definition const & primitive_ : _primitives.entries_) {
  624.                     if (is_function_name_(primitive_)) {
  625.                         return std::cref(primitive_);
  626.                     }
  627.                 }
  628.                 return {};
  629.             }
  630.             for (ast::entry_definition const & primitive_ : reverse(derivatives_.entries_)) {
  631.                 if (is_function_name_(primitive_)) {
  632.                     derivator_.external_context_.push(primitive_.entry_name_.wrts_);
  633.                     return std::cref(primitive_);
  634.                 }
  635.             }
  636.             ast::symbol wrt_ = std::move(_wrts.back());
  637.             _wrts.pop_back();
  638.             context context_ = std::move(derivator_.external_context_);
  639.             entry_definition_opt_cref const primitive_ = derive(_primitives, _symbol, std::move(_wrts), std::forward< filter >(_filter));
  640.             derivator_.external_context_ = std::move(context_);
  641.             if (!primitive_) {
  642.                 return {};
  643.             }
  644.             derivator_.external_context_.push(std::move(wrt_));
  645.             ast::entry_definition derivative_ = std::forward< filter >(_filter)(derivator_.derive_function(*primitive_));
  646.             { // callies first
  647.                 context_ = std::move(derivator_.external_context_);
  648.                 auto callies_ = std::move(derivator_.callies_);
  649.                 for (ast::identifier & callee_ : callies_) {
  650.                     entry_definition_opt_cref const derivative_ = derive(_primitives, callee_.symbol_, ast::symbols(callee_.wrts_), std::forward< filter >(_filter));
  651.                     derivator_.external_context_.clear();
  652.                     if (!derivative_) {
  653.                         return {};
  654.                     }
  655.                 }
  656.                 derivator_.external_context_ = std::move(context_);
  657.             }
  658.             derivatives_.entries_.push_back(std::move(derivative_));
  659.             return std::cref(derivatives_.entries_.back());
  660.         }
  661.  
  662.     public :
  663.  
  664.         template< typename filter >
  665.         boolean_type
  666.         operator () (ast::program const & _primitives, ast::symbols const & _symbols, ast::symbols const & _wrts, filter && _filter)
  667.         {
  668.             assert(derivatives_.entries_.empty());
  669.             for (ast::symbol const & symbol_ : _symbols) {
  670.                 entry_definition_opt_cref const derivative_ = derive(_primitives, symbol_, ast::symbols(_wrts), std::forward< filter >(_filter));
  671.                 derivator_.external_context_.clear();
  672.                 if (!derivative_) {
  673.                     return false;
  674.                 }
  675.             }
  676.             return true;
  677.         }
  678.  
  679.     };
  680.  
  681.     void
  682.     clear()
  683.     {
  684.         external_context_.clear();
  685.         local_variables_.clear();
  686.         callies_.clear();
  687.     }
  688.  
  689. };
  690.  
  691. }
  692. }
Advertisement
Add Comment
Please, Sign In to add comment