libisabelle-protocol/protocol/Protocol.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 06 Apr 2016 16:56:47 +0200
changeset 59216 f4693c6f4bb2
permissions -rw-r--r--
update libisabelle-0.2.2 to libisabelle 0.3.3

Note: probably the dependency between
Protocol.thy and Isac_Protocol.thy need to be reverted
     1 theory Protocol
     2 imports Codec_Class 
     3 keywords "operation_setup" :: thy_decl % "ML"
     4 begin
     5 
     6 ML\<open>
     7 val _ =
     8   let
     9     open Protocol
    10     fun operation_setup_cmd name source (flags as {auto, ...}) ctxt =
    11       let
    12         fun eval enclose =
    13           ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of source)
    14             (ML_Lex.read ("Protocol.add_operation " ^ ML_Syntax.print_string name ^ "(") @
    15               enclose (ML_Lex.read_source false source) @
    16               ML_Lex.read ")" @
    17               ML_Lex.read (print_flags flags))
    18       in
    19         if auto then
    20           let
    21             val ML_Types.Fun (arg, res) = ML_Types.ml_type_of ctxt (Input.source_content source)
    22             val arg_codec = Classy.resolve @{ML.class codec} arg (Context.Proof ctxt)
    23             val res_codec = Classy.resolve @{ML.class codec} res (Context.Proof ctxt)
    24             fun enclose toks =
    25               ML_Lex.read "{from_lib=" @ ML_Lex.read arg_codec @ ML_Lex.read "," @
    26               ML_Lex.read "to_lib=" @ ML_Lex.read res_codec @ ML_Lex.read "," @
    27               ML_Lex.read "action=" @ toks @ ML_Lex.read "}"
    28           in
    29             eval enclose
    30           end
    31         else
    32           eval I
    33       end
    34     val parse_flag =
    35       (Parse.reserved "sequential" || Parse.reserved "bracket" || Parse.reserved "auto") >>
    36         (fn flag => join_flags
    37            {sequential = flag = "sequential",
    38             bracket = flag = "bracket",
    39             auto = flag = "auto"})
    40     val parse_flags =
    41       Parse.list parse_flag >> (fn fs => fold (curry op o) fs I)
    42     val parse_cmd =
    43       Scan.optional (Args.parens parse_flags) I --
    44       Parse.name --
    45       Parse.!!! (@{keyword "="} |-- Parse.ML_source)
    46   in
    47     Outer_Syntax.command @{command_keyword "operation_setup"} "define protocol operation in ML"
    48       (parse_cmd >> (fn ((flags, name), txt) =>
    49         Toplevel.keep (Toplevel.context_of #> operation_setup_cmd name txt (flags default_flags))))
    50   end
    51 \<close>
    52 
    53 end