src/Pure/Isar/expression.ML
author ballarin
Tue Nov 25 18:06:21 2008 +0100 (2008-11-25)
changeset 28885 6f6bf52e75bb
parent 28879 db2816a37a34
child 28895 4e2914c2f8c5
permissions -rw-r--r--
Expression types cleaned up, proper treatment of term patterns.
ballarin@28697
     1
(*  Title:      Pure/Isar/expression.ML
ballarin@28697
     2
    ID:         $Id$
ballarin@28697
     3
    Author:     Clemens Ballarin, TU Muenchen
ballarin@28697
     4
ballarin@28795
     5
New locale development --- experimental.
ballarin@28697
     6
*)
ballarin@28697
     7
ballarin@28697
     8
signature EXPRESSION =
ballarin@28697
     9
sig
ballarin@28885
    10
  datatype 'term map = Positional of 'term option list | Named of (string * 'term) list;
ballarin@28885
    11
  type 'term expr = (string * (string * 'term map)) list;
ballarin@28885
    12
  type expression = string expr * (Name.binding * string option * mixfix) list;
ballarin@28885
    13
  type expression_i = term expr * (Name.binding * typ option * mixfix) list;
ballarin@28697
    14
ballarin@28879
    15
  (* Processing of locale statements *)
ballarin@28879
    16
  val read_statement: Element.context list -> (string * string list) list list ->
ballarin@28879
    17
    Proof.context ->  (term * term list) list list * Proof.context;
ballarin@28879
    18
  val cert_statement: Element.context_i list -> (term * term list) list list ->
ballarin@28879
    19
    Proof.context -> (term * term list) list list * Proof.context;
ballarin@28879
    20
ballarin@28795
    21
  (* Declaring locales *)
ballarin@28795
    22
  val add_locale: string -> bstring -> expression -> Element.context list -> theory ->
ballarin@28795
    23
    string * Proof.context
ballarin@28795
    24
  val add_locale_i: string -> bstring -> expression_i -> Element.context_i list -> theory ->
ballarin@28795
    25
    string * Proof.context
ballarin@28885
    26
ballarin@28795
    27
  (* Debugging and development *)
ballarin@28795
    28
  val parse_expression: OuterParse.token list -> expression * OuterParse.token list
ballarin@28697
    29
end;
ballarin@28697
    30
ballarin@28697
    31
ballarin@28885
    32
structure Expression : EXPRESSION =
ballarin@28697
    33
struct
ballarin@28697
    34
ballarin@28795
    35
datatype ctxt = datatype Element.ctxt;
ballarin@28795
    36
ballarin@28795
    37
ballarin@28795
    38
(*** Expressions ***)
ballarin@28697
    39
ballarin@28872
    40
datatype 'term map =
ballarin@28872
    41
  Positional of 'term option list |
ballarin@28872
    42
  Named of (string * 'term) list;
ballarin@28697
    43
ballarin@28885
    44
type 'term expr = (string * (string * 'term map)) list;
ballarin@28697
    45
ballarin@28885
    46
type expression = string expr * (Name.binding * string option * mixfix) list;
ballarin@28885
    47
type expression_i = term expr * (Name.binding * typ option * mixfix) list;
ballarin@28795
    48
ballarin@28697
    49
ballarin@28697
    50
(** Parsing and printing **)
ballarin@28697
    51
ballarin@28697
    52
local
ballarin@28697
    53
ballarin@28697
    54
structure P = OuterParse;
ballarin@28697
    55
ballarin@28697
    56
val loc_keyword = P.$$$ "fixes" || P.$$$ "constrains" || P.$$$ "assumes" ||
ballarin@28795
    57
   P.$$$ "defines" || P.$$$ "notes";
ballarin@28697
    58
fun plus1_unless test scan =
ballarin@28697
    59
  scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
ballarin@28697
    60
ballarin@28697
    61
val prefix = P.name --| P.$$$ ":";
ballarin@28697
    62
val named = P.name -- (P.$$$ "=" |-- P.term);
ballarin@28697
    63
val position = P.maybe P.term;
ballarin@28697
    64
val instance = P.$$$ "where" |-- P.and_list1 named >> Named ||
ballarin@28697
    65
  Scan.repeat1 position >> Positional;
ballarin@28697
    66
ballarin@28697
    67
in
ballarin@28697
    68
ballarin@28795
    69
val parse_expression =
ballarin@28697
    70
  let
ballarin@28697
    71
    fun expr2 x = P.xname x;
ballarin@28697
    72
    fun expr1 x = (Scan.optional prefix "" -- expr2 --
ballarin@28697
    73
      Scan.optional instance (Named []) >> (fn ((p, l), i) => (l, (p, i)))) x;
ballarin@28885
    74
    fun expr0 x = (plus1_unless loc_keyword expr1) x;
ballarin@28697
    75
  in expr0 -- P.for_fixes end;
ballarin@28697
    76
ballarin@28697
    77
end;
ballarin@28697
    78
ballarin@28885
    79
fun pretty_expr thy expr =
ballarin@28795
    80
  let
ballarin@28795
    81
    fun pretty_pos NONE = Pretty.str "_"
ballarin@28795
    82
      | pretty_pos (SOME x) = Pretty.str x;
ballarin@28795
    83
    fun pretty_named (x, y) = [Pretty.str x, Pretty.brk 1, Pretty.str "=",
ballarin@28795
    84
          Pretty.brk 1, Pretty.str y] |> Pretty.block;
ballarin@28795
    85
    fun pretty_ren (Positional ps) = take_suffix is_none ps |> snd |>
ballarin@28795
    86
          map pretty_pos |> Pretty.breaks
ballarin@28795
    87
      | pretty_ren (Named []) = []
ballarin@28795
    88
      | pretty_ren (Named ps) = Pretty.str "where" :: Pretty.brk 1 ::
ballarin@28795
    89
          (ps |> map pretty_named |> Pretty.separate "and");
ballarin@28795
    90
    fun pretty_rename (loc, ("", ren)) =
ballarin@28795
    91
          Pretty.block (Pretty.str (NewLocale.extern thy loc) :: Pretty.brk 1 :: pretty_ren ren) 
ballarin@28795
    92
      | pretty_rename (loc, (prfx, ren)) =
ballarin@28795
    93
          Pretty.block (Pretty.str prfx :: Pretty.brk 1 :: Pretty.str (NewLocale.extern thy loc) ::
ballarin@28795
    94
            Pretty.brk 1 :: pretty_ren ren);
