@@ -1,4 +1,5 @@
1−## Type Classes
1+Type Classes
3PureScript has basic support for type classes via the `class` and `instance` keywords.
4PureScript has basic support for type classes via the `class` and `instance` keywords.
@@ -6,24 +7,20 @@ Type class instances must be of the form `String`, `Number`, `Boolean`, `[t]` or
7Type class instances are resolved based on the order in which they appeared in the source files. In particular, overlapping instances are permitted.
8Type class instances are resolved based on the order in which they appeared in the source files. In particular, overlapping instances are permitted.
9−```haskell
10+Here is an example of the `Show` typeclass, with instances for Strings, Booleans and arrays::
10−class Show a where
12+ class Show a where
11− show :: a -> String
13+ show :: a -> String
13−instance Show String where
15+ instance Show String where
14− show s = s
16+ show s = s
16−instance Show Boolean where
18+ instance Show Boolean where
17− show true = "true"
19+ show true = "true"
18− show false = "false"
20+ show false = "false"
20−instance (Show a) => Show [a] where
22+ instance (Show a) => Show [a] where
21− show arr = "[" ++ showArray arr ++ "]"
23+ show [] = "[]"
23−showArray :: forall a. (Show a) => [a] -> String
25−showArray [x] = show x
26−showArray (x:xs) = show x ++ ", " ++ showArray xs
24+ show (x:xs) = show x ++ " : " ++ showArray xs
28−example = show [true, false]
26+ example = show [true, false]