[Haskell-cafe] Re: Classes: functional dependency (type -> value)
Heinrich Apfelmus
apfelmus at quantentunnel.de
Tue May 12 07:32:23 EDT 2009
Belka wrote:
> Hello, communion people!
>
> I seek for your advice in a matter, where it's really hard for me to
> determine a good programming style.
> Here's the problem. I'm generalizing multiple authorization procedures to
> one, using class definition. (if of any interest, the code is in the end.)
> The problem essense is folowing:
> ----------------
> data SomeRole = Role1 | Role2 | Role3
>
> class SomeClass a b c | a -> b, c where
> f1 :: ...
> f2 :: ...
> ...
> fn :: ...
> role :: SomeRole -- <-- here is the problem
>
> I want to have a fuctional dependency from a type "a" on a value of *role*,
> so that I could easily "inspect" the *role* from within any other class
> members.
> Is it possible? Or do I rougly violate some style traditions?
The problem is that when you write
role
there is no way to choose the right instance? That is, where does the
compiler get a, b, c from when looking just at an invocation of role
? Therefore, the type of role has to involve a , for example as in
class SomeClass a b c ... where
...
role :: a -> SomeRole
and used as
role (undefined :: Foo)
That being said, I think that type classes are not what you want here. I
suggest to simply use a regular data type
data SomeThing a b c = SomeThing {
f1 :: ...
, f2 :: ...
...
, fn :: ...
, role :: SomeRole
}
Remember that f1, f2, ... can be functions, this is a functional
language, after all! "Instances" are then simply a concrete value, like
for example
thething :: SomeThing Foo Bar Baz
thething = SomeThing {
f1 = id
, f2 = filter (>3) . map length
, ...
, role = Role1
}
Regards,
apfelmus
--
http://apfelmus.nfshost.com
More information about the Haskell-Cafe
mailing list