verified subtyping with traits and mixins
play

Verified Subtyping with Traits and Mixins Asankhaya Sharma - PowerPoint PPT Presentation

Verified Subtyping with Traits and Mixins Asankhaya Sharma Na;onal University of Singapore Object Oriented Design S Single Responsibility O


  1. Verified ¡Subtyping ¡with ¡Traits ¡ and ¡Mixins ¡ Asankhaya ¡Sharma ¡ Na;onal ¡University ¡of ¡Singapore ¡

  2. Object ¡Oriented ¡Design ¡ • S ¡– ¡Single ¡Responsibility ¡ • O ¡– ¡Open ¡Close ¡ • L ¡– ¡Liskov ¡Subs;tu;on ¡ • I ¡– ¡Interface ¡Segrega;on ¡ • D ¡– ¡Dependency ¡Inversion ¡ First ¡proposed ¡by ¡Robert ¡C. ¡Mar;n ¡in ¡ comp.object ¡newsgroup ¡in ¡March ¡1995 ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 2 ¡

  3. Liskov ¡Subs;tu;on ¡Principle ¡ ¡ “Let ¡q(x) ¡be ¡a ¡property ¡provable ¡about ¡objects ¡x ¡ of ¡type ¡T. ¡Then ¡q(y) ¡should ¡be ¡provable ¡for ¡ objects ¡y ¡of ¡type ¡S ¡where ¡S ¡is ¡a ¡subtype ¡of ¡T” ¡ ¡-­‑ ¡ ¡Barbara ¡Liskov ¡and ¡JeanneXe ¡Wing ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 3 ¡

  4. Behavior ¡Subtyping ¡ • Useful ¡to ¡reason ¡about ¡design ¡of ¡class ¡ hierarchies ¡ • Stronger ¡no;on ¡than ¡typical ¡subtyping ¡of ¡ func;ons ¡defined ¡in ¡type ¡theory ¡ ¡ – Func;on ¡subtyping ¡is ¡based ¡on ¡contravariance ¡of ¡ argument ¡types ¡and ¡covariance ¡of ¡the ¡return ¡type ¡ • ¡Behavioral ¡subtyping ¡is ¡trivially ¡undecidable ¡ – If ¡the ¡property ¡is ¡“this ¡method ¡always ¡terminates” ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 4 ¡

  5. Contribu;ons ¡ • Subtyping ¡with ¡Traits ¡and ¡Mixins ¡in ¡Scala ¡ – By ¡checking ¡entailments ¡in ¡separa;on ¡logic ¡ • Extend ¡Scala ¡with ¡a ¡domain ¡specific ¡language ¡ (SLEEK ¡DSL) ¡ – Allows ¡Scala ¡programmers ¡to ¡insert ¡subtyping ¡ checks ¡in ¡their ¡programs ¡ • Case ¡study ¡on ¡subtyping ¡in ¡Scala ¡Standard ¡ Library ¡ – Verified ¡subtyping ¡in ¡67% ¡of ¡Mixins ¡ ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 5 ¡

  6. Traits ¡and ¡Mixins ¡ • Traits ¡ – Fine ¡grained ¡unit ¡of ¡reuse ¡ – Similar ¡to ¡abstract ¡classes ¡but ¡some ¡methods ¡can ¡ have ¡implementa;ons ¡as ¡well ¡ • Mixin ¡Composi;on ¡(Mixin) ¡ – A ¡class ¡which ¡contains ¡a ¡combina;on ¡of ¡methods ¡ from ¡other ¡traits ¡and ¡classes ¡ – Similar ¡to ¡mul;ple ¡inheritance ¡if ¡the ¡combina;on ¡ contains ¡all ¡methods ¡of ¡combined ¡classes ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 6 ¡

  7. Traits ¡Example ¡ trait ¡ICell ¡{ ¡ ¡ def ¡get() ¡: ¡Int ¡ Similar ¡to ¡an ¡ ¡ Abstract ¡Class ¡ def ¡set(x ¡: ¡Int) ¡ } ¡ trait ¡BICell ¡extends ¡ICell ¡{ ¡ private ¡var ¡x ¡: ¡Int ¡ ¡= ¡0 ¡ Implementa;on ¡ of ¡ICell ¡ def ¡get() ¡{ ¡x ¡} ¡ def ¡set(x ¡: ¡Int) ¡{ ¡this.x ¡= ¡x ¡} ¡ } ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 7 ¡

  8. Traits ¡Example ¡(cont.) ¡ trait ¡Double ¡extends ¡ICell ¡{ ¡ abstract ¡override ¡def ¡set(x ¡: ¡Int) ¡{ ¡ ¡super.set(2*x) ¡ ¡} ¡ } ¡ trait ¡Inc ¡extends ¡ICell ¡{ ¡ abstract ¡override ¡def ¡set(x ¡: ¡Int) ¡{ ¡ ¡super.set(x+1) ¡ ¡} ¡ } ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 8 ¡

  9. Mixins ¡Example ¡ • OddICell ¡(odd ¡values) ¡ – class ¡OddICell ¡extends ¡BICell ¡with ¡Inc ¡with ¡Double ¡ • EvenICell ¡(even ¡values) ¡ – class ¡EvenICell ¡extends ¡BICell ¡with ¡Double ¡with ¡Inc ¡ OddICell ß Double ß Inc ß BICell ¡ Class ¡ Lineariza;on ¡ EvenICell ß Inc ß Double ß BICell ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 9 ¡

  10. Scala ¡doesn’t ¡enforce ¡Subtyping ¡ def ¡m ¡(c: ¡BICell ¡with ¡Inc ¡with ¡Double) ¡: ¡Int ¡ { ¡c.get ¡} ¡ val ¡oic ¡= ¡new ¡OddICell ¡ val ¡eic ¡= ¡new ¡EvenICell ¡ m(oic) ¡ Both ¡calls ¡are ¡allowed ¡ m(eic) ¡ by ¡the ¡type ¡system ¡ Only ¡object ¡oic ¡is ¡subtype ¡of ¡c ¡ ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 10 ¡

  11. Verified ¡Subtyping ¡ • A ¡mixin ¡can ¡be ¡represented ¡as ¡a ¡separa;on ¡ logic ¡predicate ¡based ¡on ¡class ¡lineariza;on ¡ OddICell ß Double ß Inc ß BICell ¡ OddICell<this> ¡== ¡BICell<this,p> ¡* ¡Inc<p,q> ¡* ¡Double<q,null> ¡ ¡ EvenICell ß Inc ß Double ß BICell ¡ EvenICell<this> ¡== ¡BICell<this,p> ¡* ¡Double<p,q> ¡* ¡Inc<q,null> ¡ ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 11 ¡

  12. From ¡Subtyping ¡to ¡Entailment ¡ • Subtyping ¡can ¡be ¡reduced ¡to ¡checking ¡ entailment ¡between ¡the ¡predicates ¡ m(oic) ¡ OddICell<oic> ¡|-­‑ ¡BICell<c,p> ¡* ¡Inc<p,q> ¡* ¡Double<q,null> ¡ ¡ Valid ¡ m(eic) ¡ EvenICell<eic> ¡|-­‑ ¡BICell<c,p> ¡* ¡Inc<p,q> ¡* ¡Double<q,null> ¡ ¡ Invalid ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 12 ¡

  13. SLEEK ¡DSL ¡ • Implementa;on ¡based ¡on ¡SLEEK ¡ – An ¡exis;ng ¡separa;on ¡logic ¡based ¡entailment ¡ prover ¡ – Supports ¡user ¡defined ¡predicates ¡and ¡user ¡ specified ¡lemmas ¡ – With ¡Shape, ¡Size, ¡and ¡Bag ¡proper;es ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 13 ¡

  14. Implementa;on ¡Overview ¡ sleek.lib ¡ SLEEK ¡exe ¡ sleek.dsl ¡ sleek.inter ¡ Scala ¡ Scala ¡ Programs ¡ Interpreter ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 14 ¡

  15. Scala ¡with ¡SLEEK ¡DSL ¡ • Insert ¡checks ¡in ¡Scala ¡programs ¡to ¡verify ¡ subtyping ¡using ¡SLEEK ¡DSL ¡ def ¡m ¡(c: ¡BICell ¡with ¡Inc ¡with ¡Double) ¡: ¡Int ¡{ ¡c.get ¡} ¡ val ¡oic ¡= ¡new ¡OddICell ¡ val ¡eic ¡= ¡new ¡EvenICell ¡ if ¡(OddICell<oic> ¡|-­‑ ¡BICell<c,p> ¡* ¡Inc<p,q> ¡* ¡Double<q,null> ¡) ¡ ¡m(oic) ¡ if ¡(EvenICell<eic> ¡|-­‑ ¡BICell<c,p> ¡* ¡Inc<p,q> ¡* ¡Double<q,null>) ¡ ¡m(eic) ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 15 ¡

  16. Experiments ¡ • Scala ¡Standard ¡Library ¡ • Considered ¡four ¡class ¡hierarchies ¡ – Excep;ons ¡ – Maths ¡ – Parser ¡Combinators ¡ – Collec;ons ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 16 ¡

  17. Results ¡ Class ¡ Mixins ¡in ¡the ¡ Mixins ¡with ¡ Percentage ¡ Hierarchy ¡ Hierarchy ¡ Verified ¡Subtyping ¡ Excep;ons ¡ 11 ¡ 11 ¡ 100 ¡ Maths ¡ 5 ¡ 4 ¡ 80 ¡ Combinators ¡ 6 ¡ 6 ¡ 100 ¡ Collec;ons ¡ 27 ¡ 12 ¡ 44 ¡ Total ¡ 49 ¡ 33 ¡ 67 ¡ Traits ¡provide ¡more ¡flexibility, ¡33% ¡of ¡Mixins ¡use ¡Traits ¡in ¡a ¡way ¡ that ¡does ¡not ¡conform ¡to ¡subytping ¡ ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 17 ¡

  18. Conclusions ¡ • We ¡use ¡entailment ¡proving ¡in ¡separa;on ¡logic ¡ to ¡check ¡subtyping ¡with ¡Traits ¡and ¡Mixins ¡ • A ¡domain ¡specific ¡language ¡based ¡on ¡SLEEK ¡to ¡ check ¡entailments ¡from ¡Scala ¡programs ¡ • Case ¡study ¡based ¡on ¡Scala ¡Standard ¡Library ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 18 ¡

  19. Perspec;ves ¡ • Lays ¡the ¡founda;on ¡for ¡verifying ¡OO ¡Scala ¡ programs ¡ – Specifica;on ¡reuse ¡with ¡traits ¡and ¡mixins ¡ ¡ – Inheritance ¡verifica;on ¡ • Sta;c ¡and ¡Dynamic ¡Specifica;ons ¡for ¡traits ¡ and ¡mixins ¡ – Avoid ¡re-­‑verifica;on ¡ ¡ – Composi;onal ¡and ¡modular ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 19 ¡

  20. Thank ¡You ¡! ¡ • Ques;ons ¡? ¡ • Scala ¡with ¡SLEEK ¡DSL ¡ – Web ¡Tool, ¡Source ¡Code ¡and ¡Sample ¡Programs ¡ – hXp://loris-­‑7.ddns.comp.nus.edu.sg/~project/ SLEEKDSL/ ¡ ¡ • Contact ¡ – asankhaya@nus.edu.sg ¡ 13/5/14 ¡ FSFMA ¡2014 ¡ 20 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend