0

函数从字节流中读取 ID。它知道 id 的大小 - 可以是 4 或 8 个字节。如何使返回类型多态?

(伪代码:)

    class (Integral a) => IdSize a where
      size :: a -> Int

    instance IdSize Int32 ...

    instance IdSize Int64 ...

    data Data = Data (Map (IdSize a) String)
    readData :: Data (Map (IdSize a) String)
    readId :: (forall a. IdSize a) => a -- kind of this, but not right

此 readId 将需要调用者的 IdSize 实例,但调用者不知道大小。同样,readData 返回的 Map 需要是多态的,但是调用者并不知道实际的类型。使用 Map 的函数将知道该类型。

4

3 回答 3

5

如果某事物只能有两种类型,那么它就不是“多态的”。这只是两种类型的不相交并集,或“和”类型。

import Data.Int

data Idx = I32 Int32
         | I64 Int64
  deriving (Show)

readId 4 _ = I32 0x12345678
readId _ _ = I64 0x1234567812345678

idSize (I32 _) = 4
idSize _       = 8

main :: IO ()
main = do
  let input = () -- this would be your input stream
  let idx1 = readId 4 input
  let idx2 = readId 8 input
  putStrLn $ "idx 1: size " ++ (show $ idSize idx1) ++ " value: " ++ (show idx1)
  putStrLn $ "idx 2: size " ++ (show $ idSize idx2) ++ " value: " ++ (show idx2)
  return ()

当您确实需要数据类型的类型签名具有更大的灵活性时,例如当您正在构建一个抽象语法树并希望将其限制为类型良好的结构时,GADT 是一种不错的方法:http://en.wikibooks .org/wiki/Haskell/GADT

这是 GADT 的示例:

{-# LANGUAGE GADTs              #-}
{-# LANGUAGE StandaloneDeriving #-}

import           Data.Int

data Idx a where
  I32 :: Int32 -> Idx Int32
  I64 :: Int64 -> Idx Int64

deriving instance Show (Idx a)

readId32 :: t -> Idx Int32
readId32 _ = I32 0x12345678

readId64 :: t -> Idx Int64
readId64 _ = I64 0x1234567812345678

idSize :: Num a => Idx t -> a
idSize (I32 _) = 4
idSize _       = 8

main :: IO ()
main = do
  let idx1 = readId32 ()
  let idx2 = readId64 ()
  putStrLn $ "idx 1: size " ++ (show $ idSize idx1) ++ " value: " ++ (show idx1)
  putStrLn $ "idx 2: size " ++ (show $ idSize idx2) ++ " value: " ++ (show idx2)
  return ()

我不确定这是否正是您所追求的,但它确实让您可以专门化类型,这样您就不能将Idx Int32s 与Idx Int64s 混合,但您仍然可以编写多态Idx a函数,例如idSize.

于 2013-08-20T23:12:50.790 回答
1

啊,好的,我们可以使用额外的包装类型来解决其中的一些问题:

        {-# LANGUAGE RankNTypes, ConstraintKinds, ExistentialQuantification #-}

        import Data.Int

        data Idx = forall a. IdSize a => Idx a

        instance Show Idx where
          show (Idx a) = show a

        class (Integral a, Show a) => IdSize a where
          size :: a -> Int

        instance IdSize Int32 where
          size _ = 4

        instance IdSize Int64 where
          size _ = 8

        readId :: Int -> Idx
        readId 4 = Idx (4 :: Int32)
        readId _ = Idx (8 :: Int64)

        main = print $ readId 8

那么也许 Data 将持有一个 Map Id 字符串。

于 2013-08-20T19:31:58.560 回答
1

以下工作可以满足我的要求:

    {-# LANGUAGE RankNTypes, ExistentialQuantification #-}

    import Data.Int
    import Data.Typeable
    import qualified Data.Map as M

    data H = forall a. IdSize a => H a (M.Map a String)

    class (Integral a, Show a, Typeable a) => IdSize a where
      size :: a -> Int
      readId :: a

    instance IdSize Int32 where
      size _ = 4
      readId = 4

    instance IdSize Int64 where
      size _ = 8
      readId = 8

    use :: (forall a. IdSize a => a -> M.Map a String -> b) -> H -> b
    use f (H i m) = f i m

    idSize :: H -> Int
    idSize (H i _) = size i

    mkH :: Int -> H
    mkH 4 = H (4 :: Int32) (M.singleton (4 :: Int32) "int32")
    mkH _ = H (8 :: Int64) (M.singleton (8 :: Int64) "int64")

    main = print $ use (M.lookup . const readId) $ mkH 4

mkH 可用于构造一个对调用者不透明的 H。然后调用者可以传递一个要使用的函数,该函数将解构 H 并调用给定的函数。该函数必须是 RankN 多态的 - 它应该适用于任何 IdSize 实例。这就是隐藏IdSize实现的设计意图。

于 2013-08-21T15:24:09.487 回答