Migrating from OO

35 views
Skip to first unread message

Ely Matos

unread,
Oct 5, 2024, 2:11:18 PM10/5/24
to ErgoAI and XSB Users Forum
Hi,
my name is Ely Matos. I'm new in the group :-)
I've found ergoAI recently and I'm trying to migrating a small KB from a OO oriented system to F-logic (on which I'm new too :-)
At first I thought it would be possible to express directly the OO Classes in F-Logic, but I'm in trouble about how to represent methods that access the self object. For example, if I have:

Set[|
    members->{},
|].

set1:Set[members->{K,L}].
set1[members->{M}].
set2:Set[members->{A,B,C}].

How to represent a "method" count, that returns the number of member?

Set[|
    members->{},
    count -> ?????
|].

I know I can have it in a query: 

?x=count{?c | set1[members->?c]}.

But It is possible to have it as a method?

?- set1[count->?x].

Maybe the broad question is: is it possible to a object access data about itself?

Thanks for attention,
Ely











Michael Kifer

unread,
Oct 5, 2024, 4:21:16 PM10/5/24
to ErgoAI-X...@coherentknowledge.com

hi Ely,


The easiest way to write such a method is shown below.

I realize that this might not be exactly how OO languages like Java do it but it is simple and effective.

Chapter 22 of the Ergo manual (Inheritance of Default Properties and Types) discusses inheritance in Ergo and Ch 22.6
Code Inheritance
deals with the issue of a version of inheritance that is closer to what is going on in procedural OO languages.

I am not suggesting, however, that sticking with the way procedural languages do code inheritance is any better than the straightforward option that exists in Ergo.


Here is now count() can be defined in Ergo.



Set[|
     members->{},
     count->0


|].

set1:Set[members->{K,L}].
set1[members->{M}].
set2:Set[members->{A,B,C}].

// a def for the method count.
?X[count->?C]  :-
        ?C = count{?m| ?X:Set[members->?m]}.



ergo> s[count->?X].   // nonexisting set
?X = 0
Yes

ergo> set1[count->?X].
?X = 3
Yes

ergo> set2[count->?X].
?X = 3
Yes


Ely Matos

unread,
Oct 6, 2024, 7:02:13 PM10/6/24
to ErgoAI and XSB Users Forum, Michael Kifer
Thanks, prof. Kifer, for the quick and clear answer.
Probably I'll disturb the group a little during next days with silly questions, until I can grasp the "mindset" of F-Logic/ergoAI. I apologize in advance :-) 
My next two questions:
1) How can I use the "boolean methods"? Given the same example of Sets

Set[|
     members->{},
     count->0
|].

I want to create a boolean method "isEmpty", that is true if (count == 0) and false otherwise. I had tried some rules but none successfully.

2) How can I create a method to remove members from sets? For example

set2:Set[members->{A,B,C}].
// something like "set[removeMember(B)]
// so set2 members are now {A,C}

If think methods like that fall in the same previous issue about methods that modify data from own object. I've read the section 22 but, as you said, maybe ergoAI has a more direct method for that.
Once more, thanks for attention,
Ely

Michael Kifer

unread,
Oct 6, 2024, 11:38:12 PM10/6/24
to Ely Matos, ErgoAI and XSB Users Forum

Set[|
     members->{},
     count->0
|].

set1:Set[members->{K,L}].
set1[members->{M}].
set2:Set[members->{A,B,C}].

set3:Set.

?X[count->?C]  :- ?X:Set, ?C = count{?m| ?X[members->?m]}.
?X[empty] :- ?X:Set[count->0].
?X[%delete(?M)] :- ?X:Set[members->?M], tdelete{?X[members->?M]}.
?X[%insert(?M)] :- ?X:Set, isnonvar{?M}, tinsert{?X[members->?M]}.


ergo> set2[%delete(A)].
Yes

ergo> set2[%insert(123)].
Yes

ergo> set2[members->?m].
?m = 123
?m = B
?m = C
Yes

ergo> set2[empty].
No

ergo> set3[empty].
Yes


If think methods like that fall in the same previous issue about methods that modify data from own object. I've read the section 22 but, as you said, maybe ergoAI has a more direct method for that.


I am not aware of any such issue.


Sec. 22.6 talks about simulation of  code inheritance, which is not provided organically in ErgoAI because it complicates the language while providing no additional important functionality.  ErgoAI has other ways to do what you want, as you can see from the above examples.

--

       --- michael


 

Ely Matos

unread,
Oct 7, 2024, 6:57:31 AM10/7/24
to ErgoAI and XSB Users Forum, Michael Kifer
Thanks!
Sorry by word "issue" :-) English is not my native language... I meant something like "the same question that I cited in the previous email". :-) 
Where can I find reference to use "%" ?
Ely

Michael Kifer

unread,
Oct 7, 2024, 3:12:39 PM10/7/24
to Ely Matos, ErgoAI and XSB Users Forum


On 10/7/24 06:57, Ely Matos wrote:
Thanks!
Sorry by word "issue" :-) English is not my native language... I meant something like "the same question that I cited in the previous email". :-)


You asked how to access the self object. I showed how to do what you want, so it is unclear what is still unclear.

That problem with "self"  does not even arise in F-logic. Procedural languages do have this minor "issue", but it is usually solved by adding simple constructs.  In ErgoAI, no special constructs are needed  for that.


Where can I find reference to use "%" ?


Look for "Transactional methods", 24.1, 24.2.

--

       --- michael


 

Reply all
Reply to author
Forward
0 new messages