c_wraith 2017-03-01 17:01:10
Costar, it defines a type family named example that's associated with the class.
c_wraith 2017-03-01 17:01:30
Costar, also called an associated type
c_wraith 2017-03-01 17:01:49
Costar, if you're familiar with rust, they took that feature wholesale
Costar 2017-03-01 17:02:09
c_wraith: it is like a "function of types"?
c_wraith 2017-03-01 17:02:25
Costar, type families in general are fun cti
c_wraith 2017-03-01 17:02:31
.. functions on types
Costar 2017-03-01 17:02:39
yeah
c_wraith 2017-03-01 17:02:42
associated types
c_wraith 2017-03-01 17:02:54
... I'm having operator issues. :)
c_wraith 2017-03-01 17:03:29
associated types are just defined as part of a class and it's instances, but are exactly as powerful
Costar 2017-03-01 17:03:59
c_wraith: they are used just to enhace type safety?
c_wraith 2017-03-01 17:04:22
Costar, no. they're used to expand the capability of of type classes.
c_wraith 2017-03-01 17:05:09
multiparameter type classes with functional dependencies give you similar power, but feel much more like prolog than writing functions.
jle` 2017-03-01 17:05:47
Costar: when you write a typeclass instance, you get to implement its methods
Costar 2017-03-01 17:06:00
c_wraith: I havent dig this part of Haskell deeply
jle` 2017-03-01 17:06:01
Costar: if the typeclass has an associated type, you also give the associated type
jle` 2017-03-01 17:06:07
it's just another "part" of the typeclass
jle` 2017-03-01 17:07:18
typeclasses let you define functions/values/types for each instance
mniip 2017-03-01 17:07:20
prolog is much more powerful than mptcs anyway
c_wraith 2017-03-01 17:07:23
Costar, https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/typefun.pdf gives a bunch of good examples
Costar 2017-03-01 17:07:40
c_wraith jle` thank you guys
c_wraith 2017-03-01 17:07:50
Costar, and it's very easy to read, even though it's an academic paper. :)
Costar 2017-03-01 17:09:10
c_wraith: I was reading about HLearn, and the guy defined a "type Ring a" inside a class
Costar 2017-03-01 17:09:18
c_wraith: now its clear
Costar 2017-03-01 17:10:13
jle`: i know the basics of typeclasses, but i fail in typefamilies.
jle` 2017-03-01 17:10:56
you don't really have to know about type families in general to use associated types, i think
jle` 2017-03-01 17:11:00
class Foo a where
jle` 2017-03-01 17:11:10
type Bar a
jle` 2017-03-01 17:11:21
bar :: a -> Int
jle` 2017-03-01 17:11:37
so if you write an instance of 'Foo', you provide an implementation of 'bar' (the function) and also of 'Bar' (the associated type
jle` 2017-03-01 17:11:48
instance Foo String where
jle` 2017-03-01 17:11:52
type Bar String = Bool
jle` 2017-03-01 17:11:54
bar = id
jle` 2017-03-01 17:11:56
ta dah
jle` 2017-03-01 17:11:59
that's it :)
jle` 2017-03-01 17:12:11
s/id/length
Costar 2017-03-01 17:12:50
jle`: nice example, thanks.
jle` 2017-03-01 17:41:25
no problem!