Package: alhazen-py
alhazen-pyThis repository contains a complete python implementation of the debugging toolAlhazen,
initially proposed by Kampmann et al. [KHZ2020].
With this reimplementation we not only provide a concise and fast version ofAlhazen,
we also include more machine learning models to explain the circumstances of failing programs.How to useAlhazenTo illustrateAlhazen’s capabilities, we start with a quick motivating example. First, let us introduce our program under test: The Calculator.importmathdefarith_eval(inp)->float:returneval(str(inp),{"sqrt":math.sqrt,"sin":math.sin,"cos":math.cos,"tan":math.tan})This infamous program accepts arithmetic equations, trigonometric functions and allows us to calculate the square root.
To help us determine faulty behavior, i.e., a crash, we implement an evaluation functionfromalhazen.oracleimportOracleResultdefprop(inp:str)->bool:try:arith_eval(inp)returnOracleResult.NO_BUGexceptValueError:returnOracleResult.BUGreturnOracleResult.UNDEFthat takes an input file and returns whether a bug occurred during the evaluation of the mathematical equations (BUG=True,NO_BUG=False).
We can now test the calculator with some sample inputs:inputs=['cos(10)','sqrt(28367)','tan(-12)','sqrt(-3)']print([(x,prop(x))forxininputs])The output looks like this:[('cos(10)', OracleResult.NO_BUG),
 ('sqrt(28367)', OracleResult.NO_BUG),
 ('tan(-12)', OracleResult.NO_BUG),
 ('sqrt(-3)', OracleResult.BUG)]We see thatsqrt(-3)results in the failure of our calculator program.
We can now useAlhazento learn the root causes of the program's failure.First, we need to define the input format of the calculator with a grammar:importstringgrammar={"<start>":["<arith_expr>"],"<arith_expr>":["<function>(<number>)"],"<function>":["sqrt","sin","cos","tan"],"<number>":["<maybe_minus><onenine><maybe_digits>"],"<maybe_minus>":["","-"],"<onenine>":[str(num)fornuminrange(1,10)],"<digit>":list(string.digits),"<maybe_digits>":["","<digits>"],"<digits>":["<digit>","<digit><digits>"],}Then, we can callAlhazenwith the grammar, some sample inputs, and the evaluation function (program under test).fromalhazenimportAlhazenalhazen=Alhazen(initial_inputs=inputs,grammar=grammar,evaluation_function=prop,)trees=alhazen.run()By default,Alhazenwill do10iterations of its refinement process.
Finally,Alhazenreturns the learned decision tree that describes the failure-inducing inputs.For our calculator, the learned decision tree looks something like this:We see that the failure occurs whenever we use thesqrt(x)function and the number x has a negative sign!Project StructureIn this repository, you find:thereimplementationandsource codeofAlhazen-py, anda complete introduction (jupyter-notebook) on how to useAlhazen-pyand how you can add your ownLearnersandGenerators.Install, Development, Testing, BuildInstallIf all external dependencies are available, a simple pip install alhazen-py suffices.
We recommend installingalhazen-pyinside a virtual environment (virtualenv):python3.10 -m venv venv
source venv/bin/activate

pip install --upgrade pip
pip install alhazen-pyNow, the alhazen command should be available on the command line within the virtual environment.Development and TestingFor development, we recommend usingalhazen-pyinside a virtual environment (virtualenv).
By thing the following steps in a standard shell (bash), one can run the Alhazen tests:git clone https://github.com/martineberlein/alhazen-py.git
cd alhazen-py/

python3.10 -m venv venv
source venv/bin/activate

pip install --upgrade pip

# Run tests
pip install -e .[dev]
python3 -m pytestBuildalhazen-pyis build locally as follows:git clone https://github.com/martineberlein/alhazen-py.git
cd alhazen-py/

python3.10 -m venv venv
source venv/bin/activate

pip install --upgrade pip
pip install --upgrade build
python3 -m buildThen, you will find the built wheel (*.whl) in the dist/ directory.ContributorsThe initial idea for writing the guide was to explain to our students at Humboldt-Universität Zu Berlin how to useAlhazento determine the failure circumstances of a program.
The original notebooks were a joint project with my colleagueHoang Lam Nguyenfrom Humboldt-Universität Zu Berlin.