7

我试图通过一个实现点积的简单示例来掌握类型级自然数的窍门。我代表这样的点积:

data DotP (n::Nat) = DotP [Int]
    deriving Show

现在,我可以为每个单独的点积大小创建一个幺半群实例(mappend实际的点积在哪里),如下所示:

instance Monoid (DotP 0) where
    mempty                      = DotP $ replicate 0 0
    mappend (DotP xs) (DotP ys) = DotP $ zipWith (*) xs ys

instance Monoid (DotP 1) where
    mempty                      = DotP $ replicate 1 0
    mappend (DotP xs) (DotP ys) = DotP $ zipWith (*) xs ys

instance Monoid (DotP 2) where
    mempty                      = DotP $ replicate 2 0
    mappend (DotP xs) (DotP ys) = DotP $ zipWith (*) xs ys

但我想定义一个更通用的实例,如下所示:

instance Monoid (DotP n) where
    mempty                      = DotP $ replicate n 0
    mappend (DotP xs) (DotP ys) = DotP $ zipWith (*) xs ys

我不确定如何将类型的数字转换为可以在 mempty 函数中使用的常规数字。


编辑:拥有一个在 O(1) 时间内运行的函数也很酷dotplength :: (DotP n) -> n,只需查找它是什么类型,而不必遍历整个列表。

4

1 回答 1

8

要获得Integer对应于类型级别 natural n,您可以使用

fromSing (sing :: Sing n) :: Integer

在摆弄了一下之后,我得到了这个来编译:

{-# LANGUAGE DataKinds, KindSignatures, ScopedTypeVariables #-}

import Data.Monoid
import GHC.TypeLits

data DotP (n :: Nat) = DotP [Int]
    deriving Show

instance SingI n => Monoid (DotP n) where
    mempty = DotP $ replicate (fromInteger k) 0
      where k = fromSing (sing :: Sing n)

    mappend (DotP xs) (DotP ys) = DotP $ zipWith (*) xs ys

dotplength :: forall n. SingI n => DotP n -> Integer
dotplength _ = fromSing (sing :: Sing n)
于 2012-10-17T00:31:56.543 回答