File size: 20,842 Bytes
557a17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# List Prolog Documentation

* Load List Prolog by downloading the <a href="https://github.com/luciangreen/listprologinterpreter">repository from GitHub</a>.

* Please download and install <a href="https://www.swi-prolog.org/build/">SWI-Prolog</a> for your machine.

* Load the List Prolog Interpreter by typing:
`['listprolog'].`

* The interpreter is called in the form:
`interpret(Debug,Query,Functions,Result).`

Where:
Debug - on or off for trace,
Query - the query,
Functions - the algorithm,
Result - the result.

* For example:
```
interpret(off,[[n,function],[1,1,[v,c]]],
[
        [[n,function],[[v,a],[v,b],[v,c]],":-",
        [
                [[n,+],[[v,a],[v,b],[v,c]]]
        ]
        ]
]
,Result).
```

The result of the query is:
`Result=[[[[v,c], 2]]]`  This is the list of non-deterministic results (i.e. ones that SWI-Prolog would return after pressing `";"`) containing the list of variable values. [] is returned if the predicate is false and [[]] is returned if the predicate is true, but there are no results.


# Documentation of Body Structures

In:
```
interpret(off,[[n,function],[1,1,[v,c]]],
[
        [[n,function],[[v,a],[v,b],[v,c]],":-",
        [
                THE BODY
        ]
        ]
]
,Result).
```

* Commands may be in brackets, e.g.
`[[[n,+],[[v,a],[v,b],[v,c]]]]`


* Statements may be negated in the form:

`[[n,not],[Statement]]`

For example:

`[[n,not],[[[n,=],[[v,n],1]]]]`


* Statements may be connected by the disjunctive (or):

`[[n,or],[Statements1,Statements2]]`

For example:

```
[[n,or],[[[n,is],[[v,a],1]],
[[n,is],[[v,a],2]]]]
```

A limitation of List Prolog is that multiple clauses should be used rather than "or" to give non-deterministic results.

* If-then statements may either be in the form:

`[[n,"->"],[Statements1,Statements2]]`

This means "If Statements1 then Statements2".

E.g. 
```
[[n,"->"],[[[n,>],[[v,a],[v,c122]]],
[[n,downpipe],[[v,c122],[v,b],[v,c]]]]]
```

* Or, if-then statements may be in the form:

`[[n,"->"],[Statements1,Statements2,Statements2a]]`

This means "If Statements1 then Statements2, else Statements2a".

For example:

```
[[n,"->"],[[[n,deletea2],[[v,m],[v,h],[v,m1]]],
[[n,true]],
[[n,=],[[v,m],[v,m1]]]]]
```


# Documentation of Commands


* `[[n,IsOp],[Variable1,Variable2,Variable3]]` e.g. `[[n,+],[1,2,[v,b]]]]` returns `[v,b]=3` where `IsOp` may be `+`,`-`,`*` or `/`.

* `[[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]=`e.g. `'a'`, an atom

* `[[n,string],[Variable]]`, e.g. `[[n,string],[[v,a]]]` - returns true if `[v,a]=`e.g. `"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,IsOperator],[Variable1,Variable2]]`, where `IsOperator=is` or `IsOperator="="`, e.g. `[[n,=],[[v,a],1]]` - returns true if `[v,a]=1`

* `[[n,ComparisonOperator],[Variable1,Variable2]]`, where `ComparisonOperator=">",">=","<", "=<", "=" or "=\="` e.g. `[[n,=\=],[1,2]]` - returns `not(1=2)=true`.

* `[[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,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"`

* `[[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],[["a","b","c"],[v,b]]]]` 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,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.

* See also <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/additional_lpi_commands.md">Additional LPI Commands</a>.

* See lpiverify4.pl for examples of rules (predicates without bodies) and calls to predicates.


# 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]]]]
	],
