0

i'm looking for the best data structure to represent prolog facts and rules bases in python for easy manipulation later , for example :

    parent(abraham,ismael).
    parent(abraham,isaac).
    parent(isaac,iacob).
    grandfather(B,N):- parent(B,P),parent(P,N).

is it the nested list like this :

base=[["parent","abraham","ismael"],["parent","abraham","isaac"],["parent","isaac","iacob"]]

or with dictionary :

base={"parent": ("abraham","ismael") ,"parent": ("abraham","isaac"), "parent":("isaac","iacob") }

or is there any best methods of representation that you suggest in this case .

PS: i mean by best : simplest , easiest , flexible way .

false
  • 10,264
  • 13
  • 101
  • 209
Adelov
  • 385
  • 1
  • 6
  • 15
  • 1
    Sorry but this is off-topic as it's opinion-based as you're asking for best. Really you should just use what works until it doesn't work. Saying that this is similar to a network so you may consider networkx which supports these kinds of relationships – EdChum Mar 23 '17 at 12:27
  • 2
    https://stackoverflow.com/questions/1917607/relational-logic-programming-in-python – languitar Mar 23 '17 at 12:29
  • Define `'best'` (c: Algorithms and data structures go hand-in-hand. Do you mean the simplest, or most efficient, most flexible, extensible. What do you need to achieve with the data structure? Is it just defining the grandparent relationship? – Peter Wood Mar 23 '17 at 12:31
  • @languitar i'm not looking for libraries , i have to implement it all by myself – Adelov Mar 23 '17 at 12:31
  • @EdChum i don't think so – Adelov Mar 23 '17 at 12:33
  • @PeterWood i mean by best : simplest and easiest and flexible one ; and for the link that you gived to me : i'm not looking for libraries i have to implement it all by myself – Adelov Mar 23 '17 at 12:34
  • See [PyKE](http://pyke.sourceforge.net/), in particular [this example](http://pyke.sourceforge.net/examples.html#family-relations) – Peter Wood Mar 23 '17 at 12:38
  • @PeterWood i will it's what i'm looking for , and thanx :) – Adelov Mar 23 '17 at 12:47

1 Answers1

1

You can use python-like literal dict syntax directly in prolog.

Solution

{
    'parent':
    {
        'name_of_parent': 'abraham' ,
        'name_of_child': 'isaac'
    }
}
.

{
    'parent':
    {
        'name_of_parent': 'isaac'   ,
        'name_of_child': 'iacob'
    }
}
.

{
    'grandfather':
    {
        'name_of_grandfather':  __B__   ,
        'name_of_grandchild':   __N__
    }
}
:-
(
    {
        'parent':
        {
            'name_of_parent': __B__ ,
            'name_of_child': __P__
        }
    }
    ,
    {
        'parent':
        {
            'name_of_parent': __P__ ,
            'name_of_child': __N__
        }
    }
)
.

Example Query

?- 
{
    'grandfather':
    {
        'name_of_grandfather':  __B__ ,
        'name_of_grandchild':   __N__
    }
}
.
__B__ = abraham,
__N__ = iacob ;
false.

Caveat

The : operator might not be defined automatically for your implementation of prolog . If you get a syntax error because of the : , then place this at the top of your file:

:-  op((10'1),(yfx),(:))    .
user229044
  • 232,980
  • 40
  • 330
  • 338
Kintalken
  • 763
  • 5
  • 9
  • 1
    you din't understand me i'm not looking to implement it in prolog i just want define facts and rules with the best data structure in python , so please re-read the post to get understand – Adelov Mar 23 '17 at 15:33