Giter Site home page Giter Site logo

lslpy's Introduction

lslpy

lslpy is a library that enables contracts, property-based randomized testing, and verification via symbolic execution in Python, inspired by LSL.

Installation

lslpy requires python3.10 or higher.

  1. Clone the repository and move into its directory:
git clone https://github.com/becklabs/lslpy.git && cd lslpy
  1. Install the pip package:
pip3 install -e .

Example Usage

Property-based testing of reverse:

from lslpy import check_contract, contract
from lslpy.contracts import  List, Integer, Constant


@contract
def my_reverse(l: List[Integer]) -> List[Integer]:
    l.reverse()
    return l


check_contract(my_reverse)


@contract
def my_reverse_prop(l1: List[Integer], l2: List[Integer]) -> Constant[True]:
    return my_reverse(l1 + l2) == my_reverse(l2) + my_reverse(l1)


check_contract(my_reverse_prop)

Verification of bad_mult via symbolic execution:

from lslpy import contract, verify_contract
from lslpy.contracts import Integer, Constant

@contract
def bad_mult(x: Integer, y: Integer) -> Integer:
    return 0 if x == 10456 else (x * y)

@contract
def bad_mult_prop(x: Integer, y: Integer) -> Constant[True]:
    return bad_mult(x, y) == x * y

verify_contract(bad_mult_prop, global_vars=globals())

This snippet will raise the following ContractViolation:

Found counterexample: bad_mult_prop(y = -7651, x = 10456)

lslpy's People

Contributors

becklabs avatar

Stargazers

Noah Shinn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.