Haskell中类型参数的右类型签名

时间:2017-11-19 17:30:41

标签: haskell types polymorphism parametric-polymorphism type-signature

我有两种数据类型,想要编写一个返回这些数据类型数据的类:

+-- @angular/animations@5.0.2  invalid
+-- @angular/common@5.0.2  invalid
+-- @angular/compiler@5.0.2  invalid
+-- @angular/compiler-cli@5.0.2  invalid
| +-- chokidar@1.7.0
| | +-- anymatch@1.3.2
| | | +-- micromatch@2.3.11
| | | | +-- arr-diff@2.0.0
| | | | | `-- arr-flatten@1.1.0
| | | | +-- array-unique@0.2.1
| | | | +-- braces@1.8.5
| | | | | +-- expand-range@1.8.2
| | | | | | `-- fill-range@2.2.3
| | | | | |   +-- is-number@2.1.0
| | | | | |   +-- isobject@2.1.0
| | | | | |   +-- randomatic@1.1.7
| | | | | |   | +-- is-number@3.0.0
| | | | | |   | | `-- kind-of@3.2.2
| | | | | |   | `-- kind-of@4.0.0
| | | | | |   `-- repeat-string@1.6.1
| | | | | +-- preserve@0.2.0
| | | | | `-- repeat-element@1.1.2
| | | | +-- expand-brackets@0.1.5
| | | | | `-- is-posix-bracket@0.1.1
| | | | +-- extglob@0.3.2
| | | | +-- filename-regex@2.0.1
| | | | +-- kind-of@3.2.2
| | | | | `-- is-buffer@1.1.6
| | | | +-- object.omit@2.0.1
| | | | | +-- for-own@0.1.5
| | | | | | `-- for-in@1.0.2
| | | | | `-- is-extendable@0.1.1
| | | | +-- parse-glob@3.0.4
| | | | | +-- glob-base@0.3.0
| | | | | `-- is-dotfile@1.0.3
| | | | `-- regex-cache@0.4.4
| | | |   `-- is-equal-shallow@0.1.3
| | | |     `-- is-primitive@2.0.0
| | | `-- normalize-path@2.1.1
| | |   `-- remove-trailing-separator@1.1.0
| | +-- async-each@1.0.1
| | +-- glob-parent@2.0.0
| | +-- inherits@2.0.3
| | +-- is-binary-path@1.0.1
| | | `-- binary-extensions@1.11.0
| | +-- is-glob@2.0.1
| | | `-- is-extglob@1.0.0
| | +-- path-is-absolute@1.0.1
| | `-- readdirp@2.1.0
| |   +-- graceful-fs@4.1.11
| |   +-- minimatch@3.0.4
| |   | `-- brace-expansion@1.1.8
| |   |   +-- balanced-match@1.0.0
| |   |   `-- concat-map@0.0.1
| |   +-- readable-stream@2.3.3
| |   | +-- core-util-is@1.0.2
| |   | +-- isarray@1.0.0
| |   | +-- process-nextick-args@1.0.7
| |   | +-- safe-buffer@5.1.1
| |   | +-- string_decoder@1.0.3
| |   | `-- util-deprecate@1.0.2
| |   `-- set-immediate-shim@1.0.1
| +-- minimist@1.2.0
| +-- reflect-metadata@0.1.10
| `-- tsickle@0.24.1
|   +-- mkdirp@0.5.1
|   | `-- minimist@0.0.8
|   +-- source-map@0.5.7
|   `-- source-map-support@0.4.18
+-- @angular/core@5.0.2  invalid
+-- @angular/forms@5.0.2  invalid
+-- @angular/http@5.0.2  invalid
+-- @angular/platform-browser@5.0.2  invalid
+-- @angular/platform-browser-dynamic@5.0.2  invalid
| `-- tslib@1.8.0
+-- @angular/platform-server@5.0.2  invalid
| +-- domino@1.0.30
| `-- xhr2@0.1.4
+-- @angular/router@5.0.2  invalid
+-- rxjs@5.5.2
| `-- symbol-observable@1.0.4
+-- typescript@2.4.2  invalid
`-- UNMET DEPENDENCY zone.js@^0.8.17 

如果我只有一种类型D1或D2,我可以在类型签名中命名,但在有多种可能性的情况下我该怎么办?这甚至可能吗?

1 个答案:

答案 0 :(得分:8)

您需要制作D1D2D实例,而不是D1 aD2 a。然后,您可以extracta进行量化,并使extracta中返回D所有a

因为那可能不是很清楚(对不起):

class D d where
    -- `d` is the type constructor that's an instance of `D` (i.e. `D1` or
    -- `D2`) and `a` is a new type variable that can be any possible type
    extract :: d a -> a

instance D D1 where
    extract (Da1 a) = a
    extract (Db1 a) = a