ProgTutorial/Parsing.thy
changeset 327 ce754ad78bc9
parent 326 f76135c6c527
child 328 c0cae24b9d46
equal deleted inserted replaced
326:f76135c6c527 327:ce754ad78bc9
   694   even and odd
   694   even and odd
   695 where
   695 where
   696   even0: "even 0"
   696   even0: "even 0"
   697 | evenS: "odd n \<Longrightarrow> even (Suc n)"
   697 | evenS: "odd n \<Longrightarrow> even (Suc n)"
   698 | oddS: "even n \<Longrightarrow> odd (Suc n)"
   698 | oddS: "even n \<Longrightarrow> odd (Suc n)"
       
   699 
   699 
   700 
   700 text {*
   701 text {*
   701   For this we are going to use the parser:
   702   For this we are going to use the parser:
   702 *}
   703 *}
   703 
   704 
   978 
   979 
   979 foobar
   980 foobar
   980 
   981 
   981 text {* 
   982 text {* 
   982   but you will not see any action as we chose to implement this command to do
   983   but you will not see any action as we chose to implement this command to do
   983   nothing. The point of this command is to only show the procedure of how
   984   nothing. The point of this command is only to show the procedure of how
   984   to interact with ProofGeneral. A similar procedure has to be done with any 
   985   to interact with ProofGeneral. A similar procedure has to be done with any 
   985   other new command, and also any new keyword that is introduced with 
   986   other new command, and also any new keyword that is introduced with 
   986   @{ML_ind OuterKeyword.keyword}.
   987   the function @{ML_ind keyword in OuterKeyword}. For example:
   987 *}
   988 *}
   988 
   989 
   989 ML{*val _ = OuterKeyword.keyword "blink" *}
   990 ML{*val _ = OuterKeyword.keyword "blink" *}
   990 
   991 
   991 text {*
   992 text {*
  1003   finally does nothing. For this you can write:
  1004   finally does nothing. For this you can write:
  1004 *}
  1005 *}
  1005 
  1006 
  1006 ML{*let
  1007 ML{*let
  1007   fun trace_prop str = 
  1008   fun trace_prop str = 
  1008      LocalTheory.theory (fn lthy => (tracing str; lthy))
  1009      LocalTheory.theory (fn ctxt => (tracing str; ctxt))
  1009 
  1010 
  1010   val trace_prop_parser = OuterParse.prop >> trace_prop
       
  1011   val kind = OuterKeyword.thy_decl
  1011   val kind = OuterKeyword.thy_decl
  1012 in
  1012 in
  1013   OuterSyntax.local_theory "foobar_trace" "traces a proposition" 
  1013   OuterSyntax.local_theory "foobar_trace" "traces a proposition" 
  1014     kind trace_prop_parser
  1014     kind (OuterParse.prop >> trace_prop)
  1015 end *}
  1015 end *}
  1016 
  1016 
  1017 text {*
  1017 text {*
  1018   The command is now \isacommand{foobar\_trace} and can be used to 
  1018   The command is now \isacommand{foobar\_trace} and can be used to 
  1019   see the proposition in the tracing buffer.  
  1019   see the proposition in the tracing buffer.  
  1034   "local_theory_to_proof" in OuterSyntax} to set up the command.  Note,
  1034   "local_theory_to_proof" in OuterSyntax} to set up the command.  Note,
  1035   however, once you change the ``kind'' of a command from @{ML thy_decl in
  1035   however, once you change the ``kind'' of a command from @{ML thy_decl in
  1036   OuterKeyword} to @{ML thy_goal in OuterKeyword} then the keyword file needs
  1036   OuterKeyword} to @{ML thy_goal in OuterKeyword} then the keyword file needs
  1037   to be re-created!
  1037   to be re-created!
  1038 
  1038 
  1039   Below we change \isacommand{foobar\_trace} so that it takes a proposition as
  1039   Below we show the command \isacommand{foobar\_goal} which takes a
  1040   argument and then starts a proof in order to prove it. Therefore in Line 13, 
  1040   proposition as argument and then starts a proof in order to prove
  1041   we set the kind indicator to @{ML thy_goal in OuterKeyword}.
  1041   it. Therefore in Line 9, we set the kind indicator to @{ML thy_goal in
       
  1042   OuterKeyword}.
  1042 *}
  1043 *}
  1043 
  1044 
  1044 ML%linenosgray{*let
  1045 ML%linenosgray{*let
  1045   fun prove_prop str lthy =
  1046   fun goal_prop str lthy =
  1046     let
  1047     let
  1047       val prop = Syntax.read_prop lthy str
  1048       val prop = Syntax.read_prop lthy str
  1048     in
  1049     in
  1049       Proof.theorem_i NONE (K I) [[(prop,[])]] lthy
  1050       Proof.theorem_i NONE (K I) [[(prop,[])]] lthy
  1050     end;
  1051     end
  1051   
  1052   
  1052   val prove_prop_parser = OuterParse.prop >> prove_prop 
       
  1053   val kind = OuterKeyword.thy_goal
  1053   val kind = OuterKeyword.thy_goal
  1054 in
  1054 in
  1055   OuterSyntax.local_theory_to_proof "foobar_goal" "proving a proposition" 
  1055   OuterSyntax.local_theory_to_proof "foobar_goal" "proves a proposition" 
  1056     kind prove_prop_parser
  1056     kind (OuterParse.prop >> goal_prop)
  1057 end *}
  1057 end *}
  1058 
  1058 
  1059 text {*
  1059 text {*
  1060   The function @{text prove_prop} in Lines 2 to 7 takes a string (the proposition to be
  1060   The function @{text goal_prop} in Lines 2 to 7 takes a string (the proposition to be
  1061   proved) and a context as argument.  The context is necessary in order to be able to use
  1061   proved) and a context as argument.  The context is necessary in order to be able to use
  1062   @{ML_ind  read_prop in Syntax}, which converts a string into a proper proposition.
  1062   @{ML_ind  read_prop in Syntax}, which converts a string into a proper proposition.
  1063   In Line 6 the function @{ML_ind  theorem_i in Proof} starts the proof for the
  1063   In Line 6 the function @{ML_ind  theorem_i in Proof} starts the proof for the
  1064   proposition. Its argument @{ML NONE} stands for a locale (which we chose to
  1064   proposition. Its argument @{ML NONE} stands for a locale (which we chose to
  1065   omit); the argument @{ML "(K I)"} stands for a function that determines what
  1065   omit); the argument @{ML "(K I)"} stands for a function that determines what
  1081 apply(rule conjI)
  1081 apply(rule conjI)
  1082 apply(rule TrueI)+
  1082 apply(rule TrueI)+
  1083 done
  1083 done
  1084 
  1084 
  1085 text {*
  1085 text {*
       
  1086   {\bf TBD below}
  1086 
  1087 
  1087   (FIXME: read a name and show how to store theorems; see @{ML_ind  note in LocalTheory})
  1088   (FIXME: read a name and show how to store theorems; see @{ML_ind  note in LocalTheory})
  1088   
  1089   
  1089 *}
  1090 *}
  1090 
  1091