On Sat, Jun 6, 2015 at 11:04 AM, Keean Schupke <[email protected]> wrote:
> On 6 Jun 2015 11:46, "Matt Oliveri" <[email protected]> wrote:
>> What you still haven't told me is whether "a" from your examples is
>> itself a type. If so, what are its elements? If not, how can you apply
>> a type class to it? Without this information, you cannot expect me to
>> make much sense of this. Maybe it'd help if you actually showed me the
>> definition of Triangle.
>
> class Point2D container unit where
>     x_coord :: container -> unit
>     y_coord :: container -> unit
>
> instance Point2D (Vector Float) Float where
>     x_coord c = c[0]
>     y_coord c = c[1]
>
> class Triangle container point where
>     point1 :: container -> point
>     point2 :: container -> point
>     point3 :: container -> point
>
> instance Triangle Point2D  where
>     point1 p = p[0]
>     point2 p = p[1]
>     point3 p = p[2]
>
> We don't care what the generic container is used to hold the points, nor do
> we care about how many dimensions the points have.

Lovely, Keean. But since this is just an example for discussing
refinements vs. type classes, it might be simpler to stick with lists,
for the polygons, and pairs of floats, for 2D points.

More importantly, now I understand why members of Triangle are types
whose elements represent triangles. (And not themselves triangles.)

> class RightTriangle container unit where ...
>
> So RightTriangle is a predicate that is true if the type is a right
> triangle.

So how would you code that? See this is the part I don't know how to
do with Haskell type classes.

Actually, no. I do know a way to do it. But it seems awkward,
especially if you don't limit points to be 2D. You work around the
absence of actual predicates by presenting the data of a right
triangle so that it's right by construction. Like if points were 2D,
you could have a corner (point), base and height (scalar), flipped
(boolean), and rotation angle.

What I don't know how to do is simply state that one of the angles is
right. (Like, by saying that the dot product of two of the sides
(considered as vectors) is zero.)

> So the point is functions like scale:
>
> scale :: Polygon a unit => unit -> a -> a
>
> a is a type constrained by the type-classes, that's all we ever need to know
> about it.

OK. Now how would you state that scale preserves rightness of triangles?
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to