author | nipkow |
Wed, 25 Jun 2025 14:16:30 +0200 | |
changeset 82735 | 5d0d35680311 |
parent 81933 | cb05f8d3fd05 |
permissions | -rw-r--r-- |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
1 |
(* Title: HOL/Import/import_data.ML |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
2 |
Author: Cezary Kaliszyk, University of Innsbruck |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
3 |
Author: Alexander Krauss, QAware GmbH |
81933 | 4 |
Author: Makarius |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
5 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
6 |
Importer data. |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
7 |
*) |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
8 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
9 |
signature IMPORT_DATA = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
10 |
sig |
81835 | 11 |
val get_const_map : theory -> string -> string option |
12 |
val get_typ_map : theory -> string -> string option |
|
13 |
val get_const_def : theory -> string -> thm option |
|
14 |
val get_typ_def : theory -> string -> thm option |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
15 |
val add_const_map : string -> string -> theory -> theory |
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
16 |
val add_const_map_cmd : string -> string -> theory -> theory |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
17 |
val add_typ_map : string -> string -> theory -> theory |
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
18 |
val add_typ_map_cmd : string -> string -> theory -> theory |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
19 |
val add_const_def : string -> thm -> string option -> theory -> theory |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
20 |
val add_typ_def : string -> string -> string -> thm -> theory -> theory |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
21 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
22 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
23 |
structure Import_Data: IMPORT_DATA = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
24 |
struct |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
25 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
26 |
structure Data = Theory_Data |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
27 |
( |
81862 | 28 |
type T = |
29 |
{const_map: string Symtab.table, ty_map: string Symtab.table, |
|
30 |
const_def: thm Symtab.table, ty_def: thm Symtab.table} |
|
31 |
val empty = |
|
32 |
{const_map = Symtab.empty, ty_map = Symtab.empty, |
|
33 |
const_def = Symtab.empty, ty_def = Symtab.empty} |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
34 |
fun merge |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
35 |
({const_map = cm1, ty_map = tm1, const_def = cd1, ty_def = td1}, |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
36 |
{const_map = cm2, ty_map = tm2, const_def = cd2, ty_def = td2}) : T = |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
37 |
{const_map = Symtab.merge (K true) (cm1, cm2), ty_map = Symtab.merge (K true) (tm1, tm2), |
81862 | 38 |
const_def = Symtab.merge (K true) (cd1, cd2), ty_def = Symtab.merge (K true) (td1, td2)} |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
39 |
) |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
40 |
|
81835 | 41 |
val get_const_map = Symtab.lookup o #const_map o Data.get |
42 |
val get_typ_map = Symtab.lookup o #ty_map o Data.get |
|
43 |
val get_const_def = Symtab.lookup o #const_def o Data.get |
|
44 |
val get_typ_def = Symtab.lookup o #ty_def o Data.get |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
45 |
|
81862 | 46 |
fun add_const_map c d = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
47 |
Data.map (fn {const_map, ty_map, const_def, ty_def} => |
81862 | 48 |
{const_map = Symtab.update (c, d) const_map, ty_map = ty_map, |
49 |
const_def = const_def, ty_def = ty_def}) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
50 |
|
81862 | 51 |
fun add_const_map_cmd c s thy = |
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
52 |
let |
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
53 |
val ctxt = Proof_Context.init_global thy |
81862 | 54 |
val d = dest_Const_name (Proof_Context.read_const {proper = true, strict = false} ctxt s) |
55 |
in add_const_map c d thy end |
|
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
56 |
|
81862 | 57 |
fun add_typ_map c d = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
58 |
Data.map (fn {const_map, ty_map, const_def, ty_def} => |
81862 | 59 |
{const_map = const_map, ty_map = (Symtab.update (c, d) ty_map), |
60 |
const_def = const_def, ty_def = ty_def}) |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
61 |
|
81862 | 62 |
fun add_typ_map_cmd c s thy = |
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
63 |
let |
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
64 |
val ctxt = Proof_Context.init_global thy |
81862 | 65 |
val d = dest_Type_name (Proof_Context.read_type_name {proper = true, strict = false} ctxt s) |
66 |
in add_typ_map c d thy end |
|
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
67 |
|
81862 | 68 |
fun add_const_def c th name_opt thy = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
69 |
let |
81862 | 70 |
val th' = Thm.legacy_freezeT th |
81864 | 71 |
val d = |
81862 | 72 |
(case name_opt of |
73 |
NONE => dest_Const_name (#1 (HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th')))) |
|
81864 | 74 |
| SOME d => d) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
75 |
in |
81862 | 76 |
thy |
81864 | 77 |
|> add_const_map c d |
81862 | 78 |
|> Data.map (fn {const_map, ty_map, const_def, ty_def} => |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
79 |
{const_map = const_map, ty_map = ty_map, |
81862 | 80 |
const_def = (Symtab.update (c, th') const_def), ty_def = ty_def}) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
81 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
82 |
|
81862 | 83 |
fun add_typ_def type_name abs_name rep_name th thy = |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
84 |
let |
81862 | 85 |
val th' = Thm.legacy_freezeT th |
81863 | 86 |
val \<^Const_>\<open>type_definition A _ for rep abs _\<close> = HOLogic.dest_Trueprop (Thm.prop_of th') |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
87 |
in |
81862 | 88 |
thy |
89 |
|> add_typ_map type_name (dest_Type_name A) |
|
90 |
|> add_const_map abs_name (dest_Const_name abs) |
|
91 |
|> add_const_map rep_name (dest_Const_name rep) |
|
92 |
|> Data.map (fn {const_map, ty_map, const_def, ty_def} => |
|
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
93 |
{const_map = const_map, ty_map = ty_map, |
81862 | 94 |
const_def = const_def, ty_def = (Symtab.update (type_name, th') ty_def)}) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
95 |
end |
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
96 |
|
81862 | 97 |
val _ = |
98 |
Theory.setup (Attrib.setup \<^binding>\<open>import_const\<close> |
|
56266
da5f22a60cb3
formal check of user input, avoiding direct references of interal names;
wenzelm
parents:
53172
diff
changeset
|
99 |
(Scan.lift Parse.name -- |
69597 | 100 |
Scan.option (Scan.lift \<^keyword>\<open>:\<close> |-- Args.const {proper = true, strict = false}) >> |
81905 | 101 |
(fn (c, d) => Thm.declaration_attribute |
102 |
(fn th => Context.mapping (add_const_def c th d) I))) |
|
81862 | 103 |
"declare a theorem as an equality that maps the given constant") |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
104 |
|
81862 | 105 |
val _ = |
106 |
Theory.setup (Attrib.setup \<^binding>\<open>import_type\<close> |
|
53172
31e24d6ff1ea
more standard parser combinator expressions and tool setup;
wenzelm
parents:
53171
diff
changeset
|
107 |
(Scan.lift (Parse.name -- Parse.name -- Parse.name) >> |
81905 | 108 |
(fn ((c, abs), rep) => Thm.declaration_attribute |
109 |
(fn th => Context.mapping (add_typ_def c abs rep th) I))) |
|
81862 | 110 |
"declare a type_definition theorem as a map for an imported type with abs and rep") |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
111 |
|
53172
31e24d6ff1ea
more standard parser combinator expressions and tool setup;
wenzelm
parents:
53171
diff
changeset
|
112 |
val _ = |
69597 | 113 |
Outer_Syntax.command \<^command_keyword>\<open>import_type_map\<close> |
53172
31e24d6ff1ea
more standard parser combinator expressions and tool setup;
wenzelm
parents:
53171
diff
changeset
|
114 |
"map external type name to existing Isabelle/HOL type name" |
69597 | 115 |
((Parse.name --| \<^keyword>\<open>:\<close>) -- Parse.type_const >> |
81862 | 116 |
(fn (c, d) => Toplevel.theory (add_typ_map_cmd c d))) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
117 |
|
53172
31e24d6ff1ea
more standard parser combinator expressions and tool setup;
wenzelm
parents:
53171
diff
changeset
|
118 |
val _ = |
69597 | 119 |
Outer_Syntax.command \<^command_keyword>\<open>import_const_map\<close> |
53172
31e24d6ff1ea
more standard parser combinator expressions and tool setup;
wenzelm
parents:
53171
diff
changeset
|
120 |
"map external const name to existing Isabelle/HOL const name" |
69597 | 121 |
((Parse.name --| \<^keyword>\<open>:\<close>) -- Parse.const >> |
81862 | 122 |
(fn (c, d) => Toplevel.theory (add_const_map_cmd c d))) |
47258
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
123 |
|
880e587eee9f
Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff
changeset
|
124 |
end |