【问题标题】:Haskell: Existential Types and IOHaskell:存在类型和 IO
【发布时间】:2014-09-08 16:00:28
【问题描述】:

       Cross-posted at Code Review SE

在我尝试掌握 Haskell 中的存在类型时,我决定实现一个基于整数的固定长度向量数据类型。我正在使用 ghc 7.8.3。

具体来说,我想编写一个程序,要求用户提供一个可能的新值以附加到一个固定长度的向量,然后显示结果向量。

首先我编写了这样的程序的第一个版本:

{-# LANGUAGE GADTs                     #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE Rank2Types                #-}

import System.IO (hFlush, stdout)

data Z
data S n

data Vect n where
    ZV :: Vect Z
    CV :: Int -> Vect n -> Vect (S n)

data AnyVect = forall n. AnyVect (Vect n)

instance Show (Vect n) where
    show ZV = "Nil"
    show (CV x v) = show x ++ " : " ++ show v

vecAppend :: Int -> Vect n -> Vect (S n)
vecAppend x ZV = CV x ZV
vecAppend x v = CV x v

appendElem :: AnyVect -> IO AnyVect
appendElem (AnyVect v) = do
    putStr "> "
    hFlush stdout
    x <- readLn

    return $ if x == 0 then AnyVect v else AnyVect $ vecAppend x v

main = do
    AnyVect v <- appendElem $ AnyVect ZV
    putStrLn $ show v

按预期工作。然后我决定摆脱不必要的 AnyVect:

{-# LANGUAGE GADTs                     #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE Rank2Types                #-}

import System.IO (hFlush, stdout)

data Z
data S n

data Vect n where
    ZV :: Vect Z
    CV :: Int -> Vect n -> Vect (S n)

instance Show (Vect n) where
    show ZV = "Nil"
    show (CV x v) = show x ++ " : " ++ show v

vecAppend :: Int -> Vect n -> Vect (S n)
vecAppend x ZV = CV x ZV
vecAppend x v = CV x v

appendElem :: Vect n -> (forall n. Vect n -> a) -> IO a
appendElem v f = do
    putStr "> "
    hFlush stdout
    x <- readLn

    return $ if x == 0 then f v else f $ vecAppend x v

main = do
    appendElem ZV show >>= putStrLn

即使我不太喜欢 main 的编写方式,它也能正常工作。

还有其他更简单/更简洁的写法吗?

【问题讨论】:

  • 看起来这样更适合codereview?
  • 交叉发布到 codereview(我不知道它存在)。谢谢。
  • vecAppend = CV 更直接。我还将它重命名为 vecCons,因为“附加”听起来好像它适用于两个向量。

标签: haskell existential-type


【解决方案1】:

如果您不想在 GHC 7.8 中使用新的 TypeLits,您仍然可以使用 DataKinds 以及将 IO 和纯代码分开的典型重构来改进您的代码。

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

import System.IO (hFlush, stdout)

data Nat = Z | S Nat -- using DataKinds to promote Z and S to type level

-- don't restrict ourselves to only Vec of Int, we can be more general
data Vec (n :: Nat) a where
    Nil  :: Vec Z a
    Cons :: a -> Vec n a -> Vec (S n) a

instance Show a => Show (Vec n a) where
    show Nil = "Nil"
    show (Cons a v) = show a ++ " : " ++ show v

-- vecAppend in the OP, append is usually reserved for functions that
-- look like `Vec n a -> Vec m a -> Vec (n + m) a`
cons :: a -> Vec n a -> Vec (S n) a
cons = Cons

-- refactor the IO related code into a separate function
prompt :: IO Int
prompt = do
    putStr "> "
    hFlush stdout
    readLn

-- the "if 0 then ... else ..." could also be refactored into a separate 
-- function that takes a initial Vec as input
main = do
    x <- prompt
    if x == 0
    then print (Nil :: Vec Z Int)
    else print (cons x Nil)

【讨论】:

  • 这很好,但对于我实际打算做的事情,我的例子可能是一个坏例子。为了找到一个更好的,我想到了反转用户传递的向量,但似乎我一开始就无法编写反转函数。
猜你喜欢
  • 2021-03-24
  • 1970-01-01
  • 2013-05-04
  • 2012-06-08
  • 2013-03-16
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多