| author | wenzelm | 
| Wed, 23 Mar 2022 12:21:13 +0100 | |
| changeset 75311 | 5960bae73afe | 
| parent 74375 | ba880f3a4e52 | 
| child 80760 | be8c0e039a5e | 
| permissions | -rw-r--r-- | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31031diff
changeset | 1 | (* Author: Florian Haftmann, TU Muenchen *) | 
| 26168 | 2 | |
| 60758 | 3 | section \<open>Reflecting Pure types into HOL\<close> | 
| 26168 | 4 | |
| 28952 
15a4b2cf8c34
made repository layout more coherent with logical distribution structure; stripped some $Id$s
 haftmann parents: 
28562diff
changeset | 5 | theory Typerep | 
| 51112 | 6 | imports String | 
| 26168 | 7 | begin | 
| 8 | ||
| 58310 | 9 | datatype typerep = Typerep String.literal "typerep list" | 
| 26168 | 10 | |
| 28335 | 11 | class typerep = | 
| 31031 | 12 | fixes typerep :: "'a itself \<Rightarrow> typerep" | 
| 26168 | 13 | begin | 
| 14 | ||
| 29574 | 15 | definition typerep_of :: "'a \<Rightarrow> typerep" where | 
| 28335 | 16 |   [simp]: "typerep_of x = typerep TYPE('a)"
 | 
| 26168 | 17 | |
| 18 | end | |
| 19 | ||
| 35115 | 20 | syntax | 
| 21 |   "_TYPEREP" :: "type => logic"  ("(1TYPEREP/(1'(_')))")
 | |
| 22 | ||
| 60758 | 23 | parse_translation \<open> | 
| 52143 | 24 | let | 
| 25 | fun typerep_tr (*"_TYPEREP"*) [ty] = | |
| 69593 | 26 | Syntax.const \<^const_syntax>\<open>typerep\<close> $ | 
| 27 | (Syntax.const \<^syntax_const>\<open>_constrain\<close> $ Syntax.const \<^const_syntax>\<open>Pure.type\<close> $ | |
| 28 | (Syntax.const \<^type_syntax>\<open>itself\<close> $ ty)) | |
| 52143 | 29 |       | typerep_tr (*"_TYPEREP"*) ts = raise TERM ("typerep_tr", ts);
 | 
| 69593 | 30 | in [(\<^syntax_const>\<open>_TYPEREP\<close>, K typerep_tr)] end | 
| 60758 | 31 | \<close> | 
| 35115 | 32 | |
| 60758 | 33 | typed_print_translation \<open> | 
| 52143 | 34 | let | 
| 74375 | 35 | fun typerep_tr' ctxt (*"typerep"*) \<^Type>\<open>fun \<^Type>\<open>itself T\<close> _\<close> | 
| 69593 | 36 | (Const (\<^const_syntax>\<open>Pure.type\<close>, _) :: ts) = | 
| 52143 | 37 | Term.list_comb | 
| 69593 | 38 | (Syntax.const \<^syntax_const>\<open>_TYPEREP\<close> $ Syntax_Phases.term_of_typ ctxt T, ts) | 
| 52143 | 39 | | typerep_tr' _ T ts = raise Match; | 
| 69593 | 40 | in [(\<^const_syntax>\<open>typerep\<close>, typerep_tr')] end | 
| 60758 | 41 | \<close> | 
| 26168 | 42 | |
| 60758 | 43 | setup \<open> | 
| 31137 | 44 | let | 
| 26168 | 45 | |
| 31137 | 46 | fun add_typerep tyco thy = | 
| 26168 | 47 | let | 
| 69593 | 48 | val sorts = replicate (Sign.arity_number thy tyco) \<^sort>\<open>typerep\<close>; | 
| 43329 
84472e198515
tuned signature: Name.invent and Name.invent_names;
 wenzelm parents: 
42247diff
changeset | 49 | val vs = Name.invent_names Name.context "'a" sorts; | 
| 26168 | 50 | val ty = Type (tyco, map TFree vs); | 
| 74345 | 51 |     val lhs = \<^Const>\<open>typerep ty\<close> $ Free ("T", Term.itselfT ty);
 | 
| 52 | val rhs = \<^Const>\<open>Typerep\<close> $ HOLogic.mk_literal tyco | |
| 53 | $ HOLogic.mk_list \<^Type>\<open>typerep\<close> (map (HOLogic.mk_typerep o TFree) vs); | |
| 26168 | 54 | val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)); | 
| 55 | in | |
| 56 | thy | |
| 69593 | 57 | |> Class.instantiation ([tyco], vs, \<^sort>\<open>typerep\<close>) | 
| 26168 | 58 | |> `(fn lthy => Syntax.check_term lthy eq) | 
| 63352 | 59 | |-> (fn eq => Specification.definition NONE [] [] (Binding.empty_atts, eq)) | 
| 26168 | 60 | |> snd | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58889diff
changeset | 61 | |> Class.prove_instantiation_exit (fn ctxt => Class.intro_classes_tac ctxt []) | 
| 26168 | 62 | end; | 
| 63 | ||
| 45693 | 64 | fun ensure_typerep tyco thy = | 
| 69593 | 65 | if not (Sorts.has_instance (Sign.classes_of thy) tyco \<^sort>\<open>typerep\<close>) | 
| 66 | andalso Sorts.has_instance (Sign.classes_of thy) tyco \<^sort>\<open>type\<close> | |
| 31137 | 67 | then add_typerep tyco thy else thy; | 
| 68 | ||
| 69 | in | |
| 26168 | 70 | |
| 69593 | 71 | add_typerep \<^type_name>\<open>fun\<close> | 
| 58662 | 72 | #> Typedef.interpretation (Local_Theory.background_theory o ensure_typerep) | 
| 66330 
dcb3e6bdc00a
one single plugin for code type declarations avoids problems when bootstrapping new plugins over types which have been both declared concrete and abstract in their code historiy
 haftmann parents: 
63352diff
changeset | 73 | #> Code.type_interpretation ensure_typerep | 
| 26168 | 74 | |
| 31137 | 75 | end | 
| 60758 | 76 | \<close> | 
| 26168 | 77 | |
| 28562 | 78 | lemma [code]: | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 79 | "HOL.equal (Typerep tyco1 tys1) (Typerep tyco2 tys2) \<longleftrightarrow> HOL.equal tyco1 tyco2 | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 80 | \<and> list_all2 HOL.equal tys1 tys2" | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 81 | by (auto simp add: eq_equal [symmetric] list_all2_eq [symmetric]) | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 82 | |
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 83 | lemma [code nbe]: | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 84 | "HOL.equal (x :: typerep) x \<longleftrightarrow> True" | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38348diff
changeset | 85 | by (fact equal_refl) | 
| 26168 | 86 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52143diff
changeset | 87 | code_printing | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52143diff
changeset | 88 | type_constructor typerep \<rightharpoonup> (Eval) "Term.typ" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52143diff
changeset | 89 | | constant Typerep \<rightharpoonup> (Eval) "Term.Type/ (_, _)" | 
| 26168 | 90 | |
| 31031 | 91 | code_reserved Eval Term | 
| 26168 | 92 | |
| 36176 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
35430diff
changeset | 93 | hide_const (open) typerep Typerep | 
| 26168 | 94 | |
| 95 | end |