2

我对 Haskell 有点陌生,并且正在开发一个项目,其中包含以下代码:

data Nested a = Elem a | Nested [Nested a] deriving (Eq, Show)
data Symbol a = Value a | Transformation (a -> a -> a) deriving (Show)


toSymbol :: [Char] -> Nested (Symbol Integer)
toSymbol x  
|all isDigit x = Elem (Value (strToInt x))
|x == "+" = Elem (Transformation (\x y -> x + y))

有没有办法可以避免这个函数的类型被限制为嵌套(符号整数)?我想使用 Symbol 来表示许多不同的类型,并有一个函数 toSymbol 类似于以下内容:

toSymbol x  
|x == "1" = Elem (Value 1)
|x == "+" = Elem (Transformation (\x y -> x + y))
|x == "exampleword" = Elem (Value "word")
|x == "concatenate()" = Elem (Transformation concatTwoStrings)

我不知道这样的函数的类型签名可能是什么。我能做些什么来获得类似的功能吗?

4

2 回答 2

3

您无法弄清楚类型签名的原因是因为您正在尝试“使函数的返回类型取决于传递的字符串的值,也就是依赖类型编程”,字符串的值将是仅在运行时可用。

因此,基本上,如果您尝试说 : toSymbol :: String -> Nested (Symbol a),则a取决于字符串的运行时值以及编译器抱怨它的原因。

有许多方法可以优化您的类型,以便所有部分组合在一起,一种可能的解决方案是使用一种新类型,它指定符号可能具有的不同类型的值。下面是示例:

data Nested a = Elem a | Nested [Nested a] deriving (Eq, Show)
data Symbol a = Value a | Transformation (a -> a -> a)
data SymbolType = SInteger Integer | SString String

addSymbols :: SymbolType -> SymbolType -> SymbolType
addSymbols (SInteger a) (SInteger b) = SInteger (a+b)
addSymbols _ _ = error "Not possible"

concatSymbols :: SymbolType -> SymbolType -> SymbolType
concatSymbols (SString a) (SString b) = SString (a++b)
concatSymbols _ _ = error "Not possible"

toSymbol :: String -> Nested (Symbol SymbolType)
toSymbol x  
  |x == "1" = Elem (Value (SInteger 1))
  |x == "+" = Elem (Transformation addSymbols)
  |x == "exampleword" = Elem (Value (SString "word"))
  |x == "concatenate()" = Elem (Transformation concatSymbols)
于 2013-10-30T06:53:12.877 回答
3

我认为不可能编写一个函数来做到这一点。一种可能的解决方案是使用类型类,它保持单一功能 API:

{-# LANGUAGE FlexibleInstances #-}

...

class Token a where
    toSymbol :: String -> Nested a

instance Token (Symbol Integer) where
    toSymbol x
        |all isDigit x = Elem (Value (read x))
        |x == "+" = Elem (Transformation (\x y -> x + y))
        |otherwise = error "Wrong type"

instance Token (Symbol String) where
    toSymbol "exampleword" = Elem (Value "word")
    toSymbol "concatenate()" = Elem (Transformation (++))
    toSymbol _ = error "Wrong type"
于 2013-10-30T06:13:44.423 回答