具有文字和内射后继的类型级别的nat? (N-ary撰写)

时间:2013-12-28 01:22:16

标签: haskell ghc dependent-type

我正在将this n-ary complement推广到n - ary compose,但我在设置界面时遇到了麻烦。也就是说,我无法弄清楚如何在类型级别使用数字文字,同时仍然能够对后继者进行模式匹配。

滚动我自己的nats

使用roll-my-own nats,我可以使n - ary撰写作品,但我只能将n作为迭代后继者传递,而不是作为文字:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}

module RollMyOwnNats where

import Data.List (genericIndex)

-- import Data.Proxy
data Proxy (n::Nat) = Proxy

----------------------------------------------------------------
-- Stuff that works.

data Nat = Z | S Nat

class Compose (n::Nat) b b' t t' where
  compose :: Proxy n -> (b -> b') -> t -> t'

instance Compose Z b b' b b' where
  compose _ f x = f x

instance Compose n b b' t t' => Compose (S n) b b' (a -> t) (a -> t') where
  compose _ g f x = compose (Proxy::Proxy n) g (f x)

-- Complement a binary relation.
compBinRel :: (a -> a -> Bool) -> (a -> a -> Bool)
compBinRel = compose (Proxy::Proxy (S (S Z))) not

----------------------------------------------------------------
-- Stuff that does not work.

instance Num Nat where
  fromInteger n = iterate S Z `genericIndex` n
-- I now have 'Nat' literals:
myTwo :: Nat
myTwo = 2
-- But GHC thinks my type-level nat literal is a 'GHC.TypeLits.Nat',
-- even when I say otherwise:
compBinRel' :: (a -> a -> Bool) -> (a -> a -> Bool)
compBinRel' = compose (Proxy::Proxy (2::Nat)) not
{-
    Kind mis-match
    An enclosing kind signature specified kind `Nat',
    but `2' has kind `GHC.TypeLits.Nat'
    In an expression type signature: Proxy (2 :: Nat)
    In the first argument of `compose', namely
      `(Proxy :: Proxy (2 :: Nat))'
    In the expression: compose (Proxy :: Proxy (2 :: Nat)) not
-}

使用GHC.TypeLits.Nat

使用GHC.TypeLits.Nat,我得到了类型级的nat文字,但是我找不到任何后继构造函数,并且使用类型函数(1 +)不起作用,因为GHC(7.6.3) )不能推理类型函数的注入性:

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module UseGHCTypeLitsNats where

import GHC.TypeLits

-- import Data.Proxy
data Proxy (t::Nat) = Proxy

----------------------------------------------------------------
-- Stuff that works.

class Compose (n::Nat) b b' t t' where
  compose :: Proxy n -> (b -> b') -> t -> t'

instance Compose 0 b b' b b' where
  compose _ f x = f x

instance (Compose n b b' t t' , sn ~ (1 + n)) => Compose sn b b' (a -> t) (a -> t') where
  compose _ g f x = compose (Proxy::Proxy n) g (f x)

----------------------------------------------------------------
-- Stuff that does not work.

-- Complement a binary relation.
compBinRel , compBinRel' :: (a -> a -> Bool) -> (a -> a -> Bool)
compBinRel = compose (Proxy::Proxy 2) not
{-
    Couldn't match type `1 + (1 + n)' with `2'
    The type variable `n' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    In the expression: compose (Proxy :: Proxy 2) not
    In an equation for `compBinRel':
        compBinRel = compose (Proxy :: Proxy 2) not
-}
{-
    No instance for (Compose n Bool Bool Bool Bool)
      arising from a use of `compose'
    The type variable `n' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there is a potential instance available:
      instance Compose 0 b b' b b'
-}
compBinRel' = compose (Proxy::Proxy (1+(1+0))) not
{-
    Couldn't match type `1 + (1 + 0)' with `1 + (1 + n)'
    NB: `+' is a type function, and may not be injective
    The type variable `n' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Expected type: Proxy (1 + (1 + 0))
      Actual type: Proxy (1 + (1 + n))
    In the first argument of `compose', namely
      `(Proxy :: Proxy (1 + (1 + 0)))'
-}

我同意semantic editor combinators更优雅,更通用 - 具体而言,写(.) . (.) . ...n次)代替{{1}总是很容易} - 但我很沮丧,我不能让compose (Proxy::Proxy n) - ary作品和我预期的一样有效。此外,似乎我会遇到n的其他用途的类似问题,例如在尝试定义类型函数时:

GHC.TypeLits.Nat

更新:接受答案的摘要和改编

接受的答案中有很多有趣的东西, 但对我来说关键是GHC 7.6中的模板Haskell技巧 解决方案:这有效地让我向我的GHC添加类型级文字 7.6.3版本,已经有内射接班人。

使用上面的类型,我通过TH定义文字:

type family   T (n::Nat) :: *
type instance T 0     = ...
type instance T (S n) = ...

