Dafny runtime for Python

4.11.0 · active · verified Thu Apr 16

The `dafnyruntimepython` library provides the necessary runtime support for Python code generated by the Dafny verifier-aware programming language. Dafny compiles its formally verified code into various target languages, including Python. This runtime library is automatically included as source when Dafny builds a Python target. The current version is 4.11.0, and the Dafny project maintains a regular release cadence, with minor versions often released monthly or bi-monthly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the typical workflow for using `dafnyruntimepython`: writing a Dafny program, compiling it to Python using the `dafny` CLI, and then executing the generated Python code. The `dafnyruntimepython` library is implicitly used by the generated Python files. Ensure the `dafny` CLI is installed and in your system's PATH to run this example.

import os
import subprocess

# 1. Create a dummy Dafny file
dafny_code = """
// MyDafnyProgram.dfy
module {:extern "MyDafnyModule"} MyModule {
  class C {
    static method SayHello() {
      print "Hello from Dafny!\n";
    }
  }
}
method Main() {
  MyModule.C.SayHello();
}
"""

with open("MyDafnyProgram.dfy", "w") as f:
    f.write(dafny_code)

print("Dafny source file created: MyDafnyProgram.dfy")

# 2. Compile Dafny to Python
# Requires the 'dafny' CLI tool to be installed and in PATH.
# This step generates a directory like MyDafnyProgram-py/
compile_cmd = ["dafny", "build", "--target:py", "MyDafnyProgram.dfy"]
print(f"Executing: {' '.join(compile_cmd)}")
try:
    subprocess.run(compile_cmd, check=True, capture_output=True, text=True)
    print("Dafny compiled successfully to Python.")
except subprocess.CalledProcessError as e:
    print(f"Dafny compilation failed:\n{e.stdout}\n{e.stderr}")
    exit(1)

# 3. Execute the generated Python code
# Add the generated directory to PYTHONPATH to allow imports
os.environ["PYTHONPATH"] = os.getcwd() + ":" + os.path.join(os.getcwd(), "MyDafnyProgram-py")

# The entry point for the generated Python code is typically in the top-level generated .py file.
# The name will be derived from the .dfy file (e.g., MyDafnyProgram.py in MyDafnyProgram-py/)
# For Dafny 4.x, the Main method is typically directly runnable or imported.
# Let's find the main generated file.

output_dir = "MyDafnyProgram-py"
if os.path.exists(output_dir):
    python_main_file = os.path.join(output_dir, "MyDafnyProgram.py")
    if os.path.exists(python_main_file):
        print(f"Executing generated Python: python3 {python_main_file}")
        try:
            run_cmd = ["python3", python_main_file]
            result = subprocess.run(run_cmd, check=True, capture_output=True, text=True)
            print("\n--- Generated Python Output ---")
            print(result.stdout)
            if result.stderr:
                print("--- Generated Python Errors ---")
                print(result.stderr)
            print("-------------------------------")
        except subprocess.CalledProcessError as e:
            print(f"Execution of generated Python failed:\n{e.stdout}\n{e.stderr}")
            exit(1)
    else:
        print(f"Error: Main Python file not found in {output_dir}")
        exit(1)
else:
    print(f"Error: Dafny output directory {output_dir} not found.")
    exit(1)

# Clean up generated files (optional)
# import shutil
# if os.path.exists("MyDafnyProgram.dfy"):
#     os.remove("MyDafnyProgram.dfy")
# if os.path.exists(output_dir):
#     shutil.rmtree(output_dir)

view raw JSON →