1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Detection of partial matches and unused match cases. *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Typedtree
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
(*************************************)
|
|
|
|
(* Utilities for building patterns *)
|
|
|
|
(*************************************)
|
|
|
|
|
|
|
|
let make_pat desc ty tenv =
|
2012-05-30 07:52:37 -07:00
|
|
|
{pat_desc = desc; pat_loc = Location.none; pat_extra = [];
|
2013-03-25 07:16:07 -07:00
|
|
|
pat_type = ty ; pat_env = tenv;
|
|
|
|
pat_attributes = [];
|
|
|
|
}
|
2002-05-24 07:34:37 -07:00
|
|
|
|
|
|
|
let omega = make_pat Tpat_any Ctype.none Env.empty
|
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let extra_pat =
|
2012-05-30 07:52:37 -07:00
|
|
|
make_pat
|
|
|
|
(Tpat_var (Ident.create "+", mknoloc "+"))
|
|
|
|
Ctype.none Env.empty
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
let rec omegas i =
|
|
|
|
if i <= 0 then [] else omega :: omegas (i-1)
|
|
|
|
|
|
|
|
let omega_list l = List.map (fun _ -> omega) l
|
|
|
|
|
2003-08-14 18:35:24 -07:00
|
|
|
let zero = make_pat (Tpat_constant (Const_int 0)) Ctype.none Env.empty
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
(***********************)
|
|
|
|
(* Compatibility check *)
|
|
|
|
(***********************)
|
|
|
|
|
|
|
|
(* p and q compatible means, there exists V that matches both *)
|
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
let is_absent tag row = Btype.row_field tag !row = Rabsent
|
2004-01-09 05:12:17 -08:00
|
|
|
|
|
|
|
let is_absent_pat p = match p.pat_desc with
|
|
|
|
| Tpat_variant (tag, _, row) -> is_absent tag row
|
|
|
|
| _ -> false
|
2002-05-24 07:34:37 -07:00
|
|
|
|
2012-09-18 23:57:17 -07:00
|
|
|
let const_compare x y =
|
|
|
|
match x,y with
|
|
|
|
| Const_float f1, Const_float f2 ->
|
|
|
|
Pervasives.compare (float_of_string f1) (float_of_string f2)
|
2013-03-26 04:17:17 -07:00
|
|
|
| Const_string (s1, _), Const_string (s2, _) ->
|
|
|
|
Pervasives.compare s1 s2
|
2012-09-18 23:57:17 -07:00
|
|
|
| _, _ -> Pervasives.compare x y
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
let records_args l1 l2 =
|
2011-06-15 02:26:30 -07:00
|
|
|
(* Invariant: fields are already sorted by Typecore.type_label_a_list *)
|
2002-05-24 07:34:37 -07:00
|
|
|
let rec combine r1 r2 l1 l2 = match l1,l2 with
|
2011-06-15 02:26:30 -07:00
|
|
|
| [],[] -> List.rev r1, List.rev r2
|
2012-10-24 05:03:00 -07:00
|
|
|
| [],(_,_,p2)::rem2 -> combine (omega::r1) (p2::r2) [] rem2
|
|
|
|
| (_,_,p1)::rem1,[] -> combine (p1::r1) (omega::r2) rem1 []
|
|
|
|
| (_,lbl1,p1)::rem1, ( _,lbl2,p2)::rem2 ->
|
2002-05-24 07:34:37 -07:00
|
|
|
if lbl1.lbl_pos < lbl2.lbl_pos then
|
|
|
|
combine (p1::r1) (omega::r2) rem1 l2
|
|
|
|
else if lbl1.lbl_pos > lbl2.lbl_pos then
|
|
|
|
combine (omega::r1) (p2::r2) l1 rem2
|
|
|
|
else (* same label on both sides *)
|
|
|
|
combine (p1::r1) (p2::r2) rem1 rem2 in
|
|
|
|
combine [] [] l1 l2
|
|
|
|
|
|
|
|
|
|
|
|
let rec compat p q =
|
|
|
|
match p.pat_desc,q.pat_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_),_ -> compat p q
|
|
|
|
| _,Tpat_alias (q,_,_) -> compat p q
|
2002-05-24 07:34:37 -07:00
|
|
|
| (Tpat_any|Tpat_var _),_ -> true
|
|
|
|
| _,(Tpat_any|Tpat_var _) -> true
|
|
|
|
| Tpat_or (p1,p2,_),_ -> compat p1 q || compat p2 q
|
2010-01-22 04:48:24 -08:00
|
|
|
| _,Tpat_or (q1,q2,_) -> compat p q1 || compat p q2
|
2012-09-18 23:57:17 -07:00
|
|
|
| Tpat_constant c1, Tpat_constant c2 -> const_compare c1 c2 = 0
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_tuple ps, Tpat_tuple qs -> compats ps qs
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy p, Tpat_lazy q -> compat p q
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_, c1,ps1, _), Tpat_construct (_, c2,ps2, _) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
c1.cstr_tag = c2.cstr_tag && compats ps1 ps2
|
|
|
|
| Tpat_variant(l1,Some p1, r1), Tpat_variant(l2,Some p2,_) ->
|
2004-01-09 05:12:17 -08:00
|
|
|
l1=l2 && compat p1 p2
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_variant (l1,None,r1), Tpat_variant(l2,None,_) ->
|
2010-01-22 04:48:24 -08:00
|
|
|
l1 = l2
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_variant (_, None, _), Tpat_variant (_,Some _, _) -> false
|
|
|
|
| Tpat_variant (_, Some _, _), Tpat_variant (_, None, _) -> false
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (l1,_),Tpat_record (l2,_) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let ps,qs = records_args l1 l2 in
|
|
|
|
compats ps qs
|
|
|
|
| Tpat_array ps, Tpat_array qs ->
|
|
|
|
List.length ps = List.length qs &&
|
|
|
|
compats ps qs
|
|
|
|
| _,_ ->
|
|
|
|
assert false
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
and compats ps qs = match ps,qs with
|
|
|
|
| [], [] -> true
|
|
|
|
| p::ps, q::qs -> compat p q && compats ps qs
|
|
|
|
| _,_ -> assert false
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(****************************************)
|
|
|
|
(* Utilities for retrieving constructor *)
|
|
|
|
(* and record label names *)
|
|
|
|
(****************************************)
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
exception Empty (* Empty pattern *)
|
|
|
|
|
2009-05-20 04:52:42 -07:00
|
|
|
(* May need a clean copy, cf. PR#4745 *)
|
|
|
|
let clean_copy ty =
|
|
|
|
if ty.level = Btype.generic_level then ty
|
|
|
|
else Subst.type_expr Subst.identity ty
|
|
|
|
|
2003-05-02 02:57:53 -07:00
|
|
|
let get_type_path ty tenv =
|
2009-05-20 04:52:42 -07:00
|
|
|
let ty = Ctype.repr (Ctype.expand_head tenv (clean_copy ty)) in
|
2003-05-02 02:57:53 -07:00
|
|
|
match ty.desc with
|
|
|
|
| Tconstr (path,_,_) -> path
|
|
|
|
| _ -> fatal_error "Parmatch.get_type_path"
|
|
|
|
|
2012-05-29 06:41:14 -07:00
|
|
|
let get_type_descr ty tenv =
|
2003-06-19 08:53:53 -07:00
|
|
|
match (Ctype.repr ty).desc with
|
|
|
|
| Tconstr (path,_,_) -> Env.find_type path tenv
|
|
|
|
| _ -> fatal_error "Parmatch.get_type_descr"
|
|
|
|
|
|
|
|
let rec get_constr tag ty tenv =
|
1999-10-29 07:42:37 -07:00
|
|
|
match get_type_descr ty tenv with
|
2007-10-09 03:29:37 -07:00
|
|
|
| {type_kind=Type_variant constr_list} ->
|
1999-10-29 07:42:37 -07:00
|
|
|
Datarepr.find_constr_by_tag tag constr_list
|
2003-06-19 08:53:53 -07:00
|
|
|
| {type_manifest = Some _} ->
|
2009-05-20 04:52:42 -07:00
|
|
|
get_constr tag (Ctype.expand_head_once tenv (clean_copy ty)) tenv
|
1999-10-29 07:42:37 -07:00
|
|
|
| _ -> fatal_error "Parmatch.get_constr"
|
|
|
|
|
|
|
|
let find_label lbl lbls =
|
|
|
|
try
|
|
|
|
let name,_,_ = List.nth lbls lbl.lbl_pos in
|
|
|
|
name
|
2012-05-30 07:52:37 -07:00
|
|
|
with Failure "nth" -> Ident.create "*Unknown label*"
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2003-06-19 08:53:53 -07:00
|
|
|
let rec get_record_labels ty tenv =
|
1999-10-29 07:42:37 -07:00
|
|
|
match get_type_descr ty tenv with
|
2007-10-09 03:29:37 -07:00
|
|
|
| {type_kind = Type_record(lbls, rep)} -> lbls
|
2003-06-19 08:53:53 -07:00
|
|
|
| {type_manifest = Some _} ->
|
2009-05-20 04:52:42 -07:00
|
|
|
get_record_labels (Ctype.expand_head_once tenv (clean_copy ty)) tenv
|
1999-10-29 07:42:37 -07:00
|
|
|
| _ -> fatal_error "Parmatch.get_record_labels"
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(*************************************)
|
2002-05-24 07:34:37 -07:00
|
|
|
(* Values as patterns pretty printer *)
|
1999-10-29 07:42:37 -07:00
|
|
|
(*************************************)
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
open Format
|
|
|
|
;;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
let get_constr_name tag ty tenv = match tag with
|
2012-03-06 11:47:07 -08:00
|
|
|
| Cstr_exception (path, _) -> Path.name path
|
2002-05-24 07:34:37 -07:00
|
|
|
| _ ->
|
|
|
|
try
|
2012-05-30 07:52:37 -07:00
|
|
|
let name,_,_ = get_constr tag ty tenv in Ident.name name
|
2002-05-24 07:34:37 -07:00
|
|
|
with
|
|
|
|
| Datarepr.Constr_not_found -> "*Unknown constructor*"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
let is_cons tag v = match get_constr_name tag v.pat_type v.pat_env with
|
|
|
|
| "::" -> true
|
|
|
|
| _ -> false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2012-09-27 05:32:54 -07:00
|
|
|
let pretty_const c = match c with
|
|
|
|
| Const_int i -> Printf.sprintf "%d" i
|
|
|
|
| Const_char c -> Printf.sprintf "%C" c
|
2013-03-26 04:17:17 -07:00
|
|
|
| Const_string (s, _) -> Printf.sprintf "%S" s
|
2012-09-27 05:32:54 -07:00
|
|
|
| Const_float f -> Printf.sprintf "%s" f
|
|
|
|
| Const_int32 i -> Printf.sprintf "%ldl" i
|
|
|
|
| Const_int64 i -> Printf.sprintf "%LdL" i
|
|
|
|
| Const_nativeint i -> Printf.sprintf "%ndn" i
|
2008-07-09 06:03:38 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let rec pretty_val ppf v =
|
|
|
|
match v.pat_extra with
|
2013-03-25 07:16:07 -07:00
|
|
|
(cstr, _loc, _attrs) :: rem ->
|
2012-05-30 07:52:37 -07:00
|
|
|
begin match cstr with
|
|
|
|
| Tpat_unpack ->
|
|
|
|
fprintf ppf "@[(module %a)@]" pretty_val { v with pat_extra = rem }
|
|
|
|
| Tpat_constraint ctyp ->
|
|
|
|
fprintf ppf "@[(%a : _)@]" pretty_val { v with pat_extra = rem }
|
|
|
|
| Tpat_type _ ->
|
|
|
|
fprintf ppf "@[(# %a)@]" pretty_val { v with pat_extra = rem }
|
|
|
|
end
|
|
|
|
| [] ->
|
|
|
|
match v.pat_desc with
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_any -> fprintf ppf "_"
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_var (x,_) -> Ident.print ppf x
|
2012-09-27 05:32:54 -07:00
|
|
|
| Tpat_constant c -> fprintf ppf "%s" (pretty_const c)
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_tuple vs ->
|
|
|
|
fprintf ppf "@[(%a)@]" (pretty_vals ",") vs
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_, {cstr_tag=tag},[], _) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let name = get_constr_name tag v.pat_type v.pat_env in
|
|
|
|
fprintf ppf "%s" name
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_, {cstr_tag=tag},[w], _) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let name = get_constr_name tag v.pat_type v.pat_env in
|
|
|
|
fprintf ppf "@[<2>%s@ %a@]" name pretty_arg w
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_, {cstr_tag=tag},vs, _) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let name = get_constr_name tag v.pat_type v.pat_env in
|
|
|
|
begin match (name, vs) with
|
|
|
|
("::", [v1;v2]) ->
|
|
|
|
fprintf ppf "@[%a::@,%a@]" pretty_car v1 pretty_cdr v2
|
|
|
|
| _ ->
|
|
|
|
fprintf ppf "@[<2>%s@ @[(%a)@]@]" name (pretty_vals ",") vs
|
|
|
|
end
|
|
|
|
| Tpat_variant (l, None, _) ->
|
|
|
|
fprintf ppf "`%s" l
|
|
|
|
| Tpat_variant (l, Some w, _) ->
|
|
|
|
fprintf ppf "@[<2>`%s@ %a@]" l pretty_arg w
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (lvs,_) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
fprintf ppf "@[{%a}@]"
|
|
|
|
(pretty_lvals (get_record_labels v.pat_type v.pat_env))
|
|
|
|
(List.filter
|
|
|
|
(function
|
2012-10-24 05:03:00 -07:00
|
|
|
| (_,_,{pat_desc=Tpat_any}) -> false (* do not show lbl=_ *)
|
2002-05-24 07:34:37 -07:00
|
|
|
| _ -> true) lvs)
|
|
|
|
| Tpat_array vs ->
|
|
|
|
fprintf ppf "@[[| %a |]@]" (pretty_vals " ;") vs
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy v ->
|
|
|
|
fprintf ppf "@[<2>lazy@ %a@]" pretty_arg v
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (v, x,_) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
fprintf ppf "@[(%a@ as %a)@]" pretty_val v Ident.print x
|
|
|
|
| Tpat_or (v,w,_) ->
|
|
|
|
fprintf ppf "@[(%a|@,%a)@]" pretty_or v pretty_or w
|
|
|
|
|
|
|
|
and pretty_car ppf v = match v.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_,{cstr_tag=tag}, [_ ; _], _)
|
2002-05-24 07:34:37 -07:00
|
|
|
when is_cons tag v ->
|
|
|
|
fprintf ppf "(%a)" pretty_val v
|
|
|
|
| _ -> pretty_val ppf v
|
|
|
|
|
|
|
|
and pretty_cdr ppf v = match v.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_,{cstr_tag=tag}, [v1 ; v2], _)
|
2002-05-24 07:34:37 -07:00
|
|
|
when is_cons tag v ->
|
|
|
|
fprintf ppf "%a::@,%a" pretty_car v1 pretty_cdr v2
|
|
|
|
| _ -> pretty_val ppf v
|
|
|
|
|
|
|
|
and pretty_arg ppf v = match v.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_,_,_::_, _) -> fprintf ppf "(%a)" pretty_val v
|
2002-05-24 07:34:37 -07:00
|
|
|
| _ -> pretty_val ppf v
|
|
|
|
|
|
|
|
and pretty_or ppf v = match v.pat_desc with
|
|
|
|
| Tpat_or (v,w,_) ->
|
|
|
|
fprintf ppf "%a|@,%a" pretty_or v pretty_or w
|
|
|
|
| _ -> pretty_val ppf v
|
|
|
|
|
|
|
|
and pretty_vals sep ppf = function
|
|
|
|
| [] -> ()
|
|
|
|
| [v] -> pretty_val ppf v
|
|
|
|
| v::vs ->
|
|
|
|
fprintf ppf "%a%s@ %a" pretty_val v sep (pretty_vals sep) vs
|
|
|
|
|
|
|
|
and pretty_lvals lbls ppf = function
|
|
|
|
| [] -> ()
|
2012-10-24 05:03:00 -07:00
|
|
|
| [_,lbl,v] ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let name = find_label lbl lbls in
|
2012-05-30 07:52:37 -07:00
|
|
|
fprintf ppf "%s=%a" (Ident.name name) pretty_val v
|
2012-10-24 05:03:00 -07:00
|
|
|
| (_, lbl,v)::rest ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let name = find_label lbl lbls in
|
2012-05-31 01:07:31 -07:00
|
|
|
fprintf ppf "%s=%a;@ %a"
|
|
|
|
(Ident.name name) pretty_val v (pretty_lvals lbls) rest
|
2002-05-24 07:34:37 -07:00
|
|
|
|
|
|
|
let top_pretty ppf v =
|
|
|
|
fprintf ppf "@[%a@]@?" pretty_val v
|
|
|
|
|
|
|
|
|
|
|
|
let prerr_pat v =
|
|
|
|
top_pretty str_formatter v ;
|
|
|
|
prerr_string (flush_str_formatter ())
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(****************************)
|
|
|
|
(* Utilities for matching *)
|
|
|
|
(****************************)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(* Check top matching *)
|
2010-01-22 04:48:24 -08:00
|
|
|
let simple_match p1 p2 =
|
1995-05-04 03:15:53 -07:00
|
|
|
match p1.pat_desc, p2.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct(_, c1, _, _), Tpat_construct(_, c2, _, _) ->
|
1995-05-04 03:15:53 -07:00
|
|
|
c1.cstr_tag = c2.cstr_tag
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tpat_variant(l1, _, _), Tpat_variant(l2, _, _) ->
|
|
|
|
l1 = l2
|
2012-09-18 23:57:17 -07:00
|
|
|
| Tpat_constant(c1), Tpat_constant(c2) -> const_compare c1 c2 = 0
|
1999-10-29 07:42:37 -07:00
|
|
|
| Tpat_tuple _, Tpat_tuple _ -> true
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy _, Tpat_lazy _ -> true
|
1999-10-29 07:42:37 -07:00
|
|
|
| Tpat_record _ , Tpat_record _ -> true
|
|
|
|
| Tpat_array p1s, Tpat_array p2s -> List.length p1s = List.length p2s
|
1995-05-04 03:15:53 -07:00
|
|
|
| _, (Tpat_any | Tpat_var(_)) -> true
|
|
|
|
| _, _ -> false
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(* extract record fields as a whole *)
|
|
|
|
let record_arg p = match p.pat_desc with
|
|
|
|
| Tpat_any -> []
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (args,_) -> args
|
1999-10-29 07:42:37 -07:00
|
|
|
| _ -> fatal_error "Parmatch.as_record"
|
|
|
|
|
|
|
|
|
|
|
|
(* Raise Not_found when pos is not present in arg *)
|
|
|
|
let get_field pos arg =
|
2012-10-24 05:03:00 -07:00
|
|
|
let _,_, p = List.find (fun (_,lbl,_) -> pos = lbl.lbl_pos) arg in
|
1999-10-29 07:42:37 -07:00
|
|
|
p
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
let extract_fields omegas arg =
|
|
|
|
List.map
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun (_,lbl,_) ->
|
1999-10-29 07:42:37 -07:00
|
|
|
try
|
|
|
|
get_field lbl.lbl_pos arg
|
|
|
|
with Not_found -> omega)
|
|
|
|
omegas
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
let all_record_args lbls = match lbls with
|
2012-10-24 05:03:00 -07:00
|
|
|
| (_,{lbl_all=lbl_all},_)::_ ->
|
2001-02-19 12:27:52 -08:00
|
|
|
let t =
|
|
|
|
Array.map
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun lbl -> mknoloc (Longident.Lident "?temp?"), lbl,omega)
|
2012-05-31 01:07:31 -07:00
|
|
|
lbl_all in
|
2001-02-19 12:27:52 -08:00
|
|
|
List.iter
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun ((_, lbl,_) as x) -> t.(lbl.lbl_pos) <- x)
|
2001-02-19 12:27:52 -08:00
|
|
|
lbls ;
|
|
|
|
Array.to_list t
|
|
|
|
| _ -> fatal_error "Parmatch.all_record_args"
|
|
|
|
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(* Build argument list when p2 >= p1, where p1 is a simple pattern *)
|
2002-09-23 01:36:46 -07:00
|
|
|
let rec simple_match_args p1 p2 = match p2.pat_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p2,_,_) -> simple_match_args p1 p2
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct(_, cstr, args, _) -> args
|
2002-09-23 01:36:46 -07:00
|
|
|
| Tpat_variant(lab, Some arg, _) -> [arg]
|
|
|
|
| Tpat_tuple(args) -> args
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record(args,_) -> extract_fields (record_arg p1) args
|
2002-09-23 01:36:46 -07:00
|
|
|
| Tpat_array(args) -> args
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy arg -> [arg]
|
2002-09-23 01:36:46 -07:00
|
|
|
| (Tpat_any | Tpat_var(_)) ->
|
|
|
|
begin match p1.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
Tpat_construct(_, _,args, _) -> omega_list args
|
2002-09-23 01:36:46 -07:00
|
|
|
| Tpat_variant(_, Some _, _) -> [omega]
|
|
|
|
| Tpat_tuple(args) -> omega_list args
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record(args,_) -> omega_list args
|
2002-09-23 01:36:46 -07:00
|
|
|
| Tpat_array(args) -> omega_list args
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy _ -> [omega]
|
2002-09-23 01:36:46 -07:00
|
|
|
| _ -> []
|
|
|
|
end
|
|
|
|
| _ -> []
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(*
|
1999-10-29 07:42:37 -07:00
|
|
|
Normalize a pattern ->
|
|
|
|
all arguments are omega (simple pattern) and no more variables
|
1995-05-04 03:15:53 -07:00
|
|
|
*)
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
let rec normalize_pat q = match q.pat_desc with
|
2001-02-19 12:27:52 -08:00
|
|
|
| Tpat_any | Tpat_constant _ -> q
|
1999-10-29 07:42:37 -07:00
|
|
|
| Tpat_var _ -> make_pat Tpat_any q.pat_type q.pat_env
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) -> normalize_pat p
|
1999-10-29 07:42:37 -07:00
|
|
|
| Tpat_tuple (args) ->
|
|
|
|
make_pat (Tpat_tuple (omega_list args)) q.pat_type q.pat_env
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (lid, c,args,explicit_arity) ->
|
2012-05-31 01:07:31 -07:00
|
|
|
make_pat
|
2012-10-24 05:03:00 -07:00
|
|
|
(Tpat_construct (lid, c,omega_list args, explicit_arity))
|
2012-05-31 01:07:31 -07:00
|
|
|
q.pat_type q.pat_env
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tpat_variant (l, arg, row) ->
|
|
|
|
make_pat (Tpat_variant (l, may_map (fun _ -> omega) arg, row))
|
|
|
|
q.pat_type q.pat_env
|
1999-10-29 07:42:37 -07:00
|
|
|
| Tpat_array (args) ->
|
|
|
|
make_pat (Tpat_array (omega_list args)) q.pat_type q.pat_env
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (largs, closed) ->
|
2012-05-31 01:07:31 -07:00
|
|
|
make_pat
|
2012-10-24 05:03:00 -07:00
|
|
|
(Tpat_record (List.map (fun (lid,lbl,_) ->
|
|
|
|
lid, lbl,omega) largs, closed))
|
1999-10-29 07:42:37 -07:00
|
|
|
q.pat_type q.pat_env
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy _ ->
|
|
|
|
make_pat (Tpat_lazy omega) q.pat_type q.pat_env
|
2001-09-25 02:54:18 -07:00
|
|
|
| Tpat_or _ -> fatal_error "Parmatch.normalize_pat"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(*
|
|
|
|
Build normalized (cf. supra) discriminating pattern,
|
|
|
|
in the non-data type case
|
|
|
|
*)
|
|
|
|
|
|
|
|
let discr_pat q pss =
|
|
|
|
|
|
|
|
let rec acc_pat acc pss = match pss with
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias (p,_,_)}::ps)::pss ->
|
1999-10-29 07:42:37 -07:00
|
|
|
acc_pat acc ((p::ps)::pss)
|
2001-09-25 02:54:18 -07:00
|
|
|
| ({pat_desc = Tpat_or (p1,p2,_)}::ps)::pss ->
|
1999-10-29 07:42:37 -07:00
|
|
|
acc_pat acc ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| ({pat_desc = (Tpat_any | Tpat_var _)}::_)::pss ->
|
|
|
|
acc_pat acc pss
|
|
|
|
| (({pat_desc = Tpat_tuple _} as p)::_)::_ -> normalize_pat p
|
2008-07-09 06:03:38 -07:00
|
|
|
| (({pat_desc = Tpat_lazy _} as p)::_)::_ -> normalize_pat p
|
2012-05-30 07:52:37 -07:00
|
|
|
| (({pat_desc = Tpat_record (largs,closed)} as p)::_)::pss ->
|
1999-10-29 07:42:37 -07:00
|
|
|
let new_omegas =
|
2011-11-24 01:02:48 -08:00
|
|
|
List.fold_right
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun (lid, lbl,_) r ->
|
1999-10-29 07:42:37 -07:00
|
|
|
try
|
|
|
|
let _ = get_field lbl.lbl_pos r in
|
|
|
|
r
|
|
|
|
with Not_found ->
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid, lbl,omega)::r)
|
2011-11-24 01:02:48 -08:00
|
|
|
largs (record_arg acc)
|
|
|
|
in
|
1999-10-29 07:42:37 -07:00
|
|
|
acc_pat
|
2012-05-30 07:52:37 -07:00
|
|
|
(make_pat (Tpat_record (new_omegas, closed)) p.pat_type p.pat_env)
|
1999-10-29 07:42:37 -07:00
|
|
|
pss
|
|
|
|
| _ -> acc in
|
|
|
|
|
|
|
|
match normalize_pat q with
|
2011-07-29 03:32:43 -07:00
|
|
|
| {pat_desc= (Tpat_any | Tpat_record _)} as q -> acc_pat q pss
|
1999-10-29 07:42:37 -07:00
|
|
|
| q -> q
|
|
|
|
|
|
|
|
(*
|
|
|
|
In case a matching value is found, set actual arguments
|
|
|
|
of the matching pattern.
|
|
|
|
*)
|
|
|
|
|
|
|
|
let rec read_args xs r = match xs,r with
|
|
|
|
| [],_ -> [],r
|
|
|
|
| _::xs, arg::rest ->
|
|
|
|
let args,rest = read_args xs rest in
|
|
|
|
arg::args,rest
|
2002-05-24 07:34:37 -07:00
|
|
|
| _,_ ->
|
|
|
|
fatal_error "Parmatch.read_args"
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2005-03-11 02:12:05 -08:00
|
|
|
let do_set_args erase_mutable q r = match q with
|
1999-10-29 07:42:37 -07:00
|
|
|
| {pat_desc = Tpat_tuple omegas} ->
|
|
|
|
let args,rest = read_args omegas r in
|
|
|
|
make_pat (Tpat_tuple args) q.pat_type q.pat_env::rest
|
2012-05-30 07:52:37 -07:00
|
|
|
| {pat_desc = Tpat_record (omegas,closed)} ->
|
1999-10-29 07:42:37 -07:00
|
|
|
let args,rest = read_args omegas r in
|
|
|
|
make_pat
|
|
|
|
(Tpat_record
|
2012-10-24 05:03:00 -07:00
|
|
|
(List.map2 (fun (lid, lbl,_) arg ->
|
2005-03-11 02:12:05 -08:00
|
|
|
if
|
|
|
|
erase_mutable &&
|
|
|
|
(match lbl.lbl_mut with
|
|
|
|
| Mutable -> true | Immutable -> false)
|
|
|
|
then
|
2012-10-24 05:03:00 -07:00
|
|
|
lid, lbl, omega
|
2005-03-11 02:12:05 -08:00
|
|
|
else
|
2012-10-24 05:03:00 -07:00
|
|
|
lid, lbl, arg)
|
2012-05-30 07:52:37 -07:00
|
|
|
omegas args, closed))
|
1999-10-29 07:42:37 -07:00
|
|
|
q.pat_type q.pat_env::
|
|
|
|
rest
|
2012-10-24 05:03:00 -07:00
|
|
|
| {pat_desc = Tpat_construct (lid, c,omegas, explicit_arity)} ->
|
1999-10-29 07:42:37 -07:00
|
|
|
let args,rest = read_args omegas r in
|
|
|
|
make_pat
|
2012-10-24 05:03:00 -07:00
|
|
|
(Tpat_construct (lid, c,args, explicit_arity))
|
2012-05-31 01:07:31 -07:00
|
|
|
q.pat_type q.pat_env::
|
1999-10-29 07:42:37 -07:00
|
|
|
rest
|
1999-11-30 08:07:38 -08:00
|
|
|
| {pat_desc = Tpat_variant (l, omega, row)} ->
|
|
|
|
let arg, rest =
|
|
|
|
match omega, r with
|
|
|
|
Some _, a::r -> Some a, r
|
|
|
|
| None, r -> None, r
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
make_pat
|
|
|
|
(Tpat_variant (l, arg, row)) q.pat_type q.pat_env::
|
|
|
|
rest
|
2008-07-09 06:03:38 -07:00
|
|
|
| {pat_desc = Tpat_lazy omega} ->
|
|
|
|
begin match r with
|
|
|
|
arg::rest ->
|
|
|
|
make_pat (Tpat_lazy arg) q.pat_type q.pat_env::rest
|
|
|
|
| _ -> fatal_error "Parmatch.do_set_args (lazy)"
|
|
|
|
end
|
1999-10-29 07:42:37 -07:00
|
|
|
| {pat_desc = Tpat_array omegas} ->
|
|
|
|
let args,rest = read_args omegas r in
|
|
|
|
make_pat
|
|
|
|
(Tpat_array args) q.pat_type q.pat_env::
|
|
|
|
rest
|
2001-02-19 12:27:52 -08:00
|
|
|
| {pat_desc=Tpat_constant _|Tpat_any} ->
|
|
|
|
q::r (* case any is used in matching.ml *)
|
1999-10-29 07:42:37 -07:00
|
|
|
| _ -> fatal_error "Parmatch.set_args"
|
|
|
|
|
2005-03-11 02:12:05 -08:00
|
|
|
let set_args q r = do_set_args false q r
|
|
|
|
and set_args_erase_mutable q r = do_set_args true q r
|
1999-10-29 07:42:37 -07:00
|
|
|
|
|
|
|
(* filter pss acording to pattern q *)
|
1995-05-04 03:15:53 -07:00
|
|
|
let filter_one q pss =
|
|
|
|
let rec filter_rec = function
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias(p,_,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec ((p::ps)::pss)
|
2001-09-25 02:54:18 -07:00
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| (p::ps)::pss ->
|
|
|
|
if simple_match q p
|
|
|
|
then (simple_match_args q p @ ps) :: filter_rec pss
|
|
|
|
else filter_rec pss
|
|
|
|
| _ -> [] in
|
|
|
|
filter_rec pss
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(*
|
|
|
|
Filter pss in the ``extra case''. This applies :
|
|
|
|
- According to an extra constructor (datatype case, non-complete signature).
|
|
|
|
- Acordinng to anything (all-variables case).
|
|
|
|
*)
|
1995-05-04 03:15:53 -07:00
|
|
|
let filter_extra pss =
|
|
|
|
let rec filter_rec = function
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias(p,_,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec ((p::ps)::pss)
|
2001-09-25 02:54:18 -07:00
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| ({pat_desc = (Tpat_any | Tpat_var(_))} :: qs) :: pss ->
|
|
|
|
qs :: filter_rec pss
|
|
|
|
| _::pss -> filter_rec pss
|
|
|
|
| [] -> [] in
|
|
|
|
filter_rec pss
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
(*
|
1999-10-29 07:42:37 -07:00
|
|
|
Pattern p0 is the discriminating pattern,
|
2002-05-24 07:34:37 -07:00
|
|
|
returns [(q0,pss0) ; ... ; (qn,pssn)]
|
1999-10-29 07:42:37 -07:00
|
|
|
where the qi's are simple patterns and the pssi's are
|
|
|
|
matched matrices.
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
NOTES
|
|
|
|
* (qi,[]) is impossible.
|
|
|
|
* In the case when matching is useless (all-variable case),
|
|
|
|
returns []
|
1999-10-29 07:42:37 -07:00
|
|
|
*)
|
2002-05-24 07:34:37 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let filter_all pat0 pss =
|
|
|
|
|
|
|
|
let rec insert q qs env =
|
|
|
|
match env with
|
1999-10-29 07:42:37 -07:00
|
|
|
[] ->
|
|
|
|
let q0 = normalize_pat q in
|
|
|
|
[q0, [simple_match_args q0 q @ qs]]
|
|
|
|
| ((q0,pss) as c)::env ->
|
|
|
|
if simple_match q0 q
|
|
|
|
then (q0, ((simple_match_args q0 q @ qs) :: pss)) :: env
|
1995-05-04 03:15:53 -07:00
|
|
|
else c :: insert q qs env in
|
|
|
|
|
|
|
|
let rec filter_rec env = function
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias(p,_,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec env ((p::ps)::pss)
|
2001-09-25 02:54:18 -07:00
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_rec env ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| ({pat_desc = (Tpat_any | Tpat_var(_))}::_)::pss ->
|
|
|
|
filter_rec env pss
|
|
|
|
| (p::ps)::pss ->
|
|
|
|
filter_rec (insert p ps env) pss
|
|
|
|
| _ -> env
|
|
|
|
|
|
|
|
and filter_omega env = function
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias(p,_,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_omega env ((p::ps)::pss)
|
2001-09-25 02:54:18 -07:00
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps)::pss ->
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_omega env ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| ({pat_desc = (Tpat_any | Tpat_var(_))}::ps)::pss ->
|
|
|
|
filter_omega
|
2012-05-31 01:07:31 -07:00
|
|
|
(List.map (fun (q,qss) -> (q,(simple_match_args q omega @ ps) :: qss))
|
|
|
|
env)
|
1995-05-04 03:15:53 -07:00
|
|
|
pss
|
|
|
|
| _::pss -> filter_omega env pss
|
|
|
|
| [] -> env in
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
filter_omega
|
|
|
|
(filter_rec
|
|
|
|
(match pat0.pat_desc with
|
2008-07-09 06:03:38 -07:00
|
|
|
(Tpat_record(_) | Tpat_tuple(_) | Tpat_lazy(_)) -> [pat0,[]]
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ -> [])
|
|
|
|
pss)
|
|
|
|
pss
|
|
|
|
|
2003-08-14 18:35:24 -07:00
|
|
|
(* Variant related functions *)
|
|
|
|
|
|
|
|
let rec set_last a = function
|
|
|
|
[] -> []
|
|
|
|
| [_] -> [a]
|
|
|
|
| x::l -> x :: set_last a l
|
|
|
|
|
|
|
|
(* mark constructor lines for failure when they are incomplete *)
|
|
|
|
let rec mark_partial = function
|
2012-05-30 07:52:37 -07:00
|
|
|
({pat_desc = Tpat_alias(p,_,_)}::ps)::pss ->
|
2003-08-14 18:35:24 -07:00
|
|
|
mark_partial ((p::ps)::pss)
|
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps)::pss ->
|
|
|
|
mark_partial ((p1::ps)::(p2::ps)::pss)
|
|
|
|
| ({pat_desc = (Tpat_any | Tpat_var(_))} :: _ as ps) :: pss ->
|
|
|
|
ps :: mark_partial pss
|
|
|
|
| ps::pss ->
|
|
|
|
(set_last zero ps) :: mark_partial pss
|
|
|
|
| [] -> []
|
|
|
|
|
2003-08-14 02:04:43 -07:00
|
|
|
let close_variant env row =
|
|
|
|
let row = Btype.row_repr row in
|
|
|
|
let nm =
|
|
|
|
List.fold_left
|
|
|
|
(fun nm (tag,f) ->
|
|
|
|
match Btype.row_field_repr f with
|
|
|
|
| Reither(_, _, false, e) ->
|
|
|
|
(* m=false means that this tag is not explicitly matched *)
|
|
|
|
Btype.set_row_field e Rabsent;
|
|
|
|
None
|
|
|
|
| Rabsent | Reither (_, _, true, _) | Rpresent _ -> nm)
|
|
|
|
row.row_name row.row_fields in
|
|
|
|
if not row.row_closed || nm != row.row_name then begin
|
|
|
|
(* this unification cannot fail *)
|
|
|
|
Ctype.unify env row.row_more
|
|
|
|
(Btype.newgenty
|
|
|
|
(Tvariant {row with row_fields = []; row_more = Btype.newgenvar();
|
|
|
|
row_closed = true; row_name = nm}))
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
let row_of_pat pat =
|
|
|
|
match Ctype.expand_head pat.pat_env pat.pat_type with
|
|
|
|
{desc = Tvariant row} -> Btype.row_repr row
|
|
|
|
| _ -> assert false
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(*
|
1999-10-29 07:42:37 -07:00
|
|
|
Check whether the first column of env makes up a complete signature or
|
|
|
|
not.
|
2008-01-11 08:13:18 -08:00
|
|
|
*)
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let generalized_constructor x =
|
|
|
|
match x with
|
2012-10-24 05:03:00 -07:00
|
|
|
({pat_desc = Tpat_construct(_,c,_, _);pat_env=env},_) ->
|
2010-10-07 01:31:59 -07:00
|
|
|
c.cstr_generalized
|
2010-10-07 00:12:50 -07:00
|
|
|
| _ -> assert false
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let clean_env env =
|
|
|
|
let rec loop =
|
2010-10-07 00:12:50 -07:00
|
|
|
function
|
|
|
|
| [] -> []
|
|
|
|
| x :: xs ->
|
2012-07-30 11:04:46 -07:00
|
|
|
if generalized_constructor x then loop xs else x :: loop xs
|
2010-10-07 00:12:50 -07:00
|
|
|
in
|
|
|
|
loop env
|
2010-09-18 21:55:40 -07:00
|
|
|
|
2010-10-18 22:57:45 -07:00
|
|
|
let full_match ignore_generalized closing env = match env with
|
2012-10-24 05:03:00 -07:00
|
|
|
| ({pat_desc = Tpat_construct (_,{cstr_tag=Cstr_exception _},_,_)},_)::_ ->
|
1999-10-29 07:42:37 -07:00
|
|
|
false
|
2012-10-24 05:03:00 -07:00
|
|
|
| ({pat_desc = Tpat_construct(_,c,_,_);pat_type=typ},_) :: _ ->
|
2010-10-18 22:57:45 -07:00
|
|
|
if ignore_generalized then
|
2012-05-31 01:07:31 -07:00
|
|
|
(* remove generalized constructors;
|
|
|
|
those cases will be handled separately *)
|
2012-05-30 07:52:37 -07:00
|
|
|
let env = clean_env env in
|
2010-10-18 22:57:45 -07:00
|
|
|
List.length env = c.cstr_normal
|
|
|
|
else
|
|
|
|
List.length env = c.cstr_consts + c.cstr_nonconsts
|
2010-10-19 22:05:05 -07:00
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
| ({pat_desc = Tpat_variant _} as p,_) :: _ ->
|
1999-11-30 08:07:38 -08:00
|
|
|
let fields =
|
|
|
|
List.map
|
2001-04-09 22:03:44 -07:00
|
|
|
(function ({pat_desc = Tpat_variant (tag, _, _)}, _) -> tag
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> assert false)
|
|
|
|
env
|
|
|
|
in
|
2008-01-11 08:13:18 -08:00
|
|
|
let row = row_of_pat p in
|
2012-07-17 20:21:12 -07:00
|
|
|
if closing && not (Btype.row_fixed row) then
|
2003-08-18 01:26:18 -07:00
|
|
|
(* closing=true, we are considering the variant as closed *)
|
|
|
|
List.for_all
|
|
|
|
(fun (tag,f) ->
|
|
|
|
match Btype.row_field_repr f with
|
|
|
|
Rabsent | Reither(_, _, false, _) -> true
|
|
|
|
| Reither (_, _, true, _)
|
|
|
|
(* m=true, do not discard matched tags, rather warn *)
|
|
|
|
| Rpresent _ -> List.mem tag fields)
|
|
|
|
row.row_fields
|
|
|
|
else
|
2000-08-11 12:58:52 -07:00
|
|
|
row.row_closed &&
|
2005-03-24 09:20:54 -08:00
|
|
|
List.for_all
|
|
|
|
(fun (tag,f) ->
|
|
|
|
Btype.row_field_repr f = Rabsent || List.mem tag fields)
|
|
|
|
row.row_fields
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc = Tpat_constant(Const_char _)},_) :: _ ->
|
|
|
|
List.length env = 256
|
|
|
|
| ({pat_desc = Tpat_constant(_)},_) :: _ -> false
|
|
|
|
| ({pat_desc = Tpat_tuple(_)},_) :: _ -> true
|
|
|
|
| ({pat_desc = Tpat_record(_)},_) :: _ -> true
|
|
|
|
| ({pat_desc = Tpat_array(_)},_) :: _ -> false
|
2008-07-09 06:03:38 -07:00
|
|
|
| ({pat_desc = Tpat_lazy(_)},_) :: _ -> true
|
1999-10-29 07:42:37 -07:00
|
|
|
| _ -> fatal_error "Parmatch.full_match"
|
|
|
|
|
2010-11-07 23:34:09 -08:00
|
|
|
let full_match_gadt env = match env with
|
2012-10-24 05:03:00 -07:00
|
|
|
| ({pat_desc = Tpat_construct(_,c,_,_);pat_type=typ},_) :: _ ->
|
2011-01-06 23:17:38 -08:00
|
|
|
List.length env = c.cstr_consts + c.cstr_nonconsts
|
|
|
|
| _ -> true
|
2010-11-07 23:34:09 -08:00
|
|
|
|
2003-05-02 02:57:53 -07:00
|
|
|
let extendable_match env = match env with
|
2012-10-24 05:03:00 -07:00
|
|
|
| ({pat_desc=Tpat_construct(_,{cstr_tag=(Cstr_constant _|Cstr_block _)},_,_)}
|
2012-05-31 01:07:31 -07:00
|
|
|
as p,_) :: _ ->
|
2003-05-02 02:57:53 -07:00
|
|
|
let path = get_type_path p.pat_type p.pat_env in
|
2003-05-02 07:57:24 -07:00
|
|
|
not
|
|
|
|
(Path.same path Predef.path_bool ||
|
|
|
|
Path.same path Predef.path_list ||
|
|
|
|
Path.same path Predef.path_option)
|
2003-05-02 02:57:53 -07:00
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let should_extend ext env = match ext with
|
|
|
|
| None -> false
|
|
|
|
| Some ext -> match env with
|
2012-07-30 11:04:46 -07:00
|
|
|
| ({pat_desc =
|
2012-10-24 05:03:00 -07:00
|
|
|
Tpat_construct(_, {cstr_tag=(Cstr_constant _|Cstr_block _)},_,_)}
|
2012-05-31 01:07:31 -07:00
|
|
|
as p, _) :: _ ->
|
2006-09-21 07:54:54 -07:00
|
|
|
let path = get_type_path p.pat_type p.pat_env in
|
|
|
|
Path.same path ext
|
|
|
|
| _ -> false
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
(* complement constructor tags *)
|
1999-10-29 07:42:37 -07:00
|
|
|
let complete_tags nconsts nconstrs tags =
|
|
|
|
let seen_const = Array.create nconsts false
|
|
|
|
and seen_constr = Array.create nconstrs false in
|
|
|
|
List.iter
|
|
|
|
(function
|
|
|
|
| Cstr_constant i -> seen_const.(i) <- true
|
|
|
|
| Cstr_block i -> seen_constr.(i) <- true
|
|
|
|
| _ -> assert false)
|
|
|
|
tags ;
|
|
|
|
let r = ref [] in
|
|
|
|
for i = 0 to nconsts-1 do
|
|
|
|
if not seen_const.(i) then
|
|
|
|
r := Cstr_constant i :: !r
|
|
|
|
done ;
|
|
|
|
for i = 0 to nconstrs-1 do
|
|
|
|
if not seen_constr.(i) then
|
|
|
|
r := Cstr_block i :: !r
|
2010-01-22 04:48:24 -08:00
|
|
|
done ;
|
1999-10-29 07:42:37 -07:00
|
|
|
!r
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
(* build a pattern from a constructor list *)
|
|
|
|
let pat_of_constr ex_pat cstr =
|
2012-05-31 01:07:31 -07:00
|
|
|
{ex_pat with pat_desc =
|
2012-10-24 05:03:00 -07:00
|
|
|
Tpat_construct (mknoloc (Longident.Lident "?pat_of_constr?"),
|
2012-05-31 01:07:31 -07:00
|
|
|
cstr,omegas cstr.cstr_arity,false)}
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
let rec pat_of_constrs ex_pat = function
|
|
|
|
| [] -> raise Empty
|
|
|
|
| [cstr] -> pat_of_constr ex_pat cstr
|
2010-01-22 04:48:24 -08:00
|
|
|
| cstr::rem ->
|
2001-02-19 12:27:52 -08:00
|
|
|
{ex_pat with
|
|
|
|
pat_desc=
|
|
|
|
Tpat_or
|
|
|
|
(pat_of_constr ex_pat cstr,
|
2001-09-25 02:54:18 -07:00
|
|
|
pat_of_constrs ex_pat rem, None)}
|
2001-02-19 12:27:52 -08:00
|
|
|
|
2010-11-15 22:01:59 -08:00
|
|
|
exception Not_an_adt
|
|
|
|
|
2010-11-17 22:56:02 -08:00
|
|
|
let rec adt_path env ty =
|
2010-11-15 22:01:59 -08:00
|
|
|
match get_type_descr ty env with
|
|
|
|
| {type_kind=Type_variant constr_list} ->
|
|
|
|
begin match (Ctype.repr ty).desc with
|
|
|
|
| Tconstr (path,_,_) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
path
|
2010-11-15 22:01:59 -08:00
|
|
|
| _ -> assert false end
|
|
|
|
| {type_manifest = Some _} ->
|
2010-11-17 22:56:02 -08:00
|
|
|
adt_path env (Ctype.expand_head_once env (clean_copy ty))
|
2010-11-15 22:01:59 -08:00
|
|
|
| _ -> raise Not_an_adt
|
|
|
|
;;
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let rec map_filter f =
|
2010-11-15 22:01:59 -08:00
|
|
|
function
|
|
|
|
[] -> []
|
|
|
|
| x :: xs ->
|
2012-07-30 11:04:46 -07:00
|
|
|
match f x with
|
|
|
|
| None -> map_filter f xs
|
|
|
|
| Some y -> y :: map_filter f xs
|
2010-11-15 22:01:59 -08:00
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
(* Sends back a pattern that complements constructor tags all_tag *)
|
2012-05-30 07:52:37 -07:00
|
|
|
let complete_constrs p all_tags =
|
2010-11-17 22:23:00 -08:00
|
|
|
match p.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_,c,_,_) ->
|
2010-11-17 22:23:00 -08:00
|
|
|
begin try
|
2012-07-30 11:04:46 -07:00
|
|
|
let not_tags = complete_tags c.cstr_consts c.cstr_nonconsts all_tags in
|
2012-09-20 23:37:17 -07:00
|
|
|
let (constrs, _) =
|
|
|
|
Env.find_type_descrs (adt_path p.pat_env p.pat_type) p.pat_env in
|
2012-07-30 11:04:46 -07:00
|
|
|
map_filter
|
2010-11-17 22:23:00 -08:00
|
|
|
(fun cnstr ->
|
2012-07-30 11:04:46 -07:00
|
|
|
if List.mem cnstr.cstr_tag not_tags then Some cnstr else None)
|
|
|
|
constrs
|
2010-11-17 22:23:00 -08:00
|
|
|
with
|
|
|
|
| Datarepr.Constr_not_found ->
|
2012-07-30 11:04:46 -07:00
|
|
|
fatal_error "Parmatch.complete_constr: constr_not_found"
|
2010-11-17 22:23:00 -08:00
|
|
|
end
|
|
|
|
| _ -> fatal_error "Parmatch.complete_constr"
|
2001-02-19 12:27:52 -08:00
|
|
|
|
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
(* Auxiliary for build_other *)
|
|
|
|
|
|
|
|
let build_other_constant proj make first next p env =
|
|
|
|
let all = List.map (fun (p, _) -> proj p.pat_desc) env in
|
|
|
|
let rec try_const i =
|
|
|
|
if List.mem i all
|
|
|
|
then try_const (next i)
|
|
|
|
else make_pat (make i) p.pat_type p.pat_env
|
|
|
|
in try_const first
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(*
|
|
|
|
Builds a pattern that is incompatible with all patterns in
|
|
|
|
in the first column of env
|
|
|
|
*)
|
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let build_other ext env = match env with
|
2012-05-31 01:07:31 -07:00
|
|
|
| ({pat_desc =
|
2012-10-24 05:03:00 -07:00
|
|
|
Tpat_construct (lid, ({cstr_tag=Cstr_exception _} as c),_,_)},_)
|
1999-10-29 07:42:37 -07:00
|
|
|
::_ ->
|
|
|
|
make_pat
|
|
|
|
(Tpat_construct
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid, {c with
|
1999-10-29 07:42:37 -07:00
|
|
|
cstr_tag=(Cstr_exception
|
2012-03-06 11:47:07 -08:00
|
|
|
(Path.Pident (Ident.create "*exception*"), Location.none))},
|
2012-05-30 07:52:37 -07:00
|
|
|
[], false))
|
1999-10-29 07:42:37 -07:00
|
|
|
Ctype.none Env.empty
|
2012-10-24 05:03:00 -07:00
|
|
|
| ({pat_desc = Tpat_construct (_, _,_,_)} as p,_) :: _ ->
|
2006-09-21 07:54:54 -07:00
|
|
|
begin match ext with
|
|
|
|
| Some ext when Path.same ext (get_type_path p.pat_type p.pat_env) ->
|
|
|
|
extra_pat
|
|
|
|
| _ ->
|
|
|
|
let get_tag = function
|
2012-10-24 05:03:00 -07:00
|
|
|
| {pat_desc = Tpat_construct (_,c,_,_)} -> c.cstr_tag
|
2006-09-21 07:54:54 -07:00
|
|
|
| _ -> fatal_error "Parmatch.get_tag" in
|
|
|
|
let all_tags = List.map (fun (p,_) -> get_tag p) env in
|
|
|
|
pat_of_constrs p (complete_constrs p all_tags)
|
|
|
|
end
|
2008-01-11 08:13:18 -08:00
|
|
|
| ({pat_desc = Tpat_variant (_,_,r)} as p,_) :: _ ->
|
1999-11-30 08:07:38 -08:00
|
|
|
let tags =
|
|
|
|
List.map
|
|
|
|
(function ({pat_desc = Tpat_variant (tag, _, _)}, _) -> tag
|
|
|
|
| _ -> assert false)
|
|
|
|
env
|
|
|
|
in
|
2008-01-11 08:13:18 -08:00
|
|
|
let row = row_of_pat p in
|
1999-11-30 08:07:38 -08:00
|
|
|
let make_other_pat tag const =
|
|
|
|
let arg = if const then None else Some omega in
|
2008-01-11 08:13:18 -08:00
|
|
|
make_pat (Tpat_variant(tag, arg, r)) p.pat_type p.pat_env in
|
1999-11-30 08:07:38 -08:00
|
|
|
begin match
|
|
|
|
List.fold_left
|
2000-09-19 19:40:50 -07:00
|
|
|
(fun others (tag,f) ->
|
|
|
|
if List.mem tag tags then others else
|
|
|
|
match Btype.row_field_repr f with
|
2001-03-02 05:15:58 -08:00
|
|
|
Rabsent (* | Reither _ *) -> others
|
2001-03-02 16:14:35 -08:00
|
|
|
(* This one is called after erasing pattern info *)
|
|
|
|
| Reither (c, _, _, _) -> make_other_pat tag c :: others
|
2000-09-19 19:40:50 -07:00
|
|
|
| Rpresent arg -> make_other_pat tag (arg = None) :: others)
|
1999-11-30 08:07:38 -08:00
|
|
|
[] row.row_fields
|
2003-08-14 02:04:43 -07:00
|
|
|
with
|
|
|
|
[] ->
|
|
|
|
make_other_pat "AnyExtraTag" true
|
1999-11-30 08:07:38 -08:00
|
|
|
| pat::other_pats ->
|
|
|
|
List.fold_left
|
|
|
|
(fun p_res pat ->
|
2001-09-25 02:54:18 -07:00
|
|
|
make_pat (Tpat_or (pat, p_res, None)) p.pat_type p.pat_env)
|
1999-11-30 08:07:38 -08:00
|
|
|
pat other_pats
|
|
|
|
end
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc = Tpat_constant(Const_char _)} as p,_) :: _ ->
|
|
|
|
let all_chars =
|
|
|
|
List.map
|
|
|
|
(fun (p,_) -> match p.pat_desc with
|
|
|
|
| Tpat_constant (Const_char c) -> c
|
|
|
|
| _ -> assert false)
|
|
|
|
env in
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
let rec find_other i imax =
|
|
|
|
if i > imax then raise Not_found
|
|
|
|
else
|
|
|
|
let ci = Char.chr i in
|
|
|
|
if List.mem ci all_chars then
|
|
|
|
find_other (i+1) imax
|
|
|
|
else
|
|
|
|
make_pat (Tpat_constant (Const_char ci)) p.pat_type p.pat_env in
|
|
|
|
let rec try_chars = function
|
|
|
|
| [] -> omega
|
|
|
|
| (c1,c2) :: rest ->
|
|
|
|
try
|
|
|
|
find_other (Char.code c1) (Char.code c2)
|
|
|
|
with
|
|
|
|
| Not_found -> try_chars rest in
|
|
|
|
|
|
|
|
try_chars
|
|
|
|
[ 'a', 'z' ; 'A', 'Z' ; '0', '9' ;
|
|
|
|
' ', '~' ; Char.chr 0 , Char.chr 255]
|
2003-04-25 05:27:31 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc=(Tpat_constant (Const_int _))} as p,_) :: _ ->
|
2003-04-25 05:27:31 -07:00
|
|
|
build_other_constant
|
|
|
|
(function Tpat_constant(Const_int i) -> i | _ -> assert false)
|
|
|
|
(function i -> Tpat_constant(Const_int i))
|
|
|
|
0 succ p env
|
|
|
|
| ({pat_desc=(Tpat_constant (Const_int32 _))} as p,_) :: _ ->
|
|
|
|
build_other_constant
|
|
|
|
(function Tpat_constant(Const_int32 i) -> i | _ -> assert false)
|
|
|
|
(function i -> Tpat_constant(Const_int32 i))
|
|
|
|
0l Int32.succ p env
|
|
|
|
| ({pat_desc=(Tpat_constant (Const_int64 _))} as p,_) :: _ ->
|
|
|
|
build_other_constant
|
|
|
|
(function Tpat_constant(Const_int64 i) -> i | _ -> assert false)
|
|
|
|
(function i -> Tpat_constant(Const_int64 i))
|
|
|
|
0L Int64.succ p env
|
|
|
|
| ({pat_desc=(Tpat_constant (Const_nativeint _))} as p,_) :: _ ->
|
|
|
|
build_other_constant
|
|
|
|
(function Tpat_constant(Const_nativeint i) -> i | _ -> assert false)
|
|
|
|
(function i -> Tpat_constant(Const_nativeint i))
|
|
|
|
0n Nativeint.succ p env
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc=(Tpat_constant (Const_string _))} as p,_) :: _ ->
|
2003-04-25 05:27:31 -07:00
|
|
|
build_other_constant
|
2013-03-26 04:17:17 -07:00
|
|
|
(function Tpat_constant(Const_string (s, _)) -> String.length s
|
2003-04-25 05:27:31 -07:00
|
|
|
| _ -> assert false)
|
2013-03-26 04:17:17 -07:00
|
|
|
(function i -> Tpat_constant(Const_string(String.make i '*', None)))
|
2003-04-25 05:27:31 -07:00
|
|
|
0 succ p env
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc=(Tpat_constant (Const_float _))} as p,_) :: _ ->
|
2003-04-25 05:27:31 -07:00
|
|
|
build_other_constant
|
|
|
|
(function Tpat_constant(Const_float f) -> float_of_string f
|
|
|
|
| _ -> assert false)
|
|
|
|
(function f -> Tpat_constant(Const_float (string_of_float f)))
|
|
|
|
0.0 (fun f -> f +. 1.0) p env
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
| ({pat_desc = Tpat_array args} as p,_)::_ ->
|
|
|
|
let all_lengths =
|
|
|
|
List.map
|
|
|
|
(fun (p,_) -> match p.pat_desc with
|
|
|
|
| Tpat_array args -> List.length args
|
|
|
|
| _ -> assert false)
|
|
|
|
env in
|
|
|
|
let rec try_arrays l =
|
|
|
|
if List.mem l all_lengths then try_arrays (l+1)
|
|
|
|
else
|
|
|
|
make_pat
|
|
|
|
(Tpat_array (omegas l))
|
|
|
|
p.pat_type p.pat_env in
|
|
|
|
try_arrays 0
|
|
|
|
| [] -> omega
|
2010-01-22 04:48:24 -08:00
|
|
|
| _ -> omega
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let build_other_gadt ext env =
|
2010-11-07 23:34:09 -08:00
|
|
|
match env with
|
2012-05-30 07:52:37 -07:00
|
|
|
| ({pat_desc = Tpat_construct _} as p,_) :: _ ->
|
2010-11-07 23:34:09 -08:00
|
|
|
let get_tag = function
|
2012-10-24 05:03:00 -07:00
|
|
|
| {pat_desc = Tpat_construct (_,c,_,_)} -> c.cstr_tag
|
2010-11-07 23:34:09 -08:00
|
|
|
| _ -> fatal_error "Parmatch.get_tag" in
|
|
|
|
let all_tags = List.map (fun (p,_) -> get_tag p) env in
|
2012-07-30 11:04:46 -07:00
|
|
|
let cnstrs = complete_constrs p all_tags in
|
|
|
|
let pats = List.map (pat_of_constr p) cnstrs in
|
2011-08-09 06:59:41 -07:00
|
|
|
(* List.iter (Format.eprintf "%a@." top_pretty) pats;
|
|
|
|
Format.eprintf "@.@."; *)
|
|
|
|
pats
|
2010-11-07 23:34:09 -08:00
|
|
|
| _ -> assert false
|
2012-05-30 07:52:37 -07:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(*
|
|
|
|
Core function :
|
1995-05-04 03:15:53 -07:00
|
|
|
Is the last row of pattern matrix pss + qs satisfiable ?
|
1999-10-29 07:42:37 -07:00
|
|
|
That is :
|
|
|
|
Does there exists at least one value vector, es such that :
|
|
|
|
1- for all ps in pss ps # es (ps and es are not compatible)
|
|
|
|
2- qs <= es (es matches qs)
|
1995-05-04 03:15:53 -07:00
|
|
|
*)
|
|
|
|
|
2002-05-27 01:04:03 -07:00
|
|
|
let rec has_instance p = match p.pat_desc with
|
|
|
|
| Tpat_variant (l,_,r) when is_absent l r -> false
|
|
|
|
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_variant (_,None,_) -> true
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) | Tpat_variant (_,Some p,_) -> has_instance p
|
2002-05-27 01:04:03 -07:00
|
|
|
| Tpat_or (p1,p2,_) -> has_instance p1 || has_instance p2
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_,_,ps,_) | Tpat_tuple ps | Tpat_array ps ->
|
2012-05-31 01:07:31 -07:00
|
|
|
has_instances ps
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_record (lps,_) -> has_instances (List.map (fun (_,_,x) -> x) lps)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_lazy p
|
|
|
|
-> has_instance p
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-05-27 01:04:03 -07:00
|
|
|
and has_instances = function
|
|
|
|
| [] -> true
|
|
|
|
| q::rem -> has_instance q && has_instances rem
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
let rec satisfiable pss qs = match pss with
|
2010-01-22 04:48:24 -08:00
|
|
|
| [] -> has_instances qs
|
2002-05-24 07:34:37 -07:00
|
|
|
| _ ->
|
2003-05-02 02:57:53 -07:00
|
|
|
match qs with
|
|
|
|
| [] -> false
|
2010-01-22 04:48:24 -08:00
|
|
|
| {pat_desc = Tpat_or(q1,q2,_)}::qs ->
|
2003-05-02 02:57:53 -07:00
|
|
|
satisfiable pss (q1::qs) || satisfiable pss (q2::qs)
|
2012-05-30 07:52:37 -07:00
|
|
|
| {pat_desc = Tpat_alias(q,_,_)}::qs ->
|
2003-05-02 02:57:53 -07:00
|
|
|
satisfiable pss (q::qs)
|
|
|
|
| {pat_desc = (Tpat_any | Tpat_var(_))}::qs ->
|
|
|
|
let q0 = discr_pat omega pss in
|
|
|
|
begin match filter_all q0 pss with
|
|
|
|
(* first column of pss is made of variables only *)
|
|
|
|
| [] -> satisfiable (filter_extra pss) qs
|
|
|
|
| constrs ->
|
2010-10-18 22:57:45 -07:00
|
|
|
if full_match false false constrs then
|
2004-01-09 05:12:17 -08:00
|
|
|
List.exists
|
|
|
|
(fun (p,pss) ->
|
|
|
|
not (is_absent_pat p) &&
|
|
|
|
satisfiable pss (simple_match_args p omega @ qs))
|
|
|
|
constrs
|
|
|
|
else
|
|
|
|
satisfiable (filter_extra pss) qs
|
2003-05-02 02:57:53 -07:00
|
|
|
end
|
|
|
|
| {pat_desc=Tpat_variant (l,_,r)}::_ when is_absent l r -> false
|
|
|
|
| q::qs ->
|
|
|
|
let q0 = discr_pat q pss in
|
|
|
|
satisfiable (filter_one q0 pss) (simple_match_args q0 q @ qs)
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
(*
|
|
|
|
Now another satisfiable function that additionally
|
|
|
|
supplies an example of a matching value.
|
|
|
|
|
|
|
|
This function should be called for exhaustiveness check only.
|
|
|
|
*)
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
type 'a result =
|
2002-05-24 07:34:37 -07:00
|
|
|
| Rnone (* No matching value *)
|
|
|
|
| Rsome of 'a (* This matching value *)
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2010-11-07 23:34:09 -08:00
|
|
|
let rec orify_many =
|
2012-07-30 11:04:46 -07:00
|
|
|
let orify x y =
|
|
|
|
make_pat (Tpat_or (x, y, None)) x.pat_type x.pat_env
|
2010-11-09 01:13:15 -08:00
|
|
|
in
|
2010-11-07 23:34:09 -08:00
|
|
|
function
|
|
|
|
| [] -> assert false
|
|
|
|
| [x] -> x
|
|
|
|
| x :: xs -> orify x (orify_many xs)
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2010-11-07 23:34:09 -08:00
|
|
|
let rec try_many f = function
|
1999-11-09 02:53:31 -08:00
|
|
|
| [] -> Rnone
|
2010-11-07 23:34:09 -08:00
|
|
|
| (p,pss)::rest ->
|
|
|
|
match f (p,pss) with
|
|
|
|
| Rnone -> try_many f rest
|
2003-08-18 01:26:18 -07:00
|
|
|
| r -> r
|
2010-11-07 23:34:09 -08:00
|
|
|
|
2013-01-24 20:26:02 -08:00
|
|
|
let rappend r1 r2 =
|
|
|
|
match r1, r2 with
|
|
|
|
| Rnone, _ -> r2
|
|
|
|
| _, Rnone -> r1
|
|
|
|
| Rsome l1, Rsome l2 -> Rsome (l1 @ l2)
|
2010-11-07 23:34:09 -08:00
|
|
|
|
2013-01-24 20:26:02 -08:00
|
|
|
let rec try_many_gadt f = function
|
2010-11-07 23:34:09 -08:00
|
|
|
| [] -> Rnone
|
|
|
|
| (p,pss)::rest ->
|
2013-01-24 20:26:02 -08:00
|
|
|
rappend (f (p, pss)) (try_many_gadt f rest)
|
2002-05-24 07:34:37 -07:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let rec exhaust ext pss n = match pss with
|
2002-09-25 09:37:04 -07:00
|
|
|
| [] -> Rsome (omegas n)
|
2002-05-24 07:34:37 -07:00
|
|
|
| []::_ -> Rnone
|
2002-09-25 09:37:04 -07:00
|
|
|
| pss ->
|
2003-08-11 20:11:38 -07:00
|
|
|
let q0 = discr_pat omega pss in
|
2002-05-24 07:34:37 -07:00
|
|
|
begin match filter_all q0 pss with
|
1995-05-04 03:15:53 -07:00
|
|
|
(* first column of pss is made of variables only *)
|
2003-08-14 02:12:56 -07:00
|
|
|
| [] ->
|
2006-09-21 07:54:54 -07:00
|
|
|
begin match exhaust ext (filter_extra pss) (n-1) with
|
2002-05-24 07:34:37 -07:00
|
|
|
| Rsome r -> Rsome (q0::r)
|
|
|
|
| r -> r
|
|
|
|
end
|
2010-01-22 04:48:24 -08:00
|
|
|
| constrs ->
|
2002-05-24 07:34:37 -07:00
|
|
|
let try_non_omega (p,pss) =
|
2004-01-09 05:12:17 -08:00
|
|
|
if is_absent_pat p then
|
|
|
|
Rnone
|
|
|
|
else
|
|
|
|
match
|
2006-09-21 07:54:54 -07:00
|
|
|
exhaust
|
|
|
|
ext pss (List.length (simple_match_args p omega) + n - 1)
|
2004-01-09 05:12:17 -08:00
|
|
|
with
|
|
|
|
| Rsome r -> Rsome (set_args p r)
|
|
|
|
| r -> r in
|
2006-09-21 07:54:54 -07:00
|
|
|
if
|
2010-10-18 22:57:45 -07:00
|
|
|
full_match true false constrs && not (should_extend ext constrs)
|
2004-01-09 05:12:17 -08:00
|
|
|
then
|
|
|
|
try_many try_non_omega constrs
|
2002-05-24 07:34:37 -07:00
|
|
|
else
|
2003-08-14 02:12:56 -07:00
|
|
|
(*
|
|
|
|
D = filter_extra pss is the default matrix
|
|
|
|
as it is included in pss, one can avoid
|
|
|
|
recursive calls on specialized matrices,
|
|
|
|
Essentially :
|
|
|
|
* D exhaustive => pss exhaustive
|
|
|
|
* D non-exhaustive => we have a non-filtered value
|
|
|
|
*)
|
2006-09-21 07:54:54 -07:00
|
|
|
let r = exhaust ext (filter_extra pss) (n-1) in
|
2003-08-14 02:12:56 -07:00
|
|
|
match r with
|
|
|
|
| Rnone -> Rnone
|
2002-05-24 07:34:37 -07:00
|
|
|
| Rsome r ->
|
|
|
|
try
|
2006-09-21 07:54:54 -07:00
|
|
|
Rsome (build_other ext constrs::r)
|
1999-11-30 08:07:38 -08:00
|
|
|
with
|
2001-02-19 12:27:52 -08:00
|
|
|
(* cannot occur, since constructors don't make a full signature *)
|
2002-05-24 07:34:37 -07:00
|
|
|
| Empty -> fatal_error "Parmatch.exhaust"
|
|
|
|
end
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let combinations f lst lst' =
|
|
|
|
let rec iter2 x =
|
2010-11-07 23:34:09 -08:00
|
|
|
function
|
2012-07-30 11:04:46 -07:00
|
|
|
[] -> []
|
2010-11-07 23:34:09 -08:00
|
|
|
| y :: ys ->
|
2012-07-30 11:04:46 -07:00
|
|
|
f x y :: iter2 x ys
|
2010-11-07 23:34:09 -08:00
|
|
|
in
|
|
|
|
let rec iter =
|
|
|
|
function
|
2012-07-30 11:04:46 -07:00
|
|
|
[] -> []
|
2011-08-09 06:59:41 -07:00
|
|
|
| x :: xs -> iter2 x lst' @ iter xs
|
2010-11-07 23:34:09 -08:00
|
|
|
in
|
|
|
|
iter lst
|
2012-05-30 07:52:37 -07:00
|
|
|
|
|
|
|
(*
|
|
|
|
let print_pat pat =
|
|
|
|
let rec string_of_pat pat =
|
|
|
|
match pat.pat_desc with
|
|
|
|
Tpat_var _ -> "v"
|
|
|
|
| Tpat_any -> "_"
|
|
|
|
| Tpat_alias (p, x) -> Printf.sprintf "(%s) as ?" (string_of_pat p)
|
|
|
|
| Tpat_constant n -> "0"
|
|
|
|
| Tpat_construct (_, lid, _, _) ->
|
|
|
|
Printf.sprintf "%s" (String.concat "." (Longident.flatten lid.txt))
|
|
|
|
| Tpat_lazy p ->
|
|
|
|
Printf.sprintf "(lazy %s)" (string_of_pat p)
|
|
|
|
| Tpat_or (p1,p2,_) ->
|
|
|
|
Printf.sprintf "(%s | %s)" (string_of_pat p1) (string_of_pat p2)
|
|
|
|
| Tpat_tuple list ->
|
|
|
|
Printf.sprintf "(%s)" (String.concat "," (List.map string_of_pat list))
|
|
|
|
| Tpat_variant (_, _, _) -> "variant"
|
|
|
|
| Tpat_record (_, _) -> "record"
|
|
|
|
| Tpat_array _ -> "array"
|
|
|
|
in
|
|
|
|
Printf.fprintf stderr "PAT[%s]\n%!" (string_of_pat pat)
|
|
|
|
*)
|
|
|
|
|
2011-01-06 23:17:38 -08:00
|
|
|
(* strictly more powerful than exhaust; however, exhaust
|
|
|
|
was kept for backwards compatibility *)
|
2012-05-31 01:07:31 -07:00
|
|
|
let rec exhaust_gadt (ext:Path.t option) pss n = match pss with
|
2010-11-07 23:34:09 -08:00
|
|
|
| [] -> Rsome [omegas n]
|
|
|
|
| []::_ -> Rnone
|
|
|
|
| pss ->
|
|
|
|
let q0 = discr_pat omega pss in
|
|
|
|
begin match filter_all q0 pss with
|
|
|
|
(* first column of pss is made of variables only *)
|
|
|
|
| [] ->
|
|
|
|
begin match exhaust_gadt ext (filter_extra pss) (n-1) with
|
|
|
|
| Rsome r -> Rsome (List.map (fun row -> q0::row) r)
|
|
|
|
| r -> r
|
|
|
|
end
|
|
|
|
| constrs ->
|
|
|
|
let try_non_omega (p,pss) =
|
|
|
|
if is_absent_pat p then
|
|
|
|
Rnone
|
|
|
|
else
|
|
|
|
match
|
|
|
|
exhaust_gadt
|
|
|
|
ext pss (List.length (simple_match_args p omega) + n - 1)
|
|
|
|
with
|
|
|
|
| Rsome r -> Rsome (List.map (fun row -> (set_args p row)) r)
|
|
|
|
| r -> r in
|
2012-07-30 11:04:46 -07:00
|
|
|
let before = try_many_gadt try_non_omega constrs in
|
2010-11-07 23:34:09 -08:00
|
|
|
if
|
2012-07-30 11:04:46 -07:00
|
|
|
full_match_gadt constrs && not (should_extend ext constrs)
|
2010-11-07 23:34:09 -08:00
|
|
|
then
|
2012-07-30 11:04:46 -07:00
|
|
|
before
|
2010-11-07 23:34:09 -08:00
|
|
|
else
|
|
|
|
(*
|
|
|
|
D = filter_extra pss is the default matrix
|
|
|
|
as it is included in pss, one can avoid
|
|
|
|
recursive calls on specialized matrices,
|
|
|
|
Essentially :
|
|
|
|
* D exhaustive => pss exhaustive
|
|
|
|
* D non-exhaustive => we have a non-filtered value
|
|
|
|
*)
|
|
|
|
let r = exhaust_gadt ext (filter_extra pss) (n-1) in
|
|
|
|
match r with
|
|
|
|
| Rnone -> before
|
|
|
|
| Rsome r ->
|
|
|
|
try
|
2012-07-30 11:04:46 -07:00
|
|
|
let missing_trailing = build_other_gadt ext constrs in
|
|
|
|
let dug =
|
|
|
|
combinations
|
|
|
|
(fun head tail -> head :: tail)
|
|
|
|
missing_trailing
|
|
|
|
r
|
|
|
|
in
|
2013-01-24 20:26:02 -08:00
|
|
|
match before with
|
|
|
|
| Rnone -> Rsome dug
|
|
|
|
| Rsome x -> Rsome (x @ dug)
|
2010-11-07 23:34:09 -08:00
|
|
|
with
|
|
|
|
(* cannot occur, since constructors don't make a full signature *)
|
|
|
|
| Empty -> fatal_error "Parmatch.exhaust"
|
|
|
|
end
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let exhaust_gadt ext pss n =
|
|
|
|
let ret = exhaust_gadt ext pss n in
|
2010-11-07 23:34:09 -08:00
|
|
|
match ret with
|
|
|
|
Rnone -> Rnone
|
|
|
|
| Rsome lst ->
|
2011-07-29 03:32:43 -07:00
|
|
|
(* The following line is needed to compile stdlib/printf.ml *)
|
|
|
|
if lst = [] then Rsome (omegas n) else
|
2012-05-30 07:52:37 -07:00
|
|
|
let singletons =
|
2012-07-30 11:04:46 -07:00
|
|
|
List.map
|
|
|
|
(function
|
|
|
|
[x] -> x
|
|
|
|
| _ -> assert false)
|
|
|
|
lst
|
2010-11-07 23:34:09 -08:00
|
|
|
in
|
|
|
|
Rsome [orify_many singletons]
|
|
|
|
|
2003-08-18 01:26:18 -07:00
|
|
|
(*
|
|
|
|
Another exhaustiveness check, enforcing variant typing.
|
|
|
|
Note that it does not check exact exhaustiveness, but whether a
|
|
|
|
matching could be made exhaustive by closing all variant types.
|
|
|
|
When this is true of all other columns, the current column is left
|
|
|
|
open (even if it means that the whole matching is not exhaustive as
|
|
|
|
a result).
|
|
|
|
When this is false for the matrix minus the current column, and the
|
|
|
|
current column is composed of variant tags, we close the variant
|
|
|
|
(even if it doesn't help in making the matching exhaustive).
|
|
|
|
*)
|
|
|
|
|
2003-08-14 02:04:43 -07:00
|
|
|
let rec pressure_variants tdefs = function
|
|
|
|
| [] -> false
|
|
|
|
| []::_ -> true
|
|
|
|
| pss ->
|
|
|
|
let q0 = discr_pat omega pss in
|
|
|
|
begin match filter_all q0 pss with
|
|
|
|
[] -> pressure_variants tdefs (filter_extra pss)
|
2010-01-22 04:48:24 -08:00
|
|
|
| constrs ->
|
2003-08-14 18:35:24 -07:00
|
|
|
let rec try_non_omega = function
|
|
|
|
(p,pss) :: rem ->
|
|
|
|
let ok = pressure_variants tdefs pss in
|
|
|
|
try_non_omega rem && ok
|
|
|
|
| [] -> true
|
|
|
|
in
|
2010-11-09 01:13:15 -08:00
|
|
|
if full_match true (tdefs=None) constrs then
|
2003-08-18 01:26:18 -07:00
|
|
|
try_non_omega constrs
|
|
|
|
else if tdefs = None then
|
|
|
|
pressure_variants None (filter_extra pss)
|
|
|
|
else
|
2010-10-19 21:24:09 -07:00
|
|
|
let full = full_match true true constrs in
|
2003-08-18 01:26:18 -07:00
|
|
|
let ok =
|
|
|
|
if full then try_non_omega constrs
|
|
|
|
else try_non_omega (filter_all q0 (mark_partial pss))
|
|
|
|
in
|
|
|
|
begin match constrs, tdefs with
|
2008-01-11 08:13:18 -08:00
|
|
|
({pat_desc=Tpat_variant _} as p,_):: _, Some env ->
|
|
|
|
let row = row_of_pat p in
|
2012-07-17 20:21:12 -07:00
|
|
|
if Btype.row_fixed row
|
2003-08-18 01:26:18 -07:00
|
|
|
|| pressure_variants None (filter_extra pss) then ()
|
|
|
|
else close_variant env row
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
ok
|
2003-08-14 02:04:43 -07:00
|
|
|
end
|
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
|
2002-05-27 01:04:03 -07:00
|
|
|
(* Yet another satisfiable fonction *)
|
2002-05-24 07:34:37 -07:00
|
|
|
|
|
|
|
(*
|
|
|
|
This time every_satisfiable pss qs checks the
|
|
|
|
utility of every expansion of qs.
|
|
|
|
Expansion means expansion of or-patterns inside qs
|
|
|
|
*)
|
|
|
|
|
|
|
|
type answer =
|
|
|
|
| Used (* Useful pattern *)
|
|
|
|
| Unused (* Useless pattern *)
|
2012-05-31 01:07:31 -07:00
|
|
|
| Upartial of Typedtree.pattern list (* Mixed, with list of useless ones *)
|
2002-05-24 07:34:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
let pretty_pat p =
|
|
|
|
top_pretty Format.str_formatter p ;
|
|
|
|
prerr_string (Format.flush_str_formatter ())
|
|
|
|
|
|
|
|
type matrix = pattern list list
|
|
|
|
|
|
|
|
let pretty_line ps =
|
|
|
|
List.iter
|
|
|
|
(fun p ->
|
|
|
|
top_pretty Format.str_formatter p ;
|
|
|
|
prerr_string " <" ;
|
|
|
|
prerr_string (Format.flush_str_formatter ()) ;
|
|
|
|
prerr_string ">")
|
|
|
|
ps
|
|
|
|
|
2012-05-29 04:47:28 -07:00
|
|
|
let pretty_matrix (pss : matrix) =
|
2002-05-24 07:34:37 -07:00
|
|
|
prerr_endline "begin matrix" ;
|
|
|
|
List.iter
|
|
|
|
(fun ps ->
|
|
|
|
pretty_line ps ;
|
|
|
|
prerr_endline "")
|
|
|
|
pss ;
|
|
|
|
prerr_endline "end matrix"
|
2010-01-22 04:48:24 -08:00
|
|
|
|
|
|
|
(* this row type enable column processing inside the matrix
|
2002-09-23 01:36:46 -07:00
|
|
|
- left -> elements not to be processed,
|
|
|
|
- right -> elements to be processed
|
|
|
|
*)
|
2002-09-25 09:37:04 -07:00
|
|
|
type 'a row = {no_ors : 'a list ; ors : 'a list ; active : 'a list}
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
let pretty_row {ors=ors ; no_ors=no_ors; active=active} =
|
|
|
|
pretty_line ors ; prerr_string " *" ;
|
|
|
|
pretty_line no_ors ; prerr_string " *" ;
|
|
|
|
pretty_line active
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
let pretty_rows rs =
|
|
|
|
prerr_endline "begin matrix" ;
|
|
|
|
List.iter
|
|
|
|
(fun r ->
|
|
|
|
pretty_row r ;
|
|
|
|
prerr_endline "")
|
|
|
|
rs ;
|
|
|
|
prerr_endline "end matrix"
|
|
|
|
|
|
|
|
(* Initial build *)
|
2002-09-25 09:37:04 -07:00
|
|
|
let make_row ps = {ors=[] ; no_ors=[]; active=ps}
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
let make_rows pss = List.map make_row pss
|
|
|
|
|
|
|
|
|
|
|
|
(* Useful to detect and expand or pats inside as pats *)
|
|
|
|
let rec unalias p = match p.pat_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) -> unalias p
|
2002-09-23 01:36:46 -07:00
|
|
|
| _ -> p
|
|
|
|
|
|
|
|
|
|
|
|
let is_var p = match (unalias p).pat_desc with
|
|
|
|
| Tpat_any|Tpat_var _ -> true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let is_var_column rs =
|
|
|
|
List.for_all
|
2002-09-25 09:37:04 -07:00
|
|
|
(fun r -> match r.active with
|
2002-09-23 01:36:46 -07:00
|
|
|
| p::_ -> is_var p
|
|
|
|
| [] -> assert false)
|
|
|
|
rs
|
|
|
|
|
2003-05-02 02:57:53 -07:00
|
|
|
(* Standard or-args for left-to-right matching *)
|
2002-09-25 09:37:04 -07:00
|
|
|
let rec or_args p = match p.pat_desc with
|
|
|
|
| Tpat_or (p1,p2,_) -> p1,p2
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) -> or_args p
|
2002-09-25 09:37:04 -07:00
|
|
|
| _ -> assert false
|
|
|
|
|
2002-09-23 01:36:46 -07:00
|
|
|
(* Just remove current column *)
|
2002-09-25 09:37:04 -07:00
|
|
|
let remove r = match r.active with
|
|
|
|
| _::rem -> {r with active=rem}
|
2002-09-23 01:36:46 -07:00
|
|
|
| [] -> assert false
|
|
|
|
|
|
|
|
let remove_column rs = List.map remove rs
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-09-23 01:36:46 -07:00
|
|
|
(* Current column has been processed *)
|
2002-09-25 09:37:04 -07:00
|
|
|
let push_no_or r = match r.active with
|
|
|
|
| p::rem -> { r with no_ors = p::r.no_ors ; active=rem}
|
|
|
|
| [] -> assert false
|
|
|
|
|
|
|
|
let push_or r = match r.active with
|
|
|
|
| p::rem -> { r with ors = p::r.ors ; active=rem}
|
2002-09-23 01:36:46 -07:00
|
|
|
| [] -> assert false
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
let push_or_column rs = List.map push_or rs
|
|
|
|
and push_no_or_column rs = List.map push_no_or rs
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
(* Those are adaptations of the previous homonymous functions that
|
|
|
|
work on the current column, instead of the first column
|
|
|
|
*)
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
let discr_pat q rs =
|
2002-09-25 09:37:04 -07:00
|
|
|
discr_pat q (List.map (fun r -> r.active) rs)
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
let filter_one q rs =
|
|
|
|
let rec filter_rec rs = match rs with
|
|
|
|
| [] -> []
|
|
|
|
| r::rem ->
|
2002-09-25 09:37:04 -07:00
|
|
|
match r.active with
|
2002-09-23 01:36:46 -07:00
|
|
|
| [] -> assert false
|
2012-05-30 07:52:37 -07:00
|
|
|
| {pat_desc = Tpat_alias(p,_,_)}::ps ->
|
2002-09-25 09:37:04 -07:00
|
|
|
filter_rec ({r with active = p::ps}::rem)
|
2002-09-23 01:36:46 -07:00
|
|
|
| {pat_desc = Tpat_or(p1,p2,_)}::ps ->
|
|
|
|
filter_rec
|
2002-09-25 09:37:04 -07:00
|
|
|
({r with active = p1::ps}::
|
|
|
|
{r with active = p2::ps}::
|
2002-09-23 01:36:46 -07:00
|
|
|
rem)
|
|
|
|
| p::ps ->
|
|
|
|
if simple_match q p then
|
2002-09-25 09:37:04 -07:00
|
|
|
{r with active=simple_match_args q p @ ps} :: filter_rec rem
|
2002-09-23 01:36:46 -07:00
|
|
|
else
|
|
|
|
filter_rec rem in
|
|
|
|
filter_rec rs
|
|
|
|
|
|
|
|
|
|
|
|
(* Back to normal matrices *)
|
2002-09-25 09:37:04 -07:00
|
|
|
let make_vector r = r.no_ors
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
let make_matrix rs = List.map make_vector rs
|
|
|
|
|
|
|
|
|
|
|
|
(* Standard union on answers *)
|
|
|
|
let union_res r1 r2 = match r1, r2 with
|
2002-09-23 10:19:33 -07:00
|
|
|
| (Unused,_)
|
2002-09-25 09:37:04 -07:00
|
|
|
| (_, Unused) -> Unused
|
2002-09-23 01:36:46 -07:00
|
|
|
| Used,_ -> r2
|
|
|
|
| _, Used -> r1
|
|
|
|
| Upartial u1, Upartial u2 -> Upartial (u1@u2)
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
(* propose or pats for expansion *)
|
|
|
|
let extract_elements qs =
|
|
|
|
let rec do_rec seen = function
|
|
|
|
| [] -> []
|
|
|
|
| q::rem ->
|
|
|
|
{no_ors= List.rev_append seen rem @ qs.no_ors ;
|
|
|
|
ors=[] ;
|
|
|
|
active = [q]}::
|
|
|
|
do_rec (q::seen) rem in
|
|
|
|
do_rec [] qs.ors
|
|
|
|
|
|
|
|
(* idem for matrices *)
|
|
|
|
let transpose rs = match rs with
|
|
|
|
| [] -> assert false
|
|
|
|
| r::rem ->
|
|
|
|
let i = List.map (fun x -> [x]) r in
|
|
|
|
List.fold_left
|
|
|
|
(List.map2 (fun r x -> x::r))
|
|
|
|
i rem
|
|
|
|
|
|
|
|
let extract_columns pss qs = match pss with
|
|
|
|
| [] -> List.map (fun _ -> []) qs.ors
|
|
|
|
| _ ->
|
|
|
|
let rows = List.map extract_elements pss in
|
|
|
|
transpose rows
|
2002-09-23 01:36:46 -07:00
|
|
|
|
|
|
|
(* Core function
|
2002-09-25 09:37:04 -07:00
|
|
|
The idea is to first look for or patterns (recursive case), then
|
|
|
|
check or-patterns argument usefulness (terminal case)
|
2002-09-23 01:36:46 -07:00
|
|
|
*)
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
let rec every_satisfiables pss qs = match qs.active with
|
2002-09-23 01:36:46 -07:00
|
|
|
| [] ->
|
2002-09-25 09:37:04 -07:00
|
|
|
(* qs is now partitionned, check usefulness *)
|
|
|
|
begin match qs.ors with
|
|
|
|
| [] -> (* no or-patterns *)
|
|
|
|
if satisfiable (make_matrix pss) (make_vector qs) then
|
|
|
|
Used
|
|
|
|
else
|
|
|
|
Unused
|
|
|
|
| _ -> (* n or-patterns -> 2n expansions *)
|
|
|
|
List.fold_right2
|
|
|
|
(fun pss qs r -> match r with
|
|
|
|
| Unused -> Unused
|
|
|
|
| _ ->
|
|
|
|
match qs.active with
|
|
|
|
| [q] ->
|
|
|
|
let q1,q2 = or_args q in
|
|
|
|
let r_loc = every_both pss qs q1 q2 in
|
|
|
|
union_res r r_loc
|
|
|
|
| _ -> assert false)
|
|
|
|
(extract_columns pss qs) (extract_elements qs)
|
|
|
|
Used
|
|
|
|
end
|
2002-09-23 01:36:46 -07:00
|
|
|
| q::rem ->
|
|
|
|
let uq = unalias q in
|
|
|
|
begin match uq.pat_desc with
|
|
|
|
| Tpat_any | Tpat_var _ ->
|
|
|
|
if is_var_column pss then
|
|
|
|
(* forget about ``all-variable'' columns now *)
|
|
|
|
every_satisfiables (remove_column pss) (remove qs)
|
2002-05-24 07:34:37 -07:00
|
|
|
else
|
2002-09-25 09:37:04 -07:00
|
|
|
(* otherwise this is direct food for satisfiable *)
|
|
|
|
every_satisfiables (push_no_or_column pss) (push_no_or qs)
|
2002-09-23 01:36:46 -07:00
|
|
|
| Tpat_or (q1,q2,_) ->
|
2003-02-24 07:13:01 -08:00
|
|
|
if
|
|
|
|
q1.pat_loc.Location.loc_ghost &&
|
|
|
|
q2.pat_loc.Location.loc_ghost
|
|
|
|
then
|
2002-09-23 01:36:46 -07:00
|
|
|
(* syntactically generated or-pats should not be expanded *)
|
2002-09-25 09:37:04 -07:00
|
|
|
every_satisfiables (push_no_or_column pss) (push_no_or qs)
|
2010-01-22 04:48:24 -08:00
|
|
|
else
|
2002-09-25 09:37:04 -07:00
|
|
|
(* this is a real or-pattern *)
|
|
|
|
every_satisfiables (push_or_column pss) (push_or qs)
|
|
|
|
| Tpat_variant (l,_,r) when is_absent l r -> (* Ah Jacques... *)
|
2002-09-23 01:36:46 -07:00
|
|
|
Unused
|
|
|
|
| _ ->
|
|
|
|
(* standard case, filter matrix *)
|
|
|
|
let q0 = discr_pat q pss in
|
|
|
|
every_satisfiables
|
|
|
|
(filter_one q0 pss)
|
2002-09-25 09:37:04 -07:00
|
|
|
{qs with active=simple_match_args q0 q @ rem}
|
2002-05-24 07:34:37 -07:00
|
|
|
end
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2002-09-23 01:36:46 -07:00
|
|
|
(*
|
2002-09-23 10:19:33 -07:00
|
|
|
This function ``every_both'' performs the usefulness check
|
|
|
|
of or-pat q1|q2.
|
|
|
|
The trick is to call every_satisfied twice with
|
|
|
|
current active columns restricted to q1 and q2,
|
|
|
|
That way,
|
2002-09-25 09:37:04 -07:00
|
|
|
- others orpats in qs.ors will not get expanded.
|
|
|
|
- all matching work performed on qs.no_ors is not performed again.
|
2002-09-23 10:19:33 -07:00
|
|
|
*)
|
2002-09-23 01:36:46 -07:00
|
|
|
and every_both pss qs q1 q2 =
|
2002-09-25 09:37:04 -07:00
|
|
|
let qs1 = {qs with active=[q1]}
|
|
|
|
and qs2 = {qs with active=[q2]} in
|
2002-09-23 01:36:46 -07:00
|
|
|
let r1 = every_satisfiables pss qs1
|
2002-09-23 10:19:33 -07:00
|
|
|
and r2 = every_satisfiables (if compat q1 q2 then qs1::pss else pss) qs2 in
|
2002-09-23 01:36:46 -07:00
|
|
|
match r1 with
|
|
|
|
| Unused ->
|
|
|
|
begin match r2 with
|
|
|
|
| Unused -> Unused
|
|
|
|
| Used -> Upartial [q1]
|
|
|
|
| Upartial u2 -> Upartial (q1::u2)
|
|
|
|
end
|
|
|
|
| Used ->
|
|
|
|
begin match r2 with
|
|
|
|
| Unused -> Upartial [q2]
|
|
|
|
| _ -> r2
|
|
|
|
end
|
|
|
|
| Upartial u1 ->
|
|
|
|
begin match r2 with
|
|
|
|
| Unused -> Upartial (u1@[q2])
|
|
|
|
| Used -> r1
|
|
|
|
| Upartial u2 -> Upartial (u1 @ u2)
|
|
|
|
end
|
1999-10-29 07:42:37 -07:00
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-05-24 07:34:37 -07:00
|
|
|
(* le_pat p q means, forall V, V matches q implies V matches p *)
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec le_pat p q =
|
|
|
|
match (p.pat_desc, q.pat_desc) with
|
2002-09-25 09:37:04 -07:00
|
|
|
| (Tpat_var _|Tpat_any),_ -> true
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias(p,_,_), _ -> le_pat p q
|
|
|
|
| _, Tpat_alias(q,_,_) -> le_pat p q
|
2012-09-18 23:57:17 -07:00
|
|
|
| Tpat_constant(c1), Tpat_constant(c2) -> const_compare c1 c2 = 0
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct(_,c1,ps,_), Tpat_construct(_,c2,qs,_) ->
|
1998-04-06 02:23:01 -07:00
|
|
|
c1.cstr_tag = c2.cstr_tag && le_pats ps qs
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tpat_variant(l1,Some p1,_), Tpat_variant(l2,Some p2,_) ->
|
2002-05-24 07:34:37 -07:00
|
|
|
(l1 = l2 && le_pat p1 p2)
|
|
|
|
| Tpat_variant(l1,None,r1), Tpat_variant(l2,None,_) ->
|
|
|
|
l1 = l2
|
2004-01-09 05:12:17 -08:00
|
|
|
| Tpat_variant(_,_,_), Tpat_variant(_,_,_) -> false
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tpat_tuple(ps), Tpat_tuple(qs) -> le_pats ps qs
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy p, Tpat_lazy q -> le_pat p q
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (l1,_), Tpat_record (l2,_) ->
|
2001-02-19 12:27:52 -08:00
|
|
|
let ps,qs = records_args l1 l2 in
|
1999-10-29 07:42:37 -07:00
|
|
|
le_pats ps qs
|
1998-04-06 02:23:01 -07:00
|
|
|
| Tpat_array(ps), Tpat_array(qs) ->
|
2002-09-23 01:36:46 -07:00
|
|
|
List.length ps = List.length qs && le_pats ps qs
|
2001-02-19 12:27:52 -08:00
|
|
|
(* In all other cases, enumeration is performed *)
|
2004-01-16 06:09:30 -08:00
|
|
|
| _,_ -> not (satisfiable [[p]] [q])
|
2010-01-22 04:48:24 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
and le_pats ps qs =
|
|
|
|
match ps,qs with
|
1998-04-06 02:23:01 -07:00
|
|
|
p::ps, q::qs -> le_pat p q && le_pats ps qs
|
1995-05-04 03:15:53 -07:00
|
|
|
| _, _ -> true
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
let get_mins le ps =
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec select_rec r = function
|
2002-09-23 01:36:46 -07:00
|
|
|
[] -> r
|
|
|
|
| p::ps ->
|
|
|
|
if List.exists (fun p0 -> le p0 p) ps
|
|
|
|
then select_rec r ps
|
|
|
|
else select_rec (p::r) ps in
|
1995-05-04 03:15:53 -07:00
|
|
|
select_rec [] (select_rec [] ps)
|
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
(*
|
2002-09-23 01:36:46 -07:00
|
|
|
lub p q is a pattern that matches all values matched by p and q
|
|
|
|
may raise Empty, when p and q and not compatible
|
2002-09-25 09:37:04 -07:00
|
|
|
*)
|
2001-02-19 12:27:52 -08:00
|
|
|
|
|
|
|
let rec lub p q = match p.pat_desc,q.pat_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_),_ -> lub p q
|
|
|
|
| _,Tpat_alias (q,_,_) -> lub p q
|
2001-02-19 12:27:52 -08:00
|
|
|
| (Tpat_any|Tpat_var _),_ -> q
|
|
|
|
| _,(Tpat_any|Tpat_var _) -> p
|
2001-09-25 02:54:18 -07:00
|
|
|
| Tpat_or (p1,p2,_),_ -> orlub p1 p2 q
|
|
|
|
| _,Tpat_or (q1,q2,_) -> orlub q1 q2 p (* Thanks god, lub is commutative *)
|
2012-09-18 23:57:17 -07:00
|
|
|
| Tpat_constant c1, Tpat_constant c2 when const_compare c1 c2 = 0 -> p
|
2001-02-19 12:27:52 -08:00
|
|
|
| Tpat_tuple ps, Tpat_tuple qs ->
|
|
|
|
let rs = lubs ps qs in
|
|
|
|
make_pat (Tpat_tuple rs) p.pat_type p.pat_env
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy p, Tpat_lazy q ->
|
|
|
|
let r = lub p q in
|
|
|
|
make_pat (Tpat_lazy r) p.pat_type p.pat_env
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (lid, c1,ps1,_), Tpat_construct (_,c2,ps2,_)
|
2001-02-19 12:27:52 -08:00
|
|
|
when c1.cstr_tag = c2.cstr_tag ->
|
|
|
|
let rs = lubs ps1 ps2 in
|
2012-10-24 05:03:00 -07:00
|
|
|
make_pat (Tpat_construct (lid, c1,rs, false))
|
2012-05-31 01:07:31 -07:00
|
|
|
p.pat_type p.pat_env
|
2001-02-19 12:27:52 -08:00
|
|
|
| Tpat_variant(l1,Some p1,row), Tpat_variant(l2,Some p2,_)
|
2004-01-09 05:12:17 -08:00
|
|
|
when l1=l2 ->
|
2002-09-23 01:36:46 -07:00
|
|
|
let r=lub p1 p2 in
|
|
|
|
make_pat (Tpat_variant (l1,Some r,row)) p.pat_type p.pat_env
|
2002-05-24 07:34:37 -07:00
|
|
|
| Tpat_variant (l1,None,row), Tpat_variant(l2,None,_)
|
2004-01-09 05:12:17 -08:00
|
|
|
when l1 = l2 -> p
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (l1,closed),Tpat_record (l2,_) ->
|
2001-02-19 12:27:52 -08:00
|
|
|
let rs = record_lubs l1 l2 in
|
2012-05-30 07:52:37 -07:00
|
|
|
make_pat (Tpat_record (rs, closed)) p.pat_type p.pat_env
|
2001-02-19 12:27:52 -08:00
|
|
|
| Tpat_array ps, Tpat_array qs
|
2002-09-23 01:36:46 -07:00
|
|
|
when List.length ps = List.length qs ->
|
|
|
|
let rs = lubs ps qs in
|
|
|
|
make_pat (Tpat_array rs) p.pat_type p.pat_env
|
2001-02-19 12:27:52 -08:00
|
|
|
| _,_ ->
|
|
|
|
raise Empty
|
|
|
|
|
|
|
|
and orlub p1 p2 q =
|
2010-01-22 04:48:24 -08:00
|
|
|
try
|
2001-02-19 12:27:52 -08:00
|
|
|
let r1 = lub p1 q in
|
|
|
|
try
|
2001-09-25 02:54:18 -07:00
|
|
|
{q with pat_desc=(Tpat_or (r1,lub p2 q,None))}
|
2001-02-19 12:27:52 -08:00
|
|
|
with
|
2002-09-23 01:36:46 -07:00
|
|
|
| Empty -> r1
|
|
|
|
with
|
|
|
|
| Empty -> lub p2 q
|
2001-02-19 12:27:52 -08:00
|
|
|
|
|
|
|
and record_lubs l1 l2 =
|
|
|
|
let rec lub_rec l1 l2 = match l1,l2 with
|
|
|
|
| [],_ -> l2
|
|
|
|
| _,[] -> l1
|
2012-10-24 05:03:00 -07:00
|
|
|
| (lid1, lbl1,p1)::rem1, (lid2, lbl2,p2)::rem2 ->
|
2001-02-19 12:27:52 -08:00
|
|
|
if lbl1.lbl_pos < lbl2.lbl_pos then
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid1, lbl1,p1)::lub_rec rem1 l2
|
2001-02-19 12:27:52 -08:00
|
|
|
else if lbl2.lbl_pos < lbl1.lbl_pos then
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid2, lbl2,p2)::lub_rec l1 rem2
|
2001-02-19 12:27:52 -08:00
|
|
|
else
|
2012-10-24 05:03:00 -07:00
|
|
|
(lid1, lbl1,lub p1 p2)::lub_rec rem1 rem2 in
|
2001-02-19 12:27:52 -08:00
|
|
|
lub_rec l1 l2
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2001-02-19 12:27:52 -08:00
|
|
|
and lubs ps qs = match ps,qs with
|
|
|
|
| p::ps, q::qs -> lub p q :: lubs ps qs
|
|
|
|
| _,_ -> []
|
2010-01-22 04:48:24 -08:00
|
|
|
|
|
|
|
|
1999-10-29 07:42:37 -07:00
|
|
|
(******************************)
|
2006-09-21 07:54:54 -07:00
|
|
|
(* Exported variant closing *)
|
1999-10-29 07:42:37 -07:00
|
|
|
(******************************)
|
|
|
|
|
2003-08-18 01:26:18 -07:00
|
|
|
(* Apply pressure to variants *)
|
|
|
|
|
|
|
|
let pressure_variants tdefs patl =
|
|
|
|
let pss = List.map (fun p -> [p;omega]) patl in
|
|
|
|
ignore (pressure_variants (Some tdefs) pss)
|
2001-02-19 12:27:52 -08:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
(*****************************)
|
|
|
|
(* Utilities for diagnostics *)
|
|
|
|
(*****************************)
|
|
|
|
|
2001-10-04 02:03:44 -07:00
|
|
|
(*
|
2006-09-21 07:54:54 -07:00
|
|
|
Build up a working pattern matrix by forgetting
|
|
|
|
about guarded patterns
|
2002-09-25 09:37:04 -07:00
|
|
|
*)
|
|
|
|
|
|
|
|
let has_guard act = match act.exp_desc with
|
|
|
|
| Texp_when(_, _) -> true
|
|
|
|
| _ -> false
|
|
|
|
|
2001-10-04 02:03:44 -07:00
|
|
|
|
|
|
|
let rec initial_matrix = function
|
|
|
|
[] -> []
|
|
|
|
| (pat, act) :: rem ->
|
2002-09-23 01:36:46 -07:00
|
|
|
if has_guard act
|
|
|
|
then
|
|
|
|
initial_matrix rem
|
|
|
|
else
|
|
|
|
[pat] :: initial_matrix rem
|
2001-02-19 12:27:52 -08:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
(******************************************)
|
|
|
|
(* Look for a row that matches some value *)
|
|
|
|
(******************************************)
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
(*
|
2006-09-21 07:54:54 -07:00
|
|
|
Useful for seeing if the example of
|
|
|
|
non-matched value can indeed be matched
|
|
|
|
(by a guarded clause)
|
2002-09-25 09:37:04 -07:00
|
|
|
*)
|
2006-09-21 07:54:54 -07:00
|
|
|
|
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
|
2001-10-04 09:06:31 -07:00
|
|
|
exception NoGuard
|
|
|
|
|
|
|
|
let rec initial_all no_guard = function
|
|
|
|
| [] ->
|
|
|
|
if no_guard then
|
|
|
|
raise NoGuard
|
|
|
|
else
|
2002-09-23 01:36:46 -07:00
|
|
|
[]
|
2001-10-04 09:06:31 -07:00
|
|
|
| (pat, act) :: rem ->
|
|
|
|
([pat], pat.pat_loc) :: initial_all (no_guard && not (has_guard act)) rem
|
|
|
|
|
|
|
|
|
|
|
|
let rec do_filter_var = function
|
|
|
|
| (_::ps,loc)::rem -> (ps,loc)::do_filter_var rem
|
|
|
|
| _ -> []
|
|
|
|
|
|
|
|
let do_filter_one q pss =
|
|
|
|
let rec filter_rec = function
|
2012-05-30 07:52:37 -07:00
|
|
|
| ({pat_desc = Tpat_alias(p,_,_)}::ps,loc)::pss ->
|
2001-10-04 09:06:31 -07:00
|
|
|
filter_rec ((p::ps,loc)::pss)
|
|
|
|
| ({pat_desc = Tpat_or(p1,p2,_)}::ps,loc)::pss ->
|
2002-09-23 01:36:46 -07:00
|
|
|
filter_rec ((p1::ps,loc)::(p2::ps,loc)::pss)
|
2001-10-04 09:06:31 -07:00
|
|
|
| (p::ps,loc)::pss ->
|
2002-09-23 01:36:46 -07:00
|
|
|
if simple_match q p
|
|
|
|
then (simple_match_args q p @ ps, loc) :: filter_rec pss
|
|
|
|
else filter_rec pss
|
2001-10-04 09:06:31 -07:00
|
|
|
| _ -> [] in
|
2002-09-23 01:36:46 -07:00
|
|
|
filter_rec pss
|
2001-10-04 09:06:31 -07:00
|
|
|
|
|
|
|
let rec do_match pss qs = match qs with
|
|
|
|
| [] ->
|
|
|
|
begin match pss with
|
|
|
|
| ([],loc)::_ -> Some loc
|
|
|
|
| _ -> None
|
|
|
|
end
|
|
|
|
| q::qs -> match q with
|
|
|
|
| {pat_desc = Tpat_or (q1,q2,_)} ->
|
|
|
|
begin match do_match pss (q1::qs) with
|
|
|
|
| None -> do_match pss (q2::qs)
|
|
|
|
| r -> r
|
|
|
|
end
|
|
|
|
| {pat_desc = Tpat_any} ->
|
|
|
|
do_match (do_filter_var pss) qs
|
|
|
|
| _ ->
|
|
|
|
let q0 = normalize_pat q in
|
|
|
|
do_match (do_filter_one q0 pss) (simple_match_args q0 q @ qs)
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2001-10-04 09:06:31 -07:00
|
|
|
let check_partial_all v casel =
|
|
|
|
try
|
2002-09-25 09:37:04 -07:00
|
|
|
let pss = initial_all true casel in
|
2001-10-04 09:06:31 -07:00
|
|
|
do_match pss [v]
|
|
|
|
with
|
|
|
|
| NoGuard -> None
|
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
(************************)
|
|
|
|
(* Exhaustiveness check *)
|
|
|
|
(************************)
|
|
|
|
|
2010-11-07 23:34:09 -08:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let rec get_first f =
|
2010-11-07 23:34:09 -08:00
|
|
|
function
|
2010-11-08 00:54:26 -08:00
|
|
|
| [] -> None
|
2012-05-30 07:52:37 -07:00
|
|
|
| x :: xs ->
|
2012-07-30 11:04:46 -07:00
|
|
|
match f x with
|
|
|
|
| None -> get_first f xs
|
|
|
|
| x -> x
|
2010-11-07 23:34:09 -08:00
|
|
|
|
|
|
|
|
|
|
|
(* conversion from Typedtree.pattern to Parsetree.pattern list *)
|
2011-08-09 06:59:41 -07:00
|
|
|
module Conv = struct
|
2010-11-07 23:34:09 -08:00
|
|
|
open Parsetree
|
2013-03-08 06:59:45 -08:00
|
|
|
let mkpat desc = Ast_helper.Pat.mk desc
|
2010-11-07 23:34:09 -08:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let rec select : 'a list list -> 'a list list =
|
2010-11-07 23:34:09 -08:00
|
|
|
function
|
|
|
|
| xs :: [] -> List.map (fun y -> [y]) xs
|
|
|
|
| (x::xs)::ys ->
|
2012-07-30 11:04:46 -07:00
|
|
|
List.map
|
|
|
|
(fun lst -> x :: lst)
|
|
|
|
(select ys)
|
|
|
|
@
|
|
|
|
select (xs::ys)
|
2010-11-07 23:34:09 -08:00
|
|
|
| _ -> []
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let name_counter = ref 0
|
2013-01-24 20:26:02 -08:00
|
|
|
let fresh name =
|
2012-05-30 07:52:37 -07:00
|
|
|
let current = !name_counter in
|
2011-08-09 06:59:41 -07:00
|
|
|
name_counter := !name_counter + 1;
|
2013-01-24 20:26:02 -08:00
|
|
|
"#$" ^ name ^ string_of_int current
|
2010-11-15 22:01:59 -08:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let conv (typed: Typedtree.pattern) :
|
|
|
|
Parsetree.pattern list *
|
2012-10-24 05:03:00 -07:00
|
|
|
(string, Types.constructor_description) Hashtbl.t *
|
|
|
|
(string, Types.label_description) Hashtbl.t
|
2012-05-30 07:52:37 -07:00
|
|
|
=
|
|
|
|
let constrs = Hashtbl.create 0 in
|
|
|
|
let labels = Hashtbl.create 0 in
|
|
|
|
let rec loop pat =
|
2010-11-07 23:34:09 -08:00
|
|
|
match pat.pat_desc with
|
2011-08-09 06:59:41 -07:00
|
|
|
Tpat_or (a,b,_) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
loop a @ loop b
|
2011-07-29 03:32:43 -07:00
|
|
|
| Tpat_any | Tpat_constant _ | Tpat_var _ ->
|
2012-07-30 11:04:46 -07:00
|
|
|
[mkpat Ppat_any]
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) -> loop p
|
2010-11-07 23:34:09 -08:00
|
|
|
| Tpat_tuple lst ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let results = select (List.map loop lst) in
|
|
|
|
List.map
|
|
|
|
(fun lst -> mkpat (Ppat_tuple lst))
|
|
|
|
results
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (cstr_lid, cstr,lst,_) ->
|
2013-01-24 20:26:02 -08:00
|
|
|
let id = fresh cstr.cstr_name in
|
2012-05-30 07:52:37 -07:00
|
|
|
let lid = { cstr_lid with txt = Longident.Lident id } in
|
2012-10-24 05:03:00 -07:00
|
|
|
Hashtbl.add constrs id cstr;
|
2012-07-30 11:04:46 -07:00
|
|
|
let results = select (List.map loop lst) in
|
|
|
|
begin match lst with
|
|
|
|
[] ->
|
|
|
|
[mkpat (Ppat_construct(lid, None, false))]
|
2010-11-08 00:43:57 -08:00
|
|
|
| _ ->
|
2012-07-30 11:04:46 -07:00
|
|
|
List.map
|
|
|
|
(fun lst ->
|
|
|
|
let arg =
|
|
|
|
match lst with
|
|
|
|
[] -> assert false
|
|
|
|
| [x] -> Some x
|
|
|
|
| _ -> Some (mkpat (Ppat_tuple lst))
|
|
|
|
in
|
|
|
|
mkpat (Ppat_construct(lid, arg, false)))
|
|
|
|
results
|
2011-08-09 06:59:41 -07:00
|
|
|
end
|
2010-11-07 23:34:09 -08:00
|
|
|
| Tpat_variant(label,p_opt,row_desc) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
begin match p_opt with
|
|
|
|
| None ->
|
|
|
|
[mkpat (Ppat_variant(label, None))]
|
|
|
|
| Some p ->
|
|
|
|
let results = loop p in
|
|
|
|
List.map
|
|
|
|
(fun p ->
|
|
|
|
mkpat (Ppat_variant(label, Some p)))
|
|
|
|
results
|
2011-08-09 06:59:41 -07:00
|
|
|
end
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (subpatterns, _closed_flag) ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let pats =
|
|
|
|
select
|
2012-10-24 05:03:00 -07:00
|
|
|
(List.map (fun (_,_,x) -> loop x) subpatterns)
|
2012-07-30 11:04:46 -07:00
|
|
|
in
|
|
|
|
let label_idents =
|
|
|
|
List.map
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun (_,lbl,_) ->
|
2013-01-24 20:26:02 -08:00
|
|
|
let id = fresh lbl.lbl_name in
|
2012-10-24 05:03:00 -07:00
|
|
|
Hashtbl.add labels id lbl;
|
2012-07-30 11:04:46 -07:00
|
|
|
Longident.Lident id)
|
|
|
|
subpatterns
|
|
|
|
in
|
|
|
|
List.map
|
|
|
|
(fun lst ->
|
|
|
|
let lst = List.map2 (fun lid pat ->
|
2012-05-30 07:52:37 -07:00
|
|
|
(mknoloc lid, pat)
|
|
|
|
) label_idents lst in
|
|
|
|
mkpat (Ppat_record (lst, Open)))
|
2012-07-30 11:04:46 -07:00
|
|
|
pats
|
2010-11-07 23:34:09 -08:00
|
|
|
| Tpat_array lst ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let results = select (List.map loop lst) in
|
|
|
|
List.map (fun lst -> mkpat (Ppat_array lst)) results
|
2010-11-07 23:34:09 -08:00
|
|
|
| Tpat_lazy p ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let results = loop p in
|
|
|
|
List.map (fun p -> mkpat (Ppat_lazy p)) results
|
2010-11-07 23:34:09 -08:00
|
|
|
in
|
2012-05-30 07:52:37 -07:00
|
|
|
let ps = loop typed in
|
2010-11-15 22:01:59 -08:00
|
|
|
(ps, constrs, labels)
|
2010-11-07 23:34:09 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
let do_check_partial ?pred exhaust loc casel pss = match pss with
|
2006-09-21 07:54:54 -07:00
|
|
|
| [] ->
|
2000-08-11 12:58:52 -07:00
|
|
|
(*
|
|
|
|
This can occur
|
2001-10-04 02:03:44 -07:00
|
|
|
- For empty matches generated by ocamlp4 (no warning)
|
2002-05-24 07:34:37 -07:00
|
|
|
- when all patterns have guards (then, casel <> [])
|
2002-09-23 01:36:46 -07:00
|
|
|
(specific warning)
|
2002-05-24 07:34:37 -07:00
|
|
|
Then match MUST be considered non-exhaustive,
|
2001-10-04 02:03:44 -07:00
|
|
|
otherwise compilation of PM is broken.
|
|
|
|
*)
|
2006-09-21 07:54:54 -07:00
|
|
|
begin match casel with
|
|
|
|
| [] -> ()
|
|
|
|
| _ -> Location.prerr_warning loc Warnings.All_clauses_guarded
|
|
|
|
end ;
|
|
|
|
Partial
|
2010-01-22 04:48:24 -08:00
|
|
|
| ps::_ ->
|
2006-09-21 07:54:54 -07:00
|
|
|
begin match exhaust None pss (List.length ps) with
|
|
|
|
| Rnone -> Total
|
2010-11-07 23:34:09 -08:00
|
|
|
| Rsome [u] ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let v =
|
|
|
|
match pred with
|
|
|
|
| Some pred ->
|
|
|
|
let (patterns,constrs,labels) = Conv.conv u in
|
2012-05-30 07:52:37 -07:00
|
|
|
(* Hashtbl.iter (fun s (path, _) ->
|
2012-05-31 01:07:31 -07:00
|
|
|
Printf.fprintf stderr "CONV: %s -> %s \n%!" s (Path.name path))
|
|
|
|
constrs
|
2012-05-30 07:52:37 -07:00
|
|
|
; *)
|
2012-07-30 11:04:46 -07:00
|
|
|
get_first (pred constrs labels) patterns
|
|
|
|
| None -> Some u
|
|
|
|
in
|
|
|
|
begin match v with
|
|
|
|
None -> Total
|
|
|
|
| Some v ->
|
2010-11-07 23:34:09 -08:00
|
|
|
let errmsg =
|
|
|
|
try
|
2012-07-30 11:04:46 -07:00
|
|
|
let buf = Buffer.create 16 in
|
|
|
|
let fmt = formatter_of_buffer buf in
|
|
|
|
top_pretty fmt v;
|
|
|
|
begin match check_partial_all v casel with
|
|
|
|
| None -> ()
|
|
|
|
| Some _ ->
|
2010-11-07 23:34:09 -08:00
|
|
|
(* This is 'Some loc', where loc is the location of
|
|
|
|
a possibly matching clause.
|
|
|
|
Forget about loc, because printing two locations
|
|
|
|
is a pain in the top-level *)
|
|
|
|
Buffer.add_string buf
|
|
|
|
"\n(However, some guarded clause may match this value.)"
|
2012-07-30 11:04:46 -07:00
|
|
|
end ;
|
|
|
|
Buffer.contents buf
|
2010-11-07 23:34:09 -08:00
|
|
|
with _ ->
|
2012-07-30 11:04:46 -07:00
|
|
|
"" in
|
2010-11-07 23:34:09 -08:00
|
|
|
Location.prerr_warning loc (Warnings.Partial_match errmsg) ;
|
|
|
|
Partial end
|
2006-09-21 07:54:54 -07:00
|
|
|
| _ ->
|
|
|
|
fatal_error "Parmatch.check_partial"
|
|
|
|
end
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let do_check_partial_normal loc casel pss =
|
2010-11-08 02:19:29 -08:00
|
|
|
do_check_partial exhaust loc casel pss
|
2006-09-21 07:54:54 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let do_check_partial_gadt pred loc casel pss =
|
2010-11-08 02:19:29 -08:00
|
|
|
do_check_partial ~pred exhaust_gadt loc casel pss
|
2010-11-07 23:34:09 -08:00
|
|
|
|
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
|
|
|
|
(*****************)
|
|
|
|
(* Fragile check *)
|
|
|
|
(*****************)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
(* Collect all data types in a pattern *)
|
2002-09-30 07:23:00 -07:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let rec add_path path = function
|
|
|
|
| [] -> [path]
|
|
|
|
| x::rem as paths ->
|
|
|
|
if Path.same path x then paths
|
|
|
|
else x::add_path path rem
|
2001-02-19 12:27:52 -08:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let extendable_path path =
|
|
|
|
not
|
|
|
|
(Path.same path Predef.path_bool ||
|
|
|
|
Path.same path Predef.path_list ||
|
2011-07-20 08:37:36 -07:00
|
|
|
Path.same path Predef.path_unit ||
|
2006-09-21 07:54:54 -07:00
|
|
|
Path.same path Predef.path_option)
|
2002-09-30 07:23:00 -07:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
let rec collect_paths_from_pat r p = match p.pat_desc with
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct(_, {cstr_tag=(Cstr_constant _|Cstr_block _)},ps,_) ->
|
2006-09-21 07:54:54 -07:00
|
|
|
let path = get_type_path p.pat_type p.pat_env in
|
|
|
|
List.fold_left
|
|
|
|
collect_paths_from_pat
|
|
|
|
(if extendable_path path then add_path path r else r)
|
|
|
|
ps
|
|
|
|
| Tpat_any|Tpat_var _|Tpat_constant _| Tpat_variant (_,None,_) -> r
|
|
|
|
| Tpat_tuple ps | Tpat_array ps
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_construct (_, {cstr_tag=Cstr_exception _}, ps,_)->
|
2006-09-21 07:54:54 -07:00
|
|
|
List.fold_left collect_paths_from_pat r ps
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (lps,_) ->
|
2006-09-21 07:54:54 -07:00
|
|
|
List.fold_left
|
2012-10-24 05:03:00 -07:00
|
|
|
(fun r (_, _, p) -> collect_paths_from_pat r p)
|
2006-09-21 07:54:54 -07:00
|
|
|
r lps
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_variant (_, Some p, _) | Tpat_alias (p,_,_) -> collect_paths_from_pat r p
|
2006-09-21 07:54:54 -07:00
|
|
|
| Tpat_or (p1,p2,_) ->
|
|
|
|
collect_paths_from_pat (collect_paths_from_pat r p1) p2
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_lazy p
|
|
|
|
->
|
2008-07-09 06:03:38 -07:00
|
|
|
collect_paths_from_pat r p
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2006-09-21 07:54:54 -07:00
|
|
|
|
|
|
|
(*
|
|
|
|
Actual fragile check
|
|
|
|
1. Collect data types in the patterns of the match.
|
|
|
|
2. One exhautivity check per datatype, considering that
|
|
|
|
the type is extended.
|
2003-05-02 02:57:53 -07:00
|
|
|
*)
|
|
|
|
|
2010-11-08 01:58:09 -08:00
|
|
|
let do_check_fragile_param exhaust loc casel pss =
|
2006-09-21 07:54:54 -07:00
|
|
|
let exts =
|
|
|
|
List.fold_left
|
|
|
|
(fun r (p,_) -> collect_paths_from_pat r p)
|
|
|
|
[] casel in
|
|
|
|
match exts with
|
|
|
|
| [] -> ()
|
|
|
|
| _ -> match pss with
|
|
|
|
| [] -> ()
|
|
|
|
| ps::_ ->
|
|
|
|
List.iter
|
|
|
|
(fun ext ->
|
|
|
|
match exhaust (Some ext) pss (List.length ps) with
|
|
|
|
| Rnone ->
|
|
|
|
Location.prerr_warning
|
|
|
|
loc
|
|
|
|
(Warnings.Fragile_match (Path.name ext))
|
|
|
|
| Rsome _ -> ())
|
|
|
|
exts
|
|
|
|
|
2010-11-08 01:58:09 -08:00
|
|
|
let do_check_fragile_normal = do_check_fragile_param exhaust
|
|
|
|
let do_check_fragile_gadt = do_check_fragile_param exhaust_gadt
|
2006-09-21 07:54:54 -07:00
|
|
|
|
|
|
|
(********************************)
|
|
|
|
(* Exported unused clause check *)
|
|
|
|
(********************************)
|
2003-05-02 02:57:53 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let check_unused tdefs casel =
|
2000-11-06 01:49:27 -08:00
|
|
|
if Warnings.is_active Warnings.Unused_match then
|
2002-09-30 07:23:00 -07:00
|
|
|
let rec do_rec pref = function
|
|
|
|
| [] -> ()
|
2004-11-25 04:44:33 -08:00
|
|
|
| (q,act)::rem ->
|
2002-09-30 07:23:00 -07:00
|
|
|
let qs = [q] in
|
|
|
|
begin try
|
2004-01-09 05:12:17 -08:00
|
|
|
let pss =
|
|
|
|
get_mins le_pats (List.filter (compats qs) pref) in
|
2002-09-30 07:23:00 -07:00
|
|
|
let r = every_satisfiables (make_rows pss) (make_row qs) in
|
|
|
|
match r with
|
|
|
|
| Unused ->
|
|
|
|
Location.prerr_warning
|
2006-09-21 07:54:54 -07:00
|
|
|
q.pat_loc Warnings.Unused_match
|
2002-09-30 07:23:00 -07:00
|
|
|
| Upartial ps ->
|
|
|
|
List.iter
|
|
|
|
(fun p ->
|
|
|
|
Location.prerr_warning
|
|
|
|
p.pat_loc Warnings.Unused_pat)
|
|
|
|
ps
|
2006-09-21 07:54:54 -07:00
|
|
|
| Used -> ()
|
2011-06-15 02:26:30 -07:00
|
|
|
with Empty | Not_an_adt | Not_found | NoGuard -> assert false
|
2002-09-30 07:23:00 -07:00
|
|
|
end ;
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-09-25 09:37:04 -07:00
|
|
|
if has_guard act then
|
2002-09-30 07:23:00 -07:00
|
|
|
do_rec pref rem
|
2002-09-25 09:37:04 -07:00
|
|
|
else
|
2006-09-21 07:54:54 -07:00
|
|
|
do_rec ([q]::pref) rem in
|
2002-09-30 07:23:00 -07:00
|
|
|
|
|
|
|
do_rec [] casel
|
2008-06-16 02:08:55 -07:00
|
|
|
|
2008-07-09 06:03:38 -07:00
|
|
|
(*********************************)
|
|
|
|
(* Exported irrefutability tests *)
|
|
|
|
(*********************************)
|
2008-06-16 02:08:55 -07:00
|
|
|
|
|
|
|
let irrefutable pat = le_pat pat omega
|
2008-07-09 06:03:38 -07:00
|
|
|
|
|
|
|
(* An inactive pattern is a pattern whose matching needs only
|
|
|
|
trivial computations (tag/equality tests).
|
|
|
|
Patterns containing (lazy _) subpatterns are active. *)
|
|
|
|
|
|
|
|
let rec inactive pat = match pat with
|
|
|
|
| Tpat_lazy _ ->
|
|
|
|
false
|
|
|
|
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_variant (_, None, _) ->
|
|
|
|
true
|
2012-10-24 05:03:00 -07:00
|
|
|
| Tpat_tuple ps | Tpat_construct (_, _, ps,_) | Tpat_array ps ->
|
2008-07-09 06:03:38 -07:00
|
|
|
List.for_all (fun p -> inactive p.pat_desc) ps
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_alias (p,_,_) | Tpat_variant (_, Some p, _) ->
|
2008-07-09 06:03:38 -07:00
|
|
|
inactive p.pat_desc
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tpat_record (ldps,_) ->
|
2012-10-24 05:03:00 -07:00
|
|
|
List.exists (fun (_, _, p) -> inactive p.pat_desc) ldps
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_or (p,q,_) ->
|
|
|
|
inactive p.pat_desc && inactive q.pat_desc
|
|
|
|
|
|
|
|
(* A `fluid' pattern is both irrefutable and inactive *)
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let fluid pat = irrefutable pat && inactive pat.pat_desc
|
|
|
|
|
|
|
|
|
2010-10-15 23:09:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(********************************)
|
|
|
|
(* Exported exhustiveness check *)
|
|
|
|
(********************************)
|
|
|
|
|
|
|
|
(*
|
|
|
|
Fragile check is performed when required and
|
|
|
|
on exhaustive matches only.
|
|
|
|
*)
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let check_partial_param do_check_partial do_check_fragile loc casel =
|
2010-10-15 23:09:25 -07:00
|
|
|
if Warnings.is_active (Warnings.Partial_match "") then begin
|
|
|
|
let pss = initial_matrix casel in
|
|
|
|
let pss = get_mins le_pats pss in
|
2010-11-08 01:58:09 -08:00
|
|
|
let total = do_check_partial loc casel pss in
|
2010-10-15 23:09:25 -07:00
|
|
|
if
|
2012-07-30 11:04:46 -07:00
|
|
|
total = Total && Warnings.is_active (Warnings.Fragile_match "")
|
2010-10-15 23:09:25 -07:00
|
|
|
then begin
|
2012-07-30 11:04:46 -07:00
|
|
|
do_check_fragile loc casel pss
|
2010-10-15 23:09:25 -07:00
|
|
|
end ;
|
|
|
|
total
|
|
|
|
end else
|
2012-05-30 07:52:37 -07:00
|
|
|
Partial
|
2010-10-15 23:09:25 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let check_partial =
|
|
|
|
check_partial_param
|
|
|
|
do_check_partial_normal
|
2010-11-08 01:58:09 -08:00
|
|
|
do_check_fragile_normal
|
2010-11-07 23:34:09 -08:00
|
|
|
|
|
|
|
let check_partial_gadt pred loc casel =
|
2010-11-08 01:58:09 -08:00
|
|
|
(*ignores GADT constructors *)
|
2010-10-15 23:09:25 -07:00
|
|
|
let first_check = check_partial loc casel in
|
|
|
|
match first_check with
|
|
|
|
| Partial -> Partial
|
2012-05-30 07:52:37 -07:00
|
|
|
| Total ->
|
2010-11-08 01:58:09 -08:00
|
|
|
(* checks for missing GADT constructors *)
|
2013-01-24 20:26:02 -08:00
|
|
|
(* let casel =
|
|
|
|
match casel with [] -> [] | a :: l -> a :: l @ [a] in *)
|
2011-11-24 01:02:48 -08:00
|
|
|
check_partial_param (do_check_partial_gadt pred)
|
|
|
|
do_check_fragile_gadt loc casel
|