Discussions related to Visual Prolog
Martin Meyer
VIP Member
Posts: 298
Joined: 14 Nov 2002 0:01

### Bounded polymorphism problem

Hello Thomas,

please check this code in 32bit mode (in build 1002):

Code: Select all

``````domains
dictionary{Key} = leaf(Key).

class predicates
doNothing : (dictionary{Key} Dict) -> dictionary{Key} NewDict
where Key supports unsigned64.
clauses
doNothing(leaf(K)) = doNothing_1(K).

class predicates
doNothing_1 : (Key) -> dictionary{Key} Dict
where Key supports unsigned64.
clauses
doNothing_1(K) = leaf(K).

%======
clauses
run() :-
Dict_0 = leaf(0),
stdIo::write("1> ", Dict_0, "\n"),
Dict_1 = doNothing(Dict_0),
stdIo::write("2> ", Dict_1, "\n").``````
It outputs:

Code: Select all

``````1> leaf(0)
2> leaf(5396940)``````
Regards Martin
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

### Re: Bounded polymorphism problem

I see the problem too. We will investigate it.

By the way this is a simpler example:

Code: Select all

``````class predicates
doNothing : (dictionary{Key} Dict) -> dictionary{Key} NewDict
where Key supports unsigned64.
clauses
doNothing(leaf(K)) = leaf(K + 1).``````
(And more relevant because it actually uses the boundary for something).
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 298
Joined: 14 Nov 2002 0:01

### Re: Bounded polymorphism problem

The code below crashes in 64bit mode. The somewhat longer example is an excerpt from a radix set class for keys which support type positive. I suppose the problem stems from same origin as in the before 32bit example:

Code: Select all

``````    open math, core

domains
dictionary{Key} =
empty;
leaf(Key Key);
branch(unsigned Prefix, unsigned BranchingBit, dictionary{Key} Left, dictionary{Key} Right).

class predicates
insert : (dictionary{Key} Dictionary_0, Key Key) -> dictionary{Key} Dictionary
where Key supports positive.

class predicates
isMember : (Key Key, dictionary{Key} Dictionary) determ
where Key supports positive.

%---
class predicates
branchingBit : (unsigned PrefixA, unsigned BrBitA, unsigned PrefixB, unsigned BrBitB) -> unsigned BrBit.
clauses
branchingBit(Pa, Ba, Pb, Bb) = highestBit(Pa ^^ Pb, max(1, 2 * max(Ba, Bb))).

class predicates
matchPrefix : (unsigned KeyU, unsigned Prefix, unsigned BrBit) determ.
clauses
matchPrefix(K, P, B) :-

class predicates
clauses
mask(K, M) = (K ++ M - 1) ** ~~M.

class predicates
highestBit : (unsigned Prefix, unsigned BrBit) -> unsigned BrBit.
clauses
PN = P ** ~~(B - 1).

class predicates
clauses
getHighBitMask32(X) = X_6 ** ~~(X_6 >> 1) :-
X_1 = X ++ X >> 1,
X_2 = X_1 ++ X_1 >> 1,
X_3 = X_2 ++ X_2 >> 2,
X_4 = X_3 ++ X_3 >> 4,
X_5 = X_4 ++ X_4 >> 8,
X_6 = X_5 ++ X_5 >> 16.

class predicates
join : (unsigned Pr1, unsigned BrBit1, dictionary{Key} T1, unsigned Pr2, unsigned BrBit2, dictionary{Key} T2) -> dictionary{Key} T1_join_T2
where Key supports positive.
clauses
join(P1, B1, T1, P2, B2, T2) = Dict :-
BrBit = branchingBit(P1, B1, P2, B2),
Dict = if P1 ** BrBit = 0 then branch(mask(P1, BrBit), BrBit, T1, T2) else branch(mask(P1, BrBit), BrBit, T2, T1) end if.

clauses
insert(empty, K) = leaf(K).

insert(leaf(Ky), Kx) = Dict :-
KxPo = convert(positive, Kx),
KyPo = convert(positive, Ky),
if KyPo = KxPo then
Dict = leaf(Kx)
else
Dict = join(uncheckedConvert(unsigned, KxPo), 0, leaf(Kx), uncheckedConvert(unsigned, KyPo), 0, leaf(Ky))
end if.

insert(branch(Pr, BrBit, Left, Right), K) = Dict :-
KPo = convert(positive, K),
if matchPrefix(uncheckedConvert(unsigned, KPo), Pr, BrBit) then
if uncheckedConvert(unsigned, KPo) ** BrBit = 0 then
Dict = branch(Pr, BrBit, insert(Left, K), Right)
else
Dict = branch(Pr, BrBit, Left, insert(Right, K))
end if
else
Dict = join(uncheckedConvert(unsigned, KPo), 0, leaf(K), Pr, BrBit, branch(Pr, BrBit, Left, Right))
end if.

clauses
isMember(K, leaf(K)).

isMember(K, branch(P, _BrBit, Left, _Right)) :-
uncheckedConvert(unsigned, K) <= P,
!,
isMember(K, Left).

isMember(K, branch(_P, _BrBit, _Left, Right)) :-
isMember(K, Right).

%======
clauses
run() :-
Set0 = insert(empty, 0),
Set1 = insert(Set0, 1),
if isMember(2, Set1) then
stdIo::write("yes")
else
stdIo::write("no")
end if.``````
Regards Martin
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

### Re: Bounded polymorphism problem

Thank you, we will also look at that. It looks related, but the first problem is concerned with different representation of unsigned and unsigned64 in a functor on 32 bit. Such a difference is not involved here.
Regards Thomas Linder Puls
PDC