SML 匹配表达式中的模式是否应该具有相同的类型?

Should the patterns in a SML match expression have the same type?

在 Ullman 的 SML 书中:

A match expression consists of one or more rules, which are pairs of the form

<pattern> => <expression>

The rules are separated by vertical bars, so the form of a match is:

<pattern 1> => <expression 1> |
<pattern 2> => <expression 2> |
<pattern n> => <expression n>

Each of the expressions following the =>'s must be of the same type, since any one of them could become the value of the match.

匹配表达式中的模式是否为表达式(因此它们具有类型)?

匹配表达式中的模式是否也应该具有相同的类型?

特别是当匹配表达式用于定义函数时,例如

val rec f = fn P1 => E1 | P2 => E2 | ... | Pn => En;

匹配表达式中的模式是否也应该具有相同的类型? (我猜是的,因为函数的参数是有类型的,我们不能把不同类型的参数赋给同一个参数。)

谢谢。

是的,就像表达式一样,模式也有类型。并且匹配中不同模式的类型必须相同。

对于case,pattern的类型也必须和检查者表达式的类型相同。对于函数,模式类型是函数的参数类型。