-------------------------------------------------------------------------------- -- $Id: AccumulateM.hs,v 1.2 2003/09/24 18:50:52 graham Exp $ -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : AccummulateM -- Copyright : (c) 2003, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- This module defines a monadic accumulator type. The plan is that it be -- used in conjunction with FunctorM and similar constructs to accumulate -- some or all of the values visited. -- -------------------------------------------------------------------------------- module Accumulator ( Accumulator(..) ) where import Monad ( foldM ) {- class (Monad m) => MonadAccum m c e where seedVal :: m c growVal :: c -> e -> m c reapVal :: m c -> c -} data Accumulator c = Accumulator c deriving Show instance Monad Accumulator where (Accumulator v) >>= k = k v return v = Accumulator v instance MonadAccum Accumulator Int Int where seedVal = Accumulator 0 growVal n m = Accumulator (n+m) reapVal (Accumulator n) = n {- instance MonadAccum Accumulator Integer Integer where seedVal = Accumulator 0 growVal n m = Accumulator (n+m) reapVal (Accumulator n) = n -} {- instance MonadAccum Accumulator [v] v where seedVal = Accumulator [] growVal vs v = Accumulator (v:vs) reapVal (Accumulator vs) = vs -} seedVal = Accumulator [] growVal vs v = Accumulator (v:vs) reapVal (Accumulator vs) = vs -- Tests addVal :: Int -> Int -> (Accumulator Int) addVal m n = Accumulator (n+m) testList = [1,2,3,4,5,6] :: [Int] test1 = foldM addVal 0 testList test2 = Accumulator 0 test3 = (Accumulator 0) >>= addVal 1 test4 = (Accumulator 5) >>= addVal 5 test5 = growVal 1 :: Int -> Accumulator Int test6 = foldM (growVal (Accumulator 0)) testList -------------------------------------------------------------------------------- -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- -- This file is part of Swish. -- -- Swish is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- Swish is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with Swish; if not, write to: -- The Free Software Foundation, Inc., -- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/HaskellRDF/AccumulateM.hs,v $ -- $Author: graham $ -- $Revision: 1.2 $ -- $Log: AccumulateM.hs,v $ -- Revision 1.2 2003/09/24 18:50:52 graham -- Revised module format to be Haddock compatible. -- -- Revision 1.1 2003/06/12 00:49:04 graham -- Basic query processor runs test cases OK. -- Proof framework compiles, not yet tested. --