src/Pure/Isar/theory_target.ML
author haftmann
Thu, 04 Oct 2007 19:41:52 +0200
changeset 24838 1d1bddf87353
parent 24818 d07e56a9a0c2
child 24848 5dbbd33c3236
permissions -rw-r--r--
put declarations first
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/theory_target.ML
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     4
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
     5
Common theory/locale targets.
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     6
*)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     7
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     8
signature THEORY_TARGET =
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     9
sig
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    10
  val peek: local_theory -> string option
22353
haftmann
parents: 22301
diff changeset
    11
  val begin: bstring -> Proof.context -> local_theory
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    12
  val init: xstring option -> theory -> local_theory
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    13
  val init_i: string option -> theory -> local_theory
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    14
end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    15
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    16
structure TheoryTarget: THEORY_TARGET =
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    17
struct
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    18
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    19
(** locale targets **)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    20
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    21
(* context data *)
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    22
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    23
structure Data = ProofDataFun
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    24
(
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    25
  type T = string option;
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    26
  fun init _ = NONE;
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    27
);
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    28
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    29
val peek = Data.get;
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    30
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    31
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    32
(* pretty *)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    33
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    34
fun pretty loc ctxt =
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    35
  let
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    36
    val thy = ProofContext.theory_of ctxt;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    37
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
21585
2444f1d1127b Assumption.assms_of: cterm;
wenzelm
parents: 21570
diff changeset
    38
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    39
    val elems =
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    40
      (if null fixes then [] else [Element.Fixes fixes]) @
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    41
      (if null assumes then [] else [Element.Assumes assumes]);
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    42
  in
