***
***	Tests for associative-identity unification.
***

set show timing off .

fmod AU-TEST is
  sort Foo .
  op 1 : -> Foo .
  op __ : Foo Foo -> Foo [assoc id: 1] .
  ops a b c : -> Foo .
vars A B C X Y Z : Foo .
endfm

*** one side goes to one variable
unify X =? Y X /\ Z Y =? A B .
variant unify X =? Y X /\ Z Y  =? A B .

*** one side goes to empty
unify X =? Y X /\  Z C =? C  /\ Z Y =? A B .
variant unify X =? Y X /\  Z C =? C  /\ Z Y =? A B .

*** both sides go to one variable
unify X =? Y X /\ A C =? C  /\ Z Y =? A B .
variant unify X =? Y X /\ A C =? C  /\ Z Y =? A B .

*** both side to to empty
unify X =? Y X /\ Y =? A B Z  /\ Z Y =? A B .
variant unify X =? Y X /\ Y =? A B Z  /\ Z Y =? A B .

*** AU problem completely nulls out
unify X =? 1 /\ Y =? 1 /\ Z =? 1 /\ X =? Y Z .
variant unify X =? 1 /\ Y =? 1 /\ Z =? 1 /\ X =? Y Z .
unify X =? 1 /\ Y =? 1 /\ Z =? 1 /\ X Y =? Y Z .
variant unify X =? 1 /\ Y =? 1 /\ Z =? 1 /\ X Y =? Y Z .

*** null equation
unify X =? Y X /\ 1 =? A B Z  /\ Z Y =? A B .
variant unify X =? Y X /\ 1 =? A B Z  /\ Z Y =? A B .
unify X =? Y X /\ C =? 1 /\ C =? A B Z  /\ Z Y =? A B .
variant unify X =? Y X /\ C =? 1 /\ C =? A B Z  /\ Z Y =? A B .

*** collapse
unify A B C =? a .
variant unify A B C =? a .
unify Y =? 1 /\ X =? Y Z .
variant unify Y =? 1 /\ X =? Y Z .

*** linear
unify A B =? X Y .
variant unify A B =? X Y .
unify A B =? X Y Z .
variant unify A B =? X Y Z .
unify A B C =? X Y Z .
variant unify A B C =? X Y Z .


*** order-sorted unification

fmod LIST is
  sorts Elt List .
  subsort Elt < List .
  op nil : -> List .
  op __ : List List -> List [assoc id: nil] .
vars L M N : List .
vars E F G : Elt .
endfm

unify E =? L M N .
variant unify E =? L M N .
unify E L E M =? E N F N .
variant unify E L E M =? E N F N .

*** cycle breaking

fmod NAT' is
  protecting BOOL .
  sorts Zero NzNat Nat .
  subsort Zero NzNat < Nat .
  op 0 : -> Zero .
  op s_ : Nat -> NzNat [iter] .
  op _*_ : NzNat NzNat -> NzNat [assoc id: s(0) prec 31] .
  op _*_ : Nat Nat -> Nat [ditto] .

  vars W X Y Z A B C D : Nat .
endfm

*** cycle breaking through S theory
unify X =? s (X * Y) .
unify X =? s X * Y .

*** theory conflict
unify s X =? s X * Y .
unify s X =? X * Y .

fmod COMM is
  sort Foo .
  ops a b c d : -> Foo .
  op f : Foo Foo -> Foo [assoc id: c(a, b)] .
  op c : Foo Foo -> Foo [comm] .
  vars W X Y Z A B C D : Foo .
endfm

*** cycle breaking through comm theory
unify X =? c(f(X, Y), Z) .

fmod FOO2 is
  sort Foo .
  ops a b c d : -> Foo .
  op f : Foo Foo -> Foo [assoc id: g(c, d)] .
  op g : Foo Foo -> Foo [assoc id: f(a, b)] .

  vars X Y : Foo .
endfm

*** cycle breaking with non-disjoint identities
unify X =? f(Y, a, b) /\ Y =? g(X, c, d) .

fmod FOO3 is
  sort Foo .
  ops a b : -> Foo .
  op h : Foo -> Foo .
  op f : Foo Foo -> Foo [assoc id: h(f(a, b))] .

  vars X Y : Foo .
endfm

*** cycle breaking with non-disjoint identity
unify X =? f(Y, a, b) /\ Y =? h(X) .

*** order-sorted mgus need not be unsorted mgus

fmod AU-UNSORTED is
  sort Foo .
  op 1 : -> Foo .
  op f : Foo Foo -> Foo [assoc id: 1] .
endfm

*** 1 mgu
unify X:Foo =? f(Y:Foo, Z:Foo) .
variant unify X:Foo =? f(Y:Foo, Z:Foo) .

fmod AU-SORTED1 is
  sorts Lo Hi .
  subsort Lo < Hi .
  op 1 : -> Hi .
  op f : Hi Hi -> Hi [assoc id: 1] .
endfm

*** unsorted mgu can't be sorted so collapse solutions becomes mgus
unify X:Lo =? f(Y:Hi, Z:Hi) .
variant unify X:Lo =? f(Y:Hi, Z:Hi) .

fmod AU-SORTED2 is
  sorts Lo Hi .
  subsort Lo < Hi .
  op 1 : -> Hi .
  op f : Hi Hi -> Hi [assoc id: 1] .
  op f : Lo Lo -> Lo [assoc id: 1] .
endfm

*** collapse solutions not subsumed by non-collapse solution after sorting 
unify X:Lo =? f(Y:Hi, Z:Hi) .
variant unify X:Lo =? f(Y:Hi, Z:Hi) .
