Search code examples
prolog

How to construct components from build of materials


I have the following predicates that models a PC. What I want is to print all pc compoments and sub-compoments.

I want to do something like this

find_set_of_attributes(X):-
 print(X)
 print(X[power)
 print(X[power][usb])

pc([power).
power([usb, voltageregulator]).
usb([container, usbhead, pins]).
voltageregulator([barreljack, capacitor, regulator, leds]).

Solution

  • One way to describe the computer components and sub-components is to use Definite Clause Grammars (DCGs). For example:

    computer --> monitor, cpu, keyboard, mouse.
    ...
    
    power --> usb, voltageregulator.
    
    usb --> container, usbhead, pins.
    
    voltageregulator --> barreljack, capacitor, regulator, leds.
    

    Components without sub-components can be defined as:

    capacitor --> [capacitor].
    leds --> [leds].
    

    To list all the parts (e.g for printing), you can then simply use the de facto standard phrase/2 predicate. For example:

    | ?- phrase(computer, Parts).