CookBook/antiquote_setup.ML
author Christian Urban <urbanc@in.tum.de>
Thu, 12 Mar 2009 15:43:22 +0000
changeset 172 ec47352e99c2
parent 171 18f90044c777
child 182 4d0e2edd476d
permissions -rw-r--r--
improved the solution for the simproc/conversion exercise
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
106
bdd82350cf22 renamed in the pdf all instances of cookbook to tutorial (in order to sound more serious)
Christian Urban <urbanc@in.tum.de>
parents: 103
diff changeset
     1
(* Auxiliary antiquotations for the tutorial. *)
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
structure AntiquoteSetup: sig end =
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
struct
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
     6
(* functions for generating appropriate expressions *)
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
fun ml_val_open (ys, xs) txt = 
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  let fun ml_val_open_aux ys txt = 
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
          "fn " ^ (case ys of [] => "_" | _ => enclose "(" ")" (commas ys)) ^ " => (" ^ txt ^ ")";
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  in
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    (case xs of
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
       [] => ml_val_open_aux ys txt
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
     | _  => ml_val_open_aux ys ("let open " ^ (space_implode " " xs) ^ " in " ^ txt ^ " end"))
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  end;
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
fun ml_val txt = ml_val_open ([],[]) txt;
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    18
fun ml_pat (lhs, pat) =
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    19
  let 
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    20
     val pat' = implode (map (fn "\\<dots>" => "_" | s => s) (Symbol.explode pat))
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    21
  in "val " ^ pat' ^ " = " ^ lhs end;
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
fun ml_struct txt = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end";
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
fun ml_type txt = "val _ = NONE : (" ^ txt ^ ") option";
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    26
(* eval function *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    27
fun eval_fn ctxt exp =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    28
  ML_Context.eval_in (SOME ctxt) false Position.none exp
57
065f472c09ab Repaired output of marginal comments in ML antiquotation.
berghofe
parents: 54
diff changeset
    29
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    30
(* string functions *)
103
fe10da5354a3 further cleanup
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    31
fun string_explode str txt =
fe10da5354a3 further cleanup
Christian Urban <urbanc@in.tum.de>
parents: 102
diff changeset
    32
  map (fn s => str ^ s) (space_explode "\n" txt)
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    34
val transform_cmts_str =
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    35
     Source.of_string 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    36
  #> ML_Lex.source 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    37
  #> Source.exhaust 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    38
  #> Chunks.transform_cmts 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    39
  #> implode 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    40
  #> string_explode "";
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    41
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    42
(* output function *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    43
fun output_fn txt =  Chunks.output (map Pretty.str txt)
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    44
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    45
(* checks and prints open expressions *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    46
fun output_ml {context = ctxt, ...} (txt, ovars) =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    47
  (eval_fn ctxt (ml_val_open ovars txt);
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    48
   output_fn (transform_cmts_str txt))
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    49
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    50
val parser_ml = Scan.lift (Args.name --
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    51
  (Scan.optional (Args.$$$ "for" |-- OuterParse.!!! (Scan.repeat1 Args.name)) [] --
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    52
   Scan.optional (Args.$$$ "in"  |-- OuterParse.!!! (Scan.repeat1 Args.name)) [])) 
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    54
(* checks and prints types and structures *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    55
fun output_exp ml {context = ctxt, ...} txt = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    56
  (eval_fn ctxt (ml txt);
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    57
   output_fn (string_explode "" txt))
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    59
(* checks and expression agains a result pattern *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    60
fun output_response {context = ctxt, ...} (lhs, pat) = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    61
    (eval_fn ctxt (ml_pat (lhs, pat));
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    62
     output_fn ((string_explode "" lhs) @ (string_explode "> " pat)))
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    63
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    64
(* checks the expressions, but does not check it against a result pattern *)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    65
fun output_response_fake {context = ctxt, ...} (lhs, pat) = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    66
    (eval_fn ctxt (ml_val lhs);
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    67
     output_fn ((string_explode "" lhs) @ (string_explode "> " pat)))
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    68
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    69
(* checks the expressions, but does not check it against a result pattern *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    70
fun ouput_response_fake_both _ (lhs, pat) = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    71
    output_fn ((string_explode "" lhs) @ (string_explode "> " pat))
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    72
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    73
val single_arg = Scan.lift (Args.name)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    74
val two_args   = Scan.lift (Args.name -- Args.name)
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    75
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    76
val _ = ThyOutput.antiquotation "ML" parser_ml output_ml
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    77
val _ = ThyOutput.antiquotation "ML_type" single_arg (output_exp ml_type)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    78
val _ = ThyOutput.antiquotation "ML_struct" single_arg (output_exp ml_struct)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    79
val _ = ThyOutput.antiquotation "ML_response" two_args output_response
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    80
val _ = ThyOutput.antiquotation "ML_response_fake" two_args output_response_fake
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    81
val _ = ThyOutput.antiquotation "ML_response_fake_both" two_args ouput_response_fake_both
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    82
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    83
(* checks whether a file exists in the Isabelle distribution *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    84
fun check_file_exists _ txt =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    85
  (if File.exists (Path.append (Path.explode ("~~/src")) (Path.explode txt)) 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    86
   then output_fn (string_explode "" txt)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    87
   else error ("Source file " ^ (quote txt) ^ " does not exist."))
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    88
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    89
val _ = ThyOutput.antiquotation "ML_file" single_arg check_file_exists
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    90
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 98
diff changeset
    92
(* replaces the official subgoal antiquotation with one *)
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 98
diff changeset
    93
(* that is closer to the actual output                  *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    94
fun output_goals  {state = node, ...}  _ = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    95
let
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
    96
  fun subgoals 0 = ""
96
018bfa718982 (re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
parents: 90
diff changeset
    97
    | subgoals 1 = "goal (1 subgoal):"
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    98
    | subgoals n = "goal (" ^ string_of_int n ^ " subgoals):"
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    99
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   100
  fun proof_state state =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   101
    (case try Toplevel.proof_of state of
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   102
      SOME prf => prf
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   103
    | _ => error "No proof state")
96
018bfa718982 (re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
parents: 90
diff changeset
   104
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   105
  val state = proof_state node;
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 98
diff changeset
   106
  val goals = Proof.pretty_goals false state;
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   107
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   108
  val {prop, ...} = rep_thm (Proof.get_goal state |> snd |> snd);
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   109
  val (As, B) = Logic.strip_horn prop;
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   110
  val output  = (case (length As) of
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   111
                      0 => goals 
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   112
                    | n => (Pretty.str (subgoals n))::goals)  
96
018bfa718982 (re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
parents: 90
diff changeset
   113
in 
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   114
  ThyOutput.output output
96
018bfa718982 (re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
parents: 90
diff changeset
   115
end
165
890fbfef6d6b partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 112
diff changeset
   116
96
018bfa718982 (re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Christian Urban <urbanc@in.tum.de>
parents: 90
diff changeset
   117
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   118
val _ = ThyOutput.antiquotation "subgoals" (Scan.succeed ()) output_goals
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
   
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
end;