fork(1) download
  1. {-# LANGUAGE TypeFamilies #-}
  2. {-# LANGUAGE FlexibleInstances #-}
  3. {-# LANGUAGE FlexibleContexts #-}
  4. {-# LANGUAGE UndecidableInstances #-}
  5. {-# LANGUAGE OverlappingInstances #-}
  6. {-# LANGUAGE ScopedTypeVariables #-}
  7. {-# LANGUAGE ExistentialQuantification #-}
  8. {-# LANGUAGE MultiParamTypeClasses #-}
  9. {-# LANGUAGE ConstraintKinds #-}
  10. {-# LANGUAGE NoMonomorphismRestriction #-}
  11.  
  12. import GHC.Exts (Constraint)
  13.  
  14. type family Parent a
  15.  
  16. class C a b where
  17. f :: b -> a -> String
  18.  
  19. instance (C (Parent a) b) => C a b where
  20. f _ _ = f (undefined :: b) (undefined :: Parent a)
  21.  
  22. data A1 = A1
  23. data A2 = A2
  24. data A3 = A3
  25. data A4 = A4
  26.  
  27. type instance Parent A2 = A1
  28. type instance Parent A3 = A2
  29. type instance Parent A4 = A3
  30.  
  31. data F1 = F1
  32. data F3 = F3
  33.  
  34. instance C A1 F1 where
  35. f _ _ = "F1"
  36.  
  37. instance C A3 F3 where
  38. f _ _ = "F3"
  39.  
  40. type family Constraints t a :: Constraint
  41.  
  42. data D t = forall a. (Constraints t a) => D a
  43.  
  44. type instance Constraints A1 a = (C a F1)
  45. type instance Constraints A2 a = (C a F1, C a F3)
  46. type instance Constraints A3 a = (C a F1, C a F3)
  47. type instance Constraints A4 a = (C a F1, C a F3)
  48.  
  49. main =
  50. do
  51. putStrLn (f F1 A1)
  52. putStrLn (f F1 A2)
  53. putStrLn (f F1 A3)
  54. putStrLn (f F1 A4)
  55. putStrLn (f F3 A3)
  56. putStrLn (f F3 A4)
  57. putStrLn $ (\fn (D x) -> f fn x) F1 ((D A1) :: D A1)
  58. putStrLn $ (\fn (D x) -> f fn x) F1 ((D A2) :: D A1)
  59. putStrLn $ (\fn (D x) -> f fn x) F3 ((D A3) :: D A2)
  60.  
  61. h :: (Constraints t a) => b -> D t -> String
  62. h = \fn (D x) -> f fn (x :: a)
  63.  
Compilation error #stdin compilation error #stdout 0s 4584KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:62:18:
    Overlapping instances for C a0 b arising from a use of `f'
    Matching instances:
      instance [overlap ok] C (Parent a) b => C a b
        -- Defined at prog.hs:19:10
      instance [overlap ok] C A3 F3 -- Defined at prog.hs:37:10
      instance [overlap ok] C A1 F1 -- Defined at prog.hs:34:10
    (The choice depends on the instantiation of `b, a0'
     To pick the first instance above, use -XIncoherentInstances
     when compiling the other instance declarations)
    In the expression: f fn (x :: a)
    In the expression: \ fn (D x) -> f fn (x :: a)
    In an equation for `h': h = \ fn (D x) -> f fn (x :: a)

prog.hs:62:24:
    Could not deduce (a2 ~ a1)
    from the context (Constraints t a)
      bound by the type signature for
                 h :: (Constraints t a) => b -> D t -> String
      at prog.hs:61:6-44
    or from (Constraints t a1)
      bound by a pattern with constructor
                 D :: forall t a. (Constraints t a) => a -> D t,
               in a lambda abstraction
      at prog.hs:62:10-12
      `a2' is a rigid type variable bound by
           an expression type signature: a2 at prog.hs:62:24
      `a1' is a rigid type variable bound by
           a pattern with constructor
             D :: forall t a. (Constraints t a) => a -> D t,
           in a lambda abstraction
           at prog.hs:62:10
    In the second argument of `f', namely `(x :: a)'
    In the expression: f fn (x :: a)
    In the expression: \ fn (D x) -> f fn (x :: a)
stdout
Standard output is empty