{"id":6854,"date":"2014-11-11T12:22:19","date_gmt":"2014-11-11T12:22:19","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=6854"},"modified":"2014-11-11T12:22:19","modified_gmt":"2014-11-11T12:22:19","slug":"dafny-permutations-sequences-and-multisets","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2014\/11\/11\/dafny-permutations-sequences-and-multisets\/","title":{"rendered":"Dafny: Permutations, sequences and multisets"},"content":{"rendered":"<p>Dafny has support for multisets, which are really useful for reasoning about programs that permute, or otherwise <a href=\"http:\/\/rise4fun.com\/Dafny\/L8w2O\" title=\"Dafny Subpermutation\">deal with permutations<\/a>, of sequences or arrays. <\/p>\n<pre lang=\"dafny\">\r\npredicate Subpermutation(xs:seq, ys:seq)\r\n   ensures Subpermutation(xs,ys) ==> forall x :: x in xs ==> x in ys;\r\n{\r\n  assert forall x :: x in xs ==> x in multiset(xs);\r\n  multiset(xs) <= multiset(ys)\r\n}\r\n\r\n\/\/ THEOREM\r\nlemma SubpermutationIsSmaller(xs:seq, ys:seq)\r\n  requires Subpermutation(xs,ys);\r\n  ensures |xs| <= |ys|;\r\n{\r\n  assert |multiset(xs)| == |xs|;\r\n  assert |multiset(ys)| == |ys|;\r\n  \r\n  var xs',ys' := xs,ys;\r\n  \r\n  var XS',YS' := multiset(xs),multiset(ys);\r\n  var XS'',YS'' := multiset{},multiset{};\r\n  while(|XS'|>0)\r\n     invariant Subpermutation(xs',ys');\r\n     invariant XS' == multiset(xs');\r\n     invariant YS' == multiset(ys');\r\n     invariant XS' + XS'' == multiset(xs);\r\n     invariant YS' + YS'' == multiset(ys);\r\n     invariant XS'' == YS'';\r\n     invariant XS' <= YS';\r\n  {   \r\n    assert RemoveFromSequenceReducesMultiSet(xs,multiset(xs),multiset(xs[1..]));\r\n    var x := xs'[0];\r\n    xs' := Remove(x,xs');\r\n   \r\n    ys' := Remove(x,ys');\r\n    \r\n    XS' := XS'[x := XS'[x] - 1];\r\n    XS'' := XS''[x := XS''[x] + 1];\r\n    YS' := YS'[x := YS'[x] - 1];\r\n    YS'' := YS''[x := YS''[x] + 1];    \r\n  }\r\n}\r\n\r\n\/\/ SUPPORTING DEFINITIONS\r\n\r\n\/\/ following is a function lemma\r\npredicate RemoveFromSequenceReducesMultiSet(xs:seq<T>, XS:multiset<T>, XS':multiset<T>)\r\n   requires xs != [];\r\n   requires XS' == multiset(xs[1..]);\r\n   ensures XS' == multiset(xs)[xs[0] := multiset(xs)[xs[0]] - 1];\r\n   ensures RemoveFromSequenceReducesMultiSet(xs,XS,XS');\r\n{\r\n  assert [xs[0]]+xs[1..] == xs;\r\n  assert multiset([xs[0]]+xs[1..]) == multiset(xs);  \r\n  true\r\n}\r\n\r\nfunction Remove(x:T, xs:seq<T>) :seq<T>\r\n   requires x in xs;\r\n   ensures multiset(Remove(x,xs)) == multiset(xs)[x := multiset(xs)[x] - 1];\r\n   ensures |Remove(x,xs)|+1 == |xs|;\r\n{\r\n     assert RemoveFromSequenceReducesMultiSet(xs,multiset(xs),multiset(xs[1..]));\r\n     if xs[0]==x \r\n     then xs[1..] \r\n     else [xs[0]] + Remove(x,xs[1..])\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dafny has support for multisets, which are really useful for reasoning about programs that permute, or otherwise deal with permutations, of sequences or arrays. predicate Subpermutation(xs:seq, ys:seq) ensures Subpermutation(xs,ys) ==> forall x :: x in xs ==> x in ys; &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2014\/11\/11\/dafny-permutations-sequences-and-multisets\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[],"class_list":["post-6854","post","type-post","status-publish","format-standard","hentry","category-dafny"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-1My","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/6854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/comments?post=6854"}],"version-history":[{"count":1,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions"}],"predecessor-version":[{"id":6856,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/6854\/revisions\/6856"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=6854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=6854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=6854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}