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