-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathAPI.mli
1191 lines (948 loc) · 41.9 KB
/
API.mli
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
(* elpi: embedded lambda prolog interpreter *)
(* license: GNU Lesser General Public License Version 2.1 or later *)
(* ------------------------------------------------------------------------- *)
(** This module is the API for clients of the Elpi library. *)
(* ************************************************************************* *)
(* *************************** Basic API *********************************** *)
(* ************************************************************************* *)
(** These APIs are sufficient to parse programs and queries from text, run
the interpreter and finally print the result *)
module Ast : sig
type program
type query
module Loc : sig
type t = {
source_name : string;
source_start: int;
source_stop: int;
line: int;
line_starts_at: int;
}
val pp : Format.formatter -> t -> unit
val show : t -> string
val equal : t -> t -> bool
val compare : t -> t -> int
val initial : string -> t
end
end
module Setup : sig
(* Built-in predicates, see {!module:BuiltIn} *)
type builtins
(* Compilation flags, see {!module:Compile} *)
type flags
(* Handle to an elpi instance *)
type elpi
(** Initialize ELPI.
[init] must be called before invoking the parser.
@param flags for the compiler, see {!type:Compile.flags}
@param builtins the set of built-in predicates, eg {!val:Elpi.Builtin.std_builtins}
@param file_resolver maps a file name to an absolute path, if not specified the
options like [-I] or the env variable [TJPATH] serve as resolver. The
resolver returns the abslute file name
(possibly adjusting the unit extension). By default it fails.
See also {!val:Parse.std_resolver}.
@return a handle [elpi] to an elpi instance equipped with the given
[builtins] and where accumulate resolves files with the given
[file_resolver]. *)
val init :
?flags:flags ->
builtins:builtins list ->
?file_resolver:(?cwd:string -> unit:string -> unit -> string) ->
?legacy_parser:bool ->
unit ->
elpi
(** Usage string *)
val usage : string
(** Set tracing options.
[trace argv] can be called before {!module:Execute}.
returns options not known to the trace system.
*)
val trace : string list -> string list
(** Override default runtime error functions (they call exit) *)
val set_warn : (?loc:Ast.Loc.t -> string -> unit) -> unit
val set_error : (?loc:Ast.Loc.t -> string -> 'a) -> unit
val set_anomaly : (?loc:Ast.Loc.t -> string -> 'a) -> unit
val set_type_error : (?loc:Ast.Loc.t -> string -> 'a) -> unit
val set_std_formatter : Format.formatter -> unit
val set_err_formatter : Format.formatter -> unit
(** The legacy parser is an optional build dependency *)
val legacy_parser_available : bool
end
module Parse : sig
(** [program file_list] parses a list of files,
Raises Failure if the file does not exist. *)
val program : elpi:Setup.elpi ->
files:string list -> Ast.program
val program_from : elpi:Setup.elpi ->
loc:Ast.Loc.t -> Lexing.lexbuf -> Ast.program
(** [goal file_list] parses the query,
Raises Failure if the file does not exist. *)
val goal : elpi:Setup.elpi ->
loc:Ast.Loc.t -> text:string -> Ast.query
val goal_from : elpi:Setup.elpi ->
loc:Ast.Loc.t -> Lexing.lexbuf -> Ast.query
(** [resolve f] computes the full path of [f] as the parser would do (also)
for files recursively accumulated. Raises Failure if the file does not
exist. *)
val resolve_file : elpi:Setup.elpi -> ?cwd:string -> unit:string -> unit -> string
(** [std_resolver cwd paths ()] returns a resolver function that looks in cwd
and paths (relative to cwd, or absolute) *)
val std_resolver :
?cwd:string -> paths:string list -> unit ->
(?cwd:string -> unit:string -> unit -> string)
exception ParseError of Ast.Loc.t * string
end
module Data : sig
module StrMap : sig
include Map.S with type key = string
val show : (Format.formatter -> 'a -> unit) -> 'a t -> string
val pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit
end
(* what is assigned to the query variables *)
type term
(* goals suspended via the declare_constraint built-in *)
type constraints
(* user defined state (not goals) *)
type state
(* Pass it to function in the Pp module *)
type pretty_printer_context
(* a solution is an assignment map from query variables (name) to terms,
* plus the goals that were suspended and the user defined constraints *)
type 'a solution = {
assignments : term StrMap.t;
constraints : constraints;
state : state;
output : 'a;
pp_ctx : pretty_printer_context;
}
(* Hypothetical context *)
type hyp
type hyps = hyp list
end
module Compile : sig
module StrSet : sig
include Set.S with type elt = string
val show : t -> string
val pp : Format.formatter -> t -> unit
end
type flags = {
(* variables used in conditional compilation, that is :if clauses *)
defined_variables : StrSet.t;
(* debug: print intermediate data during the compilation phase *)
print_passes : bool;
(* debug: print compilation units *)
print_units : bool;
}
val default_flags : flags
val to_setup_flags : flags -> Setup.flags
type program
type 'a query
type 'a executable
exception CompileError of Ast.Loc.t option * string
(* basic API: Compile all program files in one go.
Note:
- programs are concatened and compiled together, as if their sources
were glued together
- unless explicitly delimited via `{` and `}`, shorten directives and
macros are globally visible
- the `accumulate` directive inserts `{` and `}` around the accumulated
code
*)
val program : ?flags:flags -> elpi:Setup.elpi -> Ast.program list -> program
(* separate compilation API: units are marshalable and closed w.r.t.
the host application (eg quotations are desugared).
Note:
- macros and shorten directives part of a unit are not visible in other
units
- macros declared as part of the builtins given to Setup.init are
visible in all units
- types, type abbreviations and mode declarations from all units are
merged at assembly time
*)
type compilation_unit
val unit : ?flags:flags -> elpi:Setup.elpi -> Ast.program -> compilation_unit
val assemble : ?flags:flags -> elpi:Setup.elpi -> compilation_unit list -> program
val extend : ?flags:flags -> base:program -> compilation_unit list -> program
(* then compile the query *)
val query : program -> Ast.query -> unit query
(* finally obtain the executable *)
val optimize : 'a query -> 'a executable
(** Runs a checker. Returns true if no errors were found.
See also Builtins.default_checker. *)
val static_check : checker:program -> 'a query -> bool
end
module Execute : sig
type 'a outcome = Success of 'a Data.solution | Failure | NoMoreSteps
(* Returns the first solution, if any, within the optional steps bound.
* Setting delay_outside_fragment (false by default) results in unification
* outside the pattern fragment to be delayed (behavior of Teyjus), rather
* than abort the execution (default behavior) *)
val once : ?max_steps:int -> ?delay_outside_fragment:bool ->
'a Compile.executable -> 'a outcome
(** Prolog's REPL.
[pp] is called on all solutions.
[more] is called to know if another solution has to be searched for. *)
val loop :
?delay_outside_fragment:bool ->
'a Compile.executable ->
more:(unit -> bool) -> pp:(float -> 'a outcome -> unit) -> unit
end
module Pp : sig
val term : Data.pretty_printer_context -> Format.formatter -> Data.term -> unit
val constraints : Data.pretty_printer_context -> Format.formatter -> Data.constraints -> unit
val state : Format.formatter -> Data.state -> unit
val program : Format.formatter -> 'a Compile.query -> unit
val goal : Format.formatter -> 'a Compile.query -> unit
module Ast : sig
val program : Format.formatter -> Ast.program -> unit
val query : Format.formatter -> Ast.query -> unit
end
end
(* ************************************************************************* *)
(* ************************* Extension API ********************************* *)
(* ************************************************************************* *)
(** This API lets one exchange with the host application opaque (primitive)
data such as integers or strings as well as algebraic data such OCaml's
ADT. No support for binders or unification variables at this point, see
the RawData module. *)
(** This module defines what embedding and readback functions are *)
module Conversion : sig
type ty_ast = TyName of string | TyApp of string * ty_ast * ty_ast list
type extra_goal = ..
type extra_goal +=
| Unify of Data.term * Data.term
type extra_goals = extra_goal list
type 'a embedding =
depth:int ->
Data.state -> 'a -> Data.state * Data.term * extra_goals
type 'a readback =
depth:int ->
Data.state -> Data.term -> Data.state * 'a * extra_goals
type 'a t = {
ty : ty_ast;
pp_doc : Format.formatter -> unit -> unit;
pp : Format.formatter -> 'a -> unit;
embed : 'a embedding; (* 'a -> term *)
readback : 'a readback; (* term -> 'a *)
}
exception TypeErr of ty_ast * int (*depth*) * Data.term (* a type error at data conversion time *)
end
(** This module defines what embedding and readback functions are
for datatypes that need the context of the program (hypothetical clauses and
constraints) *)
module ContextualConversion : sig
type ty_ast = Conversion.ty_ast = TyName of string | TyApp of string * ty_ast * ty_ast list
type ('a,'hyps,'constraints) embedding =
depth:int -> 'hyps -> 'constraints ->
Data.state -> 'a -> Data.state * Data.term * Conversion.extra_goals
type ('a,'hyps,'constraints) readback =
depth:int -> 'hyps -> 'constraints ->
Data.state -> Data.term -> Data.state * 'a * Conversion.extra_goals
type ('a,'h,'c) t = {
ty : ty_ast;
pp_doc : Format.formatter -> unit -> unit;
pp : Format.formatter -> 'a -> unit;
embed : ('a,'h,'c) embedding; (* 'a -> term *)
readback : ('a,'h,'c) readback; (* term -> 'a *)
}
type ('hyps,'constraints) ctx_readback =
depth:int -> Data.hyps -> Data.constraints ->
Data.state -> Data.state * 'hyps * 'constraints * Conversion.extra_goals
val unit_ctx : (unit,unit) ctx_readback
val raw_ctx : (Data.hyps,Data.constraints) ctx_readback
(* cast *)
val (!<) : ('a,unit,unit) t -> 'a Conversion.t
(* morphisms *)
val (!>) : 'a Conversion.t -> ('a,'hyps,'constraints) t
val (!>>) : ('a Conversion.t -> 'b Conversion.t) -> ('a,'hyps,'constraints) t -> ('b,'hyps,'constraints) t
val (!>>>) : ('a Conversion.t -> 'b Conversion.t -> 'c Conversion.t) -> ('a,'hyps,'constraints) t -> ('b,'hyps,'constraints) t -> ('c,'hyps,'constraints) t
end
(** Conversion for Elpi's built-in data types *)
module BuiltInData : sig
(** See {!module:Elpi.Builtin} for a few more *)
val int : int Conversion.t
val float : float Conversion.t
val string : string Conversion.t
val list : 'a Conversion.t -> 'a list Conversion.t
val loc : Ast.Loc.t Conversion.t
(* poly "A" is what one would use for, say, [type eq A -> A -> prop] *)
val poly : string -> Data.term Conversion.t
(* like poly "A" but "A" must be a closed term, e.g. no unification variables
and no variables bound by the program (context) *)
val closed : string -> (Data.term * int) Conversion.t
(* any is like poly "X" for X fresh *)
val any : Data.term Conversion.t
end
(** Declare data from the host application that is opaque (no syntax), like
int but not like list or pair *)
module OpaqueData : sig
type doc = string
type name = string
(** The [eq] function is used by unification. Limitation: unification of
* two cdata cannot alter the constraint store. This can be lifted in the
* future if there is user request.
*
* If the hconsed is true, then the [readback] function is
* automatically hashcons the data using the [eq] and [hash] functions.
*)
type 'a declaration = {
name : name;
doc : doc;
pp : Format.formatter -> 'a -> unit;
compare : 'a -> 'a -> int;
hash : 'a -> int;
hconsed : bool;
constants : (name * 'a) list; (* global constants of that type, eg "std_in" *)
}
val declare : 'a declaration -> 'a Conversion.t
end
(** Declare data from the host application that has syntax, like
list or pair but not like int. So far there is no support for
data with binder using this API. The type of each constructor is
described using a GADT so that the code to build or match the data
can be given the right type. Example: define the ADT for "option a"
{[
let option_declaration a = {
ty = TyApp("option",a.ty,[]);
doc = "The option type (aka Maybe)";
pp = (fun fmt -> function
| None -> Format.fprintf fmt "None"
| Some x -> Format.fprintf fmt "Some %a" a.pp x);
constructors = [
K("none","nothing in this case",
N, (* no arguments *)
B None, (* builder *)
M (fun ~ok ~ko -> function None -> ok | _ -> ko ())); (* matcher *)
K("some","something in this case",
A (a,N), (* one argument of type a *)
B (fun x -> Some x), (* builder *)
M (fun ~ok ~ko -> function Some x -> ok x | _ -> ko ())); (* matcher *)
]
}
]}
[K] stands for "constructor", [B] for "build", [M] for "match".
Variants [BS] and [MS] give read/write access to the state.
*)
module AlgebraicData : sig
type name = string
type doc = string
type ('match_stateful_t,'match_t, 't) match_t =
| M of (
ok:'match_t -> (* cont. to call passing subterms *)
ko:(unit -> Data.term) -> (* cont. to move to next constructor *)
't -> Data.term) (* match 't, pass its subterms to ~ok or call ~ko *)
| MS of (
ok:'match_stateful_t ->
ko:(Data.state -> Data.state * Data.term * Conversion.extra_goals) ->
't -> Data.state -> Data.state * Data.term * Conversion.extra_goals)
type ('build_stateful_t,'build_t) build_t =
| B of 'build_t
| BS of 'build_stateful_t
(** GADT for describing the type of the constructor:
- N is the terminator
- A(a,...) is an argument of type a (a is a Conversion.t)
- S stands for self
- C stands for container
*)
type ('stateful_builder,'builder, 'stateful_matcher, 'matcher, 'self, 'hyps,'constraints) constructor_arguments =
(* No arguments *)
| N : (Data.state -> Data.state * 'self, 'self, Data.state -> Data.state * Data.term * Conversion.extra_goals, Data.term, 'self, 'hyps,'constraints) constructor_arguments
(* An argument of type 'a *)
| A : 'a Conversion.t * ('bs,'b, 'ms,'m, 'self, 'hyps,'constraints) constructor_arguments -> ('a -> 'bs, 'a -> 'b, 'a -> 'ms, 'a -> 'm, 'self, 'hyps,'constraints) constructor_arguments
(* An argument of type 'a in context 'hyps,'constraints *)
| CA : ('a,'hyps,'constraints) ContextualConversion.t * ('bs,'b, 'ms,'m, 'self, 'hyps,'constraints) constructor_arguments -> ('a -> 'bs, 'a -> 'b, 'a -> 'ms, 'a -> 'm, 'self, 'hyps,'constraints) constructor_arguments
(* An argument of type 'self *)
| S : ('bs,'b, 'ms, 'm, 'self, 'hyps,'constraints) constructor_arguments -> ('self -> 'bs, 'self -> 'b, 'self -> 'ms, 'self -> 'm, 'self, 'hyps,'constraints) constructor_arguments
(* An argument of type `T 'self` for a constainer `T`, like a `list 'self`.
`S args` above is a shortcut for `C(fun x -> x, args)` *)
| C : (('self,'hyps,'constraints) ContextualConversion.t -> ('a,'hyps,'constraints) ContextualConversion.t) * ('bs,'b,'ms,'m,'self, 'hyps,'constraints) constructor_arguments -> ('a -> 'bs, 'a -> 'b, 'a -> 'ms,'a -> 'm, 'self, 'hyps,'constraints) constructor_arguments
type ('t,'h,'c) constructor =
K : name * doc *
('build_stateful_t,'build_t,'match_stateful_t,'match_t,'t,'h,'c) constructor_arguments * (* args ty *)
('build_stateful_t,'build_t) build_t *
('match_stateful_t,'match_t,'t) match_t
-> ('t,'h,'c) constructor
type ('t,'h,'c) declaration = {
ty : Conversion.ty_ast;
doc : doc;
pp : Format.formatter -> 't -> unit;
constructors : ('t,'h,'c) constructor list;
}
val declare : ('t,'h,'c) declaration -> ('t,'h,'c) ContextualConversion.t
end
(* Built-in predicates are implemented in ML using the following FFI.
*
* The ffi data type uses GADTs to let one describe the type of an OCaml
* function. Terms passed to the built-in predicate are then checked against
* and converted to their types before being passed to the OCaml code.
* The ffi data type is also used to generate the documentation of the
* built-in (Elpi code with comments).
*
* Example: built-in "div" taking two int and returning their division and
* remainder. {[
*
* Pred("div",
* In(int, "N",
* In(int, "M",
* Out(int, "D",
* Out(int, "R",
* Easy "division of N by M gives D with reminder R")))),
* (fun n m _ _ -> !: (n div m) +! (n mod n)))
* ]}
*
* In( type, documentation, ... ) declares an input of a given type.
* In the example above both "n" and "m" are declare as input, and
* as expected the OCaml code receives two inputs (n and m) of type
* int
* Out( type, documentation, ...) declares an input/output argument.
* The OCaml code receives an "int arg" (and not just an int).
* We will detail this later, for now lets ignore that.
* Easy( documentation ) just signals that the built-in does not alter the
* store of custom constraints
*
* The OCaml code has to produce a tuple of outputs, the convenience
* notations "!: x" and "x +! y" should be used to produce, respectively,
* the first output and any extra one ("!:" begins the list, "+!" continues
* it). In the example two outputs (division and reminder) are produced.
*
* In the ffi declaration above "int" is of type "int data" that is a
* record containing functions to inject/eject integers into terms.
* The function to eject (of_term) does not return an "int" but an
* "int arg" where "'a arg" can be one of
* Data of 'a | Flex of term | Discard
* For arguments that are described as In in the ffi, only the first
* constructor is allowed (i.e. if the user passes a term that is ejected
* as Flex or Discard a (fatal, run-time) type error is raised).
* For arguments described as Out all 3 cases are valid.
*
* Now let's go back to the two arguments the OCaml code discards.
* They are of type "int arg" as a consequence the OCaml code is
* made aware of the user passed. For example
* div 4 2 D _
* would result in the OCaml code being passed: 4, 2, Flex, Discard.
* In such a way the code can decide to not even produce the second
* output, since it is not requested (not very useful in this specific
* case). The notations "?:" and "+?" are like "!:" and "+!" but their
* argument is of type option, so one can output None in response to a
* Discard.
*
* The FFI unifies the outputs produces by the OCaml code with the
* terms provided by the user. It is always correct to produce all
* outputs (and ignore the corresponding arguments in OCaml).
* *)
module BuiltInPredicate : sig
exception No_clause (* signals logical Failure, i.e. demands backtrack *)
type name = string
type doc = string
type 'a oarg = Keep | Discard
type 'a ioarg = private Data of 'a | NoData
type ('function_type, 'inernal_outtype_in, 'internal_hyps, 'internal_constraints) ffi =
(* Arguemnts that are translated independently of the program context *)
| In : 't Conversion.t * doc * ('i, 'o,'h,'c) ffi -> ('t -> 'i,'o,'h,'c) ffi
| Out : 't Conversion.t * doc * ('i, 'o * 't option,'h,'c) ffi -> ('t oarg -> 'i,'o,'h,'c) ffi
| InOut : 't ioarg Conversion.t * doc * ('i, 'o * 't option,'h,'c) ffi -> ('t ioarg -> 'i,'o,'h,'c) ffi
(* Arguemnts that are translated looking at the program context *)
| CIn : ('t,'h,'c) ContextualConversion.t * doc * ('i, 'o,'h,'c) ffi -> ('t -> 'i,'o,'h,'c) ffi
| COut : ('t,'h,'c) ContextualConversion.t * doc * ('i, 'o * 't option,'h,'c) ffi -> ('t oarg -> 'i,'o,'h,'c) ffi
| CInOut : ('t ioarg,'h,'c) ContextualConversion.t * doc * ('i, 'o * 't option,'h,'c) ffi -> ('t ioarg -> 'i,'o,'h,'c) ffi
(* The easy case: all arguments are context independent *)
| Easy : doc -> (depth:int -> 'o, 'o, unit, unit) ffi
(* The advanced case: arguments are context dependent, here we provide the
context readback function *)
| Read : ('h,'c) ContextualConversion.ctx_readback * doc -> (depth:int -> 'h -> 'c -> Data.state -> 'o, 'o,'h,'c) ffi
| Full : ('h,'c) ContextualConversion.ctx_readback * doc -> (depth:int -> 'h -> 'c -> Data.state -> Data.state * 'o * Conversion.extra_goals, 'o,'h,'c) ffi
| VariadicIn : ('h,'c) ContextualConversion.ctx_readback * ('t,'h,'c) ContextualConversion.t * doc -> ('t list -> depth:int -> 'h -> 'c -> Data.state -> Data.state * 'o, 'o,'h,'c) ffi
| VariadicOut : ('h,'c) ContextualConversion.ctx_readback * ('t,'h,'c) ContextualConversion.t * doc -> ('t oarg list -> depth:int -> 'h -> 'c -> Data.state -> Data.state * ('o * 't option list option), 'o,'h,'c) ffi
| VariadicInOut : ('h,'c) ContextualConversion.ctx_readback * ('t ioarg,'h,'c) ContextualConversion.t * doc -> ('t ioarg list -> depth:int -> 'h -> 'c -> Data.state -> Data.state * ('o * 't option list option), 'o,'h,'c) ffi
type t = Pred : name * ('a,unit,'h,'c) ffi * 'a -> t
(** Tools for InOut arguments.
*
* InOut arguments need to be equipped with an 'a ioarg Conversion.t.
* The ioarg adaptor here maps variables to NoData and anything else to the
* to Data of the provided 'a Conversion.t.
*
* If the 'a is an atomic data type, eg int, then things are good.
* If the 'a is an algebraic data type then some more work has to be done
* in order to have a good implementation, but the type system cannot
* enforce it hence this documentation. Let's take the example of int option.
* The Conversion.t to be passed is [int ioarg option ioarg Conversion.t],
* that is, ioarg should wrap each type constructor. In this way the user
* can pass non-ground terms. Eg
* given term : X none some X some 3
* readback to: NoData Data None Data (Some NoData) Data (Some (Data 3))
*
* Alternatively the data type 'a must be able to represent unification
* variables, such as the raw terms, see [ioarg_any] below. It gives NoData
* if the user passed _ (Discard) and Data t for any other t including
* variables such as X (UnifVar).
*
* An example of an API taking advantage of this feature is
* pred typecheck i:term, o:ty, o:diagnostic
* that can be used to both check a term is well typed and backtrack if not
* typecheck T TY ok
* or assert a term is illtyped or to test weather it is illtyped
* typecheck T TY (error _), typecheck T TY Diagnostic
* The ML code can see in which case we are and for example optimize the
* first case by not even generating the error message (since error "message"
* would fail to unify with ok anyway) or the second one by not assigning TY.
*)
val mkData : 'a -> 'a ioarg
val ioargC : ('t,'h,'c) ContextualConversion.t -> ('t ioarg,'h,'c) ContextualConversion.t
val ioarg : 't Conversion.t -> 't ioarg Conversion.t
val ioargC_flex : ('t,'h,'c) ContextualConversion.t -> ('t ioarg,'h,'c) ContextualConversion.t
val ioarg_flex : 't Conversion.t -> 't ioarg Conversion.t
val ioarg_any : Data.term ioarg Conversion.t
module Notation : sig
(* Handy notation to construct the value generated by built-in predicates.
*
* "!" means the output is there, while "?" that it may not be.
*
* "?:" and "!:" begin the sequence of outputs, while "+?" and "+!"
* continue.
*
* Eg !: 3 +! 4 +? None +? (Some 5) -->
* ((((), Some 3), Some 4), None), Some 5
*)
val (?:) : 'a -> unit * 'a
val (!:) : 'a -> unit * 'a option
val (+?) : 'a -> 'b -> 'a * 'b
val (+!) : 'a -> 'b -> 'a * 'b option
end
end
(** Setup.init takes a list of declarations of data types and predicates,
plus some doc and eventually some Elpi code. All this constitutes the
"prelude", that is what is avaiable to an Elpi program *)
module BuiltIn : sig
(** Where to print the documentation. For the running example DocAbove
* generates
* % [div N M D R] division of N by M gives D with reminder R
* pred div i:int, i:int, o:int, o:int.
* while DocNext generates
* pred div % division of N by M gives D with reminder R
* i:int, % N
* i:int, % M
* o:int, % D
* o:int. % R
* The latter format it is useful to give longer doc for each argument. *)
type doc_spec = DocAbove | DocNext
(* When an elpi interpreter is set up one can pass a list of
* declarations that constitute the base environment in which
* programs run *)
type declaration =
(* Real OCaml code *)
| MLCode of BuiltInPredicate.t * doc_spec
(* Declaration of an OCaml data *)
| MLData : 'a Conversion.t -> declaration
| MLDataC : ('a,'h,'c) ContextualConversion.t -> declaration
(* Extra doc *)
| LPDoc of string
(* Sometimes you wrap OCaml code in regular predicates in order
* to implement the desired builtin *)
| LPCode of string
(** What is passed to [Setup.init] *)
val declare :
file_name:string -> declaration list -> Setup.builtins
(** Prints in LP syntax the "external" declarations.
* The file builtin.elpi is generated by calling this API on the
* declaration list from elpi_builtin.ml *)
val document_fmt : Format.formatter -> Setup.builtins -> unit
val document_file : ?header:string -> Setup.builtins -> unit
end
(** Commodity module to build a simple query
and extract the output from the solution found by Elpi.
Example: "foo data Output" where [data] has type [t] ([a] is [t Conversion.t])
and [Output] has type [v] ([b] is a [v Conversion.t]) can be described as:
{[
let q : (v * unit) t = Query {
predicate = "foo";
arguments = D(a, data,
Q(b, "Output",
N))
}
]}
Then [compile q] can be used to obtain the compiled query such that the
resulting solution has a fied output of type [(v * unit)]. Example:
{[
Query.compile q |> Compile.link |> Execute.once |> function
| Execute.Success { output } -> output
| _ -> ...
]} *)
module Query : sig
type name = string
type _ arguments =
| N : unit arguments
| D : 'a Conversion.t * 'a * 'x arguments -> 'x arguments
| Q : 'a Conversion.t * name * 'x arguments -> ('a * 'x) arguments
type 'x t = Query of { predicate : name; arguments : 'x arguments }
val compile : Compile.program -> Ast.Loc.t -> 'a t -> 'a Compile.query
end
(* ************************************************************************* *)
(* ********************* Advanced Extension API **************************** *)
(* ************************************************************************* *)
(** This API lets one access the low lever representation of terms in order
to exchange data with binders and unification variables with the host
application. It also lets one define quotations and extend the state
theraded by Elpi with custom data. *)
(** State is a collection of purely functional piece of data carried
by the interpreter. Such data is kept in sync with the backtracking, i.e.
changes made in a branch are lost if that branch fails.
It can be used to both store custom constraints to be manipulated by
custom solvers, or any other piece of data the host application may
need to use. *)
module State : sig
(** 'a MUST be purely functional, i.e. backtracking is implemented by using
* an old binding for 'a.
* This limitation can be lifted if there is user request. *)
type 'a component
val declare :
name:string ->
pp:(Format.formatter -> 'a -> unit) ->
init:(unit -> 'a) ->
(* run just before the goal is compiled (but after the program is) *)
start:('a -> 'a) ->
'a component
type t = Data.state
val get : 'a component -> t -> 'a
val set : 'a component -> t -> 'a -> t
(** Allowed to raise BuiltInPredicate.No_clause *)
val update : 'a component -> t -> ('a -> 'a) -> t
val update_return : 'a component -> t -> ('a -> 'a * 'b) -> t * 'b
end
(** Flexible data is for unification variables. One can use Elpi's unification
variables to represent the host equivalent, here the API the keep a link
between the two. *)
module FlexibleData : sig
(** key for Elpi's flexible data *)
module Elpi : sig
type t
val make : ?name:string -> Data.state -> Data.state * t
val get : name:string -> Data.state -> t option
val pp : Format.formatter -> t -> unit
val show : t -> string
val equal : t -> t -> bool
val hash : t -> int
end
module type Host = sig
type t
val compare : t -> t -> int
val pp : Format.formatter -> t -> unit
val show : t -> string
end
module Map : functor(Host : Host) -> sig
type t
val empty : t
val add : Elpi.t -> Host.t -> t -> t
val remove_elpi : Elpi.t -> t -> t
val remove_host : Host.t -> t -> t
val filter : (Host.t -> Elpi.t -> bool) -> t -> t
(* The eventual body at its depth *)
val fold : (Host.t -> Elpi.t -> Data.term option -> 'a -> 'a) -> t -> 'a -> 'a
val elpi : Host.t -> t -> Elpi.t
val host : Elpi.t -> t -> Host.t
val uvmap : t State.component
val pp : Format.formatter -> t -> unit
val show : t -> string
end
module type Show = sig
type t
val pp : Format.formatter -> t -> unit
val show : t -> string
end
(** Example from Hol-light + elpi:
{[
module UV2STV = FlexibleData.Map(struct
type t = int
let compare x y = x - y
let pp fmt i = Format.fprintf fmt "%d" i
let show = string_of_int
end)
let stv = ref 0
let incr_get r = incr r; !r
let record k state =
State.update_return UV2STV.uvmap state (fun m ->
try m, Stv (UV2STV.host k m)
with Not_found ->
let j = incr_get stv in
UV2STV.add k j m, Stv j)
(* The constructor name "uvar" is special and has to be used with the
following Conversion.t *)
let hol_pretype = AlgebraicData.declare {
ty = TyName "pretype";
doc = "The algebraic data type of pretypes";
pp = (fun fmt t -> ...);
constructors = [
...
K("uvar","",A(uvar,N),
BS (fun (k,_) state -> record k state),
M (fun ~ok ~ko _ -> ko ()))
]
}
]}
In this way an Elpi term containig a variable [X] twice gets read back
using [Stv i] for the same [i].
*)
val uvar : (Elpi.t * Data.term list) Conversion.t
end
(** Low level module for OpaqueData *)
module RawOpaqueData : sig
type name = string
type doc = string
type t
(** If the data_hconsed is true, then the [cin] function below will
automatically hashcons the data using the [eq] and [hash] functions. *)
type 'a declaration = 'a OpaqueData.declaration = {
name : name;
doc : doc;
pp : Format.formatter -> 'a -> unit;
compare : 'a -> 'a -> int;
hash : 'a -> int;
hconsed : bool;
constants : (name * 'a) list; (* global constants of that type, eg "std_in" *)
}
type 'a cdata = private {
cin : 'a -> Data.term;
isc : t -> bool;
cout: t -> 'a;
name : string;
}
val declare : 'a declaration -> 'a cdata * 'a Conversion.t
val pp : Format.formatter -> t -> unit
val show : t -> string
val equal : t -> t -> bool
val compare : t -> t -> int
val hash : t -> int
val name : t -> string
val hcons : t -> t
(* tests if two cdata have the same given type *)
val ty2 : 'a cdata -> t -> t -> bool
val morph1 : 'a cdata -> ('a -> 'a) -> t -> Data.term
val morph2 : 'a cdata -> ('a -> 'a -> 'a) -> t -> t -> Data.term
val map : 'a cdata -> 'b cdata -> ('a -> 'b) -> t -> Data.term
(* Raw builtin *)
val int : int cdata
val is_int : t -> bool
val to_int : t -> int
val of_int : int -> Data.term
val float : float cdata
val is_float : t -> bool
val to_float : t -> float
val of_float : float -> Data.term
val string : string cdata
val is_string : t -> bool
val to_string : t -> string
val of_string : string -> Data.term
val loc : Ast.Loc.t cdata
val is_loc : t -> bool
val to_loc : t -> Ast.Loc.t
val of_loc : Ast.Loc.t -> Data.term
end
(** This module exposes the low level representation of terms.
*
* The data type [term] is opaque and can only be accessed by using the
* [look] API that exposes a term [view]. The [look] view automatically
* substitutes assigned unification variables by their value. *)
module RawData : sig
type constant = int (** De Bruijn levels (not indexes):
the distance of the binder from the root.
Starts at 0 and grows for bound variables;
global constants have negative values. *)
type builtin
type term = Data.term
type view = private
(* Pure subterms *)
| Const of constant (* global constant or a bound var *)
| Lam of term (* lambda abstraction, i.e. x\ *)
| App of constant * term * term list (* application (at least 1 arg) *)
(* Optimizations *)
| Cons of term * term (* :: *)
| Nil (* [] *)
(* FFI *)
| Builtin of builtin * term list (* call to a built-in predicate *)
| CData of RawOpaqueData.t (* opaque data *)
(* Unassigned unification variables *)
| UnifVar of FlexibleData.Elpi.t * term list
(** Terms must be inspected after dereferencing their head.
If the resulting term is UVar then its uvar_body is such that
get_assignment uvar_body = None *)
val look : depth:int -> term -> view
(* to reuse a term that was looked at *)
val kool : view -> term
(** Smart constructors *)
val mkBound : constant -> term (* bound variable, i.e. >= 0 *)
val mkLam : term -> term
val mkCons : term -> term -> term
val mkNil : term
val mkDiscard : term
val mkCData : RawOpaqueData.t -> term
val mkUnifVar : FlexibleData.Elpi.t -> args:term list -> State.t -> term
(** Lower level smart constructors *)
val mkGlobal : constant -> term (* global constant, i.e. < 0 *)
val mkApp : constant -> term -> term list -> term
val mkAppL : constant -> term list -> term
val mkBuiltin : builtin -> term list -> term
val mkConst : constant -> term (* no check, works for globals and bound *)
val cmp_builtin : builtin -> builtin -> int
type hyp = {
hdepth : int;
hsrc : term
}
type hyps = hyp list
val of_hyp : Data.hyp -> hyp
val of_hyps : Data.hyp list -> hyps
type suspended_goal = {
context : hyps;
goal : int * term
}
val constraints : Data.constraints -> suspended_goal list
val no_constraints : Data.constraints
module Constants : sig
val declare_global_symbol : string -> constant
val show : constant -> string
val eqc : builtin (* = *)
val orc : constant (* ; *)
val andc : constant (* , *)
val rimplc : constant (* :- *)
val pic : constant (* pi *)
val sigmac : constant (* sigma *)
val implc : constant (* => *)
val cutc : builtin (* ! *)
(* LambdaProlog built-in data types are just instances of CData.