Package: imandra
Imandra CLI and API client libraryImandrais a cloud-native automated reasoning engine for analysis of algorithms and data.This package contains a python libraryimandrafor interacting with Imandra's web APIs, to do things such as:Creating cloud-hosted Imandra Core instances to interact with Imandra via:Jupyter Notebooksthe HTTP API wrapper around an Imandra Core session,imandra-http-api.Submitting Imandra Protocol Languge (IPL) analysis jobsIf you're interested in developing Imandra models, you may also want to see themain Imandra docs site, and consider setting up Imandra Core locally by following the installation instructions there.Theimandrapython API reference documentation is availablehere.Theimandrapackage also adds an entry point calledimandra-cliwhich exposes theimandralibrary functionality in a more discoverable way:$python3-mvenv./my/venv
...
$./my/venv/pipinstallimandra
...
$./my/venv/bin/imandra-cli--help
usage:imandra[-h]auth,ipl,core,rule-synth,cfb...

ImandraCLI

positionalarguments:{auth,ipl,core,rule-synth,cfb}optionalarguments:-h,--helpshowthishelpmessageandexitOn Windows, the entry point can be found as.\my\venv\Scripts\imandra-cli.exe.AuthenticationThis is the first step to start using Imandra via APIs. Our cloud environment requires a user account, which you may setup like this:$./my/venv/bin/imandra-cliauthloginand follow the prompts to authenticate. This will create the relevant credentials in~/.imandra(or%APPDATA%\imandraon Windows).You should now be able to invoke CLI commands that require authentication, and construct anauthobject from python code:importimandra.authauth=imandra.auth.Auth()This auth object can then be passed to library functions which make requests to Imandra's web APIs.Quick Start with Python APIs:Theimandra.sessionclass provides an easy-to-use interface for requesting and managing an instance of Imandra Core within our cloud environment. It has built-in use ofauthclass described above.importimandrawithimandra.session()ass:s.eval("let f x = if x > 0 then if x * x < 0 then x else x + 1 else x")verify_result=s.verify("fun x -> x > 0 ==> f x > 0")instance_result=s.instance("fun x -> f x = 43")decomposition=s.decompose("f")print(verify_result)print(instance_result)forn,regioninenumerate(decomposition.regions):print("-"*10+" Region",n,"-"*10+"\nConstraints")forcinregion.constraints_pp:print("  ",c)print("Invariant:","\n",region.invariant_pp)Please note, while this simplifies usage, it can be less resource-efficient for repeated high-volume requests, as a new Imandra instance pod is started for each block (which may take a few seconds and be subject to availability if you're using community license). For more practical applications, manual management of your instances may be preferable:importimandras=imandra.session()print(s.verify("fun x -> x > 0 ==> f x > 0"))s.close()We recommend using Jypyter notebooks (Python kernel) with our APIs so you can instanciate a session in one cell and repeatedly perform computation in others.Please note: we enforce limits on the number of pods you can run simultaneously (per license level).(Advanced) Manually interacting with Imandra Core via HTTPThe package also includesimandra_http_api_client, an open-api generated API
client forimandra-http-api, the HTTP wrapper for Imandra itself, which can be
used for verifying Imandra models.Although developing models is best done via alocal Imandra Core
installation, if
you've already built a model and want to dynamically interrogate its logical
constitution using Imandra, Imandra Core's HTTP API can lend a hand by giving
you lightweight access to an Imandra Core instance, and returning responses in a
structured, machine-readable format over HTTP.See theimandra_http_api_clientAPI reference
documentationfor this module.If you have alocal Imandra Core
installation,
you can invokeimandra-http-apidirectly, and connect to it on localhost:# In a terminal$imandra-http-api# with imandra-http-api running locally on port 3000importimandra_http_api_clientconfig=imandra_http_api_client.Configuration(host='http://localhost:3000',)If you do not have a local Imandra Core installation, you can create an instance
ofimandra-http-apion our cloud (after authenticating via the CLI commandimandra auth login):importimandra.authimportimandra.instanceimportimandra_http_api_clientauth=imandra.auth.Auth()instance=imandra.instance.create(auth,None,"imandra-http-api")config=imandra_http_api_client.Configuration(host=instance['new_pod']['url'],access_token=instance['new_pod']['exchange_token'],)You can then use yourimandra_http_api_client.Configurationobject to make requests toimandra-http-api:withimandra_http_api_client.ApiClient(config)asapi_client:# Create an instance of the API classapi_instance=imandra_http_api_client.DefaultApi(api_client)eval_req={"src":"fun x -> List.rev (List.rev x) = x","syntax":"iml","hints":{"method":{"type":"auto"}}}req={"src":"fun x -> List.rev (List.rev x) = x","syntax":"iml","hints":{"method":{"type":"auto"}}}print("Request: ")print(req)verify_request_src=imandra_http_api_client.VerifyRequestSrc.from_dict(req)try:# Verify a string of source codeapi_response=api_instance.verify_by_src(verify_request_src)print("The response of DefaultApi->verify:\n")print(api_response)exceptApiExceptionase:print("Exception when calling DefaultApi->verify:%s\n"%e)Once you're done with your instance, terminate it with:imandra.instance.delete(auth, instance['new_pod']['id'])