2021年3月30日星期二

OCaml expects pattern which doesn't appear anywhere during pattern matching

The line

Concat((a,b)) -> let x = regexp_to_nfa a and y = regexp_to_nfa b in match x.fs with [t] ->  

brings up the error "this pattern matches values of type regexp_t but a pattern was expected which matches values of type int list * int list" with Concat((a,b)) underlined. Why would the compiler think int list * int list needs to be matched when a, b clearly need type regexp_t for the regexp_to_nfa function to use them?

For reference, these are the types I'm working with and the code of the function I'm trying to debug:

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)]}  
https://stackoverflow.com/questions/66880311/ocaml-expects-pattern-which-doesnt-appear-anywhere-during-pattern-matching March 31, 2021 at 09:05AM

没有评论:

发表评论