[Haskell-cafe] Re: How to "Show" an Operation?

Maciej Piechotka uzytkownik2 at gmail.com
Thu Jun 10 16:10:08 EDT 2010


On Thu, 2010-06-10 at 19:44 +0200, Martin Drautzburg wrote:
> On Thursday, 10. June 2010 00:08:34 Luke Palmer wrote:
> 
> > Or just:
> >
> > apply = val_of
> 
> > So, to summarize:  if you have something that isn't a function and you
> > want to use it like a function, convert it to a function (using
> > another function :-P).  That's all.  No syntax magic, just say what
> > you're doing.
> 
> Thanks Luke
> 
> The reason I was asking is the following: suppose I have some code which uses 
> some functions, and what it primarily does with those functions is CALL them 
> in different orders.
> 
> Now at a later point in time I decide I need to give names to  those functions 
> because at the end I need to print information about the functions which 
> together solved a certain problem. Think of my problem as "In which order do 
> I have to call f,g,h such that (f.g.h) 42 = 42?".
> 
> I don't want to change all places where those functions are called 
> into "apply" style. Therefore I was looking for some idiom like the python 
> __call__() method, which, when present, can turn just about anything into a 
> callable.
> 
> I could change the *definition* of my original functions into "apply" style 
> and the rest of the code would not notice any difference. But that does not 
> really help, because in the end I want to Show something like [g,h,f], but my 
> functions would no longer carry names.
> 
> Alternatively I could associate functions with names in some association 
> function, but that function simply has to "know to much" for my taste.
> 
> The thing is, I only need the names at the very end. Throughout the majority 
> of the computation they should stay out of the way.
> 
> 

data Named a = Named String a

instance Functor Named where
    f `fmap` (Named s v) = Named s (f v)

instance Applicative Named where
    pure x = Named "" x
    (Named s f) <*> (Named t v) = Named (s ++ "(" ++ t ++ ")") (f v)

instance Eq a => Eq (Named a) where
    (Named _ x) == (Named _ y) = x == y

instance Show (Named a) where
    show (Named s _) = s

namedPure :: Show a => a -> Named a
namedPure x = Named (show x) x

test :: Num a
     => (a -> a) -> (a -> a) -> (a -> a) -> [String]
test f g h = do
    [f', g', h'] <- permutations [Named "f" f, Named "g" g, Named "h" h]
    guard $ namedPure 42 == f' <*> g' <*> h' <*> namedPure 42
    return $ show f' ++ " . " ++ show g' ++ " . " ++ show h'

(code is not tested but it should work)

Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100610/1f6e0754/attachment.bin


More information about the Haskell-Cafe mailing list