Update Trac ticket URLs to point to GitLab
[ghc.git] / testsuite / tests / gadt / karl1.hs
1 {-# LANGUAGE GADTs, KindSignatures #-}
2
3 -- See #301
4 -- This particular one doesn't use GADTs per se,
5 -- but it does use dictionaries in constructors
6
7 module Expr1 where
8
9 import Data.Kind (Type)
10
11 data Expr :: Type -> Type where -- Not a GADT at all
12 Const :: Show a => a -> Expr a
13 -- Note the Show constraint here
14 Var :: Var a -> Expr a
15
16 newtype Var a = V String
17
18 instance Show (Var a) where show (V s) = s
19
20 --------------------------
21 e1 :: Expr Int
22 e1 = Const 42
23
24 e2 :: Expr Bool
25 e2 = Const True
26
27 e3 :: Expr Integer
28 e3 = Var (V "mersenne100")
29
30 --------------------------
31 eval :: Expr a -> a
32 eval (Const c) = c
33 eval (Var v) = error ("free variable `" ++ shows v "'")
34
35 {-
36 Up to here, everything works nicely:
37
38 \begin{verbatim}
39 *Expr0> eval e1
40 42
41 *Expr0> eval e2
42 True
43 *Expr1> eval e3
44 *** Exception: free variable `mersenne100'
45 \end{verbatim}
46
47 But let us now try to define a |shows| function.
48
49 In the following, without the type signature we get:
50 \begin{verbatim}
51 *Expr1> :t showsExpr
52 showsExpr :: forall a. (Show a) => Expr a -> String -> String
53 *Expr1> showsExpr e1 ""
54 "42"
55 *Expr1> showsExpr e2 ""
56 "True"
57 *Expr1> showsExpr e3 ""
58 "mersenne100"
59 \end{verbatim}
60
61 However, in the last case, the instance |Show Integer| was not used,
62 so should not have been required.
63 Therefore I would expect it to work as it is now, i.e.,
64 with the type signature:
65 -}
66
67 showsExpr :: Expr a -> ShowS
68 showsExpr (Const c) = shows c
69 showsExpr (Var v) = shows v
70
71 {-
72
73 We used to get a complaint about the |Const| alternative (then line
74 63) that documents that the constraint in the type of |Const| must
75 have been ignored:
76
77 No instance for (Show a)
78 arising from use of `shows' at Expr1.lhs:63:22-26
79 Probable fix: add (Show a) to the type signature(s) for `showsExpr'
80 In the definition of `showsExpr': showsExpr (Const c) = shows c
81 -}