Niko Matsakis
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # chalk design meeting 2019-12-16 normalization ## The problem See [chalk#234] for some details. Essentially, the way Chalk handles projections can lead to ambiguity, because there is both a "placeholder type" `(Trait::Item)<T>` as well as the normalized type `<T as Trait>::Item -> U` (where `U` is the normalized type). This leads to two separate answers that are "equal", but the aggregator treats them as unequal currently, so the final answer is ambiguous. [chalk#234]: https://github.com/rust-lang/chalk/issues/234 ## Thoughts on potential solutions * **Improve the uniqueness checker** Essentially, don't modify the engine or solver at all with regards to the existing answers. Instead, modify the aggregator to check if the two types normalize to each other. [source] * **Don't search all options** Here, instead of having two answers (placeholdero and normalized type), we have some way to try the normalized type *first*; then, if that fails, use the placeholder type. [source] * **Using a different approach to normalization** There has been [previous work](https://www.microsoft.com/en-us/research/publication/type-checking-with-open-type-functions/) that applies normalization in Haskell. [source] * **Remove unnormalized types**. This is somewhat of a stepping stone. The idea here is to "desugar" projection types into explicit goals when lowering, so there is no longer "unnormalized projections", only normalized projections and placeholder types. [source 2] * **Merge placeholder and projection types** This is somewhat different to the previous thought. Here, our idea of "unnormalized projections" is instead replaced by "normalized projections to placeholders" where placeholders can act like inference variables. * **Only add the placeholder clause when no impls exist** This extends upon the second "Don't search all options" thought. Currently, the placeholder clause is added separately to the normalized values (from impls). Perhaps we could instead only add the placeholder clauses if no impls exist. This has implications for well-formed goals, since they rely on the placeholder type right now. [source]: https://github.com/rust-lang/chalk/issues/234#issuecomment-528109851 [source 2]: https://github.com/rust-lang/chalk/issues/234#issuecomment-530823193 ## Cases to consider ### Simple case See [chalk#234]. ``` program { trait Trait1 { type Type; } trait Trait2<T> { } impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {} struct u32 {} struct S {} impl Trait1 for S { type Type = u32; } } goal { exists<U> { S: Trait2<U> } } yields { "Unique" } ``` Here, there really is only one solution: `U = u32` (because `<S as Trait1>::Type = u32`). However, because of the placeholder type `(Trait1::Type)<S>`, the actual answer returned is ambiguous. ### With inference variables ``` program { trait Trait1 { type Type; } trait Trait2<T> { } impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {} struct u32 {} struct S {} impl Trait1 for S { type Type = u32; } struct i32 {} struct X {} impl Trait1 for X { type Type = i32; } } goal { exists<T, U> { T: Trait2<U> } } first 3 with max 10 { ... } ``` Here, the *best* answer we could give would be `?0 = ^0, ?1 = <?0 as Trait1>::Type`. But, maybe also "ambiguous" is okay here, because `?0 = S, ?1 = u32` and `?0 = X, ?1 = i32` are both *distinct* answers. ### Mapping If the projection type to be normalized contains inference variables, what do we use to figure out their values? Here is one simple case: ```rust trait Id { type Output; } impl Id for i32 { type Output = i32; } impl Id for u32 { type Output = u32; } ``` Given `ProjectionEq(<?X as Id>::Output = i32)`, can we infer that `?X = i32`? rustc cannot, but chalk can. ## Other relevant questions * **Do we need both `ProjectionEq` and `Normalize`?** Relevant for conditionally adding placeholder type if no impls. * **What benefit do we get by having a separate projection type and associated type?** ## Potentially related problems * **[rust analyzer infinite loop](https://github.com/rust-lang/chalk/issues/280)** The core problem with this was an inference variable was getting normalized to a projection with an inference variable, which got truncated and a cycle happened. The [accepted solution](https://github.com/rust-lang/chalk/pull/294) didn't necessarily fix the "root" cause. A [proposed solution](https://github.com/rust-lang/chalk/pull/305) touched projections irt. unification with inference variables. ## Previous work or progress ### Some work towards thoughts 5/6 [Branch 1](https://github.com/jackh726/chalk/commits/associated_types) [Branch 2](https://github.com/jackh726/chalk/commits/associated_types2) [Branch 3](https://github.com/jackh726/chalk/tree/associated_types3) The core approach with these approaches revolve around *not* storing the answer to a `ProjectionEq`/`Normalize` as a simple type, but instead as a "normalized projection". E.g. from the simple case about, the answer would be `<S as Trait1>::Type as u32`. If there are no impls it would instead be `<S as Trait1>::Type as (Trait1::Type)<?0>` or just `<S as Trait1>::Type as ?0`. Some thoughts from this: * *Mostly* there. Each branch has a slightly different approach. *Probably* don't need both `ProjectionEq` and `Normalize`. *Probably* don't need both the projection type and placeholder type. * Need to think about how well-formed goals interact with projection.

    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