package lambdapi

  1. Overview
  2. Docs

Module Handle.RewriteSource

Implementation of the rewrite tactic.

Sourceval log : 'a Lplib.Base.outfmt -> 'a
Sourcetype eq_config = {
  1. symb_P : Core.Term.sym;
    (*

    Encoding of propositions.

    *)
  2. symb_T : Core.Term.sym;
    (*

    Encoding of types.

    *)
  3. symb_eq : Core.Term.sym;
    (*

    Equality proposition.

    *)
  4. symb_eqind : Core.Term.sym;
    (*

    Induction principle on equality.

    *)
  5. symb_refl : Core.Term.sym;
    (*

    Reflexivity of equality.

    *)
}

Equality configuration.

Sourcetype binder_rewrite_config = {
  1. symb_arr : Core.Term.sym;
    (*

    HOL non-dependent function type constructor.

    *)
  2. symb_funext : Core.Term.sym;
    (*

    Functional extensionality principle.

    *)
}

Additional configuration needed only when rewrite has to lift an equality over a binder.

get_eq_config ss pos returns the current configuration for equality, used by tactics such as “rewrite” or “reflexivity”.

Sourceval get_binder_rewrite_config : Core.Sig_state.t -> Common.Pos.popt -> binder_rewrite_config

get_binder_rewrite_config ss pos returns the configuration needed for binder-dependent rewrite occurrences.

get_eq_data pos cfg a returns ((a,l,r),[v1;..;vn]) if a ≡ Π v1:A1, .., Π vn:An, P (eq a l r) and fails otherwise.

Sourcetype to_subst = Core.Term.var array * Core.Term.term

Type of a term with the free variables that need to be substituted. It is usually used to store the LHS of a proof of equality, together with the variables that were quantified over.

Sourceval matches : ?allow_binder_holes:??? -> Core.Term.term -> Core.Term.term -> bool

matches p t instantiates the TRefs of p so that p becomes equal to t and returns true if all TRefs of p could be instantiated, and false otherwise. With allow_binder_holes, a TRef belonging to a selection pattern may match a term containing a variable bound inside p. Callers must preserve the scope of those variables when using the assignments, or discard them.

Sourceval no_match : ?subterm:??? -> Common.Pos.popt -> Core.Term.term -> Core.Term.term -> 'a
Sourceval matching_subs : to_subst -> Core.Term.term -> Core.Term.term array option

matching_subs (xs,p) t attempts to match the pattern p containing the variables xs with the term t. If successful, it returns Some ts where ts is an array of terms such that substituting xs by the corresponding elements of ts in p yields t.

WARNING: Some elements of ts may be uninstantiated TRefs. This happens if not all xs are in LibTerm.free_vars p, for instance when some xs are type variables not occurring in p, for instance when trying to apply an equation of the form x = ... with x of polymorphic type. This could be improved by generating p_terms instead of terms. Indeed, in this case, we could replace uninstantiated TRefs by underscores.

Sourceval check_subs : Common.Pos.popt -> Core.Term.var array -> Core.Term.term array -> unit

check_subs vars ts checks that no element of ts is an uninstantiated TRef.

Sourceval matching_subs_check_TRef : Common.Pos.popt -> (Core.Term.var array * Core.Term.term) -> Core.Term.term -> Core.Term.term array
Sourcetype binder_frame = {
  1. binder_var : Core.Term.var;
  2. binder_typ : Core.Term.term;
  3. binder_path : int list;
}

Binder frame enclosing a subterm. binder_path identifies the original binder by the path to its body, storing child indices in reverse order. Application children are 0 for the function and 1 for the argument; abstraction/product children are 0 for the domain and 1 for the body; let children are 0 for the type, 1 for the definition and 2 for the body. The path recognises the same binder across traversals even when unbinding creates fresh variables. binder_var is the variable opened in this traversal, with type binder_typ.

Sourceval abstract_over_frame : binder_frame -> Core.Term.term -> Core.Term.term

abstract_over_frame frame t abstracts t over the variable recorded in frame, using the frame's binder type.

Sourceval abstract_over_frames : binder_frame list -> Core.Term.term -> Core.Term.term

abstract_over_frames frames t abstracts t over all frames. The frame list must be ordered from innermost to outermost binder, and the returned abstractions are nested from outermost to innermost binder.

Sourceval same_binder_paths : binder_frame list -> binder_frame list -> bool

