package alba
Alba compiler
Install
Dune Dependency
Authors
Maintainers
Sources
0.4.4.tar.gz
sha256=4817038301d3e45bac9edf7e6f2fc8bf0a6d78e76e02ad7ea33ef69bcc17df3b
md5=25234357587126685d64f16236167937
doc/src/alba.albalib/build_inductive.ml.html
Source file build_inductive.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 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
(* General form of an inductive definition mutual class I0 (p0: P0) ... : K0 := co00: CT00 co01: CT01 ... class I1 (p0: P0) ... : K1 := co10: CT10 co11: CT11 ... ... Kinds K0, K1, ... are the kinds of the inductive types. Each kind must have the normal form Ki = all (a0: A0) (a1: A1) ... : s where s is either Any or Proposition. a0, a1, ... are the indices of the type. Headers The headers of an inductive definition are I0 (p0: P0) ... : K0 I1 (p0: P0) ... : K1 ... We store the paramaters, which are common to all types of the family and the pairs (Ii, Ki) separately. In this module the headers are just the pairs. Checks - The parameters are the same (same number, same names and same types) in a inductive definitions of the family. - Names of inductive classes and all constructors different. - Each constructor type CTij has the form all (b0: B0) (b1: B1) ... : R - Each constructor constructs an object of its corresponding type, i.e. the final type R of each constructor type CTij has the form R = Ii p0 p1 ... a0 a1 ... where p0 p1 ... are exactly the parameters of the inductive definition and the indices are arbitrary but do not contain any Ij of the family. - Each constructor argument Bk has the form Bk = all (c0: C0) (c1: C1) ... : RBk where none of the Ii occurs in any of C0, C1, ... and any Ii can occur only positively in RBk. C0, C1, ... are the negative positions and RBk is the positive position of the constructor argument type Bk. Positive occurrence A variable x occurs positively in a type T, if - T = x a0 a1 ... where x does not occur in a0 a1 ... - T = I a0 a1 ... where I is an external inductive type which is not mutually defined and has some positive parameters and x occurs positively in ai which correspond to a positive parameter and does not occur in other arguments. Positive parameters A parameter p is positive, if - p: Any or p: Proposition - p occurs only positively in argument types of constructors. *) open Fmlib open Common open Alba_core open Ast module Result = Fmlib.Result.Make (Build_problem) module Interval_monadic = Interval.Monadic (Result) module List_monadic = List.Monadic (Result) module Algo = Gamma_algo.Make (Gamma) type 'a result2 = ('a, Build_problem.t) result type params_located = (string Located.t * Term.typ) array (* Needed data to analyze constructors. *) module Data = struct type t = { cnt0: int; (* Size of the context without headers and parameters. *) params: Inductive.params; headers: Inductive.Header.t array; positives: Int_set.t; (* Candidate set for positive parameters. At the end of the analysis, the set contains all positive parameters. *) } let make cnt0 headers params = let positives = Array.foldi_left (fun set i (_, typ) -> let open Term in match typ with | Sort Sort.Any _ | Sort Sort.Proposition -> Int_set.add i set | _ -> set ) Int_set.empty params in {cnt0; params; headers; positives} let header (i: int) (d: t): Inductive.Header.t = d.headers.(i) let params (d: t): Inductive.params = d.params let headers (d: t): Inductive.Header.t array = d.headers let constructor_base_count (d: t): int = d.cnt0 + Array.length d.headers + Array.length d.params let is_inductive (level: int) (d: t): bool = d.cnt0 <= level && level < d.cnt0 + Array.length d.headers let is_parameter (level: int) (d: t): bool = let ntypes = Array.length d.headers in d.cnt0 + ntypes <= level && level < d.cnt0 + ntypes + Array.length d.params let positives (d: t): Int_set.t = d.positives let remove_positive (iparam) (level: int) (d: t): t = let param0 = d.cnt0 + Array.length d.headers and nparams = Array.length d.params in if param0 <= level && level < param0 + nparams && param0 + iparam <> level (* level is not iparam *) then( {d with positives = Int_set.remove (level - param0) d.positives } ) else d end (* Data *) let (>>=) = Result.(>>=) let build_type_or_any (name: string Located.t) (typ: Expression.t option) (c: Context.t) : Term.typ result2 = match typ with | None -> Ok Term.any | Some typ -> Build_expression.build_named_type name typ c let check_params (params0: Inductive.params) (name: string Located.t) (params: params_located) (context: Context.t) : unit result2 = (* [params0] are the paramters of the first type in the family. Check that a subsequent type [name,params] has the same set of parameters, i.e. same number, same names and same types. *) let nparams = Array.length params0 in if nparams <> Array.length params then Error ( Located.range name, Build_problem.Wrong_parameter_count nparams ) else Interval_monadic.( fold (fun i context -> let name, typ = params.(i) and name0, typ0 = params0.(i) in if Located.value name <> name0 then Error ( Located.range name, Build_problem.Wrong_parameter_name name0 ) else if Typecheck.equivalent typ0 typ (Context.gamma context) then Ok ( Context.push_local (Located.value name) typ context ) else Error ( Located.range name , Build_problem.Wrong_parameter_type ( typ0, Context.gamma context ) ) ) 0 nparams context ) >>= fun _ -> Ok () let class_header (i: int) (inds: Source_entry.inductive array) (c0: Context.t) : (string Located.t * params_located * Inductive.Header.t) result2 = (* Analyze the [i]ith class header of the inductive family [inds]. class Name (P0: PT0) (P1: PT1) ... : TP *) assert (i < Array.length inds); let (name, (params, kind_exp)), _ = inds.(i) in List_monadic.( fold_left (fun (name, param_typ) (lst,c1) -> build_type_or_any name param_typ c1 >>= fun param_typ -> Ok ( (name, param_typ) :: lst, Context.push_local (Located.value name) param_typ c1 ) ) params ([],c0) ) >>= fun (params, c1) -> build_type_or_any name kind_exp c1 >>= fun kind -> match Algo.split_kind kind (Context.gamma c1) with | None -> assert (kind_exp <> None); let range = Located.range (Option.value kind_exp) in Error (range, Build_problem.No_inductive_type) | Some (args, sort) -> let name_str = Located.value name in let params = Array.of_list (List.rev params) and header = Inductive.Header.make name_str kind args sort in let params1 = Array.map (fun (name,typ) -> Located.value name, typ) params in if Context.can_add_global name_str (Inductive.Header.kind params1 header) c0 then Ok (name, params, header) else Error (Located.range name, Build_problem.Ambiguous_definition) let class_headers (inds: Source_entry.inductive array) (context: Context.t) : (Inductive.params * Inductive.Header.t array) result2 = assert (0 < Array.length inds); class_header 0 inds context >>= fun (name0, params0, header0) -> let params0 = Array.map (fun (name, typ) -> Located.value name, typ) params0 in Interval_monadic.fold (fun i (set, lst) -> class_header i inds context >>= fun (name, params, header) -> check_params params0 name params context >>= fun _ -> let name_str = Located.value name in if String_set.mem name_str set then Error(Located.range name, Build_problem.Duplicate_inductive) else Ok (String_set.add name_str set, header :: lst) ) 1 (Array.length inds) (String_set.singleton (Located.value name0), []) >>= fun (_, lst) -> Ok (params0, Array.of_list (header0 :: List.rev lst)) let push_params (ntypes: int) (params: Inductive.params) (context: Context.t) : Context.t = Array.foldi_left (fun context iparam (name,typ) -> Context.push_local name (Term.up_from iparam ntypes typ) context) context params let push_types (params: Inductive.params) (headers: Inductive.Header.t array) (context: Context.t) : Context.t = let _, context = Array.fold_left (fun (i,context) header -> let open Inductive.Header in i + 1, Context.add_axiom (name header) (Term.up i (kind params header)) context ) (0,context) headers in context let fold_type (argf: 'a -> Term.typ -> Gamma.t -> 'a result2) (resf: 'a -> Term.typ -> Gamma.t -> 'b result2) (a: 'a) (typ: Term.typ) (gamma: Gamma.t) : 'b result2 = let rec fold a typ gamma = let open Term in match Algo.key_normal typ gamma with | Pi (arg, res, info) -> argf a arg gamma >>= fun a -> fold a res (Gamma.push_local (Pi_info.name info) arg gamma) | res -> resf a res gamma in fold a typ gamma let check_non_occurrence (error: unit -> Data.t result2) (iparam: int) (* param allowed to occur. *) (term: Term.t) (gamma: Gamma.t) (data: Data.t): Data.t result2 = let module TermM = Term.Monadic (Result) in TermM.fold_free (fun idx data -> let level = Gamma.level_of_index idx gamma in if Data.is_inductive level data then error () else Ok (Data.remove_positive iparam level data) ) term data let check_constructor_argument_result_type (range: range) (data: Data.t) (typ: Term.typ) (gamma: Gamma.t): Data.t result2 = let open Build_problem in let not_positive _ = Error (range, Not_positive (typ,gamma)) in let rec check term = let key, args = Algo.key_split term gamma in match key with | Variable idx -> let level = Gamma.level_of_index idx gamma in if Data.is_inductive level data then (* Positive parameters can occur at proper place! *) non_occurrence true args not_positive else if Data.is_parameter level data && args = [] then Ok data else begin match Gamma.inductive_at_level level gamma with | None -> non_occurrence false args not_positive (* Positive parameters cannot occur! *) | Some ind -> if Inductive.count_types ind = 1 then positive_occurrence ind args else non_occurrence false args not_positive (* Positive parameters cannot appear. *) end | _ -> check_non_occurrence not_positive (-1) term gamma data and non_occurrence might_occur args error = List_monadic.foldi_left (fun iparam (arg, _) data -> let iparam = if might_occur then iparam else - 1 in check_non_occurrence error iparam arg gamma data ) args data and positive_occurrence ind args = List_monadic.foldi_left (fun iparam (arg, _) data -> if Inductive.is_param_positive iparam ind then check arg else check_non_occurrence (fun _ -> Error ( range, Nested_negative (ind, iparam, gamma) )) (-1) arg gamma data ) args data in check typ let check_constructor_argument_type (range: range) (data: Data.t) (arg_typ: Term.typ) (gamma: Gamma.t) : Data.t result2 = fold_type (fun data typ gamma -> check_non_occurrence (fun _ -> Error (range, Build_problem.Negative)) (-1) typ gamma data ) (check_constructor_argument_result_type range) data arg_typ gamma let check_constructor_result_type (i: int) (range: range) (data: Data.t) (res: Term.typ) (gamma: Gamma.t) : Data.t result2 = if Inductive.Header.is_well_constructed i (Data.params data) (Data.headers data) Gamma.(count gamma - Data.constructor_base_count data) res then Ok data else Error ( range, Build_problem.Wrong_type_constructed (res, gamma) ) let check_constructor_type (i: int) (data: Data.t) (name: string Located.t) (typ: Term.typ) (c: Context.t) : (Data.t * Inductive.Constructor.t) result2 = let range = Located.range name in fold_type (check_constructor_argument_type range) (check_constructor_result_type i range) data typ (Context.gamma c) >>= fun data -> Ok (data, Inductive.Constructor.make (Located.value name) typ) let one_constructor (i: int) (* inductive type *) (data: Data.t) ((name, (fargs, typ)) : Source_entry.named_signature) (c: Context.t) (* with types and params *) : (Data.t * Inductive.Constructor.t) result2 = (* Collect constructor arguments. *) let module Lst = List.Monadic (Result) in Lst.fold_left (fun (name, typ) (fargs, c) -> match typ with | None -> assert false (* Illegal call! Parser has to prevent that. *) | Some typ -> Build_expression.build_named_type name typ c >>= fun typ -> let name = Located.value name in Ok ( (name, typ) :: fargs , Context.push_local name typ c ) ) fargs ([], c) >>= fun (fargs, c1) -> ( (* Analyze final type of the signature. *) match typ with | None -> (* Must be the default inductive type. Only possible without indices. *) if Inductive.Header.has_index (Data.header i data) then Error ( Located.range name, Build_problem.Missing_inductive_type ) else Ok ( Inductive.Header.default_type i (Data.params data) (Data.headers data) ) | Some typ -> Build_expression.build_named_type name typ c1 ) >>= fun typ -> let typ = List.fold_left (fun res (name, typ) -> Term.(Pi (typ, res, Pi_info.typed name))) typ fargs in check_constructor_type i data name typ c let one_constructor_set (i: int) (* inductive type *) (data: Data.t) (inds: Source_entry.inductive array) (c: Context.t) (* with types and params *) : (Data.t * Inductive.Constructor.t array) result2 = let module Arr = Array.Monadic (Result) in Arr.fold_left (fun constructor (set, data, lst) -> one_constructor i data constructor c >>= fun (data, co) -> let name, _ = constructor in let name_str = Located.value name in if String_set.mem name_str set then Error (Located.range name, Build_problem.Duplicate_constructor) else Ok (String_set.add name_str set, data, co :: lst) ) (snd inds.(i)) (String_set.empty, data, []) >>= fun (_, data, lst) -> Ok (data, Array.of_list (List.rev lst)) let constructors (params: Inductive.params) (headers: Inductive.Header.t array) (inds: Source_entry.inductive array) (context: Context.t) : Inductive.t result2 = let context1 = push_types params headers context |> push_params (Array.length headers) params and data = Data.make (Context.count context) headers params in (* list of constructor sets with corresponding header and number of previous constructors. *) let module Arr = Array.Monadic (Result) in Arr.foldi_left (fun i header (n, data, constructors) -> one_constructor_set i data inds context1 >>= fun (data, constructor_set) -> Ok ( n + Array.length constructor_set , data , Inductive.Type.make n header constructor_set :: constructors ) ) headers (0, data, []) >>= fun (_, data, types) -> Ok Inductive.(make params (Data.positives data) (Array.of_list (List.rev types))) let build (inds: Source_entry.inductive array) (context: Context.t) : Inductive.t result2 = class_headers inds context >>= fun (params,headers) -> constructors params headers inds context
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>