| author | wenzelm | 
| Wed, 29 Jun 2005 15:13:38 +0200 | |
| changeset 16608 | 4f8d7b83c7e2 | 
| parent 15979 | c81578ac2d31 | 
| child 17065 | c1cd17010a1b | 
| permissions | -rw-r--r-- | 
| 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 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 5 | Standard constant definitions, with type-inference and optional | 
| 
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 | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 12 | val add_constdefs: (string list * string option) list * | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 13 | ((bstring * string option * Syntax.mixfix) option * | 
| 15703 | 14 | ((bstring * Attrib.src list) * string)) list | 
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 15 | -> theory -> theory | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 16 | val add_constdefs_i: (string list * typ option) list * | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 17 | ((bstring * typ option * Syntax.mixfix) option * | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 18 | ((bstring * theory attribute list) * term)) list | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 19 | -> theory -> theory | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 20 | end; | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 21 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 22 | structure Constdefs: CONSTDEFS = | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 23 | struct | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 24 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 25 | (** add_constdefs(_i) **) | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 26 | |
| 14664 | 27 | fun pretty_const sg (c, T) = | 
| 28 | Pretty.block [Pretty.str c, Pretty.str " ::", Pretty.brk 1, | |
| 29 | Pretty.quote (Sign.pretty_typ sg T)]; | |
| 30 | ||
| 31 | fun pretty_constdefs sg decls = | |
| 32 | Pretty.big_list "constants" (map (pretty_const sg) decls); | |
| 33 | ||
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 34 | fun gen_constdef prep_typ prep_term prep_att | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 35 | structs (thy, (decl, ((raw_name, raw_atts), raw_prop))) = | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 36 | let | 
| 14664 | 37 | val sign = Theory.sign_of thy; | 
| 38 | fun err msg ts = | |
| 39 | error (cat_lines (msg :: map (Sign.string_of_term sign) ts)); | |
| 40 | ||
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 41 | val ctxt = | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 42 | ProofContext.init thy |> ProofContext.add_fixes | 
| 15570 | 43 | (List.concat (map (fn (As, T) => map (fn A => (A, T, NONE)) As) structs)); | 
| 14719 | 44 | val (ctxt', d, mx) = | 
| 15531 | 45 | (case decl of NONE => (ctxt, NONE, Syntax.NoSyn) | SOME (x, raw_T, mx) => | 
| 14719 | 46 | let | 
| 47 | val x' = Syntax.const_name x mx and mx' = Syntax.fix_mixfix x mx; | |
| 15570 | 48 | val T = Option.map (prep_typ ctxt) raw_T; | 
| 15531 | 49 | 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 | 50 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 51 | val prop = prep_term ctxt' raw_prop; | 
| 14664 | 52 | val concl = Logic.strip_imp_concl prop; | 
| 53 | val (lhs, _) = Logic.dest_equals concl handle TERM _ => | |
| 54 | err "Not a meta-equality (==):" [concl]; | |
| 55 | val head = Term.head_of lhs; | |
| 56 | val (c, T) = Term.dest_Free head handle TERM _ => | |
| 57 | err "Locally fixed variable required as head of definition:" [head]; | |
| 15531 | 58 | val _ = (case d of NONE => () | SOME c' => | 
| 14719 | 59 | if c <> c' then | 
| 60 |         err ("Head of definition " ^ quote c ^ " differs from declaration " ^ quote c') []
 | |
| 14664 | 61 | else ()); | 
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 62 | |
| 14664 | 63 | 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 | 64 | 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 | 65 | val atts = map (prep_att thy) raw_atts; | 
| 14664 | 66 | |
| 67 | val thy' = | |
| 68 | thy | |
| 69 | |> Theory.add_consts_i [(c, T, mx)] | |
| 70 | |> PureThy.add_defs_i false [((name, def), atts)] |> #1; | |
| 71 | in (thy', (c, T)) end; | |
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 72 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 73 | fun gen_constdefs prep_vars prep_typ prep_term prep_att (raw_structs, specs) thy = | 
| 14664 | 74 | let | 
| 75 | val structs = #2 (foldl_map prep_vars (ProofContext.init thy, raw_structs)); | |
| 76 | val (thy', decls) = (thy, specs) | |
| 77 | |> foldl_map (gen_constdef prep_typ prep_term prep_att structs); | |
| 78 | 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 | 79 | |
| 14719 | 80 | val add_constdefs = gen_constdefs ProofContext.read_vars_liberal | 
| 81 | ProofContext.read_typ ProofContext.read_term_liberal Attrib.global_attribute; | |
| 82 | val add_constdefs_i = gen_constdefs ProofContext.cert_vars_liberal | |
| 14657 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 83 | ProofContext.cert_typ ProofContext.cert_term (K I); | 
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 84 | |
| 
c7cc01735801
'constdefs' with automatic type-inference and structure context;
 wenzelm parents: diff
changeset | 85 | end; |