thys/#MyTree.thy#
author fahadausaf <fahad.ausaf@icloud.com>
Fri, 10 Oct 2014 18:45:10 +0100
changeset 28 d3831bf423f2
permissions -rw-r--r--
trees
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
theory MyTree
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
imports Main
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
begin
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
datatype 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
  'a tree = Tip | 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
  Node " 'a tree" 'a " 'a tree"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
fun mirror :: " 'a tree \<Rightarrow> 'a tree" where
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
  "mirror Tip = Tip" |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
  "mirror (Node l a r) = Node (mirror r) a (mirror l)"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
lemma "mirror(mirror t) = t"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
  apply(induction t)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
  apply(auto)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
  done
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
datatype 'a option = None | Some 'a
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
fun lookup :: "('a * 'b) list \<Rightarrow> 'a \<Rightarrow> 'b option" where
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
  "lookup [] x = None" |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
  "lookup ((a,b) # ps) x = (if a = x then Some b else lookup ps x)"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
definition sq :: "nat \<Rightarrow> nat" where
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
  "sq n = n * n"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
 
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
abbreviation sq' :: "nat \<Rightarrow> nat" where
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  "sq' n \<equiv> n * n"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
fun div2 :: "nat \<Rightarrow> nat" where
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
  "div2 0 = 0" |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
  "div2 (Suc 0) = 0" |
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
  "div2 (Suc(Suc n)) = Suc(div2 n)"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
lemma "div2(n) = n div 2"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
apply(induction n rule: div2.induct)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
apply(auto)
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
done
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
value "div2 8"
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42