Commit 27838fd1 authored by Michael Hanus 's avatar Michael Hanus
Browse files

Prelude.compare implemented as flexible on user-defined types

parent 4b941747
PAKCS: Release Notes
====================
Release notes for PAKCS Version 1.14.0 (October 12, 2015)
Release notes for PAKCS Version 1.14.0 (October 13, 2015)
---------------------------------------------------------
Changes to version 1.13.0:
* Type `Success` is now a type synonym for `Bool` and
`success` is defined as `True` in the prelude.
* Operation `Prelude.compare` and related comparison operations
are flexible (instead of rigid as before) on user-defined datatypes.
It still suspends when comparing two variables or
one variable with a number or character.
Release notes for PAKCS Version 1.13.1 (October 2, 2015)
......
......@@ -409,10 +409,17 @@ prim_compare(X,Y,R,E0,E) :-
user:hnf(Y,HY,E1,E2),
prim_compareHNF(HX,HY,R,E2,E).
?- block prim_compareHNF(-,?,?,?,?), prim_compareHNF(?,-,?,?,?),
?- block %prim_compareHNF(-,?,?,?,?), prim_compareHNF(?,-,?,?,?),
prim_compareHNF(?,?,?,-,?).
prim_compareHNF(X,Y,R,E0,E) :- var(X), var(Y), !,
when((nonvar(X);nonvar(Y)), prim_compareHNF(X,Y,R,E0,E)).
prim_compareHNF(X,Y,R,E0,E) :- var(X), !,
prim_compareHNF(Y,X,R0,E0,E1),
switchOrdering(R0,R), E1=E.
prim_compareHNF('FAIL'(Src),_,'FAIL'(Src),E,E) :- !.
prim_compareHNF(_,'FAIL'(Src),'FAIL'(Src),E,E) :- !.
prim_compareHNF(_,Y,R,E0,E) :- nonvar(Y), Y='FAIL'(_), !, R=Y, E0=E.
prim_compareHNF(X,Y,R,E0,E) :- var(Y), (number(X); isCharCons(X)), !,
when(nonvar(Y), prim_compareHNF(X,Y,R,E0,E)).
prim_compareHNF(X,Y,R,E0,E) :- number(X), !,
(X=Y -> R='Prelude.EQ' ; (X<Y -> R='Prelude.LT' ; R='Prelude.GT')),
E0=E.
......@@ -420,6 +427,15 @@ prim_compareHNF(X,Y,R,E0,E) :- isCharCons(X), !,
char_int(X,VX), char_int(Y,VY),
(VX=VY -> R='Prelude.EQ' ; (VX<VY -> R='Prelude.LT' ; R='Prelude.GT')),
E0=E.
prim_compareHNF(X,Y,R,E0,E) :- var(Y), !,
functor(X,FX,NX),
( functor(Y,FX,NX), prim_compareArgs(1,NX,X,Y,R,E0,E)
; user:constructortype(FX,_,NX,_,IX,_,OtherCons),
member(FY/NY,OtherCons),
user:constructortype(FY,_,NY,_,IY,_,_),
functor(Y,FY,NY),
(IX<IY -> R='Prelude.LT', E0=E ; (IX>IY -> R='Prelude.GT', E0=E))
).
prim_compareHNF(X,Y,R,E0,E) :-
functor(X,FX,NX), functor(Y,FY,NY),
user:constructortype(FX,_,NX,_,IX,_,_),
......@@ -435,6 +451,9 @@ prim_compareArgs(I,N,X,Y,R,E0,E) :-
(ArgR='Prelude.EQ' -> I1 is I+1, prim_compareArgs(I1,N,X,Y,R,E1,E)
; R=ArgR, E1=E).
switchOrdering('Prelude.LT','Prelude.GT') :- !.
switchOrdering('Prelude.GT','Prelude.LT') :- !.
switchOrdering(X,X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% encapsulated search not yet implemented in Curry2Prolog:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment