haskell - 从类型列表中获取常规列表

标签 haskell types ghc dependent-type

我找到了一种使用 ProxynatValNat 转换为 Integer 的方法可以在下面的代码中看到:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import Data.Proxy (Proxy)
import Data.Monoid ((<>))
import GHC.TypeLits

main :: IO ()
main = do
  fromNat (undefined :: Proxy 5)

fromNat :: KnownNat n => Proxy n -> IO ()
fromNat proxy = do
  let (num :: Integer) = natVal proxy -- converting a Nat to an Integer
  putStrLn $ "Some num: " <> show num

但是我想不出一种直接的方法将类型列表转换为常规列表,下面的代码甚至没有类型检查:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import Data.Proxy (Proxy)
import Data.Monoid ((<>))
import GHC.TypeLits

main :: IO ()
main = do
  fromNat     (undefined :: Proxy 5)
  fromListNat (undefined :: Proxy '[2,3,10])

fromNat :: KnownNat n => Proxy n -> IO ()
fromNat proxy = do
  let (num :: Integer) = natVal proxy -- converting a Nat to an Integer
  putStrLn $ "Some num: " <> show num

fromListNat :: Proxy [Nat] -> IO ()
fromListNat = undefined

如何将类型列表转换为常规列表?

最佳答案

答案是制作类似于 KnownNat 的内容,但用于 Nat 的类型级别列表。我们使用类型类在类型级列表上进行归纳。该类型类通过其父类(super class)约束检查您列出的所有元素是否满足 KnownNat,然后它将使用该事实重建术语级列表。

{-# LANGUAGE TypeOperators, KindSignatures #-}

-- Similar to `KnownNat (n :: Nat)`
class KnownNatList (ns :: [Nat]) where
   natListVal :: proxy ns -> [Integer]

-- Base case
instance KnownNatList '[] where
  natListVal _ = []

-- Inductive step
instance (KnownNat n, KnownNatList ns) => KnownNatList (n ': ns) where
  natListVal _ = natVal (Proxy :: Proxy n) : natListVal (Proxy :: Proxy ns)

然后,fromListNat 采用与 fromNat 相同的形状:

fromListNat :: KnownNatList ns => Proxy ns -> IO ()
fromListNat proxy = do
  let (listNum :: [Integer]) = natListVal proxy
  putStrLn $ "Some list of num: " <> show listNum

将这些更改拼接到您的初始代码中,我得到了预期的输出:

$ ghc Main.hs
$ ./Main
Some num: 5
Some list of num: [2,3,10]

关于haskell - 从类型列表中获取常规列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751129/

相关文章:

haskell - 如何使用镜头语法检查 map 是否有键?

haskell - 从外部网络采样行为

sql - 错误 : return type mismatch in function declared to return

haskell - 有人可以提供解决可能的包冲突的故障排除步骤吗?

haskell - 使用 QuickCheck 生成存在类型?

haskell - 为预先存在的类型自动生成 `PersistEntity`

c++ - 我可以访问变量的类型以进行泛型编程吗?

c# - 将基类型转换为派生类型

haskell - 如何使 FFI 调用可中断

haskell - 定义类型同义词 (GHC) 的类型同义词时出现奇怪的错误