src/Pure/Isar/constdefs.ML
author obua
Sun, 09 May 2004 23:04:36 +0200
changeset 14722 8e739a6eaf11
parent 14719 d1157ff6ffcb
child 15010 72fbe711e414
permissions -rw-r--r--
replaced apply-style proof for instance Multiset :: plus_ac0 by recommended Isar proof style
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
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     5
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     6
Standard constant definitions, with type-inference and optional
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     7
structure context; specifications need to observe strictly sequential
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
     8
dependencies; no support for overloading.
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
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    11
signature CONSTDEFS =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    12
sig
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    13
  val add_constdefs: (string list * string option) list *
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    14
    ((bstring * string option * Syntax.mixfix) option *
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    15
      ((bstring * Args.src list) * string)) list
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    16
    -> theory -> theory
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    17
  val add_constdefs_i: (string list * typ option) list *
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    18
    ((bstring * typ option * Syntax.mixfix) option *
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    19
      ((bstring * theory attribute list) * term)) list
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    20
    -> theory -> theory
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    21
end;
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
structure Constdefs: CONSTDEFS =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    24
struct
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    25
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    26
(** add_constdefs(_i) **)
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    27
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    28
fun pretty_const sg (c, T) =
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    29
  Pretty.block [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    30
    Pretty.quote (Sign.pretty_typ sg T)];
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    31
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    32
fun pretty_constdefs sg decls =
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    33
  Pretty.big_list "constants" (map (pretty_const sg) decls);
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    34
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    35
fun gen_constdef prep_typ prep_term prep_att
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    36
    structs (thy, (decl, ((raw_name, raw_atts), raw_prop))) =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    37
  let
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    38
    val sign = Theory.sign_of thy;
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    39
    fun err msg ts =
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    40
      error (cat_lines (msg :: map (Sign.string_of_term sign) ts));
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    41
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    42
    val ctxt =
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    43
      ProofContext.init thy |> ProofContext.add_fixes
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    44
        (flat (map (fn (As, T) => map (fn A => (A, T, None)) As) structs));
14719
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    45
    val (ctxt', d, mx) =
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    46
      (case decl of None => (ctxt, None, Syntax.NoSyn) | Some (x, raw_T, mx) =>
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    47
        let
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    48
          val x' = Syntax.const_name x mx and mx' = Syntax.fix_mixfix x mx;
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    49
          val T = apsome (prep_typ ctxt) raw_T;
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    50
        in (ProofContext.add_fixes_liberal [(x', T, Some mx')] ctxt, Some x', mx') end);
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    51
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    52
    val prop = prep_term ctxt' raw_prop;
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    53
    val concl = Logic.strip_imp_concl prop;
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    54
    val (lhs, _) = Logic.dest_equals concl handle TERM _ =>
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    55
      err "Not a meta-equality (==):" [concl];
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    56
    val head = Term.head_of lhs;
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    57
    val (c, T) = Term.dest_Free head handle TERM _ =>
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    58
      err "Locally fixed variable required as head of definition:" [head];
14719
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    59
    val _ = (case d of None => () | Some c' =>
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    60
      if c <> c' then
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    61
        err ("Head of definition " ^ quote c ^ " differs from declaration " ^ quote c') []
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    62
      else ());
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    63
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    64
    val def = Term.subst_atomic [(Free (c, T), Const (Sign.full_name sign c, T))] prop;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    65
    val name = if raw_name = "" then Thm.def_name c else raw_name;
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    66
    val atts = map (prep_att thy) raw_atts;
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    67
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    68
    val thy' =
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    69
      thy
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    70
      |> Theory.add_consts_i [(c, T, mx)]
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    71
      |> PureThy.add_defs_i false [((name, def), atts)] |> #1;
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    72
  in (thy', (c, T)) end;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    73
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    74
fun gen_constdefs prep_vars prep_typ prep_term prep_att (raw_structs, specs) thy =
14664
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    75
  let
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    76
    val structs = #2 (foldl_map prep_vars (ProofContext.init thy, raw_structs));
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    77
    val (thy', decls) = (thy, specs)
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    78
      |> foldl_map (gen_constdef prep_typ prep_term prep_att structs);
148f6175fa78 improved messages;
wenzelm
parents: 14657
diff changeset
    79
  in Pretty.writeln (pretty_constdefs (Theory.sign_of thy') decls); thy' end;
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    80
14719
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    81
val add_constdefs = gen_constdefs ProofContext.read_vars_liberal
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    82
  ProofContext.read_typ ProofContext.read_term_liberal Attrib.global_attribute;
d1157ff6ffcb be liberal about constant names;
wenzelm
parents: 14664
diff changeset
    83
val add_constdefs_i = gen_constdefs ProofContext.cert_vars_liberal
14657
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    84
  ProofContext.cert_typ ProofContext.cert_term (K I);
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    85
c7cc01735801 'constdefs' with automatic type-inference and structure context;
wenzelm
parents:
diff changeset
    86
end;