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

List type conversion problem

Unread post by Martin Meyer »

Hello once more Thomas,

please also have a look at the following. In below mySubObject* is a subtype of myObjectList (I suppose otherwise the call test(SubObjectList) would not compile):

Code: Select all

interface myObject end interface myObject   %---   class myObject : myObject end class myObject   %---   implement myObject end implement myObject   %===   interface mySubObject     supports myObject end interface mySubObject   %---   class mySubObject : mySubObject end class mySubObject   %---   implement mySubObject     inherits myObject end implement mySubObject   %===   implement main     open core   domains     myObjectList = myObject*.   class predicates     test : (myObjectList). clauses     test(_).   clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = [SubObject],         test(SubObjectList).   end implement main
But when now introducing a named type for mySubObject*, then the compiler (build 7501) throws error c504 : The expression has type 'mySubObject*', which is incompatible with the type 'myObject*':

Code: Select all

interface myObject end interface myObject   %---   class myObject : myObject end class myObject   %---   implement myObject end implement myObject   %===   interface mySubObject     supports myObject end interface mySubObject   %---   class mySubObject : mySubObject end class mySubObject   %---   implement mySubObject     inherits myObject end implement mySubObject   %===   implement main     open core   domains     myObjectList = myObject*.     mySubObjectList = mySubObject*. %added line   class predicates     test : (myObjectList). clauses     test(_).   clauses     run() :-         SubObject = mySubObject::new(),         hasDomain(mySubObjectList, SubObjectList), %added line         SubObjectList = [SubObject],         test(SubObjectList).   end implement main
Nevertheless conversion from mySubObject* to myObjectList has worked out in the initial code above, same conversion via build-in convert rises problems. The compiler throws error c504 : The expression has type 'A$*', which is incompatible with the type 'myObject*' on that:

Code: Select all

interface myObject end interface myObject   %---   class myObject : myObject end class myObject   %---   implement myObject end implement myObject   %===   interface mySubObject     supports myObject end interface mySubObject   %---   class mySubObject : mySubObject end class mySubObject   %---   implement mySubObject     inherits myObject end implement mySubObject   %===   implement main     open core   domains     myObjectList = myObject*.   clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = [SubObject],         _ = convert(myObjectList, SubObjectList).   end implement main
Many regards
Martin
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

In future versions of Visual Prolog you will be right, but currently you are wrong.

The current and previous versions of Visual Prolog does not have "structural subtyping". In short this means that if A is a subtype of B it does not imply that A* is a subtype of B*.

You conclusion on this code is wrong:

Code: Select all

clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = [SubObject],         test(SubObjectList).
The correct conclusion corresponds to this code:

Code: Select all

clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = [convert(myObject, SubObject)],         test(SubObjectList).
Whereas this conclusion is invalid:

Code: Select all

clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = convert(list{myObject}, [SubObject]),         test(SubObjectList).
I.e. the object is automatically converted to a super type, but the list type must match.
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Unread post by Martin Meyer »

Ah, I understand. Thanx Thomas for the explanation!

Best regards
Martin
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Unread post by Martin Meyer »

But one question did not give me peace over the issue: When current VIP version does not have structural subtyping, why has my initial code piece compiled then?

I've found the answer by inspecting variable SubObjectList in the debugger: To my surprise SubObjectList has type myObject*, but not type mySubObject* as I had presumed.

So it is, that the compiler has seen the usage of SubObjectList in the call test(SubObjectList) and cleverly has given SubObjectList type myObject*. Consequently actually nothing needed to be converted in the call test(SubObjectList) - and that's why the code has compiled.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Yes, that was what I tried to explain with this piece of code:

Code: Select all

clauses     run() :-         SubObject = mySubObject::new(),         SubObjectList = [convert(myObject, SubObject)],         test(SubObjectList).
But I can emphasise it further like this:

Code: Select all

clauses     run() :-         SubObject = mySubObject::new(),         MyObject = convert(myObject, SubObject),         MyObjectList = [MyObject],         test(MyObjectList).
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Unread post by Martin Meyer »

Yes, now I see it. Your code explains the same what I formulated in words. Thank you again for being patient with me and explaining everything so well!

Many regards
Martin
Post Reply