| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** Type of regular tree with nodes labelled by values of type 'a |
| 12 | The implementation uses de Bruijn indices, so binding capture |
| 13 | is avoided by the lift operator (see example below). |
| 14 | |
| 15 | Note that it differs from standard regular trees by accepting |
| 16 | vectors of vectors in nodes, which is useful for encoding |
| 17 | disjunctive-conjunctive recursive trees such as inductive types. |
| 18 | Standard regular trees can however easily be simulated by using |
| 19 | singletons of vectors *) |
| 20 | type 'a t |
| 21 | |
| 22 | (** Building trees *) |
| 23 | |
| 24 | (** Build a node given a label and a vector of vectors of sons *) |
| 25 | val mk_node : 'a -> 'a t array array -> 'a t |
| 26 | |
| 27 | (** Build mutually recursive trees: |
| 28 | X_1 = f_1(X_1,..,X_n) ... X_n = f_n(X_1,..,X_n) |
| 29 | is obtained by the following pseudo-code |
| 30 | let vx = mk_rec_calls n in |
| 31 | let [|x_1;..;x_n|] = |
| 32 | mk_rec[|f_1(vx.(0),..,vx.(n-1);..;f_n(vx.(0),..,vx.(n-1))|] |
| 33 | |
| 34 | First example: build rec X = a(X,Y) and Y = b(X,Y,Y) |
| 35 | let [|vx;vy|] = mk_rec_calls 2 in |
| 36 | let [|x;y|] = mk_rec [|mk_node a [|[|vx;vy|]|]; mk_node b [|[|vx;vy;vy|]|]|] |
| 37 | |
| 38 | Another example: nested recursive trees rec Y = b(rec X = a(X,Y),Y,Y) |
| 39 | let [|vy|] = mk_rec_calls 1 in |
| 40 | let [|vx|] = mk_rec_calls 1 in |
| 41 | let [|x|] = mk_rec[|mk_node a [|[|vx;lift 1 vy|]|]|] |
| 42 | let [|y|] = mk_rec[|mk_node b [|[|x;vy;vy|]|]|] |
| 43 | (note the lift so that Y links to the "rec Y" skipping the "rec X") |
| 44 | *) |
| 45 | val mk_rec_calls : int -> 'a t array |
| 46 | val mk_rec : 'a t array -> 'a t array |
| 47 | |
| 48 | (** [lift k t] increases of [k] the free parameters of [t]. Needed |
| 49 | to avoid captures when a tree appears under [mk_rec] *) |
| 50 | val lift : int -> 'a t -> 'a t |
| 51 | |
| 52 | val is_node : 'a t -> bool |
| 53 | |
| 54 | (** Destructors (recursive calls are expanded) *) |
| 55 | val dest_node : 'a t -> 'a * 'a t array array |
| 56 | |
| 57 | (** First projection of {!dest_node} *) |
| 58 | val dest_head : 'a t -> 'a |
| 59 | |
| 60 | (** dest_var is not needed for closed trees (i.e. with no free variable) *) |
| 61 | val dest_var : 'a t -> int * int |
| 62 | |
| 63 | (** Tells if a tree has an infinite branch. The first arg is a comparison |
| 64 | used to detect already seen elements, hence loops *) |
| 65 | val is_infinite : ('a -> 'a -> bool) -> 'a t -> bool |
| 66 | |
| 67 | (** [Rtree.equiv eq eqlab t1 t2] compares t1 t2 (top-down). |
| 68 | If t1 and t2 are both nodes, [eqlab] is called on their labels, |
| 69 | in case of success deeper nodes are examined. |
| 70 | In case of loop (detected via structural equality parametrized |
| 71 | by [eq]), then the comparison is successful. *) |
| 72 | val equiv : |
| 73 | ('a -> 'a -> bool) -> ('a -> 'a -> bool) -> 'a t -> 'a t -> bool |
| 74 | |
| 75 | (** [Rtree.equal eq t1 t2] compares t1 and t2, first via physical |
| 76 | equality, then by structural equality (using [eq] on elements), |
| 77 | then by logical equivalence [Rtree.equiv eq eq] *) |
| 78 | val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool |
| 79 | |
| 80 | val inter : ('a -> 'a -> bool) -> ('a -> 'a -> 'a option) -> 'a -> 'a t -> 'a t -> 'a t |
| 81 | |
| 82 | val incl : ('a -> 'a -> bool) -> ('a -> 'a -> 'a option) -> 'a -> 'a t -> 'a t -> bool |
| 83 | |
| 84 | (** Iterators *) |
| 85 | |
| 86 | (** See also [Smart.map] *) |
| 87 | val map : ('a -> 'b) -> 'a t -> 'b t |
| 88 | |
| 89 | (** Pretty-printer *) |
| 90 | val pr_tree : ('a -> Pp.t) -> 'a t -> Pp.t |
| 91 | |
| 92 | module Smart : |
| 93 | sig |
| 94 | |
| 95 | (** [(Smart.map f t) == t] if [(f a) ==a ] for all nodes *) |
| 96 | val map : ('a -> 'a) -> 'a t -> 'a t |
| 97 | |
| 98 | end |
| 99 | |
| 100 | module Kind : |
| 101 | sig |
| 102 | |
| 103 | type 'a rtree = 'a t |
| 104 | type 'a t |
| 105 | type 'a kind = Var of int * int | Node of 'a * 'a t array array |
| 106 | |
| 107 | val make : 'a rtree -> 'a t |
| 108 | val kind : 'a t -> 'a kind |
| 109 | val repr : 'a t -> 'a rtree |
| 110 | |
| 111 | end |