Difference between revisions of "The Other Prelude"

From HaskellWiki
Jump to navigation Jump to search
(Numeric Prelude)
 
(11 intermediate revisions by 4 users not shown)
Line 17: Line 17:
   
 
=== Taking Typeclasses Seriously ===
 
=== Taking Typeclasses Seriously ===
Following [[NotJustMaybe]] (unfortunately I can't find the page), functions should be generalized whenever possible. Of course, efficiency might be a concern but this is a fun project anyway.
+
Following [[Not just Maybe]], functions should be generalized whenever possible. Of course, efficiency might be a concern but this is a fun project anyway.
 
* <hask>concat</hask> means the same thing as <hask>join</hask>. We propose we don't use <hask>concat</hask> at all.
 
* <hask>concat</hask> means the same thing as <hask>join</hask>. We propose we don't use <hask>concat</hask> at all.
 
* <hask>concatMap</hask> is just <hask>(>>=)</hask>. That is, monadic functions are preferred over the same functions with different name.
 
* <hask>concatMap</hask> is just <hask>(>>=)</hask>. That is, monadic functions are preferred over the same functions with different name.
Line 23: Line 23:
 
=== The Hierarchy ===
 
=== The Hierarchy ===
   
Although, not Haskell 98, hierarchical modules will definitely be in Haskell'. We take it for granted.
+
Although, not Haskell98, hierarchical modules are already in Haskell2010. We take it for granted.
 
* <hask>TheOtherPrelude</hask> - Minimalistic module.
 
* <hask>TheOtherPrelude</hask> - Minimalistic module.
 
* <hask>TheOtherPrelude.Utilities</hask> - Convenient definitions. The reasoning behind its existence is that we want the Prelude to be very concise. It should not steal good names.
 
* <hask>TheOtherPrelude.Utilities</hask> - Convenient definitions. The reasoning behind its existence is that we want the Prelude to be very concise. It should not steal good names.
  +
* <hask>TheOtherPrelude.Legacy</hask> - providing as much backwards compatibility as possible
   
 
== Open Issues ==
 
== Open Issues ==
Line 47: Line 48:
   
 
<haskell>
 
<haskell>
  +
{-# LANGUAGE NoImplicitPrelude #-}
  +
 
module TheOtherPrelude where
 
module TheOtherPrelude where
   
import Prelude (id, const, flip) -- hide almost everything
+
import Prelude (id, const, flip, (.))
  +
-- hide almost everything
  +
-- in fact, we could do better, by just defining them here
   
 
-- The idea is to rename 'fmap'.
 
-- The idea is to rename 'fmap'.
Line 60: Line 65:
 
map :: (a -> b) -> f a -> f b
 
map :: (a -> b) -> f a -> f b
   
  +
-- definitely a bad idea, sorry Cale!
(.) :: (Functor f) => (a -> b) -> f a -> f b
+
-- (.) :: (Functor f) => (a -> b) -> f a -> f b
(.) = map
+
-- (.) = map
   
 
class (Functor p) => Applicative p where
 
class (Functor p) => Applicative p where
-- Minimal complete definition: return and (<*>).
+
-- Minimal complete definition: return and (<@>).
return :: a -> p a -- value lifting
+
pure :: a -> p a -- value lifting
(<*>) :: p (a -> b) -> p a -> p b -- lifted application
+
-- actually I think we should
  +
-- stick to return
  +
-- to make do notation work
  +
(<@>) :: p (a -> b) -> p a -> p b -- lifted application
 
(>>) :: p a -> p b -> p b -- when the second is independent of the first
 
(>>) :: p a -> p b -> p b -- when the second is independent of the first
   
pa >> pb = map (const id) pa <*> pb
+
pa >> pb = (const id) . pa <@> pb
--map f pa = return f <*> pa -- see Class system extension proposal, below
+
--map f pa = return f <@> pa -- see Class system extension proposal, below
   
 
apply :: (Applicative p) => p (a -> b) -> p a -> p b
 
apply :: (Applicative p) => p (a -> b) -> p a -> p b
apply = (<*>)
+
apply = (<@>)
   
 
class (Applicative m) => Monad m where
 
class (Applicative m) => Monad m where
Line 82: Line 91:
 
ma >>= k = join (map k ma)
 
ma >>= k = join (map k ma)
 
join mma = mma >>= id
 
join mma = mma >>= id
--mf <*> ma = mf >>= flip map ma -- see Class system extension proposal, below
+
--mf <@> ma = mf >>= flip map ma -- see Class system extension proposal, below
 
--ma >> mb = ma >>= const mb
 
--ma >> mb = ma >>= const mb
 
--map f ma = ma >>= return . f -- this depends on (.), which is map! Be careful.
 
--map f ma = ma >>= return . f -- this depends on (.), which is map! Be careful.
Line 109: Line 118:
 
-- (return a) `orElse` b = a
 
-- (return a) `orElse` b = a
 
orElse :: mo a -> mo a -> mo a
 
orElse :: mo a -> mo a -> mo a
  +
  +
class (Monad m) => MonadFail m where
  +
fail :: String -> m a
 
</haskell>
 
</haskell>
   
Line 119: Line 131:
 
-- this is the if-then-else proposal
 
-- this is the if-then-else proposal
 
-- the name has been chosen to reflect the magic of Church booleans!
 
-- the name has been chosen to reflect the magic of Church booleans!
  +
-- the order of arguments matches that of maybe and either.
boolean True x _ = x
 
boolean False _ y = y
+
boolean x _ True = x
 
boolean _ y False = y
 
</haskell>
 
</haskell>
   
Line 128: Line 141:
 
-- ''The Other Prelude'' is an alternative, not a replacement.
 
-- ''The Other Prelude'' is an alternative, not a replacement.
 
-- So we need to hide everything from the Prelude
 
-- So we need to hide everything from the Prelude
import Prelude ()
+
--import Prelude ()
  +
-- Now that we have it,
 
  +
{-# LANGUAGE NoImplicitPrelude #-}
 
-- This is just an example assuming there is nothing to hide
 
-- This is just an example assuming there is nothing to hide
 
import TheOtherPrelude
 
import TheOtherPrelude
Line 135: Line 149:
 
-- Hopefully, this module will contain lift,...
 
-- Hopefully, this module will contain lift,...
 
-- Standard convention is to use M.lift (instead of liftM)
 
-- Standard convention is to use M.lift (instead of liftM)
import qualified TheOtherPrelude.Monad.Kleisli as M
+
-- import qualified TheOtherPrelude.Monad.Kleisli as M
 
</haskell>
 
</haskell>
   
Line 141: Line 155:
   
 
* [[Class system extension proposal]] - Makes this proposal worth reading at last
 
* [[Class system extension proposal]] - Makes this proposal worth reading at last
  +
* [[Quantified contexts]] - Another important issue
 
* [[Functor hierarchy proposal]] - Making <hask>Monad m</hask> imply <hask>Functor m</hask> (adopted by ''The Other Prelude'').
 
* [[Functor hierarchy proposal]] - Making <hask>Monad m</hask> imply <hask>Functor m</hask> (adopted by ''The Other Prelude'').
  +
* [[Functor-Applicative-Monad Proposal]] - in essence the same proposal, perhaps showing this sentiment is more common than assumed
 
* [[If-then-else]] - Making <hask>if</hask> a function (partially adopted by ''The Other Prelude'', we are silent on the bigger issue of sugar).
 
* [[If-then-else]] - Making <hask>if</hask> a function (partially adopted by ''The Other Prelude'', we are silent on the bigger issue of sugar).
 
* [http://software.complete.org/missingh/static/doc/ MissingH] - Functions "missing" from the Haskell Prelude/libraries.
 
* [http://software.complete.org/missingh/static/doc/ MissingH] - Functions "missing" from the Haskell Prelude/libraries.
Line 147: Line 163:
 
* [[Mathematical prelude discussion]] - A [[Numeric Prelude]] in good shape already. Will a merger be ever possible?
 
* [[Mathematical prelude discussion]] - A [[Numeric Prelude]] in good shape already. Will a merger be ever possible?
 
* [[Prelude extensions]] and [[List function suggestions]] - Unlike ''The Other Prelude'' they ''enhance'' the Prelude.
 
* [[Prelude extensions]] and [[List function suggestions]] - Unlike ''The Other Prelude'' they ''enhance'' the Prelude.
* [http://haskell.org/hawiki/NotJustMaybe NotJustMaybe] - Instead of writing inside a specific monad (i.e. Maybe) write functions generalized on (Monad m)=> where possible.
+
* [[Not just Maybe]] - Instead of writing inside a specific monad (i.e. Maybe) write functions generalized on (Monad m)=> where possible.
   
 
[[Category:Proposals]]
 
[[Category:Proposals]]

Latest revision as of 22:37, 22 December 2010

Call For Contribution

This fun project, called The Other Prelude, is a creative reconstruction of the standard Prelude. By disregarding history and compatibility, we get a clean sheet.

Committee

This project has no committee whatsoever. Issues are discussed on the talk page.

Naming Conventions

  • Function names should be easy for beginners to consume.
  • Specifically, The Other Prelude naming convention is to use
    • descriptive symbols for functions that are naturally infix (e.g., mplus is replaced by (++))
    • whole English words and camelCase for functions (e.g., orElse but not fmap)

Design Philosophy

Taking Typeclasses Seriously

Following Not just Maybe, functions should be generalized whenever possible. Of course, efficiency might be a concern but this is a fun project anyway.

  • concat means the same thing as join. We propose we don't use concat at all.
  • concatMap is just (>>=). That is, monadic functions are preferred over the same functions with different name.

The Hierarchy

Although, not Haskell98, hierarchical modules are already in Haskell2010. We take it for granted.

  • TheOtherPrelude - Minimalistic module.
  • TheOtherPrelude.Utilities - Convenient definitions. The reasoning behind its existence is that we want the Prelude to be very concise. It should not steal good names.
  • TheOtherPrelude.Legacy - providing as much backwards compatibility as possible

Open Issues

  • When the same function has an infix and a prefix implementation, should one of them be outside the class to enforce consistency?
  • Should Prelude functions use Integer instead of Int? Maybe Integral n => n or Ix i => i in some cases?
  • Should String be a class rather than a type synonym?
  • The current proposal lacks a well thought fail mechanism. Should it be integrated into MonadZero, or have a class of his own, or remain in the Monad class?

Reality

What we have here right now is not ready to be adopted by existing projects. The class system extension proposal might make a difference.

The Code

Currently, the code is in Wiki form. If people do agree that the collaborative decisions begot something pretty, we'll have a group of files in darcs.haskell.org some time.

The imaginary Prelude as it stands:

TheOtherPrelude.hs

{-# LANGUAGE NoImplicitPrelude #-}

module TheOtherPrelude where

import Prelude (id, const, flip, (.)) 
-- hide almost everything
-- in fact, we could do better, by just defining them here

-- The idea is to rename 'fmap'.
-- Both map :: (a -> b) -> [a] -> [b]           (in [])
-- and  (.) :: (a -> b) -> (e -> a) -> (e -> b) (in (->) e)
-- are good names, and are intuitively prefix and infix respectively.
-- 'map' is aliased as (.) below.

class Functor f where
  map :: (a -> b) -> f a -> f b

-- definitely a bad idea, sorry Cale!
-- (.) :: (Functor f) => (a -> b) -> f a -> f b
-- (.) = map

class (Functor p) => Applicative p where
  -- Minimal complete definition: return and (<@>).
  pure   :: a -> p a                 -- value lifting
                                     -- actually I think we should
                                     -- stick to return
                                     -- to make do notation work
  (<@>)  :: p (a -> b) -> p a -> p b -- lifted application
  (>>)   :: p a -> p b -> p b        -- when the second is independent of the first

  pa >> pb = (const id) . pa <@> pb
--map f pa = return f <@> pa -- see Class system extension proposal, below

apply :: (Applicative p) => p (a -> b) -> p a -> p b
apply = (<@>)

class (Applicative m) => Monad m where
  -- Minimal complete definition: one of join or (>>=).
  (>>=) :: m a -> (a -> m b) -> m b -- bind
  join  :: m (m a) -> m a           -- combining levels of structure

  ma >>= k  = join (map k ma)
  join mma  = mma >>= id
--mf <@> ma = mf >>= flip map ma -- see Class system extension proposal, below
--ma >> mb  = ma >>= const mb
--map f ma  = ma >>= return . f -- this depends on (.), which is map!  Be careful.

-- We copy from the MonadPlus reform proposal (link below) now.
-- 'zero' will be used when pattern matching against refutable patterns in
-- do-notation as well as to provide support for monad comprehensions.

class (Monad mz) => MonadZero mz where
  -- Should satisfy 'left zero': zero >>= f = zero
  zero :: mz a

class (MonadZero mp) => MonadPlus mp where
  -- Should satisfy 'monoid':
  --   zero ++ b = b;  b ++ zero = b
  --   (a ++ b) ++ c = a ++ (b ++ c)
  -- and 'left distribution':
  --   (a ++ b) >>= f = (a >>= f) ++ (b >>= f)
  (++) :: mp a -> mp a -> mp a

class (MonadZero mo) => MonadOr mo where
  -- Should satisfy 'monoid':
  --   zero `orElse` b = b;  b `orElse` zero = b
  --   (a `orElse` b) `orElse` c = a `orElse` (b `orElse` c)
  -- and 'left catch':
  --   (return a) `orElse` b = a
  orElse :: mo a -> mo a -> mo a

class (Monad m) => MonadFail m where
  fail :: String -> m a

TheOtherPrelude/Utilities.hs

module TheOtherPrelude.Utilities where
import Prelude () -- hide everything

-- this is the if-then-else proposal
-- the name has been chosen to reflect the magic of Church booleans!
-- the order of arguments matches that of maybe and either.
boolean x _ True = x
boolean _ y False = y

How To Use

-- ''The Other Prelude'' is an alternative, not a replacement.
-- So we need to hide everything from the Prelude
--import Prelude ()
-- Now that we have it,
{-# LANGUAGE NoImplicitPrelude #-}
-- This is just an example assuming there is nothing to hide
import TheOtherPrelude

-- Hopefully, this module will contain lift,...
-- Standard convention is to use M.lift (instead of liftM)
-- import qualified TheOtherPrelude.Monad.Kleisli as M

See also