Types OCaml期望的模式不';在模式匹配过程中,任何位置都不会出现
线路Types OCaml期望的模式不';在模式匹配过程中,任何位置都不会出现,types,pattern-matching,ocaml,Types,Pattern Matching,Ocaml,线路 Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] -> 提出错误 37 | Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] -> ^^^^^^^^^^^^^ Error: This pattern
Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] ->
提出错误
37 | Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] ->
^^^^^^^^^^^^^
Error: This pattern matches values of type regexp_t
but a pattern was expected which matches values of type
int list * int list
33 | Empty_String -> let x = fresh() and y = fresh() in
^^^^^^^^^^^^
Error: This variant pattern is expected to have type int list
The constructor Empty_String does not belong to type list
当a、b显然需要regexp\u to\u nfa函数使用类型regexp\u t时,为什么编译器会认为int list*int list需要匹配
带有类似错误的问题表明错误源于前面的表达式,其类型为int list*int list。但是,如果我将Concat行移到开头(在空字符串->之前),我会得到错误
37 | Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] ->
^^^^^^^^^^^^^
Error: This pattern matches values of type regexp_t
but a pattern was expected which matches values of type
int list * int list
33 | Empty_String -> let x = fresh() and y = fresh() in
^^^^^^^^^^^^
Error: This variant pattern is expected to have type int list
The constructor Empty_String does not belong to type list
,这表明Concat((a,b))或使用它的行中的某物具有int-list类型,这就没有什么意义了
作为参考,以下是我正在使用的类型和我正在尝试调试的函数的代码:
type ('q, 's) transition = 'q * 's option * 'q
type ('q, 's) nfa_t = {
sigma : 's list;
qs : 'q list;
q0 : 'q;
fs : 'q list;
delta : ('q, 's) transition list;
}
type regexp_t =
| Empty_String
| Char of char
| Union of regexp_t * regexp_t
| Concat of regexp_t * regexp_t
| Star of regexp_t
let fresh =
let cntr = ref 0 in
fun () ->
cntr := !cntr + 1 ;
!cntr
let rec regexp_to_nfa (regexp: regexp_t) : (int, char) nfa_t = match regexp with
Empty_String -> let x = fresh() and y = fresh() in
{sigma = []; qs = [x;y]; q0 = x; fs = [y]; delta = []}|
Char(a) -> let x = fresh() and y = fresh() in
{sigma = [a]; qs = [x;y]; q0 = x; fs = [y]; delta = [(x,Some a,y)]}|
Union((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b and r = fresh() and s = fresh() in match (x.fs, y.fs) with ([t],[u]) ->
{sigma = Sets.union x.sigma y.sigma; qs = Sets.union (Sets.union x.qs y.qs) [r;s]; q0 = r; fs = [s];
delta = Sets.union (Sets.union x.delta y.delta) [(r, None, x.q0); (r,None,y.q0); (t,None,s); (u,None,s)]}|
Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] ->
{sigma = Sets.union x.sigma y.sigma; qs = Sets.union x.qs y.qs; q0 = x.q0; fs = y.fs;
delta = Sets.union (Sets.union x.delta y.delta) [(t,None,y.q0)]} |
Star(a) -> let x = regexp_to_nfa a and r = fresh() and s = fresh() in match x.fs with [t] ->
{sigma = x.sigma; qs = Sets.union x.qs [r;s]; q0 = r; fs = [s];
delta = Sets.union x.delta [(r, None, x.q0);(r,None,s);(s,None,r);(t, None, r)]}
您有一个嵌套的
匹配项
。因此,Concat
模式被解析为嵌套匹配的一部分
您需要使用开始/结束
或括号来分隔嵌套在另一个匹配
中的匹配
如果继续使用嵌套匹配,还需要处理(x.fs,y.fs)
没有预期形式的情况(即,其中一个列表的长度不是1)。如果你确定它们从来都不是空的,你可以这样重写:
Union((a,b)) ->
let x = regexp_to_nfa a
and y = regexp_to_nfa b
and r = fresh()
and s = fresh() in
{ sigma = Sets.union x.sigma y.sigma;
qs = Sets.union (Sets.union x.qs y.qs) [r;s];
q0 = r;
fs = [s];
delta = Sets.union (Sets.union x.delta y.delta)
[(r, None, x.q0);
(r, None, y.q0);
(List.hd x.fs, None, s);
(List.hd y.fs, None, s)]
}|
Concat((a,b)) -> . . .
(值得一提的是,您使用的是一种非常密集的编码风格。最好是更松散地格式化,甚至使用格式化工具来自动排列代码。)关于使用List.hd的好主意。递归应确保x.fs、y.fs的大小始终正好为1,我的原始匹配语句试图利用这一点。但是OCaml穷举性检查(通常非常棒)不知道您知道什么。因此,您可能会收到警告,或者必须插入注释(这些注释很难看),才能关闭警告。