src/Pure/ML/ml_antiquotations.ML
author wenzelm
Sun, 24 Sep 2023 15:55:42 +0200
changeset 78690 e10ef4f9c848
parent 78689 37b49c592265
child 78701 c7b2697094ac
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
     1
(*  Title:      Pure/ML/ml_antiquotations.ML
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     3
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
     4
Miscellaneous ML antiquotations.
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     5
*)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     6
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
     7
signature ML_ANTIQUOTATIONS =
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
     8
sig
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
     9
  val make_judgment: Proof.context -> term -> term
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
    10
  val dest_judgment: Proof.context -> term -> term
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
    11
end;
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
    12
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
    13
structure ML_Antiquotations: ML_ANTIQUOTATIONS =
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    14
struct
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    15
58632
wenzelm
parents: 58046
diff changeset
    16
(* ML support *)
wenzelm
parents: 58046
diff changeset
    17
wenzelm
parents: 58046
diff changeset
    18
val _ = Theory.setup
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
    19
 (ML_Antiquotation.inline \<^binding>\<open>undefined\<close>
61597
53e32a9b66b8 added @{undefined} with somewhat undefined symbol;
wenzelm
parents: 61596
diff changeset
    20
    (Scan.succeed "(raise General.Match)") #>
53e32a9b66b8 added @{undefined} with somewhat undefined symbol;
wenzelm
parents: 61596
diff changeset
    21
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
    22
  ML_Antiquotation.inline \<^binding>\<open>assert\<close>
58632
wenzelm
parents: 58046
diff changeset
    23
    (Scan.succeed "(fn b => if b then () else raise General.Fail \"Assertion failed\")") #>
wenzelm
parents: 58046
diff changeset
    24
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    25
  ML_Antiquotation.declaration_embedded \<^binding>\<open>print\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    26
    (Scan.lift (Scan.optional Parse.embedded "Output.writeln"))
58632
wenzelm
parents: 58046
diff changeset
    27
      (fn src => fn output => fn ctxt =>
wenzelm
parents: 58046
diff changeset
    28
        let
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    29
          val struct_name = ML_Context.struct_name ctxt;
58632
wenzelm
parents: 58046
diff changeset
    30
          val (_, pos) = Token.name_of_src src;
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59064
diff changeset
    31
          val (a, ctxt') = ML_Context.variant "output" ctxt;
58632
wenzelm
parents: 58046
diff changeset
    32
          val env =
wenzelm
parents: 58046
diff changeset
    33
            "val " ^ a ^ ": string -> unit =\n\
wenzelm
parents: 58046
diff changeset
    34
            \  (" ^ output ^ ") o (fn s => s ^ Position.here (" ^
wenzelm
parents: 58046
diff changeset
    35
            ML_Syntax.print_position pos ^ "));\n";
wenzelm
parents: 58046
diff changeset
    36
          val body =
62900
c641bf9402fd simplified default print_depth: context is usually available, in contrast to 0d295e339f52;
wenzelm
parents: 62899
diff changeset
    37
            "(fn x => (" ^ struct_name ^ "." ^ a ^ " (" ^ ML_Pretty.make_string_fn ^ " x); x))";
63204
921a5be54132 support rat numerals via special antiquotation syntax;
wenzelm
parents: 63120
diff changeset
    38
        in (K (env, body), ctxt') end) #>
921a5be54132 support rat numerals via special antiquotation syntax;
wenzelm
parents: 63120
diff changeset
    39
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
    40
  ML_Antiquotation.value \<^binding>\<open>rat\<close>
63204
921a5be54132 support rat numerals via special antiquotation syntax;
wenzelm
parents: 63120
diff changeset
    41
    (Scan.lift (Scan.optional (Args.$$$ "~" >> K ~1) 1 -- Parse.nat --
921a5be54132 support rat numerals via special antiquotation syntax;
wenzelm
parents: 63120
diff changeset
    42
      Scan.optional (Args.$$$ "/" |-- Parse.nat) 1) >> (fn ((sign, a), b) =>
73586
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    43
        "Rat.make " ^ ML_Syntax.print_pair ML_Syntax.print_int ML_Syntax.print_int (sign * a, b))) #>
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    44
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    45
  ML_Antiquotation.conditional \<^binding>\<open>if_linux\<close> (fn _ => ML_System.platform_is_linux) #>
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    46
  ML_Antiquotation.conditional \<^binding>\<open>if_macos\<close> (fn _ => ML_System.platform_is_macos) #>
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    47
  ML_Antiquotation.conditional \<^binding>\<open>if_windows\<close> (fn _ => ML_System.platform_is_windows) #>
76d0b6597c91 support for conditional ML text;
wenzelm
parents: 73550
diff changeset
    48
  ML_Antiquotation.conditional \<^binding>\<open>if_unix\<close> (fn _ => ML_System.platform_is_unix));
58632
wenzelm
parents: 58046
diff changeset
    49
wenzelm
parents: 58046
diff changeset
    50
wenzelm
parents: 58046
diff changeset
    51
(* formal entities *)
wenzelm
parents: 58046
diff changeset
    52
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    53
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    54
 (ML_Antiquotation.value_embedded \<^binding>\<open>system_option\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    55
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) =>
67208
16519cd83ed4 clarified signature;
wenzelm
parents: 67147
diff changeset
    56
      (Completion.check_option (Options.default ()) ctxt (name, pos) |> ML_Syntax.print_string))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    57
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    58
  ML_Antiquotation.value_embedded \<^binding>\<open>theory\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    59
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) =>
68482
cb84beb84ca9 clarified signature;
wenzelm
parents: 67208
diff changeset
    60
      (Theory.check {long = false} ctxt (name, pos);
cb84beb84ca9 clarified signature;
wenzelm
parents: 67208
diff changeset
    61
       "Context.get_theory {long = false} (Proof_Context.theory_of ML_context) " ^
cb84beb84ca9 clarified signature;
wenzelm
parents: 67208
diff changeset
    62
        ML_Syntax.print_string name))
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    63
    || Scan.succeed "Proof_Context.theory_of ML_context") #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    64
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    65
  ML_Antiquotation.value_embedded \<^binding>\<open>theory_context\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    66
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) =>
68482
cb84beb84ca9 clarified signature;
wenzelm
parents: 67208
diff changeset
    67
      (Theory.check {long = false} ctxt (name, pos);
77889
5db014c36f42 clarified signature: explicitly distinguish theory_base_name vs. theory_long_name;
wenzelm
parents: 74880
diff changeset
    68
       "Proof_Context.get_global {long = false} (Proof_Context.theory_of ML_context) " ^
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    69
        ML_Syntax.print_string name))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    70
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
    71
  ML_Antiquotation.inline \<^binding>\<open>context\<close>
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    72
    (Args.context >> (fn ctxt => ML_Context.struct_name ctxt ^ ".ML_context")) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    73
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    74
  ML_Antiquotation.inline_embedded \<^binding>\<open>typ\<close>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    75
    (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ)) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    76
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    77
  ML_Antiquotation.inline_embedded \<^binding>\<open>term\<close>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    78
    (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term)) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    79
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    80
  ML_Antiquotation.inline_embedded \<^binding>\<open>prop\<close>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    81
    (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term)) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    82
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    83
  ML_Antiquotation.value_embedded \<^binding>\<open>ctyp\<close> (Args.typ >> (fn T =>
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59573
diff changeset
    84
    "Thm.ctyp_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_typ T))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    85
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    86
  ML_Antiquotation.value_embedded \<^binding>\<open>cterm\<close> (Args.term >> (fn t =>
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59573
diff changeset
    87
    "Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    88
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
    89
  ML_Antiquotation.value_embedded \<^binding>\<open>cprop\<close> (Args.prop >> (fn t =>
62075
ea3360245939 added ML antiquotation @{method};
wenzelm
parents: 61598
diff changeset
    90
    "Thm.cterm_of ML_context " ^ ML_Syntax.atomic (ML_Syntax.print_term t))) #>
ea3360245939 added ML antiquotation @{method};
wenzelm
parents: 61598
diff changeset
    91
70565
d0b75c59beca added ML antiquotation @{oracle_name};
wenzelm
parents: 69595
diff changeset
    92
  ML_Antiquotation.inline_embedded \<^binding>\<open>oracle_name\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    93
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) =>
73613
c1d8cd6d1a49 early definition of ML antiquotations;
wenzelm
parents: 73586
diff changeset
    94
      ML_Syntax.print_string (Thm.check_oracle ctxt (name, pos)))));
