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