20984
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    43
    if loc = "" then
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    44
      [Pretty.block
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    45
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    46
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    47
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    48
    else
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    49
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
d09f388fa9db exit: pass interactive flag;
wenzelm
parents: 20962
diff changeset
    50
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    51
  end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    52
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    53
24838
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    54
(* target declarations *)
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    55
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    56
fun target_morphism loc lthy =
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    57
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    58
  Morphism.thm_morphism Goal.norm_result;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    59
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    60
fun target_decl add loc d lthy =
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    61
  let
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    62
    val d' = Morphism.transform (target_morphism loc lthy) d;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    63
    val d0 = Morphism.form d';
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    64
  in
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    65
    if loc = "" then
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    66
      lthy
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    67
      |> LocalTheory.theory (Context.theory_map d0)
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    68
      |> LocalTheory.target (Context.proof_map d0)
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    69
    else
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    70
      lthy
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    71
      |> LocalTheory.target (add loc d')
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    72
  end;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    73
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    74
val type_syntax = target_decl Locale.add_type_syntax;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    75
val term_syntax = target_decl Locale.add_term_syntax;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    76
val declaration = target_decl Locale.add_declaration;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    77
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    78
fun target_naming loc lthy =
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    79
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    80
   else ProofContext.naming_of (LocalTheory.target_of lthy))
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    81
  |> NameSpace.qualified_names;
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    82
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
    83
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
    84
(* consts *)
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
    85
23205
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    86
fun target_abbrev prmode ((c, mx), rhs) phi =
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    87
  let
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    88
    val c' = Morphism.name phi c;
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    89
    val rhs' = Morphism.term phi rhs;
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    90
    val arg' = (c', rhs');
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    91
    val eq_heads =
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    92
      (case pairself Term.head_of (rhs, rhs') of
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    93
        (Const (a, _), Const (a', _)) => a = a'
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    94
      | _ => false);
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    95
  in
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    96
    eq_heads ? (Context.mapping_result
24779
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
    97
      (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
23205
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    98
    #-> (fn (lhs, _) =>
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
    99
      Term.equiv_types (rhs, rhs') ?
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   100
        Morphism.form (ProofContext.target_notation prmode [(lhs, mx)])))
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   101
  end;
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   102
24779
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   103
fun internal_abbrev prmode ((c, mx), t) lthy = lthy
22380
7635e59e3125 abbrev: be permissive after transformation;
wenzelm
parents: 22353
diff changeset
   104
  (* FIXME really permissive *)
24779
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   105
  |> LocalTheory.term_syntax (perhaps o try o target_abbrev prmode ((c, mx), t))
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   106
  |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   107
  |> snd;
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   108
22353
haftmann
parents: 22301
diff changeset
   109
fun consts is_loc some_class depends decls lthy =
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   110
  let
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   111
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   112
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   113
    fun const ((c, T), mx) thy =
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   114
      let
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   115
        val U = map #2 xs ---> T;
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   116
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents: 23205
diff changeset
   117
        val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
24779
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   118
        val thy' = Sign.add_consts_authentic (ContextPosition.properties_of lthy) [(c, U, mx1)] thy;
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   119
      in (((c, mx2), t), thy') end;
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   120
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   121
    fun const_class (SOME class) ((c, _), mx) (_, t) =
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents: 23205
diff changeset
   122
          Class.add_const_in_class class ((c, t), mx)
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   123
      | const_class NONE _ _ = I;
24657
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   124
    fun hide_abbrev (SOME class) abbrs thy =
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   125
          let
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   126
            val raw_cs = map (fst o fst) abbrs;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   127
            val prfx = (Logic.const_of_class o NameSpace.base) class;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   128
            fun mk_name c =
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   129
              let
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   130
                val n1 = Sign.full_name thy c;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   131
                val n2 = NameSpace.qualifier n1;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   132
                val n3 = NameSpace.base n1;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   133
              in NameSpace.implode [n2, prfx, prfx, n3] end;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   134
            val cs = map mk_name raw_cs;
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   135
          in
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   136
            Sign.hide_consts_i true cs thy
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   137
          end
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   138
      | hide_abbrev NONE _ thy = thy;
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   139
    val (abbrs, lthy') = lthy
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   140
      |> LocalTheory.theory_result (fold_map const decls)
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   141
    val defs = map (apsnd (pair ("", []))) abbrs;
24818
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   142
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   143
  in
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   144
    lthy'
24657
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   145
    |> LocalTheory.raw_theory (fold2 (const_class some_class) decls abbrs)
21825
6f6bf23f4c1e internal_abbrev: observe print mode;
wenzelm
parents: 21808
diff changeset
   146
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
24657
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   147
    |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   148
        (*FIXME abbreviations should never occur*)
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   149
    |> LocalDefs.add_defs defs
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   150
    |>> map (apsnd snd)
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   151
  end;
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   152
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   153
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   154
(* abbrev *)
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   155
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   156
fun occ_params ctxt ts =
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   157
  #1 (ProofContext.inferred_fixes ctxt)
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   158
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   159
23205
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   160
fun local_abbrev (x, rhs) =
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   161
  Variable.add_fixes [x] #-> (fn [x'] =>
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   162
    let
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   163
      val T = Term.fastype_of rhs;
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   164
      val lhs = Free (x', T);
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   165
    in
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   166
      Variable.declare_term lhs #>
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   167
      Variable.declare_term rhs #>
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   168
      Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   169
      pair (lhs, rhs)
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   170
    end);
b12f1c03cc9a added target/local_abbrev (from proof_context.ML);
wenzelm
parents: 23087
diff changeset
   171
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   172
fun abbrev is_loc some_class prmode ((raw_c, mx), raw_t) lthy =
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   173
  let
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   174
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   175
    val target = LocalTheory.target_of lthy;
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   176
    val target_morphism = LocalTheory.target_morphism lthy;
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   177
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   178
    val c = Morphism.name target_morphism raw_c;
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   179
    val t = Morphism.term target_morphism raw_t;
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   180
    val xs = map Free (occ_params target [t]);
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   181
    val u = fold_rev Term.lambda xs t;
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   182
    val U = Term.fastype_of u;
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   183
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents: 23205
diff changeset
   184
    val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   185
  in
24838
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   186
    lthy
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   187
    |> LocalTheory.theory_result
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   188
        (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   189
    |-> (fn (lhs as Const (full_c, _), rhs) => LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   190
    #> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
1d1bddf87353 put declarations first
haftmann
parents: 24818
diff changeset
   191
    #> local_abbrev (c, rhs))
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   192
  end;
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   193
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   194
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   195
(* defs *)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   196
21845
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   197
local
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   198
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   199
fun expand_term ctxt t =
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   200
  let
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   201
    val thy = ProofContext.theory_of ctxt;
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   202
    val thy_ctxt = ProofContext.init thy;
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   203
    val ct = Thm.cterm_of thy t;
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   204
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
21708
45e7491bea47 reorganized structure Tactic vs. MetaSimplifier;
wenzelm
parents: 21691
diff changeset
   205
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   206
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   207
fun add_def (name, prop) =
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   208
  Theory.add_defs_i false false [(name, prop)] #>
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   209
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   210
24818
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   211
fun add_def_new (name, prop) thy =  (* FIXME inactive --- breaks codegen *)
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   212
  let
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   213
    val tfrees = rev (map TFree (Term.add_tfrees prop []));
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   214
    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context "'a" (length tfrees));
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   215
    val strip_sorts = tfrees ~~ tfrees';
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   216
    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   217
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   218
    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   219
    val thy' = Theory.add_defs_i false false [(name, prop')] thy;
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   220
    val axm = Thm.get_axiom_i thy' (Sign.full_name thy' name);
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   221
    val def = Thm.instantiate (recover_sorts, []) axm;
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   222
  in (Drule.unvarify def, thy') end;
d07e56a9a0c2 added add_defs_new, which strips sorts for axioms (presently inactive);
wenzelm
parents: 24779
diff changeset
   223
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   224
in
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   225
24303
32b67bdf2c3a dropped junk
haftmann
parents: 24218
diff changeset
   226
fun defs kind args lthy0 =
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   227
  let
21533
bada5ac1216a simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents: 21498
diff changeset
   228
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   229
      let
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   230
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   231
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   232
        val T = Term.fastype_of rhs;
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   233
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   234
        val ([(lhs, lhs_def)], lthy2) = lthy1
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   235
          |> LocalTheory.consts (member (op =) xs) [((c, T), mx)];
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   236
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
21533
bada5ac1216a simplified consts: no auxiliary params, sane mixfix syntax;
wenzelm
parents: 21498
diff changeset
   237
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   238
        val name' = Thm.def_name_optional c name;
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   239
        val (def, lthy3) = lthy2
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   240
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
21845
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   241
        val eq = LocalDefs.trans_terms lthy3
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   242
          [(*c == loc.c xs*) lhs_def,
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   243
           (*lhs' == rhs'*)  def,
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   244
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
23087
ad7244663431 rudimentary class target implementation
haftmann
parents: 22846
diff changeset
   245
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   246
21570
f20f9304ab48 simplified '?' operator;
wenzelm
parents: 21533
diff changeset
   247
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
21433
89104dca628e LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21293
diff changeset
   248
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
22424
8a5412121687 *** empty log message ***
haftmann
parents: 22380
diff changeset
   249
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   250
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   251
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   252
end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   253
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   254
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   255
(* axioms *)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   256
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   257
local
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   258
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   259
fun add_axiom hyps (name, prop) thy =
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   260
  let
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   261
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   262
    val prop' = Logic.list_implies (hyps, prop);
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   263
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   264
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   265
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   266
  in (Drule.implies_elim_list axm prems, thy') end;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   267
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   268
in
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   269
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   270
fun axioms kind specs lthy =
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   271
  let
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   272
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   273
    fun axiom ((name, atts), props) thy = thy
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   274
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   275
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   276
  in
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   277
    lthy
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   278
    |> fold (fold Variable.declare_term o snd) specs
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   279
    |> LocalTheory.theory_result (fold_map axiom specs)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   280
    |-> LocalTheory.notes kind
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   281
  end;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   282
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   283
end;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   284
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   285
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   286
(* notes *)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   287
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   288
fun import_export_proof ctxt (name, raw_th) =
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   289
  let
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   290
    val thy = ProofContext.theory_of ctxt;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   291
    val thy_ctxt = ProofContext.init thy;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   292
    val certT = Thm.ctyp_of thy;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   293
    val cert = Thm.cterm_of thy;
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   294
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   295
    (*export assumes/defines*)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   296
    val th = Goal.norm_result raw_th;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   297
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
21708
45e7491bea47 reorganized structure Tactic vs. MetaSimplifier;
wenzelm
parents: 21691
diff changeset
   298
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
45e7491bea47 reorganized structure Tactic vs. MetaSimplifier;
wenzelm
parents: 21691
diff changeset
   299
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   300
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   301
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   302
    (*export fixes*)
22692
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22673
diff changeset
   303
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
1e057a3f087d proper ProofContext.infer_types;
wenzelm
parents: 22673
diff changeset
   304
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   305
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   306
      |> Variable.export ctxt thy_ctxt
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   307
      |> Drule.zero_var_indexes_list;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   308
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   309
    (*thm definition*)
21644
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   310
    val result = th''
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   311
      |> PureThy.name_thm true true ""
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   312
      |> Goal.close_result
24779
2949fb459c7b keep context position as tags for consts/thms;
wenzelm
parents: 24748
diff changeset
   313
      |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
21644
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   314
      |> PureThy.name_thm true true name;
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   315
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   316
    (*import fixes*)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   317
    val (tvars, vars) =
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   318
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   319
      |>> map Logic.dest_type;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   320
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   321
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   322
    val inst = filter (is_Var o fst) (vars ~~ frees);
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   323
    val cinstT = map (pairself certT o apfst TVar) instT;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   324
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   325
    val result' = Thm.instantiate (cinstT, cinst) result;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   326
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   327
    (*import assumes/defines*)
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   328
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   329
    val result'' =
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   330
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   331
        NONE => raise THM ("Failed to re-import result", 0, [result'])
21845
da545169fe06 defs/notes: more robust transitivity reasoning;
wenzelm
parents: 21825
diff changeset
   332
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
21644
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   333
      |> Goal.norm_result
5154a7213d3c notes: added non-official name;
wenzelm
parents: 21615
diff changeset
   334
      |> PureThy.name_thm false false name;
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   335
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   336
  in (result'', result) end;
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   337
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   338
fun notes loc kind facts lthy =
21585
2444f1d1127b Assumption.assms_of: cterm;
wenzelm
parents: 21570
diff changeset
   339
  let
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   340
    val is_loc = loc <> "";
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   341
    val thy = ProofContext.theory_of lthy;
21615
1bd558879c44 notes: proper naming of thm proof, activated import_export_proof;
wenzelm
parents: 21613
diff changeset
   342
    val full = LocalTheory.full_name lthy;
21585
2444f1d1127b Assumption.assms_of: cterm;
wenzelm
parents: 21570
diff changeset
   343
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   344
    val facts' = facts
21615
1bd558879c44 notes: proper naming of thm proof, activated import_export_proof;
wenzelm
parents: 21613
diff changeset
   345
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
1bd558879c44 notes: proper naming of thm proof, activated import_export_proof;
wenzelm
parents: 21613
diff changeset
   346
      |> PureThy.map_facts (import_export_proof lthy);
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   347
    val local_facts = PureThy.map_facts #1 facts'
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   348
      |> Attrib.map_facts (Attrib.attribute_i thy);
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   349
    val target_facts = PureThy.map_facts #1 facts'
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   350
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   351
    val global_facts = PureThy.map_facts #2 facts'
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   352
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   353
  in
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   354
    lthy |> LocalTheory.theory
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   355
      (Sign.qualified_names
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   356
        #> PureThy.note_thmss_i kind global_facts #> snd
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   357
        #> Sign.restore_naming thy)
21585
2444f1d1127b Assumption.assms_of: cterm;
wenzelm
parents: 21570
diff changeset
   358
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   359
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
21594
2859c94d67d4 reworked notes: towards proper import/export of proof terms;
wenzelm
parents: 21585
diff changeset
   360
21433
89104dca628e LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21293
diff changeset
   361
    |> ProofContext.set_stmt true
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   362
    |> ProofContext.qualified_names
21611
fc95ff1fe738 notes: proper import/export of proofs (still inactive);
wenzelm
parents: 21594
diff changeset
   363
    |> ProofContext.note_thmss_i kind local_facts
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   364
    ||> ProofContext.restore_naming lthy
24389
9ddef2b1118a ProofContext.restore_mode;
wenzelm
parents: 24303
diff changeset
   365
    ||> ProofContext.restore_mode lthy
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   366
  end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   367
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   368
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   369
(* init and exit *)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   370
22353
haftmann
parents: 22301
diff changeset
   371
fun begin loc ctxt =
haftmann
parents: 22301
diff changeset
   372
  let
haftmann
parents: 22301
diff changeset
   373
    val thy = ProofContext.theory_of ctxt;
haftmann
parents: 22301
diff changeset
   374
    val is_loc = loc <> "";
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents: 23205
diff changeset
   375
    val some_class = Class.class_of_locale thy loc;
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   376
    fun class_init (SOME class) =
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   377
          Class.init [class]
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   378
      | class_init NONE =
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   379
          I;
22353
haftmann
parents: 22301
diff changeset
   380
  in
haftmann
parents: 22301
diff changeset
   381
    ctxt
haftmann
parents: 22301
diff changeset
   382
    |> Data.put (if is_loc then SOME loc else NONE)
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   383
    |> class_init some_class
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   384
    |> LocalTheory.init (NameSpace.base loc)
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   385
     {pretty = pretty loc,
22353
haftmann
parents: 22301
diff changeset
   386
      consts = consts is_loc some_class,
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   387
      axioms = axioms,
22353
haftmann
parents: 22301
diff changeset
   388
      abbrev = abbrev is_loc some_class,
24303
32b67bdf2c3a dropped junk
haftmann
parents: 24218
diff changeset
   389
      defs = defs,
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   390
      notes = notes loc,
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   391
      type_syntax = type_syntax loc,
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   392
      term_syntax = term_syntax loc,
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   393
      declaration = declaration loc,
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   394
      target_morphism = target_morphism loc,
22203
efc0cdc01307 added interface for target_naming;
wenzelm
parents: 21845
diff changeset
   395
      target_naming = target_naming loc,
21804
515499542d84 removed is_class -- handled internally;
wenzelm
parents: 21761
diff changeset
   396
      reinit = fn _ =>
24657
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   397
        (if is_loc then Locale.init loc else ProofContext.init)
185502d54c3d fixed wrong syntax treatment in class target
haftmann
parents: 24389
diff changeset
   398
        #> begin loc,
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24657
diff changeset
   399
      exit = LocalTheory.target_of }
21747
d650305c609a added is_class (approximation);
wenzelm
parents: 21718
diff changeset
   400
  end;
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   401
22353
haftmann
parents: 22301
diff changeset
   402
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann
parents: 22301
diff changeset
   403
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   404
21276
2285cf5a7560 init: '-' refers to global context;
wenzelm
parents: 21037
diff changeset
   405
fun init (SOME "-") thy = init_i NONE thy
2285cf5a7560 init: '-' refers to global context;
wenzelm
parents: 21037
diff changeset
   406
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   407
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   408
end;