nntpnews.net

Global Usenet Archiver


Register

Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

Reply

  #1  
Old 06-02-10, 12:43 PM
John Lato
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

> From: Aran Donohue < - >
>
> Hi Haskell-Cafe,
>
> Consider a data type such as
>
> data Binding = Binding Var (Either Value [Value])
>
> representing a variable bound either to a fixed value or that has a list of
> possible values.
>
> I'd like to perform an operation on say, the fixed-value members of a list
> of bindings. Data.Either has "partitionEithers"---I'd essentially like to
> use partitionEithers, but in a way that it "peeks" into the value field of
> the binding. For the sake of argument, let's say I can't or can't modify
> Binding to move the Either to the outside.


I think that partitionEithers is leading you down the wrong trail. If
what you want to do is modify some values inside the binding, I would
start with this:

> mapVal :: (Either Value [Value] -> Either Value [Value]) -> Binding -> Binding
> mapVal f (Binding v e) = Binding v (f e)
>
> mapLeft :: (a -> b) -> Either a c -> Either b c
> mapLeft f = either (Left . f) Right
>
> -- mapRight is just fmap, but for symmetry
> mapRight :: (b -> c) -> Either a b -> Either a c
> mapRight = fmap
>
> modifyFixed :: (Value -> Value) -> Binding -> Binding
> modifyFixed f b = mapVal (mapLeft f) b
>
> modifyList :: ([Value] -> [Value]) -> Binding -> Binding
> modifyList f b = mapVal (mapRight f) b
>
> -- note that modifyFixed and modifyList have very nice point-free representations
> -- modifyFixed = mapVal . mapLeft
> -- modifyList = mapVal . mapRight


Now to apply this to a list:

> modifyFixedBindings :: (Value -> Value) -> [Binding] -> [Binding]
> modifyFixedBindings f binds = map (modifyFixed f) binds
> -- or point-free
> modifyFixedBindings' = map . modifyFixed


In my opinion, this would be more idiomatic if Binding were polymorphic:

> data Binding' k v = Binding' k v
>
> instance Functor (Binding' k) where
> fmap f (Binding' k v) = Binding' k (f v)
>
> type Binding2 = Binding' Var (Either Value [Value])


now mapVal is just fmap, and these functions are:

> modifyFixed2 :: (Val -> Val) -> [Binding2] -> [Binding2]
> modifyFixed2 = fmap . fmap . mapLeft
>
> modifyList2 :: ([Val] -> [Val]) -> [Binding2] -> [Binding2]
> modifyList2 = fmap . fmap . mapRight



I've typed out all the steps for clarity, but to be honest, I wouldn't
bother with the Fixed and List variants, unless you're going to use
them frequently. I would do just:

> mapVals :: (Either Value [Value] -> Either Value [Value]) -> [Binding] -> [Binding]
> mapVals f = map (\(Binding var val) -> Binding var (f val))


and leave it at that, using "mapVals" with the "either" function when
necessary. I would consider making Binding polymorphic, though, so
you can write the Functor instance.

You may also want to look at Data.Traversable.

Cheers,
John
_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 06-02-10, 08:14 PM
Aran Donohue
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

One way or the other, this has been a very productive question for getting
good pointers on what to read about next...

Thanks again.
Aran

On Sat, Feb 6, 2010 at 8:18 AM, Stephen Tetley < - >wrote:

> Hi John
>
> I'm not sure about making Binding polymorphic to get Functor,
> Traversable, Foldable...
>
> While I think you're correct that partitionEithers might not be a
> useful example to draw from in this case, I'd assume that Binding
> would be part of a larger syntax-tree, thus there might not be a
> appropriate single leaf to make the tree polymorphic on. Felipe
> Lessa's point - to use Uniplate or one of the Generics packages -
> might be a better candidate for implementing traversals.
>
> Best wishes
>
> Stephen
> _______________________________________________
> Haskell-Cafe mailing list
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.
>


_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-cafe] a beginner question: decorate-op-undecorate Aran Donohue fa.haskell 5 06-02-10 02:23 AM
[Haskell-cafe] Hayoo and Hoogle (beginner question) drostin77 fa.haskell 10 08-12-09 01:12 AM
[Haskell-beginners] decorate-sort-undecorate in haskell Ivan Uemlianin fa.haskell 7 11-07-09 11:28 AM
[Haskell-cafe] beginner question: assigning local variable to afunction Nico Rolle fa.haskell 6 07-05-09 09:45 PM
[Haskell-cafe] Beginner question Benjamin Bach fa.haskell 3 02-01-09 05:19 PM


All times are GMT +1. The time now is 04:58 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System