ballarin@28795
    95
  in Pretty.separate "+" (map pretty_rename expr) |> Pretty.block end;
ballarin@28795
    96
ballarin@28885
    97
fun err_in_expr thy msg expr =
ballarin@28795
    98
  let
ballarin@28795
    99
    val err_msg =
ballarin@28795
   100
      if null expr then msg
ballarin@28795
   101
      else msg ^ "\n" ^ Pretty.string_of (Pretty.block
ballarin@28795
   102
        [Pretty.str "The above error(s) occurred in expression:", Pretty.brk 1,
ballarin@28885
   103
          pretty_expr thy expr])
ballarin@28795
   104
  in error err_msg end;
ballarin@28697
   105
ballarin@28697
   106
ballarin@28859
   107
(** Internalise locale names in expr **)
ballarin@28697
   108
ballarin@28885
   109
fun intern thy instances =  map (apfst (NewLocale.intern thy)) instances;
ballarin@28697
   110
ballarin@28697
   111
ballarin@28859
   112
(** Parameters of expression.
ballarin@28697
   113
   Sanity check of instantiations.
ballarin@28795
   114
   Positional instantiations are extended to match full length of parameter list. **)
ballarin@28697
   115
ballarin@28859
   116
fun parameters_of thy (expr, fixed) =
ballarin@28697
   117
  let
ballarin@28697
   118
    fun reject_dups message xs =
ballarin@28697
   119
      let val dups = duplicates (op =) xs
ballarin@28697
   120
      in
ballarin@28697
   121
        if null dups then () else error (message ^ commas dups)
ballarin@28697
   122
      end;
ballarin@28697
   123
ballarin@28795
   124
    fun match_bind (n, b) = (n = Name.name_of b);
ballarin@28795
   125
    fun bind_eq (b1, b2) = (Name.name_of b1 = Name.name_of b2);
ballarin@28795
   126
      (* FIXME: cannot compare bindings for equality. *)
ballarin@28795
   127
ballarin@28795
   128
    fun params_loc loc =
ballarin@28859
   129
          (NewLocale.params_of thy loc |> map (fn (p, _, mx) => (p, mx)), loc);
ballarin@28795
   130
    fun params_inst (expr as (loc, (prfx, Positional insts))) =
ballarin@28697
   131
          let
ballarin@28795
   132
            val (ps, loc') = params_loc loc;
ballarin@28697
   133
	    val d = length ps - length insts;
ballarin@28697
   134
	    val insts' =
ballarin@28879
   135
	      if d < 0 then error ("More arguments than parameters in instantiation of locale " ^
ballarin@28879
   136
                quote (NewLocale.extern thy loc))
ballarin@28697
   137
	      else insts @ replicate d NONE;
ballarin@28697
   138
            val ps' = (ps ~~ insts') |>
ballarin@28697
   139
              map_filter (fn (p, NONE) => SOME p | (_, SOME _) => NONE);
ballarin@28885
   140
          in (ps', (loc', (prfx, Positional insts'))) end
ballarin@28795
   141
      | params_inst (expr as (loc, (prfx, Named insts))) =
ballarin@28697
   142
          let
ballarin@28697
   143
            val _ = reject_dups "Duplicate instantiation of the following parameter(s): "
ballarin@28859
   144
              (map fst insts);
ballarin@28697
   145
ballarin@28795
   146
            val (ps, loc') = params_loc loc;
ballarin@28697
   147
            val ps' = fold (fn (p, _) => fn ps =>
ballarin@28795
   148
              if AList.defined match_bind ps p then AList.delete match_bind p ps
ballarin@28859
   149
              else error (quote p ^" not a parameter of instantiated expression.")) insts ps;
ballarin@28885
   150
          in (ps', (loc', (prfx, Named insts))) end;
ballarin@28885
   151
    fun params_expr is =
ballarin@28697
   152
          let
ballarin@28697
   153
            val (is', ps') = fold_map (fn i => fn ps =>
ballarin@28697
   154
              let
ballarin@28795
   155
                val (ps', i') = params_inst i;
ballarin@28795
   156
                val ps'' = AList.join bind_eq (fn p => fn (mx1, mx2) =>
ballarin@28795
   157
                  (* FIXME: should check for bindings being the same.
ballarin@28795
   158
                     Instead we check for equal name and syntax. *)
ballarin@28697
   159
                  if mx1 = mx2 then mx1
haftmann@28862
   160
                  else error ("Conflicting syntax for parameter" ^ quote (Name.display p) ^
ballarin@28859
   161
                    " in expression.")) (ps, ps')
ballarin@28697
   162
              in (i', ps'') end) is []
ballarin@28885
   163
          in (ps', is') end;
ballarin@28697
   164
ballarin@28795
   165
    val (parms, expr') = params_expr expr;
ballarin@28697
   166
ballarin@28859
   167
    val parms' = map (#1 #> Name.name_of) parms;
ballarin@28697
   168
    val fixed' = map (#1 #> Name.name_of) fixed;
ballarin@28697
   169
    val _ = reject_dups "Duplicate fixed parameter(s): " fixed';
ballarin@28697
   170
    val _ = reject_dups "Parameter(s) declared simultaneously in expression and for clause: " (parms' @ fixed');
ballarin@28697
   171
ballarin@28859
   172
  in (expr', (map (fn (b, mx) => (b, NONE, mx)) parms @ fixed)) end;
ballarin@28697
   173
ballarin@28795
   174
ballarin@28795
   175
(** Read instantiation **)
ballarin@28795
   176
ballarin@28872
   177
(* Parse positional or named instantiation *)
ballarin@28872
   178
ballarin@28859
   179
local
ballarin@28859
   180
ballarin@28872
   181
fun prep_inst parse_term parms (Positional insts) ctxt =
ballarin@28872
   182
      (insts ~~ parms) |> map (fn
ballarin@28872
   183
        (NONE, p) => Syntax.parse_term ctxt p |
ballarin@28872
   184
        (SOME t, _) => parse_term ctxt t)
ballarin@28872
   185
  | prep_inst parse_term parms (Named insts) ctxt =
ballarin@28872
   186
      parms |> map (fn p => case AList.lookup (op =) insts p of
ballarin@28872
   187
        SOME t => parse_term ctxt t |
ballarin@28872
   188
        NONE => Syntax.parse_term ctxt p);
ballarin@28872
   189
ballarin@28872
   190
in
ballarin@28872
   191
ballarin@28872
   192
fun parse_inst x = prep_inst Syntax.parse_term x;
ballarin@28872
   193
fun make_inst x = prep_inst (K I) x;
ballarin@28872
   194
ballarin@28872
   195
end;
ballarin@28872
   196
ballarin@28872
   197
ballarin@28872
   198
(* Instantiation morphism *)
ballarin@28872
   199
ballarin@28872
   200
fun inst_morph (parm_names, parm_types) (prfx, insts') ctxt =
ballarin@28795
   201
  let
ballarin@28795
   202
    (* parameters *)
ballarin@28795
   203
    val type_parm_names = fold Term.add_tfreesT parm_types [] |> map fst;
ballarin@28795
   204
ballarin@28795
   205
    (* type inference and contexts *)
ballarin@28795
   206
    val parm_types' = map (TypeInfer.paramify_vars o Logic.varifyT) parm_types;
ballarin@28795
   207
    val type_parms = fold Term.add_tvarsT parm_types' [] |> map (Logic.mk_type o TVar);
ballarin@28795
   208
    val arg = type_parms @ map2 TypeInfer.constrain parm_types' insts';
ballarin@28795
   209
    val res = Syntax.check_terms ctxt arg;
ballarin@28795
   210
    val ctxt' = ctxt |> fold Variable.auto_fixes res;
ballarin@28872
   211
    
ballarin@28795
   212
    (* instantiation *)
ballarin@28795
   213
    val (type_parms'', res') = chop (length type_parms) res;
ballarin@28795
   214
    val insts'' = (parm_names ~~ res') |> map_filter
ballarin@28795
   215
      (fn (inst as (x, Free (y, _))) => if x = y then NONE else SOME inst |
ballarin@28795
   216
        inst => SOME inst);
ballarin@28795
   217
    val instT = Symtab.make (type_parm_names ~~ map Logic.dest_type type_parms'');
ballarin@28795
   218
    val inst = Symtab.make insts'';
ballarin@28795
   219
  in
ballarin@28795
   220
    (Element.inst_morphism (ProofContext.theory_of ctxt) (instT, inst) $>
ballarin@28795
   221
      Morphism.name_morphism (Name.qualified prfx), ctxt')
ballarin@28795
   222
  end;
ballarin@28859
   223
ballarin@28795
   224
ballarin@28795
   225
(*** Locale processing ***)
ballarin@28795
   226
ballarin@28852
   227
(** Parsing **)
ballarin@28852
   228
ballarin@28852
   229
fun parse_elem prep_typ prep_term ctxt elem =
ballarin@28852
   230
  Element.map_ctxt {name = I, var = I, typ = prep_typ ctxt,
ballarin@28852
   231
    term = prep_term ctxt, fact = I, attrib = I} elem;
ballarin@28852
   232
ballarin@28852
   233
fun parse_concl prep_term ctxt concl =
ballarin@28852
   234
  (map o map) (fn (t, ps) =>
ballarin@28852
   235
    (prep_term ctxt, map (prep_term ctxt) ps)) concl;
ballarin@28852
   236
ballarin@28852
   237
ballarin@28885
   238
(** Simultaneous type inference: instantiations + elements + conclusion **)
ballarin@28885
   239
ballarin@28885
   240
local
ballarin@28885
   241
ballarin@28885
   242
fun mk_type T = (Logic.mk_type T, []);
ballarin@28885
   243
fun mk_term t = (t, []);
ballarin@28885
   244
fun mk_propp (p, pats) = (Syntax.type_constraint propT p, pats);
ballarin@28852
   245
ballarin@28885
   246
fun dest_type (T, []) = Logic.dest_type T;
ballarin@28885
   247
fun dest_term (t, []) = t;
ballarin@28885
   248
fun dest_propp (p, pats) = (p, pats);
ballarin@28885
   249
ballarin@28885
   250
fun extract_inst (_, (_, ts)) = map mk_term ts;
ballarin@28885
   251
fun restore_inst ((l, (p, _)), cs) = (l, (p, map dest_term cs));
ballarin@28885
   252
ballarin@28885
   253
fun extract_elem (Fixes fixes) = map (#2 #> the_list #> map mk_type) fixes
ballarin@28885
   254
  | extract_elem (Constrains csts) = map (#2 #> single #> map mk_type) csts
ballarin@28885
   255
  | extract_elem (Assumes asms) = map (#2 #> map mk_propp) asms
ballarin@28885
   256
  | extract_elem (Defines defs) = map (fn (_, (t, ps)) => [mk_propp (t, ps)]) defs
ballarin@28852
   257
  | extract_elem (Notes _) = [];
ballarin@28795
   258
ballarin@28885
   259
fun restore_elem (Fixes fixes, css) =
ballarin@28885
   260
      (fixes ~~ css) |> map (fn ((x, _, mx), cs) =>
ballarin@28885
   261
        (x, cs |> map dest_type |> try hd, mx)) |> Fixes
ballarin@28885
   262
  | restore_elem (Constrains csts, css) =
ballarin@28885
   263
      (csts ~~ css) |> map (fn ((x, _), cs) =>
ballarin@28885
   264
        (x, cs |> map dest_type |> hd)) |> Constrains
ballarin@28885
   265
  | restore_elem (Assumes asms, css) =
ballarin@28885
   266
      (asms ~~ css) |> map (fn ((b, _), cs) => (b, map dest_propp cs)) |> Assumes
ballarin@28885
   267
  | restore_elem (Defines defs, css) =
ballarin@28885
   268
      (defs ~~ css) |> map (fn ((b, _), [c]) => (b, dest_propp c)) |> Defines
ballarin@28852
   269
  | restore_elem (Notes notes, _) = Notes notes;
ballarin@28852
   270
ballarin@28885
   271
fun check cs context =
ballarin@28885
   272
  let
ballarin@28885
   273
    fun prep (_, pats) (ctxt, t :: ts) =
ballarin@28885
   274
      let val ctxt' = Variable.auto_fixes t ctxt
ballarin@28885
   275
      in
ballarin@28885
   276
        ((t, Syntax.check_props (ProofContext.set_mode ProofContext.mode_pattern ctxt') pats),
ballarin@28885
   277
          (ctxt', ts))
ballarin@28885
   278
      end
ballarin@28885
   279
    val (cs', (context', _)) = fold_map prep cs
ballarin@28885
   280
      (context, Syntax.check_terms
ballarin@28885
   281
        (ProofContext.set_mode ProofContext.mode_schematic context) (map fst cs));
ballarin@28885
   282
  in (cs', context') end;
ballarin@28885
   283
ballarin@28885
   284
in
ballarin@28885
   285
ballarin@28872
   286
fun check_autofix insts elems concl ctxt =
ballarin@28852
   287
  let
ballarin@28885
   288
    val inst_cs = map extract_inst insts;
ballarin@28885
   289
    val elem_css = map extract_elem elems;
ballarin@28885
   290
    val concl_cs = (map o map) mk_propp concl;
ballarin@28885
   291
    (* Type inference *)
ballarin@28885
   292
    val (inst_cs' :: css', ctxt') =
ballarin@28885
   293
      (fold_burrow o fold_burrow) check (inst_cs :: elem_css @ [concl_cs]) ctxt;
ballarin@28885
   294
    (* Re-check to resolve bindings, elements and conclusion only *)
ballarin@28885
   295
    val (css'', _) = (fold_burrow o fold_burrow) check css' ctxt';
ballarin@28885
   296
    val (elem_css'', [concl_cs'']) = chop (length elem_css) css'';
ballarin@28885
   297
  in
ballarin@28885
   298
    (map restore_inst (insts ~~ inst_cs'), map restore_elem (elems ~~ elem_css''),
ballarin@28885
   299
      concl_cs'', ctxt')
ballarin@28885
   300
  end;
ballarin@28885
   301
ballarin@28885
   302
end;
ballarin@28852
   303
ballarin@28852
   304
ballarin@28852
   305
(** Prepare locale elements **)
ballarin@28795
   306
ballarin@28795
   307
fun declare_elem prep_vars (Fixes fixes) ctxt =
ballarin@28795
   308
      let val (vars, _) = prep_vars fixes ctxt
ballarin@28852
   309
      in ctxt |> ProofContext.add_fixes_i vars |> snd end
ballarin@28795
   310
  | declare_elem prep_vars (Constrains csts) ctxt =
ballarin@28852
   311
      ctxt |> prep_vars (map (fn (x, T) => (Name.binding x, SOME T, NoSyn)) csts) |> snd
ballarin@28872
   312
  | declare_elem _ (Assumes _) ctxt = ctxt
ballarin@28872
   313
  | declare_elem _ (Defines _) ctxt = ctxt
ballarin@28852
   314
  | declare_elem _ (Notes _) ctxt = ctxt;
ballarin@28795
   315
ballarin@28852
   316
(** Finish locale elements, extract specification text **)
ballarin@28795
   317
ballarin@28795
   318
val norm_term = Envir.beta_norm oo Term.subst_atomic;
ballarin@28795
   319
ballarin@28795
   320
fun abstract_thm thy eq =
ballarin@28795
   321
  Thm.assume (Thm.cterm_of thy eq) |> Drule.gen_all |> Drule.abs_def;
ballarin@28795
   322
ballarin@28795
   323
fun bind_def ctxt eq (xs, env, ths) =
ballarin@28795
   324
  let
ballarin@28795
   325
    val ((y, T), b) = LocalDefs.abs_def eq;
ballarin@28795
   326
    val b' = norm_term env b;
ballarin@28795
   327
    val th = abstract_thm (ProofContext.theory_of ctxt) eq;
ballarin@28795
   328
    fun err msg = error (msg ^ ": " ^ quote y);
ballarin@28795
   329
  in
ballarin@28795
   330
    exists (fn (x, _) => x = y) xs andalso
ballarin@28795
   331
      err "Attempt to define previously specified variable";
ballarin@28795
   332
    exists (fn (Free (y', _), _) => y = y' | _ => false) env andalso
ballarin@28795
   333
      err "Attempt to redefine variable";
ballarin@28795
   334
    (Term.add_frees b' xs, (Free (y, T), b') :: env, th :: ths)
ballarin@28795
   335
  end;
ballarin@28795
   336
ballarin@28872
   337
fun eval_text _ _ (Fixes _) text = text
ballarin@28872
   338
  | eval_text _ _ (Constrains _) text = text
ballarin@28872
   339
  | eval_text _ is_ext (Assumes asms)
ballarin@28795
   340
        (((exts, exts'), (ints, ints')), (xs, env, defs)) =
ballarin@28795
   341
      let
ballarin@28795
   342
        val ts = maps (map #1 o #2) asms;
ballarin@28795
   343
        val ts' = map (norm_term env) ts;
ballarin@28872
   344
        val spec' =
ballarin@28872
   345
          if is_ext then ((exts @ ts, exts' @ ts'), (ints, ints'))
ballarin@28872
   346
          else ((exts, exts'), (ints @ ts, ints' @ ts'));
ballarin@28795
   347
      in (spec', (fold Term.add_frees ts' xs, env, defs)) end
ballarin@28872
   348
  | eval_text ctxt _ (Defines defs) (spec, binds) =
ballarin@28795
   349
      (spec, fold (bind_def ctxt o #1 o #2) defs binds)
ballarin@28872
   350
  | eval_text _ _ (Notes _) text = text;
ballarin@28795
   351
ballarin@28852
   352
fun closeup _ _ false elem = elem
ballarin@28852
   353
  | closeup ctxt parms true elem =
ballarin@28795
   354
      let
ballarin@28795
   355
        fun close_frees t =
ballarin@28795
   356
          let
ballarin@28795
   357
            val rev_frees =
ballarin@28795
   358
              Term.fold_aterms (fn Free (x, T) =>
ballarin@28852
   359
                if AList.defined (op =) parms x then I else insert (op =) (x, T) | _ => I) t [];
ballarin@28795
   360
          in Term.list_all_free (rev rev_frees, t) end;
ballarin@28795
   361
ballarin@28795
   362
        fun no_binds [] = []
ballarin@28852
   363
          | no_binds _ = error "Illegal term bindings in context element";
ballarin@28795
   364
      in
ballarin@28795
   365
        (case elem of
ballarin@28795
   366
          Assumes asms => Assumes (asms |> map (fn (a, propps) =>
ballarin@28795
   367
            (a, map (fn (t, ps) => (close_frees t, no_binds ps)) propps)))
ballarin@28795
   368
        | Defines defs => Defines (defs |> map (fn (a, (t, ps)) =>
ballarin@28795
   369
            (a, (close_frees (#2 (LocalDefs.cert_def ctxt t)), no_binds ps))))
ballarin@28795
   370
        | e => e)
ballarin@28795
   371
      end;
ballarin@28795
   372
ballarin@28872
   373
fun finish_primitive parms _ (Fixes fixes) = Fixes (map (fn (binding, _, mx) =>
ballarin@28795
   374
      let val x = Name.name_of binding
ballarin@28795
   375
      in (binding, AList.lookup (op =) parms x, mx) end) fixes)
ballarin@28872
   376
  | finish_primitive _ _ (Constrains _) = Constrains []
ballarin@28872
   377
  | finish_primitive _ close (Assumes asms) = close (Assumes asms)
ballarin@28872
   378
  | finish_primitive _ close (Defines defs) = close (Defines defs)
ballarin@28872
   379
  | finish_primitive _ _ (Notes facts) = Notes facts;
ballarin@28872
   380
ballarin@28872
   381
fun finish_inst ctxt parms do_close (loc, (prfx, inst)) text =
ballarin@28872
   382
  let
ballarin@28872
   383
    val thy = ProofContext.theory_of ctxt;
ballarin@28872
   384
    val (parm_names, parm_types) = NewLocale.params_of thy loc |>
ballarin@28872
   385
      map (fn (b, SOME T, _) => (Name.name_of b, T)) |> split_list;
ballarin@28872
   386
    val (asm, defs) = NewLocale.specification_of thy loc;
ballarin@28872
   387
    val (morph, _) = inst_morph (parm_names, parm_types) (prfx, inst) ctxt;
ballarin@28872
   388
    val asm' = Option.map (Morphism.term morph) asm;
ballarin@28872
   389
    val defs' = map (Morphism.term morph) defs;
ballarin@28872
   390
    val text' = text |>
ballarin@28872
   391
      (if is_some asm
ballarin@28872
   392
        then eval_text ctxt false (Assumes [(Attrib.no_binding, [(the asm', [])])])
ballarin@28872
   393
        else I) |>
ballarin@28872
   394
      (if not (null defs)
ballarin@28872
   395
        then eval_text ctxt false (Defines (map (fn def => (Attrib.no_binding, (def, []))) defs'))
ballarin@28872
   396
        else I)
ballarin@28872
   397
(* FIXME clone from new_locale.ML *)
ballarin@28872
   398
  in ((loc, morph), text') end;
ballarin@28795
   399
ballarin@28852
   400
fun finish_elem ctxt parms do_close elem text =
ballarin@28795
   401
  let
ballarin@28872
   402
    val elem' = finish_primitive parms (closeup ctxt parms do_close) elem;
ballarin@28872
   403
    val text' = eval_text ctxt true elem' text;
ballarin@28795
   404
  in (elem', text') end
ballarin@28795
   405
  
ballarin@28872
   406
fun finish ctxt parms do_close insts elems text =
ballarin@28872
   407
  let
ballarin@28872
   408
    val (deps, text') = fold_map (finish_inst ctxt parms do_close) insts text;
ballarin@28872
   409
    val (elems', text'') = fold_map (finish_elem ctxt parms do_close) elems text';
ballarin@28872
   410
  in (deps, elems', text'') end;
ballarin@28795
   411
ballarin@28795
   412
ballarin@28795
   413
local
ballarin@28795
   414
ballarin@28852
   415
(* nice, but for now not needed
ballarin@28852
   416
fun fold_suffixes f [] y = f [] y
ballarin@28852
   417
  | fold_suffixes f (x :: xs) y = f (x :: xs) (f xs y);
ballarin@28795
   418
ballarin@28852
   419
fun fold_prefixes f xs y = fold_suffixes (f o rev) (rev xs) y;
ballarin@28852
   420
*)
ballarin@28795
   421
ballarin@28872
   422
fun prep_elems parse_typ parse_prop parse_inst prep_vars
ballarin@28872
   423
    do_close context fixed raw_insts raw_elems raw_concl =
ballarin@28795
   424
  let
ballarin@28872
   425
    val thy = ProofContext.theory_of context;
ballarin@28872
   426
ballarin@28879
   427
    fun prep_inst (loc, (prfx, inst)) (i, marked, insts, ctxt) =
ballarin@28872
   428
      let
ballarin@28872
   429
        val (parm_names, parm_types) = NewLocale.params_of thy loc |>
ballarin@28872
   430
          map (fn (b, SOME T, _) => (Name.name_of b, T)) |> split_list;
ballarin@28872
   431
        val inst' = parse_inst parm_names inst ctxt;
ballarin@28885
   432
        val parm_types' = map (TypeInfer.paramify_vars o
ballarin@28885
   433
          Term.map_type_tvar (fn ((x, _), S) => TVar ((x, i), S)) o Logic.varifyT) parm_types;
ballarin@28872
   434
        val inst'' = map2 TypeInfer.constrain parm_types' inst';
ballarin@28872
   435
        val insts' = insts @ [(loc, (prfx, inst''))];
ballarin@28872
   436
        val (insts'', _, _, ctxt') = check_autofix insts' [] [] ctxt;
ballarin@28872
   437
        val inst''' = insts'' |> List.last |> snd |> snd;
ballarin@28872
   438
        val (morph, _) = inst_morph (parm_names, parm_types) (prfx, inst''') ctxt;
ballarin@28879
   439
        val (marked', ctxt'') = NewLocale.activate_declarations thy (loc, morph) (marked, ctxt);
ballarin@28879
   440
      in (i+1, marked', insts', ctxt'') end;
ballarin@28872
   441
  
ballarin@28872
   442
    fun prep_elem raw_elem (insts, elems, ctxt) =
ballarin@28852
   443
      let
ballarin@28852
   444
        val ctxt' = declare_elem prep_vars raw_elem ctxt;
ballarin@28852
   445
        val elems' = elems @ [parse_elem parse_typ parse_prop ctxt' raw_elem];
ballarin@28852
   446
        (* FIXME term bindings *)
ballarin@28872
   447
        val (_, _, _, ctxt'') = check_autofix insts elems' [] ctxt';
ballarin@28872
   448
      in (insts, elems', ctxt') end;
ballarin@28795
   449
ballarin@28872
   450
    fun prep_concl raw_concl (insts, elems, ctxt) =
ballarin@28795
   451
      let
ballarin@28852
   452
        val concl = (map o map) (fn (t, ps) =>
ballarin@28852
   453
          (parse_prop ctxt t, map (parse_prop ctxt) ps)) raw_concl;
ballarin@28872
   454
      in check_autofix insts elems concl ctxt end;
ballarin@28795
   455
ballarin@28872
   456
    val fors = prep_vars fixed context |> fst;
ballarin@28872
   457
    val ctxt = context |> ProofContext.add_fixes_i fors |> snd;
ballarin@28872
   458
    val (_, _, insts', ctxt') = fold prep_inst raw_insts (0, NewLocale.empty, [], ctxt);
ballarin@28872
   459
    val (_, elems'', ctxt'') = fold prep_elem raw_elems (insts', [], ctxt');
ballarin@28872
   460
    val (insts, elems, concl, ctxt) = prep_concl raw_concl (insts', elems'', ctxt'');
ballarin@28795
   461
ballarin@28872
   462
    (* Retrieve parameter types *)
ballarin@28795
   463
    val xs = fold (fn Fixes fixes => (fn ps => ps @ map (Name.name_of o #1) fixes) |
ballarin@28872
   464
      _ => fn ps => ps) (Fixes fors :: elems) [];
ballarin@28859
   465
    val (Ts, ctxt') = fold_map ProofContext.inferred_param xs ctxt; 
ballarin@28852
   466
    val parms = xs ~~ Ts;
ballarin@28795
   467
ballarin@28872
   468
    val Fixes fors' = finish_primitive parms I (Fixes fors);
ballarin@28872
   469
    val (deps, elems', text) = finish ctxt' parms do_close insts elems ((([], []), ([], [])), ([], [], []));
ballarin@28852
   470
    (* text has the following structure:
ballarin@28795
   471
           (((exts, exts'), (ints, ints')), (xs, env, defs))
ballarin@28795
   472
       where
ballarin@28872
   473
         exts: external assumptions (terms in assumes elements)
ballarin@28795
   474
         exts': dito, normalised wrt. env
ballarin@28872
   475
         ints: internal assumptions (terms in assumptions from insts)
ballarin@28795
   476
         ints': dito, normalised wrt. env
ballarin@28795
   477
         xs: the free variables in exts' and ints' and rhss of definitions,
ballarin@28795
   478
           this includes parameters except defined parameters
ballarin@28795
   479
         env: list of term pairs encoding substitutions, where the first term
ballarin@28795
   480
           is a free variable; substitutions represent defines elements and
ballarin@28795
   481
           the rhs is normalised wrt. the previous env
ballarin@28795
   482
         defs: theorems representing the substitutions from defines elements
ballarin@28795
   483
           (thms are normalised wrt. env).
ballarin@28795
   484
       elems is an updated version of raw_elems:
ballarin@28795
   485
         - type info added to Fixes and modified in Constrains
ballarin@28795
   486
         - axiom and definition statement replaced by corresponding one
ballarin@28795
   487
           from proppss in Assumes and Defines
ballarin@28795
   488
         - Facts unchanged
ballarin@28795
   489
       *)
ballarin@28852
   490
ballarin@28872
   491
  in ((parms, fors', deps, elems', concl), text) end
ballarin@28795
   492
ballarin@28795
   493
in
ballarin@28795
   494
ballarin@28872
   495
fun read_elems x = prep_elems Syntax.parse_typ Syntax.parse_prop parse_inst
ballarin@28872
   496
  ProofContext.read_vars x;
ballarin@28879
   497
fun cert_elems x = prep_elems (K I) (K I)  make_inst
ballarin@28872
   498
  ProofContext.cert_vars x;
ballarin@28795
   499
ballarin@28795
   500
end;
ballarin@28795
   501
ballarin@28795
   502
ballarin@28795
   503
(* full context statements: import + elements + conclusion *)
ballarin@28795
   504
ballarin@28795
   505
local
ballarin@28795
   506
ballarin@28879
   507
fun prep_context_statement prep_expr prep_elems activate_elems
ballarin@28795
   508
    do_close imprt elements raw_concl context =
ballarin@28795
   509
  let
ballarin@28795
   510
    val thy = ProofContext.theory_of context;
ballarin@28795
   511
ballarin@28885
   512
    val (expr, fixed) = parameters_of thy (apfst (prep_expr thy) imprt);
ballarin@28872
   513
    val ((parms, fors, deps, elems, concl), (spec, (_, _, defs))) =
ballarin@28872
   514
      prep_elems do_close context fixed expr elements raw_concl;
ballarin@28879
   515
ballarin@28879
   516
    val (_, ctxt0) = ProofContext.add_fixes_i fors context;
ballarin@28879
   517
    val (_, ctxt) = fold (NewLocale.activate_facts thy) deps (NewLocale.empty, ctxt0);  
ballarin@28879
   518
    val ((elems', _), ctxt') = activate_elems elems (ProofContext.set_stmt true ctxt);
ballarin@28795
   519
  in
ballarin@28872
   520
    (((fors, deps), (ctxt', elems'), (parms, spec, defs)), concl)
ballarin@28879
   521
  end;
ballarin@28879
   522
ballarin@28879
   523
fun prep_statement prep_ctxt elems concl ctxt =
ballarin@28879
   524
  let
ballarin@28885
   525
    val (((_, (ctxt', _), _)), concl) = prep_ctxt false ([], []) elems concl ctxt
ballarin@28879
   526
  in (concl, ctxt') end;
ballarin@28795
   527
ballarin@28795
   528
in
ballarin@28795
   529
ballarin@28879
   530
fun read_statement body concl ctxt =
ballarin@28879
   531
  prep_statement (prep_context_statement intern read_elems Element.activate) body concl ctxt;
ballarin@28879
   532
fun cert_statement body concl ctxt =
ballarin@28879
   533
  prep_statement (prep_context_statement (K I) cert_elems Element.activate_i) body concl ctxt;
ballarin@28879
   534
ballarin@28795
   535
fun read_context imprt body ctxt =
ballarin@28879
   536
  #1 (prep_context_statement intern read_elems Element.activate true imprt body [] ctxt);
ballarin@28795
   537
fun cert_context imprt body ctxt =
ballarin@28879
   538
  #1 (prep_context_statement (K I) cert_elems Element.activate_i true imprt body [] ctxt);
ballarin@28879
   539
ballarin@28795
   540
end;
ballarin@28795
   541
ballarin@28795
   542
ballarin@28795
   543
(** Dependencies **)
ballarin@28795
   544
ballarin@28795
   545
ballarin@28795
   546
ballarin@28795
   547
(*** Locale declarations ***)
ballarin@28795
   548
ballarin@28795
   549
local
ballarin@28795
   550
ballarin@28795
   551
(* introN: name of theorems for introduction rules of locale and
ballarin@28795
   552
     delta predicates;
ballarin@28795
   553
   axiomsN: name of theorem set with destruct rules for locale predicates,
ballarin@28795
   554
     also name suffix of delta predicates. *)
ballarin@28795
   555
ballarin@28795
   556
val introN = "intro";
ballarin@28795
   557
val axiomsN = "axioms";
ballarin@28795
   558
ballarin@28795
   559
fun atomize_spec thy ts =
ballarin@28795
   560
  let
ballarin@28795
   561
    val t = Logic.mk_conjunction_balanced ts;
ballarin@28795
   562
    val body = ObjectLogic.atomize_term thy t;
ballarin@28795
   563
    val bodyT = Term.fastype_of body;
ballarin@28795
   564
  in
ballarin@28795
   565
    if bodyT = propT then (t, propT, Thm.reflexive (Thm.cterm_of thy t))
ballarin@28795
   566
    else (body, bodyT, ObjectLogic.atomize (Thm.cterm_of thy t))
ballarin@28795
   567
  end;
ballarin@28795
   568
ballarin@28795
   569
(* achieve plain syntax for locale predicates (without "PROP") *)
ballarin@28795
   570
ballarin@28795
   571
fun aprop_tr' n c = (Syntax.constN ^ c, fn ctxt => fn args =>
ballarin@28795
   572
  if length args = n then
ballarin@28795
   573
    Syntax.const "_aprop" $
ballarin@28795
   574
      Term.list_comb (Syntax.free (Consts.extern (ProofContext.consts_of ctxt) c), args)
ballarin@28795
   575
  else raise Match);
ballarin@28795
   576
ballarin@28795
   577
(* CB: define one predicate including its intro rule and axioms
ballarin@28795
   578
   - bname: predicate name
ballarin@28795
   579
   - parms: locale parameters
ballarin@28795
   580
   - defs: thms representing substitutions from defines elements
ballarin@28795
   581
   - ts: terms representing locale assumptions (not normalised wrt. defs)
ballarin@28795
   582
   - norm_ts: terms representing locale assumptions (normalised wrt. defs)
ballarin@28795
   583
   - thy: the theory
ballarin@28795
   584
*)
ballarin@28795
   585
ballarin@28795
   586
fun def_pred bname parms defs ts norm_ts thy =
ballarin@28795
   587
  let
ballarin@28795
   588
    val name = Sign.full_name thy bname;
ballarin@28795
   589
ballarin@28795
   590
    val (body, bodyT, body_eq) = atomize_spec thy norm_ts;
ballarin@28795
   591
    val env = Term.add_term_free_names (body, []);
ballarin@28795
   592
    val xs = filter (member (op =) env o #1) parms;
ballarin@28795
   593
    val Ts = map #2 xs;
ballarin@28795
   594
    val extraTs = (Term.term_tfrees body \\ fold Term.add_tfreesT Ts [])
ballarin@28795
   595
      |> Library.sort_wrt #1 |> map TFree;
ballarin@28795
   596
    val predT = map Term.itselfT extraTs ---> Ts ---> bodyT;
ballarin@28795
   597
ballarin@28795
   598
    val args = map Logic.mk_type extraTs @ map Free xs;
ballarin@28795
   599
    val head = Term.list_comb (Const (name, predT), args);
ballarin@28795
   600
    val statement = ObjectLogic.ensure_propT thy head;
ballarin@28795
   601
ballarin@28795
   602
    val ([pred_def], defs_thy) =
ballarin@28795
   603
      thy
ballarin@28795
   604
      |> bodyT = propT ? Sign.add_advanced_trfuns ([], [], [aprop_tr' (length args) name], [])
ballarin@28795
   605
      |> Sign.declare_const [] ((Name.binding bname, predT), NoSyn) |> snd
ballarin@28795
   606
      |> PureThy.add_defs false
ballarin@28795
   607
        [((Thm.def_name bname, Logic.mk_equals (head, body)), [Thm.kind_internal])];
ballarin@28795
   608
    val defs_ctxt = ProofContext.init defs_thy |> Variable.declare_term head;
ballarin@28795
   609
ballarin@28795
   610
    val cert = Thm.cterm_of defs_thy;
ballarin@28795
   611
ballarin@28795
   612
    val intro = Goal.prove_global defs_thy [] norm_ts statement (fn _ =>
ballarin@28795
   613
      MetaSimplifier.rewrite_goals_tac [pred_def] THEN
ballarin@28795
   614
      Tactic.compose_tac (false, body_eq RS Drule.equal_elim_rule1, 1) 1 THEN
ballarin@28795
   615
      Tactic.compose_tac (false,
ballarin@28795
   616
        Conjunction.intr_balanced (map (Thm.assume o cert) norm_ts), 0) 1);
ballarin@28795
   617
ballarin@28795
   618
    val conjuncts =
ballarin@28795
   619
      (Drule.equal_elim_rule2 OF [body_eq,
ballarin@28795
   620
        MetaSimplifier.rewrite_rule [pred_def] (Thm.assume (cert statement))])
ballarin@28795
   621
      |> Conjunction.elim_balanced (length ts);
ballarin@28795
   622
    val axioms = ts ~~ conjuncts |> map (fn (t, ax) =>
ballarin@28795
   623
      Element.prove_witness defs_ctxt t
ballarin@28795
   624
       (MetaSimplifier.rewrite_goals_tac defs THEN
ballarin@28795
   625
        Tactic.compose_tac (false, ax, 0) 1));
ballarin@28795
   626
  in ((statement, intro, axioms), defs_thy) end;
ballarin@28795
   627
ballarin@28795
   628
in
ballarin@28795
   629
ballarin@28795
   630
(* CB: main predicate definition function *)
ballarin@28795
   631
ballarin@28795
   632
fun define_preds pname (parms, ((exts, exts'), (ints, ints')), defs) thy =
ballarin@28795
   633
  let
ballarin@28795
   634
    val (a_pred, a_intro, a_axioms, thy'') =
ballarin@28795
   635
      if null exts then (NONE, NONE, [], thy)
ballarin@28795
   636
      else
ballarin@28795
   637
        let
ballarin@28795
   638
          val aname = if null ints then pname else pname ^ "_" ^ axiomsN;
ballarin@28795
   639
          val ((statement, intro, axioms), thy') =
ballarin@28795
   640
            thy
ballarin@28795
   641
            |> def_pred aname parms defs exts exts';
ballarin@28795
   642
          val (_, thy'') =
ballarin@28795
   643
            thy'
ballarin@28795
   644
            |> Sign.add_path aname
ballarin@28795
   645
            |> Sign.no_base_names
ballarin@28795
   646
            |> PureThy.note_thmss Thm.internalK [((Name.binding introN, []), [([intro], [])])]
ballarin@28795
   647
            ||> Sign.restore_naming thy';
ballarin@28795
   648
          in (SOME statement, SOME intro, axioms, thy'') end;
ballarin@28795
   649
    val (b_pred, b_intro, b_axioms, thy'''') =
ballarin@28795
   650
      if null ints then (NONE, NONE, [], thy'')
ballarin@28795
   651
      else
ballarin@28795
   652
        let
ballarin@28795
   653
          val ((statement, intro, axioms), thy''') =
ballarin@28795
   654
            thy''
ballarin@28795
   655
            |> def_pred pname parms defs (ints @ the_list a_pred) (ints' @ the_list a_pred);
ballarin@28795
   656
          val (_, thy'''') =
ballarin@28795
   657
            thy'''
ballarin@28795
   658
            |> Sign.add_path pname
ballarin@28795
   659
            |> Sign.no_base_names
ballarin@28795
   660
            |> PureThy.note_thmss Thm.internalK
ballarin@28795
   661
                 [((Name.binding introN, []), [([intro], [])]),
ballarin@28795
   662
                  ((Name.binding axiomsN, []),
ballarin@28795
   663
                    [(map (Drule.standard o Element.conclude_witness) axioms, [])])]
ballarin@28795
   664
            ||> Sign.restore_naming thy''';
ballarin@28795
   665
        in (SOME statement, SOME intro, axioms, thy'''') end;
ballarin@28795
   666
  in ((a_pred, a_intro, a_axioms), (b_pred, b_intro, b_axioms), thy'''') end;
ballarin@28795
   667
ballarin@28795
   668
end;
ballarin@28795
   669
ballarin@28795
   670
ballarin@28795
   671
local
ballarin@28795
   672
ballarin@28795
   673
fun assumes_to_notes (Assumes asms) axms =
ballarin@28795
   674
      fold_map (fn (a, spec) => fn axs =>
ballarin@28795
   675
          let val (ps, qs) = chop (length spec) axs
ballarin@28795
   676
          in ((a, [(ps, [])]), qs) end) asms axms
ballarin@28795
   677
      |> apfst (curry Notes Thm.assumptionK)
ballarin@28795
   678
  | assumes_to_notes e axms = (e, axms);
ballarin@28795
   679
ballarin@28795
   680
fun defines_to_notes thy (Defines defs) defns =
ballarin@28795
   681
    let
ballarin@28795
   682
      val defs' = map (fn (_, (def, _)) => def) defs
ballarin@28795
   683
      val notes = map (fn (a, (def, _)) =>
ballarin@28795
   684
        (a, [([assume (cterm_of thy def)], [])])) defs
ballarin@28795
   685
    in
ballarin@28795
   686
      (Notes (Thm.definitionK, notes), defns @ defs')
ballarin@28795
   687
    end
ballarin@28795
   688
  | defines_to_notes _ e defns = (e, defns);
ballarin@28795
   689
ballarin@28872
   690
fun gen_add_locale prep_context
ballarin@28795
   691
    bname predicate_name raw_imprt raw_body thy =
ballarin@28795
   692
  let
ballarin@28795
   693
    val thy_ctxt = ProofContext.init thy;
ballarin@28795
   694
    val name = Sign.full_name thy bname;
ballarin@28795
   695
    val _ = NewLocale.test_locale thy name andalso
ballarin@28795
   696
      error ("Duplicate definition of locale " ^ quote name);
ballarin@28795
   697
ballarin@28872
   698
    val ((fors, deps), (_, body_elems), text as (parms, ((_, exts'), _), defs)) =
ballarin@28872
   699
      prep_context raw_imprt raw_body thy_ctxt;
ballarin@28872
   700
    val ((a_statement, a_intro, a_axioms), (b_statement, b_intro, b_axioms), thy') =
ballarin@28795
   701
      define_preds predicate_name text thy;
ballarin@28795
   702
ballarin@28795
   703
    val extraTs = fold Term.add_tfrees exts' [] \\ fold Term.add_tfreesT (map snd parms) [];
ballarin@28795
   704
    val _ = if null extraTs then ()
ballarin@28795
   705
      else warning ("Additional type variable(s) in locale specification " ^ quote bname);
ballarin@28795
   706
ballarin@28872
   707
    val satisfy = Element.satisfy_morphism b_axioms;
ballarin@28872
   708
    val params = fors @
ballarin@28872
   709
      (body_elems |> map_filter (fn Fixes fixes => SOME fixes | _ => NONE) |> flat);
ballarin@28795
   710
    val (body_elems', defns) = fold_map (defines_to_notes thy) body_elems [];
ballarin@28795
   711
ballarin@28795
   712
    val notes = body_elems' |>
ballarin@28872
   713
      (fn elems => fold_map assumes_to_notes elems (map Element.conclude_witness a_axioms)) |>
ballarin@28872
   714
      fst |> map (Element.morph_ctxt satisfy) |>
ballarin@28795
   715
      map_filter (fn Notes notes => SOME notes | _ => NONE);
ballarin@28795
   716
ballarin@28872
   717
    val deps' = map (fn (l, morph) => (l, morph $> satisfy)) deps;
ballarin@28872
   718
ballarin@28795
   719
    val loc_ctxt = thy' |>
ballarin@28872
   720
      NewLocale.register_locale name (extraTs, params)
ballarin@28872
   721
        (if is_some b_statement then b_statement else a_statement, map prop_of defs) ([], [])
ballarin@28872
   722
        (map (fn n => (n, stamp ())) notes |> rev) (map (fn d => (d, stamp ())) deps' |> rev) |>
ballarin@28795
   723
      NewLocale.init name
ballarin@28795
   724
  in (name, loc_ctxt) end;
ballarin@28795
   725
ballarin@28795
   726
in
ballarin@28795
   727
ballarin@28795
   728
val add_locale = gen_add_locale read_context;
ballarin@28885
   729
val add_locale_i = gen_add_locale cert_context;
ballarin@28795
   730
ballarin@28795
   731
end;
ballarin@28795
   732
ballarin@28795
   733
end;