| 1 |
|
| 2 |
|
| 3 |
% File generated by elpi -document-builtins, do not edit |
| 4 |
|
| 5 |
% == Core builtins ===================================== |
| 6 |
|
| 7 |
% -- Logic -- |
| 8 |
|
| 9 |
func true. |
| 10 |
|
| 11 |
true. |
| 12 |
|
| 13 |
func fail. |
| 14 |
|
| 15 |
func false. |
| 16 |
|
| 17 |
|
| 18 |
% [X = T] unifies X with Y, possibly assigning unification |
| 19 |
% variables in X and Y. |
| 20 |
external func (=) -> A, A. |
| 21 |
|
| 22 |
% [pattern_matching T P] matches T against pattern P, only |
| 23 |
% variables in P are assigned. |
| 24 |
external func pattern_match A -> A. |
| 25 |
|
| 26 |
external func (pi) (func A). |
| 27 |
|
| 28 |
external func (sigma) (func A). |
| 29 |
|
| 30 |
kind int type. |
| 31 |
|
| 32 |
|
| 33 |
kind string type. |
| 34 |
|
| 35 |
|
| 36 |
kind float type. |
| 37 |
|
| 38 |
|
| 39 |
external symbol (;) (pred) -> (pred) -> (pred). |
| 40 |
|
| 41 |
(A ; _) :- A. |
| 42 |
|
| 43 |
(_ ; B) :- B. |
| 44 |
|
| 45 |
external symbol (:-) : (func) -> (func) -> (func) = "core". |
| 46 |
|
| 47 |
external symbol (:-) : (func) -> list (pred) -> (func) = "core". |
| 48 |
|
| 49 |
external symbol (,) : variadic (func) (func). |
| 50 |
|
| 51 |
external symbol uvar : A = "core". |
| 52 |
|
| 53 |
external symbol (as) : A -> A -> A = "core". |
| 54 |
|
| 55 |
external symbol (=>) : (pred) -> (func) -> (func) = "core". |
| 56 |
|
| 57 |
external symbol (=>) : list (pred) -> (func) -> (func) = "core". |
| 58 |
|
| 59 |
external symbol (==>) : (pred) -> (func) -> (func). |
| 60 |
|
| 61 |
external symbol (==>) : list (pred) -> (func) -> (func). |
| 62 |
|
| 63 |
% -- Control -- |
| 64 |
|
| 65 |
external func !. % The cut operator |
| 66 |
|
| 67 |
func not prop. |
| 68 |
|
| 69 |
not X :- X, !, fail. |
| 70 |
|
| 71 |
not _. |
| 72 |
|
| 73 |
% [declare_constraint C Key1 Key2...] declares C blocked |
| 74 |
% on Key1 Key2 ... (variables, or lists thereof). |
| 75 |
external func declare_constraint (func) -> any .. . |
| 76 |
|
| 77 |
% [print_constraints] prints all constraints |
| 78 |
external func print_constraints. |
| 79 |
|
| 80 |
% [halt ...] halts the program and print the terms |
| 81 |
external type halt variadic any (func). |
| 82 |
|
| 83 |
func stop. |
| 84 |
|
| 85 |
stop :- halt. |
| 86 |
|
| 87 |
% -- Evaluation -- |
| 88 |
|
| 89 |
:functional pred (is) o:A, i:A. |
| 90 |
|
| 91 |
X is Y :- calc Y X. |
| 92 |
|
| 93 |
% [calc Expr Out] unifies Out with the value of Expr. It can be used in |
| 94 |
% tandem with spilling, eg [f {calc (N + 1)}] |
| 95 |
external func calc A -> A. |
| 96 |
|
| 97 |
% --- Operators --- |
| 98 |
|
| 99 |
external symbol (-) : A -> A -> A = "1". |
| 100 |
|
| 101 |
external symbol (i-) : int -> int -> int = "1". |
| 102 |
|
| 103 |
external symbol (r-) : float -> float -> float = "1". |
| 104 |
|
| 105 |
external symbol (+) : float -> float -> float = "2". |
| 106 |
|
| 107 |
external symbol (+) : int -> int -> int = "1". |
| 108 |
|
| 109 |
external symbol (i+) : int -> int -> int = "1". |
| 110 |
|
| 111 |
external symbol (r+) : float -> float -> float = "1". |
| 112 |
|
| 113 |
external symbol (*) : float -> float -> float = "2". |
| 114 |
|
| 115 |
external symbol (*) : int -> int -> int = "1". |
| 116 |
|
| 117 |
external symbol (/) : float -> float -> float = "1". |
| 118 |
|
| 119 |
external symbol (mod) : int -> int -> int = "1". |
| 120 |
|
| 121 |
external symbol (div) : int -> int -> int = "1". |
| 122 |
|
| 123 |
external symbol (^) : string -> string -> string = "1". |
| 124 |
|
| 125 |
external symbol (~) : float -> float = "2". |
| 126 |
|
| 127 |
external symbol (~) : int -> int = "1". |
| 128 |
|
| 129 |
external symbol (i~) : int -> int = "1". |
| 130 |
|
| 131 |
external symbol (r~) : float -> float = "1". |
| 132 |
|
| 133 |
external symbol abs : float -> float = "2". |
| 134 |
|
| 135 |
external symbol abs : int -> int = "1". |
| 136 |
|
| 137 |
external symbol iabs : int -> int = "1". |
| 138 |
|
| 139 |
external symbol rabs : float -> float = "1". |
| 140 |
|
| 141 |
external symbol max : float -> float -> float = "2". |
| 142 |
|
| 143 |
external symbol max : int -> int -> int = "1". |
| 144 |
|
| 145 |
external symbol min : float -> float -> float = "2". |
| 146 |
|
| 147 |
external symbol min : int -> int -> int = "1". |
| 148 |
|
| 149 |
external symbol sqrt : float -> float = "1". |
| 150 |
|
| 151 |
external symbol sin : float -> float = "1". |
| 152 |
|
| 153 |
external symbol cos : float -> float = "1". |
| 154 |
|
| 155 |
external symbol arctan : float -> float = "1". |
| 156 |
|
| 157 |
external symbol ln : float -> float = "1". |
| 158 |
|
| 159 |
external symbol int_to_real : int -> float = "1". |
| 160 |
|
| 161 |
external symbol floor : float -> int = "1". |
| 162 |
|
| 163 |
external symbol ceil : float -> int = "1". |
| 164 |
|
| 165 |
external symbol truncate : float -> int = "1". |
| 166 |
|
| 167 |
external symbol size : string -> int = "1". |
| 168 |
|
| 169 |
external symbol chr : int -> string = "1". |
| 170 |
|
| 171 |
external symbol rhc : string -> int = "1". |
| 172 |
|
| 173 |
external symbol string_to_int : string -> int = "1". |
| 174 |
|
| 175 |
external symbol int_to_string : int -> string = "1". |
| 176 |
|
| 177 |
external symbol substring : string -> int -> int -> string = "1". |
| 178 |
|
| 179 |
external symbol real_to_string : float -> string = "1". |
| 180 |
|
| 181 |
% -- Arithmetic tests -- |
| 182 |
|
| 183 |
% [lt_ X Y] checks if X < Y. Works for string, int and float |
| 184 |
external func lt_ A, A. |
| 185 |
|
| 186 |
% [gt_ X Y] checks if X > Y. Works for string, int and float |
| 187 |
external func gt_ A, A. |
| 188 |
|
| 189 |
% [le_ X Y] checks if X =< Y. Works for string, int and float |
| 190 |
external func le_ A, A. |
| 191 |
|
| 192 |
% [ge_ X Y] checks if X >= Y. Works for string, int and float |
| 193 |
external func ge_ A, A. |
| 194 |
|
| 195 |
func (>) A, A. |
| 196 |
X > Y :- gt_ X Y. |
| 197 |
|
| 198 |
func (<) A, A. |
| 199 |
X < Y :- lt_ X Y. |
| 200 |
|
| 201 |
func (=<) A, A. |
| 202 |
X =< Y :- le_ X Y. |
| 203 |
|
| 204 |
func (>=) A, A. |
| 205 |
X >= Y :- ge_ X Y. |
| 206 |
|
| 207 |
func (i>) int, int. |
| 208 |
X i> Y :- gt_ X Y. |
| 209 |
|
| 210 |
func (i<) int, int. |
| 211 |
X i< Y :- lt_ X Y. |
| 212 |
|
| 213 |
func (i=<) int, int. |
| 214 |
X i=< Y :- le_ X Y. |
| 215 |
|
| 216 |
func (i>=) int, int. |
| 217 |
X i>= Y :- ge_ X Y. |
| 218 |
|
| 219 |
func (r>) float, float. |
| 220 |
X r> Y :- gt_ X Y. |
| 221 |
|
| 222 |
func (r<) float, float. |
| 223 |
X r< Y :- lt_ X Y. |
| 224 |
|
| 225 |
func (r=<) float, float. |
| 226 |
X r=< Y :- le_ X Y. |
| 227 |
|
| 228 |
func (r>=) float, float. |
| 229 |
X r>= Y :- ge_ X Y. |
| 230 |
|
| 231 |
func (s>) string, string. |
| 232 |
X s> Y :- gt_ X Y. |
| 233 |
|
| 234 |
func (s<) string, string. |
| 235 |
X s< Y :- lt_ X Y. |
| 236 |
|
| 237 |
func (s=<) string, string. |
| 238 |
X s=< Y :- le_ X Y. |
| 239 |
|
| 240 |
func (s>=) string, string. |
| 241 |
X s>= Y :- ge_ X Y. |
| 242 |
|
| 243 |
% -- Standard data types (supported in the FFI) -- |
| 244 |
|
| 245 |
kind list type -> type. |
| 246 |
|
| 247 |
external symbol (::) : X -> list X -> list X = "core". |
| 248 |
|
| 249 |
external symbol ([]) : list X = "core". |
| 250 |
|
| 251 |
% Boolean values: tt and ff since true and false are predicates |
| 252 |
kind bool type. |
| 253 |
external symbol tt : bool = "1". |
| 254 |
external symbol ff : bool = "1". |
| 255 |
|
| 256 |
% Pair: the constructor is pr, since ',' is for conjunction |
| 257 |
kind pair type -> type -> type. |
| 258 |
external symbol pr : A -> B -> pair A B = "1". |
| 259 |
|
| 260 |
func fst pair A B -> A. |
| 261 |
|
| 262 |
fst (pr A _) A. |
| 263 |
|
| 264 |
func snd pair A B -> B. |
| 265 |
|
| 266 |
snd (pr _ B) B. |
| 267 |
|
| 268 |
|
| 269 |
kind triple type -> type -> type -> type. |
| 270 |
type triple A -> B -> C -> triple A B C. |
| 271 |
|
| 272 |
func triple_1 triple A B C -> A. |
| 273 |
triple_1 (triple A _ _) A. |
| 274 |
|
| 275 |
func triple_2 triple A B C -> B. |
| 276 |
triple_2 (triple _ B _) B. |
| 277 |
|
| 278 |
func triple_3 triple A B C -> C. |
| 279 |
triple_3 (triple _ _ C) C. |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
% The option type (aka Maybe) |
| 284 |
kind option type -> type. |
| 285 |
external symbol none : option A = "1". |
| 286 |
external symbol some : A -> option A = "1". |
| 287 |
|
| 288 |
% Result of a comparison |
| 289 |
kind cmp type. |
| 290 |
external symbol eq : cmp = "1". |
| 291 |
external symbol lt : cmp = "1". |
| 292 |
external symbol gt : cmp = "1". |
| 293 |
|
| 294 |
% Used in builtin variants that return Coq's error rather than failing |
| 295 |
kind diagnostic type. |
| 296 |
external symbol ok : diagnostic = "1". % Success |
| 297 |
external symbol error : string -> diagnostic = "1". % Failure |
| 298 |
|
| 299 |
% == Elpi builtins ===================================== |
| 300 |
|
| 301 |
% [dprint ...] prints raw terms (debugging) |
| 302 |
external type dprint variadic any (func). |
| 303 |
|
| 304 |
% [print ...] prints terms |
| 305 |
external type print variadic any (func). |
| 306 |
|
| 307 |
% Deprecated, use trace.counter |
| 308 |
func counter string -> int. |
| 309 |
counter C N :- trace.counter C N. |
| 310 |
|
| 311 |
kind loc type. |
| 312 |
|
| 313 |
|
| 314 |
% [loc.fields Loc File StartChar StopChar Line LineStartsAtChar] Decomposes |
| 315 |
% a loc into its fields |
| 316 |
external func loc.fields loc -> string, int, int, int, int. |
| 317 |
|
| 318 |
% == Regular Expressions ===================================== |
| 319 |
|
| 320 |
% [rex.match Rex Subject] checks if Subject matches Rex. Matching is based |
| 321 |
% on OCaml's Str library |
| 322 |
external func rex.match string, string. |
| 323 |
|
| 324 |
% [rex.replace Rex Replacement Subject Out] Out is obtained by replacing all |
| 325 |
% occurrences of Rex with Replacement in Subject. See also OCaml's |
| 326 |
% Str.global_replace |
| 327 |
external func rex.replace string, string, string -> string. |
| 328 |
|
| 329 |
% [rex.split Rex Subject Out] Out is obtained by splitting Subject at all |
| 330 |
% occurrences of Rex. See also OCaml's Str.split |
| 331 |
external func rex.split string, string -> list string. |
| 332 |
|
| 333 |
% Deprecated, use rex.match |
| 334 |
func rex_match string, string. |
| 335 |
rex_match Rx S :- rex.match Rx S. |
| 336 |
|
| 337 |
% Deprecated, use rex.replace |
| 338 |
func rex_replace string, string, string -> string. |
| 339 |
rex_replace Rx R S O :- rex.replace Rx R S O. |
| 340 |
|
| 341 |
% Deprecated, use rex.split |
| 342 |
func rex_split string, string -> list string. |
| 343 |
rex_split Rx S L :- rex.split Rx S L. |
| 344 |
|
| 345 |
% == Elpi nonlogical builtins ===================================== |
| 346 |
|
| 347 |
% [var V ...] checks if the term V is a variable. When used with tree |
| 348 |
% arguments it relates an applied variable with its head and argument list. |
| 349 |
external func var -> any, any.. . |
| 350 |
|
| 351 |
|
| 352 |
% [prune V L] V is pruned to L (V is unified with a variable that only sees |
| 353 |
% the list of names L) |
| 354 |
:functional :external pred prune o:any, i:list any. |
| 355 |
|
| 356 |
% [distinct_names L] checks if L is a list of distinct names. If L is the |
| 357 |
% scope of a unification variable (its arguments, as per var predicate) then |
| 358 |
% distinct_names L checks that such variable is in the Miller pattern |
| 359 |
% fragment (L_\lambda) |
| 360 |
external func distinct_names list any. |
| 361 |
|
| 362 |
% [same_var V1 V2] checks if the two terms V1 and V2 are the same variable, |
| 363 |
% ignoring the arguments of the variables |
| 364 |
external func same_var A, A. |
| 365 |
|
| 366 |
% [same_term T1 T2] checks if the two terms T1 and T2 are syntactically |
| 367 |
% equal (no unification). It behaves differently than same_var since it |
| 368 |
% recursively compares the arguments of the variables |
| 369 |
external func same_term A, A. |
| 370 |
|
| 371 |
|
| 372 |
% Infix notation for same_term |
| 373 |
func (==) A, A. |
| 374 |
X == Y :- same_term X Y. |
| 375 |
|
| 376 |
% Unification without occur check. It can create infinite terms. |
| 377 |
:nooc |
| 378 |
func unsound_unif -> A, A. |
| 379 |
unsound_unif X X. |
| 380 |
|
| 381 |
|
| 382 |
% [cmp_term A B Cmp] Compares A and B. Only works if A and B are ground. |
| 383 |
external func cmp_term any, any -> cmp. |
| 384 |
|
| 385 |
% [name T ...] checks if T is a eigenvariable. When used with tree arguments |
| 386 |
% it relates an applied name with its head and argument list. |
| 387 |
external func name -> any, any.. . |
| 388 |
|
| 389 |
% [constant T ...] checks if T is a (global) constant. When used with tree |
| 390 |
% arguments it relates an applied constant with its head and argument list. |
| 391 |
external func constant -> any, any.. . |
| 392 |
|
| 393 |
external func names % generates the list of eigenvariable |
| 394 |
-> list any. % list of eigenvariables in order of age (young first) |
| 395 |
|
| 396 |
external func occurs % checks if the atom occurs in the term |
| 397 |
any, % an atom, that is a global constant or a bound name (aka eigenvariable) |
| 398 |
any. % a term |
| 399 |
|
| 400 |
% [closed_term T] unify T with a variable that has no eigenvariables in |
| 401 |
% scope |
| 402 |
external func closed_term -> any. |
| 403 |
|
| 404 |
% [ground_term T] Checks if T contains unification variables |
| 405 |
external func ground_term any. |
| 406 |
|
| 407 |
% [is_cdata T Ctype] checks if T is primitive of type Ctype, eg "int" |
| 408 |
external func is_cdata any -> string. |
| 409 |
|
| 410 |
func primitive? any, string. |
| 411 |
|
| 412 |
primitive? X S :- is_cdata X S. |
| 413 |
|
| 414 |
% [new_int N] unifies N with a different int every time it is called. Values |
| 415 |
% of N are guaranteed to be incresing. |
| 416 |
external func new_int -> int. |
| 417 |
|
| 418 |
% [findall_solution P L] finds all the solved instances of P and puts them |
| 419 |
% in L in the order in which they are found. Instances can contain |
| 420 |
% eigenvariables and unification variables. P may or may not be |
| 421 |
% instantiated. Instances should be found in L. |
| 422 |
|
| 423 |
external func findall_solutions prop -> list prop. |
| 424 |
|
| 425 |
% Holds data across bracktracking; can only contain closed terms |
| 426 |
kind safe type. |
| 427 |
|
| 428 |
|
| 429 |
% [new_safe Safe] creates a safe: a store that persists across backtracking |
| 430 |
external func new_safe -> safe. |
| 431 |
|
| 432 |
% [stash_in_safe Safe Data] stores Data in the Safe |
| 433 |
external func stash_in_safe safe, A. |
| 434 |
|
| 435 |
% [open_safe Safe Data] retrieves the Data stored in Safe |
| 436 |
external func open_safe safe -> list A. |
| 437 |
|
| 438 |
|
| 439 |
% [if C T E] picks the first success of C then runs T (never E). |
| 440 |
% if C has no success it runs E. |
| 441 |
func if (pred), (func), (func). |
| 442 |
if B T _ :- B, !, T. |
| 443 |
if _ _ E :- E. |
| 444 |
|
| 445 |
% [if2 C1 B1 C2 B2 E] like if but with 2 then branches (and one else branch). |
| 446 |
func if2 (pred), (func), (pred), (func), (func). |
| 447 |
if2 G1 P1 _ _ _ :- G1, !, P1. |
| 448 |
if2 _ _ G2 P2 _ :- G2, !, P2. |
| 449 |
if2 _ _ _ _ E :- !, E. |
| 450 |
|
| 451 |
% [random.init Seed] Initialize OCaml's PRNG with the given Seed |
| 452 |
external func random.init int. |
| 453 |
|
| 454 |
% [random.self_init] Initialize OCaml's PRNG with some seed |
| 455 |
external func random.self_init. |
| 456 |
|
| 457 |
% [random.int Bound N] unifies N with a random int between 0 and Bound |
| 458 |
% (excluded) |
| 459 |
external func random.int int -> int. |
| 460 |
|
| 461 |
#line 1 "builtin_stdlib.elpi" |
| 462 |
% == stdlib ======================================================= |
| 463 |
|
| 464 |
% Conventions: |
| 465 |
% - all predicates declare a mode with some input arguments, unless... |
| 466 |
% - predicates whose name ends with R are relations (work in any direction, |
| 467 |
% that is all arguments are in output mode) |
| 468 |
% - predicates whose name ends with ! do contain a cut and generate only the |
| 469 |
% first result |
| 470 |
% - all errors given by this library end up calling fatal-error[-w-data], |
| 471 |
% override it in order to handle them differently |
| 472 |
% - all debug prints by this library end up calling debug-print, override it |
| 473 |
% in order to handle them differently |
| 474 |
|
| 475 |
namespace std { |
| 476 |
|
| 477 |
func fatal-error string. |
| 478 |
:name "default-fatal-error" |
| 479 |
fatal-error Msg :- halt Msg. |
| 480 |
|
| 481 |
func fatal-error-w-data string, any. |
| 482 |
:name "default-fatal-error-w-data" |
| 483 |
fatal-error-w-data Msg Data :- halt Msg ":" Data. |
| 484 |
|
| 485 |
func debug-print string, any. |
| 486 |
:name "default-debug-print" |
| 487 |
debug-print Msg Data :- print Msg Data. |
| 488 |
|
| 489 |
% -- Errors, Debugging, Hacks -- |
| 490 |
|
| 491 |
func ignore-failure! prop ->. |
| 492 |
ignore-failure! P :- P, !. |
| 493 |
ignore-failure! _. |
| 494 |
|
| 495 |
func once prop ->. |
| 496 |
once P :- P, !. |
| 497 |
|
| 498 |
% [assert! C M] takes the first success of C or fails with message M |
| 499 |
func assert! prop, string. |
| 500 |
assert! Cond Msg :- (Cond ; fatal-error-w-data Msg Cond), !. |
| 501 |
|
| 502 |
% [assert-ok! C M] like assert! but the last argument of the predicate must |
| 503 |
% be a diagnostic that is printed after M in case it is not ok |
| 504 |
func assert-ok! (pred o:diagnostic), string. |
| 505 |
assert-ok! Cond Msg :- Cond Diagnostic, !, (Diagnostic = ok ; Diagnostic = error S, fatal-error-w-data Msg S), !. |
| 506 |
assert-ok! _ Msg :- fatal-error-w-data Msg "no diagnostic returned". |
| 507 |
|
| 508 |
% [spy P] traces the call to P, printing all success and the final failure |
| 509 |
pred spy (pred). |
| 510 |
spy P :- trace.counter "run" NR, if (not(NR = 0)) (debug-print "run=" NR) true, |
| 511 |
debug-print "----<<---- enter: " P, |
| 512 |
P, |
| 513 |
debug-print "---->>---- exit: " P. |
| 514 |
spy P :- debug-print "---->>---- fail: " P, fail. |
| 515 |
|
| 516 |
% [spy! P] traces the first call to P without leaving a choice point |
| 517 |
func spy! prop. |
| 518 |
spy! P :- trace.counter "run" NR, if (not(NR = 0)) (debug-print "run=" NR) true, |
| 519 |
debug-print "----<<---- enter: " P, |
| 520 |
P, |
| 521 |
debug-print "---->>---- exit: " P, !. |
| 522 |
spy! P :- debug-print "---->>---- fail: " P, fail. |
| 523 |
|
| 524 |
% to silence the type checker |
| 525 |
func unsafe-cast A -> B. |
| 526 |
:untyped unsafe-cast X X. |
| 527 |
|
| 528 |
% -- List processing -- |
| 529 |
|
| 530 |
func length list A -> int. |
| 531 |
length [_|L] N :- length L N1, N is N1 + 1. |
| 532 |
length [] 0. |
| 533 |
|
| 534 |
func rev list A -> list A. |
| 535 |
rev L RL :- rev-append L [] RL. |
| 536 |
|
| 537 |
func rev-append list A, list A -> list A. |
| 538 |
rev-append [X|XS] ACC R :- rev-append XS [X|ACC] R. |
| 539 |
rev-append [] L L. |
| 540 |
|
| 541 |
func rev.aux list A, list A -> list A. |
| 542 |
rev.aux L1 L2 L3 :- rev-append L1 L2 L3. |
| 543 |
|
| 544 |
func last list A -> A. |
| 545 |
last [] _ :- fatal-error "last on empty list". |
| 546 |
last [X] X :- !. |
| 547 |
last [_|XS] R :- last XS R. |
| 548 |
|
| 549 |
func append list A, list A -> list A. |
| 550 |
append [X|XS] L [X|L1] :- append XS L L1 . |
| 551 |
append [] L L . |
| 552 |
|
| 553 |
pred appendR -> list A, list A, list A. |
| 554 |
appendR [] L L. |
| 555 |
appendR [X|XS] L [X|L1] :- appendR XS L L1. |
| 556 |
|
| 557 |
func take int, list A -> list A. |
| 558 |
take 0 _ [] :- !. |
| 559 |
take N [X|XS] [X|L] :- !, N1 is N - 1, take N1 XS L. |
| 560 |
take _ _ _ :- fatal-error "take run out of list items". |
| 561 |
|
| 562 |
func take-last int, list A -> list A. |
| 563 |
take-last N L R :- |
| 564 |
length L M, |
| 565 |
D is M - N, |
| 566 |
drop D L R. |
| 567 |
|
| 568 |
func drop int, list A -> list A. |
| 569 |
drop 0 L L :- !. |
| 570 |
drop N [_|XS] L :- !, N1 is N - 1, drop N1 XS L. |
| 571 |
drop _ _ _ :- fatal-error "drop run out of list items". |
| 572 |
|
| 573 |
func drop-last int, list A -> list A. |
| 574 |
drop-last N L R :- |
| 575 |
length L M, I is M - N, take I L R. |
| 576 |
|
| 577 |
func split-at int, list A -> list A, list A. |
| 578 |
split-at 0 L [] L :- !. |
| 579 |
split-at N [X|XS] [X|LN] LM :- !, N1 is N - 1, split-at N1 XS LN LM. |
| 580 |
split-at _ _ _ _ :- fatal-error "split-at run out of list items". |
| 581 |
|
| 582 |
func fold list B, A, (func B, A -> A) -> A. |
| 583 |
fold [] A _ A. |
| 584 |
fold [X|XS] A F R :- F X A A1, fold XS A1 F R. |
| 585 |
|
| 586 |
func fold-right list B, A, (func B, A -> A) -> A. |
| 587 |
fold-right [] A _ A. |
| 588 |
fold-right [X|XS] A F R :- fold-right XS A F A', F X A' R. |
| 589 |
|
| 590 |
:index (1 1) |
| 591 |
func fold2 list C, list B, A, (func C, B, A -> A) -> A. |
| 592 |
fold2 [] [_|_] _ _ _ :- fatal-error "fold2 on lists of different length". |
| 593 |
fold2 [_|_] [] _ _ _ :- fatal-error "fold2 on lists of different length". |
| 594 |
fold2 [] [] A _ A. |
| 595 |
fold2 [X|XS] [Y|YS] A F R :- F X Y A A1, fold2 XS YS A1 F R. |
| 596 |
|
| 597 |
func map list A, (func A -> B) -> list B. |
| 598 |
map [] _ []. |
| 599 |
map [X|XS] F [Y|YS] :- F X Y, map XS F YS. |
| 600 |
|
| 601 |
func map-i list A, (func int, A -> B) -> list B. |
| 602 |
map-i L F R :- map-i.aux L 0 F R. |
| 603 |
|
| 604 |
func map-i.aux list A, int, (func int, A -> B) -> list B. |
| 605 |
map-i.aux [] _ _ []. |
| 606 |
map-i.aux [X|XS] N F [Y|YS] :- F N X Y, M is N + 1, map-i.aux XS M F YS. |
| 607 |
|
| 608 |
func map-filter list A, (func A -> B) -> list B. |
| 609 |
map-filter [] _ []. |
| 610 |
map-filter [X|XS] F [Y|YS] :- F X Y, !, map-filter XS F YS. |
| 611 |
map-filter [_|XS] F YS :- map-filter XS F YS. |
| 612 |
|
| 613 |
:index(1 1) |
| 614 |
func map2 list A, list B, (func A, B -> C) -> list C. |
| 615 |
map2 [] [_|_] _ _ :- fatal-error "map2 on lists of different length". |
| 616 |
map2 [_|_] [] _ _ :- fatal-error "map2 on lists of different length". |
| 617 |
map2 [] [] _ []. |
| 618 |
map2 [X|XS] [Y|YS] F [Z|ZS] :- F X Y Z, map2 XS YS F ZS. |
| 619 |
|
| 620 |
func map2-filter list A, list B, (func A, B -> C) -> list C. |
| 621 |
map2-filter [] [_|_] _ _ :- fatal-error "map2-filter on lists of different length". |
| 622 |
map2-filter [_|_] [] _ _ :- fatal-error "map2-filter on lists of different length". |
| 623 |
map2-filter [] [] _ []. |
| 624 |
map2-filter [X|XS] [Y|YS] F [Z|ZS] :- F X Y Z, !, map2-filter XS YS F ZS. |
| 625 |
map2-filter [_|XS] [_|YS] F ZS :- map2-filter XS YS F ZS. |
| 626 |
|
| 627 |
func map-ok list A, (func A -> B, diagnostic) -> list B, diagnostic. |
| 628 |
map-ok [X|L] P [Y|YS] S :- P X Y S0, if (S0 = ok) (map-ok L P YS S) (S = S0). |
| 629 |
map-ok [] _ [] ok. |
| 630 |
|
| 631 |
func fold-map list A, B, (func A, B -> C, B) -> list C, B. |
| 632 |
fold-map [] A _ [] A. |
| 633 |
fold-map [X|XS] A F [Y|YS] A2 :- F X A Y A1, fold-map XS A1 F YS A2. |
| 634 |
|
| 635 |
func omap option A, (func A -> B) -> option B. |
| 636 |
omap none _ none. |
| 637 |
omap (some X) F (some Y) :- F X Y. |
| 638 |
|
| 639 |
% [nth N L X] picks in X the N-th element of L (L must be of length > N) |
| 640 |
func nth int, list A -> A. |
| 641 |
nth 0 [X|_ ] R :- !, X = R. |
| 642 |
nth N [_|XS] R :- N > 0, !, N1 is N - 1, nth N1 XS R. |
| 643 |
nth N _ _ :- N < 0, !, fatal-error "nth got a negative index". |
| 644 |
nth _ _ _ :- fatal-error "nth run out of list items". |
| 645 |
|
| 646 |
% [lookup L K V] sees L as a map from K to V |
| 647 |
pred lookup list (pair A B), A -> B. |
| 648 |
lookup [pr X Y|_] X Y. |
| 649 |
lookup [_|LS] X Y :- lookup LS X Y. |
| 650 |
|
| 651 |
% [lookup! L K V] sees L as a map from K to V, stops at the first binding |
| 652 |
func lookup! list (pair A B), A -> B. |
| 653 |
lookup! [pr X Y|_] X Y :- !. |
| 654 |
lookup! [_|LS] X Y :- lookup! LS X Y. |
| 655 |
|
| 656 |
% [mem! L X] succeeds once if X occurs inside L |
| 657 |
func mem! list A, A ->. |
| 658 |
mem! [X|_] X :- !. |
| 659 |
mem! [_|L] X :- mem! L X. |
| 660 |
|
| 661 |
% [mem L X] succeeds every time if X occurs inside L |
| 662 |
pred mem list A -> A. |
| 663 |
mem [X|_] X. |
| 664 |
mem [_|L] X :- mem L X. |
| 665 |
|
| 666 |
func exists! list A, (pred A). |
| 667 |
exists! [X| _] P :- P X, !. |
| 668 |
exists! [_|XS] P :- exists! XS P. |
| 669 |
|
| 670 |
pred exists list A, (pred A). |
| 671 |
exists [X|_] P :- P X. |
| 672 |
exists [_|L] P :- exists L P. |
| 673 |
|
| 674 |
pred exists2 list A, list B, (pred A, B). |
| 675 |
exists2 [] [_|_] _ :- fatal-error "exists2 on lists of different length". |
| 676 |
exists2 [_|_] [] _ :- fatal-error "exists2 on lists of different length". |
| 677 |
exists2 [X|_] [Y|_] P :- P X Y. |
| 678 |
exists2 [_|L] [_|M] P :- exists2 L M P. |
| 679 |
|
| 680 |
func forall list A, (func A). |
| 681 |
forall [] _. |
| 682 |
forall [X|L] P :- P X, forall L P. |
| 683 |
|
| 684 |
func forall-ok list A, (func A -> diagnostic) -> diagnostic. |
| 685 |
forall-ok [X|L] P S :- P X S0, if (S0 = ok) (forall-ok L P S) (S = S0). |
| 686 |
forall-ok [] _ ok. |
| 687 |
|
| 688 |
func forall2 list A, list B, (func A, B). |
| 689 |
forall2 [] [_|_] _ :- fatal-error "forall2 on lists of different length". |
| 690 |
forall2 [_|_] [] _ :- fatal-error "forall2 on lists of different length". |
| 691 |
forall2 [X|XS] [Y|YS] P :- P X Y, forall2 XS YS P. |
| 692 |
forall2 [] [] _. |
| 693 |
|
| 694 |
func filter list A, (func A) -> list A. |
| 695 |
filter [] _ []. |
| 696 |
filter [X|L] P R :- if (P X) (R = X :: L1) (R = L1), filter L P L1. |
| 697 |
|
| 698 |
:index (1) |
| 699 |
func partition list A, (pred A) -> list A, list A. |
| 700 |
partition [] _ [] []. |
| 701 |
partition [X|XS] P [X|R] L :- P X, !, partition XS P R L. |
| 702 |
partition [X|XS] P R [X|L] :- partition XS P R L. |
| 703 |
|
| 704 |
|
| 705 |
func zip list A, list B -> list (pair A B). |
| 706 |
zip [_|_] [] _ :- fatal-error "zip on lists of different length". |
| 707 |
zip [] [_|_] _ :- fatal-error "zip on lists of different length". |
| 708 |
zip [X|LX] [Y|LY] [pr X Y|LR] :- zip LX LY LR. |
| 709 |
zip [] [] []. |
| 710 |
|
| 711 |
func unzip list (pair A B) -> list A, list B. |
| 712 |
unzip [] [] []. |
| 713 |
unzip [pr X Y|L] [X|LX] [Y|LY] :- unzip L LX LY. |
| 714 |
|
| 715 |
func flatten list (list A) -> list A. |
| 716 |
flatten [X|LS] R :- flatten LS LS', append X LS' R. |
| 717 |
flatten [] []. |
| 718 |
|
| 719 |
func null list A. |
| 720 |
null []. |
| 721 |
|
| 722 |
% [make N E L] L is [E, ..., E] and L has length N |
| 723 |
func list.make int, A -> list A. |
| 724 |
list.make 0 _ [] :- !. |
| 725 |
list.make N E [E|L] :- N' is N - 1, list.make N' E L. |
| 726 |
|
| 727 |
% [init N F L] L is [F 0, ..., F (N-1)] |
| 728 |
func list.init int, (func int -> A) -> list A. |
| 729 |
list.init N F L :- assert! (N >= 0) ("list.init: negative length"), list.init.aux 0 N F L. |
| 730 |
|
| 731 |
func list.init.aux int, int, (func int -> A) -> list A. |
| 732 |
list.init.aux N N _ [] :- !. |
| 733 |
list.init.aux N M F [E|L] :- F N E, N' is N + 1, list.init.aux N' M F L. |
| 734 |
|
| 735 |
func iota int -> list int. |
| 736 |
iota N L :- list.init N (x\y\ x = y) L. |
| 737 |
|
| 738 |
% [intersperse X L R] R is [L0, X, ..., X, LN] |
| 739 |
:index(_ 1) |
| 740 |
func intersperse A, list A -> list A. |
| 741 |
intersperse _ [] []. |
| 742 |
intersperse _ [X] [X] :- !. |
| 743 |
intersperse Sep [X|XS] [X,Sep|YS] :- intersperse Sep XS YS. |
| 744 |
|
| 745 |
% -- Misc -- |
| 746 |
|
| 747 |
func flip (func A, B), B, A. |
| 748 |
flip P X Y :- P Y X. |
| 749 |
|
| 750 |
func time (func) -> float. |
| 751 |
time P T :- gettimeofday Before, P, gettimeofday After, T is After - Before. |
| 752 |
|
| 753 |
func do! list prop. |
| 754 |
do! []. |
| 755 |
do! [P|PS] :- P, !, do! PS. |
| 756 |
|
| 757 |
:index(_ 1) |
| 758 |
:functional |
| 759 |
pred do-ok! o:diagnostic, i:list (pred o:diagnostic). |
| 760 |
do-ok! ok []. |
| 761 |
do-ok! S [P|PS] :- P S0, !, if (S0 = ok) (do-ok! S PS) (S = S0). |
| 762 |
|
| 763 |
pred lift-ok (pred), string -> diagnostic. |
| 764 |
lift-ok P Msg R :- (P, R = ok; R = error Msg). |
| 765 |
|
| 766 |
func spy-do! list prop. |
| 767 |
spy-do! L :- map L (x\y\y = spy x) L1, do! L1. |
| 768 |
|
| 769 |
func while-ok-do! diagnostic, list (pred -> diagnostic) -> diagnostic. |
| 770 |
while-ok-do! (error _ as E) _ E. |
| 771 |
while-ok-do! ok [] ok. |
| 772 |
while-ok-do! ok [P|PS] R :- P C, !, while-ok-do! C PS R. |
| 773 |
|
| 774 |
func any->string A -> string. |
| 775 |
any->string X Y :- term_to_string X Y. |
| 776 |
|
| 777 |
func max A, A -> A. |
| 778 |
max N M N :- N >= M, !. |
| 779 |
max _ M M. |
| 780 |
|
| 781 |
% [findall P L] L is the list [P1,P2,P3..] where each Pi is a solution to P. |
| 782 |
func findall prop -> list prop. |
| 783 |
findall P L :- findall_solutions P L. |
| 784 |
|
| 785 |
} |
| 786 |
|
| 787 |
% [std.string.concat Separator Items Result] concatenates Items |
| 788 |
% interspersing Separator |
| 789 |
external func std.string.concat string, list string -> string. |
| 790 |
|
| 791 |
% CAVEAT: the type parameter of std.string.map must be a closed term |
| 792 |
|
| 793 |
kind std.string.map type -> type. |
| 794 |
|
| 795 |
% [std.string.map.empty M] The empty map |
| 796 |
external func std.string.map.empty -> std.string.map A. |
| 797 |
|
| 798 |
% [std.string.map.mem S M] Checks if S is bound in M |
| 799 |
external func std.string.map.mem string, std.string.map A. |
| 800 |
|
| 801 |
% [std.string.map.add S V M M1] M1 is M where V is bound to S |
| 802 |
external func std.string.map.add string, A, |
| 803 |
std.string.map A -> std.string.map A. |
| 804 |
|
| 805 |
% [std.string.map.remove S M M1] M1 is M where S is unbound |
| 806 |
external func std.string.map.remove string, |
| 807 |
std.string.map A -> std.string.map A. |
| 808 |
|
| 809 |
% [std.string.map.find S M V] V is the binding of S in M |
| 810 |
external func std.string.map.find string, std.string.map A -> A. |
| 811 |
|
| 812 |
% [std.string.map.bindings M L] L is M transformed into an associative list |
| 813 |
external func std.string.map.bindings std.string.map A -> list (pair string A). |
| 814 |
|
| 815 |
% [std.string.map.filter M F M1] Filter M w.r.t. the predicate F |
| 816 |
external func std.string.map.filter std.string.map A, |
| 817 |
(string -> A -> prop) -> std.string.map A. |
| 818 |
|
| 819 |
% [std.string.map.map M F M1] Map M w.r.t. the predicate F |
| 820 |
external func std.string.map.map std.string.map A, |
| 821 |
(string -> A -> B -> prop) -> std.string.map B. |
| 822 |
|
| 823 |
% [std.string.map.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 824 |
external func std.string.map.fold std.string.map A, C, |
| 825 |
(string -> A -> C -> C -> prop) -> C. |
| 826 |
|
| 827 |
% CAVEAT: the type parameter of std.int.map must be a closed term |
| 828 |
|
| 829 |
kind std.int.map type -> type. |
| 830 |
|
| 831 |
% [std.int.map.empty M] The empty map |
| 832 |
external func std.int.map.empty -> std.int.map A. |
| 833 |
|
| 834 |
% [std.int.map.mem S M] Checks if S is bound in M |
| 835 |
external func std.int.map.mem int, std.int.map A. |
| 836 |
|
| 837 |
% [std.int.map.add S V M M1] M1 is M where V is bound to S |
| 838 |
external func std.int.map.add int, A, std.int.map A -> std.int.map A. |
| 839 |
|
| 840 |
% [std.int.map.remove S M M1] M1 is M where S is unbound |
| 841 |
external func std.int.map.remove int, std.int.map A -> std.int.map A. |
| 842 |
|
| 843 |
% [std.int.map.find S M V] V is the binding of S in M |
| 844 |
external func std.int.map.find int, std.int.map A -> A. |
| 845 |
|
| 846 |
% [std.int.map.bindings M L] L is M transformed into an associative list |
| 847 |
external func std.int.map.bindings std.int.map A -> list (pair int A). |
| 848 |
|
| 849 |
% [std.int.map.filter M F M1] Filter M w.r.t. the predicate F |
| 850 |
external func std.int.map.filter std.int.map A, |
| 851 |
(int -> A -> prop) -> std.int.map A. |
| 852 |
|
| 853 |
% [std.int.map.map M F M1] Map M w.r.t. the predicate F |
| 854 |
external func std.int.map.map std.int.map A, |
| 855 |
(int -> A -> B -> prop) -> std.int.map B. |
| 856 |
|
| 857 |
% [std.int.map.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 858 |
external func std.int.map.fold std.int.map A, C, |
| 859 |
(int -> A -> C -> C -> prop) -> C. |
| 860 |
|
| 861 |
% CAVEAT: the type parameter of std.loc.map must be a closed term |
| 862 |
|
| 863 |
kind std.loc.map type -> type. |
| 864 |
|
| 865 |
% [std.loc.map.empty M] The empty map |
| 866 |
external func std.loc.map.empty -> std.loc.map A. |
| 867 |
|
| 868 |
% [std.loc.map.mem S M] Checks if S is bound in M |
| 869 |
external func std.loc.map.mem loc, std.loc.map A. |
| 870 |
|
| 871 |
% [std.loc.map.add S V M M1] M1 is M where V is bound to S |
| 872 |
external func std.loc.map.add loc, A, std.loc.map A -> std.loc.map A. |
| 873 |
|
| 874 |
% [std.loc.map.remove S M M1] M1 is M where S is unbound |
| 875 |
external func std.loc.map.remove loc, std.loc.map A -> std.loc.map A. |
| 876 |
|
| 877 |
% [std.loc.map.find S M V] V is the binding of S in M |
| 878 |
external func std.loc.map.find loc, std.loc.map A -> A. |
| 879 |
|
| 880 |
% [std.loc.map.bindings M L] L is M transformed into an associative list |
| 881 |
external func std.loc.map.bindings std.loc.map A -> list (pair loc A). |
| 882 |
|
| 883 |
% [std.loc.map.filter M F M1] Filter M w.r.t. the predicate F |
| 884 |
external func std.loc.map.filter std.loc.map A, |
| 885 |
(loc -> A -> prop) -> std.loc.map A. |
| 886 |
|
| 887 |
% [std.loc.map.map M F M1] Map M w.r.t. the predicate F |
| 888 |
external func std.loc.map.map std.loc.map A, |
| 889 |
(loc -> A -> B -> prop) -> std.loc.map B. |
| 890 |
|
| 891 |
% [std.loc.map.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 892 |
external func std.loc.map.fold std.loc.map A, C, |
| 893 |
(loc -> A -> C -> C -> prop) -> C. |
| 894 |
|
| 895 |
kind std.string.set type. |
| 896 |
|
| 897 |
% [std.string.set.empty A] The empty set |
| 898 |
external func std.string.set.empty -> std.string.set. |
| 899 |
|
| 900 |
% [std.string.set.mem Elem A] Checks if Elem is in a |
| 901 |
external func std.string.set.mem string, std.string.set. |
| 902 |
|
| 903 |
% [std.string.set.add Elem A B] B is A union {Elem} |
| 904 |
external func std.string.set.add string, std.string.set -> std.string.set. |
| 905 |
|
| 906 |
% [std.string.set.remove Elem A B] B is A \ {Elem} |
| 907 |
external func std.string.set.remove string, |
| 908 |
std.string.set -> std.string.set. |
| 909 |
|
| 910 |
% [std.string.set.union A B X] X is A union B |
| 911 |
external func std.string.set.union std.string.set, |
| 912 |
std.string.set -> std.string.set. |
| 913 |
|
| 914 |
% [std.string.set.inter A B X] X is A intersection B |
| 915 |
external func std.string.set.inter std.string.set, |
| 916 |
std.string.set -> std.string.set. |
| 917 |
|
| 918 |
% [std.string.set.diff A B X] X is A \ B |
| 919 |
external func std.string.set.diff std.string.set, |
| 920 |
std.string.set -> std.string.set. |
| 921 |
|
| 922 |
% [std.string.set.equal A B] tests A and B for equality |
| 923 |
external func std.string.set.equal std.string.set, std.string.set. |
| 924 |
|
| 925 |
% [std.string.set.subset A B] tests if A is a subset of B |
| 926 |
external func std.string.set.subset std.string.set, std.string.set. |
| 927 |
|
| 928 |
% [std.string.set.elements M L] L is M transformed into list |
| 929 |
external func std.string.set.elements std.string.set -> list string. |
| 930 |
|
| 931 |
% [std.string.set.choose M X] X is an element of M |
| 932 |
external func std.string.set.choose std.string.set -> string. |
| 933 |
|
| 934 |
% [std.string.set.min M X] X is the smallest element of M |
| 935 |
external func std.string.set.min std.string.set -> string. |
| 936 |
|
| 937 |
% [std.string.set.max M X] X is the bigger of M |
| 938 |
external func std.string.set.max std.string.set -> string. |
| 939 |
|
| 940 |
% [std.string.set.cardinal M N] N is the number of elements of M |
| 941 |
external func std.string.set.cardinal std.string.set -> int. |
| 942 |
|
| 943 |
% [std.string.set.filter M F M1] Filter M w.r.t. the predicate F |
| 944 |
external func std.string.set.filter std.string.set, |
| 945 |
(string -> (func)) -> std.string.set. |
| 946 |
|
| 947 |
% [std.string.set.map M F M1] Map M w.r.t. the predicate F |
| 948 |
external func std.string.set.map std.string.set, |
| 949 |
(string -> string -> prop) -> std.string.set. |
| 950 |
|
| 951 |
% [std.string.set.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 952 |
external func std.string.set.fold std.string.set, A, |
| 953 |
(string -> A -> A -> prop) -> A. |
| 954 |
|
| 955 |
% [std.string.set.partition M F M1 M2] Partitions M w.r.t. the predicate F, |
| 956 |
% M1 is where F holds |
| 957 |
external func std.string.set.partition std.string.set, |
| 958 |
(string -> (func)) -> std.string.set, |
| 959 |
std.string.set. |
| 960 |
|
| 961 |
kind std.int.set type. |
| 962 |
|
| 963 |
% [std.int.set.empty A] The empty set |
| 964 |
external func std.int.set.empty -> std.int.set. |
| 965 |
|
| 966 |
% [std.int.set.mem Elem A] Checks if Elem is in a |
| 967 |
external func std.int.set.mem int, std.int.set. |
| 968 |
|
| 969 |
% [std.int.set.add Elem A B] B is A union {Elem} |
| 970 |
external func std.int.set.add int, std.int.set -> std.int.set. |
| 971 |
|
| 972 |
% [std.int.set.remove Elem A B] B is A \ {Elem} |
| 973 |
external func std.int.set.remove int, std.int.set -> std.int.set. |
| 974 |
|
| 975 |
% [std.int.set.union A B X] X is A union B |
| 976 |
external func std.int.set.union std.int.set, std.int.set -> std.int.set. |
| 977 |
|
| 978 |
% [std.int.set.inter A B X] X is A intersection B |
| 979 |
external func std.int.set.inter std.int.set, std.int.set -> std.int.set. |
| 980 |
|
| 981 |
% [std.int.set.diff A B X] X is A \ B |
| 982 |
external func std.int.set.diff std.int.set, std.int.set -> std.int.set. |
| 983 |
|
| 984 |
% [std.int.set.equal A B] tests A and B for equality |
| 985 |
external func std.int.set.equal std.int.set, std.int.set. |
| 986 |
|
| 987 |
% [std.int.set.subset A B] tests if A is a subset of B |
| 988 |
external func std.int.set.subset std.int.set, std.int.set. |
| 989 |
|
| 990 |
% [std.int.set.elements M L] L is M transformed into list |
| 991 |
external func std.int.set.elements std.int.set -> list int. |
| 992 |
|
| 993 |
% [std.int.set.choose M X] X is an element of M |
| 994 |
external func std.int.set.choose std.int.set -> int. |
| 995 |
|
| 996 |
% [std.int.set.min M X] X is the smallest element of M |
| 997 |
external func std.int.set.min std.int.set -> int. |
| 998 |
|
| 999 |
% [std.int.set.max M X] X is the bigger of M |
| 1000 |
external func std.int.set.max std.int.set -> int. |
| 1001 |
|
| 1002 |
% [std.int.set.cardinal M N] N is the number of elements of M |
| 1003 |
external func std.int.set.cardinal std.int.set -> int. |
| 1004 |
|
| 1005 |
% [std.int.set.filter M F M1] Filter M w.r.t. the predicate F |
| 1006 |
external func std.int.set.filter std.int.set, |
| 1007 |
(int -> (func)) -> std.int.set. |
| 1008 |
|
| 1009 |
% [std.int.set.map M F M1] Map M w.r.t. the predicate F |
| 1010 |
external func std.int.set.map std.int.set, |
| 1011 |
(int -> int -> prop) -> std.int.set. |
| 1012 |
|
| 1013 |
% [std.int.set.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 1014 |
external func std.int.set.fold std.int.set, A, |
| 1015 |
(int -> A -> A -> prop) -> A. |
| 1016 |
|
| 1017 |
% [std.int.set.partition M F M1 M2] Partitions M w.r.t. the predicate F, M1 |
| 1018 |
% is where F holds |
| 1019 |
external func std.int.set.partition std.int.set, |
| 1020 |
(int -> (func)) -> std.int.set, |
| 1021 |
std.int.set. |
| 1022 |
|
| 1023 |
kind std.loc.set type. |
| 1024 |
|
| 1025 |
% [std.loc.set.empty A] The empty set |
| 1026 |
external func std.loc.set.empty -> std.loc.set. |
| 1027 |
|
| 1028 |
% [std.loc.set.mem Elem A] Checks if Elem is in a |
| 1029 |
external func std.loc.set.mem loc, std.loc.set. |
| 1030 |
|
| 1031 |
% [std.loc.set.add Elem A B] B is A union {Elem} |
| 1032 |
external func std.loc.set.add loc, std.loc.set -> std.loc.set. |
| 1033 |
|
| 1034 |
% [std.loc.set.remove Elem A B] B is A \ {Elem} |
| 1035 |
external func std.loc.set.remove loc, std.loc.set -> std.loc.set. |
| 1036 |
|
| 1037 |
% [std.loc.set.union A B X] X is A union B |
| 1038 |
external func std.loc.set.union std.loc.set, std.loc.set -> std.loc.set. |
| 1039 |
|
| 1040 |
% [std.loc.set.inter A B X] X is A intersection B |
| 1041 |
external func std.loc.set.inter std.loc.set, std.loc.set -> std.loc.set. |
| 1042 |
|
| 1043 |
% [std.loc.set.diff A B X] X is A \ B |
| 1044 |
external func std.loc.set.diff std.loc.set, std.loc.set -> std.loc.set. |
| 1045 |
|
| 1046 |
% [std.loc.set.equal A B] tests A and B for equality |
| 1047 |
external func std.loc.set.equal std.loc.set, std.loc.set. |
| 1048 |
|
| 1049 |
% [std.loc.set.subset A B] tests if A is a subset of B |
| 1050 |
external func std.loc.set.subset std.loc.set, std.loc.set. |
| 1051 |
|
| 1052 |
% [std.loc.set.elements M L] L is M transformed into list |
| 1053 |
external func std.loc.set.elements std.loc.set -> list loc. |
| 1054 |
|
| 1055 |
% [std.loc.set.choose M X] X is an element of M |
| 1056 |
external func std.loc.set.choose std.loc.set -> loc. |
| 1057 |
|
| 1058 |
% [std.loc.set.min M X] X is the smallest element of M |
| 1059 |
external func std.loc.set.min std.loc.set -> loc. |
| 1060 |
|
| 1061 |
% [std.loc.set.max M X] X is the bigger of M |
| 1062 |
external func std.loc.set.max std.loc.set -> loc. |
| 1063 |
|
| 1064 |
% [std.loc.set.cardinal M N] N is the number of elements of M |
| 1065 |
external func std.loc.set.cardinal std.loc.set -> int. |
| 1066 |
|
| 1067 |
% [std.loc.set.filter M F M1] Filter M w.r.t. the predicate F |
| 1068 |
external func std.loc.set.filter std.loc.set, |
| 1069 |
(loc -> (func)) -> std.loc.set. |
| 1070 |
|
| 1071 |
% [std.loc.set.map M F M1] Map M w.r.t. the predicate F |
| 1072 |
external func std.loc.set.map std.loc.set, |
| 1073 |
(loc -> loc -> prop) -> std.loc.set. |
| 1074 |
|
| 1075 |
% [std.loc.set.fold M Acc F Acc1] fold M w.r.t. the predicate F |
| 1076 |
external func std.loc.set.fold std.loc.set, A, |
| 1077 |
(loc -> A -> A -> prop) -> A. |
| 1078 |
|
| 1079 |
% [std.loc.set.partition M F M1 M2] Partitions M w.r.t. the predicate F, M1 |
| 1080 |
% is where F holds |
| 1081 |
external func std.loc.set.partition std.loc.set, |
| 1082 |
(loc -> (func)) -> std.loc.set, |
| 1083 |
std.loc.set. |
| 1084 |
|
| 1085 |
#line 1 "builtin_map.elpi" |
| 1086 |
kind std.map type -> type -> type. |
| 1087 |
type std.map std.map.private.map K V -> (func K, K -> cmp) -> std.map K V. |
| 1088 |
|
| 1089 |
namespace std.map { |
| 1090 |
|
| 1091 |
% [make Eq Ltn M] builds an empty map M where keys are compared using Eq and Ltn |
| 1092 |
func make (func K, K -> cmp) -> std.map K V. |
| 1093 |
make Cmp (std.map private.empty Cmp). |
| 1094 |
|
| 1095 |
% [find K M V] looks in M for the value V associated to K |
| 1096 |
func find K, std.map K V -> V. |
| 1097 |
find K (std.map M Cmp) V :- private.find M Cmp K V. |
| 1098 |
|
| 1099 |
% [add K V M M1] M1 is M where K is bound to V |
| 1100 |
func add K, V, std.map K V -> std.map K V. |
| 1101 |
add K V (std.map M Cmp) (std.map M1 Cmp) :- private.add M Cmp K V M1. |
| 1102 |
|
| 1103 |
% [remove K M M1] M1 is M where K is unbound |
| 1104 |
func remove K, std.map K V -> std.map K V. |
| 1105 |
remove K (std.map M Cmp) (std.map M1 Cmp) :- private.remove M Cmp K M1. |
| 1106 |
|
| 1107 |
% [bindings M L] L is the key-value pairs in increasing order |
| 1108 |
func bindings std.map K V -> list (pair K V). |
| 1109 |
bindings (std.map M _) L :- private.bindings M [] L. |
| 1110 |
|
| 1111 |
namespace private { |
| 1112 |
|
| 1113 |
% Taken from OCaml's map.ml |
| 1114 |
kind map type -> type -> type. |
| 1115 |
type empty map K V. |
| 1116 |
type node map K V -> K -> V -> map K V -> int -> map K V. |
| 1117 |
|
| 1118 |
func height map K V -> int. |
| 1119 |
height empty 0. |
| 1120 |
height (node _ _ _ _ H) H. |
| 1121 |
|
| 1122 |
func create map K V, K, V, map K V -> map K V. |
| 1123 |
create L K V R (node L K V R H) :- H is {std.max {height L} {height R}} + 1. |
| 1124 |
|
| 1125 |
func bal map K V, K, V, map K V -> map K V. |
| 1126 |
bal L K V R T :- |
| 1127 |
height L HL, |
| 1128 |
height R HR, |
| 1129 |
HL2 is HL + 2, |
| 1130 |
HR2 is HR + 2, |
| 1131 |
bal.aux HL HR HL2 HR2 L K V R T. |
| 1132 |
|
| 1133 |
func bal.aux int, int, int, int, map K V, K, V, map K V -> map K V. |
| 1134 |
bal.aux HL _ _ HR2 (node LL LV LD LR _) X D R T :- |
| 1135 |
HL > HR2, {height LL} >= {height LR}, !, |
| 1136 |
create LL LV LD {create LR X D R} T. |
| 1137 |
bal.aux HL _ _ HR2 (node LL LV LD (node LRL LRV LRD LRR _) _) X D R T :- |
| 1138 |
HL > HR2, !, |
| 1139 |
create {create LL LV LD LRL} LRV LRD {create LRR X D R} T. |
| 1140 |
bal.aux _ HR HL2 _ L X D (node RL RV RD RR _) T :- |
| 1141 |
HR > HL2, {height RR} >= {height RL}, !, |
| 1142 |
create {create L X D RL} RV RD RR T. |
| 1143 |
bal.aux _ HR HL2 _ L X D (node (node RLL RLV RLD RLR _) RV RD RR _) T :- |
| 1144 |
HR > HL2, !, |
| 1145 |
create {create L X D RLL} RLV RLD {create RLR RV RD RR} T. |
| 1146 |
bal.aux _ _ _ _ L K V R T :- create L K V R T. |
| 1147 |
|
| 1148 |
func add map K V, (func K, K -> cmp), K, V -> map K V. |
| 1149 |
add empty _ K V T :- create empty K V empty T. |
| 1150 |
add (node _ X _ _ _ as M) Cmp X1 XD M1 :- Cmp X1 X E, add.aux E M Cmp X1 XD M1. |
| 1151 |
|
| 1152 |
func add.aux cmp, map K V, (func K, K -> cmp), K, V -> map K V. |
| 1153 |
add.aux eq (node L _ _ R H) _ X XD T :- T = node L X XD R H. |
| 1154 |
add.aux lt (node L V D R _) Cmp X XD T :- bal {add L Cmp X XD} V D R T. |
| 1155 |
add.aux gt (node L V D R _) Cmp X XD T :- bal L V D {add R Cmp X XD} T. |
| 1156 |
|
| 1157 |
func find map K V, (func K, K -> cmp), K -> V. |
| 1158 |
find (node L K1 V1 R _) Cmp K V :- Cmp K K1 E, find.aux E Cmp L R V1 K V. |
| 1159 |
|
| 1160 |
func find.aux cmp, (func K, K -> cmp), map K V, map K V, V, K -> V. |
| 1161 |
find.aux eq _ _ _ V _ V. |
| 1162 |
find.aux lt Cmp L _ _ K V :- find L Cmp K V. |
| 1163 |
find.aux gt Cmp _ R _ K V :- find R Cmp K V. |
| 1164 |
|
| 1165 |
func remove-min-binding map K V -> map K V. |
| 1166 |
remove-min-binding (node empty _ _ R _) R :- !. |
| 1167 |
remove-min-binding (node L V D R _) X :- bal {remove-min-binding L} V D R X. |
| 1168 |
|
| 1169 |
func min-binding map K V -> K, V. |
| 1170 |
min-binding (node empty V D _ _) V D :- !. |
| 1171 |
min-binding (node L _ _ _ _) V D :- min-binding L V D. |
| 1172 |
|
| 1173 |
func merge map K V, map K V -> map K V. |
| 1174 |
merge empty X X :- !. |
| 1175 |
merge X empty X :- !. |
| 1176 |
merge M1 M2 R :- |
| 1177 |
min-binding M2 X D, |
| 1178 |
bal M1 X D {remove-min-binding M2} R. |
| 1179 |
|
| 1180 |
func remove map K V, (func K, K -> cmp), K -> map K V. |
| 1181 |
remove empty _ _ empty :- !. |
| 1182 |
remove (node L V D R _) Cmp X M :- Cmp X V E, remove.aux E Cmp L R V D X M. |
| 1183 |
|
| 1184 |
func remove.aux cmp, (func K, K -> cmp), map K V, map K V, K, V, K -> map K V. |
| 1185 |
remove.aux eq _ L R _ _ _ M :- merge L R M. |
| 1186 |
remove.aux lt Cmp L R V D X M :- bal {remove L Cmp X} V D R M. |
| 1187 |
remove.aux gt Cmp L R V D X M :- bal L V D {remove R Cmp X} M. |
| 1188 |
|
| 1189 |
func bindings map K V, list (pair K V) -> list (pair K V). |
| 1190 |
bindings empty X X. |
| 1191 |
bindings (node L V D R _) X X1 :- |
| 1192 |
bindings L [pr V D|{bindings R X}] X1. |
| 1193 |
|
| 1194 |
|
| 1195 |
} % std.map.private |
| 1196 |
} % std.map |
| 1197 |
|
| 1198 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1199 |
|
| 1200 |
% Fast maps only work ground terms but do not perform occur check |
| 1201 |
|
| 1202 |
kind std.fmap type -> type -> type. |
| 1203 |
type std.fmap std.fmap.private.map K V -> (func K, K -> cmp) -> std.fmap K V. |
| 1204 |
|
| 1205 |
namespace std.fmap { |
| 1206 |
|
| 1207 |
% [make Eq Ltn M] builds an empty map M where keys are compared using Eq and Ltn |
| 1208 |
func make (func K, K -> cmp) -> std.fmap K V. |
| 1209 |
make Cmp (std.fmap private.empty Cmp). |
| 1210 |
|
| 1211 |
% [find K M V] looks in M for the value V associated to K |
| 1212 |
func find K, std.fmap K V -> V. |
| 1213 |
find K (std.fmap M Cmp) V :- |
| 1214 |
private.assert-ground K, |
| 1215 |
private.find M Cmp K V. |
| 1216 |
|
| 1217 |
% [add K V M M1] M1 is M where K is bound to V |
| 1218 |
func add K, V, std.fmap K V -> std.fmap K V. |
| 1219 |
add K V (std.fmap M Cmp) R :- |
| 1220 |
private.assert-ground K, |
| 1221 |
private.assert-ground V, |
| 1222 |
private.add M Cmp K V M1, |
| 1223 |
unsound_unif R (std.fmap M1 Cmp). |
| 1224 |
|
| 1225 |
% [remove K M M1] M1 is M where K is unbound |
| 1226 |
func remove K, std.fmap K V -> std.fmap K V. |
| 1227 |
remove K (std.fmap M Cmp) R :- |
| 1228 |
private.assert-ground K, |
| 1229 |
private.remove M Cmp K M1, |
| 1230 |
unsound_unif R (std.fmap M1 Cmp). |
| 1231 |
|
| 1232 |
% [bindings M L] L is the key-value pairs in increasing order |
| 1233 |
func bindings std.fmap K V -> list (pair K V). |
| 1234 |
bindings (std.fmap M _) L :- private.bindings M [] L. |
| 1235 |
|
| 1236 |
namespace private { |
| 1237 |
|
| 1238 |
func assert-ground A. |
| 1239 |
assert-ground A :- std.assert! (ground_term A) "std.fmap: not ground". |
| 1240 |
|
| 1241 |
% Taken from OCaml's map.ml |
| 1242 |
kind map type -> type -> type. |
| 1243 |
type empty map K V. |
| 1244 |
type node map K V -> K -> V -> map K V -> int -> map K V. |
| 1245 |
|
| 1246 |
func height map K V -> int. |
| 1247 |
height empty 0. |
| 1248 |
height (node _ _ _ _ H) H. |
| 1249 |
|
| 1250 |
func create map K V, K, V, map K V -> map K V. |
| 1251 |
create L K V R X :- |
| 1252 |
H is {std.max {height L} {height R}} + 1, |
| 1253 |
unsound_unif X (node L K V R H). |
| 1254 |
|
| 1255 |
func bal map K V, K, V, map K V -> map K V. |
| 1256 |
bal L K V R T :- |
| 1257 |
height L HL, |
| 1258 |
height R HR, |
| 1259 |
HL2 is HL + 2, |
| 1260 |
HR2 is HR + 2, |
| 1261 |
bal.aux HL HR HL2 HR2 L K V R T. |
| 1262 |
|
| 1263 |
func bal.aux int, int, int, int, map K V, K, V, map K V -> map K V. |
| 1264 |
bal.aux HL _ _ HR2 (node LL LV LD LR _) X D R T :- |
| 1265 |
HL > HR2, {height LL} >= {height LR}, !, |
| 1266 |
create LL LV LD {create LR X D R} T. |
| 1267 |
bal.aux HL _ _ HR2 (node LL LV LD (node LRL LRV LRD LRR _) _) X D R T :- |
| 1268 |
HL > HR2, !, |
| 1269 |
create {create LL LV LD LRL} LRV LRD {create LRR X D R} T. |
| 1270 |
bal.aux _ HR HL2 _ L X D (node RL RV RD RR _) T :- |
| 1271 |
HR > HL2, {height RR} >= {height RL}, !, |
| 1272 |
create {create L X D RL} RV RD RR T. |
| 1273 |
bal.aux _ HR HL2 _ L X D (node (node RLL RLV RLD RLR _) RV RD RR _) T :- |
| 1274 |
HR > HL2, !, |
| 1275 |
create {create L X D RLL} RLV RLD {create RLR RV RD RR} T. |
| 1276 |
bal.aux _ _ _ _ L K V R T :- create L K V R T. |
| 1277 |
|
| 1278 |
func add map K V, (func K, K -> cmp), K, V -> map K V. |
| 1279 |
add empty _ K V T :- create empty K V empty T. |
| 1280 |
add (node _ X _ _ _ as M) Cmp X1 XD M1 :- Cmp X1 X E, add.aux E M Cmp X1 XD M1. |
| 1281 |
|
| 1282 |
func add.aux cmp, map K V, (func K, K -> cmp), K, V -> map K V. |
| 1283 |
add.aux eq (node L _ _ R H) _ X XD T :- unsound_unif T (node L X XD R H). |
| 1284 |
add.aux lt (node L V D R _) Cmp X XD T :- bal {add L Cmp X XD} V D R T. |
| 1285 |
add.aux gt (node L V D R _) Cmp X XD T :- bal L V D {add R Cmp X XD} T. |
| 1286 |
|
| 1287 |
func find map K V, (func K, K -> cmp), K -> V. |
| 1288 |
find (node L K1 V1 R _) Cmp K V :- Cmp K K1 E, find.aux E Cmp L R V1 K V. |
| 1289 |
|
| 1290 |
func find.aux cmp, (func K, K -> cmp), map K V, map K V, V, K -> V. |
| 1291 |
find.aux eq _ _ _ V _ V. |
| 1292 |
find.aux lt Cmp L _ _ K V :- find L Cmp K V. |
| 1293 |
find.aux gt Cmp _ R _ K V :- find R Cmp K V. |
| 1294 |
|
| 1295 |
func remove-min-binding map K V -> map K V. |
| 1296 |
remove-min-binding (node empty _ _ R _) R :- !. |
| 1297 |
remove-min-binding (node L V D R _) X :- bal {remove-min-binding L} V D R X. |
| 1298 |
|
| 1299 |
func min-binding map K V -> K, V. |
| 1300 |
min-binding (node empty V D _ _) V D :- !. |
| 1301 |
min-binding (node L _ _ _ _) V D :- min-binding L V D. |
| 1302 |
|
| 1303 |
func merge map K V, map K V -> map K V. |
| 1304 |
merge empty X X :- !. |
| 1305 |
merge X empty X :- !. |
| 1306 |
merge M1 M2 R :- |
| 1307 |
min-binding M2 X D, |
| 1308 |
bal M1 X D {remove-min-binding M2} R. |
| 1309 |
|
| 1310 |
func remove map K V, (func K, K -> cmp), K -> map K V. |
| 1311 |
remove empty _ _ empty :- !. |
| 1312 |
remove (node L V D R _) Cmp X M :- Cmp X V E, remove.aux E Cmp L R V D X M. |
| 1313 |
|
| 1314 |
func remove.aux cmp, (func K, K -> cmp), map K V, map K V, K, V, K -> map K V. |
| 1315 |
remove.aux eq _ L R _ _ _ M :- merge L R M. |
| 1316 |
remove.aux lt Cmp L R V D X M :- bal {remove L Cmp X} V D R M. |
| 1317 |
remove.aux gt Cmp L R V D X M :- bal L V D {remove R Cmp X} M. |
| 1318 |
|
| 1319 |
func bindings map K V, list (pair K V) -> list (pair K V). |
| 1320 |
bindings empty X X. |
| 1321 |
bindings (node L V D R _) X X1 :- |
| 1322 |
bindings L [pr V D|{bindings R X}] X1. |
| 1323 |
|
| 1324 |
|
| 1325 |
} % std.fmap.private |
| 1326 |
} % std.fmap |
| 1327 |
|
| 1328 |
|
| 1329 |
#line 1 "builtin_set.elpi" |
| 1330 |
kind std.set type -> type. |
| 1331 |
type std.set std.set.private.set E -> (func E, E -> cmp) -> std.set E. |
| 1332 |
|
| 1333 |
namespace std.set { |
| 1334 |
|
| 1335 |
% [make Eq Ltn M] builds an empty set M where keys are compared using Eq and Ltn |
| 1336 |
func make (func E, E -> cmp) -> std.set E. |
| 1337 |
make Cmp (std.set private.empty Cmp). |
| 1338 |
|
| 1339 |
% [mem E M] looks if E is in M |
| 1340 |
func mem E, std.set E. |
| 1341 |
mem E (std.set M Cmp):- private.mem M Cmp E. |
| 1342 |
|
| 1343 |
% [add E M M1] M1 is M + {E} |
| 1344 |
func add E, std.set E -> std.set E. |
| 1345 |
add E (std.set M Cmp) (std.set M1 Cmp) :- private.add M Cmp E M1. |
| 1346 |
|
| 1347 |
% [remove E M M1] M1 is M - {E} |
| 1348 |
func remove E, std.set E -> std.set E. |
| 1349 |
remove E (std.set M Cmp) (std.set M1 Cmp) :- private.remove M Cmp E M1. |
| 1350 |
|
| 1351 |
% [cardinal S N] N is the number of elements of S |
| 1352 |
func cardinal std.set E -> int. |
| 1353 |
cardinal (std.set M _) N :- private.cardinal M N. |
| 1354 |
|
| 1355 |
func elements std.set E -> list E. |
| 1356 |
elements (std.set M _) L :- private.elements M [] L. |
| 1357 |
|
| 1358 |
namespace private { |
| 1359 |
|
| 1360 |
% Taken from OCaml's set.ml |
| 1361 |
kind set type -> type. |
| 1362 |
type empty set E. |
| 1363 |
type node set E -> E -> set E -> int -> set E. |
| 1364 |
|
| 1365 |
func height set E -> int. |
| 1366 |
height empty 0. |
| 1367 |
height (node _ _ _ H) H. |
| 1368 |
|
| 1369 |
func create set E, E, set E -> set E. |
| 1370 |
create L E R (node L E R H) :- H is {std.max {height L} {height R}} + 1. |
| 1371 |
|
| 1372 |
func bal set E, E, set E -> set E. |
| 1373 |
bal L E R T :- |
| 1374 |
height L HL, |
| 1375 |
height R HR, |
| 1376 |
HL2 is HL + 2, |
| 1377 |
HR2 is HR + 2, |
| 1378 |
bal.aux HL HR HL2 HR2 L E R T. |
| 1379 |
|
| 1380 |
func bal.aux int, int, int, int, set E, E, set E -> set E. |
| 1381 |
bal.aux HL _ _ HR2 (node LL LV LR _) X R T :- |
| 1382 |
HL > HR2, {height LL} >= {height LR}, !, |
| 1383 |
create LL LV {create LR X R} T. |
| 1384 |
bal.aux HL _ _ HR2 (node LL LV (node LRL LRV LRR _) _) X R T :- |
| 1385 |
HL > HR2, !, |
| 1386 |
create {create LL LV LRL} LRV {create LRR X R} T. |
| 1387 |
bal.aux _ HR HL2 _ L X (node RL RV RR _) T :- |
| 1388 |
HR > HL2, {height RR} >= {height RL}, !, |
| 1389 |
create {create L X RL} RV RR T. |
| 1390 |
bal.aux _ HR HL2 _ L X (node (node RLL RLV RLR _) RV RR _) T :- |
| 1391 |
HR > HL2, !, |
| 1392 |
create {create L X RLL} RLV {create RLR RV RR} T. |
| 1393 |
bal.aux _ _ _ _ L E R T :- create L E R T. |
| 1394 |
|
| 1395 |
func add set E, (func E, E -> cmp), E -> set E. |
| 1396 |
add empty _ E T :- create empty E empty T. |
| 1397 |
add (node L X R H) Cmp X1 S :- Cmp X1 X E, add.aux E Cmp L R X X1 H S. |
| 1398 |
|
| 1399 |
func add.aux cmp, (func E, E -> cmp), set E, set E, E, E, int -> set E. |
| 1400 |
add.aux eq _ L R X _ H (node L X R H). |
| 1401 |
add.aux lt Cmp L R E X _ T :- bal {add L Cmp X} E R T. |
| 1402 |
add.aux gt Cmp L R E X _ T :- bal L E {add R Cmp X} T. |
| 1403 |
|
| 1404 |
func mem set E, (func E, E -> cmp), E. |
| 1405 |
mem (node L K R _) Cmp E :- Cmp E K O, mem.aux O Cmp L R E. |
| 1406 |
|
| 1407 |
func mem.aux cmp, (func E, E -> cmp), set E, set E, E. |
| 1408 |
mem.aux eq _ _ _ _. |
| 1409 |
mem.aux lt Cmp L _ E :- mem L Cmp E. |
| 1410 |
mem.aux gt Cmp _ R E :- mem R Cmp E. |
| 1411 |
|
| 1412 |
func remove-min-binding set E -> set E. |
| 1413 |
remove-min-binding (node empty _ R _) R :- !. |
| 1414 |
remove-min-binding (node L E R _) X :- bal {remove-min-binding L} E R X. |
| 1415 |
|
| 1416 |
func min-binding set E -> E. |
| 1417 |
min-binding (node empty E _ _) E :- !. |
| 1418 |
min-binding (node L _ _ _) E :- min-binding L E. |
| 1419 |
|
| 1420 |
func merge set E, set E -> set E. |
| 1421 |
merge empty X X :- !. |
| 1422 |
merge X empty X :- !. |
| 1423 |
merge M1 M2 R :- |
| 1424 |
min-binding M2 X, |
| 1425 |
bal M1 X {remove-min-binding M2} R. |
| 1426 |
|
| 1427 |
func remove set E, (func E, E -> cmp), E -> set E. |
| 1428 |
remove empty _ _ empty. |
| 1429 |
remove (node L E R _) Cmp X M :- Cmp X E O, remove.aux O Cmp L R E X M. |
| 1430 |
|
| 1431 |
func remove.aux cmp, (func E, E -> cmp), set E, set E, E, E -> set E. |
| 1432 |
remove.aux eq _ L R _ _ M :- merge L R M. |
| 1433 |
remove.aux lt Cmp L R E X M :- bal {remove L Cmp X} E R M. |
| 1434 |
remove.aux gt Cmp L R E X M :- bal L E {remove R Cmp X} M. |
| 1435 |
|
| 1436 |
func cardinal set E -> int. |
| 1437 |
cardinal empty 0. |
| 1438 |
cardinal (node L _ R _) N :- N is {cardinal L} + 1 + {cardinal R}. |
| 1439 |
|
| 1440 |
func elements set E, list E -> list E. |
| 1441 |
elements empty X X. |
| 1442 |
elements (node L E R _) Acc X :- |
| 1443 |
elements L [E|{elements R Acc}] X. |
| 1444 |
|
| 1445 |
} % std.set.private |
| 1446 |
} % std.set |
| 1447 |
|
| 1448 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1449 |
|
| 1450 |
% Fast sets only work ground terms but do not perform occur check |
| 1451 |
|
| 1452 |
kind std.fset type -> type. |
| 1453 |
type std.fset std.fset.private.set E -> (func E, E -> cmp) -> std.fset E. |
| 1454 |
|
| 1455 |
namespace std.fset { |
| 1456 |
|
| 1457 |
% [make Eq Ltn M] builds an empty set M where keys are compared using Eq and Ltn |
| 1458 |
func make (func E, E -> cmp) -> std.fset E. |
| 1459 |
make Cmp (std.fset private.empty Cmp). |
| 1460 |
|
| 1461 |
% [mem E M] looks if E is in M |
| 1462 |
func mem E, std.fset E. |
| 1463 |
mem E (std.fset M Cmp):- private.mem M Cmp E. |
| 1464 |
|
| 1465 |
% [add E M M1] M1 is M + {E} |
| 1466 |
func add E, std.fset E -> std.fset E. |
| 1467 |
add E (std.fset M Cmp) R :- |
| 1468 |
private.assert-ground E, |
| 1469 |
private.add M Cmp E M1, |
| 1470 |
unsound_unif R (std.fset M1 Cmp). |
| 1471 |
|
| 1472 |
% [remove E M M1] M1 is M - {E} |
| 1473 |
func remove E, std.fset E -> std.fset E. |
| 1474 |
remove E (std.fset M Cmp) R :- |
| 1475 |
private.assert-ground E, |
| 1476 |
private.remove M Cmp E M1, |
| 1477 |
unsound_unif R (std.fset M1 Cmp). |
| 1478 |
|
| 1479 |
% [cardinal S N] N is the number of elements of S |
| 1480 |
func cardinal std.fset E -> int. |
| 1481 |
cardinal (std.fset M _) N :- private.cardinal M N. |
| 1482 |
|
| 1483 |
func elements std.fset E -> list E. |
| 1484 |
elements (std.fset M _) L :- private.elements M [] L. |
| 1485 |
|
| 1486 |
namespace private { |
| 1487 |
|
| 1488 |
func assert-ground A. |
| 1489 |
assert-ground A :- std.assert! (ground_term A) "std.fset: not ground". |
| 1490 |
|
| 1491 |
% Taken from OCaml's set.ml |
| 1492 |
kind set type -> type. |
| 1493 |
type empty set E. |
| 1494 |
type node set E -> E -> set E -> int -> set E. |
| 1495 |
|
| 1496 |
func height set E -> int. |
| 1497 |
height empty 0. |
| 1498 |
height (node _ _ _ H) H. |
| 1499 |
|
| 1500 |
func create set E, E, set E -> set E. |
| 1501 |
create L E R S :- |
| 1502 |
H is {std.max {height L} {height R}} + 1, |
| 1503 |
unsound_unif S (node L E R H). |
| 1504 |
|
| 1505 |
func bal set E, E, set E -> set E. |
| 1506 |
bal L E R T :- |
| 1507 |
height L HL, |
| 1508 |
height R HR, |
| 1509 |
HL2 is HL + 2, |
| 1510 |
HR2 is HR + 2, |
| 1511 |
bal.aux HL HR HL2 HR2 L E R T. |
| 1512 |
|
| 1513 |
func bal.aux int, int, int, int, set E, E, set E -> set E. |
| 1514 |
bal.aux HL _ _ HR2 (node LL LV LR _) X R T :- |
| 1515 |
HL > HR2, {height LL} >= {height LR}, !, |
| 1516 |
create LL LV {create LR X R} T. |
| 1517 |
bal.aux HL _ _ HR2 (node LL LV (node LRL LRV LRR _) _) X R T :- |
| 1518 |
HL > HR2, !, |
| 1519 |
create {create LL LV LRL} LRV {create LRR X R} T. |
| 1520 |
bal.aux _ HR HL2 _ L X (node RL RV RR _) T :- |
| 1521 |
HR > HL2, {height RR} >= {height RL}, !, |
| 1522 |
create {create L X RL} RV RR T. |
| 1523 |
bal.aux _ HR HL2 _ L X (node (node RLL RLV RLR _) RV RR _) T :- |
| 1524 |
HR > HL2, !, |
| 1525 |
create {create L X RLL} RLV {create RLR RV RR} T. |
| 1526 |
bal.aux _ _ _ _ L E R T :- create L E R T. |
| 1527 |
|
| 1528 |
func add set E, (func E, E -> cmp), E -> set E. |
| 1529 |
add empty _ E T :- create empty E empty T. |
| 1530 |
add (node L X R H) Cmp X1 S :- Cmp X1 X E, add.aux E Cmp L R X X1 H S. |
| 1531 |
|
| 1532 |
func add.aux cmp, (func E, E -> cmp), set E, set E, E, E, int -> set E. |
| 1533 |
add.aux eq _ L R X _ H S :- unsound_unif S (node L X R H). |
| 1534 |
add.aux lt Cmp L R E X _ T :- bal {add L Cmp X} E R T. |
| 1535 |
add.aux gt Cmp L R E X _ T :- bal L E {add R Cmp X} T. |
| 1536 |
|
| 1537 |
func mem set E, (func E, E -> cmp), E. |
| 1538 |
mem (node L K R _) Cmp E :- Cmp E K O, mem.aux O Cmp L R E. |
| 1539 |
|
| 1540 |
func mem.aux cmp, (func E, E -> cmp), set E, set E, E. |
| 1541 |
mem.aux eq _ _ _ _. |
| 1542 |
mem.aux lt Cmp L _ E :- mem L Cmp E. |
| 1543 |
mem.aux gt Cmp _ R E :- mem R Cmp E. |
| 1544 |
|
| 1545 |
func remove-min-binding set E -> set E. |
| 1546 |
remove-min-binding (node empty _ R _) R :- !. |
| 1547 |
remove-min-binding (node L E R _) X :- bal {remove-min-binding L} E R X. |
| 1548 |
|
| 1549 |
func min-binding set E -> E. |
| 1550 |
min-binding (node empty E _ _) E :- !. |
| 1551 |
min-binding (node L _ _ _) E :- min-binding L E. |
| 1552 |
|
| 1553 |
func merge set E, set E -> set E. |
| 1554 |
merge empty X X :- !. |
| 1555 |
merge X empty X :- !. |
| 1556 |
merge M1 M2 R :- |
| 1557 |
min-binding M2 X, |
| 1558 |
bal M1 X {remove-min-binding M2} R. |
| 1559 |
|
| 1560 |
func remove set E, (func E, E -> cmp), E -> set E. |
| 1561 |
remove empty _ _ empty. |
| 1562 |
remove (node L E R _) Cmp X M :- Cmp X E O, remove.aux O Cmp L R E X M. |
| 1563 |
|
| 1564 |
func remove.aux cmp, (func E, E -> cmp), set E, set E, E, E -> set E. |
| 1565 |
remove.aux eq _ L R _ _ M :- merge L R M. |
| 1566 |
remove.aux lt Cmp L R E X M :- bal {remove L Cmp X} E R M. |
| 1567 |
remove.aux gt Cmp L R E X M :- bal L E {remove R Cmp X} M. |
| 1568 |
|
| 1569 |
func cardinal set E -> int. |
| 1570 |
cardinal empty 0. |
| 1571 |
cardinal (node L _ R _) N :- N is {cardinal L} + 1 + {cardinal R}. |
| 1572 |
|
| 1573 |
func elements set E, list E -> list E. |
| 1574 |
elements empty X X. |
| 1575 |
elements (node L E R _) Acc X :- |
| 1576 |
elements L [E|{elements R Acc}] X. |
| 1577 |
|
| 1578 |
} % std.fset.private |
| 1579 |
} % std.fset |
| 1580 |
|
| 1581 |
|
| 1582 |
% == I/O builtins ===================================== |
| 1583 |
|
| 1584 |
% -- I/O -- |
| 1585 |
|
| 1586 |
kind in_stream type. |
| 1587 |
|
| 1588 |
external symbol std_in : in_stream = "1". |
| 1589 |
|
| 1590 |
kind out_stream type. |
| 1591 |
|
| 1592 |
external symbol std_err : out_stream = "1". |
| 1593 |
external symbol std_out : out_stream = "1". |
| 1594 |
|
| 1595 |
% [open_in FileName InStream] opens FileName for input |
| 1596 |
external func open_in string -> in_stream. |
| 1597 |
|
| 1598 |
% [open_out FileName OutStream] opens FileName for output |
| 1599 |
external func open_out string -> out_stream. |
| 1600 |
|
| 1601 |
% [open_append FileName OutStream] opens FileName for output in append mode |
| 1602 |
external func open_append string -> out_stream. |
| 1603 |
|
| 1604 |
% [close_in InStream] closes input stream InStream |
| 1605 |
external func close_in in_stream. |
| 1606 |
|
| 1607 |
% [close_out OutStream] closes output stream OutStream |
| 1608 |
external func close_out out_stream. |
| 1609 |
|
| 1610 |
% [output OutStream Data] writes Data to OutStream |
| 1611 |
external func output out_stream, string. |
| 1612 |
|
| 1613 |
% [flush OutStream] flush all output not yet finalized to OutStream |
| 1614 |
external func flush out_stream. |
| 1615 |
|
| 1616 |
% [input InStream Bytes Data] reads Bytes from InStream |
| 1617 |
external func input in_stream, int -> string. |
| 1618 |
|
| 1619 |
% [input_line InStream Line] reads a full line from InStream |
| 1620 |
external func input_line in_stream -> string. |
| 1621 |
|
| 1622 |
% [eof InStream] checks if no more data can be read from InStream |
| 1623 |
external func eof in_stream. |
| 1624 |
|
| 1625 |
% -- System -- |
| 1626 |
|
| 1627 |
% [gettimeofday T] sets T to the number of seconds elapsed since 1/1/1970 |
| 1628 |
external func gettimeofday -> float. |
| 1629 |
|
| 1630 |
% [getenv VarName Value] Like Sys.getenv |
| 1631 |
external func getenv string -> option string. |
| 1632 |
|
| 1633 |
% [system Command RetVal] executes Command and sets RetVal to the exit code |
| 1634 |
external func system string -> int. |
| 1635 |
|
| 1636 |
% -- Unix -- |
| 1637 |
|
| 1638 |
% gathers the standard file descriptors or a process |
| 1639 |
kind unix.process type. |
| 1640 |
external symbol unix.process : out_stream -> in_stream -> in_stream -> |
| 1641 |
unix.process = "1". |
| 1642 |
|
| 1643 |
% [unix.process.open Executable Arguments Environment P Diagnostic] OCaml's |
| 1644 |
% Unix.open_process_args_full. |
| 1645 |
% Note that the first argument is the executable name (as in argv[0]). |
| 1646 |
% If Executable is omitted it defaults to the first element of |
| 1647 |
% Arguments. |
| 1648 |
% Environment can be left unspecified, defaults to the current process |
| 1649 |
% environment. |
| 1650 |
% This API only works reliably since OCaml 4.12. |
| 1651 |
external func unix.process.open string, list string, |
| 1652 |
list string -> unix.process, diagnostic. |
| 1653 |
|
| 1654 |
% [unix.process.close P Diagnostic] OCaml's Unix.close_process_full |
| 1655 |
external func unix.process.close unix.process -> diagnostic. |
| 1656 |
|
| 1657 |
% -- Debugging -- |
| 1658 |
|
| 1659 |
% [term_to_string T S] prints T to S |
| 1660 |
external func term_to_string -> any, string. |
| 1661 |
|
| 1662 |
% == Elpi runtime builtins ===================================== |
| 1663 |
|
| 1664 |
% [trace.counter Name Value] reads the Value of a trace point Name |
| 1665 |
external func trace.counter string -> int. |
| 1666 |
|
| 1667 |
external func gc.get % Reads the current settings of the garbage collector. See also OCaml's Gc.control type documentation. |
| 1668 |
-> int, % MinorHeapSize |
| 1669 |
int, % MajorHeapIncrement |
| 1670 |
int, % SpaceOverhead |
| 1671 |
int, % Verbose |
| 1672 |
int, % MaxOverhead |
| 1673 |
int, % StackLimit |
| 1674 |
int, % AllocationPolicy |
| 1675 |
int. % WindowSize |
| 1676 |
|
| 1677 |
% [gc.set MinorHeapSize MajorHeapIncrement SpaceOverhead Verbose MaxOverhead |
| 1678 |
% StackLimit AllocationPolicy WindowSize] Writes the current settings of the |
| 1679 |
% garbage collector. Any parameter left unspecificed (eg _) is not changed. |
| 1680 |
% See also OCaml's Gc.control type documentation. |
| 1681 |
external func gc.set int, int, int, int, int, int, int, int. |
| 1682 |
|
| 1683 |
% [gc.minor] See OCaml's Gc.minor documentation. |
| 1684 |
external func gc.minor. |
| 1685 |
|
| 1686 |
% [gc.major] See OCaml's Gc.major documentation. |
| 1687 |
external func gc.major. |
| 1688 |
|
| 1689 |
% [gc.full] See OCaml's Gc.full_major documentation. |
| 1690 |
external func gc.full. |
| 1691 |
|
| 1692 |
% [gc.compact] See OCaml's Gc.compact documentation. |
| 1693 |
external func gc.compact. |
| 1694 |
|
| 1695 |
% [gc.stat MinorWords PromotedWords MajorWords MinorCollections |
| 1696 |
% MajorCollections HeapWords HeapChunks LiveWords LiveBlocks FreeWords |
| 1697 |
% FreeBlocks LargestFree Fragments Compactions TopHeapWords StackSize] See |
| 1698 |
% OCaml's Gc.stat documentation. |
| 1699 |
external func gc.stat -> float, float, float, int, int, int, int, int, |
| 1700 |
int, int, int, int, int, int, int, int. |
| 1701 |
|
| 1702 |
% [gc.quick-stat MinorWords PromotedWords MajorWords MinorCollections |
| 1703 |
% MajorCollections HeapWords HeapChunks Compactions TopHeapWords StackSize] |
| 1704 |
% See OCaml's Gc.quick_stat documentation. |
| 1705 |
external func gc.quick-stat -> float, float, float, int, int, int, int, |
| 1706 |
int, int, int. |
| 1707 |
|
| 1708 |
% == Lambda Prolog builtins ===================================== |
| 1709 |
|
| 1710 |
% -- Extra I/O -- |
| 1711 |
|
| 1712 |
% [open_string DataIn InStream] opens DataIn as an input stream |
| 1713 |
external func open_string string -> in_stream. |
| 1714 |
|
| 1715 |
% [lookahead InStream NextChar] peeks one byte from InStream |
| 1716 |
external func lookahead in_stream -> string. |
| 1717 |
|
| 1718 |
pred printterm out_stream, A. |
| 1719 |
|
| 1720 |
printterm S T :- term_to_string T T1, output S T1. |
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|