{"id":8452,"date":"2015-01-21T11:02:47","date_gmt":"2015-01-21T11:02:47","guid":{"rendered":"http:\/\/www.lexicalscope.com\/blog\/?p=8452"},"modified":"2015-01-21T11:14:31","modified_gmt":"2015-01-21T11:14:31","slug":"distributivity-of-sequence-map-over-function-composition-in-dafny","status":"publish","type":"post","link":"https:\/\/www.lexicalscope.com\/blog\/2015\/01\/21\/distributivity-of-sequence-map-over-function-composition-in-dafny\/","title":{"rendered":"Distributivity of Sequence Map over Function Composition in Dafny"},"content":{"rendered":"<p><a href=\"http:\/\/rise4fun.com\/Dafny\/zplUg\">Distributivity of Sequence Map over Function Composition in Dafny<\/a><\/p>\n<pre lang=\"dafny\">\r\nlemma MapDistributivity(xs:seq<int>, f:int->int, g:int->int)\r\n   requires forall x :: x in xs ==> f.requires(x);\r\n   requires forall x :: x in xs ==> g.requires(f(x));\r\n   ensures forall x :: x in MapSeq(xs,f) ==> g.requires(x);\r\n   ensures MapSeq(MapSeq(xs,f), g) == MapSeq(xs, Compose(f,g));\r\n{\r\n   if xs != []\r\n   {\r\n     MapDistributivity(xs[1..], f, g);\r\n   }\r\n}\r\n\r\nfunction Compose(f:int->int, g:int->int) : int->int\r\n{\r\n  x\r\n    reads f.reads(x)\r\n    reads if f.requires(x) then g.reads(f(x)) else {} \r\n    requires f.requires(x) \r\n    requires g.requires(f(x)) \r\n      -> g(f(x))\r\n}\r\n\r\nfunction MapSeq(xs:seq<int>, f:int->int) : seq<int>\r\n   reads MapSeqReads(xs, f);\r\n   requires forall x :: x in xs ==> f.requires(x);\r\n   ensures |xs| == |MapSeq(xs,f)|;\r\n   ensures forall x :: x in xs ==> f(x) in MapSeq(xs,f); \r\n{\r\n  if xs == [] then []\r\n  else [f(xs[0])] + MapSeq(xs[1..], f) \r\n}\r\n\r\nfunction MapSeqReads(xs:seq<int>, f:int->int) : set<object>\r\n   reads if |xs| > 0 then f.reads(xs[0]) + MapSeqReads(xs[1..], f) else {};\r\n   decreases xs;\r\n   ensures forall x :: x in xs ==> f.reads(x) <= MapSeqReads(xs,f); \r\n{\r\n  if xs == [] then {}\r\n  else f.reads(xs[0]) + MapSeqReads(xs[1..],f)\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Distributivity of Sequence Map over Function Composition in Dafny lemma MapDistributivity(xs:seq, f:int->int, g:int->int) requires forall x :: x in xs ==> f.requires(x); requires forall x :: x in xs ==> g.requires(f(x)); ensures forall x :: x in MapSeq(xs,f) ==> g.requires(x); &hellip; <a href=\"https:\/\/www.lexicalscope.com\/blog\/2015\/01\/21\/distributivity-of-sequence-map-over-function-composition-in-dafny\/\">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_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-8452","post","type-post","status-publish","format-standard","hentry","category-dafny"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2e3P7-2ck","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/8452","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=8452"}],"version-history":[{"count":1,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/8452\/revisions"}],"predecessor-version":[{"id":8453,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/posts\/8452\/revisions\/8453"}],"wp:attachment":[{"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/media?parent=8452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/categories?post=8452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lexicalscope.com\/blog\/wp-json\/wp\/v2\/tags?post=8452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}