src/Pure/ML/ml_antiquotations.ML
author wenzelm
Wed, 10 Dec 2014 19:24:54 +0100
changeset 59127 723b11f8ffbf
parent 59112 e670969f34df
child 59573 d09cc83cdce9
permissions -rw-r--r--
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_antiquotations.ML
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     3
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     4
Miscellaneous ML antiquotations.
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     5
*)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     6
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     7
structure ML_Antiquotations: sig end =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     8
struct
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
     9
58632
wenzelm
parents: 58046
diff changeset
    10
(* ML support *)
wenzelm
parents: 58046
diff changeset
    11
wenzelm
parents: 58046
diff changeset
    12
val _ = Theory.setup
wenzelm
parents: 58046
diff changeset
    13
 (ML_Antiquotation.inline @{binding assert}
wenzelm
parents: 58046
diff changeset
    14
    (Scan.succeed "(fn b => if b then () else raise General.Fail \"Assertion failed\")") #>
wenzelm
parents: 58046
diff changeset
    15
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    16
  ML_Antiquotation.inline @{binding make_string}
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    17
    (Args.context >> (ml_make_string o ML_Context.struct_name)) #>
58632
wenzelm
parents: 58046
diff changeset
    18
wenzelm
parents: 58046
diff changeset
    19
  ML_Antiquotation.declaration @{binding print}
wenzelm
parents: 58046
diff changeset
    20
    (Scan.lift (Scan.optional Args.name "Output.writeln"))
wenzelm
parents: 58046
diff changeset
    21
      (fn src => fn output => fn ctxt =>
wenzelm
parents: 58046
diff changeset
    22
        let
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    23
          val struct_name = ML_Context.struct_name ctxt;
58632
wenzelm
parents: 58046
diff changeset
    24
          val (_, pos) = Token.name_of_src src;
59112
e670969f34df expand ML cartouches to Input.source;
wenzelm
parents: 59064
diff changeset
    25
          val (a, ctxt') = ML_Context.variant "output" ctxt;
58632
wenzelm
parents: 58046
diff changeset
    26
          val env =
wenzelm
parents: 58046
diff changeset
    27
            "val " ^ a ^ ": string -> unit =\n\
wenzelm
parents: 58046
diff changeset
    28
            \  (" ^ output ^ ") o (fn s => s ^ Position.here (" ^
wenzelm
parents: 58046
diff changeset
    29
            ML_Syntax.print_position pos ^ "));\n";
wenzelm
parents: 58046
diff changeset
    30
          val body =
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    31
            "(fn x => (" ^ struct_name ^ "." ^ a ^ " (" ^ ml_make_string struct_name ^ " x); x))";
58632
wenzelm
parents: 58046
diff changeset
    32
        in (K (env, body), ctxt') end));
wenzelm
parents: 58046
diff changeset
    33
wenzelm
parents: 58046
diff changeset
    34
wenzelm
parents: 58046
diff changeset
    35
(* formal entities *)
wenzelm
parents: 58046
diff changeset
    36
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    37
val _ = Theory.setup
56467
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56465
diff changeset
    38
 (ML_Antiquotation.value @{binding system_option}
56465
6ad693903e22 more positions and markup;
wenzelm
parents: 56405
diff changeset
    39
    (Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) =>
56467
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56465
diff changeset
    40
      (Context_Position.report ctxt pos (Options.default_markup (name, pos));
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56465
diff changeset
    41
        ML_Syntax.print_string name))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    42
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    43
  ML_Antiquotation.value @{binding theory}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    44
    (Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    45
      (Context_Position.report ctxt pos
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    46
        (Theory.get_markup (Context.get_theory (Proof_Context.theory_of ctxt) name));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    47
       "Context.get_theory (Proof_Context.theory_of ML_context) " ^ ML_Syntax.print_string name))
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    48
    || Scan.succeed "Proof_Context.theory_of ML_context") #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    49
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    50
  ML_Antiquotation.value @{binding theory_context}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    51
    (Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (name, pos)) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    52
      (Context_Position.report ctxt pos
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    53
        (Theory.get_markup (Context.get_theory (Proof_Context.theory_of ctxt) name));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    54
       "Proof_Context.get_global (Proof_Context.theory_of ML_context) " ^
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    55
        ML_Syntax.print_string name))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    56
59127
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    57
  ML_Antiquotation.inline @{binding context}
723b11f8ffbf more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents: 59112
diff changeset
    58
    (Args.context >> (fn ctxt => ML_Context.struct_name ctxt ^ ".ML_context")) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    59
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    60
  ML_Antiquotation.inline @{binding typ} (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    61
  ML_Antiquotation.inline @{binding term} (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    62
  ML_Antiquotation.inline @{binding prop} (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    63
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    64
  ML_Antiquotation.value @{binding ctyp} (Args.typ >> (fn T =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    65
    "Thm.ctyp_of (Proof_Context.theory_of ML_context) " ^
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    66
      ML_Syntax.atomic (ML_Syntax.print_typ T))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    67
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    68
  ML_Antiquotation.value @{binding cterm} (Args.term >> (fn t =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    69
    "Thm.cterm_of (Proof_Context.theory_of ML_context) " ^
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    70
     ML_Syntax.atomic (ML_Syntax.print_term t))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    71
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    72
  ML_Antiquotation.value @{binding cprop} (Args.prop >> (fn t =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    73
    "Thm.cterm_of (Proof_Context.theory_of ML_context) " ^
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    74
     ML_Syntax.atomic (ML_Syntax.print_term t))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    75
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    76
  ML_Antiquotation.value @{binding cpat}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    77
    (Args.context --
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    78
      Scan.lift Args.name_inner_syntax >> uncurry Proof_Context.read_term_pattern >> (fn t =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    79
        "Thm.cterm_of (Proof_Context.theory_of ML_context) " ^
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    80
          ML_Syntax.atomic (ML_Syntax.print_term t))));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    81
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    82
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    83
(* type classes *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    84
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    85
fun class syn = Args.context -- Scan.lift Args.name_inner_syntax >> (fn (ctxt, s) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    86
  Proof_Context.read_class ctxt s
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    87
  |> syn ? Lexicon.mark_class
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    88
  |> ML_Syntax.print_string);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    89
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    90
val _ = Theory.setup
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    91
 (ML_Antiquotation.inline @{binding class} (class false) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    92
  ML_Antiquotation.inline @{binding class_syntax} (class true) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    93
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    94
  ML_Antiquotation.inline @{binding sort}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    95
    (Args.context -- Scan.lift Args.name_inner_syntax >> (fn (ctxt, s) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    96
      ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s)))));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    97
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    98
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
    99
(* type constructors *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   100
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   101
fun type_name kind check = Args.context -- Scan.lift (Parse.position Args.name_inner_syntax)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   102
  >> (fn (ctxt, (s, pos)) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   103
    let
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   104
      val Type (c, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   105
      val decl = Type.the_decl (Proof_Context.tsig_of ctxt) (c, pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   106
      val res =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   107
        (case try check (c, decl) of
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   108
          SOME res => res
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   109
        | NONE => error ("Not a " ^ kind ^ ": " ^ quote c ^ Position.here pos));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   110
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   111
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   112
val _ = Theory.setup
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   113
 (ML_Antiquotation.inline @{binding type_name}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   114
    (type_name "logical type" (fn (c, Type.LogicalType _) => c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   115
  ML_Antiquotation.inline @{binding type_abbrev}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   116
    (type_name "type abbreviation" (fn (c, Type.Abbreviation _) => c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   117
  ML_Antiquotation.inline @{binding nonterminal}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   118
    (type_name "nonterminal" (fn (c, Type.Nonterminal) => c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   119
  ML_Antiquotation.inline @{binding type_syntax}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   120
    (type_name "type" (fn (c, _) => Lexicon.mark_type c)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   121
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   122
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   123
(* constants *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   124
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   125
fun const_name check = Args.context -- Scan.lift (Parse.position Args.name_inner_syntax)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   126
  >> (fn (ctxt, (s, pos)) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   127
    let
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   128
      val Const (c, _) = Proof_Context.read_const {proper = true, strict = false} ctxt s;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   129
      val res = check (Proof_Context.consts_of ctxt, c)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   130
        handle TYPE (msg, _, _) => error (msg ^ Position.here pos);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   131
    in ML_Syntax.print_string res end);
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   132
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   133
val _ = Theory.setup
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   134
 (ML_Antiquotation.inline @{binding const_name}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   135
    (const_name (fn (consts, c) => (Consts.the_const consts c; c))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   136
  ML_Antiquotation.inline @{binding const_abbrev}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   137
    (const_name (fn (consts, c) => (Consts.the_abbreviation consts c; c))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   138
  ML_Antiquotation.inline @{binding const_syntax}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   139
    (const_name (fn (_, c) => Lexicon.mark_const c)) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   140
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   141
  ML_Antiquotation.inline @{binding syntax_const}
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   142
    (Args.context -- Scan.lift (Parse.position Args.name) >> (fn (ctxt, (c, pos)) =>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   143
      if is_some (Syntax.lookup_const (Proof_Context.syn_of ctxt) c)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   144
      then ML_Syntax.print_string c
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   145
      else error ("Unknown syntax const: " ^ quote c ^ Position.here pos))) #>
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   146
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   147
  ML_Antiquotation.inline @{binding const}
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   148
    (Args.context -- Scan.lift (Parse.position Args.name_inner_syntax) -- Scan.optional
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   149
        (Scan.lift (Args.$$$ "(") |-- Parse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) []
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   150
      >> (fn ((ctxt, (raw_c, pos)), Ts) =>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   151
        let
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   152
          val Const (c, _) =
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   153
            Proof_Context.read_const {proper = true, strict = true} ctxt raw_c;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   154
          val consts = Proof_Context.consts_of ctxt;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   155
          val n = length (Consts.typargs consts (c, Consts.type_scheme consts c));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   156
          val _ = length Ts <> n andalso
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   157
            error ("Constant requires " ^ string_of_int n ^ " type argument(s): " ^
56251
73e2e1912571 tuned message;
wenzelm
parents: 56205
diff changeset
   158
              quote c ^ enclose "(" ")" (commas (replicate n "_")) ^ Position.here pos);
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   159
          val const = Const (c, Consts.instance consts (c, Ts));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   160
        in ML_Syntax.atomic (ML_Syntax.print_term const) end)));
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   161
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   162
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   163
(* basic combinators *)
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   164
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   165
local
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   166
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   167
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
   168
  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
   169
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   170
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
   171
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   172
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
   173
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
   174
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
   175
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
   176
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   177
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
   178
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
   179
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
   180
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
   181
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
   182
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   183
in
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   184
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   185
val _ = Theory.setup
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   186
 (ML_Antiquotation.value @{binding map}
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   187
    (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
   188
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   189
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   190
      \    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
   191
      \      | 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
   192
      \      | 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
   193
      "  in map f end")) #>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   194
  ML_Antiquotation.value @{binding fold}
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   195
    (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
   196
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   197
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   198
      \    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
   199
      \      | 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
   200
      \      | 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
   201
      "  in fold f end")) #>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   202
  ML_Antiquotation.value @{binding fold_map}
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   203
    (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
   204
      "fn f =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   205
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   206
      \    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
   207
      \      | 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
   208
      \          let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   209
      \            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
   210
      \            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
   211
      \          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
   212
      \      | 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
   213
      "  in fold_map f end")) #>
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   214
  ML_Antiquotation.value @{binding split_list}
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   215
    (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
   216
      "fn list =>\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   217
      \  let\n\
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   218
      \    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
   219
      \      | 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
   220
      \          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
   221
      \          in " ^ cons_tuple n ^ "end\n\
59057
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   222
      \  in split_list list end")) #>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   223
  ML_Antiquotation.value @{binding apply}
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   224
    (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
   225
      (fn (n, opt_index) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   226
        let
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   227
          val cond =
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   228
            (case opt_index of
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   229
              NONE => K true
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   230
            | SOME (index, index_pos) =>
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   231
                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
   232
                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
   233
        in
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   234
          "fn f => fn " ^ tuple_vars "x" n ^ " => " ^
5b649fb2f2e1 added ML antiquotation @{apply n} or @{apply n(k)};
wenzelm
parents: 58978
diff changeset
   235
            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
   236
        end)));
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   237
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   238
end;
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   239
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58632
diff changeset
   240
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   241
(* outer syntax *)
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   242
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   243
val _ = Theory.setup
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   244
 (ML_Antiquotation.value @{binding keyword}
58932
wenzelm
parents: 58928
diff changeset
   245
    (Args.theory -- Scan.lift (Parse.position Parse.string) >> (fn (thy, (name, pos)) =>
wenzelm
parents: 58928
diff changeset
   246
      if Keyword.is_keyword (Thy_Header.get_keywords thy) name then
wenzelm
parents: 58928
diff changeset
   247
        "Parse.$$$ " ^ ML_Syntax.print_string name
wenzelm
parents: 58928
diff changeset
   248
      else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos))) #>
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   249
  ML_Antiquotation.value @{binding command_spec}
58932
wenzelm
parents: 58928
diff changeset
   250
    (Args.theory -- Scan.lift (Parse.position Parse.string) >> (fn (thy, (name, pos)) =>
wenzelm
parents: 58928
diff changeset
   251
      if Keyword.is_command (Thy_Header.get_keywords thy) name then
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58634
diff changeset
   252
        ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_position (name, pos)
58932
wenzelm
parents: 58928
diff changeset
   253
      else error ("Bad outer syntax command " ^ quote name ^ Position.here pos))));
56205
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   254
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   255
end;
ceb8a93460b7 clarified modules;
wenzelm
parents:
diff changeset
   256