src/Pure/ML/ml_antiquotations1.ML
author wenzelm
Tue, 21 Sep 2021 19:42:30 +0200
changeset 74344 1c2c0380d58b
parent 74341 edf8b141a8c4
child 74374 e585e5a906ba
permissions -rw-r--r--
clarified partial application: immediate check of object-logic, and avoidance of context within closure;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73613
c1d8cd6d1a49 early definition of ML antiquotations;
wenzelm
parents: 73586
diff changeset
     1
(*  Title:      Pure/ML/ml_antiquotations1.ML
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     3
73613
c1d8cd6d1a49 early definition of ML antiquotations;
wenzelm
parents: 73586
diff changeset
     4
Miscellaneous ML antiquotations: part 1.
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     5
*)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     6
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
     7
signature ML_ANTIQUOTATIONS1 =
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
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
    13
structure ML_Antiquotations1: ML_ANTIQUOTATIONS1 =
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>
63120
629a4c5e953e embedded content may be delimited via cartouches;
wenzelm
parents: 62900
diff changeset
    26
    (Scan.lift (Scan.optional Args.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>
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
    55
    (Args.context -- Scan.lift Args.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>
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
    59
    (Args.context -- Scan.lift Args.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>
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
    66
    (Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, (name, pos)) =>
68482
cb84beb84ca9 clarified signature;
wenzelm
parents: 67208
diff changeset
    67
      (Theory.check {long = false} ctxt (name, pos);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    68
       "Proof_Context.get_global (Proof_Context.theory_of ML_context) " ^
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>
d0b75c59beca added ML antiquotation @{oracle_name};
wenzelm
parents: 69595
diff changeset
    93
    (Args.context -- Scan.lift Args.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
74290
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
    97
(* schematic variables *)
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
    98
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
    99
val schematic_input =
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   100
  Args.context -- Scan.lift Args.embedded_input >> (fn (ctxt, src) =>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   101
    (Proof_Context.set_mode Proof_Context.mode_schematic ctxt,
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   102
      (Syntax.implode_input src, Input.pos_of src)));
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   103
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   104
val _ = Theory.setup
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   105
  (ML_Antiquotation.inline_embedded \<^binding>\<open>tvar\<close>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   106
    (schematic_input >> (fn (ctxt, (s, pos)) =>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   107
      (case Syntax.read_typ ctxt s of
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   108
        TVar v => ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_sort v
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   109
      | _ => error ("Schematic type variable expected" ^ Position.here pos)))) #>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   110
   ML_Antiquotation.inline_embedded \<^binding>\<open>var\<close>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   111
    (schematic_input >> (fn (ctxt, (s, pos)) =>
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   112
      (case Syntax.read_term ctxt s of
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   113
        Var v => ML_Syntax.print_pair ML_Syntax.print_indexname ML_Syntax.print_typ v
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   114
      | _ => error ("Schematic variable expected" ^ Position.here pos)))));
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   115
b2ad24b5a42c more antiquotations;
wenzelm
parents: 73613
diff changeset
   116
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   117
(* type classes *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   118
63120
629a4c5e953e embedded content may be delimited via cartouches;
wenzelm
parents: 62900
diff changeset
   119
fun class syn = Args.context -- Scan.lift Args.embedded_inner_syntax >> (fn (ctxt, s) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   120
  Proof_Context.read_class ctxt s
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   121
  |> syn ? Lexicon.mark_class
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   122
  |> ML_Syntax.print_string);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   123
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   124
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   125
 (ML_Antiquotation.inline_embedded \<^binding>\<open>class\<close> (class false) #>
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   126
  ML_Antiquotation.inline_embedded \<^binding>\<open>class_syntax\<close> (class true) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   127
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   128
  ML_Antiquotation.inline_embedded \<^binding>\<open>sort\<close>
63120
629a4c5e953e embedded content may be delimited via cartouches;
wenzelm
parents: 62900
diff changeset
   129
    (Args.context -- Scan.lift Args.embedded_inner_syntax >> (fn (ctxt, s) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   130
      ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s)))));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   131
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   132
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   133
(* type constructors *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   134
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
   135
fun type_name kind check = Args.context -- Scan.lift Args.embedded_token
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
   136
  >> (fn (ctxt, tok) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   137
    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
   138
      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
   139
      val (_, pos) = Input.source_content (Token.input_of tok);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   140
      val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   141
      val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   142
      val res =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   143
        (case try check (c, decl) of
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   144
          SOME res => res
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   145
        | NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   146
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   147
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   148
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   149
 (ML_Antiquotation.inline_embedded \<^binding>\<open>type_name\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   150
    (type_name "logical type" (fn (c, Type.LogicalType _) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   151
  ML_Antiquotation.inline_embedded \<^binding>\<open>type_abbrev\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   152
    (type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   153
  ML_Antiquotation.inline_embedded \<^binding>\<open>nonterminal\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   154
    (type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #>
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   155
  ML_Antiquotation.inline_embedded \<^binding>\<open>type_syntax\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   156
    (type_name "type" (fn (c, _) => Lexicon.mark_type c)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   157
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   158
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   159
(* constants *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   160
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
   161
fun const_name check = Args.context -- Scan.lift Args.embedded_token
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
   162
  >> (fn (ctxt, tok) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   163
    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
   164
      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
   165
      val (_, pos) = Input.source_content (Token.input_of tok);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   166
      val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   167
      val res = check (Proof_Context.consts_of ctxt, c)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   168
        handle TYPE (msg, _, _) => error (msg ^ Position.here pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   169
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   170
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   171
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   172
 (ML_Antiquotation.inline_embedded \<^binding>\<open>const_name\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   173
    (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
   174
  ML_Antiquotation.inline_embedded \<^binding>\<open>const_abbrev\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   175
    (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
   176
  ML_Antiquotation.inline_embedded \<^binding>\<open>const_syntax\<close>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   177
    (const_name (fn (_, c) => Lexicon.mark_const c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   178
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   179
  ML_Antiquotation.inline_embedded \<^binding>\<open>syntax_const\<close>
74331
b9a3d2fb53e0 clarified signature;
wenzelm
parents: 74317
diff changeset
   180
    (Args.context -- Scan.lift Args.embedded_position >> (fn (ctxt, arg) =>
b9a3d2fb53e0 clarified signature;
wenzelm
parents: 74317
diff changeset
   181
      ML_Syntax.print_string (Proof_Context.check_syntax_const ctxt arg))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   182
69595
ec135235fbcc mark for isabelle update -u control_cartouches;
wenzelm
parents: 69592
diff changeset
   183
  ML_Antiquotation.inline_embedded \<^binding>\<open>const\<close>
63120
629a4c5e953e embedded content may be delimited via cartouches;
wenzelm
parents: 62900
diff changeset
   184
    (Args.context -- Scan.lift (Parse.position Args.embedded_inner_syntax) -- Scan.optional
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   185
        (Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) []
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   186
      >> (fn ((ctxt, (raw_c, pos)), Ts) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   187
        let
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   188
          val Const (c, _) =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   189
            Proof_Context.read_const {proper = true, strict = true} ctxt raw_c;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   190
          val consts = Proof_Context.consts_of ctxt;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   191
          val n = length (Consts.typargs consts (c, Consts.type_scheme consts c));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   192
          val _ = length Ts <> n andalso
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   193
            error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   194
              quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   195
          val const = Const (c, Consts.instance consts (c, Ts));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   196
        in ML_Syntax.atomic (ML_Syntax.print_term const) end)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   197
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   198
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   199
(* object-logic judgment *)
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   200
74344
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   201
fun make_judgment ctxt =
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   202
  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
   203
  in fn t => Const const $ t end;
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   204
74344
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   205
fun dest_judgment ctxt =
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   206
  let
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   207
    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
   208
    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
   209
  in
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   210
    fn t =>
1c2c0380d58b clarified partial application: immediate check of object-logic, and avoidance of context within closure;
wenzelm
parents: 74341
diff changeset
   211
      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
   212
      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
   213
  end;
74341
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   214
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   215
val _ = Theory.setup
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   216
 (ML_Antiquotation.value \<^binding>\<open>make_judgment\<close>
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   217
   (Scan.succeed "ML_Antiquotations1.make_judgment ML_context") #>
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   218
  ML_Antiquotation.value \<^binding>\<open>dest_judgment\<close>
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   219
   (Scan.succeed "ML_Antiquotations1.dest_judgment ML_context"));
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   220
edf8b141a8c4 ML antiquotations for object-logic judgment;
wenzelm
parents: 74331
diff changeset
   221
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   222
(* type/term constructors *)
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   223
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   224
local
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   225
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   226
fun read_embedded ctxt src parse =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   227
  let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   228
    val keywords = Thy_Header.get_keywords' ctxt;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   229
    val input = #1 (Token.syntax (Scan.lift Args.embedded_input) src ctxt);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   230
    val syms = Input.source_explode input;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   231
  in
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   232
    (case Token.read_body keywords parse syms of
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   233
      SOME res => res
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   234
    | NONE => error ("Bad input" ^ Position.here (Input.pos_of input)))
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   235
  end;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   236
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   237
fun ml_sources ctxt srcs =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   238
  let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   239
    val (decls, ctxt') = fold_map (ML_Context.expand_antiquotes o ML_Lex.read_source) srcs ctxt;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   240
    fun decl' ctxt'' = map (fn decl => decl ctxt'') decls;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   241
  in (decl', ctxt') end
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   242
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   243
val parse_name = Parse.input Parse.name;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   244
val parse_args = Scan.repeat (Parse.input Parse.underscore || Parse.embedded_input);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   245
val parse_for_args =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   246
  Scan.optional ((Parse.position (Parse.$$$ "for") >> #2) -- Parse.!!! parse_args)
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   247
    (Position.none, []);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   248
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   249
fun parse_body b =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   250
  if b then Parse.$$$ "=>" |-- Parse.!!! Parse.embedded_input >> single
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   251
  else Scan.succeed [];
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   252
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   253
fun is_dummy s = Input.string_of s = "_";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   254
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   255
val ml = ML_Lex.tokenize_no_range;
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   256
val ml_range = ML_Lex.tokenize_range;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   257
val ml_dummy = ml "_";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   258
fun ml_parens x = ml "(" @ x @ ml ")";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   259
fun ml_bracks x = ml "[" @ x @ ml "]";
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   260
fun ml_commas xs = flat (separate (ml ", ") xs);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   261
val ml_list = ml_bracks o ml_commas;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   262
val ml_string = ml o ML_Syntax.print_string;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   263
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
   264
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   265
fun type_antiquotation binding {function} =
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   266
  ML_Context.add_antiquotation binding true
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   267
    (fn range => fn src => fn ctxt =>
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   268
      let
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   269
        val ((s, type_args), fn_body) =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   270
          read_embedded ctxt src (parse_name -- parse_args -- parse_body function);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   271
        val pos = Input.pos_of s;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   272
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   273
        val Type (c, Ts) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   274
          Proof_Context.read_type_name {proper = true, strict = true} ctxt
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   275
            (Syntax.implode_input s);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   276
        val n = length Ts;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   277
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   278
          length type_args = n orelse
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   279
            error ("Type constructor " ^ quote (Proof_Context.markup_type ctxt c) ^
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   280
              " takes " ^ string_of_int n ^ " argument(s)" ^ Position.here pos);
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 (decls1, ctxt1) = ml_sources ctxt type_args;
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   283
        val (decls2, ctxt2) = (ml_sources ctxt1) fn_body;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   284
        fun decl' ctxt' =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   285
          let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   286
            val (ml_args_env, ml_args_body) = split_list (decls1 ctxt');
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   287
            val (ml_fn_env, ml_fn_body) = split_list (decls2 ctxt');
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   288
            val ml1 = ml_parens (ml "Term.Type " @ ml_pair (ml_string c, ml_list ml_args_body));
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   289
            val ml2 =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   290
              if function then
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   291
                ml "(" @ ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   292
                ml "| T => " @ ml_range range "raise" @
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   293
                ml " Term.TYPE (" @ ml_string ("Type_fn " ^ quote c) @ ml ", [T], []))"
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   294
              else ml1;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   295
          in (flat (ml_args_env @ ml_fn_env), ml2) end;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   296
      in (decl', ctxt2) end);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   297
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   298
fun const_antiquotation binding {pattern, function} =
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   299
  ML_Context.add_antiquotation binding true
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   300
    (fn range => fn src => fn ctxt =>
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   301
      let
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   302
        val (((s, type_args), (for_pos, term_args)), fn_body) =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   303
          read_embedded ctxt src (parse_name -- parse_args -- parse_for_args -- parse_body function);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   304
        val _ = Context_Position.report ctxt for_pos (Markup.keyword_properties Markup.keyword1);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   305
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   306
        val Const (c, T) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   307
          Proof_Context.read_const {proper = true, strict = true} ctxt
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   308
            (Syntax.implode_input s);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   309
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   310
        val consts = Proof_Context.consts_of ctxt;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   311
        val type_paths = Consts.type_arguments consts c;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   312
        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
   313
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   314
        val n = length type_params;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   315
        val m = length (Term.binder_types T);
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   316
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   317
        fun err msg =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   318
          error ("Constant " ^ quote (Proof_Context.markup_const ctxt c) ^ msg ^
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   319
            Position.here (Input.pos_of s));
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   320
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   321
          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
   322
        val _ =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   323
          length term_args > m andalso Term.is_Type (Term.body_type T) andalso
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   324
            err (" cannot have more than " ^ string_of_int m ^ " type argument(s)");
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   325
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   326
        val (decls1, ctxt1) = ml_sources ctxt type_args;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   327
        val (decls2, ctxt2) = ml_sources ctxt1 term_args;
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   328
        val (decls3, ctxt3) = ml_sources ctxt2 fn_body;
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   329
        fun decl' ctxt' =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   330
          let
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   331
            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
   332
            val (ml_args_env2, ml_args_body2) = split_list (decls2 ctxt');
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   333
            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
   334
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   335
            val relevant = map is_dummy type_args ~~ type_paths;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   336
            fun relevant_path is =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   337
              not pattern orelse
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   338
                let val p = rev is
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   339
                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
   340
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   341
            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
   342
            fun ml_typ is (Type (d, Us)) =
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   343
                  if relevant_path is then
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   344
                    ml "Term.Type " @
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   345
                    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
   346
                  else ml_dummy
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   347
              | 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
   348
              | ml_typ _ (TFree _) = raise Match;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   349
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   350
            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
   351
              | 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
   352
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   353
            val ml_env = flat (ml_args_env1 @ ml_args_env2 @ ml_fn_env);
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   354
            val ml1 = ml_parens (ml_app (rev ml_args_body2));
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   355
            val ml2 =
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   356
              if function then
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   357
                ml "(" @ ml_range range "fn " @ ml1 @ ml "=> " @ flat ml_fn_body @
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   358
                ml "| t => " @ ml_range range "raise" @
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   359
                ml " Term.TERM (" @ ml_string ("Const_fn " ^ quote c) @ ml ", [t]))"
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   360
              else ml1;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   361
          in (ml_env, ml2) end;
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   362
      in (decl', ctxt3) end);
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   363
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   364
val _ = Theory.setup
74317
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   365
 (type_antiquotation \<^binding>\<open>Type\<close> {function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   366
  type_antiquotation \<^binding>\<open>Type_fn\<close> {function = true} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   367
  const_antiquotation \<^binding>\<open>Const\<close> {pattern = false, function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   368
  const_antiquotation \<^binding>\<open>Const_\<close> {pattern = true, function = false} #>
0a4e93250e44 support ML antiquotations with fn abstraction;
wenzelm
parents: 74291
diff changeset
   369
  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
   370
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   371
in end;
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   372
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74290
diff changeset
   373
73550
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   374
(* special forms *)
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   375
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   376
val _ = Theory.setup
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   377
 (ML_Antiquotation.special_form \<^binding>\<open>try\<close> "() |> Basics.try" #>
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   378
  ML_Antiquotation.special_form \<^binding>\<open>can\<close> "() |> Basics.can");
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   379
2f6855142a8c support for ML special forms: modified evaluation similar to Scheme;
wenzelm
parents: 70565
diff changeset
   380
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   381
(* basic combinators *)
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
local
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 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
   386
  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
   387
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   388
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
   389
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   390
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
   391
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
   392
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
   393
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
   394
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   395
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
   396
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
   397
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
   398
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
   399
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
   400
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   401
in
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   402
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   403
val _ = Theory.setup
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   404
 (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
   405
    (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
   406
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   407
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   408
      \    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
   409
      \      | 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
   410
      \      | 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
   411
      "  in map f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   412
  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
   413
    (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
   414
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   415
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   416
      \    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
   417
      \      | 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
   418
      \      | 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
   419
      "  in fold f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   420
  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
   421
    (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
   422
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   423
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   424
      \    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
   425
      \      | 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
   426
      \          let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   427
      \            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
   428
      \            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
   429
      \          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
   430
      \      | 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
   431
      "  in fold_map f end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   432
  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
   433
    (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
   434
      "fn list =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   435
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   436
      \    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
   437
      \      | 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
   438
      \          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
   439
      \          in " ^ cons_tuple n ^ "end\n\
59057
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   440
      \  in split_list list end")) #>
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67146
diff changeset
   441
  ML_Antiquotation.value \<^binding>\<open>apply\<close>
59057
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   442
    (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
   443
      (fn (n, opt_index) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   444
        let
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   445
          val cond =
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   446
            (case opt_index of
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   447
              NONE => K true
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   448
            | SOME (index, index_pos) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   449
                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
   450
                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
   451
        in
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   452
          "fn f => fn " ^ tuple_vars "x" n ^ " => " ^
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   453
            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
   454
        end)));
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   455
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   456
end;
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   457
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   458
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   459
(* outer syntax *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   460
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   461
val _ = Theory.setup
69592
a80d8ec6c998 support for isabelle update -u control_cartouches;
wenzelm
parents: 69349
diff changeset
   462
 (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
   463
    (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
   464
      Scan.lift (Parse.embedded_position || Parse.position (Parse.keyword_with (K true)))
64594
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   465
      >> (fn (ctxt, (name, pos)) =>
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   466
        if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   467
          (Context_Position.report ctxt pos (Token.keyword_markup (true, Markup.keyword2) name);
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   468
           "Parse.$$$ " ^ ML_Syntax.print_string name)
4719f13989df more permissive syntax;
wenzelm
parents: 63553
diff changeset
   469
        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
   470
  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
   471
    (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
   472
      (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
   473
        SOME markup =>
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   474
         (Context_Position.reports ctxt [(pos, markup), (pos, Markup.keyword1)];
b65c4370f831 more position information and PIDE markup for command keywords;
wenzelm
parents: 59878
diff changeset
   475
          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
   476
      | NONE => error ("Bad outer syntax command " ^ quote name ^ Position.here pos)))));
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   477
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   478
end;