Markus de Medeiros
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Coupling Algorithm ### Problem: We describe the state of the borrow checker with a graph, whose vertices are capabilities, whose edges `X -> Y` describe how to exchange capabilities `X` for capabilities `Y` either abstractly (with a magic wand) or concretely (with flow-dependent instructions for the PCS stored in a _reborrowing DAG_ at each point). At a join point, we will have two graphs. What is the graph which describes the combined state afterwards? - The combined graph must still be able to decide whether any capability is unambiguously usable, ie. in the PCS. - The combined graph must still be able to explain how it exchanges capabilities. ### Idea 1: Store both graphs, under a flag This idea works for simple join points, but I'm not aware of any strategy to reach a fixed point at loop heads. This was the original idea for the reborroing DAG. ### Idea 2: Take the union of both graphs This is what Polonius does, since the edges in Polonius' subset graph are fully abstract, and it's edges are not linear. For example, suppose one subset graph contains `'a <: 'b` and the other contains `'a <: 'c`, then their union contains both subsets, both of which are eliminated when `'a` expires. We might hope to represent an edge in the subset graph as either a magic wand, or a subgraph of some concrete reborrowing DAG. In a naive implementation, this means we would have to apply wands `a -* b` and `a -* c` at the same time-- this will not work. For one we cannot consume `a` twice. For another, applying either wand implies we reobtain `b` and `c` in the PCS, which may not be the case as these capabilities may still blocked by some other lifetimes. It is also not immediate in this interpretation which subsets should become magic wands (subsets are transitvely closed, and the subset whose wand we might want to apply depends on the expiry order), and in this approach subset cycles are meaningless. ### Idea 2.5: Flow-dependent edges The reason idea 2 loses linearity at join points is because the union loses information about control flow. One possibility for avoing this is to tracking path conditions on every edge. However, in this approach it is more challenging to understand what "expiring an edge" means for the PCS, and the concrete annotations which explain it. We have a choice about how to intrepret expiring a conditional magic wand: 1. The conditional magic wands are applied eagerly, consuming and producing path-dependent capabilities in the PCS. Now the PCS is path-dependent, and must solve path conditions to determine if a capability is usable. 2. The conditional magic wands are applied lazily, only after all path conditions on their output are met. Now the capabilities in the PCS are not path-dependent, but graph must remember which edges are lazily expired, and solve path conditions to determine when to apply them. The current PCS implementation for Prusti plans to follow strategy 2, using future features of Viper to do the accounting about lazily applied edges. This is completely sufficient to come up with a core proof in Viper, but it means we cannot fully explain the PCS at an earlier stage. ### Idea 3: Coupling Coupling is very similar to strategy 2.5, however it uses the fact that Polonius does _not_ store flow dependent constraints to figure out when to apply the lazily expired wands (resp. subgraphs) without solving path conditions. Concretely, at each point the coupling graph will instruct a reborrowing DAG to apply some subgraph of edges at each point. Each edge (in the DAG) can still be under an arbitratily precise path condition, and that won't change the point in the program where that edge is applies. The key is the _coupling algorithm_: given two graphs like described in idea 2.5, the coupling algorithm will find collections of lifetimes `A` and `B` where every lifetime in `A` must expire before any lifetime in `B` is used. By grouping all of the edges in `A` together (from the graph of strategy 2.5), we know that it is sound to apply all annotations exactly when the last lifetime in `A` expires. In my view, the coupling graph is the most precise common approximation of the two input graphs that still respects the expiry rules of Polonius. #### Maximal Sharing/The Reborrowing DAG Coupling could in principle not be computed at simple join points and instead only at loop heads. However, computing coupling everywhere allows for a _maximally shared_ representation of the reborrowing DAG. In the past, we discussed how we might want to represent joins of reborrowing DAGs (as per idea 1) without duplicating equal stems. In the [feature-complete coupling](https://hackmd.io/@be6mqvt0QwS_i18WLiSLBA/rkuyv9Dd3/edit) document I explain how we can achieve this with the coupling graph, as well as sharing further up the reborrowing DAG as well. #### Legal Expiries By "expire" in the coupling graph, I mean removing an edge from the coupling graph and exchanging some capabilities in the PCS. By "expire" in the reborrowing DAG, I mean removing some set of edges in the reborrowing DAG, and interpreting their removal as inserting some instructions (stored on the edges) into the program. The two have to remain in sync: the explanation given by the reborrowing DAG should match the change in the PCS given by the coupling graphs. This also means that we will never ask the reborrowing DAG to expire edges finer than a single origin: to that end, all edges in the reborrowing DAG must be labelled with the coupling graph lifetime (or coupled edge identifier, as we will see later) they are waiting on. An expiry of a set of lifetimes in the coupling graph is legal if there is some sequence of edges in the reborrowing DAG, starting at the leaves under arbitrary path conditions, which contains all edges associtated to a lifetime in the sert. #### Coupling Graphs A coupling graph is similar the graphs in idea 2.5. Here I will explain the simple version of the graph which only supports non-iterated mutable borrows and no extra lifetime constraints (function calls, borrows in structs). Like idea 2.5, each vertex in the coupling graph is a lifetime, which for this subset of Rust also corresponds to an exclusive resource. Each hyperedge in the coupling graph corresponds to a subgraph of the reborrowing DAG, whose start and end vertices match the start and end vertices in the coupling graph. The coupling and reborrowing graphs must remain in sync. The interface between the reborrowing DAG and coupling graph is regulated by *introduction*, *elimination*, and *coupling* instructions. First, the analysis which calculates the coupling graph will emit a sequence of these instructions at each program point. Next, the analysis which calculates the reborrowing DAG will interpret the MIR at each program point using these instructions (the reborrowing DAG may be tuned to match the kind of information needed by the backed: for example by changing the specific repacking strategy, or with extra instructions to preserve values for purification, or by abstracting at fewer or more join points). Example introduction instructions include *issue a borrow* or *move a place*. Example elimination instructions include *consume a place from the PCS* or *apply the subgraph associated to lifeimte 'a*. We will discuss coupling instructions later. All vertices in the coupling graph are *live* if their lifetime is live, or *tagged* with the location where there lifetime died. When a lifetime is live, it's resource corresponds to a resource that is either currently in the PCS, or which could return to the PCS when unblocked. The resources in the coupling graph which are live and unblocked are also in the PCS. When a lifetime is tagged, it's corresponding resource is also tagged in the reborrowing DAG, and it no longer corresponds to a resource that could return to the PCS. The coupling graph includes a special kind of hyperedge, called a *coupled edge*. A coupled edge has an *identifier*, and there may be many coupled edges with the same identifier in the graph. Each coupled edge with the same identifier has the same set of destination vertices. Each coupled edge identifier corresponds to a sequence of _coupling instructions_. Coupling instructions include any elimination instruction, `freeze` and `unfreeze` instructions, and can depend on the control flow of the join point where they were created. Finally, coupled edges must be able to be reified in the reborrowing DAG. The reborrowing DAG chooses to represent each coupled edge identifier as either *translucent* or *opaque*. If *translucent*, it does not have to make any change aside from marking that the subgraph corresponding to the coupled edge now depends on the coupled edge identifier, rather than the lifetimes the depended upon before. If an identifier is chosen to be opaque *opaque*, in which case, it must package all of the annotations of the respective subgraph into a single magic wand. #### The Coupling Algorithm The algorithm is a greedy traversal of the two graphs. Let G1 and G2 be two coupling graphs. 1. Find the sets of leaves `L1` and `L2` for both graphs. 2. By Polonius liveness, there must exist a vertex which is either a root or a live origin, and which is reachable from `L1` in `G1` and `L2` in `G2` by at most one live hyperedge. Find some candidate for this vertex, call it `r`. 3. On each branch, combine the hyperpath from `Li` to `r` as you would Hoare triples. This yields a single Hoare triple whose precondition is contained in `Li` and postcondition contains `r`. Denote the sequence of edges (lifetimes) on this hyperpath as `e_(i, 1) ... e_(i, m_i)` and let `sig_i = { pre(i) } expire(e_i, 1) ... expire(e_(i, m_i)) { post(i) }`. 4. By Polonius rules, any invalidation of `r` means any lifetime blocking `r` in any branch is expired. Let `L'` be the union of all `Li`. In each branch, either unfreeze any elements `s` of `L'` that are not in `Li` if they are frozen (prepending `unfreeze(s)` to `sig_i`, exchanging `|s|` for `s` in `Li`) or by expiring `s` up to a live origin (prepending the sequence of expiries to `sig_i`, and updating ) 5. Now all branches definitely consume the same set of resources, and definitely all emit `r`. Each branch may emit other resources `s` as well, for each of these append a `freeze(s)` instruction to `sig_i` and replace `s` with `|s|` in `Li` 6. Emit a new coupled edge identifier `c`. The coupling instructions are is `if (branch = 1) then sig_1 ... `. For each branch, add a coupled edge with identifier `c`, whose source and destination vertices are the unfrozen part of `sig_i`. The unfrozen vertices are the same in all branches, so this is well-defined. 7. For each source vertex `v`, add a coupled edge from `v` to all destination vertices, with identifier c. 8. Remove all edges in every branch which has been coupled, updating the leaf set `Li`. Repeat until all graphs have been traversed. See the coupling examples in [feature complete coupling](https://hackmd.io/@be6mqvt0QwS_i18WLiSLBA/rkuyv9Dd3/edit). The flow-dependent change in capabilities are given by examining the "frozen" capabilities in each `Li`: frozen access to a place in `Li` means that we have access to the place in branch `i` but not all branches. This information can be removed, if a backend (like Prusti) can remember these simple constraints itself. For backends that do not store a version of the program state, freezes and unfreezes might be necessary to remember where information came from.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully