src/Pure/Isar/locale.ML
changeset 16620 2a7f46324218
parent 16458 4c6fd0c01d28
child 16736 1e792b32abef
equal deleted inserted replaced
16619:94e3d94b426d 16620:2a7f46324218
    18     Workshop, TYPES 2003, Torino, Italy, LNCS 3085, pages 34-50, 2004.
    18     Workshop, TYPES 2003, Torino, Italy, LNCS 3085, pages 34-50, 2004.
    19 *)
    19 *)
    20 
    20 
    21 (* TODO:
    21 (* TODO:
    22 - beta-eta normalisation of interpretation parameters
    22 - beta-eta normalisation of interpretation parameters
    23 - no beta reduction of interpretation witnesses
       
    24 - dangling type frees in locales
    23 - dangling type frees in locales
       
    24 - test subsumption of interpretations when merging theories
    25 *)
    25 *)
    26 
    26 
    27 signature LOCALE =
    27 signature LOCALE =
    28 sig
    28 sig
    29   type context
    29   type context
   135        from the locale predicate to the normalised assumptions of the locale
   135        from the locale predicate to the normalised assumptions of the locale
   136        (cf. [1], normalisation of locale expressions.)
   136        (cf. [1], normalisation of locale expressions.)
   137     *)
   137     *)
   138   import: expr,                                       (*dynamic import*)
   138   import: expr,                                       (*dynamic import*)
   139   elems: (element_i * stamp) list,                    (*static content*)
   139   elems: (element_i * stamp) list,                    (*static content*)
   140   params: ((string * typ option) * mixfix option) list * string list}
   140   params: ((string * typ) * mixfix option) list * string list}
   141                                                       (*all/local params*)
   141                                                       (*all/local params*)
   142 
   142 
   143 
   143 
   144 (* CB: an internal (Int) locale element was either imported or included,
   144 (* CB: an internal (Int) locale element was either imported or included,
   145    an external (Ext) element appears directly in the locale text. *)
   145    an external (Ext) element appears directly in the locale text. *)
   147 datatype ('a, 'b) int_ext = Int of 'a | Ext of 'b;
   147 datatype ('a, 'b) int_ext = Int of 'a | Ext of 'b;
   148 
   148 
   149 
   149 
   150 
   150 
   151 (** term and type instantiation, using symbol tables **)
   151 (** term and type instantiation, using symbol tables **)
       
   152 (** functions for term instantiation beta-reduce the result
       
   153     unless the instantiation table is empty (inst_tab_term)
       
   154     or the instantiation has no effect (inst_tab_thm) **)
   152 
   155 
   153 (* instantiate TFrees *)
   156 (* instantiate TFrees *)
   154 
   157 
   155 fun tinst_tab_type tinst T = if Symtab.is_empty tinst
   158 fun tinst_tab_type tinst T = if Symtab.is_empty tinst
   156       then T
   159       then T
   194                | SOME t => t)
   197                | SOME t => t)
   195             | instf (Var (xi, T)) = Var (xi, tinst_tab_type tinst T)
   198             | instf (Var (xi, T)) = Var (xi, tinst_tab_type tinst T)
   196             | instf (b as Bound _) = b
   199             | instf (b as Bound _) = b
   197             | instf (Abs (x, T, t)) = Abs (x, tinst_tab_type tinst T, instf t)
   200             | instf (Abs (x, T, t)) = Abs (x, tinst_tab_type tinst T, instf t)
   198             | instf (s $ t) = instf s $ instf t
   201             | instf (s $ t) = instf s $ instf t
   199         in instf end;
   202         in Envir.beta_norm o instf end;
   200 
   203 
   201 fun inst_tab_thm thy (inst, tinst) thm = if Symtab.is_empty inst
   204 fun inst_tab_thm thy (inst, tinst) thm = if Symtab.is_empty inst
   202       then tinst_tab_thm thy tinst thm
   205       then tinst_tab_thm thy tinst thm
   203       else let
   206       else let
   204           val cert = Thm.cterm_of thy;
   207           val cert = Thm.cterm_of thy;
   224             |> (fn (th, al) => th |> Thm.instantiate
   227             |> (fn (th, al) => th |> Thm.instantiate
   225                 ((map (fn (a, T) => (certT (TVar (valOf (assoc (al, a)))), certT T)) tinst'),
   228                 ((map (fn (a, T) => (certT (TVar (valOf (assoc (al, a)))), certT T)) tinst'),
   226                   []))
   229                   []))
   227             |> Drule.forall_intr_list (map (cert o #1) inst')
   230             |> Drule.forall_intr_list (map (cert o #1) inst')
   228             |> Drule.forall_elim_list (map (cert o #2) inst') 
   231             |> Drule.forall_elim_list (map (cert o #2) inst') 
       
   232             |> Drule.fconv_rule (Thm.beta_conversion true)
   229             |> (fn th => Drule.implies_elim_list th
   233             |> (fn th => Drule.implies_elim_list th
   230                  (map (Thm.assume o cert o inst_tab_term (inst, tinst)) hyps))
   234                  (map (Thm.assume o cert o inst_tab_term (inst, tinst)) hyps))
   231         end;
   235         end;
   232 
   236 
   233 
   237 
   258   fun untermify t =
   262   fun untermify t =
   259     let fun ut (Const _) ts = ts
   263     let fun ut (Const _) ts = ts
   260           | ut (s $ t) ts = ut s (t::ts)
   264           | ut (s $ t) ts = ut s (t::ts)
   261     in ut t [] end;
   265     in ut t [] end;
   262 
   266 
   263   (* joining of registrations: prefix and attributs of left theory,
   267   (* joining of registrations: prefix and attributes of left theory,
   264      thms are equal, no attempt to subsumption testing *)
   268      thms are equal, no attempt to subsumption testing *)
   265   fun join (r1, r2) = Termtab.join (fn _ => fn (reg, _) => SOME reg) (r1, r2);
   269   fun join (r1, r2) = Termtab.join (fn _ => fn (reg, _) => SOME reg) (r1, r2);
   266 
   270 
   267   fun dest regs = map (apfst untermify) (Termtab.dest regs);
   271   fun dest regs = map (apfst untermify) (Termtab.dest regs);
   268 
   272 
   912        also takes care of parameter syntax *)
   916        also takes care of parameter syntax *)
   913 
   917 
   914     fun eval syn ((name, xs), axs) =
   918     fun eval syn ((name, xs), axs) =
   915       let
   919       let
   916         val {params = (ps, qs), elems, ...} = the_locale thy name;
   920         val {params = (ps, qs), elems, ...} = the_locale thy name;
   917         val ps' = map #1 ps;
   921         val ps' = map (apsnd SOME o #1) ps;
   918         val ren = map #1 ps' ~~
   922         val ren = map #1 ps' ~~
   919               map (fn x => (x, valOf (Symtab.lookup (syn, x)))) xs;
   923               map (fn x => (x, valOf (Symtab.lookup (syn, x)))) xs;
   920         val (params', elems') =
   924         val (params', elems') =
   921           if null ren then ((ps', qs), map #1 elems)
   925           if null ren then ((ps', qs), map #1 elems)
   922           else ((map (apfst (rename ren)) ps', map (rename ren) qs),
   926           else ((map (apfst (rename ren)) ps', map (rename ren) qs),
  1437     val (target_stmt, fixed_params, import) =
  1441     val (target_stmt, fixed_params, import) =
  1438       (case locale of NONE => ([], [], empty)
  1442       (case locale of NONE => ([], [], empty)
  1439       | SOME name =>
  1443       | SOME name =>
  1440           let val {predicate = (stmt, _), params = (ps, _), ...} =
  1444           let val {predicate = (stmt, _), params = (ps, _), ...} =
  1441             the_locale thy name
  1445             the_locale thy name
  1442           in (stmt, param_types (map fst ps), Locale name) end);
  1446           in (stmt, map fst ps, Locale name) end);
  1443     val ((((locale_ctxt, locale_elemss), (elems_ctxt, _, _)), _), (elems_stmt, concl')) =
  1447     val ((((locale_ctxt, locale_elemss), (elems_ctxt, _, _)), _), (elems_stmt, concl')) =
  1444       prep_ctxt false fixed_params import elems concl ctxt;
  1448       prep_ctxt false fixed_params import elems concl ctxt;
  1445   in (locale, (target_stmt, elems_stmt), locale_ctxt, elems_ctxt, concl') end;
  1449   in (locale, (target_stmt, elems_stmt), locale_ctxt, elems_ctxt, concl') end;
  1446 
  1450 
  1447 in
  1451 in
  1568 (* store instantiations of args for all registered interpretations
  1572 (* store instantiations of args for all registered interpretations
  1569    of the theory *)
  1573    of the theory *)
  1570 
  1574 
  1571 fun note_thmss_registrations kind target args thy =
  1575 fun note_thmss_registrations kind target args thy =
  1572   let
  1576   let
  1573     val (parms, parmTs_o) =
  1577     val (parms, parmTs) =
  1574           the_locale thy target |> #params |> fst |> map fst |> split_list;
  1578           the_locale thy target |> #params |> fst |> map fst |> split_list;
  1575     val parmvTs = map (Type.varifyT o valOf) parmTs_o;
  1579     val parmvTs = map Type.varifyT parmTs;
  1576     val ids = flatten (ProofContext.init thy, intern_expr thy)
  1580     val ids = flatten (ProofContext.init thy, intern_expr thy)
  1577       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst |> map fst;
  1581       (([], Symtab.empty), Expr (Locale target)) |> fst |> fst |> map fst;
  1578 
  1582 
  1579     val regs = get_global_registrations thy target;
  1583     val regs = get_global_registrations thy target;
  1580 
  1584 
  1832     |> note_thmss_qualified "" name facts' |> #1
  1836     |> note_thmss_qualified "" name facts' |> #1
  1833     |> declare_locale name
  1837     |> declare_locale name
  1834     |> put_locale name {predicate = predicate, import = import,
  1838     |> put_locale name {predicate = predicate, import = import,
  1835         elems = map (fn e => (e, stamp ())) (List.concat (map #2 (List.filter (equal "" o #1 o #1) elemss'))),
  1839         elems = map (fn e => (e, stamp ())) (List.concat (map #2 (List.filter (equal "" o #1 o #1) elemss'))),
  1836         params = (params_of elemss' |>
  1840         params = (params_of elemss' |>
  1837           map (fn (x, T) => ((x, T), valOf (Symtab.lookup (syn, x)))), map #1 (params_of body_elemss))}
  1841           map (fn (x, SOME T) => ((x, T), valOf (Symtab.lookup (syn, x)))), map #1 (params_of body_elemss))}
  1838   end;
  1842   end;
  1839 
  1843 
  1840 in
  1844 in
  1841 
  1845 
  1842 val add_locale = gen_add_locale read_context intern_expr;
  1846 val add_locale = gen_add_locale read_context intern_expr;