Re: [Haskell-cafe] monad constraint + record update
On Mon, Dec 22, 2008 at 06:19:07PM +0100, Peter Padawitz wrote: I'd like to define a monad Set for types in the class Eq. But how can the arguments of Set be constrained when Set is defined as an instance of Monad? instance Eq a = Monad Set where ... obviously cannot work. Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Peter Note there is already a package on Hackage to do exactly this, rmonad. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rmonad -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] monad constraint + record update
Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Oh, I missed this question the first time. You probably want Functional References: http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.details -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] monad constraint + record update
I think that Conal Elliot's Semantic Editor Combinators do the thing even better: http://conal.net/blog/posts/semantic-editor-combinators/ 2008/12/23 Brent Yorgey byor...@seas.upenn.edu: Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Oh, I missed this question the first time. You probably want Functional References: http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.details -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Евгений Кирпичев Разработчик Яндекс.Маркета ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] monad constraint + record update
Brent Yorgey schrieb: On Mon, Dec 22, 2008 at 06:19:07PM +0100, Peter Padawitz wrote: I'd like to define a monad Set for types in the class Eq. But how can the arguments of Set be constrained when Set is defined as an instance of Monad? instance Eq a = Monad Set where ... obviously cannot work. Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Peter Note there is already a package on Hackage to do exactly this, rmonad. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/rmonad And also http://hackage.haskell.org/cgi-bin/hackage-scripts/package/data-accessor ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] monad constraint + record update
I'd like to define a monad Set for types in the class Eq. But how can the arguments of Set be constrained when Set is defined as an instance of Monad? instance Eq a = Monad Set where ... obviously cannot work. Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Peter ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] monad constraint + record update
You can use a continuation trick I describe below. First of all, I would like to work in a more general situation. So, instead of working with Set, I'd like to declare two classes: class Returnable m a where ret :: a - m a and class Bindable m a b where bind :: m a - (a - m b) - m b I'm sure you're able to define instances like instance Ord a = Returnable Set a by yourself. You'll need MultiParamTypeClasses and FlexibleInstances for that to work. Now, the trick: newtype Restricted r m a = Restricted ((a - m r) - m r) instance Monad (Restricted r m) where return x = Restricted $ \h - h x Restricted g = f = Restricted $ \h - g $ \x - let Restricted g' = f x in g' h Quite simple, and doesn't mention pseudo-monadic structure of m at all. Now, the fun part: embed :: Bindable m a r = m a - Restricted r m a embed mx = Restricted (bind mx) unembed :: Returnable m r = Restricted r m r - m r unembed (Restricted g) = g ret You may also want another class class Summable m a where zero :: m a plus :: m a - m a - m a and now you can have instance Summable m r = MonadPlus (Restricted r m) where mzero = Restricted $ const zero Restricted g1 `mplus` Restricted g2 = Restricted $ \h - g1 h `plus` g2 h From now on, you can do something like that: unembed $ do x - embed $ Set.fromList [6,2,3] (do y - return x z - embed $ Set.fromList [1..2] guard $ y 5 return $ y + z) `mplus` return 10 and have fromList [3,4,5,10], as expected On 22 Dec 2008, at 20:19, Peter Padawitz wrote: I'd like to define a monad Set for types in the class Eq. But how can the arguments of Set be constrained when Set is defined as an instance of Monad? instance Eq a = Monad Set where ... obviously cannot work. Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Peter ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] monad constraint + record update
On Mon, Dec 22, 2008 at 6:19 PM, Peter Padawitz peter.padaw...@udo.edu wrote: I'd like to define a monad Set for types in the class Eq. But how can the arguments of Set be constrained when Set is defined as an instance of Monad? instance Eq a = Monad Set where ... obviously cannot work. Shouldn't you impose the constraint when defining functions operating on Set values? I guess that the contraint should be enforced not only when dealing with monadic code, but also whenever it appears in your program. Is there a standard update function for fields in data types, something that OO programmers do with assignments like obj.attr := value ? Your statement doesn't make much sense in the functional world of Haskell. There's a record update construct which may resemble the OO data update operation but it makes a whole new copy of the original value, i.e.: foo a = a { attr = value } which is basically like doing: foo a = a + 2 Cristiano ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe