{"id":382,"date":"2012-08-04T23:51:27","date_gmt":"2012-08-04T23:51:27","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=382"},"modified":"2012-08-04T23:51:27","modified_gmt":"2012-08-04T23:51:27","slug":"printing-all-prolog-answers-to-a-file","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/printing-all-prolog-answers-to-a-file\/","title":{"rendered":"Printing all Prolog answers to a file"},"content":{"rendered":"<p>Prolog lets you specify a series of declarative logic statements (i.e. &#8220;X is an Apple&#8221;, &#8220;if X is an Apple then X is a fruit&#8221;). It can trace through all solutions to a problem. In the IDE this is done by pressing &#8220;;&#8221;. If you structure your program correctly, you can get the whole output to print to a file.<\/p>\n<pre lang=\"prolog\">\nmain :- \n\tset_prolog_flag(toplevel_print_options,\n                [quoted(true)]),\n\tsubset(['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'], X),\n\twrite(X),\n        nl,fail;true.\nrun :- main, halt.\n<\/pre>\n<p>This triggers a loop by failing each output &#8211; since the result is always considered a failure, Prolog looks for the next valid result. The first line of main sets output options &#8211; this ensures that long values are not truncated with an ellipsis. The second line is your function call. &#8220;Write&#8221; displays the results to the command line, and nl adds a line break. &#8220;Fail&#8221; causes the function to be re-evaluated with the next set of parameters, but ultimately main returns &#8220;true&#8221;, so that anything that calls it can continue on. <\/p>\n<p>Run it like so:<\/p>\n<pre>\nswipl -s test.pl -t run > output.txt\n<\/pre>\n<p>The -s argument specifies a file containing declaration statements. -t specifies a default goal to evaluate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prolog lets you specify a series of declarative logic statements (i.e. &#8220;X is an Apple&#8221;, &#8220;if X is an Apple then X is a fruit&#8221;). It can trace through all solutions to a problem. In the IDE this is done by pressing &#8220;;&#8221;. If you structure your program correctly, you can get the whole output &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/printing-all-prolog-answers-to-a-file\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Printing all Prolog answers to a file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4,5,6],"tags":[42,178,346,442],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/382"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}