| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Declarations |
| 14 |
open Declareops |
| 15 |
open Mod_subst |
| 16 |
|
| 17 |
type (_, 'v) when_mod_body = |
| 18 |
| ModBodyVal : 'v -> (mod_body, 'v) when_mod_body |
| 19 |
| ModTypeNul : (mod_type, 'v) when_mod_body |
| 20 |
|
| 21 |
type structure_field_body = |
| 22 |
(module_body, module_type_body) Declarations.structure_field_body |
| 23 |
|
| 24 |
and structure_body = |
| 25 |
(module_body, module_type_body) Declarations.structure_body |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
and module_signature = (module_type_body,structure_body) functorize |
| 30 |
|
| 31 |
and module_implementation = |
| 32 |
| Abstract |
| 33 |
| Algebraic of module_expression |
| 34 |
| Struct of delta_resolver * structure_body |
| 35 |
| FullStruct |
| 36 |
|
| 37 |
and 'a generic_module_body = |
| 38 |
{ mod_expr : ('a, module_implementation) when_mod_body; |
| 39 |
mod_type : module_signature; |
| 40 |
mod_type_alg : module_expression option; |
| 41 |
mod_delta : Mod_subst.delta_resolver; |
| 42 |
|
| 43 |
mod_retroknowledge : ('a, Retroknowledge.action list) when_mod_body } |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
and module_body = mod_body generic_module_body |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
and module_type_body = mod_type generic_module_body |
| 61 |
|
| 62 |
type 'a module_retroknowledge = ('a, Retroknowledge.action list) when_mod_body |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
let make_module_body typ delta retro = { |
| 77 |
mod_expr = ModBodyVal FullStruct; |
| 78 |
mod_type = typ; |
| 79 |
mod_type_alg = None; |
| 80 |
mod_delta = delta; |
| 81 |
mod_retroknowledge = ModBodyVal retro; |
| 82 |
} |
| 83 |
|
| 84 |
let make_module_type typ delta = { |
| 85 |
mod_expr = ModTypeNul; |
| 86 |
mod_type = typ; |
| 87 |
mod_type_alg = None; |
| 88 |
mod_delta = delta; |
| 89 |
mod_retroknowledge = ModTypeNul; |
| 90 |
} |
| 91 |
|
| 92 |
let strengthen_module_body ~src typ delta mb = |
| 93 |
{ mb with |
| 94 |
mod_expr = ModBodyVal (Algebraic (MENoFunctor (MEident src))); |
| 95 |
mod_type = typ; |
| 96 |
mod_delta = delta; } |
| 97 |
|
| 98 |
let strengthen_module_type struc delta mtb = |
| 99 |
{ mtb with |
| 100 |
mod_type = NoFunctor struc; |
| 101 |
mod_delta = delta } |
| 102 |
|
| 103 |
let replace_module_body struc delta mb = |
| 104 |
|
| 105 |
let () = match mb.mod_expr with ModBodyVal Abstract -> () | _ -> assert false in |
| 106 |
let () = match mb.mod_type with NoFunctor _ -> () | MoreFunctor _ -> assert false in |
| 107 |
{ mb with |
| 108 |
mod_type = NoFunctor struc; |
| 109 |
mod_type_alg = None; |
| 110 |
mod_delta = delta } |
| 111 |
|
| 112 |
let module_type_of_module mb = |
| 113 |
{ mb with mod_expr = ModTypeNul; mod_type_alg = None; |
| 114 |
mod_retroknowledge = ModTypeNul; } |
| 115 |
|
| 116 |
let module_body_of_type mtb = |
| 117 |
{ mtb with mod_expr = ModBodyVal Abstract; |
| 118 |
mod_retroknowledge = ModBodyVal []; } |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
let set_implementation e mb = |
| 123 |
{ mb with mod_expr = ModBodyVal e } |
| 124 |
|
| 125 |
let set_algebraic_type mb alg = |
| 126 |
{ mb with mod_type_alg = Some alg } |
| 127 |
|
| 128 |
let set_retroknowledge mb rk = |
| 129 |
{ mb with mod_retroknowledge = ModBodyVal rk } |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
let mod_expr { mod_expr = ModBodyVal v; _ } = v |
| 134 |
let mod_type m = m.mod_type |
| 135 |
let mod_type_alg m = m.mod_type_alg |
| 136 |
let mod_delta m = m.mod_delta |
| 137 |
let mod_retroknowledge { mod_retroknowledge = ModBodyVal rk; _ } = rk |
| 138 |
|
| 139 |
let mod_global_delta m = match m.mod_type with |
| 140 |
| MoreFunctor _ -> None |
| 141 |
| NoFunctor _ -> Some m.mod_delta |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
let hcons_when_mod_body (type a b) (f : b -> b) : (a, b) when_mod_body -> (a, b) when_mod_body = function |
| 146 |
| ModBodyVal v as arg -> |
| 147 |
let v' = f v in |
| 148 |
if v == v' then arg else ModBodyVal v' |
| 149 |
| ModTypeNul -> ModTypeNul |
| 150 |
|
| 151 |
let hcons_functorize hty he hself f = match f with |
| 152 |
| NoFunctor e -> |
| 153 |
let e' = he e in |
| 154 |
if e == e' then f else NoFunctor e' |
| 155 |
| MoreFunctor (mid, ty, nf) -> |
| 156 |
|
| 157 |
let mid' = mid in |
| 158 |
let ty' = hty ty in |
| 159 |
let nf' = hself nf in |
| 160 |
if mid == mid' && ty == ty' && nf == nf' then f |
| 161 |
else MoreFunctor (mid, ty', nf') |
| 162 |
|
| 163 |
let hcons_module_alg_expr me = me |
| 164 |
|
| 165 |
let rec hcons_module_expression me = match me with |
| 166 |
| MENoFunctor malg -> |
| 167 |
let malg' = hcons_module_alg_expr malg in |
| 168 |
if malg == malg' then me else MENoFunctor malg' |
| 169 |
| MEMoreFunctor mf -> |
| 170 |
let mf' = hcons_module_expression mf in |
| 171 |
if mf' == mf then me else MEMoreFunctor mf' |
| 172 |
|
| 173 |
let rec hcons_structure_field_body sb = match sb with |
| 174 |
| SFBconst cb -> |
| 175 |
let cb' = hcons_const_body cb in |
| 176 |
if cb == cb' then sb else SFBconst cb' |
| 177 |
| SFBmind mib -> |
| 178 |
let mib' = hcons_mind mib in |
| 179 |
if mib == mib' then sb else SFBmind mib' |
| 180 |
| SFBmodule mb -> |
| 181 |
let mb' = hcons_generic_module_body mb in |
| 182 |
if mb == mb' then sb else SFBmodule mb' |
| 183 |
| SFBmodtype mb -> |
| 184 |
let mb' = hcons_generic_module_body mb in |
| 185 |
if mb == mb' then sb else SFBmodtype mb' |
| 186 |
| SFBrules _ -> sb |
| 187 |
|
| 188 |
and hcons_structure_body sb = |
| 189 |
|
| 190 |
let map (l, sfb as fb) = |
| 191 |
let _, l' = Names.Id.hcons l in |
| 192 |
let sfb' = hcons_structure_field_body sfb in |
| 193 |
if l == l' && sfb == sfb' then fb else (l', sfb') |
| 194 |
in |
| 195 |
List.Smart.map map sb |
| 196 |
|
| 197 |
and hcons_module_signature ms = |
| 198 |
hcons_functorize hcons_generic_module_body hcons_structure_body hcons_module_signature ms |
| 199 |
|
| 200 |
and hcons_module_implementation mip = match mip with |
| 201 |
| Abstract -> Abstract |
| 202 |
| Algebraic me -> |
| 203 |
let me' = hcons_module_expression me in |
| 204 |
if me == me' then mip else Algebraic me' |
| 205 |
| Struct (reso, ms) -> |
| 206 |
let ms' = hcons_structure_body ms in |
| 207 |
if ms == ms' then mip else Struct (reso, ms) |
| 208 |
| FullStruct -> FullStruct |
| 209 |
|
| 210 |
and hcons_generic_module_body : |
| 211 |
'a. 'a generic_module_body -> 'a generic_module_body = |
| 212 |
fun mb -> |
| 213 |
let expr' = hcons_when_mod_body hcons_module_implementation mb.mod_expr in |
| 214 |
let type' = hcons_module_signature mb.mod_type in |
| 215 |
let type_alg' = mb.mod_type_alg in |
| 216 |
let delta' = mb.mod_delta in |
| 217 |
let retroknowledge' = mb.mod_retroknowledge in |
| 218 |
|
| 219 |
if |
| 220 |
mb.mod_expr == expr' && |
| 221 |
mb.mod_type == type' && |
| 222 |
mb.mod_type_alg == type_alg' && |
| 223 |
mb.mod_delta == delta' && |
| 224 |
mb.mod_retroknowledge == retroknowledge' |
| 225 |
then mb |
| 226 |
else { |
| 227 |
mod_expr = expr'; |
| 228 |
mod_type = type'; |
| 229 |
mod_type_alg = type_alg'; |
| 230 |
mod_delta = delta'; |
| 231 |
mod_retroknowledge = retroknowledge'; |
| 232 |
} |
| 233 |
|
| 234 |
let hcons_module_body = |
| 235 |
hcons_generic_module_body |
| 236 |
|
| 237 |
let hcons_module_type = |
| 238 |
hcons_generic_module_body |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
let rec functor_smart_map fty f0 funct = match funct with |
| 243 |
| MoreFunctor (mbid,ty,e) -> |
| 244 |
let ty' = fty mbid ty in |
| 245 |
let e' = functor_smart_map fty f0 e in |
| 246 |
if ty==ty' && e==e' then funct else MoreFunctor (mbid,ty',e') |
| 247 |
| NoFunctor a -> |
| 248 |
let a' = f0 a in if a==a' then funct else NoFunctor a' |
| 249 |
|
| 250 |
let implem_smart_map (type a) fs fa (expr : (a, _) when_mod_body) : (a, _) when_mod_body = |
| 251 |
match expr with |
| 252 |
| ModTypeNul -> ModTypeNul |
| 253 |
| ModBodyVal impl -> |
| 254 |
match impl with |
| 255 |
| Struct (reso, e) -> |
| 256 |
let reso', e' = fs (reso, e) in |
| 257 |
if reso==reso' && e==e' then expr else ModBodyVal (Struct (reso', e')) |
| 258 |
| Algebraic a -> |
| 259 |
let a' = fa a in |
| 260 |
if a==a' then expr else ModBodyVal (Algebraic a') |
| 261 |
| Abstract | FullStruct -> expr |
| 262 |
|
| 263 |
let functorize params init = |
| 264 |
List.fold_left (fun e (mbid,mt) -> MoreFunctor(mbid,mt,e)) init params |
| 265 |
|
| 266 |
let functorize_module params mb = |
| 267 |
let f x = functorize params x in |
| 268 |
let fe x = iterate (fun e -> MEMoreFunctor e) (List.length params) x in |
| 269 |
{ mb with |
| 270 |
mod_expr = implem_smart_map (fun x -> x) fe mb.mod_expr; |
| 271 |
mod_type = f mb.mod_type; |
| 272 |
mod_type_alg = Option.map fe mb.mod_type_alg } |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
type subst_kind = Dom | Codom | Both | Neither | Shallow of Mod_subst.substitution |
| 277 |
|
| 278 |
let subst_dom = Dom |
| 279 |
let subst_codom = Codom |
| 280 |
let subst_dom_codom = Both |
| 281 |
let subst_shallow_dom_codom s = Shallow s |
| 282 |
|
| 283 |
let apply_subst skind subst delta = match skind with |
| 284 |
| Dom -> subst_dom_delta_resolver subst delta |
| 285 |
| Codom -> subst_codom_delta_resolver subst delta |
| 286 |
| Both -> subst_dom_codom_delta_resolver subst delta |
| 287 |
| Neither -> delta |
| 288 |
| Shallow subst' -> subst_dom_codom_delta_resolver subst' delta |
| 289 |
|
| 290 |
let is_functor = function |
| 291 |
| NoFunctor _ -> false |
| 292 |
| MoreFunctor _ -> true |
| 293 |
|
| 294 |
let subst_with_body subst = function |
| 295 |
| WithMod(id,mp) as orig -> |
| 296 |
let mp' = subst_mp subst mp in |
| 297 |
if mp==mp' then orig else WithMod(id,mp') |
| 298 |
| WithDef(id,(c,ctx)) as orig -> |
| 299 |
let c' = subst_mps subst c in |
| 300 |
if c==c' then orig else WithDef(id,(c',ctx)) |
| 301 |
|
| 302 |
let subst_retro : type a. Mod_subst.substitution -> a module_retroknowledge -> a module_retroknowledge = |
| 303 |
fun subst retro -> |
| 304 |
match retro with |
| 305 |
| ModTypeNul as r -> r |
| 306 |
| ModBodyVal l as r -> |
| 307 |
let l' = List.Smart.map (subst_retro_action subst) l in |
| 308 |
if l == l' then r else ModBodyVal l |
| 309 |
|
| 310 |
let rec subst_structure skind subst mp sign = |
| 311 |
let subst_field ((l,body) as orig) = match body with |
| 312 |
| SFBconst cb -> |
| 313 |
let cb' = subst_const_body subst cb in |
| 314 |
if cb==cb' then orig else (l,SFBconst cb') |
| 315 |
| SFBmind mib -> |
| 316 |
let mib' = subst_mind_body subst mib in |
| 317 |
if mib==mib' then orig else (l,SFBmind mib') |
| 318 |
| SFBrules rrb -> |
| 319 |
let rrb' = subst_rewrite_rules subst rrb in |
| 320 |
if rrb==rrb' then orig else (l,SFBrules rrb') |
| 321 |
| SFBmodule mb -> |
| 322 |
let mb' = subst_module skind subst (MPdot (mp, l)) mb in |
| 323 |
if mb==mb' then orig else (l,SFBmodule mb') |
| 324 |
| SFBmodtype mtb -> |
| 325 |
let mtb' = subst_modtype skind subst (MPdot (mp, l)) mtb in |
| 326 |
if mtb==mtb' then orig else (l,SFBmodtype mtb') |
| 327 |
in |
| 328 |
List.Smart.map subst_field sign |
| 329 |
|
| 330 |
and subst_module_body : type a. _ -> _ -> _ -> _ -> a generic_module_body -> a generic_module_body = |
| 331 |
fun is_mod skind subst mp mb -> |
| 332 |
let { mod_expr=me; mod_type=ty; mod_type_alg=aty; |
| 333 |
mod_retroknowledge=retro; _ } = mb in |
| 334 |
let mp' = subst_mp subst mp in |
| 335 |
let subst = |
| 336 |
if ModPath.equal mp mp' then subst |
| 337 |
else if is_mod && not (is_functor ty) then subst |
| 338 |
else add_mp mp mp' (empty_delta_resolver mp') subst |
| 339 |
in |
| 340 |
let ty' = subst_signature skind subst mp ty in |
| 341 |
let me' = subst_impl skind subst mp me in |
| 342 |
let aty' = Option.Smart.map (subst_expression subst) aty in |
| 343 |
let retro' = subst_retro subst retro in |
| 344 |
let delta' = apply_subst skind subst mb.mod_delta in |
| 345 |
if mp==mp' && me==me' && ty==ty' && aty==aty' |
| 346 |
&& retro==retro' && delta'==mb.mod_delta |
| 347 |
then mb |
| 348 |
else |
| 349 |
{ mod_expr = me'; |
| 350 |
mod_type = ty'; |
| 351 |
mod_type_alg = aty'; |
| 352 |
mod_retroknowledge = retro'; |
| 353 |
mod_delta = delta'; |
| 354 |
} |
| 355 |
|
| 356 |
and subst_module skind subst mp mb = |
| 357 |
subst_module_body true skind subst mp mb |
| 358 |
|
| 359 |
and subst_impl : type a. _ -> _ -> _ -> (a, _) when_mod_body -> (a, _) when_mod_body = |
| 360 |
fun skind subst mp me -> |
| 361 |
implem_smart_map |
| 362 |
(fun (reso, sign) -> apply_subst skind subst reso, subst_structure Neither subst mp sign) (subst_expression subst) me |
| 363 |
|
| 364 |
and subst_modtype skind subst mp mtb = subst_module_body false skind subst mp mtb |
| 365 |
|
| 366 |
and subst_expr subst seb = match seb with |
| 367 |
| MEident mp -> |
| 368 |
let mp' = subst_mp subst mp in |
| 369 |
if mp==mp' then seb else MEident mp' |
| 370 |
| MEapply (meb1,mp2) -> |
| 371 |
let meb1' = subst_expr subst meb1 in |
| 372 |
let mp2' = subst_mp subst mp2 in |
| 373 |
if meb1==meb1' && mp2==mp2' then seb else MEapply(meb1',mp2') |
| 374 |
| MEwith (meb,wdb) -> |
| 375 |
let meb' = subst_expr subst meb in |
| 376 |
let wdb' = subst_with_body subst wdb in |
| 377 |
if meb==meb' && wdb==wdb' then seb else MEwith(meb',wdb') |
| 378 |
|
| 379 |
and subst_expression subst me = match me with |
| 380 |
| MENoFunctor malg -> |
| 381 |
let malg' = subst_expr subst malg in |
| 382 |
if malg == malg' then me else MENoFunctor malg' |
| 383 |
| MEMoreFunctor mf -> |
| 384 |
let mf' = subst_expression subst mf in |
| 385 |
if mf == mf' then me else MEMoreFunctor mf' |
| 386 |
|
| 387 |
and subst_signature skind subst mp = |
| 388 |
functor_smart_map |
| 389 |
(fun mbid mtb -> subst_modtype skind subst (MPbound mbid) mtb) |
| 390 |
(fun sign -> subst_structure skind subst mp sign) |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
type 'a mod_expr = ('a, module_implementation) when_mod_body |
| 401 |
|
| 402 |
let rec is_bounded_expr l = function |
| 403 |
| MEident (MPbound mbid) -> MBId.Set.mem mbid l |
| 404 |
| MEapply (fexpr,mp) -> |
| 405 |
is_bounded_expr l (MEident mp) || is_bounded_expr l fexpr |
| 406 |
| _ -> false |
| 407 |
|
| 408 |
let rec clean_module_body : type a. _ -> a generic_module_body -> a generic_module_body = |
| 409 |
fun l mb -> |
| 410 |
let typ = mb.mod_type in |
| 411 |
let typ' = clean_signature l typ in |
| 412 |
let expr' = clean_mod_expr l mb.mod_expr in |
| 413 |
if typ==typ' && mb.mod_expr==expr' then mb |
| 414 |
else { mb with mod_type=typ'; mod_expr = expr' } |
| 415 |
|
| 416 |
and clean_field l field = match field with |
| 417 |
| (lab,SFBmodule mb) -> |
| 418 |
let mb' = clean_module_body l mb in |
| 419 |
if mb==mb' then field else (lab,SFBmodule mb') |
| 420 |
| _ -> field |
| 421 |
|
| 422 |
and clean_structure l = List.Smart.map (clean_field l) |
| 423 |
|
| 424 |
and clean_body l (reso, v) = |
| 425 |
(reso, clean_structure l v) |
| 426 |
|
| 427 |
and clean_signature l = |
| 428 |
functor_smart_map (fun _ mb -> clean_module_body l mb) (clean_structure l) |
| 429 |
|
| 430 |
and clean_expression _ me = me |
| 431 |
|
| 432 |
and clean_mod_expr : type a. _ -> a mod_expr -> a mod_expr = |
| 433 |
fun l me -> match me with |
| 434 |
| ModBodyVal (Algebraic (MENoFunctor m)) when is_bounded_expr l m -> |
| 435 |
ModBodyVal FullStruct |
| 436 |
| _ -> |
| 437 |
let me' = implem_smart_map (clean_body l) (clean_expression l) me in |
| 438 |
if me == me' then me else me' |