src/Pure/Isar/constdefs.ML
author haftmann
Tue, 29 Jul 2008 08:15:40 +0200
changeset 27691 ce171cbd4b93
parent 26939 1035c89b4c02
child 28083 103d9282a946
permissions -rw-r--r--
PureThy: dropped note_thmss_qualified, dropped _i suffix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/constdefs.ML
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius, Hagia Maria Sion Abbey (Jerusalem)
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     4
18779
a9f41c380b2f tuned comment;
wenzelm
parents: 18765
diff changeset
     5
Old-style constant definitions, with type-inference and optional
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     6
structure context; specifications need to observe strictly sequential
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     7
dependencies; no support for overloading.
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     8
*)
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     9
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    10
signature CONSTDEFS =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    11
sig
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    12
  val add_constdefs: (string * string option) list *
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    13
    ((bstring * string option * mixfix) option * ((bstring * Attrib.src list) * string)) list ->
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    14
    theory -> theory
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    15
  val add_constdefs_i: (string * typ option) list *
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18668
diff changeset
    16
    ((bstring * typ option * mixfix) option * ((bstring * attribute list) * term)) list ->
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    17
    theory -> theory
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    18
end;
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    19
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    20
structure Constdefs: CONSTDEFS =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    21
struct
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    22
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    23
(** add_constdefs(_i) **)
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    24
22710
f44439cdce77 read prop as prop, not term;
wenzelm
parents: 20885
diff changeset
    25
fun gen_constdef prep_vars prep_prop prep_att
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    26
    structs (raw_decl, ((raw_name, raw_atts), raw_prop)) thy =
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    27
  let
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 25352
diff changeset
    28
    fun err msg ts = error (cat_lines (msg :: map (Syntax.string_of_term_global thy) ts));
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    29
18948
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    30
    val thy_ctxt = ProofContext.init thy;
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    31
    val struct_ctxt = #2 (ProofContext.add_fixes_i structs thy_ctxt);
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    32
    val ((d, mx), var_ctxt) =
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    33
      (case raw_decl of
18948
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    34
        NONE => ((NONE, NoSyn), struct_ctxt)
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    35
      | SOME raw_var =>
18948
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    36
          struct_ctxt |> prep_vars [raw_var] |-> (fn [(x, T, mx)] =>
25352
24d1568af397 discontinued legacy vars;
wenzelm
parents: 25327
diff changeset
    37
            ProofContext.add_fixes_i [(x, T, mx)] #> snd #> pair (SOME x, mx)));
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    38
22710
f44439cdce77 read prop as prop, not term;
wenzelm
parents: 20885
diff changeset
    39
    val prop = prep_prop var_ctxt raw_prop;
18948
b6b19cc8454f LocalDefs.cert_def;
wenzelm
parents: 18873
diff changeset
    40
    val (c, T) = #1 (LocalDefs.cert_def thy_ctxt (Logic.strip_imp_concl prop));
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15010
diff changeset
    41
    val _ = (case d of NONE => () | SOME c' =>
14719
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    42
      if c <> c' then
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    43
        err ("Head of definition " ^ quote c ^ " differs from declaration " ^ quote c') []
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    44
      else ());
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    45
17065
c1cd17010a1b replaced sign by thy;
wenzelm
parents: 15979
diff changeset
    46
    val def = Term.subst_atomic [(Free (c, T), Const (Sign.full_name thy c, T))] prop;
20885
e0223c1bd7e8 Thm.def_name_optional;
wenzelm
parents: 18948
diff changeset
    47
    val name = Thm.def_name_optional c raw_name;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    48
    val atts = map (prep_att thy) raw_atts;
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    49
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    50
    val thy' =
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    51
      thy
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22761
diff changeset
    52
      |> Sign.add_consts_i [(c, T, mx)]
27691
ce171cbd4b93 PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents: 26939
diff changeset
    53
      |> PureThy.add_defs false [((name, def), atts)]
24624
b8383b1bbae3 distinction between regular and default code theorems
haftmann
parents: 24219
diff changeset
    54
      |-> (fn [thm] => Code.add_default_func thm);
17853
wenzelm
parents: 17065
diff changeset
    55
  in ((c, T), thy') end;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    56
22710
f44439cdce77 read prop as prop, not term;
wenzelm
parents: 20885
diff changeset
    57
fun gen_constdefs prep_vars prep_prop prep_att (raw_structs, specs) thy =
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    58
  let
18638
e135f6a1b76c Specification.pretty_consts ctxt;
wenzelm
parents: 18615
diff changeset
    59
    val ctxt = ProofContext.init thy;
18668
4a15c09bd46a uniform handling of fixes;
wenzelm
parents: 18638
diff changeset
    60
    val (structs, _) = prep_vars (map (fn (x, T) => (x, T, Structure)) raw_structs) ctxt;
22710
f44439cdce77 read prop as prop, not term;
wenzelm
parents: 20885
diff changeset
    61
    val (decls, thy') = fold_map (gen_constdef prep_vars prep_prop prep_att structs) specs thy;
20885
e0223c1bd7e8 Thm.def_name_optional;
wenzelm
parents: 18948
diff changeset
    62
  in Pretty.writeln (ProofDisplay.pretty_consts ctxt (K true) decls); thy' end;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    63
25352
24d1568af397 discontinued legacy vars;
wenzelm
parents: 25327
diff changeset
    64
val add_constdefs = gen_constdefs ProofContext.read_vars Syntax.read_prop Attrib.attribute;
24d1568af397 discontinued legacy vars;
wenzelm
parents: 25327
diff changeset
    65
val add_constdefs_i = gen_constdefs ProofContext.cert_vars ProofContext.cert_prop (K I);
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    66
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    67
end;