package logtk
Core types and algorithms for logic
Install
Dune Dependency
Authors
Maintainers
Sources
1.5.1.tar.gz
md5=cc320f66f10555c54822da624419e003
sha512=f8d5f7a5ae790bf0388d74261673803cf375f91f92f7b413b70db1ce5841ef55343a208f98727c8551d66f1840ab892f1c0c943a34861d14d79ce469b235a2f2
doc/src/logtk/Builtin.ml.html
Source file Builtin.ml
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
(* This file is free software, part of Logtk. See file "license" for more details. *) (** {1 Builtin Objects} *) module Fmt = CCFormat type t = | Not | And | Or | Imply | Equiv | Xor | Eq | Neq | HasType | True | False | Arrow | Wildcard | Multiset (* type of multisets *) | TType (* type of types *) | Prop | Term | ForallConst (** constant for simulating forall *) | ExistsConst (** constant for simulating exists *) | Grounding (** used for inst-gen *) | TyInt | TyRat | TyReal | Int of Z.t | Rat of Q.t | Real of string | Floor | Ceiling | Truncate | Round | Prec | Succ | Sum | Difference | Uminus | Product | Quotient | Quotient_e | Quotient_t | Quotient_f | Remainder_e | Remainder_t | Remainder_f | Is_int | Is_rat | To_int | To_rat | Less | Lesseq | Greater | Greatereq | Box_opaque (** hint not to open this formula *) | Pseudo_de_bruijn of int (** magic to embed De Bruijn indices in normal terms *) type t_ = t let to_int_ = function | Not -> 0 | And -> 1 | Or -> 2 | Imply -> 3 | Equiv -> 4 | Xor -> 5 | Eq -> 6 | Neq -> 7 | HasType -> 8 | False -> 10 | True -> 11 (* bigger than false *) | Arrow -> 12 | Wildcard -> 13 | Multiset -> 14 | TType -> 15 | Int _ -> 16 | Rat _ -> 17 | Prop -> 18 | Term -> 19 | TyRat -> 20 | TyInt -> 21 | Floor -> 22 | Ceiling -> 23 | Truncate -> 24 | Round -> 25 | Prec -> 26 | Succ -> 27 | Sum -> 28 | Difference -> 29 | Uminus -> 30 | Product -> 31 | Quotient -> 32 | Quotient_e -> 33 | Quotient_t -> 34 | Quotient_f -> 35 | Remainder_e -> 36 | Remainder_t -> 37 | Remainder_f -> 38 | Is_int -> 39 | Is_rat -> 40 | To_int -> 41 | To_rat -> 42 | Less -> 43 | Lesseq -> 44 | Greater -> 45 | Greatereq -> 46 | ForallConst -> 47 | ExistsConst -> 48 | Grounding -> 50 | Box_opaque -> 60 | TyReal -> 70 | Real _ -> 71 | Pseudo_de_bruijn _ -> 100 let compare a b = match a, b with | Int i, Int j -> Z.compare i j | Rat i, Rat j -> Q.compare i j | _ -> to_int_ a - to_int_ b let equal a b = compare a b = 0 let hash s = match s with | Int i -> Hash.combine2 1 (Z.hash i) | Rat r -> Hash.combine2 2 (Hash.string (Q.to_string r)) | c -> Hash.combine2 3 (Hashtbl.hash c) module Map = Iter.Map.Make(struct type t = t_ let compare = compare end) module Set = Iter.Set.Make(struct type t = t_ let compare = compare end) module Tbl = Hashtbl.Make(struct type t = t_ let equal = equal let hash = hash end) let is_int = function Int _ -> true | _ -> false let is_rat = function Rat _ -> true | _ -> false let is_numeric = function Int _ | Rat _ -> true | _ -> false let is_not_numeric x = not (is_numeric x) let is_arith = function | Int _ | Rat _ | Floor | Ceiling | Truncate | Round | Prec | Succ | Sum | Difference | Uminus | Product | Quotient | Quotient_e | Quotient_t | Quotient_f | Remainder_e | Remainder_t | Remainder_f | Is_int | Is_rat | To_int | To_rat | Less | Lesseq | Greater | Greatereq -> true | _ -> false let to_string s = match s with | Int n -> Z.to_string n | Rat n -> Q.to_string n | Real r -> r | Not -> "¬" | And -> "∧" | Or -> "∨" | Imply -> "⇒" | Equiv -> "≡" | Xor -> "<~>" | Eq -> "=" | Neq -> "≠" | HasType -> ":" | True -> "true" | False -> "false" | Arrow -> "→" | Wildcard -> "_" | Multiset -> "Ms" | TType -> "type" | Prop -> "prop" | Term -> "ι" | ForallConst -> "·∀" | ExistsConst -> "·∃" | Grounding -> "★" | TyInt -> "int" | TyRat -> "rat" | TyReal -> "real" | Floor -> "floor" | Ceiling -> "ceiling" | Truncate -> "truncate" | Round -> "round" | Prec -> "prec" | Succ -> "succ" | Sum -> "+" | Difference -> "-" | Uminus -> "uminus" | Product -> "×" | Quotient -> "/" | Quotient_e -> "quotient_e" | Quotient_t -> "quotient_t" | Quotient_f -> "quotient_f" | Remainder_e -> "remainder_e" | Remainder_t -> "remainder_t" | Remainder_f -> "remainder_f" | Is_int -> "is_int" | Is_rat -> "is_rat" | To_int -> "to_int" | To_rat -> "to_rat" | Less -> "<" | Lesseq -> "≤" | Greater -> ">" | Greatereq -> "≥" | Box_opaque -> "<box>" | Pseudo_de_bruijn i -> Printf.sprintf "db_%d" i let pp out s = Format.pp_print_string out (to_string s) type fixity = | Infix_binary | Infix_nary | Prefix let fixity = function | And | Or -> Infix_nary | Imply | Equiv | Xor | Eq | Neq | HasType | Sum | Difference | Product | Quotient | Quotient_e | Quotient_f | Quotient_t | Remainder_e | Remainder_t | Remainder_f | Less | Lesseq | Greater | Greatereq -> Infix_binary | _ -> Prefix let is_prefix o = fixity o = Prefix let is_infix o = match fixity o with Infix_nary | Infix_binary -> true | Prefix -> false let ty = function | Int _ -> `Int | Rat _ -> `Rat | _ -> `Other let mk_int s = Int s let of_int i = Int (Z.of_int i) let int_of_string s = Int (Z.of_string s) let mk_rat s = Rat s let of_rat i j = Rat (Q.of_ints i j) let rat_of_string s = Rat (Q.of_string s) let true_ = True let false_ = False let wildcard = Wildcard let and_ = And let or_ = Or let imply = Imply let equiv = Equiv let xor = Xor let not_ = Not let eq = Eq let neq = Neq let arrow = Arrow let has_type = HasType let tType = TType let multiset = Multiset let prop = Prop let term = Term let ty_int = TyInt let ty_rat = TyRat let grounding = Grounding module Tag = struct type t = | T_lia (** integer arith *) | T_lra (** rational arith *) | T_ho (** higher order *) | T_ext (** extensionality *) | T_ind (** induction *) | T_data (** datatypes *) | T_distinct (** distinct constants *) | T_ac of ID.t (** AC symbols *) let compare = Pervasives.compare let pp out = function | T_lia -> Fmt.string out "lia" | T_lra -> Fmt.string out "lra" | T_ho -> Fmt.string out "ho" | T_ext -> Fmt.string out "extensionality" | T_ind -> Fmt.string out "ind" | T_data -> Fmt.string out "data" | T_distinct -> Fmt.string out "distinct_constants" | T_ac id -> Fmt.fprintf out "(ac %a)" ID.pp_full id end module Arith = struct let floor = Floor let ceiling = Ceiling let truncate = Truncate let round = Round let prec = Prec let succ = Succ let sum = Sum let difference = Difference let uminus = Uminus let product = Product let quotient = Quotient let quotient_e = Quotient_e let quotient_t = Quotient_t let quotient_f = Quotient_f let remainder_e = Remainder_e let remainder_t = Remainder_t let remainder_f = Remainder_f let is_int = Is_int let is_rat = Is_rat let to_int = To_int let to_rat = To_rat let less = Less let lesseq = Lesseq let greater = Greater let greatereq = Greatereq end module TPTP = struct let to_string = function | Eq -> "=" | Neq -> "!=" | And -> "&" | Or -> "|" | Not -> "~" | Imply -> "=>" | Equiv -> "<=>" | Xor -> "<~>" | HasType -> ":" | True -> "$true" | False -> "$false" | Arrow -> ">" | Wildcard -> "$_" | TType -> "$tType" | Term -> "$i" | Prop -> "$o" | Multiset -> failwith "cannot print this symbol in TPTP" | ForallConst -> "!!" | ExistsConst -> "??" | Grounding -> "$$ground" | TyInt -> "$int" | TyRat -> "$rat" | TyReal -> "$real" | Int x -> Z.to_string x | Rat x -> Q.to_string x | Real r -> r | Floor -> "$floor" | Ceiling -> "$ceiling" | Truncate -> "$truncate" | Round -> "$round" | Prec -> "$prec" | Succ -> "$succ" | Sum -> "$sum" | Difference -> "$diff" | Uminus -> "$uminus" | Product -> "$product" | Quotient -> "$quotient" | Quotient_e -> "$quotient_e" | Quotient_t -> "$quotient_t" | Quotient_f -> "$quotient_f" | Remainder_e -> "$remainder_e" | Remainder_t -> "$remainder_t" | Remainder_f -> "$remainder_f" | Is_int -> "$is_int" | Is_rat -> "$is_rat" | To_int -> "$to_int" | To_rat -> "$to_rat" | Less -> "$less" | Lesseq -> "$lesseq" | Greater -> "$greater" | Greatereq -> "$greatereq" | Box_opaque -> "$$box" | Pseudo_de_bruijn i -> Printf.sprintf "$$db_%d" i let pp out b = CCFormat.string out (to_string b) exception NotABuiltin let of_string_exn = function | "$true" -> True | "$false" -> False | "$_" -> Wildcard | "$tType" -> TType | "$i" -> Term | "$o" -> Prop | "!!" -> ForallConst | "??" -> ExistsConst | "$int" -> TyInt | "$rat" -> TyRat | "$floor" -> Floor | "$ceiling" -> Ceiling | "$truncate" -> Truncate | "$round" -> Round | "$prec" -> Prec | "$succ" -> Succ | "$sum" -> Sum | "$difference" -> Difference | "$uminus" -> Uminus | "$product" -> Product | "$quotient" -> Quotient | "$quotient_e" -> Quotient_e | "$quotient_t" -> Quotient_t | "$quotient_f" -> Quotient_f | "$remainder_e" -> Remainder_e | "$remainder_t" -> Remainder_t | "$remainder_f" -> Remainder_f | "$is_int" -> Is_int | "$is_rat" -> Is_rat | "$to_int" -> To_int | "$to_rat" -> To_rat | "$less" -> Less | "$lesseq" -> Lesseq | "$greater" -> Greater | "$greatereq" -> Greatereq | _ -> raise NotABuiltin let fixity = function | And | Or -> Infix_nary | Imply | Equiv | Xor | Eq | Neq | HasType -> Infix_binary | _ -> Prefix let is_prefix o = fixity o = Prefix let is_infix o = match fixity o with Infix_nary | Infix_binary -> true | Prefix -> false let of_string b = try Some (of_string_exn b) with NotABuiltin -> None (* TODO add the other ones *) let connectives = Set.of_seq (Iter.of_list [ and_; or_; equiv; imply; ]) let is_connective = function | Int _ | Rat _ -> false | _ -> true end module ArithOp = struct exception TypeMismatch of string (** This exception is raised when Arith functions are called on non-numeric values (Cst). *) (* helper to raise errors *) let _ty_mismatch fmt = CCFormat.ksprintf ~f:(fun msg -> raise (TypeMismatch msg)) fmt let sign = function | Int n -> Z.sign n | Rat n -> Q.sign n | s -> _ty_mismatch "cannot compute sign of symbol %a" pp s type arith_view = [ `Int of Z.t | `Rat of Q.t | `Other of t ] let view = function | Int i -> `Int i | Rat n -> `Rat n | s -> `Other s let parse_num s = if String.contains s '/' then mk_rat (Q.of_string s) else mk_int (Z.of_string s) let one_i = mk_int Z.one let zero_i = mk_int Z.zero let one_rat = mk_rat Q.one let zero_rat = mk_rat Q.zero let zero_of_ty = function | `Rat -> zero_rat | `Int -> zero_i let one_of_ty = function | `Rat -> one_rat | `Int -> one_i let is_zero = function | Int n -> Z.sign n = 0 | Rat n -> Q.sign n = 0 | s -> _ty_mismatch "not a number: %a" pp s let is_one = function | Int n -> Z.equal n Z.one | Rat n -> Q.equal n Q.one | s -> _ty_mismatch "not a number: %a" pp s let is_minus_one = function | Int n -> Z.equal n Z.minus_one | Rat n -> Q.equal n Q.minus_one | s -> _ty_mismatch "not a number: %a" pp s let floor s = match s with | Int _ -> s | Rat n -> mk_int (Q.to_bigint n) | s -> _ty_mismatch "not a numeric constant: %a" pp s let ceiling s = match s with | Int _ -> s | Rat _ -> failwith "Q.ceiling: not implemented" (* TODO *) | s -> _ty_mismatch "not a numeric constant: %a" pp s let truncate s = match s with | Int _ -> s | Rat n when Q.sign n >= 0 -> mk_int (Q.to_bigint n) | Rat _ -> failwith "Q.truncate: not implemented" (* TODO *) | s -> _ty_mismatch "not a numeric constant: %a" pp s let round s = match s with | Int _ -> s | Rat _ -> failwith "Q.round: not implemented" (* TODO *) | s -> _ty_mismatch "not a numeric constant: %a" pp s let prec s = match s with | Int n -> mk_int Z.(n - one) | Rat n -> mk_rat Q.(n - one) | s -> _ty_mismatch "not a numeric constant: %a" pp s let succ s = match s with | Int n -> mk_int Z.(n + one) | Rat n -> mk_rat Q.(n + one) | s -> _ty_mismatch "not a numeric constant: %a" pp s let err2_ s1 s2 = match s1, s2 with | Int _, Rat _ | Rat _, Int _ -> _ty_mismatch "incompatible numeric types: %a and %a" pp s1 pp s2 | _ -> _ty_mismatch "not numeric constants: %a, %a" pp s1 pp s2 let sum s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int Z.(n1 + n2) | Rat n1, Rat n2 -> mk_rat Q.(n1 + n2) | _ -> err2_ s1 s2 let difference s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int Z.(n1 - n2) | Rat n1, Rat n2 -> mk_rat Q.(n1 - n2) | _ -> err2_ s1 s2 let uminus s = match s with | Int n -> mk_int (Z.neg n) | Rat n -> mk_rat (Q.neg n) | s -> _ty_mismatch "not a numeric constant: %a" pp s let product s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int Z.(n1 * n2) | Rat n1, Rat n2 -> mk_rat Q.(n1 * n2) | _ -> err2_ s1 s2 let quotient s1 s2 = match s1, s2 with | Int n1, Int n2 -> let q, r = Z.div_rem n1 n2 in if Z.sign r = 0 then mk_int q else _ty_mismatch "non-exact integral division: %a / %a" pp s1 pp s2 | Rat n1, Rat n2 -> if Q.sign n2 = 0 then raise Division_by_zero else mk_rat (Q.div n1 n2) | _ -> err2_ s1 s2 let quotient_e s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.div n1 n2) | _ -> if sign s2 > 0 then floor (quotient s1 s2) else ceiling (quotient s1 s2) let quotient_t s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.div n1 n2) | _ -> truncate (quotient s1 s2) let quotient_f s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.div n1 n2) | _ -> floor (quotient s1 s2) let remainder_e s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.rem n1 n2) | _ -> difference s1 (product (quotient_e s1 s2) s2) let remainder_t s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.rem n1 n2) | _ -> difference s1 (product (quotient_t s1 s2) s2) let remainder_f s1 s2 = match s1, s2 with | Int n1, Int n2 -> mk_int (Z.rem n1 n2) | _ -> difference s1 (product (quotient_f s1 s2) s2) let to_int s = match s with | Int _ -> s | _ -> floor s let to_rat s = match s with | Int n -> mk_rat (Q.of_bigint n) | Rat _ -> s | _ -> _ty_mismatch "not a numeric constant: %a" pp s let abs s = match s with | Int n -> mk_int (Z.abs n) | Rat n -> mk_rat (Q.abs n) | _ -> _ty_mismatch "not a numeric constant: %a" pp s let divides a b = match a, b with | Rat i, Rat _ -> Q.sign i <> 0 | Int a, Int b -> Z.sign a <> 0 && Z.sign (Z.rem b a) = 0 | _ -> _ty_mismatch "divides: expected two numerical types" let gcd a b = match a, b with | Rat _, Rat _ -> one_rat | Int a, Int b -> mk_int (Z.gcd a b) | _ -> _ty_mismatch "gcd: expected two numerical types" let lcm a b = match a, b with | Rat _, Rat _ -> one_rat | Int a, Int b -> mk_int (Z.lcm a b) | _ -> _ty_mismatch "gcd: expected two numerical types" let less s1 s2 = match s1, s2 with | Int n1, Int n2 -> Z.lt n1 n2 | Rat n1, Rat n2 -> Q.lt n1 n2 | _ -> err2_ s1 s2 let lesseq s1 s2 = match s1, s2 with | Int n1, Int n2 -> Z.leq n1 n2 | Rat n1, Rat n2 -> Q.leq n1 n2 | _ -> err2_ s1 s2 let greater s1 s2 = less s2 s1 let greatereq s1 s2 = lesseq s2 s1 (* factorize [n] into a product of prime numbers. [n] must be positive *) let divisors n = if (Z.leq n Z.zero) then raise (Invalid_argument "prime_factors: expected number > 0") else try let n = Z.to_int n in let l = ref [] in for i = 2 to n/2 do if i < n && n mod i = 0 then l := i :: !l done; List.rev_map Z.of_int !l with Z.Overflow -> [] (* too big *) end module ZF = struct let to_string = function | Eq -> "=" | Neq -> "!=" | And -> "&&" | Or -> "||" | Not -> "~" | Imply -> "=>" | Equiv -> "<=>" | HasType -> ":" | True -> "true" | False -> "false" | Arrow -> ">" | Wildcard -> "_" | TType -> "type" | Prop -> "prop" | Term -> "term" (* XXX needs to be declared! *) | Xor | Multiset -> failwith "cannot print this symbol in ZF" | ForallConst -> "!!" | ExistsConst -> "??" | Grounding -> "$$grounding" | TyInt -> "int" | TyRat -> "rat" | TyReal -> "real" | Int x -> Z.to_string x | Rat x -> Q.to_string x | Real x -> x (* FIXME: update *) | Floor -> "$floor" | Ceiling -> "$ceiling" | Truncate -> "$truncate" | Round -> "$round" | Prec -> "$prec" | Succ -> "$succ" | Sum -> "+" | Difference -> "-" | Uminus -> "-" | Product -> "*" | Quotient -> "$quotient" | Quotient_e -> "/" | Quotient_t -> "$quotient_t" | Quotient_f -> "$quotient_f" | Remainder_e -> "mod" | Remainder_t -> "$remainder_t" | Remainder_f -> "$remainder_f" | Is_int -> "$is_int" | Is_rat -> "$is_rat" | To_int -> "$to_int" | To_rat -> "$to_rat" | Less -> "<" | Lesseq -> "<=" | Greater -> ">" | Greatereq -> ">=" | Box_opaque -> "<box>" | Pseudo_de_bruijn i -> Printf.sprintf "<db %d>" i let pp out b = CCFormat.string out (to_string b) end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>