63553
4a72b37ac4b8 text antiquotation for locales (similar to classes)
haftmann
parents: 63204
diff changeset
    95
4a72b37ac4b8 text antiquotation for locales (similar to classes)
haftmann
parents: 63204
diff changeset
    96
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    97
(* type classes *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    98
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
    99
fun class syn = Args.context -- Scan.lift Parse.embedded_inner_syntax >> (fn (ctxt, s) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   100
  Proof_Context.read_class ctxt s
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   101
  |> syn ? Lexicon.mark_class
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   102
  |> ML_Syntax.print_string);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   103
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   104
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   105
 (ML_Antiquotation.inline_embedded \<^binding>\<open>class\<close> (class false) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   106
  ML_Antiquotation.inline_embedded \<^binding>\<open>class_syntax\<close> (class true) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   107
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   108
  ML_Antiquotation.inline_embedded \<^binding>\<open>sort\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
   109
    (Args.context -- Scan.lift Parse.embedded_inner_syntax >> (fn (ctxt, s) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   110
      ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s)))));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   111
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   112
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   113
(* type constructors *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   114
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
   115
fun type_name kind check = Args.context -- Scan.lift (Parse.token Parse.embedded)
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   116
  >> (fn (ctxt, tok) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   117
    let
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   118
      val s = Token.inner_syntax_of tok;
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   119
      val (_, pos) = Input.source_content (Token.input_of tok);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   120
      val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   121
      val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   122
      val res =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   123
        (case try check (c, decl) of
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   124
          SOME res => res
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   125
        | NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   126
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   127
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   128
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   129
 (ML_Antiquotation.inline_embedded \<^binding>\<open>type_name\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   130
    (type_name "logical type" (fn (c, Type.LogicalType _) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   131
  ML_Antiquotation.inline_embedded \<^binding>\<open>type_abbrev\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   132
    (type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   133
  ML_Antiquotation.inline_embedded \<^binding>\<open>nonterminal\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   134
    (type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   135
  ML_Antiquotation.inline_embedded \<^binding>\<open>type_syntax\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   136
    (type_name "type" (fn (c, _) => Lexicon.mark_type c)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   137
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   138
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   139
(* constants *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   140
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
   141
fun const_name check = Args.context -- Scan.lift (Parse.token Parse.embedded)
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   142
  >> (fn (ctxt, tok) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   143
    let
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   144
      val s = Token.inner_syntax_of tok;
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   145
      val (_, pos) = Input.source_content (Token.input_of tok);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   146
      val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   147
      val res = check (Proof_Context.consts_of ctxt, c)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   148
        handle TYPE (msg, _, _) => error (msg ^ Position.here pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   149
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   150
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   151
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   152
 (ML_Antiquotation.inline_embedded \<^binding>\<open>const_name\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   153
    (const_name (fn (consts, c) => (Consts.the_const consts c; c))) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   154
  ML_Antiquotation.inline_embedded \<^binding>\<open>const_abbrev\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   155
    (const_name (fn (consts, c) => (Consts.the_abbreviation consts c; c))) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   156
  ML_Antiquotation.inline_embedded \<^binding>\<open>const_syntax\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   157
    (const_name (fn (_, c) => Lexicon.mark_const c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   158
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   159
  ML_Antiquotation.inline_embedded \<^binding>\<open>syntax_const\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
   160
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, arg) =>
74331
b9a3d2fb53e0 clarified signature;
wenzelm
parents: 74317
diff changeset
   161
      ML_Syntax.print_string (Proof_Context.check_syntax_const ctxt arg))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   162
69595
ec135235fbcc mark for isabelle update -u control_cartouches;
wenzelm
parents: 69592
diff changeset
   163
  ML_Antiquotation.inline_embedded \<^binding>\<open>const\<close>
74563
042041c0ebeb clarified modules;
wenzelm
parents: 74562
diff changeset
   164
    (Args.context -- Scan.lift (Parse.position Parse.embedded_inner_syntax) -- Scan.optional
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   165
        (Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) []
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   166
      >> (fn ((ctxt, (raw_c, pos)), Ts) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   167
        let
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   168
          val Const (c, _) =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   169
            Proof_Context.read_const {proper = true, strict = true} ctxt raw_c;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   170
          val consts = Proof_Context.consts_of ctxt;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   171
          val n = length (Consts.typargs consts (c, Consts.type_scheme consts c));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   172
          val _ = length Ts <> n andalso
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   173
            error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   174
              quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   175
          val const = Const (c, Consts.instance consts (c, Ts));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   176
        in ML_Syntax.atomic (ML_Syntax.print_term const) end)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   177
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   178
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   179
(* object-logic judgment *)
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   180
74344
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   181
fun make_judgment ctxt =
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   182
  let val const = Object_Logic.judgment_const ctxt
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   183
  in fn t => Const const $ t end;
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   184
74344
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   185
fun dest_judgment ctxt =
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   186
  let
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   187
    val is_judgment = Object_Logic.is_judgment ctxt;
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   188
    val drop_judgment = Object_Logic.drop_judgment ctxt;
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   189
  in
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   190
    fn t =>
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   191
      if is_judgment t then drop_judgment t
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   192
      else raise TERM ("dest_judgment", [t])
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   193
  end;
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   194
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   195
val _ = Theory.setup
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   196
 (ML_Antiquotation.value \<^binding>\<open>make_judgment\<close>
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
   197
   (Scan.succeed "ML_Antiquotations.make_judgment ML_context") #>
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   198
  ML_Antiquotation.value \<^binding>\<open>dest_judgment\<close>
74559
9189d949abb9 clarified modules;
wenzelm
parents: 74556
diff changeset
   199
   (Scan.succeed "ML_Antiquotations.dest_judgment ML_context"));
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   200
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   201
74580
d114553793df tuned comments;
wenzelm
parents: 74579
diff changeset
   202
(* type/term constructors *)
d114553793df tuned comments;
wenzelm
parents: 74579
diff changeset
   203
74556
b45b85a4145e clarified keywords and reports;
wenzelm
parents: 74387
diff changeset
   204
local
b45b85a4145e clarified keywords and reports;
wenzelm
parents: 74387
diff changeset
   205
b45b85a4145e clarified keywords and reports;
wenzelm
parents: 74387
diff changeset
   206
val keywords = Keyword.add_minor_keywords ["for", "=>"] Keyword.empty_keywords;
b45b85a4145e clarified keywords and reports;
wenzelm
parents: 74387
diff changeset
   207
74880
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   208
val parse_name_args =
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   209
  Parse.input Parse.name -- Scan.repeat Parse.embedded_ml;
74374
e585e5a906ba more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents: 74344
diff changeset
   210
74880
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   211
val parse_for_args =
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   212
  Scan.optional (Parse.$$$ "for" |-- Parse.!!! (Scan.repeat1 Parse.embedded_ml)) [];
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   213
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   214
fun parse_body b =
74880
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   215
  if b then Parse.$$$ "=>" |-- Parse.!!! (Parse.embedded_ml >> single) else Scan.succeed [];
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   216
74374
e585e5a906ba more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents: 74344
diff changeset
   217
fun is_dummy [Antiquote.Text tok] = ML_Lex.content_of tok = "_"
e585e5a906ba more convenient ML arguments: avoid excessive nesting of cartouches;
wenzelm
parents: 74344
diff changeset
   218
  | is_dummy _ = false;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   219
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   220
val ml = ML_Lex.tokenize_no_range;
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   221
val ml_range = ML_Lex.tokenize_range;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   222
val ml_dummy = ml "_";
74387
6edb71482de6 avoid overlapping PIDE markup (amending bb25ea271b15);
wenzelm
parents: 74378
diff changeset
   223
fun ml_enclose range x = ml "(" @ x @ ml_range range ")";
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   224
fun ml_parens x = ml "(" @ x @ ml ")";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   225
fun ml_bracks x = ml "[" @ x @ ml "]";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   226
fun ml_commas xs = flat (separate (ml ", ") xs);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   227
val ml_list = ml_bracks o ml_commas;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   228
val ml_string = ml o ML_Syntax.print_string;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   229
fun ml_pair (x, y) = ml_parens (ml_commas [x, y]);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   230
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   231
fun type_antiquotation binding {function} =
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   232
  ML_Context.add_antiquotation binding true
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   233
    (fn range => fn src => fn ctxt =>
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   234
      let
74562
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   235
        val ((s, type_args), fn_body) = src
74880
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   236
          |> Parse.read_embedded_src ctxt keywords (parse_name_args -- parse_body function);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   237
        val pos = Input.pos_of s;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   238
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   239
        val Type (c, Ts) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   240
          Proof_Context.read_type_name {proper = true, strict = true} ctxt
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   241
            (Syntax.implode_input s);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   242
        val n = length Ts;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   243
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   244
          length type_args = n orelse
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   245
            error ("Type constructor " ^ quote (Proof_Context.markup_type ctxt c) ^
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   246
              " takes " ^ string_of_int n ^ " argument(s)" ^ Position.here pos);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   247
74562
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   248
        val (decls1, ctxt1) = ML_Context.expand_antiquotes_list type_args ctxt;
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   249
        val (decls2, ctxt2) = ML_Context.expand_antiquotes_list fn_body ctxt1;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   250
        fun decl' ctxt' =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   251
          let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   252
            val (ml_args_env, ml_args_body) = split_list (decls1 ctxt');
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   253
            val (ml_fn_env, ml_fn_body) = split_list (decls2 ctxt');
74378
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   254
            val ml1 =
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   255
              ml_enclose range (ml "Term.Type " @ ml_pair (ml_string c, ml_list ml_args_body));
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   256
            val ml2 =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   257
              if function then
74378
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   258
                ml_enclose range
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   259
                 (ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   260
                  ml "| T => " @ ml_range range "raise" @
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   261
                  ml " Term.TYPE (" @ ml_string ("Type_fn " ^ quote c) @ ml ", [T], [])")
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   262
              else ml1;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   263
          in (flat (ml_args_env @ ml_fn_env), ml2) end;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   264
      in (decl', ctxt2) end);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   265
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   266
fun const_antiquotation binding {pattern, function} =
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   267
  ML_Context.add_antiquotation binding true
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   268
    (fn range => fn src => fn ctxt =>
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   269
      let
74562
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   270
        val (((s, type_args), term_args), fn_body) = src
74564
0a66a61e740c clarified modules;
wenzelm
parents: 74563
diff changeset
   271
          |> Parse.read_embedded_src ctxt keywords
74880
944d4d616ca0 clarified corner cases of syntax;
wenzelm
parents: 74879
diff changeset
   272
              (parse_name_args -- parse_for_args -- parse_body function);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   273
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   274
        val Const (c, T) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   275
          Proof_Context.read_const {proper = true, strict = true} ctxt
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   276
            (Syntax.implode_input s);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   277
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   278
        val consts = Proof_Context.consts_of ctxt;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   279
        val type_paths = Consts.type_arguments consts c;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   280
        val type_params = map Term.dest_TVar (Consts.typargs consts (c, T));
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   281
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   282
        val n = length type_params;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   283
        val m = length (Term.binder_types T);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   284
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   285
        fun err msg =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   286
          error ("Constant " ^ quote (Proof_Context.markup_const ctxt c) ^ msg ^
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   287
            Position.here (Input.pos_of s));
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   288
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   289
          length type_args <> n andalso err (" takes " ^ string_of_int n ^ " type argument(s)");
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   290
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   291
          length term_args > m andalso Term.is_Type (Term.body_type T) andalso
74377
6cefe97cb3ab clarified message;
wenzelm
parents: 74374
diff changeset
   292
            err (" cannot have more than " ^ string_of_int m ^ " argument(s)");
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   293
74562
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   294
        val (decls1, ctxt1) = ML_Context.expand_antiquotes_list type_args ctxt;
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   295
        val (decls2, ctxt2) = ML_Context.expand_antiquotes_list term_args ctxt1;
8403bd51f8b1 clarified modules;
wenzelm
parents: 74559
diff changeset
   296
        val (decls3, ctxt3) = ML_Context.expand_antiquotes_list fn_body ctxt2;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   297
        fun decl' ctxt' =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   298
          let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   299
            val (ml_args_env1, ml_args_body1) = split_list (decls1 ctxt');
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   300
            val (ml_args_env2, ml_args_body2) = split_list (decls2 ctxt');
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   301
            val (ml_fn_env, ml_fn_body) = split_list (decls3 ctxt');
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   302
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   303
            val relevant = map is_dummy type_args ~~ type_paths;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   304
            fun relevant_path is =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   305
              not pattern orelse
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   306
                let val p = rev is
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   307
                in relevant |> exists (fn (u, q) => not u andalso is_prefix (op =) p q) end;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   308
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   309
            val ml_typarg = the o AList.lookup (op =) (type_params ~~ ml_args_body1);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   310
            fun ml_typ is (Type (d, Us)) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   311
                  if relevant_path is then
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   312
                    ml "Term.Type " @
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   313
                    ml_pair (ml_string d, ml_list (map_index (fn (i, U) => ml_typ (i :: is) U) Us))
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   314
                  else ml_dummy
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   315
              | ml_typ is (TVar arg) = if relevant_path is then ml_typarg arg else ml_dummy
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   316
              | ml_typ _ (TFree _) = raise Match;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   317
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   318
            fun ml_app [] = ml "Term.Const " @ ml_pair (ml_string c, ml_typ [] T)
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   319
              | ml_app (u :: us) = ml "Term.$ " @ ml_pair (ml_app us, u);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   320
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   321
            val ml_env = flat (ml_args_env1 @ ml_args_env2 @ ml_fn_env);
74378
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   322
            val ml1 = ml_enclose range (ml_app (rev ml_args_body2));
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   323
            val ml2 =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   324
              if function then
74378
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   325
                ml_enclose range
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   326
                 (ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   327
                  ml "| t => " @ ml_range range "raise" @
bb25ea271b15 clarified positions, notably for ML compiler errors;
wenzelm
parents: 74377
diff changeset
   328
                  ml " Term.TERM (" @ ml_string ("Const_fn " ^ quote c) @ ml ", [t])")
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   329
              else ml1;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   330
          in (ml_env, ml2) end;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   331
      in (decl', ctxt3) end);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   332
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   333
val _ = Theory.setup
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   334
 (type_antiquotation \<^binding>\<open>Type\<close> {function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   335
  type_antiquotation \<^binding>\<open>Type_fn\<close> {function = true} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   336
  const_antiquotation \<^binding>\<open>Const\<close> {pattern = false, function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   337
  const_antiquotation \<^binding>\<open>Const_\<close> {pattern = true, function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   338
  const_antiquotation \<^binding>\<open>Const_fn\<close> {pattern = true, function = true});
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   339
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   340
in end;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   341
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   342
77907
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   343
(* special forms for option type *)
73550
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   344
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   345
val _ = Theory.setup
78688
ff7db9055002 clarified signature;
wenzelm
parents: 77907
diff changeset
   346
 (ML_Antiquotation.special_form \<^binding>\<open>try\<close> "Isabelle_Thread.try" #>
ff7db9055002 clarified signature;
wenzelm
parents: 77907
diff changeset
   347
  ML_Antiquotation.special_form \<^binding>\<open>can\<close> "Isabelle_Thread.can" #>
77907
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   348
  ML_Context.add_antiquotation \<^binding>\<open>if_none\<close> true
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   349
    (fn _ => fn src => fn ctxt =>
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   350
      let
78690
e10ef4f9c848 clarified signature;
wenzelm
parents: 78689
diff changeset
   351
        val s = Token.read ctxt Parse.embedded_input src;
77907
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   352
        val tokenize = ML_Lex.tokenize_no_range;
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   353
78689
37b49c592265 clarified signature;
wenzelm
parents: 78688
diff changeset
   354
        val (decl, ctxt') = ML_Context.read_antiquotes s ctxt;
77907
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   355
        fun decl' ctxt'' =
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   356
          let
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   357
            val (ml_env, ml_body) = decl ctxt'';
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   358
            val ml_body' = tokenize "(fn SOME x => x | NONE => " @ ml_body @ tokenize ")";
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   359
          in (ml_env, ml_body') end;
ee9785abbcd6 provide ML antiquotation "if_none": non-strict version of "the_default";
wenzelm
parents: 77889
diff changeset
   360
      in (decl', ctxt') end));
73550
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   361
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   362
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   363
(* basic combinators *)
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   364
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   365
local
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   366
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   367
val parameter = Parse.position Parse.nat >> (fn (n, pos) =>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   368
  if n > 1 then n else error ("Bad parameter: " ^ string_of_int n ^ Position.here pos));
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   369
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   370
fun indices n = map string_of_int (1 upto n);
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   371
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   372
fun empty n = replicate_string n " []";
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   373
fun dummy n = replicate_string n " _";
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   374
fun vars x n = implode (map (fn a => " " ^ x ^ a) (indices n));
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   375
fun cons n = implode (map (fn a => " (x" ^ a ^ " :: xs" ^ a ^ ")") (indices n));
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   376
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   377
val tuple = enclose "(" ")" o commas;
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   378
fun tuple_empty n = tuple (replicate n "[]");
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   379
fun tuple_vars x n = tuple (map (fn a => x ^ a) (indices n));
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   380
fun tuple_cons n = "(" ^ tuple_vars "x" n ^ " :: xs)"
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   381
fun cons_tuple n = tuple (map (fn a => "x" ^ a ^ " :: xs" ^ a) (indices n));
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   382
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   383
in
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   384
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   385
val _ = Theory.setup
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   386
 (ML_Antiquotation.value \<^binding>\<open>map\<close>
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   387
    (Scan.lift parameter >> (fn n =>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   388
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   389
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   390
      \    fun map _" ^ empty n ^ " = []\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   391
      \      | map f" ^ cons n ^ " = f" ^ vars "x" n ^ " :: map f" ^ vars "xs" n ^ "\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   392
      \      | map _" ^  dummy n ^ " = raise ListPair.UnequalLengths\n" ^
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   393
      "  in map f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   394
  ML_Antiquotation.value \<^binding>\<open>fold\<close>
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   395
    (Scan.lift parameter >> (fn n =>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   396
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   397
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   398
      \    fun fold _" ^ empty n ^ " a = a\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   399
      \      | fold f" ^ cons n ^ " a = fold f" ^ vars "xs" n ^ " (f" ^ vars "x" n ^ " a)\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   400
      \      | fold _" ^  dummy n ^ " _ = raise ListPair.UnequalLengths\n" ^
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   401
      "  in fold f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   402
  ML_Antiquotation.value \<^binding>\<open>fold_map\<close>
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   403
    (Scan.lift parameter >> (fn n =>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   404
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   405
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   406
      \    fun fold_map _" ^ empty n ^ " a = ([], a)\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   407
      \      | fold_map f" ^ cons n ^ " a =\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   408
      \          let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   409
      \            val (x, a') = f" ^ vars "x" n ^ " a\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   410
      \            val (xs, a'') = fold_map f" ^ vars "xs" n ^ " a'\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   411
      \          in (x :: xs, a'') end\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   412
      \      | fold_map _" ^  dummy n ^ " _ = raise ListPair.UnequalLengths\n" ^
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   413
      "  in fold_map f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   414
  ML_Antiquotation.value \<^binding>\<open>split_list\<close>
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   415
    (Scan.lift parameter >> (fn n =>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   416
      "fn list =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   417
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   418
      \    fun split_list [] =" ^ tuple_empty n ^ "\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   419
      \      | split_list" ^ tuple_cons n ^ " =\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   420
      \          let val" ^ tuple_vars "xs" n ^ " = split_list xs\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   421
      \          in " ^ cons_tuple n ^ "end\n\
59057
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   422
      \  in split_list list end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   423
  ML_Antiquotation.value \<^binding>\<open>apply\<close>
59057
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   424
    (Scan.lift (parameter -- Scan.option (Args.parens (Parse.position Parse.nat))) >>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   425
      (fn (n, opt_index) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   426
        let
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   427
          val cond =
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   428
            (case opt_index of
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   429
              NONE => K true
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   430
            | SOME (index, index_pos) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   431
                if 1 <= index andalso index <= n then equal (string_of_int index)
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   432
                else error ("Bad index: " ^ string_of_int index ^ Position.here index_pos));
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   433
        in
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   434
          "fn f => fn " ^ tuple_vars "x" n ^ " => " ^
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   435
            tuple (map (fn a => (if cond a then "f x" else "x") ^ a) (indices n))
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   436
        end)));
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   437
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   438
end;
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   439
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   440
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   441
(* outer syntax *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   442
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   443
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   444
 (ML_Antiquotation.value_embedded \<^binding>\<open>keyword\<close>
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   445
    (Args.context --
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   446
      Scan.lift (Parse.embedded_position || Parse.position (Parse.keyword_with (K true)))
64594
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   447
      >> (fn (ctxt, (name, pos)) =>
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   448
        if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   449
          (Context_Position.report ctxt pos (Token.keyword_markup (true, Markup.keyword2) name);
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   450
           "Parse.$$$ " ^ ML_Syntax.print_string name)
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   451
        else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos))) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   452
  ML_Antiquotation.value_embedded \<^binding>\<open>command_keyword\<close>
69349
7cef9e386ffe more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
wenzelm
parents: 68482
diff changeset
   453
    (Args.context -- Scan.lift Parse.embedded_position >> (fn (ctxt, (name, pos)) =>
59934
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   454
      (case Keyword.command_markup (Thy_Header.get_keywords' ctxt) name of
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   455
        SOME markup =>
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   456
         (Context_Position.reports ctxt [(pos, markup), (pos, Markup.keyword1)];
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   457
          ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_position (name, pos))
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   458
      | NONE => error ("Bad outer syntax command " ^ quote name ^ Position.here pos)))));
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   459
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   460
end;