[Haskell-cafe] Factoring into type classes
Iavor Diatchki
iavor.diatchki at gmail.com
Tue Jan 20 14:12:20 EST 2009
Hello,
I don't mean to be negative here but I really fail to see how do any
of these ideas help the situation. (I do think it would be cool to
have a generic way to lift functions from one type to another that is
isomorphic to it). The fundamental problem is that there are multiple
functions of type Int -> Int -> Int (Int being just an example, for
the sake of concreteness), that can be the binary operation of a
monoid. Therefore, we cannot use the type system to determine how to
resolve the overloading of symbols like "mappend". Monoids are
general enough so that many types have multiple monoidal structure,
which is why I wrote that I don't think that they are good match for
the class system.
Defining a newtype and passing around isomorphisms seems more complex
to me than simply passing around the operations directly (i.e., not
using the class system). By the way, Miguel's preference can be coded
almost verbatim in ML using "local" declarations (I am referring to
the "let"-like construct that allows the definition of local values
that scope over multiple declarations) without any fancy type magic.
-Iavor
On Tue, Jan 20, 2009 at 8:42 AM, Conor McBride
<conor at strictlypositive.org> wrote:
> Hi folks
>
> I have been known to venture the viewpoint that the
> "newtype trick" might benefit from improved library
> support, for example, here
>
> http://www.mail-archive.com/haskell-cafe@haskell.org/msg37213.html
>
> This is in a similar vein to Derek's approach, if
> accompanied by a little more grotesque whizzbangery.
>
> On 19 Jan 2009, at 21:51, Derek Elkins wrote:
>
>> On Mon, 2009-01-19 at 12:10 -0800, Iavor Diatchki wrote:
>>>
>>> Sure, the point is that you are essentially adding a type annotation,
>>> which is like using a non-overloaded function. Compare, for example:
>>> "mappend add x y" and "getSum (mappend (Sum x) (Sum y))". I think
>>> that the first one is quite a bit more readable but, of course, this
>>> is somewhat subjective.
>>
>> data Iso a b = Iso { to :: a -> b, from :: b -> a }
>>
>> under :: Iso a b -> (b -> b) -> (a -> a)
>> under iso = to iso ~> from iso
>>
>> under2 :: Iso a b -> (b -> b -> b) -> (a -> a -> a)
>> under2 iso = to iso ~> under iso
>>
>> sumIso = Iso Sum getSum
>>
>> (+) = under2 sumIso mappend
>
>
> Perhaps it's worth trying to push in this direction,
> in search of a coherent kit.
>
> After all, there's a lot of structure out there.
>
> All the best
>
> Conor
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list