thys2/Sort_ops.thy~
author Xingyuan Zhang <xingyuanzhang@126.com>
Sat, 13 Sep 2014 10:07:14 +0800
changeset 25 a5f5b9336007
permissions -rw-r--r--
thys2 added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     1
theory Sort_ops
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     2
imports Main
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     3
begin
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     4
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     5
ML {*
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     6
local 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     7
  open Array
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     8
in
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
     9
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    10
 fun swap i j a = let val ai = sub(a, i);
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    11
                       val _ = update(a, i, sub(a, j))
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    12
                       val _ = update(a, j, ai)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    13
                   in
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    14
                        a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    15
                   end
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    16
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    17
  fun pre_ops_of a = 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    18
   getM :|-- (fn (l, r, piv, i, j) => let
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    19
       (* val _ = Output.tracing ("("^string_of_int i^","^string_of_int j^")") *)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    20
    in
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    21
    if (j < i) then (returnM (swap l j a) |-- returnM [(l, j)])
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    22
    else (if (sub(a, i) <= piv andalso i <= r) then (setM (l, r, piv, i + 1, j)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    23
                                                        |-- pre_ops_of a)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    24
          else if (sub(a, j) > piv ) then (setM (l, r, piv, i, j - 1) |-- pre_ops_of a)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    25
          else (pre_ops_of (swap i j a) :|-- (fn ops => returnM ((i,j)::ops))))
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    26
    end  
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    27
   )
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    28
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    29
  fun ops_of i j a = 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    30
     if (j < i) then []
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    31
  else let
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    32
     val piv = sub(a, i)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    33
     val (ops1, (_, _, _, i', j')) = pre_ops_of a (i, j, piv, i, j) |> normVal
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    34
     val ops2 = ops_of i (j' - 1) a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    35
     val ops3 = ops_of (j' + 1) j a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    36
  in
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    37
     ops1 @ ops2 @ ops3 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    38
  end
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    39
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    40
  fun rem_sdup [] = []
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    41
    | rem_sdup [c] = [c]
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    42
    | rem_sdup ((i, j)::(i', j')::xs) = if ((i = i' andalso j = j') orelse
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    43
                                            (i = j' andalso j = i')) then rem_sdup (xs)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    44
                            else (i, j)::rem_sdup ((i', j')::xs)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    45
  fun sexec [] a = a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    46
   | sexec ((i, j)::ops) a = sexec ops (swap i j a)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    47
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    48
  fun swaps_of (l:int list) = 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    49
    ops_of 0 (List.length l - 1) (fromList l) |> rem_sdup
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    50
      |> filter (fn (i, j) => i <> j)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    51
end
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    52
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    53
*}
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    54
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    55
text {* Examples *}
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    56
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    57
ML {*
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    58
  val l = [8, 9, 10, 1, 12, 13, 14] 
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    59
  val ops = (swaps_of l)
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    60
  val a = (sexec ops (Array.fromList l))
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    61
  val l' = Array.vector a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    62
  val a = sexec (rev ops) a
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    63
*}
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    64
a5f5b9336007 thys2 added
Xingyuan Zhang <xingyuanzhang@126.com>
parents:
diff changeset
    65
end