1 (* Copyright (C) 2003-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
27 module E = CicEnvironment
29 module TC = CicTypeChecker
31 module UM = UriManager
32 module Rd = CicReduction
34 module P = ProceduralPreprocess
35 module T = ProceduralTypes
36 module M = ProceduralMode
38 (* helpers ******************************************************************)
40 let cic = D.deannotate_term
42 let rec list_sub start length = function
43 | _ :: tl when start > 0 -> list_sub (pred start) length tl
44 | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
47 (* proof construction *******************************************************)
50 let rec lift_xns k (uri, t) = uri, lift_term k t
51 and lift_ms k = function
53 | Some t -> Some (lift_term k t)
54 and lift_fix len k (id, name, i, ty, bo) =
55 id, name, i, lift_term k ty, lift_term (k + len) bo
56 and lift_cofix len k (id, name, ty, bo) =
57 id, name, lift_term k ty, lift_term (k + len) bo
58 and lift_term k = function
60 | C.AImplicit _ as t -> t
61 | C.ARel (id, rid, m, b) as t ->
63 if m + n > 0 then C.ARel (id, rid, m + n, b) else
65 | C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
66 | C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
67 | C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
68 | C.AMutConstruct (id, uri, tyno, consno, xnss) -> C.AMutConstruct (id, uri,tyno,consno, List.map (lift_xns k) xnss)
69 | C.AMeta (id, i, mss) -> C.AMeta(id, i, List.map (lift_ms k) mss)
70 | C.AAppl (id, ts) -> C.AAppl (id, List.map (lift_term k) ts)
71 | C.ACast (id, te, ty) -> C.ACast (id, lift_term k te, lift_term k ty)
72 | C.AMutCase (id, sp, i, outty, t, pl) -> C.AMutCase (id, sp, i, lift_term k outty, lift_term k t, List.map (lift_term k) pl)
73 | C.AProd (id, n, s, t) -> C.AProd (id, n, lift_term k s, lift_term (succ k) t)
74 | C.ALambda (id, n, s, t) -> C.ALambda (id, n, lift_term k s, lift_term (succ k) t)
75 | C.ALetIn (id, n, s, t) -> C.ALetIn (id, n, lift_term k s, lift_term (succ k) t)
76 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (lift_fix (List.length fl) k) fl)
77 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
82 let rec aux k n = function
83 | C.AImplicit (_, None) as t -> t
84 | C.ALambda (id, s, v, t) when k > 0 ->
85 C.ALambda (id, s, v, aux (pred k) n t)
86 | C.ALambda (_, _, _, t) when n > 0 ->
87 aux 0 (pred n) (lift 1 (-1) t)
89 Printf.eprintf "CLEAR: %u %s\n" n (CicPp.ppterm (cic t));
95 let hole id = C.AImplicit (id, Some `Hole)
97 let meta id = C.AImplicit (id, None)
99 let anon = C.Anonymous
104 | C.AImplicit (_, None) when b -> b
107 List.fold_left map true
109 let rec gen_fix len k (id, name, i, ty, bo) =
110 id, name, i, gen_term k ty, gen_term (k + len) bo
111 and gen_cofix len k (id, name, ty, bo) =
112 id, name, gen_term k ty, gen_term (k + len) bo
113 and gen_term k = function
115 | C.AImplicit (id, _)
116 | C.AConst (id, _, _)
118 | C.AMutInd (id, _, _, _)
119 | C.AMutConstruct (id, _, _, _, _)
120 | C.AMeta (id, _, _) -> meta id
121 | C.ARel (id, _, m, _) ->
122 if m = succ (k - n) then hole id else meta id
123 | C.AAppl (id, ts) ->
124 let ts = List.map (gen_term k) ts in
125 if is_meta ts then meta id else C.AAppl (id, ts)
126 | C.ACast (id, te, ty) ->
127 let te, ty = gen_term k te, gen_term k ty in
128 if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
129 | C.AMutCase (id, sp, i, outty, t, pl) ->
130 let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
131 if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
132 | C.AProd (id, _, s, t) ->
133 let s, t = gen_term k s, gen_term (succ k) t in
134 if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
135 | C.ALambda (id, _, s, t) ->
136 let s, t = gen_term k s, gen_term (succ k) t in
137 if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
138 | C.ALetIn (id, _, s, t) ->
139 let s, t = gen_term k s, gen_term (succ k) t in
140 if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, t)
141 | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
142 | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
146 let mk_pattern rpsno predicate =
147 let body = generalize rpsno predicate in
148 clear_absts 0 rpsno body