Mercurial > hg > Papers > 2015 > atton-thesis
diff src/deltaM_example.hs @ 50:37a832dff044
Add DeltaM example
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 15 Feb 2015 17:56:51 +0900 |
parents | ba7f0b5454ab |
children |
line wrap: on
line diff
--- a/src/deltaM_example.hs Sun Feb 15 14:07:07 2015 +0900 +++ b/src/deltaM_example.hs Sun Feb 15 17:56:51 2015 +0900 @@ -1,46 +1,32 @@ -module Example.DeltaM where - -import Control.Monad.Writer -import Data.Numbers.Primes -- $ cabal install primes - -import Delta -import DeltaM - - --- DeltaM examples - --- DeltaM example utils type DeltaLog = Writer [String] type DeltaWithLog = DeltaM DeltaLog returnW :: (Show a) => a -> DeltaLog a -returnW x = do tell $ [show x] +returnW x = do tell ([show x]) return x -dmap :: (m a -> b) -> DeltaM m a -> Delta b -dmap f (DeltaM d) = fmap f d +deltaAppend :: Delta a -> Delta a -> Delta a +deltaAppend (Mono x) d = Delta x d +deltaAppend (Delta x d) ds = Delta x (deltaAppend d ds) -deltaWithLogFromList :: (Show a) => [a] -> DeltaWithLog a -deltaWithLogFromList xs = DeltaM $ deltaFromList $ fmap returnW xs +deltaFromList :: [a] -> Delta a +deltaFromList = (foldl1 deltaAppend) . (fmap return) --- example : prime filter --- usage : runWriter $ checkOut 0 $ numberCountM 30 -- run specific version --- : dmap runWriter $ numberCountM 30 -- run all version generatorM :: Int -> DeltaWithLog [Int] generatorM x = let intList = [1..x] in - DeltaM $ deltaFromList $ fmap returnW $ replicate 2 intList + DeltaM (deltaFromList (fmap returnW (replicate 2 intList))) numberFilterM :: [Int] -> DeltaWithLog [Int] numberFilterM xs = let primeList = filter isPrime xs evenList = filter even xs in - DeltaM $ deltaFromList $ fmap returnW [primeList, evenList] + DeltaM (deltaFromList (fmap returnW [primeList, evenList])) countM :: [Int] -> DeltaWithLog Int countM xs = let numberCount = length xs in - DeltaM $ deltaFromList $ fmap returnW $ replicate 2 numberCount + DeltaM (deltaFromList (fmap returnW (replicate 2 numberCount))) numberCountM :: Int -> DeltaWithLog Int numberCountM x = generatorM x >>= numberFilterM >>= countM