-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbuiltin.elpi
More file actions
1770 lines (1263 loc) · 55.6 KB
/
Copy pathbuiltin.elpi
File metadata and controls
1770 lines (1263 loc) · 55.6 KB
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% File generated by elpi -document-builtins, do not edit
% == Core builtins =====================================
% -- Logic --
func true.
true.
func fail.
func false.
% [X = T] unifies X with Y, possibly assigning unification
% variables in X and Y.
external func (=) -> A, A.
% [pattern_matching T P] matches T against pattern P, only
% variables in P are assigned.
external func pattern_match A -> A.
external func (pi) (func A).
external func (sigma) (func A).
kind int type.
kind string type.
kind float type.
external symbol (;) (pred) -> (pred) -> (pred).
(A ; _) :- A.
(_ ; B) :- B.
external symbol (:-) : (func) -> (func) -> (func) = "core".
external symbol (:-) : (func) -> list (pred) -> (func) = "core".
external symbol (,) : variadic (func) (func).
external symbol uvar : A = "core".
external symbol (as) : A -> A -> A = "core".
external symbol (=>) : (pred) -> (func) -> (func) = "core".
external symbol (=>) : list (pred) -> (func) -> (func) = "core".
external symbol (==>) : (pred) -> (func) -> (func).
external symbol (==>) : list (pred) -> (func) -> (func).
% -- Control --
external func !. % The cut operator
func not prop.
not X :- X, !, fail.
not _.
% [declare_constraint C Key1 Key2...] declares C blocked
% on Key1 Key2 ... (variables, or lists thereof).
external func declare_constraint (func) -> any .. .
% [print_constraints] prints all constraints
external func print_constraints.
% [halt ...] halts the program and print the terms
external type halt variadic any (func).
func stop.
stop :- halt.
% -- Evaluation --
:functional pred (is) o:A, i:A.
X is Y :- calc Y X.
% [calc Expr Out] unifies Out with the value of Expr. It can be used in
% tandem with spilling, eg [f {calc (N + 1)}]
external func calc A -> A.
% --- Operators ---
external symbol (-) : A -> A -> A = "1".
external symbol (i-) : int -> int -> int = "1".
external symbol (r-) : float -> float -> float = "1".
external symbol (+) : float -> float -> float = "2".
external symbol (+) : int -> int -> int = "1".
external symbol (i+) : int -> int -> int = "1".
external symbol (r+) : float -> float -> float = "1".
external symbol (*) : float -> float -> float = "2".
external symbol (*) : int -> int -> int = "1".
external symbol (/) : float -> float -> float = "1".
external symbol (mod) : int -> int -> int = "1".
external symbol (div) : int -> int -> int = "1".
external symbol (^) : string -> string -> string = "1".
external symbol (~) : float -> float = "2".
external symbol (~) : int -> int = "1".
external symbol (i~) : int -> int = "1".
external symbol (r~) : float -> float = "1".
external symbol abs : float -> float = "2".
external symbol abs : int -> int = "1".
external symbol iabs : int -> int = "1".
external symbol rabs : float -> float = "1".
external symbol max : float -> float -> float = "2".
external symbol max : int -> int -> int = "1".
external symbol min : float -> float -> float = "2".
external symbol min : int -> int -> int = "1".
external symbol sqrt : float -> float = "1".
external symbol sin : float -> float = "1".
external symbol cos : float -> float = "1".
external symbol arctan : float -> float = "1".
external symbol ln : float -> float = "1".
external symbol int_to_real : int -> float = "1".
external symbol floor : float -> int = "1".
external symbol ceil : float -> int = "1".
external symbol truncate : float -> int = "1".
external symbol size : string -> int = "1".
external symbol chr : int -> string = "1".
external symbol rhc : string -> int = "1".
external symbol string_to_int : string -> int = "1".
external symbol int_to_string : int -> string = "1".
external symbol substring : string -> int -> int -> string = "1".
external symbol real_to_string : float -> string = "1".
% -- Arithmetic tests --
% [lt_ X Y] checks if X < Y. Works for string, int and float
external func lt_ A, A.
% [gt_ X Y] checks if X > Y. Works for string, int and float
external func gt_ A, A.
% [le_ X Y] checks if X =< Y. Works for string, int and float
external func le_ A, A.
% [ge_ X Y] checks if X >= Y. Works for string, int and float
external func ge_ A, A.
func (>) A, A.
X > Y :- gt_ X Y.
func (<) A, A.
X < Y :- lt_ X Y.
func (=<) A, A.
X =< Y :- le_ X Y.
func (>=) A, A.
X >= Y :- ge_ X Y.
func (i>) int, int.
X i> Y :- gt_ X Y.
func (i<) int, int.
X i< Y :- lt_ X Y.
func (i=<) int, int.
X i=< Y :- le_ X Y.
func (i>=) int, int.
X i>= Y :- ge_ X Y.
func (r>) float, float.
X r> Y :- gt_ X Y.
func (r<) float, float.
X r< Y :- lt_ X Y.
func (r=<) float, float.
X r=< Y :- le_ X Y.
func (r>=) float, float.
X r>= Y :- ge_ X Y.
func (s>) string, string.
X s> Y :- gt_ X Y.
func (s<) string, string.
X s< Y :- lt_ X Y.
func (s=<) string, string.
X s=< Y :- le_ X Y.
func (s>=) string, string.
X s>= Y :- ge_ X Y.
% -- Standard data types (supported in the FFI) --
kind list type -> type.
external symbol (::) : X -> list X -> list X = "core".
external symbol ([]) : list X = "core".
% Boolean values: tt and ff since true and false are predicates
kind bool type.
external symbol tt : bool = "1".
external symbol ff : bool = "1".
% Pair: the constructor is pr, since ',' is for conjunction
kind pair type -> type -> type.
external symbol pr : A -> B -> pair A B = "1".
func fst pair A B -> A.
fst (pr A _) A.
func snd pair A B -> B.
snd (pr _ B) B.
kind triple type -> type -> type -> type.
type triple A -> B -> C -> triple A B C.
func triple_1 triple A B C -> A.
triple_1 (triple A _ _) A.
func triple_2 triple A B C -> B.
triple_2 (triple _ B _) B.
func triple_3 triple A B C -> C.
triple_3 (triple _ _ C) C.
% The option type (aka Maybe)
kind option type -> type.
external symbol none : option A = "1".
external symbol some : A -> option A = "1".
% Result of a comparison
kind cmp type.
external symbol eq : cmp = "1".
external symbol lt : cmp = "1".
external symbol gt : cmp = "1".
% Used in builtin variants that return Coq's error rather than failing
kind diagnostic type.
external symbol ok : diagnostic = "1". % Success
external symbol error : string -> diagnostic = "1". % Failure
% == I/O builtins =====================================
% -- I/O --
kind in_stream type.
external symbol std_in : in_stream = "1".
kind out_stream type.
external symbol std_err : out_stream = "1".
external symbol std_out : out_stream = "1".
% [open_in FileName InStream] opens FileName for input
external func open_in string -> in_stream.
% [open_out FileName OutStream] opens FileName for output
external func open_out string -> out_stream.
% [open_append FileName OutStream] opens FileName for output in append mode
external func open_append string -> out_stream.
% [close_in InStream] closes input stream InStream
external func close_in in_stream.
% [close_out OutStream] closes output stream OutStream
external func close_out out_stream.
% [output OutStream Data] writes Data to OutStream
external func output out_stream, string.
% [flush OutStream] flush all output not yet finalized to OutStream
external func flush out_stream.
% [input InStream Bytes Data] reads Bytes from InStream
external func input in_stream, int -> string.
% [input_line InStream Line] reads a full line from InStream
external func input_line in_stream -> string.
% [eof InStream] checks if no more data can be read from InStream
external func eof in_stream.
% -- System --
% [gettimeofday T] sets T to the number of seconds elapsed since 1/1/1970
external func gettimeofday -> float.
% [sys.file_exists Path] is like [Sys.file_exists]. It succeeds if the file
% at [Path] exists
external func sys.file_exists string.
% [sys.is_directory Path Diagnostic] is like [Sys.is_directory]. It succeeds
% if [Diagnostic = ok] and [Path] is a directory or [Diagnostic = error Msg]
% and [Msg] describes an encountered system error.
external func sys.is_directory string -> diagnostic.
% [sys.remove Path Diagnostic] is like [Sys.remove]. It succeeds if
% [Diagnostic = ok] and the file at [Path] is removed or [Diagnostic = error
% Msg] and [Msg] describes an encountered system error.
external func sys.remove string -> diagnostic.
% [sys.rename OldPath NewPath Diagnostic] is like [Sys.rename]. It succeeds
% if [Diagnostic = ok] and the file at [OldPath] is renamed to [NewPath] or
% [Diagnostic = error Msg] and [Msg] describes an encountered system error.
external func sys.rename string, string -> diagnostic.
% [getenv VarName Value] Like Sys.getenv_opt
external func getenv string -> option string.
% [system Command RetVal] executes Command and sets RetVal to the exit code
external func system string -> int.
% [sys.chdir DirPath Diagnostic] is like [Sys.chdir]. It succeeds if
% [Diagnostic = ok] and the current working directory is updated to
% [DirPath] or [Diagnostic = error Msg] and [Msg] describes an encountered
% system error.
external func sys.chdir string -> diagnostic.
% [sys.mkdir DirPath Permissions Diagnostic] is like [Sys.mkdir]. It
% succeeds if [Diagnostic = ok] and the directory [DirPath] is created or
% [Diagnostic = error Msg] and [Msg] describes an encountered system error.
external func sys.mkdir string, int -> diagnostic.
% [sys.rmdir DirPath Diagnostic] is like [Sys.rmdir]. It succeeds if
% [Diagnostic = ok] and the directory [DirPath] is removed or [Diagnostic =
% error Msg] and [Msg] describes an encountered system error.
external func sys.rmdir string -> diagnostic.
% [sys.getcwd DirPath Diagnostic] is like [Sys.getcwd]. It succeeds if
% [Diagnostic = ok] and [DirPath] is the current working directory or
% [Diagnostic = error Msg] and [Msg] describes an encountered system error.
external func sys.getcwd -> string, diagnostic.
% [sys.readdir DirPath Contents Diagnostic] is like [Sys.readdir]. It
% succeeds if [Diagnostic = ok] and [Contents] is the list of files in
% [DirPath] or [Diagnostic = error Msg] and [Msg] describes an encountered
% system error.
external func sys.readdir string -> list string, diagnostic.
% -- Unix --
% gathers the standard file descriptors or a process
kind unix.process type.
external symbol unix.process : out_stream -> in_stream -> in_stream ->
unix.process = "1".
% [unix.process.open Executable Arguments Environment P Diagnostic] OCaml's
% Unix.open_process_args_full.
% Note that the first argument is the executable name (as in argv[0]).
% If Executable is omitted it defaults to the first element of
% Arguments.
% Environment can be left unspecified, defaults to the current process
% environment.
% This API only works reliably since OCaml 4.12.
external func unix.process.open string, list string,
list string -> unix.process, diagnostic.
% [unix.process.close P Diagnostic] OCaml's Unix.close_process_full
external func unix.process.close unix.process -> diagnostic.
% -- Debugging --
% [term_to_string T S] prints T to S
external func term_to_string -> any, string.
% == Lambda Prolog builtins =====================================
% -- Extra I/O --
% [open_string DataIn InStream] opens DataIn as an input stream
external func open_string string -> in_stream.
% [lookahead InStream NextChar] peeks one byte from InStream
external func lookahead in_stream -> string.
pred printterm out_stream, A.
printterm S T :- term_to_string T T1, output S T1.
% == Elpi builtins =====================================
% [dprint ...] prints raw terms (debugging)
external type dprint variadic any (func).
% [print ...] prints terms
external type print variadic any (func).
% Deprecated, use trace.counter
func counter string -> int.
counter C N :- trace.counter C N.
kind loc type.
% [loc.fields Loc File StartChar StopChar Line LineStartsAtChar] Decomposes
% a loc into its fields
external func loc.fields loc -> string, int, int, int, int.
% == Regular Expressions =====================================
% [rex.match Rex Subject] checks if Subject matches Rex. Matching is based
% on OCaml's Str library
external func rex.match string, string.
% [rex.replace Rex Replacement Subject Out] Out is obtained by replacing all
% occurrences of Rex with Replacement in Subject. See also OCaml's
% Str.global_replace
external func rex.replace string, string, string -> string.
% [rex.split Rex Subject Out] Out is obtained by splitting Subject at all
% occurrences of Rex. See also OCaml's Str.split
external func rex.split string, string -> list string.
% Deprecated, use rex.match
func rex_match string, string.
rex_match Rx S :- rex.match Rx S.
% Deprecated, use rex.replace
func rex_replace string, string, string -> string.
rex_replace Rx R S O :- rex.replace Rx R S O.
% Deprecated, use rex.split
func rex_split string, string -> list string.
rex_split Rx S L :- rex.split Rx S L.
% == Elpi nonlogical builtins =====================================
% [var V ...] checks if the term V is a variable. When used with tree
% arguments it relates an applied variable with its head and argument list.
external func var -> any, any.. .
% [prune V L] V is pruned to L (V is unified with a variable that only sees
% the list of names L)
:functional :external pred prune o:any, i:list any.
% [distinct_names L] checks if L is a list of distinct names. If L is the
% scope of a unification variable (its arguments, as per var predicate) then
% distinct_names L checks that such variable is in the Miller pattern
% fragment (L_\lambda)
external func distinct_names list any.
% [same_var V1 V2] checks if the two terms V1 and V2 are the same variable,
% ignoring the arguments of the variables
external func same_var A, A.
% [same_term T1 T2] checks if the two terms T1 and T2 are syntactically
% equal (no unification). It behaves differently than same_var since it
% recursively compares the arguments of the variables
external func same_term A, A.
% Infix notation for same_term
func (==) A, A.
X == Y :- same_term X Y.
% Unification without occur check. It can create infinite terms.
:nooc
func unsound_unif -> A, A.
unsound_unif X X.
% [cmp_term A B Cmp] Compares A and B. Only works if A and B are ground.
external func cmp_term any, any -> cmp.
% [name T ...] checks if T is a eigenvariable. When used with tree arguments
% it relates an applied name with its head and argument list.
external func name -> any, any.. .
% [constant T ...] checks if T is a (global) constant. When used with tree
% arguments it relates an applied constant with its head and argument list.
external func constant -> any, any.. .
external func names % generates the list of eigenvariable
-> list any. % list of eigenvariables in order of age (young first)
external func occurs % checks if the atom occurs in the term
any, % an atom, that is a global constant or a bound name (aka eigenvariable)
any. % a term
% [closed_term T] unify T with a variable that has no eigenvariables in
% scope
external func closed_term -> any.
% [ground_term T] Checks if T contains unification variables
external func ground_term any.
% [is_cdata T Ctype] checks if T is primitive of type Ctype, eg "int"
external func is_cdata any -> string.
func primitive? any, string.
primitive? X S :- is_cdata X S.
% [new_int N] unifies N with a different int every time it is called. Values
% of N are guaranteed to be incresing.
external func new_int -> int.
% [findall_solution P L] finds all the solved instances of P and puts them
% in L in the order in which they are found. Instances can contain
% eigenvariables and unification variables. P may or may not be
% instantiated. Instances should be found in L.
external func findall_solutions prop -> list prop.
% Holds data across bracktracking; can only contain closed terms
kind safe type.
% [new_safe Safe] creates a safe: a store that persists across backtracking
external func new_safe -> safe.
% [stash_in_safe Safe Data] stores Data in the Safe
external func stash_in_safe safe, A.
% [open_safe Safe Data] retrieves the Data stored in Safe
external func open_safe safe -> list A.
% [if C T E] picks the first success of C then runs T (never E).
% if C has no success it runs E.
func if (pred), (func), (func).
if B T _ :- B, !, T.
if _ _ E :- E.
% [if2 C1 B1 C2 B2 E] like if but with 2 then branches (and one else branch).
func if2 (pred), (func), (pred), (func), (func).
if2 G1 P1 _ _ _ :- G1, !, P1.
if2 _ _ G2 P2 _ :- G2, !, P2.
if2 _ _ _ _ E :- !, E.
% [random.init Seed] Initialize OCaml's PRNG with the given Seed
external func random.init int.
% [random.self_init] Initialize OCaml's PRNG with some seed
external func random.self_init.
% [random.int Bound N] unifies N with a random int between 0 and Bound
% (excluded)
external func random.int int -> int.
#line 1 "builtin_stdlib.elpi"
% == stdlib =======================================================
% Conventions:
% - all predicates declare a mode with some input arguments, unless...
% - predicates whose name ends with R are relations (work in any direction,
% that is all arguments are in output mode)
% - predicates whose name ends with ! do contain a cut and generate only the
% first result
% - all errors given by this library end up calling fatal-error[-w-data],
% override it in order to handle them differently
% - all debug prints by this library end up calling debug-print, override it
% in order to handle them differently
namespace std {
func fatal-error string.
:name "default-fatal-error"
fatal-error Msg :- halt Msg.
func fatal-error-w-data string, any.
:name "default-fatal-error-w-data"
fatal-error-w-data Msg Data :- halt Msg ":" Data.
func debug-print string, any.
:name "default-debug-print"
debug-print Msg Data :- print Msg Data.
% -- Errors, Debugging, Hacks --
func ignore-failure! prop ->.
ignore-failure! P :- P, !.
ignore-failure! _.
func once prop ->.
once P :- P, !.
% [assert! C M] takes the first success of C or fails with message M
func assert! prop, string.
assert! Cond Msg :- (Cond ; fatal-error-w-data Msg Cond), !.
% [assert-ok! C M] like assert! but the last argument of the predicate must
% be a diagnostic that is printed after M in case it is not ok
func assert-ok! (pred o:diagnostic), string.
assert-ok! Cond Msg :- Cond Diagnostic, !, (Diagnostic = ok ; Diagnostic = error S, fatal-error-w-data Msg S), !.
assert-ok! _ Msg :- fatal-error-w-data Msg "no diagnostic returned".
% [spy P] traces the call to P, printing all success and the final failure
pred spy (pred).
spy P :- trace.counter "run" NR, if (not(NR = 0)) (debug-print "run=" NR) true,
debug-print "----<<---- enter: " P,
P,
debug-print "---->>---- exit: " P.
spy P :- debug-print "---->>---- fail: " P, fail.
% [spy! P] traces the first call to P without leaving a choice point
func spy! prop.
spy! P :- trace.counter "run" NR, if (not(NR = 0)) (debug-print "run=" NR) true,
debug-print "----<<---- enter: " P,
P,
debug-print "---->>---- exit: " P, !.
spy! P :- debug-print "---->>---- fail: " P, fail.
% to silence the type checker
func unsafe-cast A -> B.
:untyped unsafe-cast X X.
% -- List processing --
func length list A -> int.
length [_|L] N :- length L N1, N is N1 + 1.
length [] 0.
func rev list A -> list A.
rev L RL :- rev-append L [] RL.
func rev-append list A, list A -> list A.
rev-append [X|XS] ACC R :- rev-append XS [X|ACC] R.
rev-append [] L L.
func rev.aux list A, list A -> list A.
rev.aux L1 L2 L3 :- rev-append L1 L2 L3.
func last list A -> A.
last [] _ :- fatal-error "last on empty list".
last [X] X :- !.
last [_|XS] R :- last XS R.
func append list A, list A -> list A.
append [X|XS] L [X|L1] :- append XS L L1 .
append [] L L .
pred appendR -> list A, list A, list A.
appendR [] L L.
appendR [X|XS] L [X|L1] :- appendR XS L L1.
func take int, list A -> list A.
take 0 _ [] :- !.
take N [X|XS] [X|L] :- !, N1 is N - 1, take N1 XS L.
take _ _ _ :- fatal-error "take run out of list items".
func take-last int, list A -> list A.
take-last N L R :-
length L M,
D is M - N,
drop D L R.
func drop int, list A -> list A.
drop 0 L L :- !.
drop N [_|XS] L :- !, N1 is N - 1, drop N1 XS L.
drop _ _ _ :- fatal-error "drop run out of list items".
func drop-last int, list A -> list A.
drop-last N L R :-
length L M, I is M - N, take I L R.
func split-at int, list A -> list A, list A.
split-at 0 L [] L :- !.
split-at N [X|XS] [X|LN] LM :- !, N1 is N - 1, split-at N1 XS LN LM.
split-at _ _ _ _ :- fatal-error "split-at run out of list items".
func fold list B, A, (func B, A -> A) -> A.
fold [] A _ A.
fold [X|XS] A F R :- F X A A1, fold XS A1 F R.
func fold-right list B, A, (func B, A -> A) -> A.
fold-right [] A _ A.
fold-right [X|XS] A F R :- fold-right XS A F A', F X A' R.
:index (1 1)
func fold2 list C, list B, A, (func C, B, A -> A) -> A.
fold2 [] [_|_] _ _ _ :- fatal-error "fold2 on lists of different length".
fold2 [_|_] [] _ _ _ :- fatal-error "fold2 on lists of different length".
fold2 [] [] A _ A.
fold2 [X|XS] [Y|YS] A F R :- F X Y A A1, fold2 XS YS A1 F R.
func map list A, (func A -> B) -> list B.
map [] _ [].
map [X|XS] F [Y|YS] :- F X Y, map XS F YS.
func map-i list A, (func int, A -> B) -> list B.
map-i L F R :- map-i.aux L 0 F R.
func map-i.aux list A, int, (func int, A -> B) -> list B.
map-i.aux [] _ _ [].
map-i.aux [X|XS] N F [Y|YS] :- F N X Y, M is N + 1, map-i.aux XS M F YS.
func map-filter list A, (func A -> B) -> list B.
map-filter [] _ [].
map-filter [X|XS] F [Y|YS] :- F X Y, !, map-filter XS F YS.
map-filter [_|XS] F YS :- map-filter XS F YS.
:index(1 1)
func map2 list A, list B, (func A, B -> C) -> list C.
map2 [] [_|_] _ _ :- fatal-error "map2 on lists of different length".
map2 [_|_] [] _ _ :- fatal-error "map2 on lists of different length".
map2 [] [] _ [].
map2 [X|XS] [Y|YS] F [Z|ZS] :- F X Y Z, map2 XS YS F ZS.
func map2-filter list A, list B, (func A, B -> C) -> list C.
map2-filter [] [_|_] _ _ :- fatal-error "map2-filter on lists of different length".
map2-filter [_|_] [] _ _ :- fatal-error "map2-filter on lists of different length".
map2-filter [] [] _ [].
map2-filter [X|XS] [Y|YS] F [Z|ZS] :- F X Y Z, !, map2-filter XS YS F ZS.
map2-filter [_|XS] [_|YS] F ZS :- map2-filter XS YS F ZS.
func map-ok list A, (func A -> B, diagnostic) -> list B, diagnostic.
map-ok [X|L] P [Y|YS] S :- P X Y S0, if (S0 = ok) (map-ok L P YS S) (S = S0).
map-ok [] _ [] ok.
func fold-map list A, B, (func A, B -> C, B) -> list C, B.
fold-map [] A _ [] A.
fold-map [X|XS] A F [Y|YS] A2 :- F X A Y A1, fold-map XS A1 F YS A2.
func omap option A, (func A -> B) -> option B.
omap none _ none.
omap (some X) F (some Y) :- F X Y.
% [nth N L X] picks in X the N-th element of L (L must be of length > N)
func nth int, list A -> A.
nth 0 [X|_ ] R :- !, X = R.
nth N [_|XS] R :- N > 0, !, N1 is N - 1, nth N1 XS R.
nth N _ _ :- N < 0, !, fatal-error "nth got a negative index".
nth _ _ _ :- fatal-error "nth run out of list items".
% [lookup L K V] sees L as a map from K to V
pred lookup list (pair A B), A -> B.
lookup [pr X Y|_] X Y.
lookup [_|LS] X Y :- lookup LS X Y.
% [lookup! L K V] sees L as a map from K to V, stops at the first binding
func lookup! list (pair A B), A -> B.
lookup! [pr X Y|_] X Y :- !.
lookup! [_|LS] X Y :- lookup! LS X Y.
% [mem! L X] succeeds once if X occurs inside L
func mem! list A, A ->.
mem! [X|_] X :- !.
mem! [_|L] X :- mem! L X.
% [mem L X] succeeds every time if X occurs inside L
pred mem list A -> A.
mem [X|_] X.
mem [_|L] X :- mem L X.
func exists! list A, (pred A).
exists! [X| _] P :- P X, !.
exists! [_|XS] P :- exists! XS P.
pred exists list A, (pred A).
exists [X|_] P :- P X.
exists [_|L] P :- exists L P.
pred exists2 list A, list B, (pred A, B).
exists2 [] [_|_] _ :- fatal-error "exists2 on lists of different length".
exists2 [_|_] [] _ :- fatal-error "exists2 on lists of different length".
exists2 [X|_] [Y|_] P :- P X Y.
exists2 [_|L] [_|M] P :- exists2 L M P.
func forall list A, (func A).
forall [] _.
forall [X|L] P :- P X, forall L P.
func forall-ok list A, (func A -> diagnostic) -> diagnostic.
forall-ok [X|L] P S :- P X S0, if (S0 = ok) (forall-ok L P S) (S = S0).
forall-ok [] _ ok.
func forall2 list A, list B, (func A, B).
forall2 [] [_|_] _ :- fatal-error "forall2 on lists of different length".
forall2 [_|_] [] _ :- fatal-error "forall2 on lists of different length".
forall2 [X|XS] [Y|YS] P :- P X Y, forall2 XS YS P.
forall2 [] [] _.
func filter list A, (func A) -> list A.
filter [] _ [].
filter [X|L] P R :- if (P X) (R = X :: L1) (R = L1), filter L P L1.
:index (1)
func partition list A, (pred A) -> list A, list A.
partition [] _ [] [].
partition [X|XS] P [X|R] L :- P X, !, partition XS P R L.
partition [X|XS] P R [X|L] :- partition XS P R L.
func zip list A, list B -> list (pair A B).
zip [_|_] [] _ :- fatal-error "zip on lists of different length".
zip [] [_|_] _ :- fatal-error "zip on lists of different length".
zip [X|LX] [Y|LY] [pr X Y|LR] :- zip LX LY LR.
zip [] [] [].
func unzip list (pair A B) -> list A, list B.
unzip [] [] [].
unzip [pr X Y|L] [X|LX] [Y|LY] :- unzip L LX LY.
func flatten list (list A) -> list A.
flatten [X|LS] R :- flatten LS LS', append X LS' R.
flatten [] [].
func null list A.
null [].
% [make N E L] L is [E, ..., E] and L has length N
func list.make int, A -> list A.
list.make 0 _ [] :- !.
list.make N E [E|L] :- N' is N - 1, list.make N' E L.
% [init N F L] L is [F 0, ..., F (N-1)]
func list.init int, (func int -> A) -> list A.
list.init N F L :- assert! (N >= 0) ("list.init: negative length"), list.init.aux 0 N F L.
func list.init.aux int, int, (func int -> A) -> list A.
list.init.aux N N _ [] :- !.
list.init.aux N M F [E|L] :- F N E, N' is N + 1, list.init.aux N' M F L.
func iota int -> list int.
iota N L :- list.init N (x\y\ x = y) L.
% [intersperse X L R] R is [L0, X, ..., X, LN]
:index(_ 1)
func intersperse A, list A -> list A.
intersperse _ [] [].
intersperse _ [X] [X] :- !.
intersperse Sep [X|XS] [X,Sep|YS] :- intersperse Sep XS YS.
% -- Misc --
func flip (func A, B), B, A.
flip P X Y :- P Y X.
func time (func) -> float.
time P T :- gettimeofday Before, P, gettimeofday After, T is After - Before.
func do! list prop.
do! [].
do! [P|PS] :- P, !, do! PS.
:index(_ 1)
:functional
pred do-ok! o:diagnostic, i:list (pred o:diagnostic).
do-ok! ok [].
do-ok! S [P|PS] :- P S0, !, if (S0 = ok) (do-ok! S PS) (S = S0).
pred lift-ok (pred), string -> diagnostic.
lift-ok P Msg R :- (P, R = ok; R = error Msg).
func spy-do! list prop.
spy-do! L :- map L (x\y\y = spy x) L1, do! L1.
func while-ok-do! diagnostic, list (pred -> diagnostic) -> diagnostic.
while-ok-do! (error _ as E) _ E.
while-ok-do! ok [] ok.
while-ok-do! ok [P|PS] R :- P C, !, while-ok-do! C PS R.
func any->string A -> string.
any->string X Y :- term_to_string X Y.
func max A, A -> A.
max N M N :- N >= M, !.
max _ M M.
% [findall P L] L is the list [P1,P2,P3..] where each Pi is a solution to P.
func findall prop -> list prop.
findall P L :- findall_solutions P L.
}
% [std.string.concat Separator Items Result] concatenates Items
% interspersing Separator
external func std.string.concat string, list string -> string.
% CAVEAT: the type parameter of std.string.map must be a closed term
kind std.string.map type -> type.
% [std.string.map.empty M] The empty map
external func std.string.map.empty -> std.string.map A.
% [std.string.map.mem S M] Checks if S is bound in M
external func std.string.map.mem string, std.string.map A.
% [std.string.map.add S V M M1] M1 is M where V is bound to S
external func std.string.map.add string, A,
std.string.map A -> std.string.map A.
% [std.string.map.remove S M M1] M1 is M where S is unbound
external func std.string.map.remove string,
std.string.map A -> std.string.map A.
% [std.string.map.find S M V] V is the binding of S in M
external func std.string.map.find string, std.string.map A -> A.
% [std.string.map.bindings M L] L is M transformed into an associative list
external func std.string.map.bindings std.string.map A -> list (pair string A).
% [std.string.map.filter M F M1] Filter M w.r.t. the predicate F
external func std.string.map.filter std.string.map A,
(string -> A -> prop) -> std.string.map A.
% [std.string.map.map M F M1] Map M w.r.t. the predicate F
external func std.string.map.map std.string.map A,
(string -> A -> B -> prop) -> std.string.map B.
% [std.string.map.fold M Acc F Acc1] fold M w.r.t. the predicate F
external func std.string.map.fold std.string.map A, C,
(string -> A -> C -> C -> prop) -> C.
% CAVEAT: the type parameter of std.int.map must be a closed term
kind std.int.map type -> type.
% [std.int.map.empty M] The empty map
external func std.int.map.empty -> std.int.map A.
% [std.int.map.mem S M] Checks if S is bound in M
external func std.int.map.mem int, std.int.map A.
% [std.int.map.add S V M M1] M1 is M where V is bound to S
external func std.int.map.add int, A, std.int.map A -> std.int.map A.
% [std.int.map.remove S M M1] M1 is M where S is unbound
external func std.int.map.remove int, std.int.map A -> std.int.map A.
% [std.int.map.find S M V] V is the binding of S in M
external func std.int.map.find int, std.int.map A -> A.
% [std.int.map.bindings M L] L is M transformed into an associative list
external func std.int.map.bindings std.int.map A -> list (pair int A).
% [std.int.map.filter M F M1] Filter M w.r.t. the predicate F
external func std.int.map.filter std.int.map A,
(int -> A -> prop) -> std.int.map A.
% [std.int.map.map M F M1] Map M w.r.t. the predicate F
external func std.int.map.map std.int.map A,
(int -> A -> B -> prop) -> std.int.map B.
% [std.int.map.fold M Acc F Acc1] fold M w.r.t. the predicate F
external func std.int.map.fold std.int.map A, C,
(int -> A -> C -> C -> prop) -> C.