1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE DataKinds #-}
4 -- | This module gathers utilities for currying or uncurrying tuples
5 -- of size greater or equal to two.
6 module Symantic.Syntaxes.CurryN where
8 import Data.Function (($), (.))
9 import Data.Kind (Type)
11 import Symantic.Syntaxes.EithersOfTuples (Tuples)
15 -- | Produce and consume 'Tuples'.
16 -- Not actually useful for 'Symantic.Syntaxes.EithersOfTuples',
17 -- but related through the use of 'Tuples'.
18 class CurryN args where
19 -- Like 'curry' but for an arbitrary number of nested 2-tuples.
20 curryN :: (Tuples args -> res) -> args -..-> res
22 -- Like 'uncurry' but for an arbitrary number of nested 2-tuples.
23 uncurryN :: (args -..-> res) -> Tuples args -> res
25 -- Like 'fmap' on @('->')@ but for an arbitrary number of arguments.
26 mapresultN :: (a -> b) -> (args -..-> a) -> args -..-> b
28 instance CurryN '[a] where
32 instance CurryN (b ': as) => CurryN (a ': b ': as) where
33 curryN f x = curryN @(b ': as) (\xs -> f (x, xs))
34 uncurryN f (x, xs) = uncurryN @(b ': as) (f x) xs
35 mapresultN f as2r = mapresultN @(b ': as) f . as2r
37 -- ** Type family ('-..->')
38 type family (args :: [Type]) -..-> (r :: Type) :: Type where
40 (a : args) -..-> r = a -> args -..-> r
42 -- ** Type family 'Args'
43 type family Args (f :: Type) :: [Type] where
44 Args (a -> r) = a : Args r
47 -- ** Type family 'Result'
48 type family Result (as :: Type) :: Type where
49 Result (a -> r) = Result r