--- /dev/null
+definition module StdSortList\r
+\r
+import StdClass\r
+\r
+:: SortList a\r
+\r
+newSortList :: SortList a // lege gesorteerde lijst\r
+memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van\r
+insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe\r
+removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen\r
+removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens\r
+elements :: (SortList a) -> [a] // geef alle elementen\r
+count :: (SortList a) -> Int // aantal elementen\r
+\r
+minimum :: (SortList a) -> a // huidige minimum waarde\r
+maximum :: (SortList a) -> a // huidige maximum waarde\r
+\r
+mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten\r
--- /dev/null
+implementation module StdSortList\r
+\r
+import StdEnv\r
+\r
+:: SortList a :== [a]\r
+\r
+newSortList :: (SortList a)\r
+newSortList = []\r
+\r
+memberSort :: a (SortList a) -> Bool | Eq, Ord a // is element van\r
+memberSort _ [] = False\r
+memberSort m l\r
+ | hd l == m = True\r
+ | hd l > m = False\r
+ | otherwise = memberSort m (tl l)\r
+\r
+insertSort :: a (SortList a) -> SortList a | Ord a // voeg element toe\r
+insertSort m l = insertSort` newSortList m l \r
+where \r
+ insertSort` :: (SortList a) a (SortList a) -> (SortList a) | Ord a\r
+ insertSort` l1 m l2\r
+ | count l2 == 0 = l1 ++ [m]\r
+ | minimum l2 >= m = l1 ++ [m] ++ l2\r
+ | otherwise = insertSort` (l1 ++ [hd l2]) m (tl l2)\r
+\r
+removeFirst :: a (SortList a) -> SortList a | Eq, Ord a // verwijder eerste voorkomen\r
+removeFirst m l = removeFirst` newSortList m l\r
+where\r
+ removeFirst` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a\r
+ removeFirst` l1 m l2\r
+ | count l2 == 0 = l1\r
+ | minimum l2 > m = l1 ++ l2\r
+ | minimum l2 == m = l1 ++ tl l2\r
+ | otherwise = removeFirst` (l1 ++ [hd l2]) m (tl l2)\r
+\r
+removeAll :: a (SortList a) -> SortList a | Eq, Ord a // verwijder alle voorkomens\r
+removeAll m l = removeAll` newSortList m l\r
+where\r
+ removeAll` :: (SortList a) a (SortList a) -> (SortList a) | Eq, Ord a\r
+ removeAll` l1 m l2\r
+ | count l2 == 0 = l1\r
+ | minimum l2 > m = l1 ++ l2\r
+ | minimum l2 == m = removeAll` l1 m (tl l2)\r
+ | otherwise = removeAll` (l1 ++ [hd l2]) m (tl l2)\r
+\r
+elements :: (SortList a) -> [a] // geef alle elementen\r
+elements l = l\r
+\r
+count :: (SortList a) -> Int // aantal elementen\r
+count l = length l\r
+\r
+minimum :: (SortList a) -> a // huidige minimum waarde\r
+minimum l = hd l\r
+\r
+maximum :: (SortList a) -> a // huidige maximum waarde\r
+maximum l = last l\r
+\r
+mergeSortList :: (SortList a) (SortList a) -> SortList a | Eq, Ord a // meng gesorteerde lijsten\r
+mergeSortList l1 [] = l1\r
+mergeSortList l1 l2 = mergeSortList (insertSort (hd l2) l1) (tl l2)\r
--- /dev/null
+module StdSortListTest\r
+\r
+/* Test module StdSortList\r
+ Voor werken met Gast: \r
+ (*) gebruik Environment 'Gast'\r
+ (*) zet Project Options op 'Basic Values Only' en '16M' Maximum Heap Size.\r
+*/\r
+\r
+import gast\r
+import GenLexOrd\r
+import StdSortList\r
+\r
+Start = testn 10000 \r
+ (\n` n2` m -> let n = lst2slst (cast [A,B,C] n` )\r
+ n2 = lst2slst (cast [A,B,C] n2`) \r
+ in \r
+ leeg_is_leeg /\\r
+ count_matches_elems n /\\r
+ is_sorted_elems n /\\r
+ member_is_member n m /\\r
+ member_na_insert n m /\\r
+ member_na_remove n m /\\r
+ insert_remove_invariant n m /\\r
+ minimum_property n /\\r
+ maximum_property n /\\r
+ merge_additive n n2 /\\r
+ merge_member n n2 m /\\r
+ True\r
+ )\r
+\r
+:: Enum = A | B | C \r
+\r
+derive bimap []\r
+derive ggen Enum\r
+derive genShow Enum\r
+derive gEq Enum\r
+derive gLexOrd Enum\r
+instance == Enum where (==) x y = gEq{|*|} x y\r
+instance < Enum where (<) x y = gEq{|*|} (gLexOrd{|*|} x y) LT\r
+\r
+// clean should have something like this!\r
+cast :: a a -> a\r
+cast _ x = x\r
+\r
+leeg_is_leeg :: Property\r
+leeg_is_leeg \r
+ = name "leeg_is_leeg"\r
+ (count newSortList == 0)\r
+\r
+count_matches_elems :: (SortList a) -> Property | Eq, Ord a\r
+count_matches_elems n \r
+ = name "count_matches_elems" \r
+ (length (elements n) == count n)\r
+\r
+is_sorted_elems :: (SortList a) -> Property | Eq, Ord a\r
+is_sorted_elems n\r
+ = name "is_sorted_elems"\r
+ (isSorted (elements n))\r
+ where isSorted lst = and [ x<=y \\ x<-lst & y<-tl lst ]\r
+\r
+member_is_member :: (SortList a) a -> Property | Eq, Ord a\r
+member_is_member lst e\r
+ = name "member_is_member"\r
+ ((isMember e (elements lst)) <==> (memberSort e lst))\r
+\r
+member_na_insert :: (SortList a) a -> Property | Eq, Ord a\r
+member_na_insert lst e\r
+ = name "member_na_insert"\r
+ (memberSort e (insertSort e lst))\r
+\r
+member_na_remove :: (SortList a) a -> Property | Eq, Ord a\r
+member_na_remove lst e\r
+ = name "member_na_remove"\r
+ (not (memberSort e (removeAll e lst)))\r
+\r
+insert_remove_invariant :: (SortList a) a -> Property | Eq, Ord a\r
+insert_remove_invariant lst e\r
+ = name "insert_remove_invariant"\r
+ (memberSort e lst <==> memberSort e lst`)\r
+ where lst` = removeFirst e (insertSort e lst)\r
+\r
+minimum_property :: (SortList a) -> Property | Eq,Ord a\r
+minimum_property n\r
+ = name "minimum_property"\r
+ (count n > 0 ==> (memberSort min n /\ all ((<=) min) (elements n)))\r
+ where min = minimum n\r
+\r
+maximum_property :: (SortList a) -> Property | Eq,Ord a\r
+maximum_property n\r
+ = name "maximum_property"\r
+ (count n > 0 ==> (memberSort max n /\ all ((>=) max) (elements n)))\r
+ where max = maximum n\r
+\r
+merge_member :: (SortList a) (SortList a) a -> Property | Eq,Ord a\r
+merge_member n m e\r
+ = name "merge_member"\r
+ (memberSort e nm <==> (memberSort e n \/ memberSort e m))\r
+ where nm = mergeSortList n m\r
+\r
+merge_additive :: (SortList a) (SortList a) -> Property | Eq,Ord a\r
+merge_additive n m \r
+ = name "merge_additive"\r
+ (count n + count m == count nm)\r
+ where nm = mergeSortList n m\r
+\r
+lst2slst :: [a] -> SortList a | Eq,Ord a\r
+lst2slst xs = seq (map insertSort xs) newSortList\r