a beginner question: decorate-op-undecorate

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. What would be an idiomatic Haskell way to accomplish this? Currently I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which is my own version of partitionEithers that calls a selector first. Another option would be to map each Binding to a new datatype that has the Either on the outside, use partitionEithers, and map back. Thanks, Aran

On Fri, Feb 05, 2010 at 12:34:01PM -0500, Aran Donohue wrote:
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.
What would be an idiomatic Haskell way to accomplish this? Currently I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which is my own version of partitionEithers that calls a selector first. Another option would be to map each Binding to a new datatype that has the Either on the outside, use partitionEithers, and map back.
Thanks, Aran
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
You could try using uniplate[1], something like transformBi (either doWhatYouLike id) I guess :). [1] http://hackage.haskell.org/package/uniplate -- Felipe.

On Feb 5, 2010, at 12:34 , Aran Donohue wrote:
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.
What would be an idiomatic Haskell way to accomplish this? Currently I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which is my own version of partitionEithers that calls a selector first. Another option would be to map each Binding to a new datatype that has the Either on the outside, use partitionEithers, and map back.
Hm. Does it make sense to make this a Functor?
instance Functor Binding where fmap f (Binding v e) = Binding v (f e)
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Feb 5, 2010, at 19:13 , Brandon S. Allbery KF8NH wrote:
Hm. Does it make sense to make this a Functor?
instance Functor Binding where fmap f (Binding v e) = Binding v (f e)
Inaccurate/incomplete as written, since Functor expects kind (*) and Binding is (* -> *). You'd have to fix v to declare instances. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Fri, Feb 5, 2010 at 10:34 AM, Aran Donohue
What would be an idiomatic Haskell way to accomplish this? Currently I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which is my own version of partitionEithers that calls a selector first.
Since you are not using b or c anywhere else, the only thing you care about in that Either is whether it is Left or Right. Which makes it seem much more like a Bool. After this conversion, I can hoogle for your signature. http://haskell.org/hoogle/?hoogle=[a]+-%3E+%28a+-%3E+Bool%29+-%3E+%28[a]%2C[a]%29 Which gives, among other things, Data.List.partition :: (a -> Bool) -> [a] -> ([a],[a]). Without more details about the precise thing you want to accomplish, I don't know what else to say. Many idioms are about the details of the problem, even down to argument order. Luke

Thanks for the helpful thoughts.
I guess I was just reaching for a Haskell version of a programming pattern
from other languages---dealing with baggage if you will.
Thanks,
Aran
On Fri, Feb 5, 2010 at 7:24 PM, Luke Palmer
On Fri, Feb 5, 2010 at 10:34 AM, Aran Donohue
wrote: What would be an idiomatic Haskell way to accomplish this? Currently I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which is my own version of partitionEithers that calls a selector first.
Since you are not using b or c anywhere else, the only thing you care about in that Either is whether it is Left or Right. Which makes it seem much more like a Bool. After this conversion, I can hoogle for your signature.
http://haskell.org/hoogle/?hoogle=[a]+-%3E+%28a+-%3E+Bool%29+-%3E+%28[a]%2C[a]%29
Which gives, among other things, Data.List.partition :: (a -> Bool) -> [a] -> ([a],[a]).
Without more details about the precise thing you want to accomplish, I don't know what else to say. Many idioms are about the details of the problem, even down to argument order.
Luke

Hello Aran Changing to an explicit sum type rather than using Either might subsequent functions that process a Binding cleaner: data Binding = BoundVar Var Value | PossiblyBound Var [Value] Naturally you might want to consider a better constructor name than 'PossiblyBound'. -- As an open question to the list - the above change can be seen as a 'denormalisation' of the data type (adding redundancy), does anyone know of a reference that covers such things? The only thing I can think of close is David S. Wile's "Abstract syntax from concrete syntax"... Thanks Stephen
participants (5)
-
Aran Donohue
-
Brandon S. Allbery KF8NH
-
Felipe Lessa
-
Luke Palmer
-
Stephen Tetley