简体   繁体   English

不具备n-ary功能

[英]Uncurry for n-ary functions

I have a type level numbers 我有一个类型级别的数字

data Z   deriving Typeable
data S n deriving Typeable

and n-ary functions (code from fixed-vector package) 和n-ary函数(来自固定向量包的代码)

-- | Type family for n-ary functions.
type family   Fn n a b
type instance Fn Z     a b = b
type instance Fn (S n) a b = a -> Fn n a b

-- | Newtype wrapper which is used to make 'Fn' injective. It's also a
--   reader monad.
newtype Fun n a b = Fun { unFun :: Fn n a b }

I need function like 我需要像这样的功能

uncurryN :: Fun (n + k) a b -> Fun n a (Fun k a b)

I read several articles about type level computations, but all about type safe list concatenation. 我读了几篇关于类型级计算的文章,但都是关于类型安全列表连接的。

This required a bit of care in unwrapping/rewrapping the Fun newtype. 这需要在解开/重新包装Fun newtype时要小心。 I also exploited the DataKinds extension. 我也利用了DataKinds扩展。

{-# LANGUAGE DataKinds, KindSignatures, TypeFamilies, 
    MultiParamTypeClasses, ScopedTypeVariables, FlexibleInstances #-}
{-# OPTIONS -Wall #-}

-- | Type-level naturals.
data Nat = Z | S Nat

-- | Type family for n-ary functions.
type family   Fn (n :: Nat) a b
type instance Fn Z     a b = b
type instance Fn (S n) a b = a -> Fn n a b

-- | Addition.
type family   Add (n :: Nat) (m :: Nat) :: Nat
type instance Add Z          m = m
type instance Add (S n)      m = S (Add n m)

-- | Newtype wrapper which is used to make 'Fn' injective.
newtype Fun n a b = Fun { unFun :: Fn n a b }

class UncurryN (n :: Nat) (m :: Nat) a b where
    uncurryN :: Fun (Add n m) a b -> Fun n a (Fun m a b)

instance UncurryN Z m a b where
    uncurryN g = Fun g

instance UncurryN n m a b => UncurryN (S n) m a b where
    uncurryN g = Fun (\x -> unFun (uncurryN (Fun (unFun g x)) :: Fun n a (Fun m a b)))

{- An expanded equivalent with more signatures:

instance UncurryN n m a b => UncurryN (S n) m a b where
    uncurryN g = let f :: a -> Fn n a (Fun m a b)
                     f x = let h :: Fun (Add n m) a b
                               h = Fun ((unFun g :: Fn (Add (S n) m) a b) x)
                           in unFun (uncurryN h :: Fun n a (Fun m a b))
                     in Fun f
-}

You can do this without any type classes by constructing a datatype which can represent the type Nat on the data level: 您可以通过构造一个数据类型来实现此目的,而不需要任何类型类,该数据类型可以表示数据级别上的Nat类型:

data Nat = Z | S Nat

type family   Fn (n :: Nat) a b
type instance Fn Z     a b = b
type instance Fn (S n) a b = a -> Fn n a b

type family   Add (n :: Nat) (m :: Nat) :: Nat
type instance Add Z          m = m
type instance Add (S n)      m = S (Add n m)

newtype Fun n a b = Fun { unFun :: Fn n a b }

data SNat (n :: Nat) where 
  SZ :: SNat Z
  SS :: SNat n -> SNat (S n)

uncurryN :: forall n m a b . SNat n -> Fun (Add n m) a b -> Fun n a (Fun m a b) 
uncurryN SZ f = Fun f
uncurryN (SS (n :: SNat n')) g = Fun (\x -> unFun (uncurryN n (Fun (unFun g x)) :: Fun n' a (Fun m a b)))

If you don't like explicitly mentioning the n parameter, thats ok since you can always go back and forth between a function which takes an parameter as a type class and which takes a parameter as data: 如果您不喜欢明确提及n参数,那就好了,因为您总是可以在将参数作为类型类并将参数作为数据的函数之间来回切换:

class SingI (a :: k) where
  type Sing :: k -> * 
  sing :: Sing a

instance SingI Z where 
  type Sing = SNat
  sing = SZ

instance SingI n => SingI (S n) where
  type Sing = SNat
  sing = SS sing 

toNatSing :: (SNat n -> t) -> (SingI n => t)
toNatSing f = f sing 

fromNatSing :: (SingI n => t) -> (SNat n -> t)
fromNatSing f SZ = f 
fromNatSing f (SS n) = fromNatSing f n 

uncurryN' :: SingI n => Fun (Add n m) a b -> Fun n a (Fun m a b) 
uncurryN' = toNatSing uncurryN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM