~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to kernel/mod_typing.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(************************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
 
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
 
 
9
(*i $Id: mod_typing.ml 11514 2008-10-28 13:39:02Z soubiran $ i*)
 
10
 
 
11
open Util
 
12
open Names
 
13
open Univ
 
14
open Declarations
 
15
open Entries
 
16
open Environ
 
17
open Term_typing
 
18
open Modops
 
19
open Subtyping
 
20
open Mod_subst
 
21
 
 
22
exception Not_path
 
23
 
 
24
let path_of_mexpr = function
 
25
  | MSEident mp -> mp
 
26
  | _ -> raise Not_path
 
27
 
 
28
let rec list_split_assoc k rev_before = function
 
29
  | [] -> raise Not_found
 
30
  | (k',b)::after when k=k' -> rev_before,b,after
 
31
  | h::tail -> list_split_assoc k (h::rev_before) tail
 
32
 
 
33
let rec list_fold_map2 f e = function 
 
34
  |  []  -> (e,[],[])
 
35
  |  h::t -> 
 
36
       let e',h1',h2' = f e h in
 
37
       let e'',t1',t2' = list_fold_map2 f e' t in
 
38
         e'',h1'::t1',h2'::t2'
 
39
 
 
40
let rec rebuild_mp mp l =
 
41
  match l with
 
42
      []-> mp
 
43
    | i::r -> rebuild_mp (MPdot(mp,i)) r 
 
44
 
 
45
let type_of_struct env b meb = 
 
46
  let rec aux env = function 
 
47
  | SEBfunctor (mp,mtb,body) ->
 
48
      let env = add_module (MPbound mp) (module_body_of_type mtb) env in
 
49
        SEBfunctor(mp,mtb, aux env body)
 
50
  | SEBident mp -> 
 
51
      strengthen env (lookup_modtype mp env).typ_expr mp
 
52
  | SEBapply _ as mtb -> eval_struct env mtb
 
53
  | str -> str
 
54
  in
 
55
    if b then
 
56
      Some (aux env meb)
 
57
    else
 
58
    None
 
59
 
 
60
let rec bounded_str_expr = function
 
61
     | SEBfunctor (mp,mtb,body) -> bounded_str_expr body
 
62
     | SEBident mp -> (check_bound_mp mp)
 
63
     | SEBapply (f,a,_)->(bounded_str_expr f)
 
64
     | _ -> false
 
65
 
 
66
let return_opt_type mp env mtb = 
 
67
  if (check_bound_mp mp) then
 
68
    Some (strengthen env mtb.typ_expr mp)
 
69
  else
 
70
    None
 
71
 
 
72
let rec check_with env mtb with_decl = 
 
73
  match with_decl with
 
74
    | With_Definition (id,_) -> 
 
75
        let cb = check_with_aux_def env mtb with_decl in
 
76
          SEBwith(mtb,With_definition_body(id,cb)),empty_subst
 
77
    | With_Module (id,mp) -> 
 
78
        let cst,sub,typ_opt = check_with_aux_mod env mtb with_decl true in
 
79
          SEBwith(mtb,With_module_body(id,mp,typ_opt,cst)),sub
 
80
 
 
81
and check_with_aux_def env mtb with_decl = 
 
82
  let msid,sig_b = match (eval_struct env mtb) with 
 
83
    | SEBstruct(msid,sig_b) ->
 
84
        msid,sig_b
 
85
    | _ -> error_signature_expected mtb
 
86
  in
 
87
  let id,idl = match with_decl with 
 
88
    | With_Definition (id::idl,_) | With_Module (id::idl,_) -> id,idl
 
89
    | With_Definition ([],_) | With_Module ([],_) -> assert false
 
90
  in
 
91
  let l = label_of_id id in
 
92
    try
 
93
      let rev_before,spec,after = list_split_assoc l [] sig_b in
 
94
      let before = List.rev rev_before in
 
95
      let env' = Modops.add_signature (MPself msid) before env in
 
96
        match with_decl with
 
97
          | With_Definition ([],_) -> assert false
 
98
          | With_Definition ([id],c) -> 
 
99
              let cb = match spec with
 
100
                  SFBconst cb -> cb
 
101
                | _ -> error_not_a_constant l
 
102
              in 
 
103
                begin
 
104
                  match cb.const_body with
 
105
                    | None -> 
 
106
                        let (j,cst1) = Typeops.infer env' c in
 
107
                        let typ = Typeops.type_of_constant_type env' cb.const_type in
 
108
                        let cst2 = Reduction.conv_leq env' j.uj_type typ in
 
109
                        let cst = 
 
110
                          Constraint.union 
 
111
                            (Constraint.union cb.const_constraints cst1)
 
112
                            cst2 in
 
113
                        let body = Some (Declarations.from_val j.uj_val) in
 
114
                        let cb' = {cb with 
 
115
                                     const_body = body;
 
116
                                     const_body_code = Cemitcodes.from_val
 
117
                            (compile_constant_body env' body false false);
 
118
                                     const_constraints = cst} in
 
119
                          cb'
 
120
                    | Some b -> 
 
121
                        let cst1 = Reduction.conv env' c (Declarations.force b) in
 
122
                        let cst = Constraint.union cb.const_constraints cst1 in
 
123
                        let body = Some (Declarations.from_val c) in
 
124
                        let cb' = {cb with 
 
125
                                     const_body = body;
 
126
                                     const_body_code = Cemitcodes.from_val
 
127
                            (compile_constant_body env' body false false);
 
128
                                     const_constraints = cst} in
 
129
                          cb'
 
130
              end
 
131
          | With_Definition (_::_,_) ->
 
132
              let old = match spec with
 
133
                  SFBmodule msb -> msb
 
134
                | _ -> error_not_a_module (string_of_label l)
 
135
              in
 
136
                begin
 
137
                  match old.mod_expr with
 
138
                    | None ->
 
139
                        let new_with_decl = match with_decl with
 
140
                            With_Definition (_,c) -> With_Definition (idl,c)
 
141
                          | With_Module (_,c) -> With_Module (idl,c) in 
 
142
                          check_with_aux_def env' (type_of_mb env old) new_with_decl
 
143
                    | Some msb ->
 
144
                        error_a_generative_module_expected l
 
145
                end
 
146
          | _ -> anomaly "Modtyping:incorrect use of with"
 
147
    with
 
148
        Not_found -> error_no_such_label l
 
149
      | Reduction.NotConvertible -> error_with_incorrect l
 
150
 
 
151
and check_with_aux_mod env mtb with_decl now = 
 
152
  let initmsid,msid,sig_b = match (eval_struct env mtb) with 
 
153
    | SEBstruct(msid,sig_b) ->let msid'=(refresh_msid msid) in
 
154
        msid,msid',(subst_signature_msid msid (MPself(msid')) sig_b)
 
155
    | _ -> error_signature_expected mtb
 
156
  in
 
157
  let id,idl = match with_decl with 
 
158
    | With_Definition (id::idl,_) | With_Module (id::idl,_) -> id,idl
 
159
    | With_Definition ([],_) | With_Module ([],_) -> assert false
 
160
  in
 
161
  let l = label_of_id id in
 
162
    try
 
163
      let rev_before,spec,after = list_split_assoc l [] sig_b in
 
164
      let before = List.rev rev_before in
 
165
      let rec mp_rec = function
 
166
        | [] -> MPself initmsid
 
167
        | i::r -> MPdot(mp_rec r,label_of_id i)
 
168
      in 
 
169
      let env' = Modops.add_signature (MPself msid) before env in
 
170
        match with_decl with
 
171
          | With_Module ([],_) -> assert false
 
172
          | With_Module ([id], mp) ->
 
173
              let old,alias = match spec with
 
174
                  SFBmodule msb -> Some msb,None
 
175
                | SFBalias (mp',_,cst) -> None,Some (mp',cst)
 
176
                | _ -> error_not_a_module (string_of_label l)
 
177
              in
 
178
              let mtb' = lookup_modtype mp env' in
 
179
              let cst =
 
180
                match old,alias with
 
181
                    Some msb,None ->
 
182
                      begin
 
183
                        try Constraint.union 
 
184
                          (check_subtypes env' mtb' (module_type_of_module None msb))
 
185
                          msb.mod_constraints
 
186
                        with Failure _ -> error_with_incorrect (label_of_id id)
 
187
                      end
 
188
                  | None,Some (mp',None) ->
 
189
                      check_modpath_equiv env' mp mp';
 
190
                      Constraint.empty
 
191
                  | None,Some (mp',Some cst) ->
 
192
                      check_modpath_equiv env' mp mp';
 
193
                      cst
 
194
                  | _,_ ->
 
195
                      anomaly "Mod_typing:no implementation and no alias"
 
196
              in
 
197
                if now then 
 
198
                  let mp' = scrape_alias mp env' in
 
199
                  let _,sub = Modops.update_subst env' (module_body_of_type mtb') mp' in
 
200
                  let up_subst = update_subst sub (map_mp (mp_rec [id]) mp') in
 
201
                    cst, (join (map_mp (mp_rec [id]) mp') up_subst),(return_opt_type mp env' mtb')
 
202
                else
 
203
                cst,empty_subst,(return_opt_type mp env' mtb')
 
204
        | With_Module (_::_,mp) -> 
 
205
            let old,alias = match spec with
 
206
                SFBmodule msb -> Some msb, None
 
207
              | SFBalias (mpold,typ_opt,cst)->None, Some mpold
 
208
              | _ -> error_not_a_module (string_of_label l)
 
209
            in
 
210
              begin
 
211
                if alias = None then
 
212
                  let old = Option.get old in
 
213
                    match old.mod_expr with
 
214
                        None ->
 
215
                          let new_with_decl = match with_decl with
 
216
                              With_Definition (_,c) -> 
 
217
                                With_Definition (idl,c)
 
218
                            | With_Module (_,c) -> With_Module (idl,c) in
 
219
                          let cst,_,typ_opt =
 
220
                            check_with_aux_mod env' 
 
221
                              (type_of_mb env' old) new_with_decl false in
 
222
                            if now then 
 
223
                            let mtb' = lookup_modtype mp env' in
 
224
                            let mp' = scrape_alias mp env' in
 
225
                            let _,sub = Modops.update_subst env' (module_body_of_type mtb') mp' in
 
226
                            let up_subst = update_subst 
 
227
                              sub (map_mp (mp_rec (List.rev (id::idl))) mp') in
 
228
                              cst, 
 
229
                          (join (map_mp (mp_rec (List.rev (id::idl))) mp') up_subst),
 
230
                          typ_opt
 
231
                            else
 
232
                              cst,empty_subst,typ_opt
 
233
                      | Some msb ->
 
234
                          error_a_generative_module_expected l
 
235
                else
 
236
                  let mpold =  Option.get alias in 
 
237
                  let mpnew = rebuild_mp mpold (List.map label_of_id idl) in
 
238
                    check_modpath_equiv env' mpnew mp;
 
239
                    let mtb' = lookup_modtype mp env' in
 
240
                      Constraint.empty,empty_subst,(return_opt_type mp env' mtb')
 
241
              end
 
242
        | _ -> anomaly "Modtyping:incorrect use of with"
 
243
    with
 
244
        Not_found -> error_no_such_label l
 
245
      | Reduction.NotConvertible -> error_with_incorrect l
 
246
          
 
247
and translate_module env me =
 
248
  match me.mod_entry_expr, me.mod_entry_type with
 
249
    | None, None -> 
 
250
        anomaly "Mod_typing.translate_module: empty type and expr in module entry"
 
251
    | None, Some mte -> 
 
252
        let mtb,sub = translate_struct_entry env mte in
 
253
        { mod_expr = None;
 
254
          mod_type = Some mtb;
 
255
          mod_alias = sub;
 
256
          mod_constraints = Constraint.empty; 
 
257
          mod_retroknowledge = []}
 
258
    | Some mexpr, _ -> 
 
259
        let meb,sub1 = translate_struct_entry env mexpr in
 
260
        let mod_typ,sub,cst =
 
261
          match me.mod_entry_type with
 
262
            | None ->  
 
263
                (type_of_struct env (bounded_str_expr meb) meb)
 
264
                  ,sub1,Constraint.empty
 
265
            | Some mte -> 
 
266
                let mtb2,sub2 = translate_struct_entry env mte in
 
267
                let cst = check_subtypes env
 
268
                  {typ_expr = meb;
 
269
                   typ_strength = None;
 
270
                   typ_alias = sub1;}
 
271
                  {typ_expr = mtb2;
 
272
                   typ_strength = None;
 
273
                   typ_alias = sub2;}
 
274
                in
 
275
                  Some mtb2,sub2,cst
 
276
        in
 
277
          { mod_type = mod_typ;
 
278
            mod_expr = Some meb;
 
279
            mod_constraints = cst;
 
280
            mod_alias = sub;
 
281
            mod_retroknowledge = []} (* spiwack: not so sure about that. It may
 
282
                                        cause a bug when closing nested modules.
 
283
                                        If it does, I don't really know how to
 
284
                                        fix the bug.*)
 
285
 
 
286
 
 
287
and translate_struct_entry env mse = match mse with
 
288
  | MSEident mp ->
 
289
      let mtb = lookup_modtype mp env in 
 
290
      SEBident mp,mtb.typ_alias
 
291
  | MSEfunctor (arg_id, arg_e, body_expr) ->
 
292
      let arg_b,sub = translate_struct_entry env arg_e in
 
293
      let mtb =
 
294
        {typ_expr = arg_b;
 
295
         typ_strength = None;
 
296
         typ_alias = sub} in
 
297
      let env' = add_module (MPbound arg_id) (module_body_of_type mtb) env in
 
298
      let body_b,sub = translate_struct_entry env' body_expr in
 
299
        SEBfunctor (arg_id, mtb, body_b),sub
 
300
  | MSEapply (fexpr,mexpr) ->
 
301
      let feb,sub1 = translate_struct_entry env fexpr in
 
302
      let feb'= eval_struct env feb
 
303
      in
 
304
      let farg_id, farg_b, fbody_b = destr_functor env feb' in
 
305
      let mtb,mp = 
 
306
        try
 
307
          let mp = scrape_alias (path_of_mexpr mexpr) env in
 
308
          lookup_modtype mp env,mp
 
309
        with
 
310
          | Not_path -> error_application_to_not_path mexpr
 
311
              (* place for nondep_supertype *) in
 
312
      let meb,sub2= translate_struct_entry env (MSEident mp) in
 
313
        if sub1 = empty_subst then 
 
314
          let cst = check_subtypes env mtb farg_b in
 
315
            SEBapply(feb,meb,cst),sub1
 
316
        else
 
317
          let sub2 = match eval_struct env (SEBident mp) with
 
318
            | SEBstruct (msid,sign) -> 
 
319
                join_alias 
 
320
                  (subst_key (map_msid msid mp) sub2)
 
321
                  (map_msid msid mp)
 
322
            | _ -> sub2 in
 
323
          let sub3 = join_alias sub1 (map_mbid farg_id mp None) in
 
324
          let sub4 = update_subst sub2 sub3 in
 
325
          let cst = check_subtypes env mtb farg_b in
 
326
            SEBapply(feb,meb,cst),(join sub3 sub4)
 
327
  | MSEwith(mte, with_decl) ->
 
328
        let mtb,sub1 = translate_struct_entry env mte in
 
329
        let mtb',sub2 = check_with env mtb with_decl in
 
330
          mtb',join sub1 sub2
 
331
            
 
332
 
 
333
let rec add_struct_expr_constraints env = function
 
334
  | SEBident _ -> env
 
335
 
 
336
  | SEBfunctor (_,mtb,meb) -> 
 
337
      add_struct_expr_constraints 
 
338
        (add_modtype_constraints env mtb) meb
 
339
 
 
340
  | SEBstruct (_,structure_body) ->
 
341
      List.fold_left 
 
342
        (fun env (l,item) -> add_struct_elem_constraints env item)
 
343
        env
 
344
        structure_body
 
345
 
 
346
  | SEBapply (meb1,meb2,cst) ->
 
347
      Environ.add_constraints cst 
 
348
        (add_struct_expr_constraints 
 
349
          (add_struct_expr_constraints env meb1) 
 
350
          meb2)
 
351
  | SEBwith(meb,With_definition_body(_,cb))->
 
352
      Environ.add_constraints cb.const_constraints
 
353
        (add_struct_expr_constraints env meb)
 
354
  | SEBwith(meb,With_module_body(_,_,_,cst))->
 
355
      Environ.add_constraints cst
 
356
        (add_struct_expr_constraints env meb)   
 
357
                
 
358
and add_struct_elem_constraints env = function 
 
359
  | SFBconst cb -> Environ.add_constraints cb.const_constraints env
 
360
  | SFBmind mib -> Environ.add_constraints mib.mind_constraints env
 
361
  | SFBmodule mb -> add_module_constraints env mb
 
362
  | SFBalias (mp,_,Some cst) -> Environ.add_constraints cst env
 
363
  | SFBalias (mp,_,None) -> env
 
364
  | SFBmodtype mtb -> add_modtype_constraints env mtb
 
365
 
 
366
and add_module_constraints env mb = 
 
367
  let env = match mb.mod_expr with
 
368
    | None -> env
 
369
    | Some meb -> add_struct_expr_constraints env meb
 
370
  in
 
371
  let env = match mb.mod_type with
 
372
    | None -> env
 
373
    | Some mtb -> 
 
374
        add_struct_expr_constraints env mtb
 
375
  in
 
376
    Environ.add_constraints mb.mod_constraints env
 
377
 
 
378
and add_modtype_constraints env mtb = 
 
379
  add_struct_expr_constraints env mtb.typ_expr
 
380
      
 
381
 
 
382
let rec struct_expr_constraints cst = function
 
383
  | SEBident _ -> cst
 
384
 
 
385
  | SEBfunctor (_,mtb,meb) -> 
 
386
      struct_expr_constraints 
 
387
        (modtype_constraints cst mtb) meb
 
388
 
 
389
  | SEBstruct (_,structure_body) ->
 
390
      List.fold_left 
 
391
        (fun cst (l,item) -> struct_elem_constraints cst item)
 
392
        cst
 
393
        structure_body
 
394
 
 
395
  | SEBapply (meb1,meb2,cst1) ->
 
396
      struct_expr_constraints 
 
397
        (struct_expr_constraints (Univ.Constraint.union cst1 cst) meb1)
 
398
        meb2
 
399
  | SEBwith(meb,With_definition_body(_,cb))->
 
400
      struct_expr_constraints
 
401
        (Univ.Constraint.union cb.const_constraints cst) meb
 
402
  | SEBwith(meb,With_module_body(_,_,_,cst1))->
 
403
      struct_expr_constraints (Univ.Constraint.union cst1 cst) meb      
 
404
                
 
405
and struct_elem_constraints cst = function 
 
406
  | SFBconst cb -> cst
 
407
  | SFBmind mib -> cst
 
408
  | SFBmodule mb -> module_constraints cst mb
 
409
  | SFBalias (mp,_,Some cst1) -> Univ.Constraint.union cst1 cst
 
410
  | SFBalias (mp,_,None) -> cst
 
411
  | SFBmodtype mtb -> modtype_constraints cst mtb
 
412
 
 
413
and module_constraints cst mb = 
 
414
  let cst = match mb.mod_expr with
 
415
    | None -> cst
 
416
    | Some meb -> struct_expr_constraints cst meb in
 
417
  let cst = match mb.mod_type with
 
418
    | None -> cst
 
419
    | Some mtb -> struct_expr_constraints cst mtb in
 
420
  Univ.Constraint.union mb.mod_constraints cst
 
421
 
 
422
and modtype_constraints cst mtb = 
 
423
  struct_expr_constraints cst mtb.typ_expr
 
424
      
 
425
 
 
426
let struct_expr_constraints = struct_expr_constraints Univ.Constraint.empty
 
427
let module_constraints = module_constraints Univ.Constraint.empty