Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Isabelle/Archive-of-Formal-Proofs/thys/Simpl/ex/   (Sammlung formaler Beweise Version 2026-5©)  Datei vom 29.4.2026 mit Grâße 8 kB image not shown  

Quelle  Quicksort.thy

  Sprache: Isabelle
 

(*
    Author:      Norbert Schirmer
    Maintainer:  Norbert Schirmer, norbert.schirmer at web de

Copyright (C) 2006-2008 Norbert Schirmer
*)


section "Example: Quicksort on Heap Lists"

theory Quicksort
imports "../Vcg" "../HeapList" "HOL-Library.Multiset"
begin

record globals_heap =
  next_' :: "ref ==> ref"
  cont_' :: "ref ==> nat"

record 'g vars = "'g state" +
  p_'    :: "ref"
  q_'    :: "ref"
  le_'   :: "ref"
  gt_'   :: "ref"
  hd_'   :: "ref"
  tl_'   :: "ref"

procedures
  append(p,q|p) =
    "IF πŸ‹p=Null THEN πŸ‹p :== πŸ‹q ELSE πŸ‹pπŸ‹next :== CALL append(πŸ‹pπŸ‹next,πŸ‹q) FI"

  append_spec:
   "βˆ€σ Ps Qs.
     Γ⊨ {σ. List πŸ‹p πŸ‹next Ps ∧ List πŸ‹q πŸ‹next Qs ∧ set Ps ∩ set Qs = {}}
           πŸ‹p :== PROC append(πŸ‹p,πŸ‹q)
         {List πŸ‹p πŸ‹next (Ps@Qs) ∧ (βˆ€x. xβˆ‰set Ps ⟢ πŸ‹next x = <sigma>next x)}"

  append_modifies:
   "βˆ€σ. Γ⊨ {σ} πŸ‹p :== PROC append(πŸ‹p,πŸ‹q){t. t may_only_modify_globals σ in [next]}"


lemma (in append_impl) append_modifies:
  shows
   "βˆ€σ. Γ⊨ {σ} πŸ‹p :== PROC append(πŸ‹p,πŸ‹q){t. t may_only_modify_globals σ in [next]}"
  apply (hoare_rule HoarePartial.ProcRec1)
  apply (vcg spec=modifies)
  done


lemma (in append_impl) append_spec:
  shows "βˆ€σ Ps Qs. Γ⊨
            {σ. List πŸ‹p πŸ‹next Ps ∧ List πŸ‹q πŸ‹next Qs ∧ set Ps ∩ set Qs = {}}
                πŸ‹p :== PROC append(πŸ‹p,πŸ‹q)
            {List πŸ‹p πŸ‹next (Ps@Qs) ∧ (βˆ€x. xβˆ‰set Ps ⟢ πŸ‹next x = <sigma>next x)}"
  apply (hoare_rule HoarePartial.ProcRec1)
  apply vcg
  apply fastforce
  done

primrec sorted:: "('a ==> 'a ==> bool) ==> 'a list ==> bool"
where
"sorted le [] = True" |
"sorted le (x#xs) = ((βˆ€y∈set xs. le x y) ∧ sorted le xs)"

lemma sorted_append[simp]:
 "sorted le (xs@ys) = (sorted le xs ∧ sorted le ys ∧
                       (βˆ€x ∈ set xs. βˆ€y ∈ set ys. le x y))"
by (induct xs, auto)

procedures quickSort(p|p) =
 "IF πŸ‹p=Null THEN SKIP
  ELSE πŸ‹tl :== πŸ‹pπŸ‹next;;
       πŸ‹le :== Null;;
       πŸ‹gt :== Null;;
       WHILE πŸ‹tlβ‰ Null DO
         πŸ‹hd :== πŸ‹tl;;
         πŸ‹tl :== πŸ‹tlπŸ‹next;;
         IF πŸ‹hdπŸ‹cont ≀ πŸ‹pπŸ‹cont
         THEN πŸ‹hdπŸ‹next :== πŸ‹le;;
              πŸ‹le :== πŸ‹hd
         ELSE πŸ‹hdπŸ‹next :== πŸ‹gt;;
              πŸ‹gt :== πŸ‹hd
         FI
       OD;;
       πŸ‹le :== CALL quickSort(πŸ‹le);;
       πŸ‹gt :== CALL quickSort(πŸ‹gt);;
       πŸ‹pπŸ‹next :== πŸ‹gt;;
       πŸ‹le :== CALL append(πŸ‹le,πŸ‹p);;
       πŸ‹p :== πŸ‹le
  FI"

  quickSort_spec:
  "βˆ€σ Ps. Γ⊨ {σ. List πŸ‹p πŸ‹next Ps} πŸ‹p :== PROC quickSort(πŸ‹p)
       {(βˆƒsortedPs. List πŸ‹p πŸ‹next sortedPs ∧
        sorted (≀) (map <sigma>cont sortedPs) ∧
        mset Ps = mset sortedPs) ∧
        (βˆ€x. xβˆ‰set Ps ⟢ πŸ‹next x = <sigma>next x)}"

  quickSort_modifies:
  "βˆ€σ. Γ⊨ {σ} πŸ‹p :== PROC quickSort(πŸ‹p) {t. t may_only_modify_globals σ in [next]}"


