Share
Embed
TPPMark2016 を解きながら学ぶ VeriFast
(静的コード解析の会 第0回)[https://metasepi.connpass.com/event/42141/] で発表した資料です。
TPP2016で出題された問題である TPPMark を題材として、C 言語検証器である VeriFast による回答を構成しつつ VeriFast 自体の入門的解説をします。
eldesh
November 19, 2016
Transcript
96 slides
-
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark2016 Λղ͖ͳ͕ΒֶͿ
VeriFast @eldesh https://twitter.com/eldesh http://d.hatena.ne.jp/eldesh ੩తίʔυղੳͷձ ୈ 0 ճ 2016/11/19 2016/11/19 VeriFast Introduction 1 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ࣍ 1
VeriFast ֓આ 2 VeriFast ೖ 3 جຊ෦ͷఆٛ 4 ओఆཧͷূ໌ 5 ࢀর 2016/11/19 VeriFast Introduction 2 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর What is
this slide? TPP20161 Ͱग़͞ΕͨͰ͋Δ TPPMark Λࡐͱͯ͠ɺC ݴޠݕূث Ͱ͋Δ VeriFast ʹΑΔճΛߏͭͭ͠ VeriFast ࣗମͷೖతղઆΛ͠ ·͢ɻ 112th Theorem Proving and Provers. ఆཧূ໌ͱূ໌ثʹؔ͢Δձٞ 2016/11/19 VeriFast Introduction 3 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ఆ/ରಡऀ ඞਢ
C ݴޠ͕͔Δ ·͍͠ ࣄલ݅/ࣄޙ݅ͱ͍͏ݴ༿Λฉ͍ͨ͜ͱ͕͋Δ ؔܕݴޠ (ML) ͷॳาతͳཧղ 2016/11/19 VeriFast Introduction 4 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ֓ཁ
1/2 ·ͣ VeriFast ͷಛΛڍ͓͛ͯ͘ɻ ཧʹج͍ͮͯ C ·ͨ Java ϓϩάϥϜΛݕূ͢Δݕূث ίϝϯτʹऍΛॻ͔ͤɺ߹ੑΛݕࠪ ࣄલ݅, ࣄޙ݅ ϧʔϓෆม݅ C ϥΠΫͳ७ਮؔܕݴޠ Ϟδϡϥʔͳ༷ͱݕࠪ ΑΓԼͷϨΠϠͷ໋ͷதʹґଘ͠ͳ͍ ໋ͷ߹ੑݕࠪʹ SMT ιϧό 2 Λ͏ ઐ༻ IDE ͕͋Δ ݕূʹࣦഊͨ͠߹ɺࣦഊͨ͠εςοϓ·ͰͷγϯϘϦοΫ࣮ߦͷϑ ϩʔ͕֬ೝͰ͖Δ tactic ແ͠ 2༻͢Διϧό Redux(ιϧό) ·ͨ z3 2016/11/19 VeriFast Introduction 5 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ֓ཁ
2/2 VeriFast ͰݕূͰ͖Δ༰ ܭࢉ݁Ռ͕༷Λຬ͢͜ͱ ϝϞϦ҆શੑ ϦʔΫ͠ͳ͍ ෆਖ਼ͳσϦϑΝϨϯεΛ͠ͳ͍ ԋࢉͷΦʔόʔ/Ξϯμʔϑϩʔ͕ແ͍͜ͱ (skip Մೳ) ฒྻϓϩάϥϜͷมΞΫηεͷҰ؏ੑอূ ϩοΫΛऔಘͤͣ (ϩοΫͰอޢͨ͠มͷ) ॻ͖ࠐΈߦΘΕͳ͍ ϩοΫෆม݅Λຬ͍ͨͯ͠ͳ͍ΞϯϩοΫى͖ͳ͍ ؔͷఀࢭੑ 2016/11/19 VeriFast Introduction 6 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark2016 TPPMark
ͷจΛݟ͓ͯ͜͏ɻ 2.1(ґଘܕϓϩάϥϛϯάతͳදݱ) Ҿͱͯ͠ҎԼͷ̏ͭ ϦετͷϦετ lst i j Λड͚औͬͨΒɺ lst ͷ i ൪ͷϦετͷ j ൪ͷཁૉΛ আͨ͠ͷΛฦ ؔ͢ remove-lst Λ࡞ͤΑɻͦͷࡍɺҎԼͷ̏ͭͷ݅ lst ͷ i ൪ͷϦετ͕ଘࡏ͢Δɻ i ൪ͷϦετͷ j ൪ͷཁૉ͕ଘࡏ͢Δɻ ฦ͞ΕΔϦετͨͪͷ͞ɺi ൪ͷΈ 1 ͘ͳ͓ͬͯΓɺଞมΘ Βͳ͍ɻ ͕อূ͞ΕΔΑ͏ʹͤΑɻ ʢ͜͜Ͱɺઌ಄͍ͣΕ 0 ൪ͱ͢Δɻ ʣ 2016/11/19 VeriFast Introduction 7 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark ͷܗࣜԽΛ࢝ΊΔલʹ.
. . VeriFast ͷ “࠷جຊతͳػೳ” Λղઆ͢Δɻ 2016/11/19 VeriFast Introduction 8 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ࣄલ݅ͱࣄޙ݅ VeriFast
ͷऍશͯ//@ʹΑΔҰߦίϝϯτ·ͨ/*@͔Β@*/·Ͱͷϒ ϩοΫίϝϯτʹهड़͢Δɻݕূ͍ͨؔ͠ʹ requires Ͱࣄલ݅Λɺ ensures Ͱࣄޙ݅Λهड़͠ɺVeriFast Ͱ࣮ͱͷҰ؏ੑΛݕূ͢Δ 3ɻ P=ࣄલ݅ Q=ࣄޙ݅ int F (int x) //@ requires P; //@ ensures Q; { // ໋ P ͕Γཱͭ ... // ໋ Q ͕Γཱͭ } 3ݕূ͍͔ͨؔ͠ΒݺΕ͍ͯΔؔʹऍ͕ඞཁ 2016/11/19 VeriFast Introduction 9 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ۭͷ݅ ·ͣԿ͠ͳ͍͔ؔΒ࢝ΊΑ͏ɻҎԼͷؔ
empty_cmd Կҙຯͷ͋ Δ͜ͱΛ͠ͳ͍ͨΊɺͦΕΛ໋Ͱද͍ͨ͠ɻͦͷͨΊʹ͍ͭͰΓ ໋ཱͭΛࢦఆ͢ΕΑ͍ɻ Կ͠ͳ͍ؔͷݕূ void empty_cmd (void) //@ requires emp; //@ ensures emp; { } emp ৗʹΓ໋ཱͭ 4ɻ 4emp ͷଞʹ true ಉ͡ҙຯ͕ͩɺϒʔϧͱฆΒΘ͍͠ͷͰ emp Λ࠾༻ͨ͠ɻ 2016/11/19 VeriFast Introduction 10 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর Γʹؔ͢Δ໋ Γʹؔ͢Δ໋Λॻ͘͜ͱ͕ग़དྷΔɻΓ
ensures અͷதͰ result ͱͯ͠ࢀরͰ͖Δ 5ɻ less than ͷ༷ bool compare (int x, int y) //@ requires emp; //@ ensures result == (x < y); { return x < y; // ฦΓେখൺֱͷ݁ՌʹҰக͢Δ } 5࣮ result requires ͷதͰࢀরͰ͖Δ 2016/11/19 VeriFast Introduction 11 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ੵ ෳͷ໋͕Γཱͭ͜ͱΛࣔͨ͢Ίʹɺ໋Λ&*&6
Ͱ۠ͬͯฒΕ Α͍ɻྫ͑͋Δ໋ P ͱ͋Δ໋ Q ͕ͲͪΒΓཱͭ߹ P &*& Q ͱදه͢Δɻ ಉ࣌ʹΓཱͭෳͷ໋ int plus_positive (int x, int y) //@ requires 0 < x &*& 0 < y; //@ ensures result == (x + y); { return x + y; } plus_positive Ҿ x ͱ y ͦΕͧΕ͕ਖ਼Ͱ͋Δ͜ͱΛ྆ํཁٻ͢Δ 7ɻ 6ੵ; separation conjunctionɻseparation logic ͷಋೖ͢ΔΦϖϨʔλɻ 7ݫີʹࣄલ݅ʹ (ྫ͑)(x + y) ≤ INT_MAX ͕Γͳ͍ɻCheck arithmetic overflow Λແޮʹͯ͠ݕࠪ͢Δ͜ͱɻ 2016/11/19 VeriFast Introduction 12 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর σϦϑΝϨϯε 1/2
ϙΠϯλͷࢦ͍ͯ͠Δʹݴٴ͢Δʹઐ༻ΦϖϨʔλ | → Λ͏ɻ| → ͷ ӈลʹແ໊ύλʔϯ (_) ·ͨมΛࢦఆग़དྷΔɻ ϓϦϛςΟϒܕͷϙΠϯλ void div (int x, int d, int * q, int * m) //@ requires d != 0 &*& *q |-> _ &*& *m |-> _; //@ ensures *q |-> x/d &*& *m |-> x%d; { *q = x/d; *m = x%d; } | → ਖ਼͍͠ϝϞϦΛࢦ͍ͯ͠Δͱ͍͏ओுΛؚΜͰ͍Δࣄʹҙɻ 2016/11/19 VeriFast Introduction 13 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর σϦϑΝϨϯε 1/2
ϙΠϯλͷࢦ͍ͯ͠Δʹݴٴ͢Δʹઐ༻ΦϖϨʔλ | → Λ͏ɻ| → ͷ ӈลʹແ໊ύλʔϯ (_) ·ͨมΛࢦఆग़དྷΔɻ struct ܕͷϙΠϯλ struct boxed { int x; }; void update (boxed * b, int v) //@ requires b->x |-> _; //@ ensures b->x |-> v; { b->x = v; } | → ਖ਼͍͠ϝϞϦΛࢦ͍ͯ͠Δͱ͍͏ओுΛؚΜͰ͍Δࣄʹҙɻ 2016/11/19 VeriFast Introduction 13 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর σϦϑΝϨϯε 2/2
| → ͷӈลͰ?Λࢦఆ͢ΔͱมΛಋೖ͢Δ͜ͱ͕ग़དྷΔ 8ɻ มͷಋೖ struct S { int v; }; int getV (struct S * s) //@ requires s->v |-> ?value; a //@ ensures s->v |-> value &*& result == value; { return s->v; } a࣮ s->v |-> _ͱ͍͏ͷ S_v(s,_) ͷҥߏจ యܕతͳ getter ͷ༷͕هड़Ͱ͖͍ͯΔɻ 8ͦͷείʔϓͰϢχʔΫͳ໊લͰ͋Δ͜ͱɻshadowing ແ͍ɻ 2016/11/19 VeriFast Introduction 14 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ༷ʹؔ͢Δද໌ ༷ʹؔ͢Δද໌Λ
assert จʹΑͬͯهड़Ͱ͖ΔɻσϦϑΝϨϯε໋ͱ ಉ༷ͷύλʔϯΛࢦఆͰ͖Δɻ ༷ʹؔ͢Δද໌ struct boxed { int x; }; void plus_ass (struct boxed * b, int v) //@ requires b->x |-> ?bx &*& 0 < v; //@ ensures b->x |-> (bx + v); { //@ assert b->x |-> _; b->x += v; //@ assert b->x |-> ?new; //@ assert bx < new; } assert هड़ͨ࣌͠ͰίϯςΩετ͔Βಋग़Ͱ͖Δ໋Ͱ͋Δ͜ͱΛ֬ ೝग़དྷΔ 9ɻ 9ιϧόʹର͢ΔώϯτʹͳΒͳ͍͜ͱʹҙɻ 2016/11/19 VeriFast Introduction 15 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর malloc ͱ
free ༷Ͱͷώʔϓͷѻ͍ΛΔͨΊʹ malloc ͱ free ͷ༷Λࣔ͢ 10ɻ malloc ͷ༷ void *malloc(int size ); //@ requires 0 ≤ size; /*@ ensures result == 0 ? emp : malloc_block(result , size) &*& ... @*/ free ͷ༷ void free(void *array ); //@ requires malloc_block(array , ?size); //@ ensures emp; malloc ͷΓʹ͍ͭͯ malloc_block ͱ͍͏໋͕ΓཱͭɻҰํ free ͦΕΛҾʹཁٻ͢Δɻ 10bin/malloc.h ʹ͋Δɻຊͬͱෳࡶɻ 2016/11/19 VeriFast Introduction 16 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর malloc ͱ
free ༷Ͱͷώʔϓͷѻ͍ΛΔͨΊʹ malloc ͱ free ͷ༷Λࣔ͢ 10ɻ malloc ͷ༷ void *malloc(int size ); //@ requires 0 ≤ size; /*@ ensures result == 0 ? emp : malloc_block(result , size) &*& ... @*/ free ͷ༷ void free(void *array ); //@ requires malloc_block(array , ?size); //@ ensures emp; ݕূίϯςΩετதͰΓཱ͍ͬͯΔ malloc_block Λ malloc_block chunk ͱݺͿɻ 10bin/malloc.h ʹ͋Δɻຊͬͱෳࡶɻ 2016/11/19 VeriFast Introduction 16 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ॴ༗ݖΛද໋͢ VeriFast
ϝϞϦͷॴ༗ݖ 11 Λ͢Δɻ ͷߦͰ໋ (malloc_block_ints)12 ͕Γཱ͍ͬͯΔ͕. . . malloc;free void malloc_free (void) //@ requires emp; //@ ensures emp; { int * px = malloc(sizeof(int )); if (!px) abort (); free(px); } ໋ px̸=NULL *px | → _ malloc_block_ints(px,1) 11ϝϞϦͷϥΠϑλΠϜΛࣗ༝ʹ͢ΔݖརͱϝϞϦΛഁغ͢Δٛͷ͜ͱɻ 12malloc_block_ints(p,s) = malloc_block(p,4*s) 2016/11/19 VeriFast Introduction 17 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ॴ༗ݖΛද໋͢ free
͔Βฦͬͨ࣌Ͱ malloc_block_ints Γཱͨͳ͘ͳΔʂ malloc;free void malloc_free (void) //@ requires emp; //@ ensures emp; { int * px = malloc(sizeof(int )); if (!px) abort (); free(px); } ໋ px̸=NULL *px| →_ malloc_block_ints(px,1) px̸=NULL Γཱͬͨ··ͳ͜ͱʹҙɻ 2016/11/19 VeriFast Introduction 17 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ॴ༗ݖͷͳ͍ϙΠϯλ ݕࠪʹࣦഊ͢ΔྫΛݟͯΈΑ͏ɻmalloc_block
͕ແ͍߹ free ग़དྷແ͍ ͷͰɺҎԼͷύλʔϯݕࠪʹࣦഊ͢Δɻ not owned ptr void try_free_stack (void) //@ requires emp; //@ ensures emp; { struct S sv; struct S * sp = &sv; //@ assert sp ->v |-> _; free(sp); // No matching heap chunks: // malloc_block_S (sv_addr) } assert ʹ͢Δͱɺmalloc_block ͱσϦϑΝϨϯε͕ؔผͷ໋Ͱද ͞Ε͍ͯΔҙ͕͔ٛΔɻ 2016/11/19 VeriFast Introduction 18 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর Double Free
࣍ 2 ॏʹϝϞϦΛղ์͠Α͏ͱ͢Δ߹Ͱ͋Δɻfree Ͱ malloc_block ໋͕ফඅ͞ΕΔͷͰ double free ҎԼͷΑ͏ʹΤϥʔʹͳΔɻ double free void try_free_free (void) //@ requires emp; //@ ensures emp; { struct S * ms = malloc(sizeof(struct S)); if (ms == NULL) abort (); free(ms); free(ms); // No matching heap chunks: S_v(ms , _) } malloc_block ͕ফඅ͞Εͨ߹ɺͦΕΛσϦϑΝϨϯε͢Δ໋ಉ࣌ ʹແޮʹͳΔɻ 2016/11/19 VeriFast Introduction 19 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result ੵ & ∗ &(≒ ∧) 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result ੵ & ∗ &(≒ ∧) σϦϑΝϨϯεؔ expr | → ?var 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result ੵ & ∗ &(≒ ∧) σϦϑΝϨϯεؔ expr | → ?var ༷Ϩϕϧද໌ assert 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result ੵ & ∗ &(≒ ∧) σϦϑΝϨϯεؔ expr | → ?var ༷Ϩϕϧද໌ assert ώʔϓΛද໋͢ malloc_block 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর VeriFast ೖ·ͱΊ
͜͜·ͰͰ VeriFast ͷ࠷جຊతͳػೳΛղઆͨ͠ɻ ࣄલ݅ͱࣄޙ݅ requires,ensures ฦΓͷݴٴ result ੵ & ∗ &(≒ ∧) σϦϑΝϨϯεؔ expr | → ?var ༷Ϩϕϧද໌ assert ώʔϓΛද໋͢ malloc_block 2016/11/19 VeriFast Introduction 20 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark2016 TPPMark
ͷҰ෦Λ࠶ܝɻ 2.1(ґଘܕϓϩάϥϛϯάతͳදݱ) ͷҰ෦ Ҿͱͯ͠ҎԼͷ̏ͭ ϦετͷϦετ lst i j Λड͚औͬͨΒɺ. . . ϦετͷϦετʹ͍ͭͯͷૢ࡞Λ࣮͢Δඞཁ͕͋Δɻ 2016/11/19 VeriFast Introduction 21 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ࣮ߦੈքͷϦετ TPPMark
ϦετͷϦετͷૢ࡞Λཁٻ͍ͯ͠ΔͷͰɺ·ͣ C ݴޠͰϦε τͷ࣮Λ༩͑Α͏ɻ int ͷྻΛอ࣋͢ΔܕΛఆٛ͢Δ struct list_t { int val; struct list_t * next; }; આ໌ͷඞཁແ͍ΑͶ. . . 2016/11/19 VeriFast Introduction 22 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর predicate VeriFast
Ͱ C ݴޠͷ·༷ͨதͷͷؒʹΓ໋ཱͭʹ໊લΛ ͚ͯఆٛͰ͖Δɻ͜ͷͨΊʹ predicate ΩʔϫʔυΛ͏ɻ ҎԼͷ໋ list_len ɺlst ͷ͕͞ len Ͱ͋Δ͜ͱΛද͢ 11ɻ list_len ؔ predicate list_len (struct list_t * lst , int len) = lst == NULL ? len == 0 : malloc_block_list_t (lst) &*& lst ->val | → _ &*& lst ->next | → ?next &*& list_len(next , len -1); predicate ؔͰͳ͍ͷͰΓແ͘ɺҾؒʹΓཱͭ݅Λॻ͘ ͜ͱͰఆٛ͞ΕΔɻ 11lst ͱ len ͷؒʹ list_len ͕͕ࣔؔ͢Γཱͭ 2016/11/19 VeriFast Introduction 23 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর predicate ͷ༻ྫ
predicate ͷ VeriFast ͷ༻͢Δ໋ͱͯ͜͠Ε·ͰͷߏจͷதͰ༻ ͢Δ͜ͱ͕Ͱ͖Δɻ predicate ͷ͍ํ void ignore_list (struct list_t * xs) //@ requires list_len(xs , ?len); //@ ensures emp; { //@ assert list_len(xs , ?v); //@ assert v == len; free_list_t(xs); // ϦετΛഁغ } predicate தͰσϦϑΝϨϯε໋ 12 ͱಉ༷ͷύλʔϯ͕ࢦఆͰ͖ɺಋ ೖ͞ΕͨมͦΕҎ߱Ͱ༻Ͱ͖Δɻ 12expr | → ?var 2016/11/19 VeriFast Introduction 24 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর Ϧετૢ࡞ؔͷ༷ ໋
list_len Λͬͯ (࣮ߦ࣌ͷ) Ϧετ্ͷجຊతͳؔͷ༷Λهड़͠ ͯΈΑ͏ɻ ctor ͷ༷ struct list_t * cons_v (int v, struct list_t * tl); //@ requires list_len(tl , ?len); //@ ensures list_len(result , 1+len); length ͷ༷ int length_list_t (struct list_t * lst); /*@ requires list_len(lst , ?len) &*& len ≤ INT_MAX; /*@ ensures list_len(lst , len) &*& result == len; @*/ 2016/11/19 VeriFast Introduction 25 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ cons_v
ͷ࣮ predicate Ͱऍͨ͠ cons_v Λ࣮ͯ͠ΈΑ͏ɻ cons_v ͷ࣮ struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_len(tl , ?len); //@ ensures list_len(result , len +1); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; // } No matching heap chunks:list_len(xs,(len+1)) 2016/11/19 VeriFast Introduction 26 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ cons_v
ͷ࣮ ૉͷ࣮ͰΞϊςʔγϣϯ͕ΓͣɺVeriFast Γʹ͍ͭͯ list_len ͕Γཱͭ͜ͱ͕͔Βͳ͍ɻ cons_v ͷ࣮ struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_len(tl , ?len); //@ ensures list_len(result , len +1); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; // } No matching heap chunks:list_len(xs,(len+1)) 2016/11/19 VeriFast Introduction 26 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ cons_v
ͷ࣮ return ࣌ͰΓ໋ཱͭΛྻڍͯ͠ΈΔɻ cons_v ͷ࣮ struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_len(tl , ?len); //@ ensures list_len(result , len +1); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; // } ໋ xs ̸= NULL malloc_block(xs) xs->val | → v xs->next | → tl list_len(tl,len) No matching heap chunks:list_len(xs,(len+1)) 2016/11/19 VeriFast Introduction 27 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর predicate ͷߏ
close ίϚϯυ close ίϚϯυʹΑͬͯɺͦͷ࣌Ͱಛఆͷ predicate ͕Γཱͭ͜ͱΛࣔ ͢͜ͱ͕ग़དྷΔ 13ɻ cons_v ͷ࣮ struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_len(tl , ?len); //@ ensures list_len(result , len +1); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; //@ close list_len(xs , len +1); } ໋ xs ̸= NULL malloc_block(xs) xs->val | → v xs->next | → tl list_len(tl,len) ໋ xs ̸= NULL list_len(xs,len+1) close 13ࣗಈͰ close ͞ΕΔ͜ͱ͋Δ 2016/11/19 VeriFast Introduction 28 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর predicate ͷߏ
close ίϚϯυ ϝϞϦʹ͍ͭͯͷ໋ফඅ͞ΕΔ͕ɺߏจ͔Βಋग़͞Ε໋ͨ (xs ̸= NULL) ແ͘ͳΒͳ͍͜ͱʹҙɻ cons_v ͷ࣮ struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_len(tl , ?len); //@ ensures list_len(result , len +1); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; //@ close list_len(xs , len +1); } ໋ xs ̸= NULL malloc_block(xs) xs->val | → v xs->next | → tl list_len(tl,len) ໋ xs ̸= NULL list_len(xs,len+1) close 2016/11/19 VeriFast Introduction 28 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ length
࣮ ࣍ʹ͞ΛٻΊΔؔ length_list_t Λ࣮ͯ͠ΈΑ͏ɻ raw length int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { if (lst == NULL) { return 0; // Cannot prove condition result == len } else { // No matching heap chunks: list_t_next(list , _) int n = length_list_t(lst ->next ); return 1 + n; } } ͜Εૉͷ࣮ͰΞϊςʔγϣϯ͕Γͳ͍ɻ 2016/11/19 VeriFast Introduction 29 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ length
࣮ ࣍ʹ͞ΛٻΊΔؔ length_list_t Λ࣮ͯ͠ΈΑ͏ɻ raw length VeriFast ʹΓͱ len ͱͷ͕͔ؔΒͳ͍ɻ { if (lst == NULL) { return 0; // Cannot prove condition result == len } else { // No matching heap chunks: list_t_next(list , _) int n = length_list_t(lst ->next ); return 1 + n; } } ͜Εૉͷ࣮ͰΞϊςʔγϣϯ͕Γͳ͍ɻ 2016/11/19 VeriFast Introduction 29 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ૉͷ length
࣮ ࣍ʹ͞ΛٻΊΔؔ length_list_t Λ࣮ͯ͠ΈΑ͏ɻ raw length ඌ෦ (lst->next) ʹ͍ͭͯ list_len ͕Γཱͭࣄ͔Βͳ͍ɻ { if (lst == NULL) { return 0; // Cannot prove condition result == len } else { // No matching heap chunks: list_t_next(list , _) int n = length_list_t(lst ->next ); return 1 + n; } } ͜Εૉͷ࣮ͰΞϊςʔγϣϯ͕Γͳ͍ɻ 2016/11/19 VeriFast Introduction 29 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ऍ͖ length
࣮ open/close open/close ໋ྩΛࢦఆ͢ΔͱݕূͰ͖Δɻ annotated length int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); if (lst == NULL) { return 0; //@ close list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); } } 2016/11/19 VeriFast Introduction 30 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর open ͷৼ͍
open ໋ྩͰ list_len ͷఆ͕ٛ VeriFast ͷจ຺Ͱల։͞ΕΔ 13ɻ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); if (lst == NULL) { return 0; //@ close list_len(lst , len); predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1)... list_lenΛల։ 13list_len ͕Γཱͭ͜ͱ͔Βͳ͘ͳΔ 2016/11/19 VeriFast Introduction 31 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ then
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); if (lst == NULL) { return 0; //@ close list_len(lst , len); } else { predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1)... ಋग़ lst==NULL ͔Β result==len ͕ಋग़Ͱ͖Δɻ 2016/11/19 VeriFast Introduction 32 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ then
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); if (lst == NULL) { return 0; //@ close list_len(lst , len); } else { predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1)... ΈࠐΉ open Ͱల։͞ΕͨఆٛΛ close Ͱ list_len ʹΈࠐΉɻ 2016/11/19 VeriFast Introduction 32 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ else
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1) ... lst != NULL lst̸=NULL ͔Β n==length(next) ͕ಋग़Ͱ͖Δɻ 2016/11/19 VeriFast Introduction 33 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ else
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1) ... ࠶ؼݺͼग़͢͠Δ length_list_t ͷࣄલ݅Λຬ͍ͨͯ͠Δɻ 2016/11/19 VeriFast Introduction 33 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ else
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1) ... result == n == len-1 length_list_t ͷࣄޙ͔݅Β n==len-1 ͱͳΔɻ 2016/11/19 VeriFast Introduction 33 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ߏจͱ໋͔Βͷಋग़ else
અ͕࣮ߦ͞ΕΔ߹Λݕূ Proof. int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); predicate list_len (list_t * lst , int len) = lst == NULL ? len == 0 : lst ->next |-> ?next &*& list_len(next , len -1) ... 1+n == 1+len-1 1 খ͍͞͞ʹ 1 ͨ͠ͷͰ result len ͱ͘͠ͳΔ͜ͱ͕͔Δɻ 2016/11/19 VeriFast Introduction 33 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ऍ͖ length
࣮ (࠶) annotated length int length_list_t (struct list_t * lst) //@ requires list_len(lst , ?len) &*& len <= INT_MAX; //@ ensures list_len(lst , len) &*& result == len; { //@ open list_len(lst , len); if (lst == NULL) { return 0; //@ close list_len(lst , len); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_len(lst , len); } } 2016/11/19 VeriFast Introduction 34 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark2016 ͱ͜ΖͰ
TPPMark ϦετͷϦετʹ͍ͭͯͷૢ࡞Λཁٻ͍ͯ͠ΔͷͰ . . . 2.1(ґଘܕϓϩάϥϛϯάతͳදݱ) ͷҰ෦ Ҿͱͯ͠ҎԼͷ̏ͭ ϦετͷϦετ lst i j Λड͚औͬͨΒɺ. . . ϦετͷϦετʹ͍ͭͯΓ໋ཱͭΛهड़͢Δඞཁ͕͋Δɻ 2016/11/19 VeriFast Introduction 35 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর int ͷϦετͷϦετ
͜Ε·Ͱ int ͷϦετΛఆٛͨ͠ͷͰɺTPPMark ຊདྷͷతͰ͋ΔϦετ ͷϦετΛఆٛ͠Α͏ɻ int ͷϦετͷϦετͷఆٛ struct list_list_t { struct list_t * list; struct list_list_t * next; }; ͜Εઆ໌ཁΒͳ͍ΑͶɻ 2016/11/19 VeriFast Introduction 36 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ϦετͷϦετͷෆม݅ list_list_t*ʹ͍ͭͯͷෆม݅
list_t*ͷ࣌ͱ΄ͱΜͲมΘΒͳ͍ܗͰ ॻ͚Δɻ list_list_t*ͷ͞ʹ͍ͭͯͷ໋ predicate list_list_len (struct list_list_t * lst , int len) = lst == NULL ? len == 0 : malloc_block_list_list_t (lst) &*& lst ->list | → ?list &*& lst ->next | → ?next &*& list_len(list , _) &*& list_list_len(next , len -1); 2016/11/19 VeriFast Introduction 37 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark ͷܗࣜԽʹ͚ͯ
TPPMark ͷจΛ࠶ܝɻ 2.1(ґଘܕϓϩάϥϛϯάతͳදݱ) Ҿͱͯ͠ҎԼͷ̏ͭ ϦετͷϦετ lst i j Λड͚औͬͨΒɺ lst ͷ i ൪ͷϦετͷ j ൪ͷཁૉΛ আͨ͠ͷΛฦ ؔ͢ remove-lst Λ࡞ͤΑɻͦͷࡍɺҎԼͷ̏ͭͷ݅ lst ͷ i ൪ͷϦετ͕ଘࡏ͢Δɻ i ൪ͷϦετͷ j ൪ͷཁૉ͕ଘࡏ͢Δɻ ฦ͞ΕΔϦετͨͪͷ͞ɺi ൪ͷΈ 1 ͘ͳ͓ͬͯΓɺଞมΘ Βͳ͍ɻ ͕อূ͞ΕΔΑ͏ʹͤΑɻ ʢ͜͜Ͱɺઌ಄͍ͣΕ 0 ൪ͱ͢Δɻ ʣ 2016/11/19 VeriFast Introduction 38 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark ͷܗࣜԽ
Ϧετ (ͷϦετ) ʹؔ͢Δ໋͕ߏͰ͖ΔΑ͏ʹͳͬͨͷͰɺTPPMark ͷ༷ͷܗࣜԽΛ࢝ΊΔɻ remove-lst ͷܗࣜԽͷࢼΈ struct list_list_t * remove_lst(struct list_list_t* lst ,int i,int j); /*@ requires list_list_len(lst , ?len) &*& 0 ≤ i &*& i < len &*& 0 ≤ j; @*/ /*@ ensures list_list_len(lst , len) &*& list_list_len(result , len); @*/ Ϧετͷ༰ʹ͍ͭͯݴٴͰ͖ͳ͍ͷͰෆेͳ໋ʹͳ͍ͬͯΔɻ 2016/11/19 VeriFast Introduction 39 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর list_list_len ʹΓͳ͍ͷ
list_list_len Ϧετͷ͞ʹ͍ͭͯड़͍ͯΔ͕ɺTPPMark ͷཁٻΛ ຬͨͨ͢Ίʹɺ໋தͰϦετͷ༰ʹ͍ͭͯड़Δඞཁ͕͋Δɻ͜ͷ ͨΊ list_t* list_list_t*ͷ͚ͩ͞Ͱͳ͘ɺอ͍࣋ͯ͠Δ (Ϧετ) Λ໋தͰදݱ͢Δඞཁ͕͋Δɻ remove-lst ͷܗࣜԽͷࢼΈ struct list_list_t * remove_lst(struct list_list_t* lst ,int i,int j); /*@ requires list_list_len(lst , ?len) &*& 0 ≤ i &*& i < len &*& 0 ≤ j; @*/ /*@ ensures list_list_len(lst , len) &*& list_list_len(result , len); @*/ 2016/11/19 VeriFast Introduction 40 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ༷ੈքͷϦετ ·༷ͣͷੈքͰ͑ΔϦετܕΛఆٛ͢Δɻ͜Εʹ
inductive Ωʔ ϫʔυΛ͏ 14ɻ Ϧετܕఆٛ inductive list <t> = nil | cons(t, list <t>); <t>ܕมΛද͠ɺlist ܕ͕ଟ૬Ͱ͋Δ͜ͱΛࣔ͢ɻ 14࣮ࡍʹඪ४ϥΠϒϥϦ (list.gh) Ͱఏڙ͞ΕΔͷͰࣗͰఆٛ͢Δඞཁͳ͍ 2016/11/19 VeriFast Introduction 41 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ༷ੈքͷม ༷தͰม͕͑ΔͷͰɺ͜ΕΛͬͯϦετͷڍಈΛ֬ೝ͓ͯ͘͠ɻ
Ϧετͷಈ࡞Λ֬ೝ void list_sample (void) //@ requires emp; //@ ensures emp; { //@ list <int > xs = cons(3, cons(5, nil )); //@ list <int > ys = cons(1, xs); //@ assert xs == {3 ,5}; //@ assert ys == {1 ,3 ,5}; } Ϧετͷಛผʹ{v_0,v_1,v_2,...}ͱ͍͏ϦςϥϧͰߏͰ͖Δɻ 2016/11/19 VeriFast Introduction 42 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ༷ੈքͷؔ VeriFast
Ͱఆٛͨ͠ܕͷσʔλʹ͍ͭͯͷੑ࣭ʹ͍ͭͯड़ΔͨΊ༷ ੈքͰ͕ؔఆٛͰ͖Δɻ͜Εʹ fixpoint ΩʔϫʔυΛ͏ɻ Ϧετͷ͞ΛٻΊΔ fixpoint int length <t>(list <t> xs) { switch (xs) { case nil: return 0; case cons(x, xs0): return 1 + length(xs0); } } inductive ͱಉ͘͡<t>ܕมΛࣔ͢ɻͭ·Γ্ͷίʔυҙͷܕͷϦ ετͷ͞ΛٻΊΔଟ૬ؔͱͳ͍ͬͯΔ 15ɻ 15ඪ४Ͱ bin/list.gh ͔Βఏڙ͞ΕΔ 2016/11/19 VeriFast Introduction 43 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ͦͷଞͷ fixpoint
͜ΕҎ߱Ͱࢀর͢ΔؔΛ͍͔͓ͭࣔͯ͘͘͠ 16ɻ ͦͷଞͷϦετ্ͷؔ fixpoint t nth <t>(int n, list <t> xs) { switch (xs) { case nil: return default_value <t>; case cons(x, xs0): return n == 0 ? x : nth(n - 1, xs0); }} a adefault_value<int> = 0 16͍ͣΕ bin/list.gh ʹ͋Δ 2016/11/19 VeriFast Introduction 44 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ͦͷଞͷ fixpoint
͜ΕҎ߱Ͱࢀর͢ΔؔΛ͍͔͓ͭࣔͯ͘͘͠ 16ɻ ͦͷଞͷϦετ্ͷؔ fixpoint list <t> update <t>(int i, t y, list <t> xs) { switch (xs) { case nil: return nil; case cons(x, xs0): return i == 0 ? cons(y, xs0) : cons(x, update(i - 1, y, xs0 )); }} fixpoint list <t> remove_nth <t>(int n, list <t> xs) { switch(xs) { case nil: return nil; case cons(h, t): return n == 0 ? t : cons(h, remove_nth(n - 1, t)); }} 16͍ͣΕ bin/list.gh ʹ͋Δ 2016/11/19 VeriFast Introduction 44 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর fixpoint ͷ੍ݶ
fixpoint ʹΑΔؔͷఆٛʹɺҙͷ͕ؔهड़Ͱ͖ΔΘ͚Ͱͳ͘ز ੍͔͕ͭ͏ 17ɻ fixpoint ͷ੍ݶ શؔͰ͋Δ͜ͱ ༗ΓಘΔશͯͷೖྗʹ͍ͭͯ݁Ռ͕ఆٛ͞Ε͍ͯΔ͜ͱ a ୯ಠͷ return ·ͨ switch จͰ͋Δ͜ͱ switch ʹͤΔͷ inductive σʔλͷΈ ࠶ؼ͢Δ߹ύλʔϯϚονͰऔΓग़ͨ͠σʔλʹ͍ͭͯࣗΛݺ ΜͰ͍Δ͜ͱ b a෦ؔʹ͍ͭͯޙड़͢Δ predicate Ͱఆٛ͢Δ͜ͱ͕Ͱ͖Δ b͜ΕΛຬ͍ͨͯ͠Ε໌Β͔ʹఀࢭ͢Δ Ҏ্ͷΑ͏ͳ੍ݶΛकΔ͜ͱͰɺʹґͬͯະఆٛ͋Δ͍ݕ͕ࠪࢭ· Βͳ͘ͳΔΑ͏ͳ (͓͔͠ͳ) ༷Λආ͚Δ͜ͱ͕Ͱ͖Δɻ 17ͦΕͧΕ VeriFast ͕ݕࠪ͢Δ 2016/11/19 VeriFast Introduction 45 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ଞͷ inductive
σʔλ option ଞͷ inductive ܕͷྫͱͯ͠ option ܕΛ͓ࣔͯ͘͠ 18ɻ option ܕఆٛ inductive option <t> = none | some(t); option<t>ʮΛ࣋ͨͳ͍ʯΛܕ t ʹՃ͢Δɻ݁ՌΛ࣋ͨͳ͍߹ʹ none Λ͏͜ͱͰ (fixpoint ʹΑΔ) શؔΛఆٛ͢ΔͨΊʹॏๅ͢Δɻ 18prelude_core.gh Ͱఆٛ͞ΕΔ 2016/11/19 VeriFast Introduction 46 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ࣮ߦੈքͷϦετ͕ࣔ͢Ϟϊ ͜͜·ͰͰ࣮ߦੈք
(C ݴޠ) ͷϦετ࣮ͱ໋ੈքͰͷϦετΛఆٛ͠ ͖ͯͨɻ͋ͱ͜ΕΒΛؔ࿈͚ͯɺstruct list_t*ܕͷ͕อ͍࣋ͯ͠ Δ͕ϦετͰ͋Δ͜ͱΛ໋ͱͯ͠දͤΑ͍ɻ 2016/11/19 VeriFast Introduction 47 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর C ݴޠੈքͱ໋ੈքͷଓ
list_t*͕Ϧετ list<int>Λอ͍࣋ͯ͠Δ͜ͱΛ໋ࣔ͢ list_p Λఆٛ ͢Δɻ list_t*ͱ list<int>ʹΓ໋ཱͭ predicate list_p (struct list_t * lst; list <int > xs) = lst == NULL ? xs == nil : malloc_block_list_t (lst) &*& lst ->val | → ?v &*& lst ->next | → ?next &*& list_p(next , ?xs0) &*& xs == cons(v, xs0); list_len ͱಉ༷ʹ࠶ؼతʹఆ͍ٛͯ͠Δ 19ɻ 19Ҿؒͷ; ఆ͍ٛͯ͠Δ͕ؔؔͰ͋Δ͜ͱΛࣔ͢ 2016/11/19 VeriFast Introduction 48 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর Ϧετͷجຊૢ࡞ͷ༷ ໋
list_p ΛͬͯϦετ্ͷؔͷ༷Λमਖ਼͢Δɻ ctor struct list_t * cons_v (int v, struct list_t * tl); //@ requires list_p(tl , ?tl0); //@ ensures list_p(result , cons(v, tl0 )); length int length_list_t (struct list_t * lst); /*@ requires list_p(lst , ?lst0) &*& length(lst0) ≤ INT_MAX; @*/ /*@ ensures list_p(lst , lst0) &*& result == length(lst0 ); @*/ cons_v ͷ༷ίϯςϯπΛͰ͖ΔΑ͏ʹͳΓɺৄࡉʹͳͬͨɻ 2016/11/19 VeriFast Introduction 49 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর cons_v ͷ࣮
(2) ༷ΛৄࡉԽͨ͠ cons_v Λ࣮ͯ͠ΈΑ͏ɻ ctor struct list_t * cons_v (int v, struct list_t * tl) //@ requires list_p(tl , ?ts); //@ ensures list_p(result , cons(v,ts)); { struct list_t * xs = malloc(sizeof(struct list_t )); if (xs == NULL) abort (); xs ->val = v; xs ->next = tl; return xs; //@ close list_p(xs , cons(v,ts)); } list_len ͷ࣌ͱ΄ͱΜͲมΘΒͣɺclose ʹΑͬͯ list_p ͕Γཱͭ͜ͱ Λ໌ࣔ͢Δ͚ͩͰ͋Δɻ 2016/11/19 VeriFast Introduction 50 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর length ͷ࣮
(2) length_list_t ͷ࣮֬ೝ͓ͯ͘͠ɻ length int length_list_t (struct list_t * lst) //@ requires list_p(lst , ?xs) &*& length(xs) <= INT_MAX; //@ ensures list_p(lst , xs) &*& result == length(xs); { //@ open list_p(lst , xs); if (lst == NULL) { return 0; //@ close list_p(lst , xs); } else { int n = length_list_t(lst ->next ); return 1 + n; //@ close list_p(lst , xs); } } ͜ͷ߹ list_len ͷ࣌ͱಉ͘͡ɺೖྗϦετͷߏʹ͍ͭͯ࠶ؼ͢Δͷ Ͱ open ίϚϯυΛ໌ࣔ͢Δ͜ͱͰݕূͰ͖Δɻ 2016/11/19 VeriFast Introduction 51 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর Ϧετͷجຊૢ࡞ͷ༷ ͦͷଞඞཁʹͳΔϦετૢ࡞Λ࣮͢Δ
(͜͜ͰγάωνϟͷΈࣔ͢)ɻ ൺֱ bool eq_list_t (struct list_t * xs , struct list_t * ys); //@ requires list_p(xs , ?xs0) &*& list_p(ys , ?ys0); /*@ ensures list_p(xs , xs0) &*& list_p(ys , ys0) &*& result == (xs0 == ys0); @*/ σΟʔϓίϐʔ struct list_t * dup_list_t (struct list_t * lst); //@ requires list_p(lst , ?lst0 ); //@ ensures list_p(lst , lst0) &*& list_p(result , lst0 ); ͍ͣΕφΠʔϒʹ࣮ͯ͠ open ͱ close Λదʹૠೖ͢Δ͜ͱͰݕূͰ ͖Δɻdup_list_t ͕σΟʔϓίϐʔͩͱ͍͏͜ͱ͕༷ʹݱΕ͍ͯΔ͜ͱ ͕ಡΈऔΕΔͱྑ͍ 20ɻ 20list_p ʹ malloc_block ؚ͕·ΕΔ͜ͱʹҙ 2016/11/19 VeriFast Introduction 52 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর remove ͷ༷
Ϧετͷૢ࡞͕ѻ͑ΔΑ͏ʹͳͬͨͷͰɺ͜͜Ͱ TPPMark ͰඞཁʹͳΔૢ ࡞ͷҰͭͰ͋Δ (remove) Λ࣮͠Α͏ɻ·༷ͣΛࣔ͢ɻ ؔ remove ͷ༷ struct list_t * remove (struct list_t * lst , int i); /*@ requires list_p(lst , ?lst0) &*& 0 ≤ i &*& i < length(lst0 ); @*/ /*@ ensures list_p(lst , lst0) &*& list_p(result , ?lst1) &*& remove_nth(i, lst0) == lst1; @*/ ༷͔Βɺؔ remove i ൪ͷཁૉΛআͨ͠ϦετΛ৽ͨʹߏங͢Δ ؔͱͳ͍ͬͯΔ͜ͱ͕Θ͔Δ 21ɻ 21remove_nth ඪ४ϥΠϒϥϦ͔Βఏڙ͞ΕΔ n ൪ͷཁૉΛআ͢Δؔ 2016/11/19 VeriFast Introduction 53 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর remove ͷ࣮ͱݕূ
ଓ͍ͯɺ༷Λࣔͨؔ͠ remove ͷ࣮Λݕূ͢Δɻ ؔ remove ͷఆٛ struct list_t * remove (struct list_t * lst , int i) /*@ requires list_p(lst , ?lst0) &*& 0 <= i &*& i < length(lst0 ); @*/ /*@ ensures list_p(lst , lst0) &*& list_p(result , ?lst1) &*& remove_nth(i, lst0) == lst1; @*/ { //@ open list_p(lst , lst0 ); if (i == 0) { return dup_list_t(lst ->next ); } else { struct list_t * rst_ = remove(lst ->next , i - 1); struct list_t * lst_ = cons_v(lst ->val , rst_ ); return lst_; } //@ close list_p(lst , lst0 ); } 2016/11/19 VeriFast Introduction 54 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর int ͷϦετͷϦετ
int ͷϦετͷ༰ΛͰ͖ΔΑ͏ʹͳͬͨͷͰɺϦετͷϦετͷ໋ ༰͕Ͱ͖ΔΑ͏ʹ͓ͯ͘͠ɻ int ͷϦετͷϦετͷఆٛ (࠶) struct list_list_t { struct list_t * list; struct list_list_t * next; }; 2016/11/19 VeriFast Introduction 55 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ϦετͷϦετͷෆม݅ (վ)
list_list_t*ͱ list<list<int> >ʹΓ໋ཱͭ predicate list_list_p(struct list_list_t * lst; list <list <int > > xxs) = lst == NULL ? xxs == nil : malloc_block_list_list_t (lst) &*& lst ->list | → ?list &*& lst ->next | → ?next &*& list_p(list , ?list0) &*& list_list_p(next , ?xxs0) &*& xxs == cons(list0 , xxs0 ); list_t*ܕͷ࣌ͱಉ༷ʹमਖ਼͢ΕΑ͍ɻ 2016/11/19 VeriFast Introduction 56 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ϦετͷϦετͷجຊૢ࡞ ϦετͷϦετʹ͍ͭͯඞཁͳجຊૢ࡞Λఆٛ͢Δ
(͜͜Ͱγάωνϟ ͷΈࣔ͢)ɻ cons struct list_list_t * cons_list (struct list_t * lst , struct list_list_t * llst ); /*@ requires list_p(lst , ?lst0) &*& list_list_p(llst , ?llst0 ); @*/ //@ ensures list_list_p(result , cons(lst0 , llst0 )); ͦͷଞͷؔࣗ໌ͳ༷ͳͷͰলུɻ 2016/11/19 VeriFast Introduction 57 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark2016 TPPMark
ͷཁٻΛܗࣜԽ͢ΔͨΊจΛ࠶ܝɻ 2.1(ґଘܕϓϩάϥϛϯάతͳදݱ) Ҿͱͯ͠ҎԼͷ̏ͭ ϦετͷϦετ lst i j Λड͚औͬͨΒɺ lst ͷ i ൪ͷϦετͷ j ൪ͷཁૉΛ আͨ͠ͷΛฦ ؔ͢ remove-lst Λ࡞ͤΑɻͦͷࡍɺҎԼͷ̏ͭͷ݅ lst ͷ i ൪ͷϦετ͕ଘࡏ͢Δɻ i ൪ͷϦετͷ j ൪ͷཁૉ͕ଘࡏ͢Δɻ ฦ͞ΕΔϦετͨͪͷ͞ɺi ൪ͷΈ 1 ͘ͳ͓ͬͯΓɺଞมΘ Βͳ͍ɻ ͕อূ͞ΕΔΑ͏ʹͤΑɻ ʢ͜͜Ͱɺઌ಄͍ͣΕ 0 ൪ͱ͢Δɻ ʣ 2016/11/19 VeriFast Introduction 58 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর TPPMark ͷܗࣜԽ
(࠶ͼ) ͜͜·ͰͰ͔ͬͨൣғΛ౿·͑ͯ࠶ͼ TPPMark ͷཁٻ͢Δ remove_lst ͷ༷ΛܗࣜԽͯ͠ΈΑ͏ɻ remove-lst ͷܗࣜԽͷࢼΈ (2) struct list_list_t * remove_lst(struct list_list_t* lst ,int i,int j); /*@ requires list_list_p(lst , ?lst0 )}@ &*& 0 ≤ i &*& i < length(lst0) &*& 0 ≤ j &*& j < length(nth(i,lst0 )); @*/ /*@ ensures list_list_p(lst , lst0) &*& list_list_p(result , ?lst1 ); @*/ ΄ͱΜͲશͯͷ෦͕ܗࣜԽͰ͖ͨ 22ɻ͋ͱ lst1 ͕ͲͷΑ͏ͳ݅Λຬ ͖͔ͨ͢Λهड़͢Δ͚ͩͰ͋Δɻ 22ͱࢥ͏ 2016/11/19 VeriFast Introduction 59 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর remove_lst_p Γͷ༷ͷܗࣜԽʹඞཁͳͷɺؔͷΓ͕ೖྗϦετʹର͢Δ
( ༷௨Γͷ) ૢ࡞ͷ݁ՌͱҰக͢Δ͜ͱΛද໌͢Δ͜ͱͰ͋Δɻ͜ͷͨΊʹҎ ԼͷΑ͏ͳؔ remove_lst_p Λఆٛ͢Δɻ remove_lst_p fixpoint option <list <list <t> > > remove_lst_p <t>(list <list <t> > lst , int i, int j) { return (i < 0 || length(lst) <= i) ? none : (j < 0 || length(nth(i, lst)) <= j) ? none : some( update(i, remove_nth(j, nth(i, lst)), lst )); } 2016/11/19 VeriFast Introduction 60 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ओఆཧ Α͏͘
TPPMark ͷཁٻશͯΛද໋͢ΛܗࣜԽ͢Δ͜ͱ͕ग़དྷΔɻ remove_lst ͷ༷ struct list_list_t * remove_lst (struct list_list_t * lst , int i, int j) /*@ requires list_list_p(lst , ?lst0) &*& 0 ≤ i &*& i < length(lst0) &*& 0 ≤ j &*& j < length(nth(i,lst0 )); @*/ /*@ ensures list_list_p(lst , lst0) &*& list_list_p(result , ?lst1) &*& remove_lst_p(lst0 , i, j) == some(lst1 ); @*/ 2016/11/19 VeriFast Introduction 61 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ͜Ε·Ͱఆ͖ٛͯͨؔ͠ΛΈ߹Θ࣮ͤͯͰ͖Δɻ remove_lst
ͷ࣮ struct list_list_t * remove_lst (struct list_list_t * lst , int i, int j) /*@ requires list_list_p(lst , ?lst0) &*& 0 <= i &*& i < length(lst0) &*& 0 <= j &*& j < length(nth(i,lst0 )); @*/ /*@ ensures list_list_p(lst , lst0) &*& list_list_p(result , ?lst1) &*& remove_lst_p(lst0 , i, j) == some(lst1 ); @*/ { //@ open list_list_p(lst , lst0 ); if (i == 0) { list_t * rst = remove(lst ->list , j); list_list_t * next = dup_list_list_t (lst ->next ); return cons_list(rst , next ); } else { list_list_t * rest = remove_lst(lst ->next , i-1, j); return cons_list(dup_list_t(lst ->list), rest ); } } 2016/11/19 VeriFast Introduction 62 / 77 -
࣍ VeriFast ֓આ VeriFast ೖ جຊ෦ͷఆٛ ओఆཧͷূ໌ ࢀর ओఆཧ ࠷ޙʹ͏Ұݕ༷ࠪͨ͠Λࣔͯ͠ऴΘΖ͏ɻ
remove_lst ͷ༷ struct list_list_t * remove_lst (struct list_list_t * lst , int i, int j) /*@ requires list_list_p(lst , ?lst0) &*& 0 ≤ i &*& i < length(lst0) &*& 0 ≤ j &*& j < length(nth(i,lst0 )); @*/ /*@ ensures list_list_p(lst , lst0) &*& list_list_p(result , ?lst1) &*& remove_lst_p(lst0 , i, j) == some(lst1 ); @*/ 2016/11/19 VeriFast Introduction 63 / 77 -
ࢀߟࢿྉ VeriFast official web site https://people.cs.kuleuven.be/~bart.jacobs/verifast/ VeriFast Tutorial(ຊޠ൛) https://github.com/jverifast-ug/translate/ 12th
Theorem Proving and Provers (TPP2016) http://pllab.is.ocha.ac.jp/~asai/tpp2016/ J. C. Reynolds. Separation Logic: A Logic for Shared Mutable Data Structures* Proceedings of the Seventeenth Annual IEEE Symposium on Logic in Computer Science, held July 22-25, 2002 in Copenhagen, Denmark. http://www.cs.cmu.edu/~jcr/seplogic.pdf -
Appendix remove ͷ࣮ ຊจதͰ࣮Λ͍ࣔͯ͠ͳ͍ͷͰ͜͜Ͱ͓ࣔͯ͘͠ɻ ؔ remove ͷγάωνϟ (࠶ܝ) // i-th
ཁૉΛআͨ͠ϦετΛߏங struct list_t * remove (struct list_t * lst , int i); /*@ requires list_p(lst , ?lst0) &*& 0 ≤ i &*& i < length(lst0 ); @*/ /*@ ensures list_p(lst , lst0) &*& list_p(result , ?lst1) &*& remove_nth(i, lst0) == lst1; @*/ 2016/11/19 VeriFast Introduction 65 / 77 -
Appendix remove ͷ࣮ open ͱ close Λదʹࢦఆ͢Δ͚ͩɻ remove ͷऍ͖࣮ struct
list_t * remove (struct list_t * lst , int i) /*@ requires list_p(lst , ?lst0) &*& 0 <= i &*& i < length(lst0 ); @*/ /*@ ensures list_p(lst , lst0) &*& list_p(result , ?lst1) &*& remove_nth(i, lst0) == lst1; @*/ { //@ open list_p(lst , lst0 ); if (i == 0) { return dup_list_t(lst ->next ); } else { struct list_t * rst_ = remove(lst ->next , i - 1); struct list_t * lst_ = cons_v(lst ->val , rst_ ); return lst_; } //@ close list_p(lst , lst0 ); } 2016/11/19 VeriFast Introduction 66 / 77 -
Appendix ิ ͋Δ໋͕Γཱ͍ͬͯΔͱ͖ʹɺ͔ͦ͜Β (ࣗ໌Ͱͳ͍) ໋Λಋग़ͨ͠ ͍߹ɺ͜ΕΛ lemma ΩʔϫʔυͰఆٛ͢Δ͜ͱ͕Ͱ͖Δɻ lemma ఆٛͷҰൠܗ
lemma t mylemma <t,...> (args ...) requires ..; ensures ..; { open ... close ... } C ݴޠͷؔͱಉ͡Α͏ʹࣄલ/ࣄޙ͕݅ݕࠪ͞ΕΔ 23ɻlemma Λهड़͢ Δͱࣄޙ͕݅ VeriFast ͷཧ͢Δ໋ू߹ʹՃ͞ΕɺҎ߱ͷݕূͰར ༻͞ΕΔɻ 23ୠ͠ lemma ඞͣఀࢭ͢Δඞཁ͕͋ΔͷͰϧʔϓʹ͍ͭͯ invariant ͷଞʹݮগ͢ ΔύϥϝʔλΛ໌ࣔ͢Δඞཁ͕͋Δ 2016/11/19 VeriFast Introduction 67 / 77 -
Appendix ิ:remove_nth_minus1len remove ͕ཁૉΛ 1 ݮΒ͢͜ͱΛิͱͯ͠ఆٛ͢Δɻ Lemma. remove_nth_minus1len lemma list
<t> remove_nth_minus1len <t>(int i, list <t> xs); requires 0 ≤ i &*& i < length(xs); ensures length(xs) == 1 + length(result) &*& result == remove_nth(i, xs); 2016/11/19 VeriFast Introduction 68 / 77 -
Appendix ิͷূ໌ remove_lst ͷ࣮ lemma list <t> remove_nth_minus1len <t>(int i,
list <t> xs) requires 0 <= i &*& i < length(xs); ensures length(xs) == 1 + length(result) &*& result == remove_nth(i, xs); { switch (xs) { case nil: assert false; case cons(x,xs0): if (i == 0) { return xs0; } else { list <t> rs = remove_nth_minus1len (i-1, xs0); return cons(x,rs); } } } 2016/11/19 VeriFast Introduction 69 / 77 -
Appendix ݅ 3 ͷݕূ ͦ͏͍͑... remove_lst ͷࣄޙ݅Ͱɺͷཁٻ͢Δ ( 2.1) ݅
3 ฦ͞ΕΔϦετͨͪͷ͞ɺi ൪ͷΈ 1 ͘ͳ͓ͬͯΓɺଞมΘΒ ͳ͍ɻ ͕ຬͨ͞ΕΔ͜ͱΛड़͍ͯͳ͍ɻ 2016/11/19 VeriFast Introduction 70 / 77 -
Appendix ݅ 3 ͷݕূ ͦ͏͍͑... remove_lst ͷࣄޙ݅Ͱɺͷཁٻ͢Δ ( 2.1) ݅
3 ฦ͞ΕΔϦετͨͪͷ͞ɺi ൪ͷΈ 1 ͘ͳ͓ͬͯΓɺଞมΘΒ ͳ͍ɻ ͕ຬͨ͞ΕΔ͜ͱΛड़͍ͯͳ͍ɻ =⇒ ผʹݕূ͢Δ 2016/11/19 VeriFast Introduction 70 / 77 -
Appendix ݅ 3 Λಋग़͢Δิ ݅ 3 remove_lst ͷࣄޙ͔݅Βಋग़Ͱ͖ΔͷͰɺ͜ΕΛิͰද͢ɻ Lemma
remove_lst_inv lemma void remove_lst_inv <t>(list <list <t> > lst , int i, int j) requires remove_lst_p(lst , i, j) == some (?rst); ensures // 0..i ·Ͱͷ͞มΘΒͳ͍ map(length , take(i,lst)) == map(length , take(i,rst)) &*& length(nth(i,lst)) == 1 + length(nth(i,rst)) &*& // i+1..n ·Ͱͷ͞มΘΒͳ͍ map(length , drop(i+1,lst)) == map(length , drop(i+1,rst )); 2016/11/19 VeriFast Introduction 71 / 77 -
Appendix ݅ 3 Λಋग़͢Δิ remove_lst_inv ͷূ໌ switch (lst) { case
nil: assert false; // ͜ͷέʔεʹͳΒͳ͍ case cons(x,lst0 ): if (0 == i) { assert 0 <= j && j < length(nth(0,lst )); remove_nth_minus1len (j, nth(0,lst )); } else if (0 < i) { remove_lst_inv (lst0 , i-1, j); // ࠶ؼ } } remove_lst_p fixpoint ͳͷͰ open ͢Δඞཁͳ͍ (Ͱ͖ͳ͍) 2016/11/19 VeriFast Introduction 72 / 77 -
Appendix ͏Ұͭͷ 2.2 ʢཧతͳදݱʣ ҎԼͷ̏ͭͷσʔλ͕͋Δɻ ϦετͷϦετ lst i
j ͜ͷͱ͖ɺlst ͷ i ൪ͷϦετͷ j ൪ͷ ཁૉΛऔΕΔͳΒɺҎԼΛຬ ͨ͢Ϧετ͕ʢ།Ұʣଘࡏ͢Δ͜ͱΛࣔͤɻlst ͷதͷ i ൪ͷϦετʹ͍ͭͯɺͦͷ j ൪ͷ ཁૉΛऔΓআ͖ɺ ͦΕҎ֎ͷϦετʹ͍ͭͯɺͱͷ·· Ͱ͋ΔΑ͏ͳϦετͰɺฦͬͯ͘ΔϦετͨͪͷ͞ɺ i ൪ͷϦετʹ͍ͭͯɺͱͷϦετΑΓ 1 ͘ͳ͓ͬͯΓɺ ͦΕҎ֎ʹ͍ͭͯɺͱͷϦετͱಉ͡ ʹͳ͍ͬͯΔɻ ʢ͜͜Ͱɺઌ಄͍ͣΕ 0 ൪ͱ͢Δɻ ʣ 2016/11/19 VeriFast Introduction 74 / 77 -
Appendix fixpoint ʮཧతͳදݱʯͷग़ʹରͯ͠ճ͢Δ߹Ͱɺզʑ fixpoint Λ ͬͯࢦఆ͞Εͨૢ࡞Λఆٛͨ͠ͷͰ 24ɺ ͦͷ݁ՌಘΒΕΔ͕Ұҙʹܾ·Δ͜ͱࣗ໌Ͱ͋Δ 24fixpoint ؔ
2016/11/19 VeriFast Introduction 75 / 77 -
Appendix predicate/lemma predicate ͱͯ͠ఆٛͨ͠߹;(semicolon) ΛͬͯؔͰ͋Δ͜ͱΛ ͔ࣔ͢ɺͦΕ͕Ͱ͖ͳ͍߹ 25lemma Ͱࣔ͢͜ͱ͕ग़དྷΔɻ predicate(;) Λ͏
predicate unique_p <T,U> (T t; U u) = switch (t) { case t0: u == ... ิΛ͏ lemma void p_is_unique (T t); requires P(t, ?u1) &*& P(t, ?u2); ensures P(t, u1) &*& P(t, u2) &*& u1 == u2; 25͜ͷνΣοΫҙ֎ͱ௨͢ͷ͕໘ͩͬͨΓ͢Δ 2016/11/19 VeriFast Introduction 76 / 77 -
Appendix VeriFast ͦͷଞͷೳྗ ຊࢿྉதͰࣔͨ͠Ҏ֎ʹ VeriFast Ͱग़དྷΔ͜ͱΛࣔ͢ 26ɻ fractional permission(ׂॴ༗ݖ) εϨουؒͰσʔλΛڞ༗͢Δ߹ɺॴ༗ݖ
1/n ʹͳΔ ՄมҾ ઐ༻ͷϦετ (vararg) ͱͯ͠ݴٴͰ͖Δ ؔͷఀࢭੑอূ Bart Jacobs,2015 ࣄલ/ࣄޙ݅ͷଞʹఀࢭ͢Δ͜ͱΛࣔ͢ऍ (terminates) ͕͋Δ ؔϙΠϯλͷݕূ ಛఆͷछྨͷؔͰ͋Δ͜ͱΛ໌ࣔ͢Δ ߴ֊ Lemma ૬ޓ࠶ؼͨ͠ Lemma ͷݕূʹ͏ શশྔԽ͞Ε໋ͨ 27 forall_(ΈࠐΈߏจ) Λ͏ is_forall_t(ඪ४ϥΠϒϥϦຐज़) Λ͏ 26͜ΕͰશͯͰͳ͍ɻbin/ͱ examples/ΛړΔ͠ 27࣮ࣄલ݅Ͱಋೖ͞ΕͨมશশྔԽ͞Ε͍ͯΔͱݟ၏ͤΔ 2016/11/19 VeriFast Introduction 77 / 77