[[]]).
```

* And:

```
test(8,[[n,grammar1],["apple"]],
[
		  [[n,grammar1],[[v,s]],":-",
		  [
		  			 [[n,noun],[[v,s],""]] 
		  ]
		  ],
		  
		  [[n,noun],"->",["apple"]]
],[[]]).
```

* In `[[n,noun],[[v,s],""]]`, the argument `[v,s]` is the entry string and `""` is the exit string.

* In the above example, the word `"apple"` is parsed.  Grammars use `"->"`, not `":-"`.

* 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.

* Grammars may have extra arguments, placed after the other arguments.  The entry and exit string arguments are only used outside the grammar, and can be accessed, e.g. `b` in:
```
		  [[n,lookahead],[[v,a],[v,a],[v,b]],":-",
		  [[[n,stringconcat],[[v,b],[v,d],[v,a]]]]]
```		  
Note: `":-"`, not `"->"`  
		
		
* Base case 1 for recursive grammars, which requires e.g.
```
		  [[n,compound212],["","",[v,t],[v,t]]],
```
which is triggered at the end of the string (`""`), taking and returning the extra argument `t`.

and a "bottom case" (base case 2) in case it is not at the end of the string, e.g.:
```
		  [[n,compound212],[[v,x],[v,x],[v,t],[v,t]]],
```
which is triggered if the first base case is not matched.  It takes and returns `x` (the entry and exit strings) and the extra argument `t`.

given the clause:
```
  [
    [n,compound21],[[v,t],[v,u]],"->",
    [
      [[n,a]],
      [[n,code],[[n,wrap],["a",[v,itemname1]]],
        [[n,append],[[v,t],[v,itemname1],[v,v]]]
      ],
      [[n,compound212],[[v,v],[v,u]]]
    ]
  ],
```

In it, `[[n,a]]` calls a grammar predicate called `"a"`.  `[[n,code],...]` is similar to `{}` in SWI-Prolog (it allows commands to be called within a grammar).  The code wraps a string and appends it to a list, before exiting code and calling the grammar predicate `compound212`.  `v` and `u` are not entry and exit strings, they are extra arguments, handled with `t` in the base cases 1 and 2 above.  The start of the entry string is matched with strings when [[n,a]] is called and any grammar predicates (outside `[[n,code],...]` i.e. `[[n,compound212],[[v,v],[v,u]]]` are given the rest of the entry string (which is an exit string), and this continues until the string ends at base case 1 or the string doesn't end at base case 2 and is processed in a later clause.

* Sometimes there is another recursive clause, which calls itself:
```
  [
    [n,compound21],[[v,t],[v,u]],"->",
    [
      [[n,a]],",",
      [[n,compound21],[[],[v,compound1name]]],
      [[n,code],[[n,wrap],["a",[v,itemname1]]],
        [[n,append],[[v,t],[v,itemname1],[v,v]]],
        [[n,append],[[v,v],[v,compound1name],[v,u]]]
      ]
    ]
  ],
```
		  
`[[n,a]]`, a call, could be substituted with `[v,b]`, however `[[n,a]]` would call a grammar predicate and `[v,b]` would return a character.

* When we need to find out what the next character is but parse the character somewhere else, we use lookahead.

E.g.:
```
  [
    [n,word21],[[v,t],[v,u]],"->",
    [
      [v,a],[[n,commaorrightbracketnext]],
      [[n,code],[[n,letters],[[v,a]]],
        [[n,stringconcat],[[v,t],[v,a],[v,v]]]
      ],
      [[n,word212],[[v,v],[v,u]]]
    ]
  ],
```

With `commaorrightbracketnext` (which looks ahead for a comma or `"]"`), it doesn't return true in `"a"` of `"ab,c"`
when it is run and goes to `"b"` instead as wanted on another run.

* Note, we can call `lookahead` as a grammar predicate:
`[[n,lookahead],["ate"]]`

even though the predicate itself is not a grammar predicate:
```
		  [[n,lookahead],[[v,a],[v,a],[v,b]],":-",
		  [[[n,stringconcat],[[v,b],[v,d],[v,a]]]]]
