1 (* Copyright (C) 2004, 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/.
26 (* identity_relocation_list_for_metavariable i canonical_context *)
27 (* returns the identity relocation list, which is the list [1 ; ... ; n] *)
28 (* where n = List.length [canonical_context] *)
29 (*CSC: ma mi basta la lunghezza del contesto canonico!!!*)
30 let identity_relocation_list_for_metavariable ?(start = 1) canonical_context =
31 let canonical_context_length = List.length canonical_context in
35 | (n,None::tl) -> None::(aux ((n+1),tl))
36 | (n,_::tl) -> (Some (Cic.Rel n))::(aux ((n+1),tl))
38 aux (start,canonical_context)
40 (* Returns the first meta whose number is above the *)
41 (* number of the higher meta. *)
42 let new_meta metasenv =
47 | None,(n,_,_)::tl -> aux (Some n,tl)
48 | Some m,(n,_,_)::tl -> if n > m then aux (Some n,tl) else aux (Some m,tl)
50 1 + aux (None,metasenv)
52 let mk_implicit metasenv context =
53 let newmeta = new_meta metasenv in
54 let irl = identity_relocation_list_for_metavariable context in
55 ([ newmeta, [], Cic.Sort Cic.Type ;
56 newmeta + 1, context, Cic.Meta (newmeta, []);
57 newmeta + 2, context, Cic.Meta (newmeta + 1,irl) ] @ metasenv,
60 let mk_implicit_type metasenv context =
61 let newmeta = new_meta metasenv in
62 ([ newmeta, [], Cic.Sort Cic.Type ;
63 newmeta + 1, context, Cic.Meta (newmeta, []) ] @metasenv,
66 let mk_implicit_sort metasenv =
67 let newmeta = new_meta metasenv in
68 ([ newmeta, [], Cic.Sort Cic.Type] @ metasenv, newmeta)
70 let n_fresh_metas metasenv context n =
71 if n = 0 then metasenv, []
73 let irl = identity_relocation_list_for_metavariable context in
74 let newmeta = new_meta metasenv in
75 let rec aux newmeta n =
76 if n = 0 then metasenv, []
78 let metasenv', l = aux (newmeta + 3) (n-1) in
79 (newmeta, context, Cic.Sort Cic.Type)::
80 (newmeta + 1, context, Cic.Meta (newmeta, irl))::
81 (newmeta + 2, context, Cic.Meta (newmeta + 1,irl))::metasenv',
82 Cic.Meta(newmeta+2,irl)::l in
85 let fresh_subst metasenv context uris =
86 let irl = identity_relocation_list_for_metavariable context in
87 let newmeta = new_meta metasenv in
88 let rec aux newmeta = function
91 let metasenv', l = aux (newmeta + 3) tl in
92 (newmeta, context, Cic.Sort Cic.Type)::
93 (newmeta + 1, context, Cic.Meta (newmeta, irl))::
94 (newmeta + 2, context, Cic.Meta (newmeta + 1,irl))::metasenv',
95 (uri,Cic.Meta(newmeta+2,irl))::l in
98 let expand_implicits metasenv context term =
99 let rec aux metasenv context = function
100 | (Cic.Rel _) as t -> metasenv, t
101 | (Cic.Sort _) as t -> metasenv, t
102 | Cic.Const (uri, subst) ->
103 let metasenv', subst' = do_subst metasenv context subst in
104 metasenv', Cic.Const (uri, subst')
105 | Cic.Var (uri, subst) ->
106 let metasenv', subst' = do_subst metasenv context subst in
107 metasenv', Cic.Var (uri, subst')
108 | Cic.MutInd (uri, i, subst) ->
109 let metasenv', subst' = do_subst metasenv context subst in
110 metasenv', Cic.MutInd (uri, i, subst')
111 | Cic.MutConstruct (uri, i, j, subst) ->
112 let metasenv', subst' = do_subst metasenv context subst in
113 metasenv', Cic.MutConstruct (uri, i, j, subst')
115 let metasenv', l' = do_local_context metasenv context l in
116 metasenv', Cic.Meta (n, l')
117 | Cic.Implicit (Some `Type) ->
118 let (metasenv', idx) = mk_implicit_type metasenv context in
119 let irl = identity_relocation_list_for_metavariable context in
120 metasenv', Cic.Meta (idx, irl)
121 | Cic.Implicit (Some `Closed) ->
122 let (metasenv', idx) = mk_implicit metasenv [] in
123 metasenv', Cic.Meta (idx, [])
124 | Cic.Implicit None ->
125 let (metasenv', idx) = mk_implicit metasenv context in
126 let irl = identity_relocation_list_for_metavariable context in
127 metasenv', Cic.Meta (idx, irl)
128 | Cic.Cast (te, ty) ->
129 let metasenv', ty' = aux metasenv context ty in
130 let metasenv'', te' = aux metasenv' context te in
131 metasenv'', Cic.Cast (te', ty')
132 | Cic.Prod (name, s, t) ->
133 let metasenv', s' = aux metasenv context s in
135 aux metasenv' (Some (name, Cic.Decl s') :: context) t
137 metasenv'', Cic.Prod (name, s', t')
138 | Cic.Lambda (name, s, t) ->
139 let metasenv', s' = aux metasenv context s in
141 aux metasenv' (Some (name, Cic.Decl s') :: context) t
143 metasenv'', Cic.Lambda (name, s', t')
144 | Cic.LetIn (name, s, t) ->
145 let metasenv', s' = aux metasenv context s in
147 aux metasenv' (Some (name, Cic.Def (s', None)) :: context) t
149 metasenv'', Cic.LetIn (name, s', t')
150 | Cic.Appl l when List.length l > 1 ->
153 (fun term (metasenv, terms) ->
154 let new_metasenv, term = aux metasenv context term in
155 new_metasenv, term :: terms)
158 metasenv', Cic.Appl l'
159 | Cic.Appl _ -> assert false
160 | Cic.MutCase (uri, i, outtype, term, patterns) ->
163 (fun term (metasenv, terms) ->
164 let new_metasenv, term = aux metasenv context term in
165 new_metasenv, term :: terms)
166 (outtype :: term :: patterns) (metasenv, [])
168 let outtype', term', patterns' =
170 | outtype' :: term' :: patterns' -> outtype', term', patterns'
173 metasenv', Cic.MutCase (uri, i, outtype', term', patterns')
174 | Cic.Fix (i, funs) ->
175 let metasenv', types =
177 (fun (name, _, typ, _) (metasenv, types) ->
178 let new_metasenv, new_type = aux metasenv context typ in
179 (new_metasenv, (name, new_type) :: types))
184 (fun (name, t) -> Some (Cic.Name name, Cic.Decl t))
188 let metasenv'', bodies =
190 (fun (_, _, _, body) (metasenv, bodies) ->
191 let new_metasenv, new_body = aux metasenv context' body in
192 (new_metasenv, new_body :: bodies))
195 let rec combine = function
196 | ((name, index, _, _) :: funs_tl),
197 ((_, typ) :: typ_tl),
199 (name, index, typ, body) :: combine (funs_tl, typ_tl, body_tl)
203 let funs' = combine (funs, types, bodies) in
204 metasenv'', Cic.Fix (i, funs')
205 | Cic.CoFix (i, funs) ->
206 let metasenv', types =
208 (fun (name, typ, _) (metasenv, types) ->
209 let new_metasenv, new_type = aux metasenv context typ in
210 (new_metasenv, (name, new_type) :: types))
215 (fun (name, t) -> Some (Cic.Name name, Cic.Decl t))
219 let metasenv'', bodies =
221 (fun (_, _, body) (metasenv, bodies) ->
222 let new_metasenv, new_body = aux metasenv context' body in
223 (new_metasenv, new_body :: bodies))
226 let rec combine = function
227 | ((name, _, _) :: funs_tl),
228 ((_, typ) :: typ_tl),
230 (name, typ, body) :: combine (funs_tl, typ_tl, body_tl)
234 let funs' = combine (funs, types, bodies) in
235 metasenv'', Cic.CoFix (i, funs')
236 and do_subst metasenv context subst =
238 (fun (uri, term) (metasenv, substs) ->
239 let metasenv', term' = aux metasenv context term in
240 (metasenv', (uri, term') :: substs))
242 and do_local_context metasenv context local_context =
244 (fun term (metasenv, local_context) ->
245 let metasenv', term' =
247 | None -> metasenv, None
249 let metasenv', term' = aux metasenv context term in
250 metasenv', Some term'
252 metasenv', term' :: local_context)
253 local_context (metasenv, [])
255 aux metasenv context term