2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
14 (* let pp m = prerr_endline (Lazy.force m);;*)
21 "z" ^ string_of_int !i
25 let id = if id = "_" then fresh_name () else id in
26 CicNotationPt.Ident (id,None)
29 let rec split_arity ~subst context te =
30 match NCicReduction.whd ~subst context te with
31 | NCic.Prod (name,so,ta) ->
32 split_arity ~subst ((name, (NCic.Decl so))::context) ta
40 | l -> CicNotationPt.Appl l
43 let rec mk_prods l t =
46 | hd::tl -> CicNotationPt.Binder (`Forall, (mk_id hd, None), mk_prods tl t)
49 let rec mk_lambdas l t =
52 | hd::tl -> CicNotationPt.Binder (`Lambda, (mk_id hd, None), mk_lambdas tl t)
55 let rec mk_arrows xs ys selection target =
56 match selection,xs,ys with
58 | false :: l,x::xs,y::ys -> mk_arrows xs ys l target
59 | true :: l,x::xs,y::ys ->
60 CicNotationPt.Binder (`Forall, (mk_id "_", Some (mk_appl [mk_id "eq" ; CicNotationPt.Implicit `JustOne;x;y])),
61 mk_arrows xs ys l target)
62 | _ -> raise (Invalid_argument "ninverter: the selection doesn't match the arity of the specified inductive type")
65 let subst_metasenv_and_fix_names status =
66 let u,h,metasenv, subst,o = status#obj in
68 NCicUntrusted.map_obj_kind ~skip_body:true
69 (NCicUntrusted.apply_subst subst []) o
71 status#set_obj(u,h,NCicUntrusted.apply_subst_metasenv subst metasenv,subst,o)
74 let mk_inverter name it leftno ?selection outsort status baseuri =
75 pp (lazy ("leftno = " ^ string_of_int leftno));
76 let _,ind_name,ty,cl = it in
77 pp (lazy ("arity: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] ty));
78 let ncons = List.length cl in
79 (**)let params,ty = NCicReduction.split_prods ~subst:[] [] leftno ty in
80 let params = List.rev_map (function name,_ -> mk_id name) params in
81 pp (lazy ("lunghezza params = " ^ string_of_int (List.length params)));(**)
82 let args,sort= split_arity ~subst:[] [] ty in
83 pp (lazy ("arity sort: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:args sort));
84 (**)let args = List.rev_map (function name,_ -> mk_id name) args in
85 pp (lazy ("lunghezza args = " ^ string_of_int (List.length args)));(**)
86 let nparams = List.length args in
88 pp (lazy ("nparams = " ^ string_of_int nparams));
90 then raise (Failure "inverter: the type must have at least one right parameter")
92 let xs = List.map (fun n -> "x" ^ (string_of_int n)) (HExtlib.list_seq 1 (leftno+nparams+1)) in
93 pp (lazy ("lunghezza xs = " ^ string_of_int (List.length xs)));
94 let ls, rs = HExtlib.split_nth leftno xs in
95 pp (lazy ("lunghezza ls = " ^ string_of_int (List.length ls)));
96 pp (lazy ("lunghezza rs = " ^ string_of_int (List.length rs)));
97 let ys = List.map (fun n -> "y" ^ (string_of_int n)) (HExtlib.list_seq (leftno+1) (leftno+nparams+1)) in
99 let _id_xs = List.map mk_id xs in
100 let id_ls = List.map mk_id ls in
101 let id_rs = List.map mk_id rs in
102 let id_ys = List.map mk_id ys in
104 (* pseudocode let t = Lambda y1 ... yr. xs_ = ys_ -> pred *)
106 (* check: assuming we have more than one right parameter *)
108 let pred = mk_appl ((mk_id "P")::id_ys) in
110 let selection = match selection with
111 None -> HExtlib.mk_list true (List.length ys)
114 let prods = mk_arrows id_rs id_ys selection pred in
116 let lambdas = mk_lambdas (ys@["p"]) prods in
119 let rec hypaux k = function
121 | n -> ("H" ^ string_of_int k) :: hypaux (k+1) (n-1)
124 pp (lazy ("lunghezza ys = " ^ string_of_int (List.length ys)));
126 let outsort, suffix = NCicElim.ast_of_sort outsort in
127 let theorem = mk_prods xs
128 (CicNotationPt.Binder (`Forall, (mk_id "P", Some (mk_prods (HExtlib.mk_list "_" (List.length ys)) (CicNotationPt.Sort outsort))),
129 mk_prods hyplist (CicNotationPt.Binder (`Forall, (mk_id "Hterm", (*Some (mk_appl (List.map mk_id (ind_name::xs)))) *)
130 Some (CicNotationPt.Implicit `JustOne)),
131 mk_appl (mk_id "P"::id_rs)))))
133 let t = mk_appl ( [mk_id (ind_name ^ "_" ^ suffix)]@ id_ls @ [lambdas] @
134 List.map mk_id hyplist @
135 HExtlib.mk_list (CicNotationPt.Implicit `JustOne) (List.length ys) @ [mk_id "Hterm"] ) in
136 let status, theorem = GrafiteDisambiguate.disambiguate_nobj status ~baseuri
137 (baseuri ^ name ^ ".def",
138 0,CicNotationPt.Theorem (`Theorem,name,theorem,Some
139 (CicNotationPt.Implicit (`Tagged "inv")),`InversionPrinciple)) in
140 let uri,height,nmenv,nsubst,nobj = theorem in
141 let ninitial_stack = Continuationals.Stack.of_nmetasenv nmenv in
142 let status = status#set_obj theorem in
143 let status = status#set_stack ninitial_stack in
144 let status = subst_metasenv_and_fix_names status in
147 let rs = List.map (fun x -> mk_id x) rs in
148 mk_arrows rs rs selection (mk_appl (mk_id "P"::rs)) in
150 let cut = mk_appl [CicNotationPt.Binder (`Lambda, (mk_id "Hcut", Some cut_theorem),
152 CicNotationPt.Implicit (`Tagged "end"));
153 CicNotationPt.Implicit (`Tagged "cut")] in
154 let intros = List.map (fun x -> pp (lazy x); NTactics.intro_tac x) (xs@["P"]@hyplist@["Hterm"]) in
155 let status = NTactics.block_tac
156 (NTactics.branch_tac ::
157 NTactics.case_tac "inv" ::
159 [NTactics.apply_tac ("",0,cut);
161 NTactics.case_tac "end";
162 NTactics.apply_tac ("",0,mk_id "Hcut");
163 NTactics.apply_tac ("",0,mk_id "refl");
165 (*NTactics.case_tac "cut";*)
166 NTactics.apply_tac ("",0,t);
169 NTactics.skip_tac])) status in