fork download
  1. -- *whistles*
  2. {-# LANGUAGE MultiParamTypeClasses,
  3.   FunctionalDependencies,
  4.   FlexibleInstances,
  5.   OverlappingInstances,
  6.   UndecidableInstances,
  7.   FlexibleContexts #-}
  8.  
  9. -- actual thingy
  10.  
  11. class PipeFirst b f | f -> b where
  12. (|>) :: f -> (b -> c) -> c
  13.  
  14. class PipeFirst' r b f | f -> b where
  15. pipeImpl :: r -> f -> (b -> c) -> c
  16.  
  17. instance (IsFunction b r, PipeFirst' r b f) => PipeFirst b f where
  18. (|>) = pipeImpl (undefined :: r)
  19.  
  20. instance PipeFirst' HTrue b (a -> b) where
  21. pipeImpl _ f g x = (f x) |> g
  22.  
  23. instance PipeFirst' HFalse b (a -> b) where
  24. pipeImpl _ = flip (.)
  25.  
  26. main = print $ (f |> g) 1 2 3
  27. where f x y z = x + y + z
  28. g = (*2)
  29.  
  30. -- start black magic
  31.  
  32. class IsFunction a b | a -> b
  33. instance TypeCast f HTrue => IsFunction (a -> b) f
  34. instance TypeCast f HFalse => IsFunction a f
  35.  
  36. class TypeCast a b | a -> b, b -> a where
  37. typeCast :: a -> b
  38. class TypeCast' t a b | t a -> b, t b -> a where
  39. typeCast' :: t->a->b
  40. class TypeCast'' t a b | t a -> b, t b -> a where
  41. typeCast'' :: t->a->b
  42. instance TypeCast' () a b => TypeCast a b where
  43. typeCast x = typeCast' () x
  44. instance TypeCast'' t a b => TypeCast' t a b where
  45. typeCast' = typeCast''
  46. instance TypeCast'' () a a where
  47. typeCast'' _ x = x
  48.  
  49. data HTrue
  50. data HFalse
  51.  
  52. -- end of black magic
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:21:2:
    Couldn't match expected type `c' with actual type `a -> c'
      `c' is a rigid type variable bound by
          the type signature for
            pipeImpl :: HTrue -> (a -> b) -> (b -> c) -> c
          at prog.hs:21:2
    The equation(s) for `pipeImpl' have four arguments,
    but its type `HTrue -> (a -> b) -> (b -> c) -> c' has only three
    In the instance declaration for `PipeFirst' HTrue b (a -> b)'

prog.hs:24:20:
    Couldn't match type `c' with `a -> c'
      `c' is a rigid type variable bound by
          the type signature for
            pipeImpl :: HFalse -> (a -> b) -> (b -> c) -> c
          at prog.hs:24:2
    Expected type: (b -> c) -> (a -> b) -> c
      Actual type: (b -> c) -> (a -> b) -> a -> c
    In the first argument of `flip', namely `(.)'
    In the expression: flip (.)
    In an equation for `pipeImpl': pipeImpl _ = flip (.)
stdout
Standard output is empty