/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ #ifndef tuscany_monad_hpp #define tuscany_monad_hpp /** * Simple monad implementations. */ #include #include #include "function.hpp" namespace tuscany { /** * Identity monad. Just wraps a value. * To get the value in the monad, just cast it to the value type. */ template class id { public: id(const V& v) : v(v) { } operator const V() const { return v; } const id& operator=(const id& m) { if(this == &m) return *this; v = m.v; return *this; } const bool operator!=(const id& m) const { return !this->operator==(m); } const bool operator==(const id& m) const { if (&m == this) return true; return v == m.v; } private: const V v; }; /** * Write an identity monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const id& m) { out << (V)m; return out; } /** * Return an identity monad from a value. */ template const id mkunit(const V& v) { return id(v); } template const lambda(V)> unit() { return mkunit; } /** * Bind a function to an identity monad. Pass the value in the monad to the function. */ template const id operator>>(const id& m, const lambda(V)>& f) { return f(m); } template const id operator>>(const id& m, const id (* const f)(const V)) { return f(m); } /** * Maybe monad. Used to represent an optional value, which may be there or not. * To get the value in the monad, just cast it to the value type. */ template class maybe { public: maybe(const V& v) : hasv(true), v(v) { } maybe() : hasv(false) { } operator const V() const { return v; } const maybe& operator=(const maybe& m) { if(this == &m) return *this; hasv = m.hasv; if (hasv) v = m.v; return *this; } const bool operator!=(const maybe& m) const { return !this->operator==(m); } const bool operator==(const maybe& m) const { if (this == &m) return true; if (!hasv) return !m.hasv; return m.hasv && v == m.v; } private: const bool hasv; V v; template friend const bool hasValue(const maybe& m); }; /** * Write a maybe monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const maybe& m) { if (!hasValue(m)) { out << "nothing"; return out; } out << (V)m; return out; } /** * Return a maybe monad with a value in it. */ template const maybe mkjust(const V& v) { return maybe(v); } template const lambda(V)> just() { return mkjust; } /** * Returns true if the monad contains a value. */ template const bool hasValue(const maybe& m) { return m.hasv; } /** * Bind a function to a maybe monad. Passes the value in the monad to the function * if present, or does nothing if there's no value. */ template const maybe operator>>(const maybe& m, const lambda(V)>& f) { if (!hasValue(m)) return m; return f(m); } template const maybe operator>>(const maybe& m, const maybe (* const f)(const V)) { if (!hasValue(m)) return m; return f(m); } /** * Failable monad. Used to represent either a success value or a failure. * To get the value in the monad, just cast it to the value type. * To get the failure in the monad, cast it to the failure type. */ template class failable { public: failable() : hasv(false) { } failable(const V& v) : hasv(true), v(v) { } failable(const failable& m) : hasv(m.hasv) { if (hasv) v = m.v; else f = m.f; } operator const V() const { return v; } const failable& operator=(const failable& m) { if(this == &m) return *this; hasv = m.hasv; if (hasv) v = m.v; else f = m.f; return *this; } const bool operator!=(const failable& m) const { return !this->operator==(m); } const bool operator==(const failable& m) const { if (this == &m) return true; if (!hasv) return !m.hasv && f == m.f; return m.hasv && v == m.v; } private: bool hasv; V v; F f; failable(const bool hasv, const F& f) : hasv(hasv), f(f) { } template friend const bool hasValue(const failable& m); template friend const B reason(const failable& m); template friend const failable mkfailure(const B& f); }; /** * Write a failable monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const failable& m) { if (!hasValue(m)) { out << reason(m); return out; } const V v = m; out << v; return out; } /** * Returns a failable monad with a success value in it. */ template const failable mksuccess(const V& v) { return failable(v); } template const lambda(V)> success() { return mksuccess; } /** * Returns a failable monad with a failure in it. */ template const failable mkfailure(const F& f) { return failable(false, f); } template const lambda(V)> failure() { return mkfailure; } /** * Returns true if the monad contains a value. */ template const bool hasValue(const failable& m) { return m.hasv; } /** * Returns the reason for failure of a failable monad. */ template const F reason(const failable& m) { return m.f; } /** * Bind a function to a failable monad. Passes the success value in the monad to the function * if present, or does nothing if there's no value and a failure instead. */ template const failable operator>>(const failable& m, const lambda(V)>& f) { if (!hasValue(m)) return m; return f(m); } template const failable operator>>(const failable& m, const failable (* const f)(const V)) { if (!hasValue(m)) return m; return f(m); } /** * State + value pair data type used by the state monad. */ template class svp { public: svp(const S& s, const V& v) : s(s), v(v) { } operator const S() const { return s; } operator const V() const { return v; } const svp& operator=(const svp& p) { if(this == &p) return *this; s = p.s; v = p.v; return *this; } const bool operator!=(const svp& p) const { return !this->operator==(p); } const bool operator==(const svp& p) const { if (this == &p) return true; return s == p.s && v == p.v; } private: const S s; const V v; }; /** * State monad. Used to represent the combination of a state and a value. * To get the state in the monad, just cast it to the state type. * To get the value in the monad, just cast it to the value type. */ template class state { public: state(const lambda(S)>& f) : f(f) { } const svp operator()(const S& s) const { return f(s); } const state& operator=(const state& m) { if(this == &m) return *this; f = m.f; return *this; } const bool operator!=(const state& m) const { return !this->operator==(m); } const bool operator==(const state& m) const { if (this == &m) return true; return f == m.f; } private: const lambda(S)> f; }; /** * Write a state monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const state& m) { const S s = m; const V v = m; out << '(' << s << ' ' << v << ')'; return out; } /** * Return a state monad carrying a result value. */ template struct returnState { const V v; returnState(const V& v) : v(v) { } const svp operator()(const S& s) const { return svp(s, v); } }; template const state result(const V& v) { return state(returnState(v)); } /** * Return a state monad with a transformer function. * A transformer function takes a state and returns an svp pair carrying a value and a * new (transformed) state. */ template const state transformer(const lambda(S)>& f) { return state(f); } /** * Bind a function to a state monad. The function takes a value and returns a state * monad carrying a return value. */ template struct stateBind { const state st; const lambda(A)>f; stateBind(const state& st, const lambda(A)>& f) : st(st), f(f) { } const svp operator()(const S& is) const { const svp iscp = st(is); const state m = f((A)iscp); return m((S)iscp); } }; template const state operator>>(const state& st, const lambda(A)>& f) { return state(stateBind(st, f)); } template const state operator>>(const state& st, const state (* const f)(const A)) { return state(stateBind(st, f)); } } #endif /* tuscany_monad_hpp */