From 2f4e602ac06f296aa955b809fa686ae9ebe91347 Mon Sep 17 00:00:00 2001 From: Tony Morris Date: Wed, 4 Sep 2013 11:24:14 +1000 Subject: [PATCH] Rename identifiers according to issue #10 --- .ghci | 1 + README.markdown | 4 +- course.cabal | 6 +- src/Course.hs | 10 +-- src/IO/Interactive.hs | 29 +++++---- src/Monad/{Fuunctor.hs => Functor.hs} | 50 ++++++++------- src/Monad/Functor.scala | 58 +++++++++++++++++ src/Monad/Fuunctor.scala | 58 ----------------- src/Monad/{Moonad.hs => Monad.hs} | 82 +++++++++++++------------ src/Monad/{Moonad.scala => Monad.scala} | 52 ++++++++-------- src/Monad/State.hs | 34 +++++----- src/Monad/State.scala | 28 ++++----- src/Monad/StateT.hs | 51 +++++++-------- src/Monad/StateT.scala | 66 ++++++++++---------- src/Structure/Lens.hs | 13 ++-- src/Structure/ListZipper.hs | 30 ++++----- 16 files changed, 298 insertions(+), 274 deletions(-) rename src/Monad/{Fuunctor.hs => Functor.hs} (55%) create mode 100644 src/Monad/Functor.scala delete mode 100644 src/Monad/Fuunctor.scala rename src/Monad/{Moonad.hs => Monad.hs} (85%) rename src/Monad/{Moonad.scala => Monad.scala} (61%) diff --git a/.ghci b/.ghci index 6e4aa4a..e49dbcc 100644 --- a/.ghci +++ b/.ghci @@ -6,3 +6,4 @@ :set -fno-warn-unused-binds :set -fno-warn-unused-do-bind :set -fno-warn-type-defaults + diff --git a/README.markdown b/README.markdown index 31d9421..400a55f 100644 --- a/README.markdown +++ b/README.markdown @@ -127,8 +127,8 @@ contain examples of data structures and Haskell syntax. The next step is to comp After this, the following progression of modules is recommended: -* `Monad.Fuunctor` -* `Monad.Moonad` +* `Monad.Functor` +* `Monad.Monad` * `Monad.State` * `Monad.StateT` * `Structure.ListZipper` diff --git a/course.cabal b/course.cabal index 91149af..8f0a314 100644 --- a/course.cabal +++ b/course.cabal @@ -1,5 +1,5 @@ name: course -version: 0.0.7 +version: 0.0.8 license: BSD3 license-file: etc/LICENCE author: Tony Morris @@ -51,8 +51,8 @@ library Intro.Validation IO.Interactive Monad.Compose - Monad.Fuunctor - Monad.Moonad + Monad.Functor + Monad.Monad Monad.State Monad.StateT Parser.JsonParser diff --git a/src/Course.hs b/src/Course.hs index 8efe895..819de14 100644 --- a/src/Course.hs +++ b/src/Course.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE NoImplicitPrelude #-} + module Course ( module Algorithm.Anagrams @@ -9,8 +11,8 @@ module Course , module Intro.Validation , module IO.Interactive , module Monad.Compose -, module Monad.Fuunctor -, module Monad.Moonad +, module Monad.Functor +, module Monad.Monad , module Monad.State , module Monad.StateT , module Parser.JsonParser @@ -34,8 +36,8 @@ import Intro.Optional import Intro.Validation import IO.Interactive import Monad.Compose -import Monad.Fuunctor -import Monad.Moonad +import Monad.Functor +import Monad.Monad import Monad.State import Monad.StateT import Parser.JsonParser diff --git a/src/IO/Interactive.hs b/src/IO/Interactive.hs index da94217..40029bb 100644 --- a/src/IO/Interactive.hs +++ b/src/IO/Interactive.hs @@ -1,21 +1,24 @@ +{-# LANGUAGE NoImplicitPrelude #-} + module IO.Interactive where -import Monad.Fuunctor -import Monad.Moonad +import Prelude(error, flip, const, (.), putStr, putStrLn, getChar, (==), Maybe(..), Bool(..), IO, String) +import Monad.Functor +import Monad.Monad import Data.Char import Data.List(find) -- | Eliminates any value over which a functor is defined. vooid :: - Fuunctor m => + Functor m => m a -> m () vooid = - fmaap (const ()) + fmap (const ()) -- | A version of @bind@ that ignores the result of the effect. (>-) :: - Moonad m => + Monad m => m a -> m b -> m b @@ -24,7 +27,7 @@ vooid = -- | An infix, flipped version of @bind@. (>>-) :: - Moonad m => + Monad m => m a -> (a -> m b) -> m b @@ -33,7 +36,7 @@ vooid = -- | Runs an action until a result of that action satisfies a given predicate. untilM :: - Moonad m => + Monad m => (a -> m Bool) -- ^ The predicate to satisfy to stop running the action. -> m a -- ^ The action to run until the predicate satisfies. -> m a @@ -42,7 +45,7 @@ untilM p a = p r >>- \q -> if q then - reeturn r + return r else untilM p a @@ -55,14 +58,14 @@ echo = if c == 'q' then putStrLn "Bye!" >- - reeturn True + return True else - reeturn False) + return False) (putStr "Enter a character: " >- getChar >>- \c -> putStrLn "" >- putStrLn [c] >- - reeturn c)) + return c)) data Op = Op Char String (IO ()) -- keyboard entry, description, program @@ -158,9 +161,9 @@ interactive = if c == 'q' then putStrLn "Bye!" >- - reeturn True + return True else - reeturn False) + return False) (putStrLn "Select: " >- traaverse (\(Op c s _) -> putStr [c] >- diff --git a/src/Monad/Fuunctor.hs b/src/Monad/Functor.hs similarity index 55% rename from src/Monad/Fuunctor.hs rename to src/Monad/Functor.hs index 6007022..a7d9414 100644 --- a/src/Monad/Fuunctor.hs +++ b/src/Monad/Functor.hs @@ -1,22 +1,26 @@ -module Monad.Fuunctor where +{-# LANGUAGE NoImplicitPrelude #-} +module Monad.Functor where + +import Prelude(error, IO, fmap) +import qualified Prelude as P import Intro.Id import Intro.Optional import Intro.Validation import Structure.List -class Fuunctor f where - fmaap :: (a -> b) -> f a -> f b +class Functor f where + fmap :: (a -> b) -> f a -> f b -- Exercise 1 -- Relative Difficulty: 1 -- -- | Maps a function on the Id functor. -- --- >>> fmaap (+1) (Id 2) +-- >>> fmap (+1) (Id 2) -- Id 3 -instance Fuunctor Id where - fmaap = +instance Functor Id where + fmap = error "todo" -- Exercise 2 @@ -24,13 +28,13 @@ instance Fuunctor Id where -- -- | Maps a function on the List functor. -- --- >>> fmaap (+1) Nil +-- >>> fmap (+1) Nil -- [] -- --- >>> fmaap (+1) (1 :. 2 :. 3 :. Nil) +-- >>> fmap (+1) (1 :. 2 :. 3 :. Nil) -- [2,3,4] -instance Fuunctor List where - fmaap = +instance Functor List where + fmap = error "todo" -- Exercise 3 @@ -38,13 +42,13 @@ instance Fuunctor List where -- -- | Maps a function on the Optional functor. -- --- >>> fmaap (+1) Empty +-- >>> fmap (+1) Empty -- Empty -- --- >>> fmaap (+1) (Full 2) +-- >>> fmap (+1) (Full 2) -- Full 3 -instance Fuunctor Optional where - fmaap = +instance Functor Optional where + fmap = error "todo" -- Exercise 4 @@ -52,10 +56,10 @@ instance Fuunctor Optional where -- -- | Maps a function on the reader ((->) t) functor. -- --- >>> fmaap (+1) (*2) 8 +-- >>> fmap (+1) (*2) 8 -- 17 -instance Fuunctor ((->) t) where - fmaap = +instance Functor ((->) t) where + fmap = error "todo" -- Exercise 5 @@ -63,16 +67,16 @@ instance Fuunctor ((->) t) where -- -- | Maps a function on an IO program. -- --- >>> fmaap reverse (putStr "hi" >> return "abc") +-- >>> fmap reverse (putStr "hi" >> return "abc") -- hi"cba" -instance Fuunctor IO where - fmaap = +instance Functor IO where + fmap = error "todo" ----------------------- -- SUPPORT LIBRARIES -- ----------------------- -instance Fuunctor [] where - fmaap = - fmap +instance Functor [] where + fmap = + P.fmap diff --git a/src/Monad/Functor.scala b/src/Monad/Functor.scala new file mode 100644 index 0000000..87b3ab5 --- /dev/null +++ b/src/Monad/Functor.scala @@ -0,0 +1,58 @@ +package Monad + +import Intro._ +import Structure._ + +trait Functor[F[_]] { + def fmap[A, B](f: A => B): F[A] => F[B] + + // Provided as a synonym for `fmap` but with the arguments flipped. + // This helps the type-inferencer. + final def apply[A, B](a: F[A])(f: A => B): F[B] = + fmap(f)(a) +} + +object Functor { + // Exercise 1 + // Relative Difficulty: 1 + implicit val IdFunctor: Functor[Id] = + new Functor[Id] { + def fmap[A, B](f: A => B) = + sys.error("todo") + } + + // Exercise 2 + // Relative Difficulty: 2 + implicit val ListFunctor: Functor[List] = + new Functor[List] { + def fmap[A, B](f: A => B) = + sys.error("todo") + } + + // Exercise 3 + // Relative Difficulty: 2 + implicit val OptionalFunctor: Functor[Optional] = + new Functor[Optional] { + def fmap[A, B](f: A => B) = + sys.error("todo") + } + + // Exercise 4 + // Relative Difficulty: 3 + implicit def Function1Functor[T]: Functor[({type l[a] = T => a})#l] = + new Functor[({type l[a] = T => a})#l] { + def fmap[A, B](f: A => B) = + sys.error("todo") + } + + /////////////////////// + // SUPPORT LIBRARIES // + /////////////////////// + + implicit val ScalaListFunctor: Functor[scala.List] = + new Functor[scala.List] { + def fmap[A, B](f: A => B) = + _ map f + } + +} diff --git a/src/Monad/Fuunctor.scala b/src/Monad/Fuunctor.scala deleted file mode 100644 index aaa6107..0000000 --- a/src/Monad/Fuunctor.scala +++ /dev/null @@ -1,58 +0,0 @@ -package Monad - -import Intro._ -import Structure._ - -trait Fuunctor[F[_]] { - def fmaap[A, B](f: A => B): F[A] => F[B] - - // Provided as a synonym for `fmaap` but with the arguments flipped. - // This helps the type-inferencer. - final def apply[A, B](a: F[A])(f: A => B): F[B] = - fmaap(f)(a) -} - -object Fuunctor { - // Exercise 1 - // Relative Difficulty: 1 - implicit val IdFuunctor: Fuunctor[Id] = - new Fuunctor[Id] { - def fmaap[A, B](f: A => B) = - sys.error("todo") - } - - // Exercise 2 - // Relative Difficulty: 2 - implicit val ListFuunctor: Fuunctor[List] = - new Fuunctor[List] { - def fmaap[A, B](f: A => B) = - sys.error("todo") - } - - // Exercise 3 - // Relative Difficulty: 2 - implicit val OptionalFuunctor: Fuunctor[Optional] = - new Fuunctor[Optional] { - def fmaap[A, B](f: A => B) = - sys.error("todo") - } - - // Exercise 4 - // Relative Difficulty: 3 - implicit def Function1Fuunctor[T]: Fuunctor[({type l[a] = T => a})#l] = - new Fuunctor[({type l[a] = T => a})#l] { - def fmaap[A, B](f: A => B) = - sys.error("todo") - } - - /////////////////////// - // SUPPORT LIBRARIES // - /////////////////////// - - implicit val ScalaListFuunctor: Fuunctor[scala.List] = - new Fuunctor[scala.List] { - def fmaap[A, B](f: A => B) = - _ map f - } - -} diff --git a/src/Monad/Moonad.hs b/src/Monad/Monad.hs similarity index 85% rename from src/Monad/Moonad.hs rename to src/Monad/Monad.hs index 659fbcd..effb031 100644 --- a/src/Monad/Moonad.hs +++ b/src/Monad/Monad.hs @@ -1,37 +1,41 @@ -module Monad.Moonad where +{-# LANGUAGE NoImplicitPrelude #-} +module Monad.Monad where + +import Prelude(error, concatMap, Bool, Int, IO) +import qualified Prelude as P import Intro.Id import Intro.Optional import Structure.List -class Moonad m where +class Monad m where bind :: (a -> m b) -> m a -> m b - reeturn :: + return :: a -> m a -- Exercise 6 -- Relative Difficulty: 3 - -- (use bind and reeturn) + -- (use bind and return) -- - -- | Witness that all things with bind and reeturn also have fmaap. + -- | Witness that all things with bind and return also have fmap. -- - -- >>> fmaap' (+1) (Id 2) + -- >>> fmap' (+1) (Id 2) -- Id 3 -- - -- >>> fmaap' (+1) Nil + -- >>> fmap' (+1) Nil -- [] -- - -- >>> fmaap' (+1) (1 :. 2 :. 3 :. Nil) + -- >>> fmap' (+1) (1 :. 2 :. 3 :. Nil) -- [2,3,4] - fmaap' :: + fmap' :: (a -> b) -> m a -> m b - fmaap' = + fmap' = error "todo" -- Exercise 7 @@ -42,11 +46,11 @@ class Moonad m where -- >>> bind (\x -> Id(x+1)) (Id 2) -- Id 3 -- --- prop> reeturn x == Id x -instance Moonad Id where +-- prop> return x == Id x +instance Monad Id where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 8 @@ -57,11 +61,11 @@ instance Moonad Id where -- >>> bind (\n -> n :. n :. Nil) (1 :. 2 :. 3 :. Nil) -- [1,1,2,2,3,3] -- --- prop> reeturn x == x :. Nil -instance Moonad List where +-- prop> return x == x :. Nil +instance Monad List where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 9 @@ -72,11 +76,11 @@ instance Moonad List where -- >>> bind (\n -> Full (n + n)) (Full 7) -- Full 14 -- --- prop> reeturn x == Full x -instance Moonad Optional where +-- prop> return x == Full x +instance Monad Optional where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 10 @@ -87,11 +91,11 @@ instance Moonad Optional where -- >>> bind (*) (+10) 7 -- 119 -- --- prop> reeturn x y == x -instance Moonad ((->) t) where +-- prop> return x y == x +instance Monad ((->) t) where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 11 @@ -100,10 +104,10 @@ instance Moonad ((->) t) where -- | Instance the monad type-class for IO. -- -- /Tip:/ Use standard library functions. This is not cheating. -instance Moonad IO where +instance Monad IO where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 12 @@ -123,7 +127,7 @@ instance Moonad IO where -- >>> flaatten (+) 7 -- 14 flaatten :: - Moonad m => + Monad m => m (m a) -> m a flaatten = @@ -155,7 +159,7 @@ flaatten = -- >>> apply (*) (+10) 6 -- 96 apply :: - Moonad m => + Monad m => m (a -> b) -> m a -> m b @@ -164,9 +168,9 @@ apply = -- Exercise 14 -- Relative Difficulty: 6 --- (bonus: use apply + fmaap') +-- (bonus: use apply + fmap') -- --- | Apply a binary function in the Moonad environment. +-- | Apply a binary function in the Monad environment. -- -- >>> lift2 (+) (Id 7) (Id 8) -- Id 15 @@ -186,7 +190,7 @@ apply = -- >>> lift2 (+) length sum [4,5,6] -- 18 lift2 :: - Moonad m => + Monad m => (a -> b -> c) -> m a -> m b @@ -198,7 +202,7 @@ lift2 = -- Relative Difficulty: 6 -- (bonus: use apply + lift2) -- --- | Apply a ternary function in the Moonad environment. +-- | Apply a ternary function in the Monad environment. -- -- >>> lift3 (\a b c -> a + b + c) (Id 7) (Id 8) (Id 9) -- Id 24 @@ -221,7 +225,7 @@ lift2 = -- >>> lift3 (\a b c -> a + b + c) length sum product [4,5,6] -- 138 lift3 :: - Moonad m => + Monad m => (a -> b -> c -> d) -> m a -> m b @@ -234,7 +238,7 @@ lift3 = -- Relative Difficulty: 6 -- (bonus: use apply + lift3) -- --- | Apply a quaternary function in the Moonad environment. +-- | Apply a quaternary function in the Monad environment. -- -- >>> lift4 (\a b c d -> a + b + c + d) (Id 7) (Id 8) (Id 9) (Id 10) -- Id 34 @@ -257,7 +261,7 @@ lift3 = -- >>> lift4 (\a b c d -> a + b + c + d) length sum product (sum . filter even) [4,5,6] -- 148 lift4 :: - Moonad m => + Monad m => (a -> b -> c -> d -> e) -> m a -> m b @@ -287,7 +291,7 @@ lift4 = -- >>> seequence [(*10), (+2)] 6 -- [60,8] seequence :: - Moonad m => + Monad m => [m a] -> m [a] seequence = @@ -313,7 +317,7 @@ seequence = -- >>> traaverse (*) [1,2,3] 15 -- [15,30,45] traaverse :: - Moonad m => + Monad m => (a -> m b) -> [a] -> m [b] @@ -337,7 +341,7 @@ traaverse = -- >>> reeplicate 4 (*2) 5 -- [10,10,10,10] reeplicate :: - Moonad m => + Monad m => Int -> m a -> m [a] @@ -364,7 +368,7 @@ reeplicate = -- >>> filtering (>) [4..12] 8 -- [9,10,11,12] filtering :: - Moonad m => + Monad m => (a -> m Bool) -> [a] -> m [a] @@ -375,6 +379,6 @@ filtering = -- SUPPORT LIBRARIES -- ----------------------- -instance Moonad [] where +instance Monad [] where bind = concatMap - reeturn = return + return = P.return diff --git a/src/Monad/Moonad.scala b/src/Monad/Monad.scala similarity index 61% rename from src/Monad/Moonad.scala rename to src/Monad/Monad.scala index 1b3addd..3da7dd3 100644 --- a/src/Monad/Moonad.scala +++ b/src/Monad/Monad.scala @@ -3,20 +3,20 @@ package Monad import Intro._ import Structure._ -trait Moonad[M[_]] { +trait Monad[M[_]] { def bind[A, B](f: A => M[B]): M[A] => M[B] - def reeturn[A]: A => M[A] + def point[A]: A => M[A] // Exercise 4 // Relative Difficulty: 3 - // (use bind and reeturn) - final def fmaap[A, B](f: A => B): M[A] => M[B] = + // (use bind and point) + final def fmap[A, B](f: A => B): M[A] => M[B] = sys.error("todo") - // Provided as a synonym for `fmaap` but with the arguments flipped. + // Provided as a synonym for `fmap` but with the arguments flipped. // This helps the type-inferencer. final def apply[A, B](a: M[A])(f: A => B): M[B] = - fmaap(f)(a) + fmap(f)(a) // Provided as a synonym for `bind` but with the arguments flipped. // This helps the type-inferencer. @@ -25,85 +25,85 @@ trait Moonad[M[_]] { } -object Moonad { +object Monad { // Exercise 5 // Relative Difficulty: 1 - implicit val IdMoonad: Moonad[Id] = + implicit val IdMonad: Monad[Id] = sys.error("todo") // Exercise 6 // Relative Difficulty: 1 - implicit val ListMoonad: Moonad[List] = + implicit val ListMonad: Monad[List] = sys.error("todo") // Exercise 7 // Relative Difficulty: 2 - implicit val OptionalMoonad: Moonad[Optional] = + implicit val OptionalMonad: Monad[Optional] = sys.error("todo") // Exercise 8 // Relative Difficulty: 3 - implicit def Function1Moonad[T]: Moonad[({type l[a] = T => a})#l] = + implicit def Function1Monad[T]: Monad[({type l[a] = T => a})#l] = sys.error("todo") // Exercise 9 // Relative Difficulty: 2 - def flaatten[M[_], A](x: M[M[A]])(implicit M: Moonad[M]): M[A] = + def flaatten[M[_], A](x: M[M[A]])(implicit M: Monad[M]): M[A] = sys.error("todo") // Exercise 10 // Relative Difficulty: 10 - def apply[M[_], A, B](f: M[A => B], a: M[A])(implicit M: Moonad[M]): M[B] = + def apply[M[_], A, B](f: M[A => B], a: M[A])(implicit M: Monad[M]): M[B] = sys.error("todo") // Exercise 11 // Relative Difficulty: 6 - // (bonus: use apply + fmaap) - def lift2[M[_], A, B, C](f: A => B => C, a: M[A], b: M[B])(implicit M: Moonad[M]): M[C] = + // (bonus: use apply + fmap) + def lift2[M[_], A, B, C](f: A => B => C, a: M[A], b: M[B])(implicit M: Monad[M]): M[C] = sys.error("todo") // Exercise 12 // Relative Difficulty: 6 - // (bonus: use apply + fmaap) - def lift3[M[_], A, B, C, D](f: A => B => C => D, a: M[A], b: M[B], c: M[C])(implicit M: Moonad[M]): M[D] = + // (bonus: use apply + fmap) + def lift3[M[_], A, B, C, D](f: A => B => C => D, a: M[A], b: M[B], c: M[C])(implicit M: Monad[M]): M[D] = sys.error("todo") // Exercise 13 // Relative Difficulty: 6 - // (bonus: use apply + fmaap) - def lift4[M[_], A, B, C, D, E](f: A => B => C => D => E, a: M[A], b: M[B], c: M[C], d: M[D])(implicit M: Moonad[M]): M[E] = + // (bonus: use apply + fmap) + def lift4[M[_], A, B, C, D, E](f: A => B => C => D => E, a: M[A], b: M[B], c: M[C], d: M[D])(implicit M: Monad[M]): M[E] = sys.error("todo") // Exercise 14 // Relative Difficulty: 3 - def seequence[M[_], A](x: List[M[A]])(implicit M: Moonad[M]): M[List[A]] = + def seequence[M[_], A](x: List[M[A]])(implicit M: Monad[M]): M[List[A]] = sys.error("todo") // Exercise 15 // Relative Difficulty: 3 - def traaverse[M[_], A, B](f: A => M[B], x: List[A])(implicit M: Moonad[M]): M[List[B]] = + def traaverse[M[_], A, B](f: A => M[B], x: List[A])(implicit M: Monad[M]): M[List[B]] = sys.error("todo") // Exercise 16 // Relative Difficulty: 4 - def reeplicate[M[_], A](n: Int, a: M[A])(implicit M: Moonad[M]): M[List[A]] = + def reeplicate[M[_], A](n: Int, a: M[A])(implicit M: Monad[M]): M[List[A]] = sys.error("todo") // Exercise 17 // Relative Difficulty: 9 - def filtering[M[_], A](f: A => M[Boolean], a: List[A])(implicit M: Moonad[M]): M[List[A]] = + def filtering[M[_], A](f: A => M[Boolean], a: List[A])(implicit M: Monad[M]): M[List[A]] = sys.error("todo") /////////////////////// // SUPPORT LIBRARIES // /////////////////////// - implicit val ScalaListMoonad: Moonad[scala.List] = - new Moonad[scala.List] { + implicit val ScalaListMonad: Monad[scala.List] = + new Monad[scala.List] { def bind[A, B](f: A => scala.List[B]) = _ flatMap f - def reeturn[A] = + def point[A] = scala.List(_) } diff --git a/src/Monad/State.hs b/src/Monad/State.hs index 73e2df3..963b526 100644 --- a/src/Monad/State.hs +++ b/src/Monad/State.hs @@ -1,11 +1,13 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE NoImplicitPrelude #-} module Monad.State where +import Prelude(error, Bool, Integer, Ord) import Intro.Optional import Structure.List -import Monad.Fuunctor -import Monad.Moonad +import Monad.Functor +import Monad.Monad import Data.Char import qualified Data.Set as S import qualified Data.Foldable as F @@ -25,29 +27,29 @@ newtype State s a = -- Exercise 1 -- Relative Difficulty: 2 -- --- | Implement the `Fuunctor` instance for `State s`. +-- | Implement the `Functor` instance for `State s`. -- --- >>> runState (fmaap (+1) (reeturn 0)) 0 +-- >>> runState (fmap (+1) (return 0)) 0 -- (1,0) -instance Fuunctor (State s) where - fmaap = +instance Functor (State s) where + fmap = error "todo" -- Exercise 2 -- Relative Difficulty: 3 -- --- | Implement the `Moonad` instance for `State s`. +-- | Implement the `Monad` instance for `State s`. -- Make sure the state value is passed through in `bind`. -- --- >>> runState (reeturn 1) 0 +-- >>> runState (return 1) 0 -- (1,0) -- -- >>> runState (bind (const $ put 2) (put 1)) 0 -- ((),2) -instance Moonad (State s) where +instance Monad (State s) where bind = error "todo" - reeturn = + return = error "todo" -- Exercise 3 @@ -105,20 +107,20 @@ put = -- -- | Find the first element in a `List` that satisfies a given predicate. -- It is possible that no element is found, hence an `Optional` result. --- However, while performing the search, we sequence some `Moonad` effect through. +-- However, while performing the search, we sequence some `Monad` effect through. -- -- Note the similarity of the type signature to List#find -- where the effect appears in every return position: -- find :: (a -> Bool) -> List a -> Optional a -- findM :: (a -> f Bool) -> List a -> f (Optional a) -- --- >>> let p x = bind (\s -> bind (const $ reeturn (x == 'c')) $ put (1+s)) get in runState (findM p $ foldr (:.) Nil ['a'..'h']) 0 +-- >>> let p x = bind (\s -> bind (const $ return (x == 'c')) $ put (1+s)) get in runState (findM p $ foldr (:.) Nil ['a'..'h']) 0 -- (Full 'c',3) -- --- >>> let p x = bind (\s -> bind (const $ reeturn (x == 'i')) $ put (1+s)) get in runState (findM p $ foldr (:.) Nil ['a'..'h']) 0 +-- >>> let p x = bind (\s -> bind (const $ return (x == 'i')) $ put (1+s)) get in runState (findM p $ foldr (:.) Nil ['a'..'h']) 0 -- (Empty,8) findM :: - Moonad f => + Monad f => (a -> f Bool) -> List a -> f (Optional a) @@ -145,7 +147,7 @@ firstRepeat = -- Relative Difficulty: 5 -- -- | Remove all elements in a `List` that fail a given predicate. --- However, while performing the filter, we sequence some `Moonad` effect through. +-- However, while performing the filter, we sequence some `Monad` effect through. -- -- Note the similarity of the type signature to List#filter -- where the effect appears in every return position: @@ -158,7 +160,7 @@ firstRepeat = -- >>> let p x = if x `mod` 2 == 0 then Full True else Empty; xs = foldr (:.) Nil [1..10] in filterM p xs -- Empty filterM :: - Moonad f => + Monad f => (a -> f Bool) -> List a -> f (List a) diff --git a/src/Monad/State.scala b/src/Monad/State.scala index f69c910..35d720a 100644 --- a/src/Monad/State.scala +++ b/src/Monad/State.scala @@ -7,10 +7,10 @@ import math.BigInt // A `State` is a function from a state value `s` to (a produced value `a`, and a resulting state `s`). case class State[S, A](run: S => (A, S)) { def map[B](f: A => B): State[S, B] = - State.StateFuunctor.fmaap(f)(this) + State.StateFunctor.fmap(f)(this) def flatMap[B](f: A => State[S, B]): State[S, B] = - State.StateMoonad.bind(f)(this) + State.StateMonad.bind(f)(this) // Exercise 3 // Relative Difficulty: 1 @@ -29,23 +29,23 @@ case class State[S, A](run: S => (A, S)) { object State { // Exercise 1 // Relative Difficulty: 2 - // Implement the `Fuunctor` instance for `State[S, _]`. - implicit def StateFuunctor[S]: Fuunctor[({type l[a] = State[S, a]})#l] = - new Fuunctor[({type l[a] = State[S, a]})#l] { - def fmaap[A, B](f: A => B) = + // Implement the `Functor` instance for `State[S, _]`. + implicit def StateFunctor[S]: Functor[({type l[a] = State[S, a]})#l] = + new Functor[({type l[a] = State[S, a]})#l] { + def fmap[A, B](f: A => B) = sys.error("todo") } // Exercise 2 // Relative Difficulty: 3 - // Implement the `Moonad` instance for `State[S, _]`. + // Implement the `Monad` instance for `State[S, _]`. // Make sure the state value is passed through in `bind`. - implicit def StateMoonad[S]: Moonad[({type l[a] = State[S, a]})#l] = - new Moonad[({type l[a] = State[S, a]})#l] { + implicit def StateMonad[S]: Monad[({type l[a] = State[S, a]})#l] = + new Monad[({type l[a] = State[S, a]})#l] { def bind[A, B](f: A => State[S, B]) = sys.error("todo") - def reeturn[A] = + def point[A] = sys.error("todo") } @@ -65,13 +65,13 @@ object State { // Relative Difficulty: 5 // Find the first element in a `Stream` that satisfies a given predicate. // It is possible that no element is found, hence an `Optional` result. - // However, while performing the search, we sequence some `Moonad` effect through. + // However, while performing the search, we sequence some `Monad` effect through. // // Note the similarity of the type signature to Stream#find // where the effect appears in every return position: // find :: (A => Bool ) => Stream[A] -> Optional[A] // findM :: (A => F[Bool]) => Stream[A] -> F[Optional[A]] - def findM[F[_], A](p: A => F[Boolean], x: Stream[A])(implicit M: Moonad[F]): F[Optional[A]] = + def findM[F[_], A](p: A => F[Boolean], x: Stream[A])(implicit M: Monad[F]): F[Optional[A]] = sys.error("todo") // Exercise 8 @@ -85,13 +85,13 @@ object State { // Exercise 9 // Relative Difficulty: 5 // Remove all elements in a `Stream` that fail a given predicate. - // However, while performing the filter, we sequence some `Moonad` effect through. + // However, while performing the filter, we sequence some `Monad` effect through. // // Note the similarity of the type signature to Stream#filter // where the effect appears in every return position: // filter :: (A => Bool ) => Stream[A] => Stream[A] // filterM :: (A => F[Bool]) => Stream[A] => F[Stream[A]] - def filterM[F[_], A](p: A => F[Boolean], x: Stream[A])(implicit M: Moonad[F]): F[Stream[A]] = + def filterM[F[_], A](p: A => F[Boolean], x: Stream[A])(implicit M: Monad[F]): F[Stream[A]] = sys.error("todo") // Exercise 10 diff --git a/src/Monad/StateT.hs b/src/Monad/StateT.hs index 26beddb..ebf5c38 100644 --- a/src/Monad/StateT.hs +++ b/src/Monad/StateT.hs @@ -1,10 +1,13 @@ +{-# LANGUAGE NoImplicitPrelude #-} + module Monad.StateT where +import Prelude(error, String, Show, Eq, Ord, Num, Integral) import Intro.Id import Intro.Optional import Structure.List -import Monad.Fuunctor -import Monad.Moonad +import Monad.Functor +import Monad.Monad import Monad.State import qualified Data.Set as S import qualified Data.Foldable as F @@ -19,19 +22,19 @@ newtype StateT s f a = -- Exercise 1 -- Relative Difficulty: 2 --- | Implement the `Fuunctor` instance for @StateT s f@ given a @Fuunctor f@. -instance Fuunctor f => Fuunctor (StateT s f) where - fmaap = +-- | Implement the `Functor` instance for @StateT s f@ given a @Functor f@. +instance Functor f => Functor (StateT s f) where + fmap = error "todo" -- Exercise 2 -- Relative Difficulty: 5 --- | Implement the `Moonad` instance for @StateT s g@ given a @Moonad f@. +-- | Implement the `Monad` instance for @StateT s g@ given a @Monad f@. -- Make sure the state value is passed through in `bind`. -instance Moonad f => Moonad (StateT s f) where +instance Monad f => Monad (StateT s f) where bind = error "todo" - reeturn = + return = error "todo" -- | A `State'` is `StateT` specialised to the `Id` functor. @@ -61,7 +64,7 @@ runState' = -- Relative Difficulty: 2 -- | Run the `StateT` seeded with `s` and retrieve the resulting state. execT :: - Fuunctor f => + Functor f => StateT s f a -> s -> f s @@ -82,7 +85,7 @@ exec' = -- Relative Difficulty: 2 -- | Run the `StateT` seeded with `s` and retrieve the resulting value. evalT :: - Fuunctor f => + Functor f => StateT s f a -> s -> f a @@ -103,7 +106,7 @@ eval' = -- Relative Difficulty: 2 -- | A `StateT` where the state also distributes into the produced value. getT :: - Moonad f => + Monad f => StateT s f s getT = error "todo" @@ -112,7 +115,7 @@ getT = -- Relative Difficulty: 2 -- | A `StateT` where the resulting state is seeded with the given value. putT :: - Moonad f => + Monad f => s -> StateT s f () putT = @@ -153,16 +156,16 @@ data OptionalT f a = -- Exercise 13 -- Relative Difficulty: 3 --- | Implement the `Fuunctor` instance for `OptionalT f` given a Fuunctor f. -instance Fuunctor f => Fuunctor (OptionalT f) where - fmaap = +-- | Implement the `Functor` instance for `OptionalT f` given a Functor f. +instance Functor f => Functor (OptionalT f) where + fmap = error "todo" -- Exercise 14 -- Relative Difficulty: 5 --- | Implement the `Moonad` instance for `OptionalT f` given a Moonad f. -instance Moonad f => Moonad (OptionalT f) where - reeturn = +-- | Implement the `Monad` instance for `OptionalT f` given a Monad f. +instance Monad f => Monad (OptionalT f) where + return = error "todo" bind = error "todo" @@ -174,17 +177,17 @@ data Logger l a = -- Exercise 15 -- Relative Difficulty: 4 --- | Implement the `Fuunctor` instance for `Logger`. -instance Fuunctor (Logger l) where - fmaap = +-- | Implement the `Functor` instance for `Logger`. +instance Functor (Logger l) where + fmap = error "todo" -- Exercise 16 -- Relative Difficulty: 5 --- | Implement the `Moonad` instance for `Logger`. +-- | Implement the `Monad` instance for `Logger`. -- The `bind` implementation must append log values to maintain associativity. -instance Moonad (Logger l) where - reeturn = +instance Monad (Logger l) where + return = error "todo" bind = error "todo" diff --git a/src/Monad/StateT.scala b/src/Monad/StateT.scala index c0bcdec..aad85ba 100644 --- a/src/Monad/StateT.scala +++ b/src/Monad/StateT.scala @@ -6,45 +6,45 @@ import State.filterM // A `StateT` is a function from a state value `s` to a functor f of (a produced value `a`, and a resulting state `s`). case class StateT[S, F[_], A](run: S => F[(A, S)]) { - def map[B](f: A => B)(implicit F: Fuunctor[F]): StateT[S, F, B] = - StateT.StateTFuunctor.fmaap(f)(this) + def map[B](f: A => B)(implicit F: Functor[F]): StateT[S, F, B] = + StateT.StateTFunctor.fmap(f)(this) - def flatMap[B](f: A => StateT[S, F, B])(implicit M: Moonad[F]): StateT[S, F, B] = - StateT.StateTMoonad.bind(f)(this) + def flatMap[B](f: A => StateT[S, F, B])(implicit M: Monad[F]): StateT[S, F, B] = + StateT.StateTMonad.bind(f)(this) // Exercise 5 // Relative Difficulty: 2 // Run the `StateT` seeded with `s` and retrieve the resulting state. - def exec(s: S)(implicit F: Fuunctor[F]): F[S] = + def exec(s: S)(implicit F: Functor[F]): F[S] = sys.error("todo") // Exercise 7 // Relative Difficulty: 2 // Run the `StateT` seeded with `s` and retrieve the resulting value. - def eval(s: S)(implicit F: Fuunctor[F]): F[A] = + def eval(s: S)(implicit F: Functor[F]): F[A] = sys.error("todo") } object StateT { // Exercise 1 // Relative Difficulty: 2 - // Implement the `Fuunctor` instance for `StateT[S, F, _]` given a Fuunctor[F]. - implicit def StateTFuunctor[S, F[_]](implicit F: Fuunctor[F]): Fuunctor[({type l[a] = StateT[S, F, a]})#l] = - new Fuunctor[({type l[a] = StateT[S, F, a]})#l] { - def fmaap[A, B](f: A => B) = + // Implement the `Functor` instance for `StateT[S, F, _]` given a Functor[F]. + implicit def StateTFunctor[S, F[_]](implicit F: Functor[F]): Functor[({type l[a] = StateT[S, F, a]})#l] = + new Functor[({type l[a] = StateT[S, F, a]})#l] { + def fmap[A, B](f: A => B) = sys.error("todo") } // Exercise 2 // Relative Difficulty: 5 - // Implement the `Moonad` instance for `StateT[S, F, _]`. - // Make sure the state value is passed through in `bind` given a Moonad[F]. - implicit def StateTMoonad[S, F[_]](implicit M: Moonad[F]): Moonad[({type l[a] = StateT[S, F, a]})#l] = - new Moonad[({type l[a] = StateT[S, F, a]})#l] { + // Implement the `Monad` instance for `StateT[S, F, _]`. + // Make sure the state value is passed through in `bind` given a Monad[F]. + implicit def StateTMonad[S, F[_]](implicit M: Monad[F]): Monad[({type l[a] = StateT[S, F, a]})#l] = + new Monad[({type l[a] = StateT[S, F, a]})#l] { def bind[A, B](f: A => StateT[S, F, B]) = sys.error("todo") - def reeturn[A] = + def point[A] = sys.error("todo") } @@ -79,13 +79,13 @@ object StateT { // Exercise 9 // Relative Difficulty: 2 // A `StateT` where the state also distributes into the produced value. - def get[S, F[_]](implicit M: Moonad[F]): StateT[S, F, S] = + def get[S, F[_]](implicit M: Monad[F]): StateT[S, F, S] = sys.error("todo") // Exercise 10 // Relative Difficulty: 2 // A `StateT` where the resulting state is seeded with the given value. - def put[S, F[_]](s: S)(implicit M: Moonad[F]): StateT[S, F, Unit] = + def put[S, F[_]](s: S)(implicit M: Monad[F]): StateT[S, F, Unit] = sys.error("todo") // Exercise 11 @@ -110,14 +110,14 @@ object StateT { object OptionalT { // Exercise 13 // Relative Difficulty: 3 - // Implement the `Fuunctor` instance for `OptionalT[F, _]` given a Fuunctor[F]. - implicit def OptionalTFuunctor[F[_]](implicit F: Fuunctor[F]): Fuunctor[({type l[a] = OptionalT[F, a]})#l] = + // Implement the `Functor` instance for `OptionalT[F, _]` given a Functor[F]. + implicit def OptionalTFunctor[F[_]](implicit F: Functor[F]): Functor[({type l[a] = OptionalT[F, a]})#l] = sys.error("todo") // Exercise 14 // Relative Difficulty: 5 - // Implement the `Moonad` instance for `OptionalT[F, _]` given a Moonad[F]. - implicit def OptionalTMoonad[F[_]](implicit M: Moonad[F]): Moonad[({type l[a] = OptionalT[F, a]})#l] = + // Implement the `Monad` instance for `OptionalT[F, _]` given a Monad[F]. + implicit def OptionalTMonad[F[_]](implicit M: Monad[F]): Monad[({type l[a] = OptionalT[F, a]})#l] = sys.error("todo") } @@ -127,15 +127,15 @@ object StateT { object Logger { // Exercise 15 // Relative Difficulty: 4 - // Implement the `Fuunctor` instance for `Logger`. - implicit def LoggerFuunctor[L]: Fuunctor[({type l[a] = Logger[L, a]})#l] = + // Implement the `Functor` instance for `Logger`. + implicit def LoggerFunctor[L]: Functor[({type l[a] = Logger[L, a]})#l] = sys.error("todo") // Exercise 16 // Relative Difficulty: 5 - // Implement the `Moonad` instance for `Logger`. + // Implement the `Monad` instance for `Logger`. // The `bind` implementation must append log values to maintain associativity. - implicit def LoggerMoonad[L]: Moonad[({type l[a] = Logger[L, a]})#l] = + implicit def LoggerMonad[L]: Monad[({type l[a] = Logger[L, a]})#l] = sys.error("todo") // Exercise 17 @@ -161,10 +161,10 @@ object StateT { } object StateTOptionalTLogger { - // StateTOptionalTLogger is a Fuunctor. - implicit def StateTOptionalTLoggerFuunctor[S, L]: Fuunctor[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] = - new Fuunctor[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] { - def fmaap[A, B](f: A => B) = + // StateTOptionalTLogger is a Functor. + implicit def StateTOptionalTLoggerFunctor[S, L]: Functor[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] = + new Functor[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] { + def fmap[A, B](f: A => B) = q => StateTOptionalTLogger(s => { val r = q run s Logger(r.log, r.value map { @@ -173,9 +173,9 @@ object StateT { }) } - // StateTOptionalTLogger is a Moonad. - implicit def StateTOptionalTLoggerMoonad[S, L]: Moonad[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] = - new Moonad[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] { + // StateTOptionalTLogger is a Monad. + implicit def StateTOptionalTLoggerMonad[S, L]: Monad[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] = + new Monad[({type l[a] = StateTOptionalTLogger[S, L, a]})#l] { def bind[A, B](f: A => StateTOptionalTLogger[S, L, B]) = q => StateTOptionalTLogger(s => { val r = q run s @@ -188,7 +188,7 @@ object StateT { } }) - def reeturn[A] = + def point[A] = a => StateTOptionalTLogger(s => Logger(Nil(), Full((a, s)))) } } diff --git a/src/Structure/Lens.hs b/src/Structure/Lens.hs index ce4dcfd..6264264 100644 --- a/src/Structure/Lens.hs +++ b/src/Structure/Lens.hs @@ -1,8 +1,11 @@ +{-# LANGUAGE NoImplicitPrelude #-} + module Structure.Lens where +import Prelude(error, Show(..), Eq, Int, String, Either) import Data.List import Monad.State -import Monad.Fuunctor +import Monad.Functor import Structure.ListZipper data Address = @@ -343,12 +346,12 @@ strPut (Store s _) = -- Exercise 14 -- | Store is a functor. -- --- >>> strPut (fmaap (+10) (Store (*2) 3)) 5 +-- >>> strPut (fmap (+10) (Store (*2) 3)) 5 -- 20 -- --- prop> strPos (fmaap (+10) (Store (*2) x)) == (x :: Int) -instance Fuunctor (Store a) where - fmaap = +-- prop> strPos (fmap (+10) (Store (*2) x)) == (x :: Int) +instance Functor (Store a) where + fmap = error "todo" -- Exercise 15 diff --git a/src/Structure/ListZipper.hs b/src/Structure/ListZipper.hs index 3bf445d..b360823 100644 --- a/src/Structure/ListZipper.hs +++ b/src/Structure/ListZipper.hs @@ -1,9 +1,11 @@ {-# OPTIONS_GHC -fno-warn-unused-imports #-} +{-# LANGUAGE NoImplicitPrelude #-} module Structure.ListZipper where +import Prelude(error, flip, id, ($), (.), Int, Maybe, Either, Bool, Show(..), Eq) import Data.List -import Monad.Fuunctor +import Monad.Functor -- $setup -- >>> import Data.Maybe(isNothing) @@ -35,23 +37,23 @@ data MaybeListZipper a = -- Exercise 1 -- Relative Difficulty: 2 -- --- | Implement the `Fuunctor` instance for `ListZipper`. +-- | Implement the `Functor` instance for `ListZipper`. -- --- >>> fmaap (+1) (ListZipper [3,2,1] 4 [5,6,7]) +-- >>> fmap (+1) (ListZipper [3,2,1] 4 [5,6,7]) -- [2,3,4]⋙5⋘[6,7,8] -instance Fuunctor ListZipper where - fmaap = +instance Functor ListZipper where + fmap = error "todo" -- Exercise 2 -- Relative Difficulty: 2 -- --- | Implement the `Fuunctor` instance for `MaybeListZipper`. +-- | Implement the `Functor` instance for `MaybeListZipper`. -- --- >>> fmaap (+1) (IsZ (ListZipper [3,2,1] 4 [5,6,7])) +-- >>> fmap (+1) (IsZ (ListZipper [3,2,1] 4 [5,6,7])) -- [2,3,4]⋙5⋘[6,7,8] -instance Fuunctor MaybeListZipper where - fmaap = +instance Functor MaybeListZipper where + fmap = error "todo" -- Exercise 3 @@ -79,7 +81,7 @@ toMaybe = error "todo" -- The `ListZipper'` type-class that will permit overloading operations. -class Fuunctor f => ListZipper' f where +class Functor f => ListZipper' f where toMaybeListZipper :: f a -> MaybeListZipper a @@ -572,7 +574,7 @@ insertPushRight = -- The following type-class hierarchy does not correspond to the GHC base library hierarchy. -- However, it is much more flexible, which we exploit here. -class Fuunctor f => Apply f where +class Functor f => Apply f where (<*>) :: f (a -> b) -> f a @@ -582,7 +584,7 @@ class Apply f => Applicative f where unit :: a -> f a -class Fuunctor f => Extend f where +class Functor f => Extend f where (<<=) :: (f a -> b) -> f a @@ -593,7 +595,7 @@ class Extend f => Comonad f where f a -> a -class Fuunctor t => Traversable t where +class Functor t => Traversable t where traverse :: Applicative f => (a -> f b) @@ -604,7 +606,7 @@ class Fuunctor t => Traversable t where -- It will also come in use later. instance Traversable [] where traverse f = - foldr (\a b -> fmaap (:) (f a) <*> b) (unit []) + foldr (\a b -> fmap (:) (f a) <*> b) (unit []) -- Exercise 32 -- Relative Difficulty: 6