Contact/support | Changelog

mcomp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE MultiParamTypeClasses,
    FunctionalDependencies,
    FlexibleInstances,
    UndecidableInstances,
    TypeFamilies,
    OverlappingInstances
  #-}

import Data.Char

class ResultType x result | x -> result where
  resultType :: x -> result

instance result ~ x => ResultType x result where
  resultType x = x

instance ResultType y result => ResultType (x -> y) result where
  resultType f = let x = undefined
                     y = f x
                 in resultType y

class MComp f cp d result | f cp d -> result where
  mcomp' :: f -> (cp -> d) -> result

instance MComp cp cp result result where
  mcomp' f g = g f

instance MComp rest cp d result => MComp (a -> rest) cp d (a -> result) where
  mcomp' f g = \a -> mcomp' (f a) g

mcomp :: (ResultType f cp, MComp f cp d result) => f -> (cp -> d) -> result
mcomp = mcomp'

-- f1 :: Int -> Integer -> Char -> Int
f1 x y z = x + fromIntegral y + ord z

-- f2 :: Int -> String -> Bool -> Int
f2 x y z = x + length y + (if z then 1 else 0)

f = mcomp f1 f2

main = print $ f 2 3 'a' "ala" True
29:3: Error: Redundant lambda
Found:
mcomp' f g = \ a -> mcomp' (f a) g
Why not:
mcomp' f g a = mcomp' (f a) g