[Haskell-cafe] Functional dependencies and overloading of operation
Kristoffer Føllesdal
kfollesdal at gmail.com
Mon Feb 29 08:08:01 UTC 2016
Internally in programs I will use the specific functions for each case. But I want some «syntactic sugar» for this functions when doing direct calculations in for example ghci. It will be more user friendly for direct interaction not to have four different functions that in practice do the same thing.
Kristoffer
> Date: Sun, 28 Feb 2016 21:11:20 +0000
> From: Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk <mailto:tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk>>
> To: haskell-cafe at haskell.org <mailto:haskell-cafe at haskell.org>
> Subject: Re: [Haskell-cafe] Functional dependencies and overloading of
> operation
> Message-ID: <20160228211120.GB23479 at weber>
> Content-Type: text/plain; charset=iso-8859-1
>
> On Sun, Feb 28, 2016 at 10:00:42PM +0100, Kristoffer F?llesdal wrote:
>> I am trying to use Functional dependencies to overload a operation on
>> Vector space and its basis
> [...]
>> operation :: a -> a -> Vect k a
>> operation :: a -> Vect k a -> Vect k a
>> operation :: Vect k a -> a -> Vect k a
>> operation :: Vect k a -> Vect k a -> Vect k a
>
> This is unlikely to be a good idea in practice. Is there a good reason you
> can't use four different names for these operations?
>
> Date: Sun, 28 Feb 2016 22:00:42 +0100
> From: Kristoffer F?llesdal <kfollesdal at gmail.com <mailto:kfollesdal at gmail.com>>
> To: haskell-cafe at haskell.org <mailto:haskell-cafe at haskell.org>
> Subject: [Haskell-cafe] Functional dependencies and overloading of
> operation
> Message-ID: <84F1908D-05B0-4EAE-9CB7-50847BD65344 at gmail.com <mailto:84F1908D-05B0-4EAE-9CB7-50847BD65344 at gmail.com>>
> Content-Type: text/plain; charset="utf-8"
>
> I am trying to use Functional dependencies to overload a operation on Vector space and its basis (Use the Vector spaces module Math.Algebras.VectorSpace <https://hackage.haskell.org/package/HaskellForMaths-0.4.8/docs/Math-Algebras-VectorSpace.html <https://hackage.haskell.org/package/HaskellForMaths-0.4.8/docs/Math-Algebras-VectorSpace.html>>). I have tried to mimic the example for matrices and vectors from https://wiki.haskell.org/Functional_dependencies <https://wiki.haskell.org/Functional_dependencies> <https://wiki.haskell.org/Functional_dependencies <https://wiki.haskell.org/Functional_dependencies>>.
> I have tried different ways of defining classes and instances, but I do not get it to work.
>
> What I want is to have the ?same? function for these cases:
>
> operation :: a -> a -> Vect k a
> operation :: a -> Vect k a -> Vect k a
> operation :: Vect k a -> a -> Vect k a
> operation :: Vect k a -> Vect k a -> Vect k a
>
> Her are som sample code to illustrate what I want. Do anybody have an idea to how to solve it?
>
> {-# LANGUAGE MultiParamTypeClasses #-}
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE FunctionalDependencies #-}
>
> import Math.Algebras.VectorSpace
>
> linearExtension :: (Eq k, Num k, Ord a)
> => (a -> a -> Vect k a) -> Vect k a -> Vect k a -> Vect k a
> linearExtension f xs ys = linear (\x -> linear (f x) ys) xs
>
> data Tree t = Root t [Tree t] deriving(Eq, Show, Ord)
>
> op :: (Eq k, Num k, Ord t) => Tree t -> Tree t -> Vect k (Tree t)
> op x y = return x <+> return y
>
> opA :: (Eq k, Num k, Ord t) => Vect k (Tree t) -> Vect k (Tree t) -> Vect k (Tree t)
> opA = linearExtension op
>
> class Operation a b c | a b -> c where
> (<.>) :: a -> b -> c
>
> instance (Ord t) => Operation (Tree t) (Tree t) (Vect k (Tree t)) where
> (<.>)= op
>
> instance (Ord t) => Operation (Vect k (Tree t)) (Vect k (Tree t)) (Vect k (Tree t)) where
> (<.>) = opA
>
> instance (Ord t) => Operation (Tree t) (Vect k (Tree t)) (Vect k (Tree t)) where
> (<.>) x = opA (return x)
>
> instance (Ord t) => Operation (Vect k (Tree t)) (Tree t) (Vect k (Tree t)) where
> (<.>) x y = opA x (return y)
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160228/bf70981a/attachment-0001.html <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160228/bf70981a/attachment-0001.html>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160229/52acff20/attachment.html>
More information about the Haskell-Cafe
mailing list