CS 3723/3721
|
The Prolog below reproduces the situation described above. The two sets of Prolog "facts": suppliers and supplies are the same as the "proper" way to do database management in the write-up above. These are the Prolog analog to "normalized" relations in database terms. The code below illustrates that one can easily construct the "bad" relation sup_info from the two that are provided. The Prolog code also illustrates how to print information. (In the code below, Prolog tries to match up the information in a rule. The the final presense of fail in the rule tells Prolog to try to match a different way. Because there are write in these rules, a side-effect of the matching is to print the data.)
In the listing below, boldface black is user input, blue is the Prolog source, and red is the output from the Prolog program. (In the final listing, I added few extra tabs by hand.)
[wagner@gateway19 db_design]$ cat supply2.pl /* SUPPLIERS (SNAME, SADDR) */ suppliers(sunshine_produce, addr_16_river_st). suppliers(purity_foodstuffs, addr_180_industrial_rd). suppliers(tasti_supply_co, addr_17_river_st). /* SUPPLIES (SNAME, ITEM, PRICE) */ supplies(sunshine_produce, granola, 129). supplies(sunshine_produce, lettuce, 89). supplies(sunshine_produce, sunflower_seeds, 109). supplies(purity_foodstuffs, whey, 70). supplies(purity_foodstuffs, curds, 80). supplies(purity_foodstuffs, granola, 125). supplies(purity_foodstuffs, unbleached_flour, 65). supplies(tasti_supply_co, lettuce, 79). supplies(tasti_supply_co, whey, 79). supplies(tasti_supply_co, sunflower_seeds, 119). items(ITEM, SNAME, SADDR) :- supplies(SNAME, ITEM, _), suppliers(SNAME, SADDR). items_print(ITEM) :- items(ITEM, SNAME, SADDR), write(ITEM), write(' '), write(SNAME), write(' '), write(SADDR), write(' '), nl, fail. sup_info(SNAME, SADDR, ITEM, PRICE) :- suppliers(SNAME, SADDR), supplies(SNAME, ITEM, PRICE). sup_info_print(_) :- sup_info(SNAME, SADDR, ITEM, PRICE), write(SNAME), write(' \t'), write(SADDR), write(' \t'), write(ITEM), write(' \t'), write(PRICE), nl, fail. [wagner@gateway19 db_design]$ pl Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13) Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- consult(supply2). % supply2 compiled 0.00 sec, 3,504 bytes Yes ?- items(granola, SNAME, SADDR). SNAME = sunshine_produce SADDR = addr_16_river_st ; SNAME = purity_foodstuffs SADDR = addr_180_industrial_rd ; No ?- items_print(granola). granola sunshine_produce addr_16_river_st granola purity_foodstuffs addr_180_industrial_rd No ?- sup_info(X, Y, Z, W). X = sunshine_produce Y = addr_16_river_st Z = granola W = 129 ; X = sunshine_produce Y = addr_16_river_st Z = lettuce W = 89 ; (many lines deleted) X = tasti_supply_co Y = addr_17_river_st Z = sunflower_seeds W = 119 ; No ?- sup_info_print(_). sunshine_produce addr_16_river_st granola 129 sunshine_produce addr_16_river_st lettuce 89 sunshine_produce addr_16_river_st sunflower_seeds 109 purity_foodstuffs addr_180_industrial_rd whey 70 purity_foodstuffs addr_180_industrial_rd curds 80 purity_foodstuffs addr_180_industrial_rd granola 125 purity_foodstuffs addr_180_industrial_rd unbleached_flour 65 tasti_supply_co addr_17_river_st lettuce 79 tasti_supply_co addr_17_river_st whey 79 tasti_supply_co addr_17_river_st sunflower_seeds 119 No ?- halt. [wagner@gateway19 db_design]$