[Haskell-cafe] Newbie Q: Monad 'fail' and 'error'

Ilya Tsindlekht eilya497 at 013.net
Wed Jun 6 06:06:31 EDT 2007


On Wed, Jun 06, 2007 at 01:39:32PM +0400, Dmitri O.Kondratiev wrote:
> Monad class contains declaration
> 
> *fail* :: String -> m a
> 
> and provides default implementation for 'fail' as:
> 
> fail s = error s
> 
> On the other hand Prelude defines:
> *
> error* :: String -> a
> 
> which stops execution and displays an error message.
> 
> Questions:
> 1) What value and type 'error' actually returns in:
> error "some message" ?
'a' here is a type variable, therefore 'error' is a polymorphic function
which can return value of any type.
> 
> 2) How declaration
> String -> m a
> matches with
> String -> a ?
as I said above, 'String->a' means that the return value can have any type.
'String-> m a' means that return value may be of type 'm a' where 'a'
can be any type.
> 
> 3) In Maybe monad:
> fail = Nothing
'Nothing' is constructor which returns value of type 'Maybe a' where 'a'
is any type. 
> 
> When and how 'fail' is used in Maybe monad?
Values of type 'Maybe a' can be either 'Just x' where x is value of type
'a' or 'Nothing'. The Maybe monad is used to represent computations
which may fail, 'Just x' represents successful computation yielding
value x, and 'Nothing' represents failing computation.
> 
> Thanks!
> 
> -- 
> Dmitri O. Kondratiev
> dokondr at gmail.com
> http://www.geocities.com/dkondr

> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list