same_binder_paths frames frames' tells whether frames and frames' identify the same original enclosing binders across independent traversals of the goal.

Sourceval apply_to_frames : Core.Term.term -> binder_frame list -> Core.Term.term

apply_to_frames t frames applies t to the variables in frames. The frame list must be ordered from innermost to outermost binder.

Sourceval adapt_to_frames : binder_frame list -> binder_frame list -> Core.Term.term -> Core.Term.term

adapt_to_frames frames frames' t adapts references in t to variables bound by frames so that they refer to the corresponding variables bound by frames'.

Sourceval same_under_binders : binder_frame list -> binder_frame list -> Core.Term.term -> Core.Term.term -> bool

same_under_binders first_frames frames first t tells whether t is the same term as first, modulo the fresh variables generated when unbinding the same original enclosing binders.

Sourceval find_subterm_from : binder_frame list -> int list -> (binder_frame list -> Core.Term.term -> 'a option) -> Core.Term.term -> 'a option

find_subterm_from binders path try_match t searches t, trying each candidate before its children. try_match receives its enclosing frames and term, and returns Some result on success. Failed attempts are rolled back before descending. Children are searched in application order, with binder domains and let definitions before bodies. Initial frames are ordered from innermost to outermost; paths store child indices in reverse. A successful result retains the state established by try_match.

Sourcetype subst_under_binders = {
  1. subst : Core.Term.term array;
  2. redex : Core.Term.term;
  3. binders : binder_frame list;
}

Result of a successful binder-aware rewrite occurrence search.

Sourceval find_subst_from : binder_frame list -> int list -> to_subst -> Core.Term.term -> subst_under_binders option

find_subst_from binders path (xs,p) t searches for the first subterm of t matching p. It returns the substitution for xs, the matched term and its enclosing binder frames. binders and path are the known enclosing context of t, ordered as in find_subterm_from.

find_subst_in_region pos binders (vars,p) t searches for p in an already-selected region t, preserving the binder context enclosing the region.

find_subst pos (vars,p) t returns the search result for the first occurrence of p in t, or fails if no subterm matches. It also checks that all variables in vars have been instantiated.

Sourceval replace_wild_by_tref : Core.Term.term -> Core.Term.term

replace_wild_by_tref t substitutes every wildcard of t by a distinct fresh TRef.

Sourcetype selected_term_under_binders = {
  1. term : Core.Term.term;
  2. binders : binder_frame list;
}

Result of matching a rewrite selector pattern. term is the actual matched goal subterm; binders records its enclosing binder frames.

Sourceval find_subterm_matching_from : binder_frame list -> int list -> Core.Term.term -> Core.Term.term -> selected_term_under_binders option

find_subterm_matching_from binders path p t tries to find a subterm of t that matches p by instantiating the TRefs of p and records the binders enclosing the selected subterm. binders and path are the already-known enclosing binder context of t. The binder list is ordered from innermost to outermost binder.

find_subterm_matching pos p t returns the first selected subterm matching p in t, or fails if no subterm matches.

Sourcetype contextual_selection = {
  1. pat_l : Core.Term.term;
  2. pat_l_binders : binder_frame list;
  3. id_val : Core.Term.term;
  4. id_val_binders : binder_frame list;
  5. id_path : int list;
  6. id_paths : int list list;
}

Result of matching a contextual pattern such as id in p. pat_l is the actual matched goal context before rewriting, retained directly rather than reconstructed from p. id_val is the term denoted by id inside it. Both terms retain their enclosing binder frames in pat_l_binders and id_val_binders, respectively. id_path is the first occurrence of id, and id_paths records every occurrence. These paths are relative to pat_l and are stored in reverse order, as in the rewrite traversal.

Sourceval match_contextual_pattern : Core.Term.var -> Core.Term.term -> binder_frame list -> Core.Term.term -> contextual_selection option

match_contextual_pattern selector p binders t matches one candidate t against p, collecting all occurrences of selector. The selected paths are relative to t; binders records its enclosing context.

find_contextual_selection pos p goal finds the first subterm of goal matching the contextual pattern p. It matches the context and identifies the selector occurrence together.

Sourceval replace_under_binders_from : binder_frame list -> int list -> (binder_frame list -> int list -> Core.Term.term -> Core.Term.term option) -> Core.Term.term -> Core.Term.term

replace_under_binders_from binders path f goal traverses goal while recording enclosing binder frames. binders and path are the already-known enclosing binder context of goal. At each subterm t, f binders path t may return a replacement. The path is relative to the initial goal unless path is supplied by a containing search. If f returns a replacement, traversal does not descend below t.

Sourceval replace_under_binders : (binder_frame list -> Core.Term.term -> Core.Term.term option) -> Core.Term.term -> Core.Term.term

replace_under_binders f goal traverses goal while recording enclosing binder frames. At each subterm t, f binders t may return a replacement. If it does, traversal does not descend below t.

Sourceval bind_pattern : ?at:??? -> binder_frame list -> Core.Term.var -> Core.Term.term -> binder_frame list -> Core.Term.term -> Core.Term.term

bind_pattern ~at binders z lhs relevant goal replaces occurrences matching the instantiated lhs in goal by z applied to the relevant bound variables. relevant is the list of binder frames that the equality is lifted over. lhs is already abstracted over these frames. Candidates must use the same original binders and match after abstraction over their local frames. binders is the already-known enclosing context of goal; at optionally restricts replacement to selected paths.

Sourceval replace_selected_under_binders : binder_frame list -> Core.Term.term -> Core.Term.term -> Core.Term.term -> Core.Term.term

replace_selected_under_binders selected_binders selected replacement goal replaces every occurrence of selected in goal by replacement, adapting references from the variables recorded in selected_binders to the corresponding fresh variables when the same original binders are unbound again while traversing goal.

Sourcetype prepared_rewrite = {
  1. eq_type : Core.Term.term;
  2. lhs : Core.Term.term;
  3. rhs : Core.Term.term;
  4. proof : Core.Term.term;
  5. pred_bind : Core.Term.binder;
  6. new_term : Core.Term.term;
}

Data prepared for the final equality-induction rewrite step.

Equality data being lifted over one or more binders.

Sourceval hol_domain_of_binder : eq_config -> Common.Pos.popt -> Core.Term.term -> Core.Term.term

hol_domain_of_binder cfg pos typ returns a when typ is convertible to T a, and fails otherwise.

lift_over_frame cfg binder_cfg pos frame (a,l,r,p) lifts the equality data p : P (eq a l r) over frame using funExt.

Sourceval prepare_eq_data_under_binders : Core.Sig_state.t -> eq_config -> Common.Pos.popt -> binder_frame list -> Core.Term.term -> eq_data -> eq_data * binder_frame list

prepare_eq_data_under_binders ss cfg pos binders redex data lifts data over every enclosing binder on which redex depends. It returns the lifted equality data and the relevant binders.

prepare_rewrite_from ~at binders ss cfg pos goal eq_data found prepares the equality data, predicate and rewritten term for a rewrite occurrence. binders is the already-known enclosing binder context of goal. If the redex depends on enclosing binders, the equality is lifted over the relevant ones and the context abstracts the resulting function-level occurrence. at optionally restricts replacement to selected paths.

prepare_rewrite ss cfg pos goal eq_data found is prepare_rewrite_from with no initial enclosing binders.

swap cfg a r l t returns a term of type P (eq a l r) from a term t of type P (eq a r l).

Sourceval at_selector : contextual_selection -> int list -> bool

at_selector selected path tells whether path names an occurrence of the contextual selector identifier. Paths are relative to selected.pat_l; repeated uses of the identifier are all included.

Sourceval within_selector : contextual_selection -> int list -> bool

within_selector selected path tells whether path is at or below a region named by the contextual selector identifier. Paths are relative to selected.pat_l and store child indices in reverse order, so a descendant path ends with its region's path.

rewrite_in_context ss cfg pos goal data at selected found prepares a rewrite inside the matched selected.pat_l, then reinserts the rewritten context and predicate into goal. data is the instantiated equality (type, LHS, RHS, proof); found records the first redex and its enclosing binders. at restricts replacement to paths relative to the matched context. The result contains the prepared equality type, whole-goal predicate binder, rewritten goal, equality proof and sides.

rewrite ss p pos gt l2r pat t generates a term for the refine tactic representing the application of the rewrite tactic to the goal type gt. Every occurrence of the first instance of the left-hand side is replaced by the right-hand side of the obtained proof (or the reverse if l2r is false). pat is an optional SSReflect pattern. t is the equational lemma that is applied. It handles the full set of SSReflect patterns.