|
# List Prolog Interpreter |
|
|
|
# Prerequisites |
|
|
|
* Please download and install SWI-Prolog for your machine at https://www.swi-prolog.org/build/. |
|
|
|
* You may need to install gawk using Homebrew. |
|
|
|
* Install <a href="https://github.com/soimort/translate-shell">Translation Shell</a> on Mac, etc. |
|
Change line in & culturaltranslationtool/ctt2.pl |
|
trans_location("../../../gawk/trans").& |
|
to correct location of <a href="https://github.com/soimort/translate-shell">trans</a>. |
|
|
|
# 1. Install manually |
|
|
|
Download <a href="http://github.com/luciangreen/listprologinterpreter/">this repository</a>, the <a href="https://github.com/luciangreen/Languages">Languages repository</a> and <a href="https://github.com/luciangreen/culturaltranslationtool">Cultural Translation Tool</a> into the same folder. |
|
|
|
# 2. Or Install from List Prolog Package Manager (LPPM) |
|
|
|
* Download the <a href="https://github.com/luciangreen/List-Prolog-Package-Manager">LPPM Repository</a>: |
|
|
|
& |
|
mkdir GitHub |
|
cd GitHub/ |
|
git clone https://github.com/luciangreen/List-Prolog-Package-Manager.git |
|
cd List-Prolog-Package-Manager |
|
swipl |
|
['lppm']. |
|
lppm_install("luciangreen","listprologinterpreter"). |
|
halt |
|
& |
|
|
|
# Running |
|
|
|
* Download the repository to your machine. In Shell: |
|
cd listprologinterpreter |
|
swipl |
|
['listprolog']. |
|
|
|
* Running the tests |
|
To run all tests, enter: |
|
&test(off,NTotal,Score).& |
|
|
|
* To run a specific test: |
|
&test1(off,TestNumber,Passed).& |
|
where TestNumber is the test number from <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4.pl">lpiverify4.pl</a>. |
|
|
|
* The query &test1(off,7,Passed).& tests the reverse predicate: |
|
test(7,`[[n,reverse],[[1,2,3],[],[v,l]]]`, |
|
`[[[n,reverse],[[],[v,l],[v,l]]],[[n,reverse],[[v,l],[v,m],[v,n]],":-",[[[n,head],[[v,l],[v,h]]],[[n,tail],[[v,l],[v,t]]],[[n,wrap],[[v,h],[v,h1]]],[[n,append],[[v,h1],[v,m],[v,o]]],[[n,reverse],[[v,t],[v,o],[v,n]]]]]]`, |
|
`[[[v,l], [3, 2, 1]]]`). |
|
|
|
# Documentation |
|
|
|
* The interpreter is called in the form: |
|
&international_interpret([lang,en]&,debug,query,type_statements,mode_statements,functions,result). |
|
|
|
Where: |
|
debug - on or off for trace, |
|
query - the query e.g. [[n,reverse],[[1,2,3],[],[v,l]]] |
|
type statements - e.g. [[n,reverse],[{[t, number]}, {[t, number]}, {[t, number]}]] |
|
mode statements - e.g. [[n,reverse],[input,input,output]] |
|
functions - the algorithm e.g. see reverse above |
|
result - the result, e.g. [[[v,l], [3, 2, 1]]] ([] indicates failed and [[]] indicates the empty list). |
|
|
|
* Statements may be negated in the form: |
|
|
|
`[[n,not],[statement]]` |
|
|
|
* Statements may be connected by the disjunctive (or): |
|
|
|
`[[n,or],[statements1,statements2]]` |
|
|
|
* If-then statements may either be in the form: |
|
|
|
`[[n,"->"],[statements1,statements2]]` |
|
|
|
This means "If Statements1 then Statements2". |
|
|
|
* Or, if-then statements may be in the form: |
|
|
|
`[[n,"->"],[statements1,statements2,statements2a]]` |
|
|
|
This means "If Statements1 then Statements2, else Statements2a". |
|
|
|
* `[[n,+],[1,2,[v,b]]]` `[v,b]=3` |
|
* `[[n,-],[1,2,[v,b]]]` `[v,b]=(-1)` |
|
* `[[n,*],[1,2,[v,b]]]` `[v,b]=2` |
|
* `[[n,/],[1,2,[v,b]]]` `[v,b]=0.5` |
|
|
|
* `[[n,cut]]` - behaves like swipl's ! (doesn't allow backtracking forward or back past it) |
|
|
|
* `[[n,true]]` - behaves like true |
|
|
|
* `[[n,fail]]` - fails the current predicate |
|
|
|
* `[[n,atom],[variable]]`, e.g. `[[n,atom],[[v,a]]]` - returns true if `[v,a]='a'`, an atom |
|
|
|
* `[[n,string],[variable]]`, e.g. `[[n,string],[[v,a]]]` - returns true if `[v,a]="a"`, a string |
|
|
|
* `[[n,number],[variable]]`, e.g. `[[n,number],[14]]` - returns true where `14` is a number |
|
|
|
* `[[n,letters],[variable]]`, e.g. `[[n,letters],["abc"]]` - returns true where `"abc"` is letters |
|
|
|
* `[[n,is_operator],[variable1,variable2]]`, where `is_operator=is` or `is_operator="="`, e.g. `[[n,=],[[v,a],1]]` - returns true if `[v,a]=1` |
|
|
|
* `[[n,comparison_operator],[variable1,variable2]]`, where comparison_operator=">",">=","<", "=<", "=" or =\= e.g. `[[n,>=],[1,2]]` - returns (1>=2)=false. |
|
|
|
* `[[n,equals1],[variable1,[variable2,variable3]]]` e.g. `[[n,equals1],[["a","b"],[[v,a],[v,b]]]]` returns `[v,a]="a"` and `[v,b]="b"` |
|
|
|
* `[[n,equals2],[variable1,[variable2,variable3]]]` e.g. `[[n,equals2],[[v,a],["a","b"]]]` returns `[v,a]=["a","b"]` |
|
|
|
* `[[n,equals3],[variable1,variable2]]` e.g. `[[n,equals3],[[v,a],[1,2,3]]]` returns `[v,a]=[1,2,3]` |
|
|
|
* `[[n,wrap],[variable1,variable2]]` e.g. `[[n,wrap],["a",[v,b]]]` returns `[v,b]=["a"]` |
|
|
|
* `[[n,unwrap],[variable1,variable2]]` e.g. `[[n,wrap],[["a"],[v,b]]]` returns `[v,b]="a"` |
|
|
|
* `[[n,head],[variable1,variable2]]` e.g. `[[n,head],[["a","b","c"],[v,b]]]` returns `[v,b]="a"` |
|
|
|
* `[[n,tail],[variable1,variable2]]` e.g. `[[n,tail],[["a","b","c"],[v,b]]]` returns `[v,b]=["b","c"]` |
|
|
|
* `[[n,member],[Variable1,Variable2]]` e.g. `[[n,member],[[v,b],["a","b","c"]]]` returns `[v,b]="a"`, `[v,b]="b"` or `[v,b]="c"`, or e.g. `[[n,member],["c",["a","b","c"]]]` returns true. (Formerly member2). |
|
|
|
* `[[n,delete],[variable1,variable2,variable3]]` e.g. `[[n,delete],[["a","b","b","c"],"b",[v,c]]]` returns `[v,c]=["a","c"]` |
|
|
|
* `[[n,append],[variable1,variable2,variable3]]` e.g. `[[n,append],[["a","b"],["c"],[v,d]]]` returns `[v,d]=["a","b","c"]`. Note: variable2 must be in list form, not e.g. `"c"`, or the command will fail. Wrap may wrap in []. |
|
|
|
* `[[n,stringconcat],[variable1,variable2,variable3]]` e.g. `[[n,stringconcat],["hello ","john",[v,c]]]` returns `[v,c]="hello john"`. |
|
|
|
* `[[n,stringtonumber],[variable1,variable2]]` e.g. `[[n,stringtonumber],["3",[v,b]]]` returns `[v,b]=3` |
|
|
|
* `[[n,random],[variable1]]` e.g. `[[n,random],[[v,r]]]` returns e.g. `[v,r]=0.19232608946956326` |
|
|
|
* `[[n,length],[variable1,variable2]]` e.g. `[[n,length],[[1,2,3],[v,l]]]` returns `[v,l]=3` |
|
|
|
* `[[n,ceiling],[variable1,variable2]]` e.g. `[[n,ceiling],[0.19232608946956326,[v,c]]]` returns `[v,c]=1` |
|
|
|
* `[[n,date],[year,month,day,hour,minute,seconds]]` e.g. `[[n,date],[[v,year],[v,month],[v,day],[v,hour],[v,minute],[v,second]]]` returns e.g. `[v,year]=2019`, `[v,month]=11`, `[v,day]=6`, `[v,hour]=12`, `[v,minute]=15`, `[v,second]=20.23353409767151`. |
|
|
|
* `[[n,sqrt],[variable1,variable2]]` e.g. `[[n,ceiling],[4,[v,c]]]` returns `[v,c]=2` |
|
|
|
* `[[n,round],[variable1,variable2]]` e.g. `[[n,round],[1.5,[v,c]]]` returns `[v,c]=2` |
|
|
|
* `[[n,writeln],[Variable1]]` e.g. `[[n,writeln],[[v,a]]]` writes `[v,a]` which is `"hello"` |
|
|
|
* `[[n,atom_string],[Variable1,Variable2]]` e.g. `[[n,atom_string],[a,[v,b]]]` returns `[v,b]="a"` or `[[n,atom_string],[[v,b],"a"]]` returns `[v,b]=a` |
|
|
|
* (1) `[[n,call],[Function,Arguments]]` e.g. `[[n,call],[[n,member2a],[[v,b],["a","b","c"]]]]` returns `[v,b]=a` |
|
|
|
* (2) `[[n,call],[[lang,Lang],Debug,[Function,Arguments],Functions]]` e.g. `[[n,call],[[lang,same],same,[[n,member2a],[[v,b],["a","b","c"]]], |
|
[[[n,member2a],[[v,b],[v,a]],":-", |
|
[[[n,member],[[v,b],[v,a]]]]]]]]` returns `[v,b]="a"`, where Lang may be e.g. `"en"`, etc., or `same` (meaning the same language as the parent predicate) and Debug may be `on`, `off` or `same` (meaning the same debug status as the parent predicate). |
|
|
|
* (3) `[[n,call],[[lang,Lang],Debug,[Function,Arguments],Types,Modes,Functions]]` e.g. `[[n,call],[[lang,same],same,[[n,member2a],[[v,b], ["a","b","c"]]], [[[n,member2a],[[t, number], [[t, number], [t, number], [t, number]]]]], |
|
[[[n,member2a],[output,input]]], |
|
[[[n,member2a],[[v,b],[v,a]],":-", |
|
[ [[n,member],[[v,b],[v,a]]]] |
|
]]]]` returns `[v,b]="a"`. (See call(2) above for possible values of Lang and Debug.) |
|
|
|
* `[[n,trace]]` switches on trace (debug) mode. |
|
|
|
* `[[n,notrace]]` switches off trace (debug) mode. |
|
|
|
* `[[n,get_lang_word],[Variable1,Variable2]` e.g. `[[n,get_lang_word],["get_lang_word",[v,word]]]` returns `[v,word]="get_lang_word"`. |
|
|
|
* `[[n,member3],[Variable1,Variable2]` e.g. `[[n,member3],[[[v,a],"|",[[v,b]]],[[1,1],[1,2],[1,3]]]]` returns `[v,a]=1`, `[v,b]=1`, etc. where Variable1 may be in any pattern. |
|
|
|
* `[[n,equals4_on]]` switches on equals4 mode (in check arguments, substitute vars and update vars but not in the equals4 command itself), for e.g. list processing in arguments. |
|
|
|
* `[[n,equals4_off]]` switches off equals4 mode, for debugging. |
|
|
|
* `[[n,equals4],[Variable1,Variable2]]` e.g. `[[n,equals4],[[[v,c],"|",[v,d]],[1,2,3]]]` returns `[v,c]=1` and `[v,d]=[2,3]`. You may use either order (i.e. a=1 or 1=a). Multiple items are allowed in the head of the list, there may be lists within lists, and lists with pipes must have the same number of items in the head in each list, or no pipe in the other list. |
|
|
|
* `[[n,findall],[Variable1,Variable2,Variable3]]` e.g. `[[[n,equals3],[[v,a],[1,2,3]]],[[n,findall],[[v,a1],[[n,member],[[v,a1],[v,a]]],[v,b]]]]` returns `[v,b]=[1,2,3]` |
|
|
|
* `[[n,string_from_file],[Variable1,Variable2]]` e.g. `[[n,string_from_file],[[v,a],"file.txt"]]` returns `[v,a]="Hello World"` |
|
|
|
* `[[n,maplist],[Variable1,Variable2,Variable3,Variable4]]` e.g. `[[n,maplist],[[n,+],[1,2,3],0,[v,b]]]` returns `[v,b]=6` |
|
|
|
* `[[n,string_length],[Variable1,Variable2]]` e.g. `[[n,string_length],["abc",[v,b]]]` returns `[v,b]=3` |
|
|
|
* `[[n,sort],[Variable1,Variable2]]` e.g. `[[n,sort],[[1,3,2],[v,b]]]` returns `[v,b]=[1,2,3]` |
|
|
|
* `[[n,intersection],[Variable1,Variable2]]` e.g. `[[n,intersection],[[1,3,2],[3,4,5],[v,b]]]` returns `[v,b]=[3]` |
|
|
|
* `[[n,read_string],[Variable1]]` e.g. `[[n,read_string],[[v,a]]]` asks for input and returns `[v,a]="hello"` |
|
|
|
# Grammars |
|
|
|
* List Prolog supports grammars, for example: |
|
|
|
* Grammars may be recursive (see test 9 in <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4.pl">lpiverify4.pl</a>), i.e. they may repeat until triggering the base case: |
|
|
|
test(9,`[[n,grammar1],["aaa"]]`, |
|
[ |
|
`[[n,grammar1],[[v,s]],":-",[[[n,noun],[[v,s],""]]]]`, |
|
`[[n,noun],"->",[""]]`, |
|
`[[n,noun],"->",["a",[[n,noun]]]]` |
|
], |
|
`[[]]`). |
|
|
|
# Type Statements |
|
|
|
* Functions may have strong types, which check inputted values when calling a function and check all values when exiting a function. So far, any type statement with the name and arity of the function may match data for a call to that function. |
|
|
|
* The user may optionally enter types after the query. The following type statement tests number, string and predicate name types. |
|
|
|
* Note: Mode statements, described in the next section, are required after Type Statements. |
|
|
|
* Types with lists (0-infinite repeats of type statements) are written inside {}. There may be nested curly brackets. |
|
|
|
* Type Statements may be recursive (see test 23 in <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4_types.pl">lpiverify4_types.pl</a>), i.e. they may repeat until triggering the base case: |
|
|
|
test_types_cases(23,`[[n,connect_cliques],[[["a",1],[1,2],[2,"b"]],[["a",3],[3,4],[4,"b"]],[v,output]]]`, |
|
`[ |
|
[[n,connect_cliques],[[t,list2],[t,list2],[t,list2]]], |
|
[[t,item],[[t,number]]], |
|
[[t,item],[[t,string]]], |
|
[[t,list2],[{[t,item]}]], |
|
[[t,list2],[{[t,list2]}]] |
|
]`, |
|
`[[[n,connect_cliques],[input,input,output]]]`, |
|
`[[[n,connect_cliques],[[v,a],[v,b],[v,c]],":-",[[[n,append],[[v,a],[v,b],[v,c]]]]]]`, |
|
`[[[[v,output],[["a",1],[1,2],[2,"b"],["a",3],[3,4],[4,"b"]]]]]`). |
|
|
|
# Mode Statements |
|
|
|
* In the following, |
|
|
|
test_types_cases(2,`[[n,function],[[v,a],[v,b],[v,c]]]`, |
|
`[[[n,function],[[t,number],[t,string],[t,predicatename]]]]`, |
|
`[[[n,function],[output,output,output]]]`, |
|
[ |
|
`[[n,function],[[v,a],[v,b],[v,c]],":-",[[[n,=],[[v,a],1]],[[n,=],[[v,b],"a"]],[[n,=],[[v,c],[n,a]]]]]` |
|
], |
|
`[[[[v,a], 1],[[v,b], "a"],[[v,c], [n,a]]]]`). |
|
|
|
* `[[[n,function],[output,output,output]]]`, is the mode statement, which must follow the type statement (although these are optional). The Mode Statement specifies whether each of the variables takes input or gives output. |
|
|
|
# Functional List Prolog (FLP) |
|
|
|
* List Prolog has an optional functional mode. In FLP, function calls may be passed as variables and functions may have strong types. |
|
|
|
* Functional algorithms may be recursive (see test 7 in <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4_types.pl">lpiverify4_types.pl</a>), i.e. they may repeat until triggering the base case: |
|
|
|
|
|
test_types_cases(7, |
|
`[[n,map],[[[n,add],[[[n,add],[[[n,add],[1]]]]]],0,[v,d]]]`, |
|
|
|
`[ |
|
[[n,map],[[t,map1],[t,number],[t,number]]], |
|
[[t,map1],[[t,number]]], |
|
[[t,map1],[[[t,predicatename],[[t,map1]]]]], |
|
[[n,add],[[t,number],[t,number],[t,number]]], |
|
[[n,getitemn],[[t,number],{[t,any]},[t,any]]] |
|
]`, |
|
|
|
`[ |
|
[[n,map],[input,input,output]], |
|
|
|
[[n,add],[input,input,output]], |
|
|
|
[[n,getitemn],[input,input,output]] |
|
]`, |
|
`[ |
|
[[n,map],[[v,f1],[v,n1],[v,n]],":-",[[[n,number],[[v,f1]]],[[n,add],[[v,n1],[v,f1],[v,n]]]]], |
|
|
|
[[n,map],[[v,f1],[v,l],[v,n]],":-",[[[n,equals1],[[v,f1],[[v,f11],[v,f12]]]],[[n,=],[[v,f11],[n,add]]],[[n,getitemn],[1,[v,f12],[v,bb]]],[[v,f11],[[v,l],1,[v,l2]]],[[n,map],[[v,bb],[v,l2],[v,n]]]]], |
|
|
|
[[n,add],[[v,a],[v,b],[v,c]],":-",[[n,+],[[v,a],[v,b],[v,c]]]], |
|
|
|
[[n,getitemn],[1,[v,b],[v,c]],":-",[[n,head],[[v,b],[v,c]]]], |
|
|
|
[[n,getitemn],[[v,a],[v,b],[v,c]],":-",[[[n,not],[[[n,=],[[v,a],1]]]],[[n,tail],[[v,b],[v,t]]],[[n,-],[[v,a],1,[v,d]]],[[n,getitemn],[[v,d],[v,t],[v,c]]]]] |
|
]`, |
|
|
|
`[[[[v,d], 4]]]` |
|
). |
|
* For other examples, please see <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4.pl">lpiverify4.pl</a>, <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4_types.pl">lpiverify4_types.pl</a> (for examples with types, including the list type), <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4_open.pl">lpiverify4_open.pl</a> (for examples with open-ended results) and <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4_open_types.pl">lpiverify4_open_types.pl</a> (for examples with open-ended results with types). |
|
|
|
* See https://github.com/luciangreen/listprologinterpreter/blob/master/LPI_docs.md for complete List Prolog Documentation. |
|
|
|
|