CookBook/Solutions.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 04 Feb 2009 20:26:38 +0000
changeset 95 7235374f34c8
parent 80 95e9c4556221
child 130 a21d7b300616
permissions -rw-r--r--
added some preliminary notes about SUBPROOF
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory Solutions
25
e2f9f94b26d4 Antiquotation setup is now contained in theory Base.
berghofe
parents: 15
diff changeset
     2
imports Base
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
chapter {* Solutions to Most Exercises *}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
text {* \solution{fun:revsum} *}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
     9
ML{*fun rev_sum t =
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    10
let
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    11
 fun dest_sum (Const (@{const_name plus}, _) $ u $ u') = u' :: dest_sum u
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    12
   | dest_sum u = [u]
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    13
 in
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
    14
   foldl1 (HOLogic.mk_binop @{const_name plus}) (dest_sum t)
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    15
 end *}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
text {* \solution{fun:makesum} *}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    19
ML{*fun make_sum t1 t2 =
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    20
    HOLogic.mk_nat (HOLogic.dest_nat t1 + HOLogic.dest_nat t2) *}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    22
text {* \solution{ex:scancmts} *}
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    23
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    24
ML{*val any = Scan.one (Symbol.not_eof);
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    25
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    26
val scan_cmt =
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    27
  let
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    28
    val begin_cmt = Scan.this_string "(*" 
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    29
    val end_cmt = Scan.this_string "*)"
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    30
  in
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    31
   begin_cmt |-- Scan.repeat (Scan.unless end_cmt any) --| end_cmt 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    32
    >> (enclose "(**" "**)" o implode)
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    33
  end
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    34
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    35
val scan_all =
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    36
  Scan.finite Symbol.stopper (Scan.repeat (scan_cmt || any)) 
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 58
diff changeset
    37
    >> implode #> fst *}
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    38
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    39
text {*
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    40
  By using @{text "#> fst"} in the last line, the function 
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    41
  @{ML scan_all} retruns a string, instead of the pair a parser would
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    42
  normally return. For example:
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    43
80
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    44
  @{ML_response [display,gray]
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    45
"let
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    46
   val input1 = (explode \"foo bar\")
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    47
   val input2 = (explode \"foo (*test*) bar (*test*)\")
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    48
 in
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    49
   (scan_all input1, scan_all input2)
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    50
 end"
Christian Urban <urbanc@in.tum.de>
parents: 72
diff changeset
    51
"(\"foo bar\",\"foo (**test**) bar (**test**)\")"}
56
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    52
*}
126646f2aa88 added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
    53
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
end