Skip to content

Better documentation of --lossy-unification #6286

Closed

Description

@andreasabel

Better documentation of --lossy-unification requested by @MatthiasHu.

This is an example where --lossy-unification loses a solution that is found without this flag:

{-# OPTIONS --lossy-unification #-}

open import Agda.Builtin.Bool
open import Agda.Builtin.Nat
open import Agda.Builtin.Equality

-- Non-injective functions
mutual
  even : Nat → Bool
  even zero = true
  even (suc n) = odd n

  odd : Nat → Bool
  odd zero = false
  odd (suc n) = even n

mutual

  X = _
  Y = _

  -- lossy unification sets X=Y
  _ : even X ≡ even Y
  _ = refl

  _ : X ≡ 2 + Y
  _ = refl

  -- With --lossy-unification this fails:
  -- 2 != 4 of type Nat
  -- when checking that the expression refl has type X ≡ 2 + Y

  _ : X ≡ 2
  _ = refl

TODO: an example where --lossy-unification is necessary for success.

Activity

  1. added
    constraints
    ux: documentation
    on Nov 4, 2022
  2. jespercockx commented on Nov 4, 2022

    @jespercockx
    Member

    How about this example?

    {-# OPTIONS --lossy-unification #-}
    
    open import Agda.Builtin.Bool
    open import Agda.Builtin.Equality
    
    f : Bool → Bool
    f true = true
    f false = true
    
    test : f false ≡ f _
    test = refl
  3. andreasabel commented on Nov 4, 2022

    @andreasabel
    MemberAuthor

    Yes. I saw that my example can be rewritten to prove both points:

    mutual
      X = _
      Y = _
    
      -- lossy unification sets X=Y
      _ : even X ≡ even Y
      _ = refl
    
      _ : X ≡ 2
      _ = refl
    
      -- With --lossy-unification, all variables are solved and the following equation triggers an error.
      -- Without --lossy-unification, Y is still unsolved and the following equation is needed to solve it.
      _ : X ≡ 2 + Y
      _ = refl
  4. nad commented on Nov 4, 2022

    @nad
    Collaborator

    I don't think we should encourage use of this option. As I wrote elsewhere:

    Note that Agda is not really designed for the use of lossy unification. In "A comprehensible guide to a new unifier for CIC including universe polymorphism and overloading" Ziliani and Sozeau argue for a form of lossy, first-order unification. They present an example with multiple solutions, and claim that the first-order solution is the natural, expected one. However, they also deliberately do not include constraint postponement, in favour of a more predictable unification algorithm.

    With non-lossy unification you have the guarantee that, if the code type-checks, and you can find one consistent way to instantiate all meta-variables, then that is the way that the code is interpreted by the system (in the absence of bugs). With lossy unification the solution you have in mind might not be the one the system uses. If the unification algorithm is very simple and predictable, then that is perhaps fine, but Agda's unification algorithm is arguably rather complicated.

    Something along these lines could perhaps be integrated into the documentation.

  5. nad commented on Nov 28, 2022

    @nad
    Collaborator

    I plan to add something like this to the 2.6.3 changelog (see #6365).

  6. added this to the 2.6.3 milestone on May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    constraintsux: documentation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions