a way to convert partial functions to functions with Maybe's

Cafe, Is there a way (without going into the IO lands) to achieve this: data T = A | B | C func1 :: T -> T func1 A = B func1Fixed :: T -> Maybe T func1Fixed A = Just B func1Fixed _ = Nothing I want a function to generate func1Fixed, given func1. I guess this is doable with exception handling (catching the error from func1)? Is it the only way? Best, -- Ozgur Akgun

Excerpts from Ozgur Akgun's message of Tue Apr 13 12:02:06 +0200 2010:
Cafe,
Is there a way (without going into the IO lands) to achieve this: [..]
There's package for that on hackage: http://hackage.haskell.org/packages/archive/spoon/0.3/doc/html/Control-Spoon... However, this is somewhat controversial regarding _|_: http://www.reddit.com/r/haskell/comments/acasn/tired_of_functions_that_could...

On 2010-04-13 03:02, Ozgur Akgun wrote:
I want a function to generate func1Fixed, given func1.
Don't do that, it's unHaskellish. Bottom is for non-termination (which you absolutely cannot catch) plus some other conditions to be treated similarly. If the implementer of func1 decided to return pseudo-non-termination, that's what you get. -- Ashley Yakeley

On 13 April 2010 20:02, Ozgur Akgun
func1 :: T -> T func1 A = B
func1Fixed :: T -> Maybe T func1Fixed A = Just B func1Fixed _ = Nothing
Why not use Maybe for func1 in the first place? Or are you wanting to automagically make all uses of head, tail, etc. safe? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

On Wed, Apr 14, 2010 at 2:32 AM, Ivan Miljenovic
Why not use Maybe for func1 in the first place? Or are you wanting to automagically make all uses of head, tail, etc. safe?
In which case there is already the 'safe' package: http://hackage.haskell.org/package/safe

Oh well, thanks for all the response.
I have a module with lots of one line function definitons, and they just
*look* ugly when I wrap the return type with Maybe. This is a
straightforward, and trivial job (the conversion) and I really don't care
about other problems (such as non-termination plus some other conditions)
for the time being.
I am not attampting to remove Maybe completely, don't worry :)
I quite like it, and use it in most places. But now always apparently.
Best,
On 14 April 2010 08:50, Roel van Dijk
On Wed, Apr 14, 2010 at 2:32 AM, Ivan Miljenovic
wrote: Why not use Maybe for func1 in the first place? Or are you wanting to automagically make all uses of head, tail, etc. safe?
In which case there is already the 'safe' package: http://hackage.haskell.org/package/safe
-- Ozgur Akgun
participants (5)
-
Ashley Yakeley
-
Daniel Schoepe
-
Ivan Miljenovic
-
Ozgur Akgun
-
Roel van Dijk