src/Pure/ML/ml_antiquote.ML
author wenzelm
Sun, 15 Mar 2009 15:59:43 +0100
changeset 30524 92af4e8c54a6
parent 30513 1796b8ea88aa
child 30721 0579dec9f8ba
permissions -rw-r--r--
ML_Syntax.make_binding;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML/ml_antiquote.ML
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     3
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     4
Common ML antiquotations.
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     5
*)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     6
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     7
signature ML_ANTIQUOTE =
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
     8
sig
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30364
diff changeset
     9
  val macro: string -> Proof.context context_parser -> unit
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    10
  val variant: string -> Proof.context -> string * Proof.context
30513
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30364
diff changeset
    11
  val inline: string -> string context_parser -> unit
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30364
diff changeset
    12
  val declaration: string -> string -> string context_parser -> unit
1796b8ea88aa eliminated type Args.T;
wenzelm
parents: 30364
diff changeset
    13
  val value: string -> string context_parser -> unit
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    14
end;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    15
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    16
structure ML_Antiquote: ML_ANTIQUOTE =
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    17
struct
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    18
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    19
(** generic tools **)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    20
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    21
(* ML names *)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    22
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    23
structure NamesData = ProofDataFun
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    24
(
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    25
  type T = Name.context;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    26
  fun init _ = ML_Syntax.reserved;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    27
);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    28
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    29
fun variant a ctxt =
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    30
  let
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    31
    val names = NamesData.get ctxt;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    32
    val ([b], names') = Name.variants [a] names;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    33
    val ctxt' = NamesData.put names' ctxt;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    34
  in (b, ctxt') end;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    35
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    36
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    37
(* specific antiquotations *)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    38
27379
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    39
fun macro name scan = ML_Context.add_antiq name
27868
a28b3cd0077b ML_Context.add_antiq: pass position;
wenzelm
parents: 27809
diff changeset
    40
  (fn _ => scan :|-- (fn ctxt => Scan.depend (fn _ => Scan.succeed
27379
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    41
    (Context.Proof ctxt, fn {background, ...} => (K ("", ""), background)))));
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    42
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    43
fun inline name scan = ML_Context.add_antiq name
27868
a28b3cd0077b ML_Context.add_antiq: pass position;
wenzelm
parents: 27809
diff changeset
    44
  (fn _ => scan >> (fn s => fn {struct_name, background} => (K ("", s), background)));
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    45
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    46
fun declaration kind name scan = ML_Context.add_antiq name
27868
a28b3cd0077b ML_Context.add_antiq: pass position;
wenzelm
parents: 27809
diff changeset
    47
  (fn _ => scan >> (fn s => fn {struct_name, background} =>
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    48
    let
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    49
      val (a, background') = variant name background;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    50
      val env = kind ^ " " ^ a ^ " = " ^ s ^ ";\n";
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    51
      val body = struct_name ^ "." ^ a;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    52
    in (K (env, body), background') end));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    53
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    54
val value = declaration "val";
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    55
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    56
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    57
30231
b3f3ad327d4d added @{binding} ML antiquotations;
wenzelm
parents: 29606
diff changeset
    58
(** misc antiquotations **)
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    59
27809
a1e409db516b unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents: 27390
diff changeset
    60
structure P = OuterParse;
a1e409db516b unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents: 27390
diff changeset
    61
30524
92af4e8c54a6 ML_Syntax.make_binding;
wenzelm
parents: 30513
diff changeset
    62
val _ = inline "binding"
92af4e8c54a6 ML_Syntax.make_binding;
wenzelm
parents: 30513
diff changeset
    63
  (Scan.lift (P.position Args.name) >> (fn name => ML_Syntax.atomic (ML_Syntax.make_binding name)));
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    64
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    65
val _ = value "theory"
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    66
  (Scan.lift Args.name >> (fn name => "ThyInfo.get_theory " ^ ML_Syntax.print_string name)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    67
  || Scan.succeed "ML_Context.the_global_context ()");
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    68
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    69
val _ = value "theory_ref"
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    70
  (Scan.lift Args.name >> (fn name =>
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    71
    "Theory.check_thy (ThyInfo.theory " ^ ML_Syntax.print_string name ^ ")")
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    72
  || Scan.succeed "Theory.check_thy (ML_Context.the_global_context ())");
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    73
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    74
val _ = value "context" (Scan.succeed "ML_Context.the_local_context ()");
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    75
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
    76
val _ = inline "sort" (Args.context -- Scan.lift Args.name_source >> (fn (ctxt, s) =>
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    77
  ML_Syntax.atomic (ML_Syntax.print_sort (Syntax.read_sort ctxt s))));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    78
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    79
val _ = inline "typ" (Args.typ >> (ML_Syntax.atomic o ML_Syntax.print_typ));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    80
val _ = inline "term" (Args.term >> (ML_Syntax.atomic o ML_Syntax.print_term));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    81
val _ = inline "prop" (Args.prop >> (ML_Syntax.atomic o ML_Syntax.print_term));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    82
27379
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    83
val _ = macro "let" (Args.context --
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
    84
  Scan.lift (P.and_list1 (P.and_list1 Args.name_source -- (Args.$$$ "=" |-- Args.name_source)))
27379
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    85
    >> (fn (ctxt, args) => #2 (ProofContext.match_bind true args ctxt)));
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    86
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    87
val _ = macro "note" (Args.context :|-- (fn ctxt =>
27809
a1e409db516b unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents: 27390
diff changeset
    88
  P.and_list1' (Scan.lift (Args.opt_thm_name I "=") -- Attrib.thms >> (fn ((a, srcs), ths) =>
27379
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    89
    ((a, map (Attrib.attribute (ProofContext.theory_of ctxt)) srcs), [(ths, [])])))
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    90
  >> (fn args => #2 (ProofContext.note_thmss_i "" args ctxt))));
c706b7201826 added macro interface;
wenzelm
parents: 27370
diff changeset
    91
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    92
val _ = value "ctyp" (Args.typ >> (fn T =>
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    93
  "Thm.ctyp_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_typ T)));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    94
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    95
val _ = value "cterm" (Args.term >> (fn t =>
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    96
  "Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    97
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    98
val _ = value "cprop" (Args.prop >> (fn t =>
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
    99
  "Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   100
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   101
val _ = value "cpat"
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
   102
  (Args.context -- Scan.lift Args.name_source >> uncurry ProofContext.read_term_pattern >> (fn t =>
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   103
    "Thm.cterm_of (ML_Context.the_global_context ()) " ^ ML_Syntax.atomic (ML_Syntax.print_term t)));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   104
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   105
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
   106
fun type_ syn = (Args.context -- Scan.lift Args.name_source >> (fn (ctxt, c) =>
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   107
    #1 (Term.dest_Type (ProofContext.read_tyname ctxt c))
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   108
    |> syn ? Long_Name.base_name
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   109
    |> ML_Syntax.print_string));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   110
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   111
val _ = inline "type_name" (type_ false);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   112
val _ = inline "type_syntax" (type_ true);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   113
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   114
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
   115
fun const syn = Args.context -- Scan.lift Args.name_source >> (fn (ctxt, c) =>
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   116
  #1 (Term.dest_Const (ProofContext.read_const_proper ctxt c))
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   117
  |> syn ? ProofContext.const_syntax_name ctxt
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   118
  |> ML_Syntax.print_string);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   119
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   120
val _ = inline "const_name" (const false);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   121
val _ = inline "const_syntax" (const true);
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   122
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   123
val _ = inline "const"
27882
eaa9fef9f4c1 Args.name_source(_position) for proper position information;
wenzelm
parents: 27868
diff changeset
   124
  (Args.context -- Scan.lift Args.name_source -- Scan.optional
27809
a1e409db516b unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents: 27390
diff changeset
   125
      (Scan.lift (Args.$$$ "(") |-- OuterParse.enum1' "," Args.typ --| Scan.lift (Args.$$$ ")")) []
27340
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   126
    >> (fn ((ctxt, c), Ts) =>
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   127
      let val (c, _) = Term.dest_Const (ProofContext.read_const_proper ctxt c)
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   128
      in ML_Syntax.atomic (ML_Syntax.print_term (ProofContext.mk_const ctxt (c, Ts))) end));
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   129
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   130
end;
3de9f20f4e28 Common ML antiquotations.
wenzelm
parents:
diff changeset
   131