```
where `[v,b]="ate"`.

* Predicates or predicates to modify to provide the function of string to list (test 15), split on character(s) (test 17), `intersection` and `minus` are in <a href="https://github.com/luciangreen/listprologinterpreter/blob/master/lpiverify4.pl">lpiverify4.pl</a>.

# 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"]]]]]).
```

* Also:

```
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]]]]).
```

* The following type statement tests number types.
```
test_types_cases(1,[[n,function],[1,1,[v,c]]],
[[[n,function],[[t,number],[t,number],[t,number]]]],
[[[n,function],[input,input,output]]],
[
        [[n,function],[[v,a],[v,b],[v,c]],":-",
        [
                [[n,+],[[v,a],[v,b],[v,c]]]
        ]
        ]
]
,[[[[v,c], 2]]]).
```

* The following type statement tests number and bracket types.
```
test_types_cases(3,[[n,function],[[v,a]]],
[
[[n,function],[[[t,number]]]]
],
[[[n,function],[output]]],
[
        [[n,function],[[1]]]
]
,[[[[v,a], [1]]]]).
```

* The following type statement tests number and string types.
```
test_types_cases(4,[[n,f],[[v,a],[v,b],[v,c],[v,d]]],
[[[n,f],[[t,number],[t,string],[t,number],[t,string]]]],
[[[n,f],[output,output,output,output]]],
[
        [[n,f],[1,"a",2,"b"]]
]
,[[[[v,a], 1],[[v,b], "a"],[[v,c], 2],[[v,d], "b"]]]).
```

* The following type statement tests unique types, number and string types.
```
test_types_cases(5,[[n,f],[[v,a],[v,b]]],
[
        [[n,f],[[t,a],[t,b]]],
        [[t,a],[[t,number]]],
        [[t,b],[[t,string]]]
],
[
        [[n,f],[output,output]]
],
[
        [[n,f],[1,"a"]]
]
,[[[[v,a], 1],[[v,b], "a"]]]).
```

* The following type statement tests any of number and string types.
```
test_types_cases(6,[[n,f],[[v,a]]],
[
        [[n,f],[[t,a]]],
        [[t,a],[[t,number]]],
        [[t,a],[[t,string]]]
],
[
        [[n,f],[output]]
],
[
        [[n,f],["a"]]
]
,[[[[v,a], "a"]]]).
```

* The following type statements test any types (including multiple types).
```
[
        [[n,map],[[[t, predicatename], [[t, any]]], [t, number], [t, number]]],
        
        [[n,getitemn],[[t, number], {[t, any]}, [t, any]]]
],
```

# 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 type and mode statements together 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]]]).
```

* In the following, `[[n,function],[[[n,function2],[2]],1,1,[v,c]]]` function2 is passed as a variable.  `[v,f11]` is replaced with the function name.
```
%% c=f(g(2), 1, 1)
test(53,[[n,function],[[[n,function2],[2]],1,1,[v,c]]],
[
        [[n,function],[[v,f1],[v,a],[v,b],[v,c]],":-",
        [
                [[n,equals1],[[v,f1],[[v,f11],[v,f12]]]],
                [[n,getitemn],[1,[v,f12],[v,bb]]],
                [[v,f11],[[v,bb],[v,d],[v,f]]],
                [[n,+],[[v,a],[v,b],[v,e]]],
                [[n,+],[[v,e],[v,f],[v,g]]],
                [[n,+],[[v,g],[v,d],[v,c]]]
        ]
        ],
        [[n,function2],[[v,bb],[v,a],[v,f]],":-",
        [
                [[n,is],[[v,a],[v,bb]]],
                [[n,is],[[v,f],1]]
        ]
        ],

        [[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],0]]]],
                [[n,tail],[[v,b],[v,t]]],
                [[n,-],[[v,a],1,[v,d]]],
                [[n,getitemn],[[v,d],[v,t],[v,c]]]
        ]]
]

,[[[[v,c], 5]]]).
```

* 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).