lemma (in quickSort_impl) quickSort_modifies:
  shows
  "βˆ€σ. Γ⊨ {σ} πŸ‹p :== PROC quickSort(πŸ‹p) {t. t may_only_modify_globals σ in [next]}"
apply (hoare_rule HoarePartial.ProcRec1)
apply (vcg spec=modifies)
done

lemma (in quickSort_impl) quickSort_spec:
shows
  "βˆ€σ Ps. Γ⊨ {σ. List πŸ‹p πŸ‹next Ps}
                  πŸ‹p :== PROC quickSort(πŸ‹p)
                {(βˆƒsortedPs. List πŸ‹p πŸ‹next sortedPs ∧
                 sorted (≀) (map <sigma>cont sortedPs) ∧
                 mset Ps = mset sortedPs) ∧
                 (βˆ€x. xβˆ‰set Ps ⟢ πŸ‹next x = <sigma>next x)}"
apply (hoare_rule HoarePartial.ProcRec1)
apply (hoare_rule anno =
 "IF πŸ‹p=Null THEN SKIP
  ELSE πŸ‹tl :== πŸ‹pπŸ‹next;;
       πŸ‹le :== Null;;
       πŸ‹gt :== Null;;
       WHILE πŸ‹tlβ‰ Null
       INV { (βˆƒles grs tls. List πŸ‹le πŸ‹next les ∧ List πŸ‹gt πŸ‹next grs ∧
               List πŸ‹tl πŸ‹next tls ∧
               mset Ps = mset (πŸ‹p#tls@les@grs) ∧
               distinct(πŸ‹p#tls@les@grs) ∧
               (βˆ€x∈set les. xπŸ‹cont ≀ πŸ‹pπŸ‹cont) ∧
               (βˆ€x∈set grs. πŸ‹pπŸ‹cont < xπŸ‹cont)) ∧
               πŸ‹p=<sigma>p ∧
               πŸ‹cont=<sigma>cont ∧
               List <sigma>p <sigma>next Ps ∧
               (βˆ€x. xβˆ‰set Ps ⟢ πŸ‹next x = <sigma>next x)}
       DO
         πŸ‹hd :== πŸ‹tl;;
         πŸ‹tl :== πŸ‹tlπŸ‹next;;
         IF πŸ‹hdπŸ‹cont ≀ πŸ‹pπŸ‹cont
         THEN πŸ‹hdπŸ‹next :== πŸ‹le;;
              πŸ‹le :== πŸ‹hd
         ELSE πŸ‹hdπŸ‹next :== πŸ‹gt;;
              πŸ‹gt :== πŸ‹hd
         FI
       OD;;
       πŸ‹le :== CALL quickSort(πŸ‹le);;
       πŸ‹gt :== CALL quickSort(πŸ‹gt);;
       πŸ‹pπŸ‹next :== πŸ‹gt;;
       πŸ‹le :== CALL append(πŸ‹le,πŸ‹p);;
       πŸ‹p :== πŸ‹le
  FI" in HoarePartial.annotateI)
  apply vcg
    apply fastforce
   apply clarsimp
   apply (rule conjI)
    apply clarify
    apply (rule conjI)
     apply (rule_tac x="tl#les" in exI)
     apply simp
     apply (rule_tac x="grs" in exI)
     apply simp
     apply (rule_tac x="ps" in exI)
     apply simp
    apply (metis insertCI set_mset_add_mset_insert set_mset_mset)
   apply clarify
   apply (rule conjI)
    apply (rule_tac x="les" in exI)
    apply simp
    apply (rule_tac x="tl#grs" in exI)
    apply simp
    apply (rule_tac x="ps" in exI)
    apply simp
   apply (metis insertCI set_mset_add_mset_insert set_mset_mset)
  apply clarsimp
  apply (rule_tac ?x=grs in exI)
  apply (rule conjI)
  apply (erule heap_eq_ListI1)
   apply clarify
   apply (erule_tac x=x in allE) back
   apply blast
  apply clarsimp
  apply (rule_tac x="sortedPs" in exI)
  apply (rule conjI)
   apply (erule heap_eq_ListI1)
   apply (clarsimp)
   apply (erule_tac x=x in allE) back back
   apply (metis IntI empty_iff set_mset_mset)
  apply (rule_tac x="p#sortedPsa" in exI)
  apply (rule conjI)
   apply (metis List_cons List_updateI Null_notin_List fun_upd_same insert_iff set_mset_add_mset_insert set_mset_mset)
  apply (rule conjI)
   apply (metis disjoint_iff mset_eq_setD set_ConsD)
  apply clarsimp
  apply (rule conjI)
   apply (metis less_or_eq_imp_le mset_eq_setD)
  apply (rule conjI)
   apply (metis leD less_le_trans mset_eq_setD nat_le_linear)
  apply clarsimp
  apply (erule_tac x=x in allE)+
  apply (metis Un_iff insert_iff list.set(2) mset.simps(2) mset_append set_append set_mset_mset)
  done

end

Messung V0.5 in Prozent
C=100 H=-187 G=149

Β€ Dauer der Verarbeitung: 0.8 Sekunden  Β€

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfΓ€ltig zusammengestellt. Es wird jedoch weder VollstΓ€ndigkeit, noch Richtigkeit, noch QualitΓ€t der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.