为什么haskell强制总是键入泛型类型?

Net*_*ave 2 haskell types

让我们说:

data Data a b c = Build (a,b,c) deriving Show

foo :: Data a b c -> Data a b c
foo d = d

main = print $ foo $ Build (1, 1, "a")
Run Code Online (Sandbox Code Playgroud)

有没有办法避免a b c每次使用数据类型的写入?

使用类型,如:

foo :: Data -> Data
foo d = d
Run Code Online (Sandbox Code Playgroud)

Dan*_*ner 11

是的,当然:

foo :: d ~ Data a b c => d -> d
Run Code Online (Sandbox Code Playgroud)


Tho*_*son 8

data Data a b c = Build (a,b,c) deriving Show
type D a t a' = Data a t a'

foo :: D a t a' -> D a t a'
foo d = d
Run Code Online (Sandbox Code Playgroud)

别名很有趣,但这不是一个严肃的答案.

编辑:一个严肃的版本,如果你知道一个常见的单态类型,那么别名就会变得有用:

type AppData = Data AppState AppValue AppResult
foo :: AppData -> AppData
...
Run Code Online (Sandbox Code Playgroud)