我将{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DataKinds #-} module RollMyOwnLiterals where import Language.Haskell.TH data Nat = Z | S Nat nat :: Integer -> Q Type nat 0 = [t| Z |] nat n = [t| S $(nat (n-1)) |] 声明移到新模块中以避免出现问题 导入循环。然后我修改了我的Nat模块:

RollMyOwnNats

2 个答案:

答案 0 :(得分:4)

不幸的是,由于最近的消息中指出了一致性问题,因此在目前发布的GHC版本(GHC 7.6.3)中原则上无法回答您的问题。 http://www.haskell.org/pipermail/haskell-cafe/2013-December/111942.html

虽然类型级数字看起来像数字,但它们不能保证完全像数字一样(并且它们没有)。我已经看到Iavor Diatchki及其同事在GHC中实现了正确的类型级算术(就像SMT求解器用作后端一样合理 - 也就是说,我们可以相信它)。在该版本发布之前,最好避免使用类型级别的数字文字,无论它们看起来多么可爱。

答案 1 :(得分:3)

编辑:重写了答案。它变得有点笨重(还有一点小车)。

GHC 7.6

由于类型级Nat在GHC 7.6中有些不完整(?),实现所需内容的最简单方法是GADT和类型族的组合。

{-# LANGUAGE GADTs, TypeFamilies #-}

module Nats where

-- Type level nats
data Zero
data Succ n

-- Value level nats
data N n f g where
    Z :: N Zero (a -> b) a
    S :: N n f g -> N (Succ n) f (a -> g)

type family Compose n f g
type instance Compose Zero (a -> b) a = b
type instance Compose (Succ n) f (a -> g) = a -> Compose n f g

compose :: N n f g -> f -> g -> Compose n f g
compose Z f x = f x
compose (S n) f g = compose n f . g

这个特定实现的优点是它不使用类型类,因此compose的应用程序不受单态限制的约束。例如,compBinRel = compose (S (S Z)) not将在没有类型注释的情况下键入check。

我们可以通过一点模板Haskell使这个更好:

{-# LANGUAGE TemplateHaskell #-}

module Nats.TH where

import Language.Haskell.TH

nat :: Integer -> Q Exp
nat 0 = conE 'Z
nat n = appE (conE 'S) (nat (n - 1))

现在我们可以写compBinRel = compose $(nat 2) not,这对于更大的数字来说更加愉快。有些人可能会认为这是“作弊”,但看到我们只是实施了一点点语法糖,我认为没关系:)

GHC 7.8

以下适用于GHC 7.8:

-- A lot more extensions.
{-# LANGUAGE DataKinds, FlexibleContexts, FlexibleInstances, GADTs, MultiParamTypeClasses, PolyKinds, TypeFamilies, TypeOperators, UndecidableInstances #-}

module Nats where

import GHC.TypeLits

data N = Z | S N

data P n = P

type family Index n where
    Index 0 = Z
    Index n = S (Index (n - 1))

-- Compose is defined using Z/S instead of 0, 1, ... in order to avoid overlapping.
class Compose n f r where
    type Return n f r
    type Replace n f r
    compose' :: P n -> (Return n f r -> r) -> f -> Replace n f r

instance Compose Z a b where
    type Return Z a b = a
    type Replace Z a b = b
    compose' _ f x = f x

instance Compose n f r => Compose (S n) (a -> f) r where
    type Return (S n) (a -> f) r = Return n f r
    type Replace (S n) (a -> f) r = a -> Replace n f r
    compose' x f g = compose' (prev x) f . g
      where
        prev :: P (S n) -> P n
        prev P = P

compose :: Compose (Index n) f r => P n -> (Return (Index n) f r -> r) -> f -> Replace (Index n) f r
compose x = compose' (convert x)
  where
    convert :: P n -> P (Index n)
    convert P = P

-- This does not type check without a signature due to the monomorphism restriction.
compBinRel :: (a -> a -> Bool) -> (a -> a -> Bool)
compBinRel = compose (P::P 2) not

-- This is an example where we compose over higher order functions.
-- Think of it as composing (a -> (b -> c)) and ((b -> c) -> c).
-- This will not typecheck without signatures, despite the fact that it has arguments.
-- However, it will if we use the first solution.
appSnd :: b -> (a -> b -> c) -> a -> c
appSnd x f = compose (P::P 1) ($ x) f

但是,这个实现有一些缺点,在源代码中有注释。

我尝试(并且失败)使用封闭类型族来自动推断组合索引。 可能可以推断出更高阶函数,如下所示:

-- Given r and f, where f = x1 -> x2 -> ... -> xN -> r, Infer r f returns N.
type family Infer r f where
    Infer r r = Zero
    Infer r (a -> f) = Succ (Infer r f)

但是,Infer不适用于具有多态参数的高阶函数。例如:

ghci> :kind! forall a b. Infer a (b -> a)
forall a b. Infer a (b -> a) :: *
= forall a b. Infer a (b -> a)

GHC无法展开Infer a (b -> a),因为它在匹配已关闭的系列实例时不执行发生检查。如果Infera被实例化,ba统一,那么GHC将不会与b -> a的第二种情况相匹配。