src/HOL/Predicate_Compile_Examples/Hotel_Example.thy
author bulwahn
Wed Aug 25 16:59:53 2010 +0200 (2010-08-25)
changeset 38734 e5508a74b11f
parent 38733 4b8fd91ea59a
child 38949 1afa9e89c885
permissions -rw-r--r--
changing hotel trace definition; adding simple handling of numerals on natural numbers
bulwahn@38730
     1
theory Hotel_Example
bulwahn@38730
     2
imports Predicate_Compile_Alternative_Defs Code_Prolog
bulwahn@38730
     3
begin
bulwahn@38730
     4
bulwahn@38730
     5
datatype guest = Guest0 | Guest1
bulwahn@38730
     6
datatype key = Key0 | Key1 | Key2 | Key3
bulwahn@38730
     7
datatype room = Room0
bulwahn@38730
     8
bulwahn@38730
     9
types card = "key * key"
bulwahn@38730
    10
bulwahn@38730
    11
datatype event =
bulwahn@38730
    12
   Check_in guest room card | Enter guest room card | Exit guest room
bulwahn@38730
    13
bulwahn@38730
    14
definition initk :: "room \<Rightarrow> key"
bulwahn@38730
    15
  where "initk = (%r. Key0)"
bulwahn@38730
    16
bulwahn@38730
    17
declare initk_def[code_pred_def, code]
bulwahn@38730
    18
bulwahn@38730
    19
primrec owns :: "event list \<Rightarrow> room \<Rightarrow> guest option"
bulwahn@38730
    20
where
bulwahn@38730
    21
  "owns [] r = None"
bulwahn@38730
    22
| "owns (e#s) r = (case e of
bulwahn@38730
    23
    Check_in g r' c \<Rightarrow> if r' = r then Some g else owns s r |
bulwahn@38730
    24
    Enter g r' c \<Rightarrow> owns s r |
bulwahn@38730
    25
    Exit g r' \<Rightarrow> owns s r)"
bulwahn@38730
    26
bulwahn@38730
    27
primrec currk :: "event list \<Rightarrow> room \<Rightarrow> key"
bulwahn@38730
    28
where
bulwahn@38730
    29
  "currk [] r = initk r"
bulwahn@38730
    30
| "currk (e#s) r = (let k = currk s r in
bulwahn@38730
    31
    case e of Check_in g r' (k1, k2) \<Rightarrow> if r' = r then k2 else k
bulwahn@38730
    32
            | Enter g r' c \<Rightarrow> k
bulwahn@38730
    33
            | Exit g r \<Rightarrow> k)"
bulwahn@38730
    34
bulwahn@38730
    35
primrec issued :: "event list \<Rightarrow> key set"
bulwahn@38730
    36
where
bulwahn@38730
    37
  "issued [] = range initk"
bulwahn@38730
    38
| "issued (e#s) = issued s \<union>
bulwahn@38730
    39
  (case e of Check_in g r (k1, k2) \<Rightarrow> {k2} | Enter g r c \<Rightarrow> {} | Exit g r \<Rightarrow> {})"
bulwahn@38730
    40
bulwahn@38730
    41
primrec cards :: "event list \<Rightarrow> guest \<Rightarrow> card set"
bulwahn@38730
    42
where
bulwahn@38730
    43
  "cards [] g = {}"
bulwahn@38730
    44
| "cards (e#s) g = (let C = cards s g in
bulwahn@38730
    45
                    case e of Check_in g' r c \<Rightarrow> if g' = g then insert c C
bulwahn@38730
    46
                                                else C
bulwahn@38730
    47
                            | Enter g r c \<Rightarrow> C
bulwahn@38730
    48
                            | Exit g r \<Rightarrow> C)"
bulwahn@38730
    49
bulwahn@38730
    50
primrec roomk :: "event list \<Rightarrow> room \<Rightarrow> key"
bulwahn@38730
    51
where
bulwahn@38730
    52
  "roomk [] r = initk r"
bulwahn@38730
    53
| "roomk (e#s) r = (let k = roomk s r in
bulwahn@38730
    54
    case e of Check_in g r' c \<Rightarrow> k
bulwahn@38730
    55
            | Enter g r' (x,y) \<Rightarrow> if r' = r (*& x = k*) then y else k
bulwahn@38730
    56
            | Exit g r \<Rightarrow> k)"
bulwahn@38730
    57
bulwahn@38730
    58
primrec isin :: "event list \<Rightarrow> room \<Rightarrow> guest set"
bulwahn@38730
    59
where
bulwahn@38730
    60
  "isin [] r = {}"
bulwahn@38730
    61
| "isin (e#s) r = (let G = isin s r in
bulwahn@38730
    62
                 case e of Check_in g r c \<Rightarrow> G
bulwahn@38730
    63
                 | Enter g r' c \<Rightarrow> if r' = r then {g} \<union> G else G
bulwahn@38730
    64
                 | Exit g r' \<Rightarrow> if r'=r then G - {g} else G)"
bulwahn@38730
    65
bulwahn@38730
    66
primrec hotel :: "event list \<Rightarrow> bool"
bulwahn@38730
    67
where
bulwahn@38730
    68
  "hotel []  = True"
bulwahn@38730
    69
| "hotel (e # s) = (hotel s & (case e of
bulwahn@38730
    70
  Check_in g r (k,k') \<Rightarrow> k = currk s r \<and> k' \<notin> issued s |
bulwahn@38730
    71
  Enter g r (k,k') \<Rightarrow> (k,k') : cards s g & (roomk s r : {k, k'}) |
bulwahn@38734
    72
  Exit g r \<Rightarrow> g : isin s r))"
bulwahn@38730
    73
bulwahn@38730
    74
lemma issued_nil: "issued [] = {Key0}"
bulwahn@38730
    75
by (auto simp add: initk_def)
bulwahn@38730
    76
bulwahn@38730
    77
lemmas issued_simps[code, code_pred_def] = issued_nil issued.simps(2)
bulwahn@38730
    78
bulwahn@38730
    79
declare Let_def[code_pred_inline]
bulwahn@38730
    80
bulwahn@38730
    81
lemma [code_pred_inline]: "insert == (%y A x. y = x | A x)"
bulwahn@38730
    82
by (auto simp add: insert_iff[unfolded mem_def] expand_fun_eq intro!: eq_reflection)
bulwahn@38730
    83
bulwahn@38730
    84
lemma [code_pred_inline]: "(op -) == (%A B x. A x \<and> \<not> B x)"
bulwahn@38730
    85
by (auto simp add: Diff_iff[unfolded mem_def] expand_fun_eq intro!: eq_reflection)
bulwahn@38730
    86
bulwahn@38730
    87
ML {* Code_Prolog.options := {ensure_groundness = true} *}
bulwahn@38730
    88
bulwahn@38734
    89
values 40 "{s. hotel s}"
bulwahn@38730
    90
bulwahn@38731
    91
bulwahn@38733
    92
setup {* Quickcheck.add_generator ("prolog", Code_Prolog.quickcheck) *}
bulwahn@38733
    93
ML {* set Code_Prolog.trace *}
bulwahn@38733
    94
bulwahn@38733
    95
lemma "\<lbrakk> hotel s; g \<in> isin s r \<rbrakk> \<Longrightarrow> owns s r = Some g"
bulwahn@38733
    96
quickcheck[generator = code, iterations = 100000, report]
bulwahn@38733
    97
quickcheck[generator = prolog, iterations = 1]
bulwahn@38733
    98
oops
bulwahn@38733
    99
bulwahn@38731
   100
bulwahn@38730
   101
end