map_some.hh

00001 // This file is a part of Aurelia.
00002 // Copyright (C) 2010  Valentin David
00003 // Copyright (C) 2010  University of Bergen
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #ifndef __MAP_SOME_HH
00018 # define __MAP_SOME_HH
00019 
00020 # include "../strategies.hh"
00021 # include "../generic/try.hh"
00022 # include "map.hh"
00023 
00024 namespace aurelia {
00025 
00026   template <typename S>
00027   struct map_some_strategy {
00028   private:
00029     S s;
00030 
00031   public:
00032     map_some_strategy(const S& s): s(s) {}
00033     map_some_strategy(const map_some_strategy& other): s(other.s) {}
00034 
00035     template <typename Pool>
00036     untyped_term<Pool> operator()(const untyped_term<Pool>& t) const {
00037       typedef typename strategy_concept<
00038         typename strategy_model<S, untyped_term<Pool> >::model>
00039         ::check require;
00040 
00041       typedef untyped_term<Pool> T;
00042       typedef untyped_constructor<Pool> C;
00043 
00044       if (t.constructor() == C::AS_LIST) {
00045         try {
00046           T a = s(t[0]);
00047           return T(C::AS_LIST, (a, map(try_(s))(t[1])));
00048         }
00049         catch (failure) {
00050           T b = (*this)(t[1]);
00051           return T(C::AS_LIST, (t[0], b));
00052         }
00053       }
00054       throw failure();
00055     }
00056   };
00057 
00061   template <typename S>
00062   map_some_strategy<S> map_some(const S& s) {
00063     return map_some_strategy<S>(s);
00064   }
00065 
00066 }
00067 
00068 #endif