Mercurial > repos > other > SevenLanguagesInSevenWeeks
changeset 51:178b18b4f9ba
Start experimenting with Prolog
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 25 Sep 2017 20:49:52 +0100 |
parents | b94161b72db6 |
children | cbaa3546f3f8 |
files | 3-Prolog/day1-food.pl 3-Prolog/day1-wallaceandgrommit.pl 3-Prolog/readme.txt |
diffstat | 3 files changed, 49 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3-Prolog/day1-food.pl Mon Sep 25 20:49:52 2017 +0100 @@ -0,0 +1,19 @@ +% Define some foods and "flavours". +% Note that this is Americanised, so some of these definitions are generous! +food_type(velveeta, cheese). +food_type(ritz, cracker). +food_type(spam, meat). +food_type(sausage, meat). +food_type(jolt, soda). +food_type(twinkie, dessert). + +flavour(sweet, dessert). +flavour(savoury, meat). +flavour(savoury, cheese). +flavour(sweet, soda). + +food_flavour(X, Y) :- food_type(X, Z), flavour(Y, Z). +% Note: flavour(sweet, What) will return 'yes' and a closed set of answers because it is just all of the facts +% but "food_flavour(What, meat)" will return answers and then say 'no', because it hasn't been given a flavour() +% fact for "ritz"/"cracker". +% This doesn't explain why "food(What, meat)" returns "no" after spam and sausage, though. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3-Prolog/day1-wallaceandgrommit.pl Mon Sep 25 20:49:52 2017 +0100 @@ -0,0 +1,16 @@ +% Start with some simple facts +% Note: like semantic systems, names are intrinsicly equal variables +% (or variables don't exist). Names are called "atoms" +likes(wallace, cheese). +likes(grommit, cheese). +likes(wendolene, sheep). +% Each statement ends with a fullstop + +% Rule syntax is a bit odd (":-" and "\+") but we're saying that X and Y +% can be friends if X and Y like the same thing. +% Poor Wendolene. +friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z). +% "\+" is apparently logical negation (because of course it is) +% so the first "subgoal" stops Wallace being Wallace's friend +% +% Note: Prolog nomenclature is that this is "friend/2" - friend func with 2 params \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3-Prolog/readme.txt Mon Sep 25 20:49:52 2017 +0100 @@ -0,0 +1,14 @@ +Installed gprolog from https://software.opensuse.org/package/gprolog + +Some documentation says that Prolog may accept a shebang as the first line, but gprolog didn't like it. + +Some editors will assume ".pl" means Perl. You may need to change your syntax highlighting. + +Running code involves calling "gprolog" without parameters and then running "['filename']." (fullstop terminates the commend). +You can then query it with function calls (e.g. "likes(grommit, sheep)." in the Wallace & Grommit example). Prolog always +returns "yes" or "no" at a prompt when it has finished. + +When you ask a question with a placeholder, the placeholder has to be capitalised. Lower-case makes it an atom. +When Prolog returns its answers, type ";" to get the next response. Sometimes Prolog will end multiple responses +with "no", sometimes with "yes". This depends on whether it needs to do more calculations to check for more answers ("no") +or whether it knows it successfully gave you everything ("yes"). \ No newline at end of file