src/Pure/pure_thy.ML
author wenzelm
Sat Jan 18 19:15:12 2014 +0100 (2014-01-18)
changeset 55033 8e8243975860
parent 53171 a5e54d4d9081
child 55108 0b7a0c1fdf7e
permissions -rw-r--r--
support for nested text cartouches;
clarified Symbol.is_symbolic: exclude \<open> and \<close>;
     1 (*  Title:      Pure/pure_thy.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Pure theory syntax and further logical content.
     5 *)
     6 
     7 signature PURE_THY =
     8 sig
     9   val old_appl_syntax: theory -> bool
    10   val old_appl_syntax_setup: theory -> theory
    11   val token_markers: string list
    12 end;
    13 
    14 structure Pure_Thy: PURE_THY =
    15 struct
    16 
    17 val typ = Simple_Syntax.read_typ;
    18 val prop = Simple_Syntax.read_prop;
    19 
    20 val tycon = Lexicon.mark_type;
    21 val const = Lexicon.mark_const;
    22 
    23 
    24 (* application syntax variants *)
    25 
    26 val appl_syntax =
    27  [("_appl", typ "('b => 'a) => args => logic", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000)),
    28   ("_appl", typ "('b => 'a) => args => aprop", Mixfix ("(1_/(1'(_')))", [1000, 0], 1000))];
    29 
    30 val applC_syntax =
    31  [("",       typ "'a => cargs",                  Delimfix "_"),
    32   ("_cargs", typ "'a => cargs => cargs",         Mixfix ("_/ _", [1000, 1000], 1000)),
    33   ("_applC", typ "('b => 'a) => cargs => logic", Mixfix ("(1_/ _)", [1000, 1000], 999)),
    34   ("_applC", typ "('b => 'a) => cargs => aprop", Mixfix ("(1_/ _)", [1000, 1000], 999))];
    35 
    36 structure Old_Appl_Syntax = Theory_Data
    37 (
    38   type T = bool;
    39   val empty = false;
    40   val extend = I;
    41   fun merge (b1, b2) : T =
    42     if b1 = b2 then b1
    43     else error "Cannot merge theories with different application syntax";
    44 );
    45 
    46 val old_appl_syntax = Old_Appl_Syntax.get;
    47 
    48 val old_appl_syntax_setup =
    49   Old_Appl_Syntax.put true #>
    50   Sign.del_modesyntax_i Syntax.mode_default applC_syntax #>
    51   Sign.add_syntax_i appl_syntax;
    52 
    53 
    54 (* main content *)
    55 
    56 val token_markers =
    57   ["_tfree", "_tvar", "_free", "_bound", "_loose", "_var", "_numeral", "_inner_string"];
    58 
    59 val _ = Theory.setup
    60   (Sign.map_naming (Name_Space.set_theory_name Context.PureN) #>
    61    Old_Appl_Syntax.put false #>
    62    Sign.add_types_global
    63    [(Binding.name "fun", 2, NoSyn),
    64     (Binding.name "prop", 0, NoSyn),
    65     (Binding.name "itself", 1, NoSyn),
    66     (Binding.name "dummy", 0, NoSyn)]
    67   #> Sign.add_nonterminals_global
    68     (map Binding.name
    69       (Lexicon.terminals @ ["logic", "type", "types", "sort", "classes",
    70         "args", "cargs", "pttrn", "pttrns", "idt", "idts", "aprop", "asms",
    71         "any", "prop'", "num_const", "float_const", "xnum_const", "num_position",
    72         "float_position", "xnum_position", "index", "struct", "tid_position",
    73         "tvar_position", "id_position", "longid_position", "var_position", "str_position",
    74         "cartouche_position", "type_name", "class_name"]))
    75   #> Sign.add_syntax_i (map (fn x => (x, typ "'a", NoSyn)) token_markers)
    76   #> Sign.add_syntax_i
    77    [("",            typ "prop' => prop",               Delimfix "_"),
    78     ("",            typ "logic => any",                Delimfix "_"),
    79     ("",            typ "prop' => any",                Delimfix "_"),
    80     ("",            typ "logic => logic",              Delimfix "'(_')"),
    81     ("",            typ "prop' => prop'",              Delimfix "'(_')"),
    82     ("_constrain",  typ "logic => type => logic",      Mixfix ("_::_", [4, 0], 3)),
    83     ("_constrain",  typ "prop' => type => prop'",      Mixfix ("_::_", [4, 0], 3)),
    84     ("_ignore_type", typ "'a",                         NoSyn),
    85     ("",            typ "tid_position => type",        Delimfix "_"),
    86     ("",            typ "tvar_position => type",       Delimfix "_"),
    87     ("",            typ "type_name => type",           Delimfix "_"),
    88     ("_type_name",  typ "id => type_name",             Delimfix "_"),
    89     ("_type_name",  typ "longid => type_name",         Delimfix "_"),
    90     ("_ofsort",     typ "tid_position => sort => type", Mixfix ("_::_", [1000, 0], 1000)),
    91     ("_ofsort",     typ "tvar_position => sort => type", Mixfix ("_::_", [1000, 0], 1000)),
    92     ("_dummy_ofsort", typ "sort => type",              Mixfix ("'_()::_", [0], 1000)),
    93     ("",            typ "class_name => sort",          Delimfix "_"),
    94     ("_class_name", typ "id => class_name",            Delimfix "_"),
    95     ("_class_name", typ "longid => class_name",        Delimfix "_"),
    96     ("_topsort",    typ "sort",                        Delimfix "{}"),
    97     ("_sort",       typ "classes => sort",             Delimfix "{_}"),
    98     ("",            typ "class_name => classes",       Delimfix "_"),
    99     ("_classes",    typ "class_name => classes => classes", Delimfix "_,_"),
   100     ("_tapp",       typ "type => type_name => type",   Mixfix ("_ _", [1000, 0], 1000)),
   101     ("_tappl",      typ "type => types => type_name => type", Delimfix "((1'(_,/ _')) _)"),
   102     ("",            typ "type => types",               Delimfix "_"),
   103     ("_types",      typ "type => types => types",      Delimfix "_,/ _"),
   104     ("\\<^type>fun", typ "type => type => type",       Mixfix ("(_/ => _)", [1, 0], 0)),
   105     ("_bracket",    typ "types => type => type",       Mixfix ("([_]/ => _)", [0, 0], 0)),
   106     ("",            typ "type => type",                Delimfix "'(_')"),
   107     ("\\<^type>dummy", typ "type",                     Delimfix "'_"),
   108     ("_type_prop",  typ "'a",                          NoSyn),
   109     ("_lambda",     typ "pttrns => 'a => logic",       Mixfix ("(3%_./ _)", [0, 3], 3)),
   110     ("_abs",        typ "'a",                          NoSyn),
   111     ("",            typ "'a => args",                  Delimfix "_"),
   112     ("_args",       typ "'a => args => args",          Delimfix "_,/ _"),
   113     ("",            typ "id_position => idt",          Delimfix "_"),
   114     ("_idtdummy",   typ "idt",                         Delimfix "'_"),
   115     ("_idtyp",      typ "id_position => type => idt",  Mixfix ("_::_", [], 0)),
   116     ("_idtypdummy", typ "type => idt",                 Mixfix ("'_()::_", [], 0)),
   117     ("",            typ "idt => idt",                  Delimfix "'(_')"),
   118     ("",            typ "idt => idts",                 Delimfix "_"),
   119     ("_idts",       typ "idt => idts => idts",         Mixfix ("_/ _", [1, 0], 0)),
   120     ("",            typ "idt => pttrn",                Delimfix "_"),
   121     ("",            typ "pttrn => pttrns",             Delimfix "_"),
   122     ("_pttrns",     typ "pttrn => pttrns => pttrns",   Mixfix ("_/ _", [1, 0], 0)),
   123     ("",            typ "aprop => aprop",              Delimfix "'(_')"),
   124     ("",            typ "id_position => aprop",        Delimfix "_"),
   125     ("",            typ "longid_position => aprop",    Delimfix "_"),
   126     ("",            typ "var_position => aprop",       Delimfix "_"),
   127     ("_DDDOT",      typ "aprop",                       Delimfix "..."),
   128     ("_aprop",      typ "aprop => prop",               Delimfix "PROP _"),
   129     ("_asm",        typ "prop => asms",                Delimfix "_"),
   130     ("_asms",       typ "prop => asms => asms",        Delimfix "_;/ _"),
   131     ("_bigimpl",    typ "asms => prop => prop",        Mixfix ("((3[| _ |])/ ==> _)", [0, 1], 1)),
   132     ("_ofclass",    typ "type => logic => prop",       Delimfix "(1OFCLASS/(1'(_,/ _')))"),
   133     ("_mk_ofclass", typ "dummy",                       NoSyn),
   134     ("_TYPE",       typ "type => logic",               Delimfix "(1TYPE/(1'(_')))"),
   135     ("",            typ "id_position => logic",        Delimfix "_"),
   136     ("",            typ "longid_position => logic",    Delimfix "_"),
   137     ("",            typ "var_position => logic",       Delimfix "_"),
   138     ("_DDDOT",      typ "logic",                       Delimfix "..."),
   139     ("_strip_positions", typ "'a", NoSyn),
   140     ("_position",   typ "num_token => num_position",   Delimfix "_"),
   141     ("_position",   typ "float_token => float_position", Delimfix "_"),
   142     ("_position",   typ "xnum_token => xnum_position", Delimfix "_"),
   143     ("_constify",   typ "num_position => num_const",   Delimfix "_"),
   144     ("_constify",   typ "float_position => float_const", Delimfix "_"),
   145     ("_constify",   typ "xnum_position => xnum_const", Delimfix "_"),
   146     ("_index",      typ "logic => index",              Delimfix "(00\\<^bsub>_\\<^esub>)"),
   147     ("_indexdefault", typ "index",                     Delimfix ""),
   148     ("_indexvar",   typ "index",                       Delimfix "'\\<index>"),
   149     ("_struct",     typ "index => logic",              Mixfix ("\\<struct>_", [1000], 1000)),
   150     ("_update_name", typ "idt",                        NoSyn),
   151     ("_constrainAbs", typ "'a",                        NoSyn),
   152     ("_position_sort", typ "tid => tid_position",      Delimfix "_"),
   153     ("_position_sort", typ "tvar => tvar_position",    Delimfix "_"),
   154     ("_position",   typ "id => id_position",           Delimfix "_"),
   155     ("_position",   typ "longid => longid_position",   Delimfix "_"),
   156     ("_position",   typ "var => var_position",         Delimfix "_"),
   157     ("_position",   typ "str_token => str_position",   Delimfix "_"),
   158     ("_position",   typ "cartouche => cartouche_position", Delimfix "_"),
   159     ("_type_constraint_", typ "'a",                    NoSyn),
   160     ("_context_const", typ "id_position => logic",     Delimfix "CONST _"),
   161     ("_context_const", typ "id_position => aprop",     Delimfix "CONST _"),
   162     ("_context_const", typ "longid_position => logic", Delimfix "CONST _"),
   163     ("_context_const", typ "longid_position => aprop", Delimfix "CONST _"),
   164     ("_context_xconst", typ "id_position => logic",    Delimfix "XCONST _"),
   165     ("_context_xconst", typ "id_position => aprop",    Delimfix "XCONST _"),
   166     ("_context_xconst", typ "longid_position => logic", Delimfix "XCONST _"),
   167     ("_context_xconst", typ "longid_position => aprop", Delimfix "XCONST _"),
   168     (const "==>",   typ "prop => prop => prop",        Delimfix "op ==>"),
   169     (const "dummy_pattern", typ "aprop",               Delimfix "'_"),
   170     ("_sort_constraint", typ "type => prop",           Delimfix "(1SORT'_CONSTRAINT/(1'(_')))"),
   171     (const "Pure.term", typ "logic => prop",           Delimfix "TERM _"),
   172     (const "Pure.conjunction", typ "prop => prop => prop", Infixr ("&&&", 2))]
   173   #> Sign.add_syntax_i applC_syntax
   174   #> Sign.add_modesyntax_i (Symbol.xsymbolsN, true)
   175    [(tycon "fun",         typ "type => type => type",   Mixfix ("(_/ \\<Rightarrow> _)", [1, 0], 0)),
   176     ("_bracket",          typ "types => type => type",  Mixfix ("([_]/ \\<Rightarrow> _)", [0, 0], 0)),
   177     ("_ofsort",           typ "tid_position => sort => type", Mixfix ("_\\<Colon>_", [1000, 0], 1000)),
   178     ("_constrain",        typ "logic => type => logic", Mixfix ("_\\<Colon>_", [4, 0], 3)),
   179     ("_constrain",        typ "prop' => type => prop'", Mixfix ("_\\<Colon>_", [4, 0], 3)),
   180     ("_idtyp",            typ "id_position => type => idt", Mixfix ("_\\<Colon>_", [], 0)),
   181     ("_idtypdummy",       typ "type => idt",            Mixfix ("'_()\\<Colon>_", [], 0)),
   182     ("_lambda",           typ "pttrns => 'a => logic",  Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3)),
   183     (const "==",          typ "'a => 'a => prop",       Infix ("\\<equiv>", 2)),
   184     (const "all_binder",  typ "idts => prop => prop",   Mixfix ("(3\\<And>_./ _)", [0, 0], 0)),
   185     (const "==>",         typ "prop => prop => prop",   Infixr ("\\<Longrightarrow>", 1)),
   186     ("_DDDOT",            typ "aprop",                  Delimfix "\\<dots>"),
   187     ("_bigimpl",          typ "asms => prop => prop",   Mixfix ("((1\\<lbrakk>_\\<rbrakk>)/ \\<Longrightarrow> _)", [0, 1], 1)),
   188     ("_DDDOT",            typ "logic",                  Delimfix "\\<dots>")]
   189   #> Sign.add_modesyntax_i ("", false)
   190    [(const "prop", typ "prop => prop", Mixfix ("_", [0], 0))]
   191   #> Sign.add_modesyntax_i ("HTML", false)
   192    [("_lambda", typ "pttrns => 'a => logic", Mixfix ("(3\\<lambda>_./ _)", [0, 3], 3))]
   193   #> Sign.add_consts_i
   194    [(Binding.name "==", typ "'a => 'a => prop", Infix ("==", 2)),
   195     (Binding.name "==>", typ "prop => prop => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
   196     (Binding.name "all", typ "('a => prop) => prop", Binder ("!!", 0, 0)),
   197     (Binding.name "prop", typ "prop => prop", NoSyn),
   198     (Binding.name "TYPE", typ "'a itself", NoSyn),
   199     (Binding.name "dummy_pattern", typ "'a", Delimfix "'_")]
   200   #> Theory.add_deps_global "==" ("==", typ "'a => 'a => prop") []
   201   #> Theory.add_deps_global "==>" ("==>", typ "prop => prop => prop") []
   202   #> Theory.add_deps_global "all" ("all", typ "('a => prop) => prop") []
   203   #> Theory.add_deps_global "TYPE" ("TYPE", typ "'a itself") []
   204   #> Theory.add_deps_global "dummy_pattern" ("dummy_pattern", typ "'a") []
   205   #> Sign.parse_ast_translation Syntax_Trans.pure_parse_ast_translation
   206   #> Sign.parse_translation Syntax_Trans.pure_parse_translation
   207   #> Sign.print_ast_translation Syntax_Trans.pure_print_ast_translation
   208   #> Sign.local_path
   209   #> Sign.add_consts_i
   210    [(Binding.name "term", typ "'a => prop", NoSyn),
   211     (Binding.name "sort_constraint", typ "'a itself => prop", NoSyn),
   212     (Binding.name "conjunction", typ "prop => prop => prop", NoSyn)]
   213   #> (Global_Theory.add_defs false o map Thm.no_attributes)
   214    [(Binding.name "prop_def", prop "(CONST prop :: prop => prop) (A::prop) == A::prop"),
   215     (Binding.name "term_def", prop "(CONST Pure.term :: 'a => prop) (x::'a) == (!!A::prop. A ==> A)"),
   216     (Binding.name "sort_constraint_def",
   217       prop "(CONST Pure.sort_constraint :: 'a itself => prop) (CONST TYPE :: 'a itself) ==\
   218       \ (CONST Pure.term :: 'a itself => prop) (CONST TYPE :: 'a itself)"),
   219     (Binding.name "conjunction_def", prop "(A &&& B) == (!!C::prop. (A ==> B ==> C) ==> C)")] #> snd
   220   #> Sign.hide_const false "Pure.term"
   221   #> Sign.hide_const false "Pure.sort_constraint"
   222   #> Sign.hide_const false "Pure.conjunction"
   223   #> Global_Theory.add_thmss [((Binding.name "nothing", []), [])] #> snd
   224   #> fold (fn (a, prop) =>
   225       snd o Thm.add_axiom_global (Binding.name a, prop)) Proofterm.equality_axms);
   226 
   227 end;