X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fcic_notation%2FcicNotationUtil.ml;h=f2c2539cde31f20e5c4a8a7a2c6019b327e189fe;hb=1bcad789810fd37d346e690f18557aeedc6fe08c;hp=740a9c1464af06c1a335c55a03ed09c7df9555cc;hpb=ea7808d83a7d6e03c0e163f0691e268dcd7c2ea4;p=helm.git diff --git a/helm/ocaml/cic_notation/cicNotationUtil.ml b/helm/ocaml/cic_notation/cicNotationUtil.ml index 740a9c146..f2c2539cd 100644 --- a/helm/ocaml/cic_notation/cicNotationUtil.ml +++ b/helm/ocaml/cic_notation/cicNotationUtil.ml @@ -25,13 +25,6 @@ open CicNotationPt - (* TODO ensure that names generated by fresh_var do not clash with user's *) -let fresh_name = - let index = ref ~-1 in - fun () -> - incr index; - "fresh" ^ string_of_int !index - (* let meta_names_of term = *) (* let rec names = ref [] in *) (* let add_name n = *) @@ -100,12 +93,12 @@ let fresh_name = let visit_ast ?(special_k = fun _ -> assert false) k = let rec aux = function - | Appl terms -> Appl (List.map k terms) | Binder (kind, var, body) -> Binder (kind, aux_capture_variable var, k body) | Case (term, indtype, typ, patterns) -> Case (k term, indtype, aux_opt typ, aux_patterns patterns) + | Cast (t1, t2) -> Cast (k t1, k t2) | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2) | LetRec (kind, definitions, term) -> let definitions = @@ -117,13 +110,11 @@ let visit_ast ?(special_k = fun _ -> assert false) k = | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs)) | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs)) | Meta (index, substs) -> Meta (index, List.map aux_opt substs) - | (AttributedTerm _ | Layout _ | Literal _ | Magic _ | Variable _) as t -> special_k t - | (Ident _ | Implicit | Num _ @@ -131,24 +122,16 @@ let visit_ast ?(special_k = fun _ -> assert false) k = | Symbol _ | Uri _ | UserInput) as t -> t - and aux_opt = function | None -> None | Some term -> Some (k term) - and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt - and aux_patterns patterns = List.map aux_pattern patterns - and aux_pattern ((head, vars), term) = ((head, List.map aux_capture_variable vars), k term) - and aux_subst (name, term) = (name, k term) - and aux_substs substs = List.map aux_subst substs - in - aux let visit_layout k = function @@ -161,8 +144,9 @@ let visit_layout k = function | Frac (t1, t2) -> Frac (k t1, k t2) | Sqrt t -> Sqrt (k t) | Root (arg, index) -> Root (k arg, k index) -(* | Break -> Break *) + | Break -> Break | Box (kind, terms) -> Box (kind, List.map k terms) + | Group terms -> Group (List.map k terms) let visit_magic k = function | List0 (t, l) -> List0 (k t, l) @@ -170,7 +154,8 @@ let visit_magic k = function | Opt t -> Opt (k t) | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2) | Default (t1, t2) -> Default (k t1, k t2) - | If (t1, t2) -> If (k t1, k t2) + | If (t1, t2, t3) -> If (k t1, k t2, k t3) + | Fail -> Fail let variables_of_term t = let rec vars = ref [] in @@ -206,6 +191,23 @@ let names_of_term t = in List.map aux (variables_of_term t) +let keywords_of_term t = + let rec keywords = ref [] in + let add_keyword k = keywords := k :: !keywords in + let rec aux = function + | AttributedTerm (_, t) -> aux t + | Layout l -> Layout (visit_layout aux l) + | Literal (`Keyword k) as t -> + add_keyword k; + t + | Literal _ as t -> t + | Magic m -> Magic (visit_magic aux m) + | Variable _ as v -> v + | t -> visit_ast aux t + in + ignore (aux t) ; + !keywords + let rec strip_attributes t = let special_k = function | AttributedTerm (_, term) -> strip_attributes term @@ -276,6 +278,11 @@ let meta_names_of_term term = | Fold (_, t1, _, t2) -> aux t1 ; aux t2 + | If (t1, t2, t3) -> + aux t1 ; + aux t2 ; + aux t3 + | Fail -> () | _ -> assert false in aux term ; @@ -312,6 +319,32 @@ let boxify = function | [ a ] -> a | l -> Layout (Box ((H, false, false), l)) +let unboxify = function + | Layout (Box ((H, false, false), [ a ])) -> a + | l -> l + +let group = function + | [ a ] -> a + | l -> Layout (Group l) + +let ungroup = + let rec aux acc = + function + [] -> List.rev acc + | Layout (Group terms) :: terms' -> aux acc (terms @ terms') + | term :: terms -> aux (term :: acc) terms + in + aux [] + +let dress sauce = + let rec aux = + function + | [] -> [] + | [hd] -> [hd] + | hd :: tl -> hd :: sauce :: aux tl + in + aux + let find_appl_pattern_uris ap = let rec aux acc = function @@ -320,8 +353,35 @@ let find_appl_pattern_uris ap = ignore (List.find (fun uri' -> UriManager.eq uri uri') acc); acc with Not_found -> uri :: acc) + | ImplicitPattern | VarPattern _ -> acc | ApplPattern apl -> List.fold_left aux acc apl in aux [] ap +let rec find_branch = + function + Magic (If (_, Magic Fail, t)) -> find_branch t + | Magic (If (_, t, _)) -> find_branch t + | t -> t + +let cic_name_of_name = function + | CicNotationPt.Ident ("_", None) -> Cic.Anonymous + | CicNotationPt.Ident (name, None) -> Cic.Name name + | _ -> assert false + +let name_of_cic_name = function + | Cic.Name s -> CicNotationPt.Ident (s, None) + | Cic.Anonymous -> CicNotationPt.Ident ("_", None) + +let fresh_index = ref ~-1 + +type notation_id = int + +let fresh_id () = + incr fresh_index; + !fresh_index + + (* TODO ensure that names generated by fresh_var do not clash with user's *) +let fresh_name () = "fresh" ^ string_of_int (fresh_id ()) +