简体   繁体   中英

Defining a flexible structure in Prolog

Well, I'm a bit new to Prolog, so my question is on Prolog pattern/logic.

I have an relationship called tablet. It has many parameters, such as name, operationSystem, ramCapacity, etc. I have many objects/predicates of this relationship, like

tablet(
         name("tablet1"),
         operatingSystem("ios"),
         ramCapacity(1024),
         screen(
                  type("IPS"),
                  resolution(1024,2048)
               )
      ).

tablet(
         name("tablet2"),
         operatingSystem("android"),
         ramCapacity(2048),
         screen(
                  type("IPS"),
                  resolution(1024,2048),
                  protected(yes)
               ),
          isSupported(yes)
      ).

And some others similar relationships, BUT with different amounts of parameters. Some of attributes in different objects I do not need OR I have created some tablets, and one day add one more field and started to use it in new tablets.

There are two questions:

  1. I need to use the most flexible structure as possible in prolog. Some of the tablets have attributes/innerPredicates and some do not, but They are all tablets.
  2. I need to access data the easiest way, for example find all tablets that have ramCapacity(1024), not include ones that do not have this attributes.
  3. I do need to change some attributes' values in the easiest way. For example query - change ramCapacity to 2048 for tablet that has name "tablet1".
  4. If it's possible it should be pretty to read in a word editor :)

Is this structure flexible? Should I use another one? Do I need additional rules to manipulate this structure? Is this structure easy to change with query?(I keep this structure in a file).

Since the number of attributes is not fixed and needs to be so flexible, consider to represent these items as in option lists, like this:

tablet([name=tablet1,
        operating_system=ios,
        ram_capacity=1024,
        screen=screen([type="IPS",
                       resolution = res(1024,2048)])]).

tablet([name=tablet2,
        operating_system=android,
        ram_capacity=2048,
        screen=screen([type="IPS",
                       resolution = res(1024,2048)]),
        is_supported=yes]).

You can easily query and arbitrarily extend such lists. Example:

?- tablet(Ts), member(name=tablet2, Ts).
Ts = [name=tablet2, operating_system=android, ram_capacity=2048, screen=screen([type="IPS", resolution=res(..., ...)]), is_supported=yes] ;
false.

Notice also the common Prolog naming_convention_to_use_underscores_for_readability instead of mixingCasesAndMakingEverythingExtremelyHardToRead .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM