Dafny Documentation

repository·master·Indexed 25 days ago

https://github.com/dafny-lang/dafny

Dafny is a verification-ready programming language designed to provide mathematical assurance that code meets its specifications. It supports inductive and lazily unbounded datatypes, subset types, and a mathematical proof toolbox including quantifiers and calculational proofs. Dafny compiles to multiple target languages, including C#, Go, Python, Java, and JavaScript. The documentation covers the Reference Manual, installation via VS Code or CLI, and specialized tools like AutoExtern for generating Dafny traits from C# code and the DafnyBenchmarkingPlugin for Java performance measurement.

Tokens
195.2K
Snippets
538
Records
1.1K
Agent score
83%

What's inside Dafny

  1. Overview of XUnitExtensions

    master

    The XUnitExtensions package provides specialized extensions for the xUnit testing framework, specifically designed to handle complex test scenarios that standard xUnit features do not support natively. It offers two primary capabilities:

    1. File-encoded Parameterized Theory Tests: Enables parameterized Theory tests where each test case is represented by an individual file. This approach allows for parallel execution of test cases within a single theory and supports sharding test cases across multiple machines.
    2. LIT Test Interpreter: Provides an alternate interpreter for LLVM Integrated Tester (LIT) tests, allowing test suites written in LIT syntax to be executed as xUnit Theory tests.
  2. Overview of Dafny language and tool

    master

    Dafny is an imperative, sequential programming language designed for static verification. It supports generic classes, inheritance, abstraction, methods, functions, dynamic allocation, and inductive/coinductive datatypes.

    Key verification features include:

    • Specifications: Pre-conditions, post-conditions, frame specifications (read/write sets), and termination metrics.
    • Ghost Constructs: Ghost variables and specification constructs used only during verification; these are omitted from the final executable code.
    • Verification Engine: Powered by Boogie and Z3.
    • Compilation Backends: The dafny compiler can produce code for .NET (via C#), Java, JavaScript, Go, and C++, each providing a Foreign Function Interface (via :extern) and a runtime library.
  3. Overview of Dafny language features

    master

    Dafny is a verification-ready programming language that allows you to write both code and specifications in the same language. It supports:

    Programming Concepts

    • Classes and Trait Inheritance
    • Inductive Datatypes: Suitable for pattern matching and containing methods.
    • Lazily Unbounded Datatypes (Co-inductive datatypes).
    • Subset Types: e.g., for bounded integers.
    • Lambdas
    • Data Structures: Both immutable and mutable.

    Mathematical Proof Toolbox

    • Quantifiers: Unbounded and bounded.
    • Calculational Proofs
    • Specification Clauses: Pre-conditions, post-conditions, termination conditions, loop invariants, and read/write specifications.

    Compilation Targets

    Compiled code can be integrated into existing workflows by targeting:

    • C#
    • Go
    • Python
    • Java
    • JavaScript
  4. Use the Wrappers module for optional and error-handling types

    master

    The Std.Wrappers module provides simple datatypes to support common patterns like optional values or operations that can fail. These types are designed to work seamlessly with Dafny's update-with-failure :- operator.

    To use these types, import the module:

    import opened Std.Wrappers

    Supported failure-compatible (FC) types:

    • Option<R>: Either Some with a value of type R or None.
    • Outcome<E>: Either Pass (no information) or Fail with an error value of type E.
    • Result<R,E>: Either Success with a value of type R or Failure with an error value of type E.
  5. Understand Dafny's Lexical Structure and Grammar

    master
    Dafny uses an attributed extended BNF grammar. The process involves a scanner that tokenizes textual input into a sequence of tokens, which a parser then consumes to produce an Abstract Syntax Tree (AST). The grammar is implemented using the Coco/R lexer and parser generator. In the source tree, the grammar definition is located in the Dafny.atg file.
  6. Access Dafny user-facing documentation

    master
    The official, user-facing documentation for Dafny is hosted at https://dafny.org. This site serves as the primary entry point for all guides, tutorials, and API references. Documentation is organized by version, with latest pointing to the most recent stable version and dev pointing to the current development documentation.
  7. Understand the kinds of types in Dafny

    master

    Dafny types are sets of values or heap data-structures with allowed operations. They are categorized into two main groups based on how they are stored:

    Value Types

    Values that do not reside in the program heap and are passed by value. They do not occupy memory in a way that requires framing expressions. Examples include:

    • Basic scalar types: bool, char, int, real, ORDINAL, and bitvector types.
    • Built-in collection types: set, iset, multiset, seq, string, map, and imap.
    • Other types: Tuple types, inductive and coinductive types, function (arrow) types, and subset/newtypes based on value types.
    • Note: nat is a pre-defined subset type of int.

    Reference Types

    Represent references to objects allocated dynamically in the program heap. Accessing members requires dereferencing the reference. Examples include:

    • Class types
    • Traits
    • Array types

    Reference types can be nullable (can contain the null value) or non-null.

  8. Use TestDafny for cross-backend testing

    master

    TestDafny (also known as %testdafny) is an internal testing utility designed to execute a single Dafny source file against multiple compile target options. It asserts that the output matches the corresponding <test file>.expect file.

    This utility is useful for ensuring consistency across different backends and is used to generate the compiler feature support matrix in the Dafny reference manual.

  9. Understand Dafny specification clauses

    master

    Specifications in Dafny describe the logical properties of methods, functions, lambdas, iterators, and loops. They are used to define preconditions, postconditions, invariants, memory access permissions (read/modify), and termination information.

    Specifications are implemented using specification clauses. These clauses:

    • Typically appear in a sequence.
    • Begin with a specific keyword.
    • Do not end with semicolons.

    Specifications are applied at different levels:

    1. Specification Clauses: The individual building blocks (e.g., RequiresClause).
    2. Entity Specifications: A sequence of clauses applied to an entity (e.g., MethodSpec).
    3. Entity Declarations: The top-level declaration that includes the specifications (e.g., MethodDecl).
  10. Understand Dafny expression types and side-effects

    master

    Dafny expressions are categorized into three types based on their behavior and allowed usage:

    1. Standard Expressions: Most expressions have no side-effects and can be used in methods, functions, specifications, and either compiled or ghost code.
    2. Right-hand-side (RHS) Expressions: These have side-effects (e.g., object allocation, method calls) and are restricted to specific locations, typically the right-hand side of update (assignment) statements. Note that while method calls and function calls are syntactically similar, method calls are semantically restricted to RHS locations.
    3. Specification Expressions: These are restricted exclusively to specifications and other ghost code.
  11. Understand the Dafny Plugin Architecture

    master

    Dafny provides a plugin architecture that allows you to build tools (analyzers, AST modifiers, or compilers) without re-implementing parsing or name/type resolution.

    Warning: The plugin API is experimental and exposes the Dafny AST, which is subject to change. Always recompile your plugin against the specific version of the Dafny binary that will import it.

    Plugins are libraries linked to a Dafny.dll of the same version as the Language Server. A plugin typically consists of:

    • Configuration Class: Extends Microsoft.Dafny.Plugins.PluginConfiguration. It handles arguments via ParseArguments and provides lists of Rewriters, Compilers, or DafnyCodeActionProviders.
    • Rewriters: Extend Microsoft.Dafny.Plugins.Rewriter. Used to modify the AST or report diagnostics.
    • Compilers: Extend Microsoft.Dafny.Plugins.Compiler.
    • Language Server Providers: If your configuration extends Microsoft.Dafny.LanguageServer.Plugins.PluginConfiguration, you can provide DafnyCodeActionProviders or modify LanguageServerOptions via WithPluginHandlers() to add custom LSP request handlers.