Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unification should schedule and normalize constraints #172

Open
BinderDavid opened this issue Mar 25, 2024 · 2 comments
Open

Unification should schedule and normalize constraints #172

BinderDavid opened this issue Mar 25, 2024 · 2 comments
Labels
enhancement New feature or request unification

Comments

@BinderDavid
Copy link
Collaborator

The following example should typecheck, but it doesn't:

data Bool { True, False }

data Nat { Z, S(n: Nat) }

def Bool.ite(a: Type, x y: a): a {
    True => x,
    False => y
}

-- We want to unify the telescope (True, Nat) with (x, x.ite(Type, Nat, Bool))

data T(x : Bool, a : Type) {
    C : T(True, Nat)
}

def T(x, x.ite(Type,Nat, Bool) : Type).foo(x : Bool) : Type {
    C => ?
}

The tricky bit is that we get the following constraint:

(True, Nat) = (x, x.ite(Type, Nat,Bool))

which can be decomposed into two constraints:

True = x
Nat = x.ite(Type,Nat,Bool)

which can be solved if done in the correct order and by interleaving normalization with unification.

@BinderDavid
Copy link
Collaborator Author

Current output with --trace option:

pol --trace check examples/counterexample.pol 
[] |- True =>
[] |- True =>
[] |- foo =>
[[]] |- Bool <= Type
[[]] |- Bool => ?
[[]] |- Bool => Type
Type =? Type
Type =? Type
[[]] |- Bool <= Type
[[]] |- Bool ▷ ?
[[]] |- Bool ▷ Bool
↓Bool ~> ?
↓Bool ~> Bool
[[[email protected]]] |- [email protected] ▷ ?
[[[email protected]]] |- [email protected][email protected]
[[[email protected]]] |- ([email protected](Type, Nat, Bool)):Type ▷ ?
[[[email protected]]] |- [email protected](Type, Nat, Bool) ▷ ?
[[[email protected]]] |- [email protected] ▷ ?
[[[email protected]]] |- [email protected][email protected]
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
[[[email protected]]] |- Nat ▷ ?
[[[email protected]]] |- Nat ▷ Nat
[[[email protected]]] |- Bool ▷ ?
[[[email protected]]] |- Bool ▷ Bool
[[[email protected]]] |- [email protected](Type, Nat, Bool) ▷ [email protected](Type, Nat, Bool)
[[[email protected]]] |- ([email protected](Type, Nat, Bool)):Type ▷ [email protected](Type, Nat, Bool)
↓[email protected] ~> ?
↓[email protected] ~> [email protected][email protected](Type, Nat, Bool) ~> ?
↓Type ~> ?
↓Type ~> Type
↓Nat ~> ?
↓Nat ~> Nat
↓Bool ~> ?
↓Bool ~> Bool
↓[email protected](Type, Nat, Bool) ~> [email protected](Type, Nat, Bool)
[[[email protected]]] |- T([email protected], ([email protected](Type, Nat, Bool)):Type) ▷ ?
[[[email protected]]] |- [email protected] ▷ ?
[[[email protected]]] |- [email protected][email protected]
[[[email protected]]] |- ([email protected](Type, Nat, Bool)):Type ▷ ?
[[[email protected]]] |- [email protected](Type, Nat, Bool) ▷ ?
[[[email protected]]] |- [email protected] ▷ ?
[[[email protected]]] |- [email protected][email protected]
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
[[[email protected]]] |- Nat ▷ ?
[[[email protected]]] |- Nat ▷ Nat
[[[email protected]]] |- Bool ▷ ?
[[[email protected]]] |- Bool ▷ Bool
[[[email protected]]] |- [email protected](Type, Nat, Bool) ▷ [email protected](Type, Nat, Bool)
[[[email protected]]] |- ([email protected](Type, Nat, Bool)):Type ▷ [email protected](Type, Nat, Bool)
[[[email protected]]] |- T([email protected], ([email protected](Type, Nat, Bool)):Type) ▷ T([email protected], [email protected](Type, Nat, Bool))
↓T([email protected], [email protected](Type, Nat, Bool)) ~> ?
↓[email protected] ~> ?
↓[email protected] ~> [email protected][email protected](Type, Nat, Bool) ~> ?
↓Type ~> ?
↓Type ~> Type
↓Nat ~> ?
↓Nat ~> Nat
↓Bool ~> ?
↓Bool ~> Bool
↓[email protected](Type, Nat, Bool) ~> [email protected](Type, Nat, Bool)
↓T([email protected], [email protected](Type, Nat, Bool)) ~> T([email protected], [email protected](Type, Nat, Bool))
[[[email protected]]] |- Bool ▷ ?
[[[email protected]]] |- Bool ▷ Bool
↓Bool ~> ?
↓Bool ~> Bool
[[Bool]] |- [email protected] <= Bool
[[Bool]] |- [email protected] => ?
[[Bool]] |- [email protected] => Bool
Bool =? Bool
Bool =? Bool
[[Bool]] |- [email protected] <= Bool
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- ([email protected](Type, Nat, Bool)):Type <= Type
[[Bool]] |- ([email protected](Type, Nat, Bool)):Type => ?
[[Bool]] |- Type <= Type
[[Bool]] |- Type => ?
[[Bool]] |- Type => Type
Type =? Type
Type =? Type
[[Bool]] |- Type <= Type
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- [email protected](Type, Nat, Bool) <= Type
[[Bool]] |- [email protected](Type, Nat, Bool) => ?
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- Type <= Type
[[Bool]] |- Type => ?
[[Bool]] |- Type => Type
Type =? Type
Type =? Type
[[Bool]] |- Type <= Type
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- Nat <= Type
[[Bool]] |- Nat => ?
[[Bool]] |- Nat => Type
Type =? Type
Type =? Type
[[Bool]] |- Nat <= Type
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- Bool <= Type
[[Bool]] |- Bool => ?
[[Bool]] |- Bool => Type
Type =? Type
Type =? Type
[[Bool]] |- Bool <= Type
[[[email protected]]] |- Bool ▷ ?
[[[email protected]]] |- Bool ▷ Bool
↓Bool ~> ?
↓Bool ~> Bool
[[Bool]] |- [email protected] <= Bool
[[Bool]] |- [email protected] => ?
[[Bool]] |- [email protected] => Bool
Bool =? Bool
Bool =? Bool
[[Bool]] |- [email protected] <= Bool
[[[email protected]]] |- Type ▷ ?
[[[email protected]]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[Bool]] |- [email protected](Type, Nat, Bool) => Type
Type =? Type
Type =? Type
[[Bool]] |- [email protected](Type, Nat, Bool) <= Type
[[Bool]] |- ([email protected](Type, Nat, Bool)):Type => Type
Type =? Type
Type =? Type
[[Bool]] |- ([email protected](Type, Nat, Bool)):Type <= Type
[[Bool],[T([email protected], [email protected](Type, Nat, Bool))]] |- Type => ?
[[Bool],[T([email protected], [email protected](Type, Nat, Bool))]] |- Type => Type
[[[email protected]],[@0.0]] |- Type ▷ ?
[[[email protected]],[@0.0]] |- Type ▷ Type
↓Type ~> ?
↓Type ~> Type
[[]] |- True ▷ ?
[[]] |- True ▷ True
[[]] |- Nat ▷ ?
[[]] |- Nat ▷ Nat
↓True ~> ?
↓True ~> True
↓Nat ~> ?
↓Nat ~> Nat
[[Bool]] |- C => ? <= Type
[[Bool]] |- C => ? <= Type
Error: U-003

  × Cannot automatically decide whether Nat and x.ite(Type, Nat, Bool) unify
    ╭─[examples/counterexample.pol:12:1]
 12 │ data T(x : Bool, a : Type) {
 13 │     C : T(True, Nat)
    ·                 ───
 14 │ }
 15 │ 
 16 │ def T(x, x.ite(Type,Nat, Bool) : Type).foo(x : Bool) : Type {
    ·          ─────────────────────
 17 │     C => ?
    ╰────

@BinderDavid BinderDavid added the enhancement New feature or request label Mar 25, 2024
@BinderDavid
Copy link
Collaborator Author

Requires #170 to be implemented first. From the error message it is also clear that the substitution is only recorded in the subst data structure, but not applied to the remaining constraints: The error message would say "Cannot automatically decide whether Nat and True.ite(Type, Nat, Bool) unify" otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unification
Projects
None yet
Development

No branches or pull requests

2 participants