CookBook/Solutions.thy
changeset 56 126646f2aa88
parent 47 4daf913fdbe1
child 58 f3794c231898
equal deleted inserted replaced
55:0b55402ae95e 56:126646f2aa88
    21 ML {*
    21 ML {*
    22 fun make_sum t1 t2 =
    22 fun make_sum t1 t2 =
    23     HOLogic.mk_nat (HOLogic.dest_nat t1 + HOLogic.dest_nat t2)
    23     HOLogic.mk_nat (HOLogic.dest_nat t1 + HOLogic.dest_nat t2)
    24 *}
    24 *}
    25 
    25 
       
    26 text {* \solution{ex:scancmts} *}
       
    27 
       
    28 ML {*
       
    29 val any = Scan.one (Symbol.not_eof);
       
    30 
       
    31 val scan_cmt =
       
    32   let
       
    33     val begin_cmt = Scan.this_string "(*" 
       
    34     val end_cmt = Scan.this_string "*)"
       
    35   in
       
    36    begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt 
       
    37    >> (enclose "(**" "**)" o implode)
       
    38   end
       
    39 
       
    40 val scan_all =
       
    41   Scan.finite Symbol.stopper (Scan.repeat (scan_cmt || any)) 
       
    42     >> implode #> fst 
       
    43 *}
       
    44 
       
    45 text {*
       
    46   By using @{ML_text "#> fst"} in the last line, the function 
       
    47   @{ML scan_all} retruns a string instead of a pair. For example:
       
    48 
       
    49   @{ML_response [display] "scan_all (explode \"foo bar\")" "\"foo bar\""}
       
    50 
       
    51   @{ML_response [display] "scan_all (explode \"foo (*test*) bar (*test*)\")" 
       
    52                           "\"foo (**test**) bar (**test**)\""}
       
    53 
       
    54 *}
       
    55 
    26 end
    56 end