ProgTutorial/antiquote_setup.ML
author Christian Urban <urbanc@in.tum.de>
Mon, 03 Aug 2009 13:53:04 +0200
changeset 300 f286dfa9f173
parent 261 358f325f4db6
child 302 0cbd34857b9e
permissions -rw-r--r--
removed rail; added external aux-files
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
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
     3
structure AntiquoteSetup =
45
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
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
     6
open OutputTutorial
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
     7
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
     8
(* functions for generating appropriate expressions *)
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
     9
fun ml_val_open ys istruc txt = 
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    10
let 
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    11
  fun ml_val_open_aux ys txt = 
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
          "fn " ^ (case ys of [] => "_" | _ => enclose "(" ")" (commas ys)) ^ " => (" ^ txt ^ ")";
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    13
in
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    14
 (case istruc of
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    15
    NONE    => ml_val_open_aux ys txt
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    16
  | SOME st => ml_val_open_aux ys ("let open " ^ st ^ " in " ^ txt ^ " end"))
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    17
end
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    19
fun ml_val txt = ml_val_open [] NONE txt;
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    21
fun ml_pat (lhs, pat) =
51
c346c156a7cd completes the recipie on antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    22
  let 
261
358f325f4db6 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 258
diff changeset
    23
     val pat' = implode (map (fn "\<dots>" => "_" | s => s) (Symbol.explode pat))
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    24
  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
    25
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
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
    27
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
    28
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    29
(* eval function *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    30
fun eval_fn ctxt exp =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    31
  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
    32
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    33
(* string functions *)
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    34
fun string_explode prefix_str txt =
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    35
  map (fn s => prefix_str ^ s) (split_lines txt)
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    37
val transform_cmts_str =
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    38
     Source.of_string 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    39
  #> ML_Lex.source 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    40
  #> Source.exhaust 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    41
  #> Chunks.transform_cmts 
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    42
  #> implode 
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    43
  #> string_explode ""
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    44
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    45
(* checks and prints open expressions, calculates index entry *)
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    46
fun output_ml {context = ctxt, ...} (txt, (ovars, istruc)) =
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    47
  (eval_fn ctxt (ml_val_open ovars istruc txt);
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    48
   case (istruc, Long_Name.base_name txt, Long_Name.qualifier txt)  of
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    49
     (NONE, bn, "")  => output_indexed (transform_cmts_str txt) {main = Code txt, minor = NoString}
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    50
   | (NONE, bn, qn)  => output_indexed (transform_cmts_str txt) {main = Code bn, minor = IStruc qn}
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    51
   | (SOME st, _, _) => output_indexed (transform_cmts_str txt) {main = Code txt, minor = IStruc st})
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    52
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    53
val parser_ml = Scan.lift (Args.name --
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    54
  (Scan.optional (Args.$$$ "for" |-- OuterParse.!!! (Scan.repeat1 Args.name)) [] --
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    55
   Scan.option (Args.$$$ "in"  |-- OuterParse.!!! 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
    56
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    57
(* checks and prints types and structures *)
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    58
fun output_struct {context = ctxt, ...} txt = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    59
  (eval_fn ctxt (ml_struct txt);
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    60
   output_indexed (string_explode "" txt) {main = Code txt, minor = Plain "structure"})
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    61
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    62
fun output_type {context = ctxt, ...} txt = 
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    63
  (eval_fn ctxt (ml_type txt);
258
03145998190b slightly modified index generation
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
    64
   output_indexed (string_explode "" txt) {main = Code txt, minor = Plain "type"})
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    66
(* checks and expression agains a result pattern *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    67
fun output_response {context = ctxt, ...} (lhs, pat) = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    68
    (eval_fn ctxt (ml_pat (lhs, pat));
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    69
     output ((string_explode "" lhs) @ (string_explode "> " pat)))
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    70
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    71
(* 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
    72
fun output_response_fake {context = ctxt, ...} (lhs, pat) = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    73
    (eval_fn ctxt (ml_val lhs);
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    74
     output ((string_explode "" lhs) @ (string_explode "> " pat)))
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    75
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    76
(* 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
    77
fun ouput_response_fake_both _ (lhs, pat) = 
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    78
    output ((string_explode "" lhs) @ (string_explode "> " pat))
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    79
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    80
val single_arg = Scan.lift (Args.name)
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    81
val two_args   = Scan.lift (Args.name -- Args.name)
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    82
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    83
val _ = ThyOutput.antiquotation "ML" parser_ml output_ml
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    84
val _ = ThyOutput.antiquotation "ML_type" single_arg output_type
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
    85
val _ = ThyOutput.antiquotation "ML_struct" single_arg output_struct
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    86
val _ = ThyOutput.antiquotation "ML_response" two_args output_response
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    87
val _ = ThyOutput.antiquotation "ML_response_fake" two_args output_response_fake
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    88
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
    89
182
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    90
fun href_link txt =
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    91
let 
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    92
  val raw = Symbol.encode_raw
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    93
  val path = "http://isabelle.in.tum.de/repos/isabelle/raw-file/tip/src/"    
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    94
in
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    95
 (raw "\\href{") ^ (raw path) ^ (raw txt) ^ (raw "}{") ^ txt ^ (raw "}")
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    96
end 
4d0e2edd476d added hyperlinks for every file pointer
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
    97
112
a90d0fb24e75 improved
Christian Urban <urbanc@in.tum.de>
parents: 106
diff changeset
    98
(* checks whether a file exists in the Isabelle distribution *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
    99
fun check_file_exists _ txt =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   100
  (if File.exists (Path.append (Path.explode ("~~/src")) (Path.explode txt)) 
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   101
   then output [href_link txt]
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   102
   else error ("Source file " ^ (quote txt) ^ " does not exist."))
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   103
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   104
val _ = ThyOutput.antiquotation "ML_file" single_arg check_file_exists
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   105
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
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
   107
(* 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
   108
(* that is closer to the actual output                  *)
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   109
fun output_goals  {state = node, ...}  _ = 
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   110
let
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   111
  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
   112
    | subgoals 1 = "goal (1 subgoal):"
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   113
    | subgoals n = "goal (" ^ string_of_int n ^ " subgoals):"
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   114
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   115
  fun proof_state state =
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   116
    (case try Toplevel.proof_of state of
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   117
      SOME prf => prf
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   118
    | _ => 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
   119
98
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   120
  val state = proof_state node;
209
17b1512f51af soem slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   121
  val goals = Pretty.chunks (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
   122
0a5c95f4d70c calculated the exact number of goals in the subgoal antiquotation
Christian Urban <urbanc@in.tum.de>
parents: 96
diff changeset
   123
  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
   124
  val (As, B) = Logic.strip_horn prop;
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   125
  val output  = (case (length As) of
209
17b1512f51af soem slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   126
                      0 => [goals] 
17b1512f51af soem slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   127
                    | 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
   128
in 
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   129
  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
   130
end
165
890fbfef6d6b partially adapted to new antiquotation infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 112
diff changeset
   131
171
18f90044c777 simplified antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 165
diff changeset
   132
val _ = ThyOutput.antiquotation "subgoals" (Scan.succeed ()) output_goals
209
17b1512f51af soem slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   133
 
45
78aeca00bb54 deleted old antiquote_setup and renamed antiquote_setup_plus to antiquuote_setup
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
end;