2 (* identity_relocation_list_for_metavariable i canonical_context *)
3 (* returns the identity relocation list, which is the list [1 ; ... ; n] *)
4 (* where n = List.length [canonical_context] *)
5 (*CSC: ma mi basta la lunghezza del contesto canonico!!!*)
6 let identity_relocation_list_for_metavariable ?(start = 1) canonical_context =
7 let canonical_context_length = List.length canonical_context in
11 | (n,None::tl) -> None::(aux ((n+1),tl))
12 | (n,_::tl) -> (Some (Cic.Rel n))::(aux ((n+1),tl))
14 aux (start,canonical_context)
16 (* Returns the first meta whose number is above the *)
17 (* number of the higher meta. *)
18 let new_meta metasenv =
23 | None,(n,_,_)::tl -> aux (Some n,tl)
24 | Some m,(n,_,_)::tl -> if n > m then aux (Some n,tl) else aux (Some m,tl)
26 1 + aux (None,metasenv)
28 let mk_implicit metasenv context =
29 let newmeta = new_meta metasenv in
30 let irl = identity_relocation_list_for_metavariable context in
31 ([ newmeta, context, Cic.Sort Cic.Type ;
32 newmeta + 1, context, Cic.Meta (newmeta, irl);
33 newmeta + 2, context, Cic.Meta (newmeta + 1,irl) ] @ metasenv,
36 let n_fresh_metas metasenv context n =
37 if n = 0 then metasenv, []
39 let irl = identity_relocation_list_for_metavariable context in
40 let newmeta = new_meta metasenv in
41 let rec aux newmeta n =
42 if n = 0 then metasenv, []
44 let metasenv', l = aux (newmeta + 3) (n-1) in
45 (newmeta, context, Cic.Sort Cic.Type)::
46 (newmeta + 1, context, Cic.Meta (newmeta, irl))::
47 (newmeta + 2, context, Cic.Meta (newmeta + 1,irl))::metasenv',
48 Cic.Meta(newmeta+2,irl)::l in
51 let fresh_subst metasenv context uris =
52 let irl = identity_relocation_list_for_metavariable context in
53 let newmeta = new_meta metasenv in
54 let rec aux newmeta = function
57 let metasenv', l = aux (newmeta + 3) tl in
58 (newmeta, context, Cic.Sort Cic.Type)::
59 (newmeta + 1, context, Cic.Meta (newmeta, irl))::
60 (newmeta + 2, context, Cic.Meta (newmeta + 1,irl))::metasenv',
61 (uri,Cic.Meta(newmeta+2,irl))::l in
64 let mk_implicit_type metasenv context =
65 let newmeta = new_meta metasenv in
66 let irl = identity_relocation_list_for_metavariable context in
67 ([ newmeta, context, Cic.Sort Cic.Type ;
68 newmeta + 1, context, Cic.Meta (newmeta, irl) ] @metasenv,
71 let expand_implicits metasenv context term =
72 let rec aux metasenv context = function
73 | (Cic.Rel _) as t -> metasenv, t
74 | (Cic.Sort _) as t -> metasenv, t
75 | Cic.Const (uri, subst) ->
76 let metasenv', subst' = do_subst metasenv context subst in
77 metasenv', Cic.Const (uri, subst')
78 | Cic.Var (uri, subst) ->
79 let metasenv', subst' = do_subst metasenv context subst in
80 metasenv', Cic.Var (uri, subst')
81 | Cic.MutInd (uri, i, subst) ->
82 let metasenv', subst' = do_subst metasenv context subst in
83 metasenv', Cic.MutInd (uri, i, subst')
84 | Cic.MutConstruct (uri, i, j, subst) ->
85 let metasenv', subst' = do_subst metasenv context subst in
86 metasenv', Cic.MutConstruct (uri, i, j, subst')
88 let metasenv', l' = do_local_context metasenv context l in
89 metasenv', Cic.Meta (n, l')
91 let (metasenv', idx) = mk_implicit metasenv context in
92 let irl = identity_relocation_list_for_metavariable context in
93 metasenv', Cic.Meta (idx, irl)
94 | Cic.Cast (te, ty) ->
95 let metasenv', ty' = aux metasenv context ty in
96 let metasenv'', te' = aux metasenv' context te in
97 metasenv'', Cic.Cast (te', ty')
98 | Cic.Prod (name, s, t) ->
99 let metasenv', s' = aux metasenv context s in
101 aux metasenv' (Some (name, Cic.Decl s') :: context) t
103 metasenv'', Cic.Prod (name, s', t')
104 | Cic.Lambda (name, s, t) ->
105 let metasenv', s' = aux metasenv context s in
107 aux metasenv' (Some (name, Cic.Decl s') :: context) t
109 metasenv'', Cic.Lambda (name, s', t')
110 | Cic.LetIn (name, s, t) ->
111 let metasenv', s' = aux metasenv context s in
113 aux metasenv' (Some (name, Cic.Def (s', None)) :: context) t
115 metasenv'', Cic.LetIn (name, s', t')
116 | Cic.Appl l when List.length l > 1 ->
119 (fun term (metasenv, terms) ->
120 let new_metasenv, term = aux metasenv context term in
121 new_metasenv, term :: terms)
124 metasenv', Cic.Appl l'
125 | Cic.Appl _ -> assert false
126 | Cic.MutCase (uri, i, outtype, term, patterns) ->
129 (fun term (metasenv, terms) ->
130 let new_metasenv, term = aux metasenv context term in
131 new_metasenv, term :: terms)
132 (outtype :: term :: patterns) (metasenv, [])
134 let outtype', term', patterns' =
136 | outtype' :: term' :: patterns' -> outtype', term', patterns'
139 metasenv', Cic.MutCase (uri, i, outtype', term', patterns')
140 | Cic.Fix (i, funs) ->
141 let metasenv', types =
143 (fun (name, _, typ, _) (metasenv, types) ->
144 let new_metasenv, new_type = aux metasenv context typ in
145 (new_metasenv, (name, new_type) :: types))
150 (fun (name, t) -> Some (Cic.Name name, Cic.Decl t))
154 let metasenv'', bodies =
156 (fun (_, _, _, body) (metasenv, bodies) ->
157 let new_metasenv, new_body = aux metasenv context' body in
158 (new_metasenv, new_body :: bodies))
161 let rec combine = function
162 | ((name, index, _, _) :: funs_tl),
163 ((_, typ) :: typ_tl),
165 (name, index, typ, body) :: combine (funs_tl, typ_tl, body_tl)
169 let funs' = combine (funs, types, bodies) in
170 metasenv'', Cic.Fix (i, funs')
171 | Cic.CoFix (i, funs) ->
172 let metasenv', types =
174 (fun (name, typ, _) (metasenv, types) ->
175 let new_metasenv, new_type = aux metasenv context typ in
176 (new_metasenv, (name, new_type) :: types))
181 (fun (name, t) -> Some (Cic.Name name, Cic.Decl t))
185 let metasenv'', bodies =
187 (fun (_, _, body) (metasenv, bodies) ->
188 let new_metasenv, new_body = aux metasenv context' body in
189 (new_metasenv, new_body :: bodies))
192 let rec combine = function
193 | ((name, _, _) :: funs_tl),
194 ((_, typ) :: typ_tl),
196 (name, typ, body) :: combine (funs_tl, typ_tl, body_tl)
200 let funs' = combine (funs, types, bodies) in
201 metasenv'', Cic.CoFix (i, funs')
202 and do_subst metasenv context subst =
204 (fun (uri, term) (metasenv, substs) ->
205 let metasenv', term' = aux metasenv context term in
206 (metasenv', (uri, term') :: substs))
208 and do_local_context metasenv context local_context =
210 (fun term (metasenv, local_context) ->
211 let metasenv', term' =
213 | None -> metasenv, None
215 let metasenv', term' = aux metasenv context term in
216 metasenv', Some term'
218 metasenv', term' :: local_context)
219 local_context (metasenv, [])
221 aux metasenv context term