Introduction
Refinement types checkers, such as Liquid Haskell [38 ] , Stainless [22 ] , F* [36 ] , and Flux [25 ] , rely on SMT solvers to reason about program properties. They work on top of existing, industrial programming languages, allowing programmers to work in familiar environments while enjoying a high degree of automation in program verification. However, practical refinement types are disconnected from their theoretical foundations. Even though program behaviors and SMT logics are very well understood separately, the connection between them has not been made explicit.
In refinement types, SMT validity checking is used to decide subtyping. To do so, a connection between program behaviors and SMT logics needs to be made. As an example, consider an expression e e e with a type that ensures it is an integer greater than 5, i.e., e : { x : Int ∣ 5 < x } e : \{x:\operatorname{Int} \mid5 < x\} e : { x : Int ∣ 5 < x } . Since 5 < x 5 < x 5 < x implies 0 < x 0 < x 0 < x , for all integers x x x , e e e can also be used at type { x : Int ∣ 0 < x } \{x:\operatorname{Int} \mid0 < x\} { x : Int ∣ 0 < x } . This reasoning is captured by the following two rules:
Γ ⊢ e : τ l Γ ⊢ τ l ⪯ τ r Γ ⊢ e : τ r \frac{\Gamma\vdash e : \tau_l \qquad\Gamma\vdash\tau_l \preceq\tau_r}{\Gamma\vdash e : \tau_r} Γ ⊢ e : τ r Γ ⊢ e : τ l Γ ⊢ τ l ⪯ τ r
SMT-Valid ( ⟦ Γ ⟧ ⇒ ⟦ r l ⟧ ⇒ ⟦ r r ⟧ ) Γ ⊢ { x : B ∣ r l } ⪯ { x : B ∣ r r } \frac{\operatorname{SMT\text{-}Valid}\left(\llbracket\Gamma\rrbracket\Rightarrow\llbracket r_l\rrbracket\Rightarrow\llbracket r_r\rrbracket\right)}{\Gamma\vdash\{x:B \mid r_l\} \preceq\{x:B \mid r_r\}} Γ ⊢ { x : B ∣ r l } ⪯ { x : B ∣ r r } SMT - Valid ( [ [ Γ ] ] ⇒ [ [ r l ] ] ⇒ [ [ r r ] ] )
The left rule, T-SUB, allows for implicit subtyping : an expression e e e of type τ l \tau_l τ l can be given the type τ r \tau_r τ r , if τ l \tau_l τ l is a subtype of τ r \tau_r τ r . To decide subtyping, refinement type systems reduce it to SMT validity checking using a set of subtyping rules, that ultimately boil down to the rule S-BASE on the right. This rule states that a refinement type { x : B ∣ r l } \{x : B \mid r_l\} { x : B ∣ r l } is a subtype of { x : B ∣ r r } \{x : B \mid r_r\} { x : B ∣ r r } if the implication r l ⇒ r r r_l \Rightarrow r_r r l ⇒ r r is valid in the SMT logic and under the assumptions in the type environment Γ \Gamma Γ .
The critical part is: program elements are translated to the SMT logic via the embedding function ⟦ ⋅ ⟧ \llbracket\cdot\rrbracket [ [ ⋅ ] ] . In cases where both program elements and logics are the same (e.g., integers with arithmetic operations), the embedding function is trivial. For example, when relating { x : Int ∣ 5 < x } \{x:\operatorname{Int} \mid5 < x\} { x : Int ∣ 5 < x } and
Authors’ Contact Information: Colin Rothgang, IMDEA Software Institute, Madrid, Spain; Loïc Peyrot, IMDEA Software Institute, Madrid, Spain; Yannick Forster, Inria, Paris, France; Michael Greenberg, Stevens Institute of Technology, USA; Niki Vazou, IMDEA Software Institute, Madrid, Spain. { x : I n t ∣ 0 < x } \{x:\mathrm{Int} \mid0 < x\} { x : Int ∣ 0 < x } . However, in general, defining the embedding function is intricate, and the details of its definition have not been formally accounted for.
Formalization of refinement type systems have provided partial descriptions of only the critical parts of the embedding function [22 , 25 , 36 , 38 ] . Accepting that it is impractical to formally specify the actual embeddings of real refinement type checker implementations, we take a different approach to establish solid foundations for SMT-based verification: translation to foundational type systems.
We present Lava, a translation from the refinement type system Liquid Haskell to Rocq. Lava connects the two worlds of programs and SMT logics by modeling both in the formal system of Rocq and explicitly embedding program elements into logical ones. Thus, our translation offers both a) deeper insights into the foundations of refinement type checking and b) an automated certificate checker for Liquid Haskell verified programs. Our automated checker is able to automatically verify sizable examples, but crucially allows the user to manually intervene whenever automation fails.
Key Challenge of the Translation: Coexistence of Refined and Unrefined Semantics
The main difficulty in our translation is that Liquid Haskell has multiple semantic levels that coexist in programs and serve different purposes. Consider for example the following snippet in which a binder x x x , which is specified via a refinement type to be greater than 5, is used to divide 42:
let x :: {x:Int | 5 < x} = 8 in div 42 x -- div :: Int → {v:Int | 0 < v} → Int
Refinement types restrict the domain of a type, here I n t \mathrm{Int} Int , with a logical predicate. In div and below, we write I n t \mathrm{Int} Int for the trivially refined { v : I n t ∣ T r u e } \{v:\mathrm{Int} \mid\mathrm{True}\} { v : Int ∣ True } .
In this snippet, the binder x x x has three different semantics. When the snippet is seen as a Haskell program, the binder x x x is a a) program integer that can be evaluated to 8. But Liquid Haskell also attaches to x x x the information that it is greater than 5. Thus, in type theoretical terms, x x x is a b) dependent pair storing the evidence that the integer attached to x x x is greater than 5. Finally, at the call site of the division, Liquid Haskell will ensure that the arguments do not violate the specification of the function. In that case, since the second argument of division is specified to be greater than 0, it will create the verification condition ∀ x , 5 < x ⇒ 0 < x \forall x, 5 < x \Rightarrow0 < x ∀ x , 5 < x ⇒ 0 < x and check it using an SMT solver. When passed to the SMT solver, the binder x x x is a c) logical integer that admits arithmetic operations.
Two parallel worlds coexist:
the refined world of the Liquid Haskell type checker, where x x x is the b) dependent pair and
the unrefined world where x x x is a a) program integer or c) logical integer.
These two semantics are crucial for how refinement types work in Liquid Haskell. The Liquid Haskell type checker uses refined definitions in the program to generate the verification conditions (VCs), while the SMT solver uses unrefined definitions in the refinements to solve the VCs.
The key challenge of the translation is to account for these two intertwined semantics, while establishing a correspondence between the refined and unrefined translations of the same object.
Refined and Unrefined Semantics in Functions
The coexistence of refined and unrefined semantics becomes even more delicate once we move from simple values to functions. Functions in Liquid Haskell simultaneously inhabit both worlds: they are defined as Haskell programs with refinement type specifications, but are also used in the logical layer to specify refinements.
As an example of this dual nature, consider a function that gets an element from an integer list.
{-@ get :: xs:IList → i:{Int | 0 ≤ i ∧ i < llen xs} → Int @-}
get :: IList → Int → Int
get (Cons x xs') i = if i == 0 then x else get xs' (i-1)
The function get xs i returns the i-th element of the list xs. The Haskell definition is partial, so a refinement type specification (in Liquid Haskell’s special comment form {-@ ... @-}) ensures that the index argument is always in-bounds. This is the main use-case for refinement types: they guarantee the lack of runtime errors without the need to defensively handle out-of-bounds cases.
Haskell definitions are on the refined semantic level: each call get xs i generates a VC ensuring that the index i is in-bounds. Type refinements however are on the unrefined level: in specifications, get is treated as an SMT logical function, thus, it cannot be partial.
As a concrete example we prove that getting the i-th element of a list xs is the same as getting the (i+1)-th element of (Cons x xs):
{-@ thm_get :: xs:IList → x:Int → i:{Int | 0 ≤ i && i < llen xs}
→ { get xs i == get (Cons x xs) (i+1) } @-}
thm_get :: IList → Int → Int → ()
thm_get _ _ _ = ()
Liquid Haskell accepts this definition by unfolding the right-hand side get (Cons x xs) (i+1) into get xs i and then generating a VC that equates the two. Equality is checked by the SMT solver, thus get should be encoded as a logical function that models the behavior of the program function get. The full definition of this logical function in Rocq is given in § 3.5.2 .
The dual nature of functions in Liquid Haskell is challenging to capture in a Rocq translation, that has to define the refined and unrefined versions of get and then has to relate them properly.
The Design of Lava
We designed Lava, a translation from well-typed Liquid Haskell programs to Rocq that respects the semantics of Liquid Haskell by carefully accounting for both the coexistence of refined and unrefined semantics in Liquid Haskell programs as well as implicit subtyping.
Modeling Refined and Unrefined Semantics. To translate functions, like get, Lava generates two objects. One object models the refined semantics of the function, as a dependently typed Rocq function that includes the necessary proofs to ensure that the function respects its refinement type specification. The other object models the unrefined semantics of the function, as a Rocq relation that captures the graph of the function. Finally, Lava also generates and proves lemmas that relate the two semantics, ensuring that the refined function behaves according to its unrefined graph relation. To support higher-order functions, a refined higher-order function needs to take as input not only the refined variant of its input, but also the unrefined variant, a proof that the unrefined variant is a functional relation, and a proof connecting the two. We call this “function plus graph plus two proofs” a Pack; packing these four components together simplifies function subtyping.
Modeling Implicit Subtyping with Explicit Coercions. When translating Liquid Haskell programs, Lava systematically uses casts to model implicit subtyping, i.e., the typing rule T-SUB which lies at the core of many refinement type systems. Lava translates program definitions based on the type checking derivation tree and translates implicit subtyping to explicit type coercions. Those type coercions generate proof obligations. Along with the translation itself, we also provide a custom automation tactic that we call solver designed to discharge these obligations. The solver tactic is able to automatically discharge the proof obligations generated by our case studies (see § 6 ), closely mirroring the level of automation provided by Liquid Haskell’s SMT backend.
Implementation. We implemented Lava, a translation from programs verified in Liquid Haskell to Rocq. The implementation consists of three main components. First, the Haskell code implementing the “skeleton” translation of terms to Rocq. This translation is the main subject of the paper and it is presented over a core calculus for refinement types λ r \lambda_r λ r that is a subset of Liquid Haskell. In reality Lava translates Liquid Haskell terms to their refined and unrefined Rocq counterparts and translates implicit subtyping to explicit coercions. Second, a Rocq library with the definitions necessary to support our translation. Third, a collection of Rocq (Ltac) automation tactics that prove obligations due to refinements (in particular the solver tactic) or prove structural lemmas, notably those relating unrefined and refined terms.
Contributions. We contribute the design, metatheory, and implementation of Lava:
A foundational semantics for monomorphic Liquid Haskell (§ 3 ): The translation of verified Liquid Haskell programs to Rocq formalizes a semantics of Liquid Haskell and crystallizes the need for the two intertwined semantics. Our design supports monomorphic programs, with user defined data types and higher-order functions. The intricacy of the semantics lies in making all fine-grained details work together as a whole and required significant fine-tuning.
A correctness theorem for the translation (§ 4 ): We prove that the translation preserves the semantics of declared functions and types (theorems 4.1 and 4.2) and that inconsistencies are preserved (theorem 4.3), so Lava is a certificate checker for Liquid Haskell. The meta-theory sheds light on how to design such a translation for similar sources and targets.
An implementation and evaluation of Lava (§ 5 and § 6 ): Our tool consists of around 4K LoC of Haskell code implementing the skeleton translation, and around 6.5K LoC for the automation tactics and Rocq library. The implementation of the tool and automation tactics is a significant engineering effort. We evaluate the translation on eight representative Liquid Haskell programs.
While our work focuses on Liquid Haskell—and, more marginally, on Rocq—we believe it reveals fundamental aspects of the semantics of refinement type systems. It offers a basis for the translation of other refinement type frameworks to proof assistants.
Overview of The Translation
We begin with an informal overview of Lava, presenting the key features of the translation. Lava works in three steps. First, parsing the Liquid Haskell program into our core calculus λ r \lambda_r λ r , a step not described in this paper. Second, typechecking the λ r \lambda_r λ r program to add explicit casts annotations. Third, translating the elaborated terms into Rocq. The main focus of this overview is the third step, as we go we detail the three different sorts of casts added in the second step.
Our explanations are centered around the “safe” indexing function get from the introduction, defined on a type of integer lists arbitrarily restricted to elements greater than 5. We start by translating the element type and the list datatype, then translate get with a refined specification and to an unrefined graph relation, followed by the theorem thm_get . We finish with an example of a higher-order function (apply ) to show how higher-order values are handled.
Translation of Refinement Types
Liquid Haskell is a refinement type system for subset types built atop Haskell and it adopts Haskell’s underlying norms. Refinement types (aka subset types) { x : B ∣ r } \{x:B \mid r\} { x : B ∣ r } denote the set of elements e e e of type B B B such that r [ x ∖ e ] r[x \setminus e] r [ x ∖ e ] holds. Rocq also supports subset types, using the same notation to denote a dependent pair of an element of e : B e:B e : B and a proof of r [ x ∖ e ] r[x \setminus e] r [ x ∖ e ] .
Our list indexing example depends on integers. Liquid Haskell comes with a builtin integer type, I n t \mathit{Int} Int , with over- and underflow left as undefined behavior. We translate these underspecified machine integers to Rocq’s unbounded integer type Z \mathbb{Z} Z . We translate the Liquid Haskell type of integers greater than five { x : I n t ∣ 5 < x } \{x:\mathsf{Int} \mid5 < x\} { x : Int ∣ 5 < x } to Rocq (definition 3.6) as { x : Z ∣ < r e l 5 x t r u e } \{x:\mathbb{Z} \mid<_{\mathrm{rel}}\ 5\ x\ \mathsf{true}\} { x : Z ∣ < rel 5 x true } .
Refinement types translate to Rocq’s subset types, so refinements translate to Rocq’s propositions, in Prop. Here, 5 < x 5 < x 5 < x turns into a proposition about < r e l <_{\mathrm{rel}} < rel , the three-way relation modeling < < < on integers: ( 5 , x , t r u e ) ∈ < r e l (5,x,\mathsf{true}) \in<_{\mathrm{rel}} ( 5 , x , true ) ∈ < rel if and only if 5 < x 5 < x 5 < x . In the translation of refinements, we represent function calls (which includes integer operations) with inductive relations, as detailed in § 3.5.1 .
In Liquid Haskell, a value of type { x : I n t ∣ 5 < x } \{x:\mathsf{Int} \mid5 < x\} { x : Int ∣ 5 < x } is instantiated by a Haskell integer expression, as verification of the refinement is external. In Rocq, given x : Z x:\mathbb{Z} x : Z and p x : < r e l 5 x t r u e p_x: <_{\mathrm{rel}}\ 5\ x\ \mathsf{true} p x : < rel 5 x true , we build a value of the type { x : Z ∣ < r e l 5 x t r u e } \{x:\mathbb{Z} \mid<_{\mathrm{rel}}\ 5\ x\ \mathsf{true}\} { x : Z ∣ < rel 5 x true } as exist ( ∀ ( x : Z ) , < r e l 5 x ) t r u e x p x \operatorname{exist}\ (\forall(x:\mathbb{Z}), <_{\mathrm{rel}}\ 5\ x)\ \mathsf{true}\ x\ p_x exist ( ∀ ( x : Z ) , < rel 5 x ) true x p x , where exist is the constructor for dependent pairs, requiring both an integer expression x x x and a proof p x p_x p x .
On dependent pairs, the conversion between the unrefined and refined semantics of a value is straightforward by construction or destruction of dependent pairs (see § 2.3 ). Instead, for each inductive datatype and function, the translation must generate an unrefined and a refined version.
Translation of Refined Inductive Datatypes
Let us move on to our inductive datatype example: lists of integers greater than five. Liquid Haskell programs refine Haskell’s inductive datatypes with predicates on both the argument and return types of the constructors. Lava supports first-order monomorphic inductive datatypes.
{-@ data IList where
Nil :: IList
Cons_gt5 :: x:{Int | 5 < x} → IList → IList @-}
data IList where Nil :: IList ; Cons_gt5 :: Int → IList → IList
Mapping unrefined definitions to Rocq amounts to a change in syntax:
Inductive IList_u := Nil_u : IList_u | Cons_gt5_u : Z → IList_u → IList_u.
The unrefined type IList_u mimics the underlying Haskell type. It is both the type assigned to lists inside type refinements (in the logical, unrefined world) and the basis of the refined type IList.
Fixpoint IList_wf (xs : IList_u) : Prop := ...
Notation IList := {xs : IList_u | IList_wf xs ∧ True}.
Definition Nil : IList := ...
Definition Cons_gt5 (x:Z | <rel 5 x) (xs: IList) : IList := ...
Indeed, IList is a restriction of IList_u to well-formed values . Such values are characterized by a recursive refinement predicate [3 , 31 ] IList_wf: IList_u → Prop encoding the original refinements, defined by recursion on IList_u and asserting that all elements are greater than 5. The type IList also has a trivial refinement True, as refined datatypes can be further refined in Liquid Haskell, so Rocq’s IList notation corresponds to Liquid Haskell’s {v:IList | True}.
The refined constructors Nil and Cons are defined separately with the refined signature of the respective constructors. Rocq’s notation (x:Z | <rel 5 x) is syntactic sugar for (x:{x:Z | <rel 5 x}), in the same way that x:{Int | 5 < x} stands for x:{x:Int | 5 < x} in Liquid Haskell.
Our translation does not target standalone refined datatypes in Rocq directly—such translations are only sometimes possible. In Liquid Haskell, constructor refinements may refer to constructors:
{-@ ... ConsNonEmpty :: {x:Int | 5 < x} → {xs:IList | xs ≠ Nil} → IList @-}
Rocq’s type theory forbids such a refinement: we cannot use a constructor while defining the type. Instead we define the unrefined datatype with its constructors first, and since refinements use the unrefined constructors, we can use ConsNonEmpty_u while defining the well-formedness predicate.
Translation of Functions with Dependent Pattern Matching
We assume an IList-length function llen in Liquid Haskell, correctly translated to a refined definition llen and an unrefined graph relation llen_rel. Our example is the safe-indexing function get, which retrieves the element at a given index from an IList. Liquid Haskell functions typically specify refinement types atop their Haskell definition:
{-@ get :: xs:IList → i:{Int | 0 ≤ i ∧ i < llen xs} → {v:Int | 5 < v} @-}
get :: IList → Int → Int
get (Cons x xs') i = if i == 0 then x else get xs' (i-1)
We translate get to a Rocq function typed with a refined signature below. The IList argument is treated as trivially refined with True: types of top-level functions in Liquid Haskell are always refined. We translate it to Rocq as the subset type IList. The translation of the refinement of the index i uses the relations for <, ≤, and llen. For the last, it is necessary to introduce a fresh variable llen_xs capturing the result of the function call (see § 3.5.1 ). The return type is translated as before. The definition is given as a proof script terminated by Defined.
Definition get (xs:IList)
(i:Z | ≤rel 0 i true ∧ ∃ llen_xs, llen_rel ⌊|xs⌋ llen_xs ∧ <rel i llen_xs true)
: {v:Z | <rel 5 v true}.
destruct xs as [xs xs_p]. destruct i as [i i_p].
generalize dependent i; induction xs as [ | x xs' IH_xs' ]; intros.
- (* xs = Nil_u *) exfalso; solver.
- (* xs = Cons_gt5_u x xs' *) destruct (i ==? 0) eqn:?.
+ (* i = 0 *) exact (exist (∀(v:Z), <rel 5 v) x ltac:(solver)).
+ (* i ≠ 0 *) exact (IH_xs' ltac:(solver) (i-1) ltac:(solver)).
Defined.
The structure of the definition follows the structure of the original term, with an induction on the list mirroring the definition’s recursion. Using induction in a “definition by tactics” gives us dependent pattern matching, with propagation of the case analysis information, as well as an inductive hypothesis (IH_xs'). Tactics neatly handle the required bookkeeping for us — the main reason we use definitions by tactics. The parameter xs is destructed first into a value of type IList_u and the proof of well-formedness xs_p, which allows matching on the unrefined xs.
If xs = Nil_u, we have a contradiction: 0 ≤ i ∧ i < llen N i l 0 \le i \land i < \operatorname{llen}\ \mathrm{Nil} 0 ≤ i ∧ i < llen Nil , but llen N i l = 0 \operatorname{llen}\ \mathrm{Nil} = 0 llen Nil = 0 . We indicate unreachability of the branch by setting our goal to False with the exfalso tactic (for a contradiction). Our automation tactic solver identifies the contradictory assumptions and applies the principle of explosion appropriately, just as SMT would for Liquid Haskell.
On the other hand, if xs = Cons_gt5_u x xs', we use destruct to go by cases on whether i is 0, translating (==) in Liquid Haskell to a generic decision procedure (==?) in Rocq. Both branches are translated directly as (Gallina) λ-terms that are injected into the proof script by the tactic exact. In our translation, structural expressions (match, lambdas and let) are translated with tactics, while simpler terms (applications, literals...) are translated directly as λ-terms.
We could have used frameworks like Program [32 ] or Equations [33 ] to define get. Instead, we choose to rely only on basic Rocq tactics, which are flexible and reliable—a trade-off that works for our implementation (§ 7 ).
Injection and projection casts. During elaboration of our core calculus λ r \lambda_r λ r , we insert explicit injections and projections between refined and unrefined types. This is required by the intertwined semantics of Liquid Haskell, where terms are used as refined or unrefined in different contexts.
Injections annotate unrefined values. In λ r \lambda_r λ r , every term is considered refined, except for literals (Integers, Booleans and Unit) and first-order local variables. We use unrefined first-order variables, so they can be matched on and destructed in the Liquid Haskell code, like xs. This is also a pragmatic choice to simplify automated verification in Rocq: it is easier to deal with a deconstructed dependent pair than with the pair itself. We always destruct function parameters, like xs and i above.
In the elaboration of get , the occurrence of x from the branch i = 0 i = 0 i = 0 is annotated with an injection to a singleton type: inj ( x , { v : I n t ∣ 5 < v } ) \operatorname{inj}\ (x,\ \{v:\mathsf{Int}\mid5 < v\}) inj ( x , { v : Int ∣ 5 < v }) . The injection is translated as the dependent pair constructor exist . The refinement witness (the second element of the pair) is generated by our automation tactic solver.
Projections occur in specifications , when refined terms appear in that logical, unrefined world. In the type of get , the first parameter xs is of type I L i s t \mathsf{IList} IList , a refined type, but then appears in the refinement of i , as an argument to the logical function llen . The name xs in the refinement of i refers to the first projection of the refined xs . The elaboration phase adds the relevant annotation:
g e t : : x s : I L i s t → i : { I n t ∣ 0 ≤ i ∧ i < l l e n ( p r o j x s ) } → { v : I n t ∣ 5 < v } \mathsf{get} :: \mathsf{xs}:\mathsf{IList} \to\mathsf{i}:\{\mathsf{Int}\mid0 \leq\mathsf{i}\land\mathsf{i}<\mathsf{llen}\ (\mathsf{proj}\ \mathsf{xs})\} \to\{v:\mathsf{Int}\mid5 < v\} get :: xs : IList → i : { Int ∣ 0 ≤ i ∧ i < llen ( proj xs )} → { v : Int ∣ 5 < v }
A proj annotation is translated to a custom operation ⌊ ∣ ⋅ ∣ ⌋ \lfloor\mathord{|}\cdot\mathord{|}\rfloor ⌊ ∣ ⋅ ∣ ⌋ , generic on dependent pairs and packs (see § 2.6 ). On dependent pairs, ⌊ ∣ ⋅ ∣ ⌋ \lfloor\mathord{|}\cdot\mathord{|}\rfloor ⌊ ∣ ⋅ ∣ ⌋ is defined as Rocq’s builtin first projection proj1_sig.
In Liquid Haskell and in Rocq, term application triggers a substitution in the type. Consider for instance the partial application get Nil (ill-typed in Liquid Haskell, which is irrelevant here). To type this term, we substitute the variable xs by the argument Nil in the refinement of i :
g e t N i l : : i : { I n t ∣ 0 ≤ i ∧ i < l l e n ( p r o j N i l ) } → { v : I n t ∣ 5 < v } \mathsf{get}\ \mathsf{Nil} :: \mathsf{i}:\{\mathsf{Int}\mid0 \leq\mathsf{i}\land\mathsf{i}<\mathsf{llen}\ (\mathsf{proj}\ \mathsf{Nil})\} \to\{v:\mathsf{Int}\mid5 < v\} get Nil :: i : { Int ∣ 0 ≤ i ∧ i < llen ( proj Nil )} → { v : Int ∣ 5 < v }
When get Nil appears in a refined context, like a function definition, Nil is refined. However, llen in the refinement of i requires an unrefined argument. The projection is implicit in Liquid Haskell, but must be done explicitly in Rocq.
Translation of Functions as Unrefined Graph Relations
A Liquid Haskell user can reflect functions into the SMT logic [39 ] . While functions are encoded in SMT as uninterpreted symbols, refinement reflection essentially permits function unfolding, thus enables reasoning about the function’s definition.
The code that is reflected into the logic is the Haskell definition—unrefined . To be reflected, a function must terminate and be defined by structural induction. In Rocq, we must give a translation of the reflected Haskell function, to be used in the translation of refinements. This function can be partial in Haskell (but must be total on the refined domain, like get ), so we choose to encode it as an inductive predicate representing the graph of the function. We translate an n n n -ary function into an n + 1 n+1 n + 1 -ary relation in Prop (§ 3.3.2).
Assuming get is reflected using the directive {-> reflect get ->}, we generate an inductive predicate with two constructors, one for each branch of the conditional in get :
I n d u c t i v e g e t _ r e l : I L i s t _ u → Z → Z → P r o p : = ∣ g e t _ C o n s _ t r u e x x s ′ i : i = 0 → g e t _ r e l ( C o n s _ g t 5 _ u x x s ′ ) i x ∣ g e t _ C o n s _ f a l s e x x s ′ i : i ≠ 0 → ∀ ( i m 1 : Z ) , s u b Z _ r e l i 1 i m 1 → ∀ ( v : Z ) , g e t _ r e l x s ′ i m 1 v → g e t _ r e l ( C o n s _ g t 5 _ u x x s ′ ) i v . \begin{aligned}
\mathsf{Inductive}\ \mathsf{get\_rel} : \mathsf{IList\_u} \to\mathsf{Z} \to\mathsf{Z} \to\mathsf{Prop} := {}\\
\mid\ \mathsf{get\_Cons\_true}\ \mathsf{x}\ \mathsf{xs'}\ \mathsf{i} : \mathsf{i} = 0 \to\mathsf{get\_rel}\ (\mathsf{Cons\_gt5\_u}\ \mathsf{x}\ \mathsf{xs'})\ \mathsf{i}\ \mathsf{x}\\
\mid\ \mathsf{get\_Cons\_false}\ \mathsf{x}\ \mathsf{xs'}\ \mathsf{i} : \mathsf{i} \ne0 \to\forall(\mathsf{im1}:\mathsf{Z}),\ \mathsf{subZ\_rel}\ \mathsf{i}\ 1\ \mathsf{im1}\\
\qquad\to\forall(\mathsf{v}:\mathsf{Z}),\ \mathsf{get\_rel}\ \mathsf{xs'}\ \mathsf{im1}\ \mathsf{v} \to\mathsf{get\_rel}\ (\mathsf{Cons\_gt5\_u}\ \mathsf{x}\ \mathsf{xs'})\ \mathsf{i}\ \mathsf{v}.
\end{aligned} Inductive get_rel : IList_u → Z → Z → Prop := ∣ get_Cons_true x x s ′ i : i = 0 → get_rel ( Cons_gt5_u x x s ′ ) i x ∣ get_Cons_false x x s ′ i : i = 0 → ∀ ( im1 : Z ) , subZ_rel i 1 im1 → ∀ ( v : Z ) , get_rel x s ′ im1 v → get_rel ( Cons_gt5_u x x s ′ ) i v .
We replace the function calls by relation calls (introducing fresh variables), as we are in an unrefined context. This includes operator calls for homogeneity and because some operators can be partial. Notice that get_rel has no constructor for Nil_u, because this case is absent from the Haskell original code. In general, the inductive predicate can hold for elements outside of the refined domain. Assume that get xs i has an additional equation get Nil i = 0 in Liquid Haskell. In that case, get_rel has a third constructor get_rel_Nil i : get_rel Nil_u i 0. The relation is consistent with the evaluation of get Nil i to 0 in Haskell.
Crucially, we automatically prove (using custom tactics) that the graph denotes a function (functionhood ) and that functions and their graphs coincide on the refined domain (coherence ):
Lemma get_funct xs i v v' : get_rel xs i v → get_rel xs i v' → v = v'.
Lemma get__get_rel xs i v : ⌊|get xs i|⌋ = v ↔ get_rel ⌊|xs|⌋ ⌊|i|⌋ v.
After proving coherence, we mark get as opaque, in other words, its body is invisible in later contexts. This way, only the graph relation is available to automated reasoning. This mimics the reflection of the unrefined function into the logical context for verification in Liquid Haskell.
We also define a relation for unreflected functions, but in terms of the refined definition and with a single constructor. Supposing that get were not reflected, we infer the relation:
Inductive get_rel2 : IList_u → Z → Z → Prop :=
| get_xs_i (xs:IList) (i:Z | ...) (v:Z) : ⌊|get xs i|⌋ = v → get_rel2 ⌊|xs|⌋ ⌊|i|⌋ v.
The functionhood and coherence lemmas trivially hold in this case. Since we made the definition of the refined get opaque, reasoning using the definition of get is not possible. This mimics the encoding of unreflected functions as uninterpreted symbols in the logic.
Theorems and Subtyping
The graph relation get_rel is used in the translation of thm_get from the introduction.
Definition thm_get (xs:IList) (x:Z | <rel 5 x true)
(i:Z | ≤rel 0 i true ∧ ∃ llen_xs, llen_rel ⌊|xs|⌋ llen_xs ∧ <rel i llen_xs true)
: {{ ∃ get_xs, get_rel ⌊|xs|⌋ ⌊|i|⌋ get_xs ∧ ∃ ip1, +rel ⌊|i|⌋ 1 ip1
∧ ∃ get_C, get_rel (Cons_u ⌊|x|⌋ ⌊|xs|⌋) ip1 get_C ∧ get_xs == get_C }}.
destruct xs as [xs xs_p]. destruct i as [i i_p].
exact (subCast (∀(u:unit), u == tt) (∀(u:unit), ∃ get_xs ... get_xs == get_C)
(exist (∀(u:unit), u == tt) tt ltac:(solver))).
Defined.
A theorem in Liquid Haskell is a function returning a refinement of the unit (where { e } stands for {u:() | e}). In particular, theorems may be defined by pattern matching on the arguments. Thus, we translate theorems in the same way as any other function (but do not generate graph relations or packs). In Rocq, we write {{ e }} for {u:unit | e }. tt is the unit term.
In Liquid Haskell, verification of get_rel is automatic: the “proof” is just the unit (). A single verification condition is generated by the subtyping constraint from the singleton type {u:() | u == ()} of () to thm_get’s return type. Using the notations from the introduction, Liquid Haskell verifies:
SMT-Valid ( ⟦ 5 < x ⟧ ∧ ⟦ 0 ≤ i ∧ i < llen x s ⟧ ⇒ ⟦ u = = ( ) ⟧ ) ⇒ ⟦ get x s i = = get ( Cons x x s ) ( i + 1 ) ⟧ \operatorname{SMT\text{-}Valid}\left(\llbracket5 < x \rrbracket\land\llbracket0 \le i \land i < \operatorname{llen}\ xs \rrbracket\Rightarrow\llbracket u == () \rrbracket\right)
\Rightarrow\llbracket\operatorname{get}\ xs\ i == \operatorname{get}\ (\operatorname{Cons}\ x\ xs)\ (i+1) \rrbracket SMT - Valid ( [ [ 5 < x ] ] ∧ [ [ 0 ≤ i ∧ i < llen x s ] ] ⇒ [ [ u == ( ) ] ] ) ⇒ [ [ get x s i == get ( Cons x x s ) ( i + 1 ) ] ]
In our intermediate calculus λ r \lambda_r λ r , we do not check such implications: subtyping is an axiom, effectively transforming Liquid Haskell’s subtyping rule S-Base to S-Base-Ax below:
\begin{aligned}
\frac{\operatorname{SMT\text{-}Valid}\left(\left\llbracket\Gamma\right\rrbracket\Rightarrow\left\llbracket r_l\right\rrbracket\Rightarrow\left\llbracket r_r\right\rrbracket\right)}{\Gamma\vdash\{x:B \mid r_l\} \preceq\{x:B \mid r_r\}} \quad\text{S-Base}
\qquad
\frac{}{\Gamma\vdash\{x:B \mid r_l\} \preceq\{x:B \mid r_r\}} \quad\text{S-Base-Ax}
\end{aligned}
Verification is the responsibility first of Liquid Haskell, then of Rocq. However, during elaboration, we make all (non-trivial) subtyping occurrences explicit, by adding a subsumption cast annotation. The body of thm_get is elaborated as:
sub (inj ((), {u:() | u == ()}),
{u:() | u == ()}, {u:() | get xs i == get (Cons x xs) (i+1)})
The subsumption annotation takes the source and target type as second and third parameters. As any literal, ( ) () ( ) is injected into its singleton type.
The body of Rocq’s thm_get is a direct translation of this elaborated term. It uses a custom cast operation subCast (described in § 3.6.1 ). Verification is handled by our automation tactic, as always.
Verification of thm_get in Liquid Haskell involves a step of evalution of the right side of the equality. This is permitted by reflection of get . In Rocq, the proof is also only possible if get is reflected, by inversion on get_Cons_false.
Function Packs
With first-order objects (datatypes or literals), it is easy to cast between refined and unrefined with injections and projections. But functions are first-class objects of Haskell, and are also used implicitly as refined or unrefined terms in Liquid Haskell depending on the context. Therefore, inside the translation of a Liquid Haskell function, we must be able to use higher-order values both as refined functions and as inductive graph relations.
To this aim, we translate higher-order values to records storing the refined definition and the unrefined graph relation, as well as the coherence lemma and functionhood property. This record is called a pack , its signature is parameterized by an arrow type.1 We always create a pack when translating a top-level function. For get, we have:
Instance get_pack : Pack (∀(xs:IList) (i:Z | ...), {v:Z | <rel 5 v true>}) :=
{| def := get; rel := get_rel; f_f_rel := get__get_rel; funct := get_funct |}.
For an example use of packs, consider a refined apply function:
{-@ reflect apply @-}
{-@ apply :: f:(xs:IList → i:{Int | ...} → {v:Int | 5 < v})
→ xs:IList → {v:Int | v = f xs} @-}
apply :: (IList → Int → Int) → IList → Int
apply f xs i = f xs i
The functional argument f f f appears unrefined in the output refinement, but refined in the body of apply . Its type is translated to a pack signature , making both versions of f f f available:
Definition apply (f: Pack (∀(xs:IList) (i:Z | ...), {v:Z | <rel 5 v true}))
(xs:IList) (i:Z | ...) : {v:Z | f.(rel) xs v}.
destruct xs as [xs_u xs_p], i as [i_u i_p].
exact (subCast (∀v, ≤rel 0 v true) (∀v, f.(rel) xs_u i_u v)
(f.(def) (exist _ xs ltac:(solver)) (exist _ i ltac:(solver)))).
Defined.
In the output refinement, we use the graph relation f . ( r e l ) f.(\mathrm{rel}) f . ( rel ) from the pack, while in the body we use the refined function f . ( d e f ) f.(\mathrm{def}) f . ( def ) .2 The function apply can be applied to the pack get_pack . Then, the projection f . ( r e l ) f.(\mathrm{rel}) f . ( rel ) is instantiated to get_rel , while f . ( d e f ) f.(\mathrm{def}) f . ( def ) is instantiated to get .
In the unrefined world, when building the relation apply , we also use a pack for the higher-order arguments, this time an unrefined pack. Unrefined packs only contain the two unrefined elements of the packs: the relation (used below with f . ( r e l _ u ) f.(\mathrm{rel\_u}) f . ( rel_u ) ) and the functionhood property.
Inductive apply_rel : uPack (IList_u → Z → Z) → IList_u → Z → Z → Prop :=
| apply_f_xs f xs i : ∀ v, f.(rel_u) xs i v → apply_rel f xs i v.
To instantiate apply_rel with get , we define a projection from refined to unrefined packs (here from Pack (IList → { v : Z ∣ ≤ r e l 0 v t r u e } \{v:Z \mid\leq_{\mathrm{rel}} 0\ v\ \mathrm{true}\} { v : Z ∣ ≤ rel 0 v true } ) to uPack (IList_u → Z)). This projection simply forgets two of the fields to keep only the graph relation and functionhood property.
The Translation
Lava translates Liquid Haskell to Rocq in three steps. The first step elaborates Haskell’s internal representation Core [35 ] and Liquid Haskell specifications into our calculus λ r \lambda_r λ r (presented below).
The second step elaborates the terms of λ r \lambda_r λ r to prepare the translation. Liquid Haskell uses implicit subtyping [28 ] and binders can appear as refined or unrefined according to the context. Therefore, elaboration converts subtyping constraints to type casts in λ r \lambda_r λ r -terms (§ 3.2 ).
The third step is the translation of annotated terms into Rocq, detailed in § 3.3 to § 3.6 .
Elements of the Translation and Internal Representation
Syntax of λ r \lambda_r λ r
Figure 1 presents the syntax of λ r \lambda_r λ r , a core λ \lambda λ -calculus with refinement types, user-defined first-order inductive data types and functions. The core grammar of λ r \lambda_r λ r is in black, while the elaborated grammar includes the terms in gray. This work does not cover the following features of Liquid Haskell: polymorphism, partial application and lambdas in refinements or reflected functions, data constructors with higher-order parameters, and non-structurally recursive functions.
Figure 1. Syntax of λ r \lambda_r λ r ; its monomorphic base types B B B are built into more general refined types R R R . We write ( ⋅ ) (\cdot) ( ⋅ ) for a sequence of zero or more elements, ( ⋅ ) + (\cdot)^+ ( ⋅ ) + for a non-empty sequence and ( ⋅ ) ? (\cdot)^? ( ⋅ ) ? for an optional annotation. In gray, the extended grammar after typing.
The calculus λ r \lambda_r λ r uses the metavariables T C TC T C and C C C for type and data constructors, while the term variables are drawn from the set of all variables V ⊇ { x , y , z , f , g , v } \mathcal{V} \supseteq\{x,y,z,f,g,v\} V ⊇ { x , y , z , f , g , v } . The name v v v is reserved for binders in refinement types. In the extended syntax, variables are annotated with their arity (n n n ) and their scope: whether they are top-level (G G G ), local (L L L ), or the name of the function being defined (Y Y Y ).
We define λ r \lambda_r λ r ’s types in two layers: base and refinement types. A base type (B B B ) is either built-in (Unit, Boolean or Integer) or a user-defined type constructor. A refinement type (R R R ) is either a base type refined by a proposition or a function between two refinement types. In function types, the codomain’s refinement can refer to the domain. We abbreviate the trivial refinement { x : B ∣ T r u e } \{x:B \mid\mathrm{True}\} { x : B ∣ True } to { B } \{B\} { B } ; we abbreviate x 1 : R 1 → ⋯ → x n : R n → R x_1:R_1 \to\cdots\to x_n:R_n \to R x 1 : R 1 → ⋯ → x n : R n → R to ( x i : R i ) i ≤ n → R (x_i:R_i)_{i\leq n} \to R ( x i : R i ) i ≤ n → R , where n n n is maximal.
A λ r \lambda_r λ r program is a sequence of declarations (D D D ), where each declaration defines either a type constructor or a function. Just like in Haskell, every function can be recursive; just like in Liquid Haskell, every function can be reflected into the logic. Top-level function definitions contain implicit
Base Types B : : = ( ) ∣ B o o l ∣ I n t ∣ T C Refinement Types R : : = { x : B ∣ r } ∣ x : R → R Declarations D : : = d a t a T C : = ( C : : R ) + ∣ ( r e f l e c t ) ? d e f f : : R : = e Expressions e : : = r ∣ l e t x : : R : = e i n e ∣ e ? ( r p r o v e s r ) ∣ i f r t h e n e e l s e e : : = ∣ c a s e r o f ( C x ↦ e ) ∣ u n r e a c h a b l e Refinements r : : = x / ( n , s ) ∣ ℓ ∣ C ∣ r r ∣ ¬ r ∣ r 1 = r 2 ∣ r 1 ⊙ r 2 s ∈ { G , L , Y } : : = ∣ i n j ( r , R ) ∣ s u b ( r , R , R ) ∣ p r o j ( r ) Literals ℓ : : = ( ) ∣ T r u e ∣ F a l s e ∣ 0 , − 1 , 1 … Binary Operators ⊙ : : = + ∣ − ∣ × ∣ / ∣ < ∣ ≤ ∣ > ∣ ≥ ∣ ⇒ ∣ ∧ ∣ ∨ ∣ ⇔ \begin{aligned}
\textit{Base Types}\quad B &::= () \mid\mathsf{Bool} \mid\mathsf{Int} \mid\mathsf{TC}\\
\textit{Refinement Types}\quad R &::= \{x:B \mid r\} \mid x:R \to R\\
\textit{Declarations}\quad D &::= \mathsf{data}\ TC := (C :: R)^{+} \mid(\mathsf{reflect})^{?}\ \mathsf{def}\ f :: R := e\\
\textit{Expressions}\quad e &::= r \mid\mathsf{let}\ x :: R := e\ \mathsf{in}\ e \mid e\ ?\ (r\ \mathsf{proves}\ r) \mid\mathsf{if}\ r\ \mathsf{then}\ e\ \mathsf{else}\ e\\
&\phantom{::=}\mid\mathsf{case}\ r\ \mathsf{of}\ (C\ x \mapsto e) \mid\mathsf{unreachable}\\
\textit{Refinements}\quad r &::= x/(n,s) \mid\ell\mid C \mid r\ r \mid\neg r \mid r_1 = r_2 \mid r_1 \mathbin{\odot} r_2 \qquad s \in\{G,L,Y\}\\
&\phantom{::=}\mid\mathsf{inj}(r,R) \mid\mathsf{sub}(r,R,R) \mid\mathsf{proj}(r)\\
\textit{Literals}\quad\ell&::= () \mid\mathsf{True} \mid\mathsf{False} \mid0,-1,1\ldots\\
\textit{Binary Operators}\quad\odot&::= + \mid- \mid\times\mid/ \mid< \mid\le\mid> \mid\ge\mid\Rightarrow\mid\wedge\mid\vee\mid\Leftrightarrow
\end{aligned} Base Types B Refinement Types R Declarations D Expressions e Refinements r Literals ℓ Binary Operators ⊙ ::= ( ) ∣ Bool ∣ Int ∣ TC ::= { x : B ∣ r } ∣ x : R → R ::= data T C := ( C :: R ) + ∣ ( reflect ) ? def f :: R := e ::= r ∣ let x :: R := e in e ∣ e ? ( r proves r ) ∣ if r then e else e ::= ∣ case r of ( C x ↦ e ) ∣ unreachable ::= x / ( n , s ) ∣ ℓ ∣ C ∣ r r ∣ ¬ r ∣ r 1 = r 2 ∣ r 1 ⊙ r 2 s ∈ { G , L , Y } ::= ∣ inj ( r , R ) ∣ sub ( r , R , R ) ∣ proj ( r ) ::= ( ) ∣ True ∣ False ∣ 0 , − 1 , 1 … ::= + ∣ − ∣ × ∣ / ∣<∣≤∣>∣≥∣⇒∣ ∧ ∣ ∨ ∣⇔
Table 1. The core functions of the Lava translation from λ r \lambda_r λ r to Rocq.
λ \lambda λ -binders and are η \eta η -long: (reflect ) def f : : x 1 : R 1 → ⋯ → x n : R n → { x : B ∣ r } : = e f :: x_1:R_1 \to\cdots\to x_n:R_n \to\{x:B \mid r\} := e f :: x 1 : R 1 → ⋯ → x n : R n → { x : B ∣ r } := e binds the formal parameters x 1 x_1 x 1 through x n x_n x n of f f f . Type constructor definitions must be first-order: every constructor’s arguments must be refinements of base types, not arrow types.
The body of a definition is a λ r \lambda_r λ r expression (e e e ). We use a form of generalized ANF [29 ] , where expressions are broken into two levels: refinements r r r and structural expressions e e e . The upper level contains let-bindings, conditionals, pattern matching, as well as proof hints (e ? r h e\ ?\ r_h e ? r h ) that provide hints (to the SMT solver) to prove theorems. Let-bindings are annotated with a refinement type: like in top-level declarations, if the type is an arrow, it binds the formal parameters of e e e . In other words, all λ \lambda λ -abstractions are implicit and let-bound. Pattern matching can be partial, but all missing branches are signaled as unreachable .
Refinements r r r are both expressions and type refinements. We distinguish Boolean equality from binary operators. Binary operators (⊙ \odot ⊙ ) are primitives for arithmetic, equality, and Boolean logic. Literals (ℓ \ell ℓ ) are Unit, Boolean, or Integer values. The extended grammar has three type casts: injections from base to refined types, subsumptions over refinement types, and projections from refined to base types.
Structure of the translation from λ r \lambda_r λ r to Rocq
The translation of λ r \lambda_r λ r is performed by the seven functions summarized in § 3.1.2 . The translation follows the structure of a Liquid Haskell file and translates each datatype or function declaration independently with DtoR (§ 3.3 ) outputting both a refined and an unrefined version of the declaration. Types, refinements, and expressions have two sets of translations: to the unrefined world (§ 3.5 ) and to the refined world (§ 3.6 ). In the unrefined world, type refinements are translated to propositions (RtoP) and refinement expressions to arbitrary terms (RtoU). In the refined world, expressions are translated as tactics (EtoTac).
Elaboration of Explicit Type Casts
Before translating λ r \lambda_r λ r to Rocq, Lava performs an elaboration phase turning implicit subtyping into explicit type casts, via well-formedness and typing derivation rules mirroring Liquid Haskell rules. We expand the explanations of the overview (§ 2 ) with formal definitions of the significant rules. The remaining rules of the type system pose no surprise and are given in § A.
Elaboration of refinement types. We define a well-formedness judgment on types that annotates type refinements with projections and pairs arity/scope on the variables. For instance, the full elaboration of the type of get (§ 2.3 ) is:
x s : I L i s t → i : { I n t ∣ 0 ≤ i / ( 0 , L ) ∧ i / ( 0 , L ) < l l e n / ( 1 , G ) ( p r o j x s / ( 0 , L ) ) } → { v : I n t ∣ 5 < v / ( 0 , L ) } \mathsf{xs}:\mathsf{IList} \to i:\{\mathsf{Int}\mid0 \le i_{/(0,L)} \land i_{/(0,L)} < \mathsf{llen}_{/(1,G)}(\mathsf{proj}\ \mathsf{xs}_{/(0,L)})\} \to\{v:\mathsf{Int}\mid5 < v_{/(0,L)}\} xs : IList → i : { Int ∣ 0 ≤ i / ( 0 , L ) ∧ i / ( 0 , L ) < llen / ( 1 , G ) ( proj xs / ( 0 , L ) )} → { v : Int ∣ 5 < v / ( 0 , L ) }
Well-formedness is checked by two rules:
if B = T C , then B ∈ dom ( Γ ) Γ , x : { B } ⊢ S I M P L E r : B o o l ⇝ r e Γ ⊢ { x : B ∣ r } ⇝ { x : B ∣ r e } W F - T R e f \frac{\begin{gathered}
\text{if }B = TC,\text{ then }B \in\operatorname{dom}(\Gamma)\\
\Gamma,x:\{B\}\vdash_{\mathrm{SIMPLE}} r:\mathrm{Bool}\rightsquigarrow r^e
\end{gathered}}
{\Gamma\vdash\{x:B\mid r\}\rightsquigarrow\{x:B\mid r^e\}}
\quad\mathrm{WF\text{-}TRef} Γ ⊢ { x : B ∣ r } ⇝ { x : B ∣ r e } if B = T C , then B ∈ dom ( Γ ) Γ , x : { B } ⊢ SIMPLE r : Bool ⇝ r e WF - TRef
Γ ⊢ R x ⇝ R x e Γ , x : R x e ⊢ R ⇝ R e Γ ⊢ x : R x → R ⇝ x : R x e → R e [ x \ proj ( x ) ] W F - T F u n \frac{\Gamma\vdash R_x\rightsquigarrow R_x^e \qquad\Gamma,x:R_x^e\vdash R\rightsquigarrow R^e}
{\Gamma\vdash x:R_x\to R\rightsquigarrow x:R_x^e\to R^e[x\backslash\operatorname{proj}(x)]}
\quad\mathrm{WF\text{-}TFun} Γ ⊢ x : R x → R ⇝ x : R x e → R e [ x \ proj ( x )] Γ ⊢ R x ⇝ R x e Γ , x : R x e ⊢ R ⇝ R e WF - TFun
Rule WF-TRef checks that type refinements r r r are Boolean in a simple type system (defined in § A). Additionally, it annotates the variables inside of r r r with their arities and scopes. The scope is either L L L , for variables local to the function, or G G G , for variables defined at top-level. We do not typecheck refinements with a refinement type system as we only want to generate type annotations and add projections, not check that refinements are satisfied. In this and in all other rules, Γ \Gamma Γ is a typing environment populated with elaborated types.
Rule WF-TFun elaborates the arrow type x : R x → R x:R_x \to R x : R x → R by projecting the occurrences of x x x in refinements of R R R . The definition of the substitution is standard, it is capture-avoiding and assumes all bound variables are distinct.
Subsumption. The elaboration of expressions introduces subsumptions and injections, as well as the arity/scope annotations on variables. Our type system for expressions is bidirectional : the types of refinements are synthesized (⇒ \Rightarrow ⇒ ), i.e., are an output of the judgment, while structural expressions (lets, conditionals, case and hints) are checked (⇐ \Leftarrow ⇐ ) against a type. Both kinds of judgments output an annotated term.
As usual in bidirectional systems, subtyping is where synthesis meets checking: the synthesized type R s R_s R s must be a subtype of the required type R t R_t R t (both are already elaborated by construction).
Γ ⊢ r ⇒ R s ⇝ r e Γ ⊢ R s ≤ R t Γ ⊢ r ⇐ R t ⇝ sub ( r e , R s , R t ) C - S y n \frac{\Gamma\vdash r\Rightarrow R_s\rightsquigarrow r^e \qquad\Gamma\vdash R_s\leq R_t}
{\Gamma\vdash r\Leftarrow R_t\rightsquigarrow\operatorname{sub}(r^e,R_s,R_t)}
\quad\mathrm{C\text{-}Syn} Γ ⊢ r ⇐ R t ⇝ sub ( r e , R s , R t ) Γ ⊢ r ⇒ R s ⇝ r e Γ ⊢ R s ≤ R t C - Syn
Since types of structural expressions are only ever checked, subsumption only applies to refinements, so the rule is defined on a refinement r r r rather than an expression e e e . However, subtyping applies on any kind of refinement type R R R : a subset or an arrow. The subtyping judgment is contravariant on arrows, and valid for any two subset of the same base type (with the rule given in § 2.3 ).
Injection. There are two unrefined atoms of λ r \lambda_r λ r : literals and local first-order variables. They are elaborated by an injection into a subset type (our judgments only derive refinement types):
Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ ) ) S - L i t ( x / L : R ) ∈ Γ arity ( R ) = 0 Γ ⊢ x ⇒ R ⇝ inj ( x / ( 0 , L ) , R ) S - V a r L \frac{}{\Gamma\vdash\ell\Rightarrow\operatorname{ty}(\ell)\rightsquigarrow\operatorname{inj}(\ell,\operatorname{ty}(\ell))}
\quad\mathrm{S\text{-}Lit}
\qquad
\frac{(x_{/L:R})\in\Gamma\qquad\operatorname{arity}(R)=0}
{\Gamma\vdash x\Rightarrow R\rightsquigarrow\operatorname{inj}(x_{/(0,L)},R)}
\quad\mathrm{S\text{-}VarL} Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ )) S - Lit Γ ⊢ x ⇒ R ⇝ inj ( x / ( 0 , L ) , R ) ( x / L : R ) ∈ Γ arity ( R ) = 0 S - VarL
Inductive IList_u := Nil_u : IList_u | Cons_gt5_u : Z → IList_u → IList_u.
Fixpoint IList_wf (xs:IList_u) : Prop :=
match xs with
| Nil_u => True (* refinement for the conclusion (trivial) *)
| Cons_gt5_u x xs' => <rel 5 x true (* refinement on x (5 < x) *)
∧ True (* refinement for xs' (trivial) *)
∧ IList_wf xs' (* recursion on xs' *)
∧ True (* refinement for the conclusion (trivial) *)
end.
Notation IList := {xs:IList_u | IList_wf xs ∧ True}.
Definition Nil : IList := exist _ Nil_u ltac:(solver).
Definition Cons_gt5 (x:Z | <rel 5 x) (xs:IList) : IList :=
exist _ (Cons_gt5_u ⌊x⌋ ⌊xs⌋) ltac:(solver).
Listing 1. Translation of IList
Rule S-Lit casts a literal into a singleton type (or selfified type [26 ] ) ty ( ℓ ) = { v : B ∣ v / ( 0 , L ) = = ℓ } \operatorname{ty}(\ell) = \{v:B \mid v/(0,L) == \ell\} ty ( ℓ ) = { v : B ∣ v / ( 0 , L ) == ℓ } , with B B B the expected base type of ℓ \ell ℓ . Rule S-VarL is the rule for a local variable. The scope is present in the environment and inserted into the elaborated term.
Application. The application rule is important, even though it does not add annotations by itself.
Γ ⊢ r 1 ⇒ x : R x → R ⇝ r 1 e Γ ⊢ r 2 ⇐ R x ⇝ r 2 e Γ ⊢ r 1 r 2 ⇒ R [ x \ r 2 e ] ⇝ r 1 e r 2 e \frac{\Gamma\vdash r_1 \Rightarrow x:R_x \to R \rightsquigarrow r_1^e \qquad\Gamma\vdash r_2 \Leftarrow R_x \rightsquigarrow r_2^e}{\Gamma\vdash r_1\ r_2 \Rightarrow R[x \backslash r_2^e] \rightsquigarrow r_1^e\ r_2^e} Γ ⊢ r 1 r 2 ⇒ R [ x \ r 2 e ] ⇝ r 1 e r 2 e Γ ⊢ r 1 ⇒ x : R x → R ⇝ r 1 e Γ ⊢ r 2 ⇐ R x ⇝ r 2 e
The type of the argument is checked , meaning that a subsumption annotation will be inserted on the argument, as the argument is a refinement whose type will be synthesized.
More importantly, the (elaborated) argument is substituted into the refinements of R R R . This motivates the syntactic restriction allowing only refinements as arguments. It also justifies surrounding x x x by projections in the type refinements of R R R , as r 2 e r_2^e r 2 e has a refinement type.
Hints. Our formalization explicitly considers “proof hints” e ? r h e\ ?\ r_h e ? r h , a common technique in Liquid Haskell for adding a fact (r h r_h r h ) for SMT to use while considering e e e . The term e ? r h e\ ?\ r_h e ? r h elaborates to e ? ( r h p r o v e s r p ) e\ ?\ (r_h\ \mathrm{proves}\ r_p) e ? ( r h proves r p ) , where r h r_h r h is the proof term given as hint and r p r_p r p is the proposition (the refinement of the type of r h r_h r h ) that it proves.
Γ ⊢ r h ⇒ { x : ( ) ∣ r p } ⇝ r h e Γ , x / L : { x : ( ) ∣ r p } ⊢ e ⇐ R ⇝ e e Γ ⊢ e ? r h ⇐ R ⇝ e e ? ( r h e p r o v e s r p ) \frac{\Gamma\vdash r_h \Rightarrow\{x:() \mid r_p\} \rightsquigarrow r_h^e \qquad\Gamma, x/L:\{x:() \mid r_p\} \vdash e \Leftarrow R \rightsquigarrow e^e}{\Gamma\vdash e\ ?\ r_h \Leftarrow R \rightsquigarrow e^e\ ?\ (r_h^e\ \mathrm{proves}\ r_p)} Γ ⊢ e ? r h ⇐ R ⇝ e e ? ( r h e proves r p ) Γ ⊢ r h ⇒ { x : ( ) ∣ r p } ⇝ r h e Γ , x / L : { x : ( ) ∣ r p } ⊢ e ⇐ R ⇝ e e
Top-level Translation of Data Type and Function Declarations
The translation of programs follows the syntactic structure, translating datatypes (§ 3.3.1 ) and functions (§ 3.4 ), using the function DtoR (§ 3.1.2 ). The translation also generates additional lemmas and proof automation hints; omitted here but detailed in § E.1.
Translation of Inductive Datatype Declarations
Recall the refined datatype IList from § 2.2 , and its translation given in listing 1 (with complete definitions of IList_wf, Nil and Cons):
data IList where Nil :: IList | Cons_gt5 :: x:{Int | 5 < x} → IList → IList
The translation of a refined datatype T C TC T C (Figure 2 ) generates the same kinds of Rocq declarations: the unrefined datatype T C u TC_u T C u , the well-formedness predicate w f T C \mathrm{wf}_{TC} wf T C , the subset type T C TC T C and the refined constructors C 1 , … , C n C_1,\ldots,C_n C 1 , … , C n . In the well-formedness predicate, we use a substitution σ \sigma σ to replace the projections of the constructor parameters by the parameters themselves, as they are unrefined in that context. The translation also instantiates two equalities (not shown here): == (returning Prop) and ==? (returning bool).
DtoR(data TC := (Cᵢ :: (xᵢʲ:Rᵢʲ)ⱼ≤ₘᵢ → {x:TC | rᵢ})ᵢ≤ₙ) ≜
Inductive TCᵤ: Set := (| Cᵢ : TtoU(Rᵢ¹) → ... → TtoU(Rᵢᵐⁱ) → TCᵤ)ᵢ≤ₙ.
Fixpoint wfTC (x:TCᵤ): Prop :=
match x with | Cᵢ xᵢ¹ ... xᵢᵐⁱ ↦ RtoP(rᵢσ[x\Cᵢ x₁ ... xᵐⁱ]) ∧ ∧ⱼ≤ₘᵢ Pᵢʲ end.
Global Notation TC := {x:TCᵤ | wfTC x ∧ True}.
(Definition Cᵢ(xᵢʲ:TtoR(Rᵢʲ; ∅))ⱼ≤ₘᵢ : TtoR(R;∅) :=
exist (λx, wfTC x ∧ True) (Cᵢ|xᵢ¹|...|xᵢᵐⁱ|) ltac:(solver).)ᵢ≤ₙ
where σ = [proj(xᵢᵏ)]ₖ≤ₘᵢ and {_:_ | pᵢʲ} ≜ TtoR(Rᵢʲσ).Figure 2. Translation of an inductive datatype. See § 3.1.2 for the definitions of the auxiliary declarations.
Table 2. Declarations in the translation of get
Translation of Function Declarations
We recall the translation of get in listing 2. Formally, the translation of a function f f f (Figure 3 ) creates the same elements: the refined function f f f , the unrefined graph relation f f f _rel, the coherence lemma f f f _f_rel, the functionhood lemma f f f _rel_funct, the refined pack f f f _pack and the unrefined pack f f f _upack 3 .
Figure 3. Translation of a function declaration
The definition of the unrefined graph relation depends on whether f f f is reflected. If f f f is not, we disallow computation on the relation: the translation builds a generic, opaque graph relation (§ 2.4 ) with the tactic fun_to_rel. If f f f is reflected, its definition is encoded in the inductive f f f _rel.
DtoR((reflect)? def f :: Rf := e) :=
Definition f (xᵢ : TtoR(Rᵢ; ∅))ᵢ≤n: TtoR(R; ∅).
destruct (xᵢ as [xᵢ x̂ᵢ])ᵢ∈I . EtoTac(e) Defined.
Inductive f_rel: TtoU(R₁) → . . . → TtoU(Rₙ) → TtoU(R) → Prop :=
if reflect then (| fᵖ : pathInd(f, p))ₚ∈paths(e;x₁,...,xₙ) else ltac:(fun_to_rel f).
Lemma f_rel_funct (xᵢ : TtoU(Rᵢ))ᵢ≤n (v v′ : TtoU(R)):
f_rel x₁ . . . xₙ v → f_rel x₁ . . . xₙ v′ → v = v′.
Lemma f_f_rel (xᵢ : TtoR(Rᵢ; ∅))ᵢ≤n (v : TtoU(R)): ⌊|f (xᵢ)ᵢ≤n|⌋ = v ⇔ f_rel (⌊|xᵢ|⌋)ᵢ≤n v.
Instance f_pack : TtoR(Rf; ∅) :=
{ def := f; rel := f_rel; f_f_rel := f_f_rel; funct := f_rel_funct }.
Instance f_upack : TtoU(Rf) :=
{ rel_u := f_rel; funct_u := f_rel_funct }.
where Rf = (xᵢ: Rᵢ)ᵢ≤n → R, fᵖ is a unique identifier, and I = {i ∈ [1..n] | Rᵢ is not an arrow}.
To build the inductive graph, the function paths(e ; x 1 , … , x n e; x_1, \ldots, x_n e ; x 1 , … , x n ) collects all the function paths of f f f , excluding the unreachable ones. For each path p p p , we map the unique constructor f p f^p f p to the path relation, given by pathInd(f , p f,p f , p ), defined in 3.5.
The original Liquid Haskell program can also have top-level declarations of constants (0-ary functions). Their refined definition is translated in the same way as functions, with tactics. But we do not generate a graph relation or a pack, and simply use the dependent pair projection if a constant is used in an unrefined context.
Unrefined Translations of Types and Refinements
We present the translation of types (TtoU) and of refinements to the unrefined world. Refinements are either type refinements, in which case they are translated as Prop terms (definition 3.3), or refinement expressions, and translated within the unrefined graph relation of the function (definition 3.5).
Definition 3.1 (Unrefined translation of types). A refinement type is translated as unrefined by scrapping refinements, and using the unrefined inductive datatypes. An arrow type becomes an unrefined pack signature. We translate the fixed length Int type to Z. Overflow and underflow on Haskell’s Int operations are undefined behavior, so arbitrary precision arithmetic on Z respects these semantics.
TtoU ( { x : B ∣ r } ) ≔ Prim ( B ) TtoU ( { x : T C ∣ r } ) ≔ T C u TtoU ( ( x i : R i ) 1 ≤ i ≤ n → R ) ≔ uPack ( TtoU ( R 1 ) → ⋯ → TtoU ( R n ) → TtoU ( R ) ) \begin{aligned}
\operatorname{TtoU}(\{x:B \mid r\}) &\coloneqq\operatorname{Prim}(B) & \operatorname{TtoU}(\{x:T_C \mid r\}) &\coloneqq T_C^u \\
\operatorname{TtoU}((x_i:R_i)_{1\le i\le n}\to R) &\coloneqq\operatorname{uPack}(\operatorname{TtoU}(R_1)\to\cdots\to\operatorname{TtoU}(R_n)\to\operatorname{TtoU}(R))
\end{aligned} TtoU ({ x : B ∣ r }) TtoU (( x i : R i ) 1 ≤ i ≤ n → R ) : = Prim ( B ) : = uPack ( TtoU ( R 1 ) → ⋯ → TtoU ( R n ) → TtoU ( R )) TtoU ({ x : T C ∣ r }) : = T C u
where Prim ( B o o l ) ≔ b o o l \operatorname{Prim}(\mathbf{Bool})\coloneqq\mathbf{bool} Prim ( Bool ) : = bool Prim ( I n t ) ≔ Z \operatorname{Prim}(\mathbf{Int})\coloneqq\mathbb{Z} Prim ( Int ) : = Z Prim ( ( ) ) ≔ U n i t \operatorname{Prim}(())\coloneqq\mathbf{Unit} Prim (( )) : = Unit
Translation of refinements
3.4.1 Translation of refinements. Our example for the translation of type refinements is the refinement of the parameter i i i of the function get, of elaborated type (omitting annotations on variables):
{ i : I n t ∣ 0 ≤ i ∧ i < len { proj x s } } \{i:\mathbf{Int}\mid0\le i\land i<\operatorname{len}\ \{\operatorname{proj}\ xs\}\} { i : Int ∣ 0 ≤ i ∧ i < len { proj x s }} and its translation:
{i:Z | ≤rel 0 i ∧ true ∧ ∃ llen_xs, llen_rel [xs] llen_xs ∧ <rel i llen_xs true}
The translation of refinements is complicated by our encoding of functions as relations. Function calls must generally be translated to a call to the corresponding relation, substituted by a free variable standing for the result (llen_xs) and extracted as an hypothesis (∃ llen_xs , llen_rel ⌊ ∣ x s ∣ ⌋ llen_xs \exists\ \texttt{llen\_xs},\ \texttt{llen\_rel}\ \lfloor|xs|\rfloor\ \texttt{llen\_xs} ∃ llen_xs , llen_rel ⌊ ∣ x s ∣ ⌋ llen_xs ).
Example 3.2. The translation of the refinement 0 ≤ i ∧ i < llen ( proj x s ) 0 \le i \land i < \operatorname{llen}(\operatorname{proj}\ xs) 0 ≤ i ∧ i < llen ( proj x s ) to Prop is as follows. First, external logical connectives are translated to corresponding ones in Prop: RtoP ( 0 ≤ i ∧ i < llen ( proj x s ) ) = RtoP ( 0 ≤ i ) ∧ RtoP ( i < llen ( proj x s ) ) \operatorname{RtoP}(0 \le i \land i < \operatorname{llen}(\operatorname{proj}\ xs)) = \operatorname{RtoP}(0 \le i) \land\operatorname{RtoP}(i < \operatorname{llen}(\operatorname{proj}\ xs)) RtoP ( 0 ≤ i ∧ i < llen ( proj x s )) = RtoP ( 0 ≤ i ) ∧ RtoP ( i < llen ( proj x s )) . We translate builtin operators as relations: RtoP ( 0 ≤ i ) = ≤ r e l RtoU ( 0 ) RtoU ( i ) t r u e = ≤ r e l 0 i t r u e \operatorname{RtoP}(0 \le i) = \le_{\mathrm{rel}}\ \operatorname{RtoU}(0)\ \operatorname{RtoU}(i)\ \mathbf{true} = \le_{\mathrm{rel}}\ 0\ i\ \mathbf{true} RtoP ( 0 ≤ i ) = ≤ rel RtoU ( 0 ) RtoU ( i ) true = ≤ rel 0 i true , where the function RtoU ( r ) \operatorname{RtoU}(r) RtoU ( r ) translates a refinement r r r to a Rocq term refining an unrefined base type. There is no need to introduce a fresh variable here.
A function or operator call that is not directly under logical connectives must be extracted as a hypothesis (as the relation is Prop-kinded) and substituted by a fresh variable name. We create a hypothesis h = ∃ llen_xs , llen ⌊ ∣ x s ∣ ⌋ llen_xs h = \exists\ \texttt{llen\_xs},\ \texttt{llen}\ \lfloor|xs|\rfloor\ \texttt{llen\_xs} h = ∃ llen_xs , llen ⌊ ∣ x s ∣ ⌋ llen_xs . Then:
RtoP ( i < llen ( proj x s ) ) = h → < r e l RtoU ( i ) RtoU ( llen ⌊ ∣ x s ∣ ⌋ ) t r u e \operatorname{RtoP}(i < \operatorname{llen}(\operatorname{proj}\ xs)) = h \to<_{\mathrm{rel}}\ \operatorname{RtoU}(i)\ \operatorname{RtoU}(\texttt{llen}\ \lfloor|xs|\rfloor)\ \mathbf{true} RtoP ( i < llen ( proj x s )) = h → < rel RtoU ( i ) RtoU ( llen ⌊ ∣ x s ∣ ⌋) true = ∃ llen_xs , llen ⌊ ∣ x s ∣ ⌋ llen_xs → < r e l i llen_xs t r u e = \exists\ \texttt{llen\_xs},\ \texttt{llen}\ \lfloor|xs|\rfloor\ \texttt{llen\_xs} \to<_{\mathrm{rel}}\ i\ \texttt{llen\_xs}\ \mathbf{true} = ∃ llen_xs , llen ⌊ ∣ x s ∣ ⌋ llen_xs → < rel i llen_xs true To handle the names, we create a map RV (for result variables ) associating each function call (outside of a projection) to a fresh name. Here, R V ⊇ { llen ( proj x s ) ↦ llen_xs } \mathrm{RV} \supseteq\{\operatorname{llen}(\operatorname{proj}\ xs) \mapsto\texttt{llen\_xs}\} RV ⊇ { llen ( proj x s ) ↦ llen_xs } . The function RtoU \operatorname{RtoU} RtoU translates a function call to its name in RV.
Definition 3.3. The function RtoP ( r ) \operatorname{RtoP}(r) RtoP ( r ) inputs a refinement and assumes a map RV from fully applied functions and operators in λ r \lambda_r λ r to fresh variable names.
We consider a function HypQ ( r ) \operatorname{HypQ}(r) HypQ ( r ) that given a quantifier Q ∈ { ∀ , ∃ } Q \in\{\forall, \exists\} Q ∈ { ∀ , ∃ } returns the sequence hypotheses needed for r r r , using RV (given in definition B.1). For instance, Hyp ∃ ( i < llen ( proj x s ) ) = ∃ llen_res , llen ⌊ ∣ x s ∣ ⌋ llen_res \operatorname{Hyp}_{\exists}(i < \operatorname{llen}(\operatorname{proj}\ xs)) = \exists\texttt{llen\_res},\ \texttt{llen}\ \lfloor|xs|\rfloor\ \texttt{llen\_res} Hyp ∃ ( i < llen ( proj x s )) = ∃ llen_res , llen ⌊ ∣ x s ∣ ⌋ llen_res . We assume the hypotheses to be returned in the correct scope order. Given refinements r 1 , … , r n r_1,\ldots,r_n r 1 , … , r n , we define Hyp ∃ ( r 1 , … , r n ) = ⋀ ^ i ≤ n ( ⋀ h ∈ Hyp ∃ ( r i ) h ) \operatorname{Hyp}_{\exists}(r_1,\ldots,r_n) = \widehat{\bigwedge}_{i \le n}(\bigwedge_{h \in\operatorname{Hyp}_{\exists}(r_i)} h) Hyp ∃ ( r 1 , … , r n ) = ⋀ i ≤ n ( ⋀ h ∈ Hyp ∃ ( r i ) h ) . Then,
RtoP ( ¬ r ′ ) ≔ ¬ RtoP ( r ′ ) RtoP ( r 1 ⊙ r 2 ) ≔ RtoP ( r 1 ) ⊙ RtoP ( r 2 ) ⊙ ∈ { ∧ , ∨ , ⇒ , ⇔ } RtoP ( f r 1 … r n ) ≔ Hyp ∃ ( r 1 , … , r n ) ∧ f r e l RtoU ( r 1 ) … RtoU ( r n ) t r u e RtoP ( r ) ≔ Hyp ∃ ( r ) ∧ is_true ( RtoU ( r ) ) otherwise \begin{aligned}
\operatorname{RtoP}(\neg r') &\coloneqq\neg\operatorname{RtoP}(r') \\
\operatorname{RtoP}(r_1 \mathbin{\odot} r_2) &\coloneqq\operatorname{RtoP}(r_1) \mathbin{\odot} \operatorname{RtoP}(r_2) \qquad\odot\in\{\land,\lor,\Rightarrow,\Leftrightarrow\} \\
\operatorname{RtoP}(f\ r_1\ldots r_n) &\coloneqq\operatorname{Hyp}_{\exists}(r_1,\ldots,r_n) \land f_{\mathrm{rel}}\ \operatorname{RtoU}(r_1)\ldots\operatorname{RtoU}(r_n)\ \mathbf{true} \\
\operatorname{RtoP}(r) &\coloneqq\operatorname{Hyp}_{\exists}(r) \land\operatorname{is\_true}(\operatorname{RtoU}(r)) \qquad\text{otherwise}
\end{aligned} RtoP ( ¬ r ′ ) RtoP ( r 1 ⊙ r 2 ) RtoP ( f r 1 … r n ) RtoP ( r ) : = ¬ RtoP ( r ′ ) : = RtoP ( r 1 ) ⊙ RtoP ( r 2 ) ⊙ ∈ { ∧ , ∨ , ⇒ , ⇔ } : = Hyp ∃ ( r 1 , … , r n ) ∧ f rel RtoU ( r 1 ) … RtoU ( r n ) true : = Hyp ∃ ( r ) ∧ is_true ( RtoU ( r )) otherwise We use Rocq’s is_true to cast a Boolean to Prop.
While RtoP translates refinements to Prop at the external level, inner terms are translated as λ \lambda λ -terms of unrefined base and inductive types with function RtoU. Most notably, this function substitutes function and operator calls with their associated result variable in RV.
Definition 3.4 (Translation of refinements as terms ). The function RtoU ( r ) \operatorname{RtoU}(r) RtoU ( r ) inputs a refinement and assumes a map RV.
RtoU ( x / ( n , L ) ) ≔ x RtoU ( x / ( n > 0 , G ) ) ≔ x upack RtoU ( x / ( 0 , G ) ) = ⌊ x / ( 0 , G ) ⌋ RtoU ( ℓ ) ≔ Prim ( ℓ ) RtoU ( r 1 = r 2 ) ≔ RtoU ( r 1 ) = = ? RtoU ( r 2 ) RtoU ( C r ‾ ) ≔ C u RtoU ( r ) ‾ RtoU ( proj ( r ) ) ≔ ∣ RtoU ( r ) ∣ RtoU ( r ) ≔ RtoU ( r ′ ) , if r ∈ { inj ( r ′ , R ) , sub ( r ′ , R s , R t ) } RtoU ( r ) ≔ RV ( r ) , if r ∈ { r 1 ∘ r 2 , f / ( n > 0 , _ ) r 1 … r n , proj ( f / ( n > 0 , _ ) r 1 … r n ) } \begin{aligned}
\operatorname{RtoU}(x_{/(n,L)}) &\coloneqq x &
\operatorname{RtoU}(x_{/(n>0,G)}) &\coloneqq x\ \operatorname{upack} &
\operatorname{RtoU}(x_{/(0,G)}) &= \lfloor x_{/(0,G)}\rfloor\\
\operatorname{RtoU}(\ell) &\coloneqq\operatorname{Prim}(\ell) &
\operatorname{RtoU}(r_1 = r_2) &\coloneqq\operatorname{RtoU}(r_1) ==? \operatorname{RtoU}(r_2) \\
\operatorname{RtoU}(C\overline{r}) &\coloneqq C_u\ \overline{\operatorname{RtoU}(r)} &
\operatorname{RtoU}(\operatorname{proj}(r)) &\coloneqq\left|\operatorname{RtoU}(r)\right| \\
\operatorname{RtoU}(r) &\coloneqq\operatorname{RtoU}(r'), & \text{if } r \in\{\operatorname{inj}(r',R),\operatorname{sub}(r',R_s,R_t)\} \\
\operatorname{RtoU}(r) &\coloneqq\operatorname{RV}(r), & \text{if } r \in\{r_1 \mathbin{\circ} r_2, f_{/(n>0,\_)}r_1\ldots r_n,\operatorname{proj}(f_{/(n>0,\_)}r_1\ldots r_n)\}
\end{aligned} RtoU ( x / ( n , L ) ) RtoU ( ℓ ) RtoU ( C r ) RtoU ( r ) RtoU ( r ) : = x : = Prim ( ℓ ) : = C u RtoU ( r ) : = RtoU ( r ′ ) , : = RV ( r ) , RtoU ( x / ( n > 0 , G ) ) RtoU ( r 1 = r 2 ) RtoU ( proj ( r )) if r ∈ { inj ( r ′ , R ) , sub ( r ′ , R s , R t )} if r ∈ { r 1 ∘ r 2 , f / ( n > 0 , _ ) r 1 … r n , proj ( f / ( n > 0 , _ ) r 1 … r n )} : = x upack : = RtoU ( r 1 ) == ? RtoU ( r 2 ) : = ∣ RtoU ( r ) ∣ RtoU ( x / ( 0 , G ) ) = ⌊ x / ( 0 , G ) ⌋ where Prim ( T r u e ) ≔ t r u e \operatorname{Prim}(\mathbf{True}) \coloneqq\mathbf{true} Prim ( True ) : = true , Prim ( F a l s e ) ≔ f a l s e \operatorname{Prim}(\mathbf{False}) \coloneqq\mathbf{false} Prim ( False ) : = false , Prim ( ( ( ) ) ) ≔ t t \operatorname{Prim}((())) \coloneqq\mathbf{tt} Prim ((( ))) : = tt , and Prim ( ℓ ) ≔ ℓ \operatorname{Prim}(\ell) \coloneqq\ell Prim ( ℓ ) : = ℓ for integers.
Note the difference of treatment between different kinds of variables: local variables are kept as is, while top-level functions, used as values, are mapped to their upack instance, and top-level constants are projected. We use our generic Boolean equality ==? directly, while other operators
extracted, as any function call. Within a projection proj ( r ) \operatorname{proj}(r) proj ( r ) (that only occurs in type refinements), the term r r r is translated as refined, then projected. Injections and subsumptions are ignored.
Construction of the graph relation
We build the inductive graph relation f _ r e l f\_\mathrm{rel} f _ rel for a function f f f from the sequence of function paths obtained from its definition. For instance, let e g e t e_\mathrm{get} e get be the body of get . The graph of get , computed by paths ( e g e t ; x s , i ) \operatorname{paths}(e_\mathrm{get}; \mathrm{xs}, i) paths ( e get ; xs , i ) , has two paths:
( [ x s ↦ C o n s _ g t 5 x x s ′ , i ↦ i ] , [ i = = 0 ↦ T r u e ] , x ) ([\mathrm{xs} \mapsto\mathrm{Cons\_gt5}\ x\ \mathrm{xs}',\ i \mapsto i],\ [i == 0 \mapsto\mathrm{True}],\ x) ([ xs ↦ Cons_gt5 x xs ′ , i ↦ i ] , [ i == 0 ↦ True ] , x )
and
( [ x s ↦ C o n s _ g t 5 x x s ′ , i ↦ i ] , [ i = = 0 ↦ F a l s e ] , g e t x s ′ ( i − 1 ) ) ([\mathrm{xs} \mapsto\mathrm{Cons\_gt5}\ x\ \mathrm{xs}',\ i \mapsto i],\ [i == 0 \mapsto\mathrm{False}],\ \mathrm{get}\ \mathrm{xs}'\ (i-1)) ([ xs ↦ Cons_gt5 x xs ′ , i ↦ i ] , [ i == 0 ↦ False ] , get xs ′ ( i − 1 ))
Formally, a function path is a triple ( σ x s , σ , r ) (\sigma_{\mathrm{xs}}, \sigma, r) ( σ xs , σ , r ) of a map from parameters to their pattern, additional matches and a term. By construction, the last term is always a refinement. The function paths is defined in definition B.2. It essentially reorganizes and collapses all pattern matching, substitutes away let-bindings and drops unreachable branches and proof hints.
Recall the definition of g e t _ r e l \mathrm{get\_rel} get_rel :
Inductive get_rel : IList_u → Z → Z → Prop :=
| get_Cons_true x xs' i : i == 0 → get_rel (Cons_gt5_u x xs') i x
| get_Cons_false x xs' i : i /= 0 → ∀ (im1:Z), subZ_rel i 1 im1
→ ∀ (v:Z), get_rel xs' im1 n → get_rel (Cons_gt5_u x xs') i v.
The translation uses the functions RtoU and H y p ∀ \mathrm{Hyp}_{\forall} Hyp ∀ , this time using a universal quantifier and implications to introduce the hypotheses as premises. For each path, we instantiate g e t _ r e l \mathrm{get\_rel} get_rel with the parameters patterns (here C o n s _ g t 5 _ u x x s ′ \mathrm{Cons\_gt5\_u}\ x\ \mathrm{xs}' Cons_gt5_u x xs ′ and i i i in both branches). We also introduce the free variables of these terms (x x x , x s ′ \mathrm{xs}' xs ′ and i i i ). Additional pattern matchings are encoded as hypotheses (i = = 0 i == 0 i == 0 and i / = 0 i /= 0 i / = 0 ). Other hypotheses arise from the extraction of function calls, in the second branch for subtraction and the recursive call to get .
Definition 3.5. We translate a path of a function f f f to the type of a constructor of f _ r e l f\_\mathrm{rel} f _ rel (as in Figure 3 ) with function pathInd below, where ( h 1 , … , h n ) = H y p ∀ ( r ) (h_1,\ldots,h_n) = \mathrm{Hyp}_{\forall}(r) ( h 1 , … , h n ) = Hyp ∀ ( r ) , resp. H y p ∀ ( r m ) \mathrm{Hyp}_{\forall}(r_m) Hyp ∀ ( r m ) .
pathInd ( f , ( σ x s , σ , r ) ) ≔ ∀ ( y ) y ∈ fv ( codom ( σ x s ) ) , go ( σ ) go ( ∅ ) ≔ h 1 → ⋯ → h n → f _ r e l ( RtoU ( r i ) ) r i ∈ codom ( σ x s ) RtoU ( r ) go ( ( r m , r p ) : σ ) ≔ h 1 → ⋯ → h n → ∀ ( y ) y ∈ fv ( r p ) , RtoU ( r m ) = = RtoU ( r p ) → go ( σ ) \begin{aligned}
\operatorname{pathInd}(f,(\sigma_{\mathrm{xs}},\sigma,r)) &\coloneqq\forall(y)_{y\in\operatorname{fv}(\operatorname{codom}(\sigma_{\mathrm{xs}}))},\operatorname{go}(\sigma) \\
\operatorname{go}(\varnothing) &\coloneqq h_1 \to\cdots\to h_n \to f\_\mathrm{rel}\left(\operatorname{RtoU}(r_i)\right)_{r_i\in\operatorname{codom}(\sigma_{\mathrm{xs}})}\operatorname{RtoU}(r) \\
\operatorname{go}((r_m,r_p):\sigma) &\coloneqq h_1 \to\cdots\to h_n \to\forall(y)_{y\in\operatorname{fv}(r_p)},\operatorname{RtoU}(r_m) == \operatorname{RtoU}(r_p) \to\operatorname{go}(\sigma)
\end{aligned} pathInd ( f , ( σ xs , σ , r )) go ( ∅ ) go (( r m , r p ) : σ ) : = ∀ ( y ) y ∈ fv ( codom ( σ xs )) , go ( σ ) : = h 1 → ⋯ → h n → f _ rel ( RtoU ( r i ) ) r i ∈ codom ( σ xs ) RtoU ( r ) : = h 1 → ⋯ → h n → ∀ ( y ) y ∈ fv ( r p ) , RtoU ( r m ) == RtoU ( r p ) → go ( σ )
The function is defined inductively on the elements of σ \sigma σ . In the inductive case, we introduce an equality between the matched term and its pattern. We use H y p ∀ ( r m ) \mathrm{Hyp}_{\forall}(r_m) Hyp ∀ ( r m ) but not H y p ∀ ( r p ) \mathrm{Hyp}_{\forall}(r_p) Hyp ∀ ( r p ) as the pattern r p r_p r p does not contain any function calls to extract. We shorten ( i = = ? 0 ) = = t r u e (i ==? 0) == \mathrm{true} ( i == ? 0 ) == true to i = = 0 i == 0 i == 0 and ( i = = ? 0 ) = = f a l s e (i ==? 0) == \mathrm{false} ( i == ? 0 ) == false to i / = 0 i /= 0 i / = 0 .
Refined Translations of Types, Refinements, and Expressions
We now introduce the translation of types (TtoR), refinements (RtoR) and expressions (EtoTac) into the refined world.
Definition 3.6 (Refined translation of types). Type refinements are translated to Prop with RtoP. Arrows become refined pack signatures. We reuse the function Prim(B) from definition 3.1.
TtoR ( { x : B ∣ r } ) ≔ { x : Prim ( B ) ∣ RtoP ( r ) } \operatorname{TtoR}(\{x:B\mid r\}) \coloneqq\{x:\operatorname{Prim}(B)\mid\operatorname{RtoP}(r)\} TtoR ({ x : B ∣ r }) : = { x : Prim ( B ) ∣ RtoP ( r )}
TtoR ( { x : T C ∣ r } ) ≔ { x : T C u ∣ wf T C x ∧ RtoP ( r ) } \operatorname{TtoR}(\{x:T_C\mid r\}) \coloneqq\{x:T_C{}_u\mid\operatorname{wf}_{T_C}x \land\operatorname{RtoP}(r)\} TtoR ({ x : T C ∣ r }) : = { x : T C u ∣ wf T C x ∧ RtoP ( r )}
TtoR ( ( x i : R i ) i ≤ n → R ) ≔ Pack ( ∀ ( x i : TtoR ( R i ) ) i ≤ n , TtoR ( R ) ) \operatorname{TtoR}((x_i:R_i)_{i\leq n}\to R) \coloneqq\operatorname{Pack}(\forall(x_i:\operatorname{TtoR}(R_i))_{i\leq n},\operatorname{TtoR}(R)) TtoR (( x i : R i ) i ≤ n → R ) : = Pack ( ∀ ( x i : TtoR ( R i ) ) i ≤ n , TtoR ( R ))
Translation of expressions and refinements
The translation of Liquid Haskell terms to tactics is cumbersome; instead of giving the full translation (available in § B.3), we illustrate it on a variation of get . The variation introduces a let-bound function double that is applied to the return value.
def doubleThm :: x:{Int | 5 < x} → {u:() | 10 < x + x} =
inj ((), {u:() | 10 < x + x})
def getDouble :: xs:IList → i:{Int | 0 ≤ i ∧ i < llen (proj xs)} → {v:Int | 10 < v} =
let double :: (y:Int) → {v:Int | proj y + proj y == v} = ↑y + ↑y
in case xs of
| Cons_gt5 x xs' →
if ↑i == ↑0 then sub (double ↑x, {v:Int | x + x == v}, {v:Int | 10 < x})
? (doubleThm (inj(x,{x:Int | 5 < x)) proves 10 < x + x)
else getDouble ↑xs' (↑i − ↑1)
| Nil → unreachable
Listing 3. Elaborated functions doubleThm and getDouble
also show how Liquid Haskell uses theorems as a hint to the SMT solver. The theorem doubleThm is here for the sake of illustration, in practice it is too trivial to be necessary.
The elaborated λ r \lambda_r λ r definition is in listing 3. We write ↑ r \uparrow r ↑ r for inj ( r , { v : B ∣ T r u e } ) \operatorname{inj}(r, \{v:B \mid\mathrm{True}\}) inj ( r , { v : B ∣ True }) where B B B is the base type of r r r . We use the same notation for a trivial injection translation in Rocq. The type of double is taken from Liquid Haskell’s type inference (during parsing to λ r \lambda_r λ r ). Liquid Haskell’s addition and other operators are refined:
+ : : x : { I n t ∣ T r u e } → y : { I n t ∣ T r u e } → { v : I n t ∣ proj x + proj y = v } . + :: x:\{\mathrm{Int} \mid\mathrm{True}\} \to y:\{\mathrm{Int} \mid\mathrm{True}\} \to\{v:\mathrm{Int} \mid\operatorname{proj} x + \operatorname{proj} y = v\}. + :: x : { Int ∣ True } → y : { Int ∣ True } → { v : Int ∣ proj x + proj y = v } .
Thus, y y y must be injected into double , but we can derive the final refinement of getDouble , using doubleThm . In Rocq, we use a custom refined addition + Z +_Z + Z .
The translation is given in listing 4. For doubleThm , it is similar to thm_get (§ 2.5 ), but with the injection on the unit and the subsumption merged into a single injection.
Translation of expressions. The preliminary destructs decompose xs and i into unrefined values and refinement proofs. This decomposition is always done on dependent pairs, and appears in Figure 3 .
Then, we introduce double_def, the refined version of double. We use a tactic assert to add double_def in the proof context, its definition is created with the tactics between brackets. From double_def, we create the whole pack by instantiating a relation from the refined definition of double. The tactic refine is like exact but accepts holes to be filled later. Later uses of double use the pack, like double.(def) for the Haskell call double x. In general, a local function might be passed around as a first-class value, for instance given as argument to a higher-order function, which makes the creation of a pack necessary.
If the value bound by a let has a subset type, the translation is different. We still use assert, but instead of creating a pack, we decompose the dependent pair once created.
The translation of the pattern matching, Nil branch and conditional are as described in § 2.3. The first branch shows how a proof hint gets translated: we assert the translated proposition. The evidence is extracted directly as the second element of the dependent pair returned by the application of getDouble. Formally,
EtoTac ( r ? ( r h p r o v e s r p ) ) : = assert ( RtoP ( r p ) ) b y e x a c t ( proj2_sig ( RtoR ( r h ) ) ) . EtoTac ( r ) \operatorname{EtoTac}(r\ ?\ (r_h\ \mathrm{proves}\ r_p)) := \operatorname{assert}\ (\operatorname{RtoP}(r_p))\ \mathrm{by\ exact}\ (\operatorname{proj2\_sig}(\operatorname{RtoR}(r_h))).\ \operatorname{EtoTac}(r) EtoTac ( r ? ( r h proves r p )) := assert ( RtoP ( r p )) by exact ( proj2_sig ( RtoR ( r h ))) . EtoTac ( r )
Translation of refinements. The tactic exact is the interface between the translation of structural expressions as tactics and as refinements: the λ \lambda λ -terms produced by the translation RtoU are inserted in the proof script with exact. Formally, for any r r r , EtoTac ( r ) : = exact ( RtoR ( r ) ) \operatorname{EtoTac}(r) := \operatorname{exact}(\operatorname{RtoR}(r)) EtoTac ( r ) := exact ( RtoR ( r )) .
Definition doubleThm (x:Z | <rel 5 x true)
: {u:unit | ∃ p, +rel x x p ∧ <rel 10 p true}.
destruct x as [x x_p]. exact (exist (∀(u:unit), ∃ p, ...) tt ltac:(solver)). Qed.
Definition getDouble (xs:IList)
(i:Z | ≤rel 0 i true ∧ ∃ llen_xs, llen_rel ⌊|xs|⌋ llen_xs ∧ <rel i llen_xs true)
: {v:Z | <rel 10 v true}.
destruct xs as [xs xs_p]. destruct i as [i i_p].
(* Translation of let double y = y + y *)
assert (double_def : ∀(y:Z | True), {v:Z | +rel ⌊|y|⌋ ⌊|y|⌋ v}).
{ intros [y y_p]. exact (↑y +Z ↑y). } (* translation of λy. ↑y + ↑y *)
refine (let double: Pack (∀(y:Z | True), {v:Z | +rel ⌊|y|⌋ ⌊|y|⌋ v})
:= ltac:(fun_to_pack double_def) in _).
(* Translation of the case expression *)
generalize dependent i; induction xs as [ | x xs' IH_xs' ]; intros.
- exfalso; solver.
- destruct (i ==? 0) eqn:?.
+ assert (<rel 10 x true) by exact (proj2_sig (getDouble (exist _ x ltac:(solver)))).
exact (subCast _ _ (double.(def) ↑x) ltac:(solver)).
+ exact (IH_xs' ltac:(solver) (i-1) ltac:(solver)).
Defined.
Listing 4. Translation of getDouble
The translation of refinements is mostly straightforward and follows the structure of the terms. The main difficulty is the translation of recursive calls.
The recursion get xs (i-1) is translated as a call to the induction hypothesis IH_xs' introduced by the induction tactic. This hypothesis is already instantiated for xs', so we apply it to i-1 (obtained by simplification of ⌊ ∣ ↑ i − Z ↑ 1 ∣ ⌋ \lfloor|{\uparrow i -Z \uparrow1}|\rfloor ⌊ ∣ ↑ i − Z ↑ 1 ∣ ⌋ ). It is necessary to generalize i before calling induction; otherwise, IH_xs' gets instantiated to i as well.
The induction hypothesis is created in a context in which the dependent pairs of the arguments are split, so IH_xs' is applied to refinement proofs along with the unrefined values: the first proof obligation is for xs' and the second for i-1; both are discharged automatically. What makes the translation of recursive calls complex in general is the necessity to (1) link a recursive call with the correct induction hypothesis and (2) find the correct subset of arguments that still need to be instantiated in that induction hypothesis.
Non-recursive calls f r 1 … r n f\ r_1 \ldots r_n f r 1 … r n are translated recursively on r 1 r_1 r 1 to r n r_n r n . We use the name f . ( d e f ) f.(\mathrm{def}) f . ( def ) if f f f is a pack (that is, f f f is local, like double) and f f f for a top-level function (like doubleThm).
Subsumption casts. Subtyping annotations are translated to a custom subCast operation. Its arguments are the original refinement, the desired refinement, the term to cast and the proof of the implication, given by solver. For subset types, we define it as follows:
subCast (∀(xs:B), ps) (∀(xt:B), pt) e
:= exist (∀(xt:B), ps) (proj1_sig e) ltac:(pose proof (proj2_sig e); solver)
The cast projects e e e , then injects it back to the expected refined type. The solver proves the verification condition: assuming the refinement of e e e (proj2_sig e), it creates a proof of p t [ x t \ p r o j 1 _ s i g e ] p_t[x_t\backslash\mathrm{proj1\_sig}\ e] p t [ x t \ proj1_sig e ] .
Subsumption on packs separately casts the four elements of the pack: two unrefined (the graph relation and its functionhood lemma) and two refined (the definition and the lemma relating the definition and the graph relation). The first two are unaffected by the cast, being unrefined.
lemma relating the definition and the graph relation is proven again using the original lemma. The refined definition is morally translated by casting the arguments contravariantly and the function body covariantly (per [30 ] ). The actual implementation, described in § D, is more complex because of the representation of function arguments as dependent lists inside packs.
Properties of the Translation
Our translation is sound, in the sense that it maps a proof of false in Liquid Haskell to a proof of false in Rocq. That is, Lava lets Liquid Haskell use Rocq as a proof checker, via translation. This check increases our confidence in Liquid Haskell, but our tool is not complete in the sense that a well typed Liquid Haskell program necessarily yields a well-typed Rocq program with provable proof obligations that solver can resolve. Such a proof would have to relate an SMT solver to our translation’s encoding; a very ambitious goal! Instead, we evaluate empirically (§ 6 ).
Preservation of evaluation semantics of expressions. To show that proofs of false are preserved, we first show that our translation preserves the input/output behavior of declared functions — both in the graph relations of the unrefined world and in the tactic-generated executable terms of the refined world. We introduce a conventional call-by-value reduction relation → λ r \to_{\lambda_r} → λ r (definition C.1) for expressions as a small-step operational semantics, parametrized by a sequence of declarations D ˉ \bar{D} D ˉ .
Theorem 4.1. Let D ˉ \bar{D} D ˉ be a well-formed and elaborated sequence of declarations, with all functions reflected. Let reflect def f : : ( x i : R i ) i ≤ n → R : = e f ∈ D ˉ f :: (x_i:R_i)_{i\leq n} \to R := e_f \in\bar{D} f :: ( x i : R i ) i ≤ n → R := e f ∈ D ˉ . Let Γ \Gamma Γ be the environment resulting from the elaboration into D ˉ \bar{D} D ˉ . Let v 1 , … , v n , v v_1,\ldots,v_n,v v 1 , … , v n , v be values (variables, literals and constructors) such that Γ ⊢ v ′ ⇐ R ⇝ v \Gamma\vdash v' \Leftarrow R \rightsquigarrow v Γ ⊢ v ′ ⇐ R ⇝ v and for all i ≤ n i \leq n i ≤ n , Γ ⊢ v i ′ ⇐ R i ⇝ v i \Gamma\vdash v'_i \Leftarrow R_i \rightsquigarrow v_i Γ ⊢ v i ′ ⇐ R i ⇝ v i for some v ′ v' v ′ and v i ′ v'_i v i ′ . Then, D ˉ ⊢ f v 1 … v n → λ r ∗ v \bar{D} \vdash f\ v_1\ldots v_n \to_{\lambda_r}^{*} v D ˉ ⊢ f v 1 … v n → λ r ∗ v if and only if in Rocq and under DtoR ( D ˉ ) \operatorname{DtoR}(\bar{D}) DtoR ( D ˉ ) : f _ r e l RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) f\_\mathrm{rel}\ \operatorname{RtoU}(v_1)\ldots\operatorname{RtoU}(v_n)\operatorname{RtoU}(v) f _ rel RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) holds.
Moreover, if each RtoR ( v i ) \operatorname{RtoR}(v_i) RtoR ( v i ) is of type TtoR ( R i ) \operatorname{TtoR}(R_i) TtoR ( R i ) and RtoR ( v ) \operatorname{RtoR}(v) RtoR ( v ) is of type TtoR ( R ) \operatorname{TtoR}(R) TtoR ( R ) in Rocq, the two properties above are equivalent to ⌊ ∣ RtoR ( f v 1 … v n ) ∣ ⌋ = RtoU ( v ) \left\lfloor\left| \operatorname{RtoR}(f\ v_1\ldots v_n)\right|\right\rfloor= \operatorname{RtoU}(v) ⌊ ∣ RtoR ( f v 1 … v n ) ∣ ⌋ = RtoU ( v ) .
Proof sketch. By induction. For the additional property, we use the coherence lemma f _ f _ r e l f\_f\_\mathrm{rel} f _ f _ rel (§ 3.4 ) from the translation output. Since we assume that the output typechecks, this lemma is verified. We must assume that the values are of the right translated refined type in Rocq because our type system does not verify refinements. The full proof is in § C.1. □
We restrict this theorem to the case where functions is reflected (any function can be reflected so long as it does not let-bind higher-order variables). For unreflected functions, the graph relation is defined in terms of the refined function, itself defined with tactics. Any meta-theoretical proof depending on the output of these tactics would be challenging.
Preservation of the models of types. We show that our translation preserves the semantics of types. To do so, we interpret both λ r \lambda_r λ r and Rocq types in the same model and show that the translation preserves the type’s model. For Rocq types, we only need to define models for types in the image of the translation, that is, instances of the typeclass Pack, as well as refinement (or subset) types of the form ∀ ( x : S ) , Q \forall(x : S), \mathbb{Q} ∀ ( x : S ) , Q and { x : U ∣ Q } \{x:\mathbb{U}\mid\mathbb{Q}\} { x : U ∣ Q } , where U \mathbb{U} U is a base type and Q \mathbb{Q} Q a predicate coming from the translation of a refinement as in definition 3.3; we have Q ⇒ w f T C x \mathbb{Q} \Rightarrow\mathrm{wf}_{TC}\ x Q ⇒ wf T C x if U = T C u \mathbb{U} = TC_u U = T C u .
We give the standard interpretation for refinement types [24 § 5] . We interpret them as the set of terms t t t such that t t t is well-typed in the underlying simply-typed system, and in the case of a refined base type with refinement r r r or Q \mathbb{Q} Q , such that r r r or Q \mathbb{Q} Q instantiated by t t t is true.
We write ⟦ ⋅ ⟧ D ˉ \llbracket\cdot\rrbracket_{\bar{D}} [ [ ⋅ ] ] D ˉ for the interpretation of λ r \lambda_r λ r types and ⟦ ⋅ ⟧ D ˉ \llbracket\cdot\rrbracket_{\bar{D}} [ [ ⋅ ] ] D ˉ for the interpretation of Rocq types. The λ r \lambda_r λ r declaration environment D ˉ \bar{D} D ˉ gives datatype and function definition and is used for the reduction relation, as in theorem 4.1 and for typechecking. The assignment ρ \rho ρ maps variables to λ \lambda λ -calculus values. The complete definitions and proofs are in § C.2.
Table 2 (PDF p. 21). Solving and preprocessing tactics. Higher level tactics use lower level ones.
Theorem 4.2. Let R R R be a refinement type in λ r \lambda_r λ r , D ‾ \overline{D} D be a λ r \lambda_r λ r declaration environment such that R R R is well-formed under D ‾ \overline{D} D . For any assignment ρ \rho ρ , ⟦ R ⟧ ρ D ‾ = ⟦ LTtoR ( R ) ⟧ ρ D ‾ \llbracket R\rrbracket_{\rho}^{\overline{D}} = \llbracket\operatorname{LTtoR}(R)\rrbracket_{\rho}^{\overline{D}} [ [ R ] ] ρ D = [ [ LTtoR ( R ) ] ] ρ D .
Theorems 4.1 and 4.2 state that the translation preserves the input-output relation at the level of values and at the level of types (including refinements). In particular, Liquid Haskell proofs are functions of unit type whose output is trivial, but the statements proved will be equivalent in Rocq.
Unprovability of contradiction. Finally, we confirm that our translation can act as a certificate generator for Liquid Haskell. If we manage to prove false in Liquid Haskell, we want to make sure that typechecking will not go through in the Rocq output (assuming Rocq’s consistency).
Theorem 4.3 (Consistency). ∄ e . ∅ ⊨ R o c q DtoR ( def f : : { x : ( ) ∣ F a l s e } : = e ) \nexists e.\ \varnothing\models_{\mathrm{Rocq}} \operatorname{DtoR}(\operatorname{def}\ f :: \{x:() \mid\mathrm{False}\} := e) ∄ e . ∅ ⊨ Rocq DtoR ( def f :: { x : ( ) ∣ False } := e ) .
Proof. Suppose there is a well-formedness derivation for f f f (in the empty context). Then, e e e can be translated into a definition of type { x : ( ) ∣ F a l s e } \{x:() \mid\mathrm{False}\} { x : ( ) ∣ False } . This would give rise to a proof of F a l s e \mathrm{False} False — a contradiction, because Rocq’s type theory is consistent. □ \square □
Implementation & the Proof Automation Tactics
The implementation of the translation (§ 3 ) is done in ∼ \sim ∼ 4K LoC of Haskell. Lava takes as input a program typechecked by Liquid Haskell and outputs a Rocq file that depends on a) a Rocq library (∼ \sim ∼ 350 LoC) containing, for example, definitions for the pack typeclasses and b) proof automation tactics (∼ \sim ∼ 6K LoC). In the rest of the section, we describe the development of the tactics.
The solver tactic discharges the proof obligations introduced by type casts. Table 2 describes the implementation of solver in four levels; each level composes a preprocessor tactic and a solver definition into a solving tactic . Lower level correspond to lower complexity, with tactics of higher level subsuming and making use of lower level ones.
The preprocessor tactics try to simplify proof obligations, only succeeding if provability of the goals is preserved by the transformations. The simpler kind unfolds, applies, and rewrites by lemmas and definitions from the library or the generated files. The more complex kind creates and discharges subgoals, relying on solving tactics from lower levels. The preprocessor tactics listed in table 2 combine many simpler preprocessor tactics, each corresponding to a single reasoning step; cleanup and preprocessor can invoke dozens of smaller steps in each successful call.
The solving tactics fail if they are unable to discharge the goal. They culminate in solver, which uses most of the other definitions and tactics in the library. Unlike the other solving tactics, solver shelves subgoals it cannot prove, which is useful to inspect incomplete proof scripts manually. The class of solving tactics also contains numerous tactics for proving the lemmas generated by the translation; they reuse many of the definitions also used by solver.
Let us give more details on each individual level.
On level 1 , idtac is a noop; fast_done uses lia and auto (with a hint library). On level 2, quick_simpl uses simple rewriting lemmas to clean up the goal. The solving_loop tactic mostly relies on rewriting, unfolding and solving with hints, simplifying with basic Rocq tactics (e.g., simpl, split). It runs rule-based simplification steps (1-5 LoC each) to simplify specific syntactic forms like quantifiers, logical connectives, or equalities.
On level 3, cleanup uses quick_simpl and the lemmas relating refined functions and their graph relations. It works extensively with graph relations, by mapping refined terms to their corresponding graph terms, reasoning about result variables, and saturating the context by reconstructing the refinement witnesses of the final outputs of graph relations.
On level 4, preprocessor extends cleanup with extra simplification steps which can require proving difficult subgoals with the oracle tactic.
Besides solver, the most powerful, but time-consuming, tactics are the ones relating a reflected definition f f f to its graph relation f _ r e l f\_\mathrm{rel} f _ rel , used in particular in the coherence theorems f _ f _ r e l f\_f\_\mathrm{rel} f _ f _ rel (§ 3.4 ). These tactics employ many preprocessing steps similar to those of cleanup. However, the proofs require additional and more aggressive preprocessing steps. These steps apply a forward reasoning that might in general get us stuck in the resolution, but are safe in this context.
Could we replace our automation tactics with existing SMT or other Rocq automation? It would not be so easy: (1) a lot of preprocessing is required to bring the proof state into a shape well supported by Rocq and the tools in its ecosystem; (2) we need to interleave forward and backwards reasoning steps matching on and modifying both goals and hypothesis; and (3) the solving process can be very slow (see the benchmarks in Table 3 ), so customizing automation enables us to optimize performance for our goals. We discuss the use of Sniper and CoqHammer specifically in § 7 .
Table 3. Quantitative Evaluation of Lava. LoC is semantic lines of code. Blowup is the ratio between the LH and the generated Rocq. Verification time is the time to verify the Rocq code. Verification time/LoC is the verification time (in seconds) divided by the number of Rocq LoC.
Evaluation
We evaluate Lava on eight benchmarks, summarized in Table 3 . The first five test specific features of the translation. The other three come from external sources: a port of Rocq’s Software Foundations [27 ] in Liquid Haskell and the benchmarks of [39 ] .
Description of the examples. The Overview example (§ 2 ) tests most features of Lava. It contains a non-trivially refined inductive data type and tests the translation of subtyping to casts in Rocq, the translation of Haskell’s integer types to Rocq’s Z and of theorems with proof hints. It also contains functions with preconditions, postconditions, reflection annotations, and higher-order parameters.
PeanoNats evaluates the translation of theorems to Rocq. It defines the inductive data type of Peano naturals and reflected functions for addition, multiplication, subtraction, inequality and equality. It also contains theorems stating that zero is the left and right neutral element of addition, associativity of addition, distributivity of right-multiplication with addition, and that ∀ m , n : ( m + n ) − n = m \forall m,n : (m+n)-n = m ∀ m , n : ( m + n ) − n = m . This example pushes the limits of our first-order proof automation in Rocq.
RBinsToBins tests the translation of a refined datatype. It defines the refined data type of binary numbers without leading zeros and a function translating from it to an unrefined version of the data type. Crucially, the refinement of one of this datatype’s constructors depends on another constructor. Supporting this example motivates our translation of refined datatypes (from § 2.2 ).
HigherOrder tests the translation of λ \lambda λ -abstractions and properties of function composition.
List tests the list operations append, map, concatMap, reverse, zip, flip, zipWith, unzip and take, and various theorems relating them.
SoftwareFnd is a Liquid Haskell port of the first section of Software Foundations [27 ] and the first five definitions/theorems of the second and third sections each. The original Rocq file is 356 LoC long and was ported to 437 LoC of Liquid Haskell.
ClassLess tests the monad and monoid laws on List, Maybe and Identity datatypes, monomorphized for integers. FoldrUniversal encodes the universality of the foldr function, as described in [23 ] . These last two examples are taken from the benchmarks of [39 ] . Their complete benchmarks suite also contain class laws for the functor and applicative classes that use features, such as partial applications and higher-order datatypes, that are unsupported by the translation.
Summary. The translated file is 14.6 times longer than the original. Indeed, for each original Liquid Haskell declaration, we generate all the elements described in § 3.3 as well as other lemmas designed to help automation and specialized for the declarations. Moreover, type translations can be large when using packs or when numerous function calls appear in refinements.
Translation time (from λ r \lambda_r λ r to Rocq) is fast (less than 2 seconds in total), LH verification and parsing into λ r \lambda_r λ r is similarly fast. But verification of the output lasts up to 29 minutes on the Overview example. Indeed, verification times per line of Rocq vary widely, ranging from 1.17 for Overview to 0.04 for SoftwareFnd. We find that most of the verification is immediate, but a few hard proof obligations take up to a few minutes.
The fact that our automation frequently solves hard goals indicate its power, but for it to take several minutes shows there is room for optimization. Much of the verification time in hard proofs comes from backtracking. In those cases, manually proving the crucial steps (after automatic preprocessing) can substantially reduce verification time.
Related Work
Translations from Haskell to Rocq. hs-to-coq [12 , 34 ] translates Haskell programs to Rocq and is used to verify a substantial part of Haskell’s containers library. This tool differs from ours in two main aspects. First, source programs lack refinement types—and so have neither dependent pattern matching nor proofs terms, simplifying the translation. The desired specifications are either inserted manually or derived from QuickCheck annotations and proved by hand. Second, hs-to-coq supports syntactic partiality of programs by inserting an opaque default value. While we allow Haskell programs to have missing cases, we require that Liquid Haskell programs are in fact total.
Our choice to use inductive predicates has its downsides: it makes support for partial applications difficult and forces the extraction of applications to hypotheses in the unrefined translation. Another way to deal with partiality in the input program is to use a monadic translation, like Dylus et al. [18 ] and Christiansen et al. [15 ] did for translating Haskell (inspired from [1 ] ), or Claret [16 ] , Chapter § 3 and Garrigue and Saikawa [20 ] do for OCaml. Monads could be used to deal with partial Liquid Haskell programs, that are partial also in the refined world. This is not a priority because the default semantics of Liquid Haskell rejects non-provably total programs.
Translation of programs with refinements in interactive theorem provers. Stainless [22 ] is a refinement type checker for Scala. Stainless has a prototype translation to Rocq [19 ] that is not (yet) covered in a scientific paper. But while Stainless internally uses refinement types, this translation does not support them nor dependent arrows. In Stainless, refinements are written as pre- and postconditions and assertions on functions, and this is the specification that the translation supports. The translated programs are effectively unrefined programs with assertions. The primary focus of our work instead is to describe the subtle interaction between unrefined and refined elements, in particular in the context of the embedding of programs into the logic. Thus, their embedding is simpler as everything is unrefined. The translation of Stainless’ programs is intended as a backend for verification, while Lava is too slow for this goal.
Characteristic formulas are another approach of translating from general-purpose proof assistants to logic: Charguéraud [13 ] translates OCaml programs to logical formulas in Rocq, instead of Rocq programs. It would be interesting to study similar approaches in future work, in particular also whether one can translate from GOSPEL-annotated OCaml programs [14 ] using our translation.
Translations of refined inductive datatypes. Atkey et al. [4 ] define a systematic translation between refined and inductive datatypes via categorical logic. This differs from our approach of using unrefined inductive datatypes and inductively-defined predicates instead. Sekiyama et al. [31 ] compare the representations of refined inductive datatypes with unrefined constructors and unrefined inductive types with refined constructors in Rocq. We use the same correspondence to translate inductive datatypes with refined constructors from Liquid Haskell to inductive datatypes refined by a predicate in Rocq.
Mechanization of Liquid Haskell. We aim to improve Liquid Haskell’s foundations by producing certificates for programs, using Rocq for a semantics-in-translation. With a similar goal, Borkowski et al. [10 ] define and formalize a type system that models Liquid Haskell and prove it sound in Rocq. However, logical implication of refinements in subtyping is modeled by a reduction semantics, so the problem of the embedding of terms into the logic and the relation with the SMT solver is avoided, which is the focus of our paper. Still, Borkowski et al. prove soundness of the type system (built on this semantic subtyping) and support polymorphism. Ideally their approach and ours would converge: the type system at the root of Lava could be mechanized and proved sound in Rocq, while still elaborating terms for a translation in Rocq.
Explicit refinement types. Ghalavani and Krishnaswami [21 ] introduce a calculus (λ e r t \lambda_{\mathrm{ert}} λ ert ) that makes refinement reasoning explicit by internalizing refinements directly in the core calculus: refinement proofs are first-class proof terms, and typing does not rely on subtyping. Lava shares the goal of making refinement reasoning explicit, but differs fundamentally in its approach. Rather than designing a refinement calculus inside type theory, Lava starts from Liquid Haskell’s SMT-based system and translates it to Rocq, reifying implicit subtyping and proof obligations as explicit casts and proof obligations in the target. Moreover, λ e r t \lambda_{\mathrm{ert}} λ ert operates in a single semantic layer, whereas Lava must model the separation between refined and unrefined worlds that arises in Liquid Haskell. Capturing this intertwined semantics is central to our contribution and does not arise in λ e r t \lambda_{\mathrm{ert}} λ ert .
Programming with dependent types in Rocq. Equations, by Sozeau and Mangin [33 ] , is a Rocq extension that simplifies writing functions defined by dependent pattern matching. Equations accepts incomplete definitions with holes, and presents them to the user as explicit proof obligations. We could have used Equations to define the refined functions. We decided to use tactics in the first place as we expected them to be more predictable. We plan on re-investigating the adoption of Equations for a future extension towards non-structural recursion. But this is merely a technical choice, as the way refined functions are defined does not influence the analysis of the semantics of Liquid Haskell, and only amounts to a local change. This made switching to Equations a non-priority, further held up by the necessity to adapt automation accordingly. In particular, the coherence lemma between a refined definition and its unrefined graph relation is a complex part of automation that has to be rewritten as part of that change.
Another possibility for writing refined functions in Rocq is the Program extension by Sozeau [32 ] . Program offers implicit predicate subtyping on subset types. This generally makes generated code look a lot like Liquid Haskell code, with no explicit casts, but with less automation and proofs to be resolved by the user. We do not target Program, as one goal is to make the relation between the unrefined and refined semantics explicit, and type casts are part of this.
Integrating SMT solvers and hammers into Rocq. The SMTCoq project [2 ] provides an SMT backend to Rocq (currently CVC4 [5 ] and veriT [11 ] ), by checking and reconstructing the solver’s proof witnesses back into Rocq. Using SMTCoq directly to solve our proof obligations is hard, as a lot of preprocessing must be done on our side. Fortunately, Sniper [8 , 9 ] extends SMTCoq with many useful transformations of the Rocq proof state. Even though those transformations are designed to be compositional, safe and, certified, we experienced that the tool is not mature enough to be used as a replacement for our automation tactic without significant effort.
Overall, it is hard to know when snipe will succeed or not without deep knowledge of the internals of Sniper. Subset types and projections often make the tactic fail or loop on simple obligations. On success, snipe is always slower than our automation tactic on our examples. This might be improved by selecting and arranging the transformations that are most relevant to us. A lot of transformations are also peculiar to our translation, for instance saturating the context with the hypotheses obtained from the graph relations. Adopting a structured approach to our automation in the style or combined with Sniper is an ambitious but interesting project.
Using an SMT solver for the resolution of verification constraints means more hope that a verified property of Liquid Haskell is automatically provable in Rocq. But this also means generating more definitions and proofs, for instance to verify properties of the encoding of datatypes in the solver logic, or translating every inductive proposition to a recursive function in bool. Sniper gives a way to generate the latter Boolean fixpoint automatically, but we often failed to make generation work, or sometimes found the generated fixpoint to be wrong.
Czajka and Kaliszyk [17 ] define a hammer [7 ] for Rocq. The hammer uses a heuristic translation into a fragment of first-order logic, applies HOL ATPs (like E, Vampire or Z3), then tries to reconstruct the proof into Rocq proof terms. Their translation and proof reconstruction is neither sound nor complete, but the approach works well enough in practice. We want to avoid making the user have to complete proofs by itself. Thus, we preferred focusing on predictable proof automation and avoided using CoqHammer or machine learning-based tools like Tactician [6 ] .
Conclusion
Lava translates from Liquid Haskell to Rocq, formally relating SMT-based refinement typing and foundational type theory. Lava turns implicit subtyping explicit through casts; it models both semantics — refined and unrefined — and generates (so far automatically solvable) proof obligations in Rocq. Our translation provides a new foundation for Liquid Haskell that correctly accounts for both refined and unrefined semantics in its SMT encoding. Our implementation shows that this translation is practically possible. Polymorphism and other more advanced features merely require refactoring and engineering; better automation (and automatic solving use SMT/hammers) is a question of connecting the dots. With Lava, we aim to put Liquid Haskell and refinement typing on a more solid footing on its own and in relation to type theoretic approaches — while also helping the community understand the subtleties of refinement types.
Full Type System for λ r \lambda_r λ r
In this section, we give all well-formedness and typing rules.
We have six kinds of judgement: well-formedness of types and of declarations, subtyping, type synthesis for refinements, type checking of expressions (including refinements), and a simple type system for type refinements. All except subtyping have an elaboration.
The grammar of type environements is given below. Notice that we have scope annotations on the variables. We write Γ ( x ) \Gamma(x) Γ ( x ) to look up the variable x x x in Γ \Gamma Γ and dom ( Γ ) \operatorname{dom}(\Gamma) dom ( Γ ) to get the domain of Γ \Gamma Γ .
Typing environment Γ : : = ∅ ∣ Γ , x / c : R ∣ Γ , ⟨ T C , ( C : R ) ⟩ \Gamma::= \varnothing\mid\Gamma, x_{/c}:R \mid\Gamma, \langle TC, (C:R)\rangle Γ ::= ∅ ∣ Γ , x / c : R ∣ Γ , ⟨ T C , ( C : R )⟩ s ∈ { G , L , Y } s \in\{G,L,Y\} s ∈ { G , L , Y }
To avoid redundancy, in the whole appendix, we do not single out if conditionals and treat them as any other pattern matching.
Simple Type System for Type Refinements. We write A A A for a simple type, that is either a base type B B B or an arrow A → A A \to A A → A . We use the function unref ( R ) \operatorname{unref}(R) unref ( R ) to project a refined type into a type A A A . It is defined as unref ( { x : B ∣ r } ) ≜ B \operatorname{unref}(\{x:B \mid r\}) \triangleq B unref ({ x : B ∣ r }) ≜ B and unref ( x : R x → R ) ≜ unref ( R x ) → unref ( R ) \operatorname{unref}(x:R_x \to R) \triangleq\operatorname{unref}(R_x) \to\operatorname{unref}(R) unref ( x : R x → R ) ≜ unref ( R x ) → unref ( R ) . The rules are in Figure 4 . They are standard and similar to the synthesis rules. The only rule with a non-trivial elaboration is the one for variables.
Figure 4. Simple type system for refinements
Well-Formedness of Types. The substitution of a term in a type is defined as { x : B ∣ r x } [ x \ r ] ≜ { x : B ∣ r x } \{x:B \mid r_x\}[x\backslash r] \triangleq\{x:B \mid r_x\} { x : B ∣ r x } [ x \ r ] ≜ { x : B ∣ r x } , { y : B ∣ r y } [ x \ r ] ≜ { y : B ∣ r y [ x \ r ] } \{y:B \mid r_y\}[x\backslash r] \triangleq\{y:B \mid r_y[x\backslash r]\} { y : B ∣ r y } [ x \ r ] ≜ { y : B ∣ r y [ x \ r ]} , ( x : R x → R ) [ x \ r ] ≜ x : R x → R (x:R_x \to R)[x\backslash r] \triangleq x:R_x \to R ( x : R x → R ) [ x \ r ] ≜ x : R x → R and ( y : R y → R ) [ x \ r ] ≜ y : R y [ x \ r ] → R [ x \ r ] (y:R_y \to R)[x\backslash r] \triangleq y:R_y[x\backslash r] \to R[x\backslash r] ( y : R y → R ) [ x \ r ] ≜ y : R y [ x \ r ] → R [ x \ r ] .
if B = T C , then B ∈ dom ( Γ ) Γ , x / L : { B } ⊢ S I M P L E r : B o o l ⇝ r e Γ ⊢ { x : B ∣ r } ⇝ { x : B ∣ r e } W F - T R e f Γ ⊢ R x ⇝ R x e Γ , x / L : R x e ⊢ R ⇝ R e Γ ⊢ x : R x → R ⇝ x : R x e → R e [ x \ proj ( x ) ] W F - T F u n \begin{aligned}
&\text{if } B = TC,\ \text{then } B \in\operatorname{dom}(\Gamma)\\
&\frac{\Gamma, x_{/L}:\{B\} \vdash_{\mathrm{SIMPLE}} r:\mathrm{Bool}\rightsquigarrow r^e}{\Gamma\vdash\{x:B\mid r\}\rightsquigarrow\{x:B\mid r^e\}}\quad\mathrm{WF\text{-}TRef}\\
&\frac{\Gamma\vdash R_x\rightsquigarrow R_x^e \qquad\Gamma,x_{/L}:R_x^e\vdash R\rightsquigarrow R^e}{\Gamma\vdash x:R_x\to R\rightsquigarrow x:R_x^e\to R^e[x\backslash\operatorname{proj}(x)]}\quad\mathrm{WF\text{-}TFun}
\end{aligned} if B = T C , then B ∈ dom ( Γ ) Γ ⊢ { x : B ∣ r } ⇝ { x : B ∣ r e } Γ , x / L : { B } ⊢ SIMPLE r : Bool ⇝ r e WF - TRef Γ ⊢ x : R x → R ⇝ x : R x e → R e [ x \ proj ( x )] Γ ⊢ R x ⇝ R x e Γ , x / L : R x e ⊢ R ⇝ R e WF - TFun
Well-Formedness of Declarations. Well-formedness of declarations takes as input a typing environment with no local variables and a sequence of declarations.
Γ , ⟨ T C , ( C k : R k e ) k < i ⟩ ⊢ R i ⇝ R i e R i e = ( x j : { _ : _ ∣ _ } ) j ≤ m i → R i ′ Γ , ⟨ T C , ( C i : R i e ) i ≤ n ⟩ ⊢ D ‾ ⇝ D e ‾ T C , C i ‾ fresh for Γ Γ ⊢ data T C : = ( C i : : R i ) i ≤ n ; D ‾ ⇝ data T C : = ( C i : : R i e ) i ≤ n ; D e ‾ W F - D T C \frac{
\Gamma,\langle TC,(C_k:R_k^e)_{k<i}\rangle\vdash R_i\rightsquigarrow R_i^e
\qquad
R_i^e=(x_j:\{\,\_:\_\mid\_\})_{j\le m_i}\to R_i'
\qquad
\Gamma,\langle TC,(C_i:R_i^e)_{i\le n}\rangle\vdash\overline{D}\rightsquigarrow\overline{D^e}
\qquad
TC,\overline{C_i}\text{ fresh for }\Gamma
}{
\Gamma\vdash\operatorname{data}\ TC := (C_i::R_i)_{i\le n};\overline{D}
\rightsquigarrow
\operatorname{data}\ TC := (C_i::R_i^e)_{i\le n};\overline{D^e}
}
\quad\mathrm{WF\text{-}DTC} Γ ⊢ data T C := ( C i :: R i ) i ≤ n ; D ⇝ data T C := ( C i :: R i e ) i ≤ n ; D e Γ , ⟨ T C , ( C k : R k e ) k < i ⟩ ⊢ R i ⇝ R i e R i e = ( x j : { _ : _ ∣ _ } ) j ≤ m i → R i ′ Γ , ⟨ T C , ( C i : R i e ) i ≤ n ⟩ ⊢ D ⇝ D e T C , C i fresh for Γ WF - DTC
Rule WF-DTC requires a fresh type constructor T C TC T C and fresh data constructors C i C_i C i with well-formed, first-order types returning a refinement of T C TC T C . First-orderness is ensured because every argument to a constructor refines a base type. The rule recursively checks the well-formedness of the subsequent declarations.
Γ ⊢ R f ⇝ R f e R f e = ( x i : R i ) i ≤ n → R Γ , f / Y : R f e , ( x i / L : noProj ( R i ) ) i ≤ n ⊢ e ⇐ noProj ( R ) ⇝ e e Γ , f / G : R f e ⊢ D ‾ ⇝ D ‾ e Γ ⊢ ( reflect ) ? def f : : R f : = e ; D ‾ ⇝ ( reflect ) ? def f : : R f e : = e e ; D ‾ e \begin{aligned}
\Gamma&\vdash R_f \leadsto R_f^e \qquad R_f^e = (x_i:R_i)_{i\leq n} \to R\\
\Gamma, f/Y:R_f^e, (x_i/L:\operatorname{noProj}(R_i))_{i\leq n} &\vdash e \Leftarrow\operatorname{noProj}(R) \leadsto e^e
\qquad
\Gamma, f/G:R_f^e \vdash\overline{D} \leadsto\overline{D}^e\\
\Gamma&\vdash(\operatorname{reflect})?~\operatorname{def}~f :: R_f := e;\overline{D}
\leadsto
(\operatorname{reflect})?~\operatorname{def}~f :: R_f^e := e^e;\overline{D}^e
\end{aligned} Γ Γ , f / Y : R f e , ( x i / L : noProj ( R i ) ) i ≤ n Γ ⊢ R f ⇝ R f e R f e = ( x i : R i ) i ≤ n → R ⊢ e ⇐ noProj ( R ) ⇝ e e Γ , f / G : R f e ⊢ D ⇝ D e ⊢ ( reflect )? def f :: R f := e ; D ⇝ ( reflect )? def f :: R f e := e e ; D e
WF-DDef
Rule WF-DDef checks the body of f f f using its “maximal” function type — the return type R R R must not be a function type. That is, we forbid point-free definitions and require complete η \eta η -expansion. We use a function noProj ( R ) \operatorname{noProj}(R) noProj ( R ) that removes projections in the refinements of r r r , if the projections are not around variables bound in R R R . For instance, noProj ( x : R x → { v : I n t ∣ ⌊ ∣ x ∣ ⌋ = = ∣ y ∣ ⌋ } ) = x : R x → { v : I n t ∣ ⌊ ∣ x ∣ ⌋ = = y } \operatorname{noProj}(x:R_x \to\{v:\mathit{Int}\mid\lfloor|x|\rfloor== |y|\rfloor\}) = x:R_x \to\{v:\mathit{Int}\mid\lfloor|x|\rfloor== y\} noProj ( x : R x → { v : Int ∣ ⌊ ∣ x ∣ ⌋ == ∣ y ∣ ⌋}) = x : R x → { v : Int ∣ ⌊ ∣ x ∣ ⌋ == y } . This is used to remove the projections around the parameters x i x_i x i that are local variables so considered unrefined in the context and in the return type R R R .
Subtyping. Subtyping has two rules: one for base types and one for function types. The left rule only checks that the base types are equal.
Γ ⊢ { x s : B ∣ r s } ⪯ { x t : B ∣ r t } Γ ⊢ { x s : B ∣ r s } ⪯ { x t : B ∣ r t } S-Base-Ax \frac{\Gamma\vdash\{x_s:B\mid r_s\} \preceq\{x_t:B\mid r_t\}}{\Gamma\vdash\{x_s:B\mid r_s\} \preceq\{x_t:B\mid r_t\}}
\quad\text{S-Base-Ax} Γ ⊢ { x s : B ∣ r s } ⪯ { x t : B ∣ r t } Γ ⊢ { x s : B ∣ r s } ⪯ { x t : B ∣ r t } S-Base-Ax
Γ ⊢ R x t ⪯ R x s Γ , x t : R x t ⊢ R s [ x s \ x t ] ⪯ R t Γ ⊢ x s : R x s → R s ⪯ x t : R x t → R t S-Arr \frac{\Gamma\vdash R_{x_t} \preceq R_{x_s}\qquad\Gamma,x_t:R_{x_t}\vdash R_s[x_s\backslash x_t]\preceq R_t}{\Gamma\vdash x_s:R_{x_s}\to R_s\preceq x_t:R_{x_t}\to R_t}
\quad\text{S-Arr} Γ ⊢ x s : R x s → R s ⪯ x t : R x t → R t Γ ⊢ R x t ⪯ R x s Γ , x t : R x t ⊢ R s [ x s \ x t ] ⪯ R t S-Arr
Typing Expressions
We have a bidirectional type system where synthesis is done exclusively on refinements and checking is done on structural expressions, and always ends with a subtyping rule when done on refinements. A synthesis rule Γ ⊢ r ⇒ R ⇝ r e \Gamma\vdash r \Rightarrow R \leadsto r^e Γ ⊢ r ⇒ R ⇝ r e inputs Γ \Gamma Γ and r r r , synthesizes the type R R R and outputs r e r^e r e . A checking rule Γ ⊢ e ⇐ R ⇝ e e \Gamma\vdash e \Leftarrow R \leadsto e^e Γ ⊢ e ⇐ R ⇝ e e inputs Γ \Gamma Γ , e e e and R R R and checks that e e e has the refinement type R R R , and outputs e e e^e e e .
The type synthesis rules for refinements are given in Figure 5 . The types of literals and operators are given by a function ty ( ⋅ ) \operatorname{ty}(\cdot) ty ( ⋅ ) that generates a singleton type (or selfified type [26 ] ). Typing equality is similar, but works for any base type.
( x / L : R ) ∈ Γ arity ( R ) = 0 Γ ⊢ x ⇒ R ⇝ inj ( x / ( 0 , L ) , R ) S-VarL \frac{(x/L:R)\in\Gamma\qquad\operatorname{arity}(R)=0}{\Gamma\vdash x\Rightarrow R\rightsquigarrow\operatorname{inj}(x/(0,L),R)}
\quad\text{S-VarL} Γ ⊢ x ⇒ R ⇝ inj ( x / ( 0 , L ) , R ) ( x / L : R ) ∈ Γ arity ( R ) = 0 S-VarL ( x / s : R ) ∈ Γ s ∈ { L , G , Y } if s = L then arity ( R ) > 0 Γ ⊢ x ⇒ R ⇝ x / ( arity ( R ) , s ) S-Var \frac{(x/s:R)\in\Gamma\qquad s\in\{L,G,Y\}\qquad\text{if }s=L\text{ then }\operatorname{arity}(R)>0}{\Gamma\vdash x\Rightarrow R\rightsquigarrow x/(\operatorname{arity}(R),s)}
\quad\text{S-Var} Γ ⊢ x ⇒ R ⇝ x / ( arity ( R ) , s ) ( x / s : R ) ∈ Γ s ∈ { L , G , Y } if s = L then arity ( R ) > 0 S-Var Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ ) ) Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ ) ) S-Lit \frac{\Gamma\vdash\ell\Rightarrow\operatorname{ty}(\ell)\rightsquigarrow\operatorname{inj}(\ell,\operatorname{ty}(\ell))}{\Gamma\vdash\ell\Rightarrow\operatorname{ty}(\ell)\rightsquigarrow\operatorname{inj}(\ell,\operatorname{ty}(\ell))}
\quad\text{S-Lit} Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ )) Γ ⊢ ℓ ⇒ ty ( ℓ ) ⇝ inj ( ℓ , ty ( ℓ )) S-Lit ∃ T C ∈ dom ( Γ ) . ( C : R ) ∈ Γ ( T C ) Γ ⊢ C ⇒ R ⇝ C S-Data \frac{\exists TC\in\operatorname{dom}(\Gamma).\ (C:R)\in\Gamma(TC)}{\Gamma\vdash C\Rightarrow R\rightsquigarrow C}
\quad\text{S-Data} Γ ⊢ C ⇒ R ⇝ C ∃ T C ∈ dom ( Γ ) . ( C : R ) ∈ Γ ( T C ) S-Data ty ( ¬ ) = x : R x → R Γ ⊢ r ⇐ R x ⇝ r e Γ ⊢ ¬ r ⇒ R [ x \ r e ] ⇝ ¬ r e S-Neg \frac{\operatorname{ty}(\neg)=x:R_x\to R\qquad\Gamma\vdash r\Leftarrow R_x\rightsquigarrow r^e}{\Gamma\vdash\neg r\Rightarrow R[x\backslash r^e]\rightsquigarrow\neg r^e}
\quad\text{S-Neg} Γ ⊢ ¬ r ⇒ R [ x \ r e ] ⇝ ¬ r e ty ( ¬ ) = x : R x → R Γ ⊢ r ⇐ R x ⇝ r e S-Neg ty ( ⊙ ) = x 1 : R 1 → x 2 : R 2 → R Γ ⊢ r 1 ⇐ R 1 ⇝ r 1 e Γ ⊢ r 2 ⇐ R 2 ⇝ r 2 e Γ ⊢ r 1 ⊙ r 2 ⇒ R [ x 1 \ r 1 e ] [ x 2 \ r 2 e ] ⇝ r 1 e ⊙ r 2 e S-Bin \frac{\operatorname{ty}(\odot)=x_1:R_1\to x_2:R_2\to R\qquad\Gamma\vdash r_1\Leftarrow R_1\rightsquigarrow r_1^e\qquad\Gamma\vdash r_2\Leftarrow R_2\rightsquigarrow r_2^e}{\Gamma\vdash r_1\odot r_2\Rightarrow R[x_1\backslash r_1^e][x_2\backslash r_2^e]\rightsquigarrow r_1^e\odot r_2^e}
\quad\text{S-Bin} Γ ⊢ r 1 ⊙ r 2 ⇒ R [ x 1 \ r 1 e ] [ x 2 \ r 2 e ] ⇝ r 1 e ⊙ r 2 e ty ( ⊙ ) = x 1 : R 1 → x 2 : R 2 → R Γ ⊢ r 1 ⇐ R 1 ⇝ r 1 e Γ ⊢ r 2 ⇐ R 2 ⇝ r 2 e S-Bin Γ ⊢ r 1 ⇒ { x 1 : B 1 ∣ r 1 ′ } ⇝ r 1 e Γ ⊢ r 2 ⇒ { x 2 : B 2 ∣ r 2 ′ } ⇝ r 2 e B 1 = B 2 Γ ⊢ r 1 = r 2 ⇒ { x : B o o l ∣ x = ( proj ( r 1 e ) = proj ( r 2 e ) ) } ⇝ r 1 e = r 2 e S-Eq \frac{\Gamma\vdash r_1\Rightarrow\{x_1:B_1\mid r_1'\}\rightsquigarrow r_1^e\qquad\Gamma\vdash r_2\Rightarrow\{x_2:B_2\mid r_2'\}\rightsquigarrow r_2^e\qquad B_1=B_2}{\Gamma\vdash r_1=r_2\Rightarrow\{x:\mathrm{Bool}\mid x=(\operatorname{proj}(r_1^e)=\operatorname{proj}(r_2^e))\}\rightsquigarrow r_1^e=r_2^e}
\quad\text{S-Eq} Γ ⊢ r 1 = r 2 ⇒ { x : Bool ∣ x = ( proj ( r 1 e ) = proj ( r 2 e ))} ⇝ r 1 e = r 2 e Γ ⊢ r 1 ⇒ { x 1 : B 1 ∣ r 1 ′ } ⇝ r 1 e Γ ⊢ r 2 ⇒ { x 2 : B 2 ∣ r 2 ′ } ⇝ r 2 e B 1 = B 2 S-Eq Γ ⊢ r 1 ⇒ x : R x → R ⇝ r 1 e Γ ⊢ r 2 ⇐ R x ⇝ r 2 e Γ ⊢ r 1 r 2 ⇒ R [ x \ r 2 e ] ⇝ r 1 e r 2 e S-App \frac{\Gamma\vdash r_1\Rightarrow x:R_x\to R\rightsquigarrow r_1^e\qquad\Gamma\vdash r_2\Leftarrow R_x\rightsquigarrow r_2^e}{\Gamma\vdash r_1\,r_2\Rightarrow R[x\backslash r_2^e]\rightsquigarrow r_1^e\,r_2^e}
\quad\text{S-App} Γ ⊢ r 1 r 2 ⇒ R [ x \ r 2 e ] ⇝ r 1 e r 2 e Γ ⊢ r 1 ⇒ x : R x → R ⇝ r 1 e Γ ⊢ r 2 ⇐ R x ⇝ r 2 e S-App Figure 5. Type synthesis rules for refinements
The type checking rules are given in Figure 6 . Both rules C-Let and C-Case use function noProj \operatorname{noProj} noProj to remove projections around newly-introduced variables in type refinements.
Γ ⊢ R x ⇝ R x e x = ( x i : R i ) i ≤ n → R ′ Γ , ( x i / L : noProj ( R i ) ) i ≤ n ⊢ e 1 ⇐ noProj ( R ′ ) ⇝ e 1 e Γ , x / L : R ⊢ e 2 ⇐ R ⇝ e 2 e Γ ⊢ R ⇝ R e \frac{\Gamma\vdash R_x\rightsquigarrow R_x^e\qquad x=(x_i:R_i)_{i\le n}\to R'}{\Gamma,(x_i/L:\operatorname{noProj}(R_i))_{i\le n}\vdash e_1\Leftarrow\operatorname{noProj}(R')\rightsquigarrow e_1^e\qquad\Gamma,x/L:R\vdash e_2\Leftarrow R\rightsquigarrow e_2^e\qquad\Gamma\vdash R\rightsquigarrow R^e} Γ , ( x i / L : noProj ( R i ) ) i ≤ n ⊢ e 1 ⇐ noProj ( R ′ ) ⇝ e 1 e Γ , x / L : R ⊢ e 2 ⇐ R ⇝ e 2 e Γ ⊢ R ⇝ R e Γ ⊢ R x ⇝ R x e x = ( x i : R i ) i ≤ n → R ′ Γ ⊢ let x : : R x : = e 1 in e 2 ⇐ R ⇝ let x : : R x e : = e 1 e in e 2 e C-Let \Gamma\vdash\operatorname{let}\ x::R_x:=e_1\ \operatorname{in}\ e_2\Leftarrow R\rightsquigarrow\operatorname{let}\ x::R_x^e:=e_1^e\ \operatorname{in}\ e_2^e
\quad\text{C-Let} Γ ⊢ let x :: R x := e 1 in e 2 ⇐ R ⇝ let x :: R x e := e 1 e in e 2 e C-Let Γ ⊢ r ⇒ { v : T C ∣ r v } ⇝ r e ⟨ T C , ( C : ( y i : R y ) → { v : T C ∣ r i } ) i ≤ n ⟩ ∈ Γ ∀ i ≤ n s.t. e i ≠ unreachable . ( Γ i ⊢ e i ⇐ R ′ ⇝ e i e ) Γ ⊢ case r of ( C i y i ‾ ↦ e i ) i ≤ n ⇐ R ⇝ case r e of ( C i y i ‾ ↦ e i e ) i ≤ n C-Case \frac{\Gamma\vdash r\Rightarrow\{v:TC\mid r_v\}\rightsquigarrow r^e\qquad\langle TC,(C:(y_i:R_y)\to\{v:TC\mid r_i\})_{i\le n}\rangle\in\Gamma\qquad\forall i\le n\ \text{s.t. }e_i\ne\operatorname{unreachable}.\ (\Gamma_i\vdash e_i\Leftarrow R'\rightsquigarrow e_i^e)}{\Gamma\vdash\operatorname{case}\ r\ \operatorname{of}\ (C_i\ \overline{y_i}\mapsto e_i)_{i\le n}\Leftarrow R\rightsquigarrow\operatorname{case}\ r^e\ \operatorname{of}\ (C_i\ \overline{y_i}\mapsto e_i^e)_{i\le n}}
\quad\text{C-Case} Γ ⊢ case r of ( C i y i ↦ e i ) i ≤ n ⇐ R ⇝ case r e of ( C i y i ↦ e i e ) i ≤ n Γ ⊢ r ⇒ { v : T C ∣ r v } ⇝ r e ⟨ T C , ( C : ( y i : R y ) → { v : T C ∣ r i } ) i ≤ n ⟩ ∈ Γ ∀ i ≤ n s.t. e i = unreachable . ( Γ i ⊢ e i ⇐ R ′ ⇝ e i e ) C-Case Γ ⊢ r h ⇒ { x : ( ) ∣ r p } ⇝ r h e Γ , x / L : { x : ( ) ∣ r p } ⊢ r ⇒ R ⇝ r e Γ ⊢ r ? r h ⇒ R ⇝ r e ? ( r h e proves r p ) C-Hint \frac{\Gamma\vdash r_h\Rightarrow\{x:()\mid r_p\}\rightsquigarrow r_h^e\qquad\Gamma,x/L:\{x:()\mid r_p\}\vdash r\Rightarrow R\rightsquigarrow r^e}{\Gamma\vdash r\ ?\ r_h\Rightarrow R\rightsquigarrow r^e\ ?\ (r_h^e\ \text{proves }r_p)}
\quad\text{C-Hint} Γ ⊢ r ? r h ⇒ R ⇝ r e ? ( r h e proves r p ) Γ ⊢ r h ⇒ { x : ( ) ∣ r p } ⇝ r h e Γ , x / L : { x : ( ) ∣ r p } ⊢ r ⇒ R ⇝ r e C-Hint Γ ⊢ r ⇒ R s ⇝ r e Γ ⊢ R s ⪯ R t Γ ⊢ r ⇐ R t ⇝ sub ( r e , R s , R t ) C-Syn \frac{\Gamma\vdash r\Rightarrow R_s\rightsquigarrow r^e\qquad\Gamma\vdash R_s\preceq R_t}{\Gamma\vdash r\Leftarrow R_t\rightsquigarrow\operatorname{sub}(r^e,R_s,R_t)}
\quad\text{C-Syn} Γ ⊢ r ⇐ R t ⇝ sub ( r e , R s , R t ) Γ ⊢ r ⇒ R s ⇝ r e Γ ⊢ R s ⪯ R t C-Syn Where in C-Case, if r = x r=x r = x then e i ′ = e i [ x \ C y i ‾ ] e_i'=e_i[x\backslash C\overline{y_i}] e i ′ = e i [ x \ C y i ] , R ′ = R [ x \ C y i ‾ ] R'=R[x\backslash C\overline{y_i}] R ′ = R [ x \ C y i ] and Γ i = Γ ′ \Gamma_i=\Gamma' Γ i = Γ ′ , ( y i / L : noProj ( R y ) ) (y_i/L:\operatorname{noProj}(R_y)) ( y i / L : noProj ( R y )) , Γ ′ ′ [ x \ C y i ‾ ] \Gamma''[x\backslash C\overline{y_i}] Γ ′′ [ x \ C y i ] assuming Γ = Γ ′ , x : { v : T C ∣ r v } , Γ ′ ′ \Gamma=\Gamma',x:\{v:TC\mid r_v\},\Gamma'' Γ = Γ ′ , x : { v : T C ∣ r v } , Γ ′′ .
Otherwise, e i ′ = e i e_i'=e_i e i ′ = e i , R ′ = R R'=R R ′ = R and Γ i = Γ , ( y i / L : noProj ( R y ) ) \Gamma_i=\Gamma,(y_i/L:\operatorname{noProj}(R_y)) Γ i = Γ , ( y i / L : noProj ( R y )) .
Figure 6. Type checking rules for expressions
Rule C-Let has two subtleties. The first is that, if R x R_x R x is an arrow type, we check e 1 e_1 e 1 in the context extended by its parameters x 1 x_1 x 1 through x n x_n x n . The second is that we check well-formedness of the type R R R : this is to ensure that this type does not depend on the defined variable x x x .
In rule C-Case, if we match on a variable, we substitute the variable for its pattern in the typing environment, the branch and the type. We do not bother adding information on the value of the match in other cases (as in occurrence typing [37 ] ): such information would only be useful to resolve VCs, which we do not do here. All branches must be present, but some can be unreachable .
Appendix for the Translations
Typeclass for projection
We use a notation [ ⋅ ] [\cdot] [ ⋅ ] in Rocq that is generic over subset types and packs. This is not just a notational trick: in our Rocq library, we define a generic projection, as part of a typeclass. The operator [ ⋅ ] [\cdot] [ ⋅ ] is an alias for the function proj of the typeclass. This function must respect a “proof irrelevance” property po ensuring equality modulo potential refinements.
Class Projection {R U : Type} :=
{ proj: R → U; po: ∀ (x y : R), proj x = proj y → x = y }.
Global Notation ⌊|x|⌋ := (proj x).
For a subset type, proj is defined simply as Rocq’s proj1_sig. For a Pack, we define the projection from a Pack to an uPack simply as projPack(x) := {|rel_u := x.(rel); funct_u := x p a c k . ( f u n c t ) ∣ x_{\mathrm{pack}}.(\mathrm{funct})| x pack . ( funct ) ∣ . The parameters R and U are implicit, as indicated by the curly brackets. Rocq can always infer the correct instance automatically, since we define the projection operator only on subset types and Packs.
Unrefined Translation
Call hypotheses
We give a thorough definition of the function Hyp used in § 3.5 .
Definition B.1 (Call hypotheses). Given a refinement r r r , a quantifier Q ∈ { ∀ , ∃ } Q \in\{\forall, \exists\} Q ∈ { ∀ , ∃ } and assuming the map RV, Hyp Q ( r ) \operatorname{Hyp}_Q(r) Hyp Q ( r ) is defined as:
Hyp Q ( r ) ≔ { Q z , f r e l RtoU ( r 1 ) … RtoU ( r n ) z ∣ f / ( n , G ) r 1 … r n subterm of r ∧ n ≥ 0 } ∪ { Q z , ( getUPackRel f ) RtoU ( r 1 ) … RtoU ( r n ) z ∣ f / ( n , L ) r 1 … r n subterm of r ∧ n > 0 } ∪ { Q z , ( getPackRel f ) RtoU ( r 1 ) … RtoU ( r n ) z ∣ proj ( f / ( n , L ) ) r 1 … r n subterm of r ∧ n > 0 } ∪ { Q z , ⊙ r e l RtoU ( r 1 ) RtoU ( r 2 ) z ∣ r 1 ⊙ r 2 subterm of r } ∪ { Q z , ¬ r e l RtoU ( r 0 ) z ∣ ¬ r 0 subterm of r } . \begin{aligned}
\operatorname{Hyp}_Q(r) \coloneqq{}&
\{Qz,\ f_{\mathrm{rel}}\ \operatorname{RtoU}(r_1)\ldots\operatorname{RtoU}(r_n)\ z \mid f/(n,G)\ r_1\ldots r_n\text{ subterm of }r\land n\ge0\}\\
&\cup\{Qz,\ (\operatorname{getUPackRel}\ f)\ \operatorname{RtoU}(r_1)\ldots\operatorname{RtoU}(r_n)\ z \mid f/(n,L)\ r_1\ldots r_n\text{ subterm of }r\land n>0\}\\
&\cup\{Qz,\ (\operatorname{getPackRel}\ f)\ \operatorname{RtoU}(r_1)\ldots\operatorname{RtoU}(r_n)\ z \mid\operatorname{proj}(f/(n,L))\ r_1\ldots r_n\text{ subterm of }r\land n>0\}\\
&\cup\{Qz,\ \odot_{\mathrm{rel}}\ \operatorname{RtoU}(r_1)\ \operatorname{RtoU}(r_2)\ z\mid r_1\odot r_2\text{ subterm of }r\}\\
&\cup\{Qz,\ \neg_{\mathrm{rel}}\ \operatorname{RtoU}(r_0)\ z\mid\neg r_0\text{ subterm of }r\}.
\end{aligned} Hyp Q ( r ) : = { Q z , f rel RtoU ( r 1 ) … RtoU ( r n ) z ∣ f / ( n , G ) r 1 … r n subterm of r ∧ n ≥ 0 } ∪ { Q z , ( getUPackRel f ) RtoU ( r 1 ) … RtoU ( r n ) z ∣ f / ( n , L ) r 1 … r n subterm of r ∧ n > 0 } ∪ { Q z , ( getPackRel f ) RtoU ( r 1 ) … RtoU ( r n ) z ∣ proj ( f / ( n , L )) r 1 … r n subterm of r ∧ n > 0 } ∪ { Q z , ⊙ rel RtoU ( r 1 ) RtoU ( r 2 ) z ∣ r 1 ⊙ r 2 subterm of r } ∪ { Q z , ¬ rel RtoU ( r 0 ) z ∣ ¬ r 0 subterm of r } .
We use getUPackRel f f f and getPackRel f f f instead of f . ( r e l _ u ) f.(\mathrm{rel\_u}) f . ( rel_u ) and f . ( r e l ) f.(\mathrm{rel}) f . ( rel ) to obtain the relation from a pack, going through the conversion from the representation inside the pack to the one we expect (see § D).
Building function paths
We now describe how we compute the paths of a reflected function, necessary to build its unrefined graph relation.
Definition B.2. Consider a reflected function f f f , not containing λ \lambda λ -abstractions or partial function applications, defined as ( r e f l e c t ) ? d e f f : : ( x i : R i ) i ≤ n → R : = e (\mathrm{reflect)}?\ \mathrm{def}\ f :: (x_i:R_i)_{i\le n}\to R := e ( reflect ) ? def f :: ( x i : R i ) i ≤ n → R := e . The paths of f f f are given by an auxiliary function P \mathcal{P} P .
paths ( e ; x ‾ ) = { ( codom ( σ x s ) , σ p , e p ) ∣ ( σ x s , σ p , e p ) ∈ P ( ( x ‾ , x ‾ ) , ∅ , e ) } \operatorname{paths}(e;\overline{x})=\{(\operatorname{codom}(\sigma_{xs}),\sigma_p,e_p)\mid(\sigma_{xs},\sigma_p,e_p)\in\mathcal{P}((\overline{x},\overline{x}),\varnothing,e)\} paths ( e ; x ) = {( codom ( σ x s ) , σ p , e p ) ∣ ( σ x s , σ p , e p ) ∈ P (( x , x ) , ∅ , e )}
The function P \mathcal{P} P takes as input a triple corresponding to the current branch and returns a set of branches where matches on the arguments are put first, and nested pattern matchings are merged into one. The arguments are thus: (1) a map from variables to simple terms, the patterns for the parameters, (2) a map from additional matched refinements to their pattern, and (3) the expression e e e to turn into branches.
Definition B.3. Given a map from variables to simple terms σ x s \sigma_{xs} σ x s , a map from simple terms to simple terms σ p \sigma_p σ p and an expression e e e , P ( σ x s , σ p , e ) \mathcal{P}(\sigma_{xs},\sigma_p,e) P ( σ x s , σ p , e ) is defined as:
P ( σ x s , σ p , r ) ≔ { ( σ x s , σ p , r ) } P ( σ x s , σ p , u n r e a c h a b l e ) ≔ ∅ P ( σ x s , σ p , l e t x : R : = e 1 i n e 2 ) ≔ ⋃ ( σ x s ′ , σ p ′ , r ) ∈ P ( σ x s , σ p , e 1 ) P ( σ x s ′ , σ p ′ , e 2 [ x \ r ] ) P ( σ x s , σ p , c a s e r o f ( C i y i ‾ ↦ e i ) i ≤ n ) ≔ { P ( σ x s , σ p , e j [ y i \ r i ] i ≤ m ) if ∃ j ≤ n . r = C j r 1 … r m or ∃ ( r , C j r 1 … r m ) ∈ σ x s ∪ σ p , ⋃ i ≤ n P ( σ x s [ x \ C i y i ‾ ] , σ p [ x \ C i y i ‾ ] , e i [ x \ C i y i ‾ ] ) if r = x , ⋃ i ≤ n P ( σ x s , ( σ p , ( r , C i y i ‾ ) ) , e i ) otherwise. \begin{aligned}
\mathcal{P}(\sigma_{xs},\sigma_p,r)&\coloneqq\{(\sigma_{xs},\sigma_p,r)\}\\
\mathcal{P}(\sigma_{xs},\sigma_p,\mathrm{unreachable})&\coloneqq\varnothing\\
\mathcal{P}(\sigma_{xs},\sigma_p,\mathrm{let}\ x:R:=e_1\ \mathrm{in}\ e_2)&\coloneqq\bigcup_{(\sigma'_{xs},\sigma'_p,r)\in\mathcal{P}(\sigma_{xs},\sigma_p,e_1)}\mathcal{P}(\sigma'_{xs},\sigma'_p,e_2[x\backslash r])\\
\mathcal{P}(\sigma_{xs},\sigma_p,\mathrm{case}\ r\ \mathrm{of}\ (C_i\ \overline{y_i}\mapsto e_i)_{i\le n})
&\coloneqq
\begin{cases}
\mathcal{P}(\sigma_{xs},\sigma_p,e_j[y_i\backslash r_i]_{i\le m})&
\text{if }\exists j\le n.\ r=C_j\ r_1\ldots r_m\\
&\text{or }\exists(r,C_j\ r_1\ldots r_m)\in\sigma_{xs}\cup\sigma_p,\\
\bigcup_{i\le n}\mathcal{P}(\sigma_{xs}[x\backslash C_i\overline{y_i}],\sigma_p[x\backslash C_i\overline{y_i}],e_i[x\backslash C_i\overline{y_i}])&
\text{if }r=x,\\
\bigcup_{i\le n}\mathcal{P}(\sigma_{xs},(\sigma_p,(r,C_i\overline{y_i})),e_i)&
\text{otherwise.}
\end{cases}
\end{aligned} P ( σ x s , σ p , r ) P ( σ x s , σ p , unreachable ) P ( σ x s , σ p , let x : R := e 1 in e 2 ) P ( σ x s , σ p , case r of ( C i y i ↦ e i ) i ≤ n ) : = {( σ x s , σ p , r )} : = ∅ : = ( σ x s ′ , σ p ′ , r ) ∈ P ( σ x s , σ p , e 1 ) ⋃ P ( σ x s ′ , σ p ′ , e 2 [ x \ r ]) : = ⎩ ⎨ ⎧ P ( σ x s , σ p , e j [ y i \ r i ] i ≤ m ) ⋃ i ≤ n P ( σ x s [ x \ C i y i ] , σ p [ x \ C i y i ] , e i [ x \ C i y i ]) ⋃ i ≤ n P ( σ x s , ( σ p , ( r , C i y i )) , e i ) if ∃ j ≤ n . r = C j r 1 … r m or ∃ ( r , C j r 1 … r m ) ∈ σ x s ∪ σ p , if r = x , otherwise.
Where we write σ x s [ x \ C i y i ‾ ] \sigma_{xs}[x\backslash C_i\overline{y_i}] σ x s [ x \ C i y i ] to substitute in the codomain of σ x s \sigma_{xs} σ x s and σ p [ x \ C i y i ‾ ] \sigma_p[x\backslash C_i\overline{y_i}] σ p [ x \ C i y i ] to substitute either in the domain or codomain of σ p \sigma_p σ p , and we assume that all y i ‾ \overline{y_i} y i are different from x x x .
EtoTac(r) := exact(RtoR(r)) EtoTac(unreachable) := exfalso; ltac:(solver).
EtoTac(case r of (Cᵢ yᵢ ⇒ eᵢ)ᵢ≤ₙ) :=
⎧ (generalize dependent z;)ᶻ∈ᴢ induction x as [des₁ | … | desₙ]; intros.
⎨ { EtoTac(eᵢ) }ᵢ≤ₙ
⎩ destruct ⌊| RtoR(r)|⌋ as [y₁ | … | yₙ] eqn:? . { EtoTac(eᵢ) }ᵢ≤ₙ
EtoTac(if r then e₁ else e₂) := destruct ⌊| RtoR(r)|⌋ eqn:? . { EtoTac(eᵢ) }ᵢ=₁,₂
EtoTac(let x :: R := e₁ in e₂) := (arity(R) = 0)
assert (x′ : TtoR(R)). { EtoTac(e₁) } destruct x′ as [x x̂]. EtoTac(e₂)
EtoTac(let x :: Rₓ (xᵢ:Rᵢ)ᵢ≤ₙ → R := e₁ in e₂) := (arity(Rₓ) > 0)
assert (x′ : ∀(xᵢ: TtoR(Rᵢ))ᵢ≤ₙ, TtoR(R)). { intros (desᵢ)ᵢ≤ₙ. EtoTac(e₁) }
unshelve refine (let x: TtoR(Rₓ) := ltac:(fun_to_pack x′) in _).
EtoTac(e₂) where desᵢ = xᵢ if Rᵢ is an arrow, otherwise desᵢ = [xᵢ x̂ᵢ]
EtoTac(r ? (rₕ proves rₚ)) := assert (RtoP(rₚ)) by (exact proj2_sig(RtoR(rₕ))). EtoTac(r)
There are two base cases: either we find a branch with a refinement at the end, or we find an unreachable branch, that we delete. This way, we ensure that no unreachable appears in the function paths. Let-bindings are substituted away: this is possible since all branches lead to a refinement, that can be substituted anywhere, unlike expressions. This would not be possible with λ \lambda λ -abstractions or partial applications.
There are three equations for pattern matching. A match on an application (last case) creates a new pattern in the map σ p \sigma_p σ p . A matched variable can be either a parameter of the function or a variable introduced by pattern matching. We substitute this variable by its pattern in the image of σ x s \sigma_{xs} σ x s and everywhere in the second case. The first case accounts for a match on an already matched variable or application, in which case there is only one valid pattern.
Refined translation
We give the full definitions of the functions EtoTac and RtoR from § 3.6.1 .
The definition of EtoTac is in Figure 7 . The formal translations missing from the presentation in § 3.6.1 are those for case, if and let. The translation of let-bindings corresponds to what is explained in § 3.6.1 : we translate the let-binding as an assert. Then, in the first-order case, we destruct the introduced dependent pair and in the higher-order case, we introduce a pack for the new function.
Figure 7. Refined translation of expressions EtoTac
Pattern matching on a refinement r r r is translated either with the destruct or the induction tactic. The difference is that the second creates induction hypotheses. For destruct, eqn:? asserts the equality between the term and its pattern in the context of each branch. Being a special case, ifs are also translated destruct.
We use induction when r r r is a variable that is used as an inductive variable in the term. We call inductive variable a variable that originates from the destruction of a parameter p p p and is used as argument of a recursive call in the position of p p p . In the recursive call getDouble xs' (i−1) from listing 4, the recursive variable is xs', obtained from the destruction of the parameter xs as Cons x xs'.
During elaboration of a matching on a variable y y y , we look for all recursive calls where y y y is an inductive variable (and no other inductive variable has been found) and annotate y y y as the inductive
RtoR ( ℓ ) ≔ Prim ( ℓ ) RtoR ( C ) ≔ C RtoR ( x / ( n > 0 , G ) ) ≔ x p a c k RtoR ( x / ( _ , _ ) ) ≔ x RtoR ( ¬ r ) ≔ ¬ RtoR ( r ) RtoR ( r 1 ⊙ r 2 ) ≔ RtoR ( r 1 ) ⊙ RtoR ( r 2 ) RtoR ( r 1 = r 2 ) ≔ RtoR ( r 1 ) = = ? RtoR ( r 2 ) RtoR ( sub ( r , R s , R t ) ) ≔ TtoR ( R t ) ↩ TtoR ( R s ) RtoR ( r ) RtoR ( inj ( r , R ) ) ≔ exist ( ∀ ( x : U ) , Q ) RtoR ( r ) ltac : ( solver ) , where { x : U ∣ Q } = TtoR ( R ) \begin{aligned}
\operatorname{RtoR}(\ell) &\coloneqq\operatorname{Prim}(\ell) & \operatorname{RtoR}(C) &\coloneqq C \\
\operatorname{RtoR}(x_{/(n>0,G)}) &\coloneqq x_{\mathrm{pack}} & \operatorname{RtoR}(x_{/(\_,\_)}) &\coloneqq x \\
\operatorname{RtoR}(\neg r) &\coloneqq\neg\operatorname{RtoR}(r) & \operatorname{RtoR}(r_1 \odot r_2) &\coloneqq\operatorname{RtoR}(r_1) \odot\operatorname{RtoR}(r_2) \\
\operatorname{RtoR}(r_1 = r_2) &\coloneqq\operatorname{RtoR}(r_1) ==? \operatorname{RtoR}(r_2) \\
\operatorname{RtoR}(\operatorname{sub}(r,R_s,R_t)) &\coloneqq\operatorname{TtoR}(R_t) \mathrel{\hookleftarrow}_{\operatorname{TtoR}(R_s)} \operatorname{RtoR}(r) \\
\operatorname{RtoR}(\operatorname{inj}(r,R)) &\coloneqq\operatorname{exist}\;(\forall(x:U),Q)\;\operatorname{RtoR}(r)\;\operatorname{ltac}:(\operatorname{solver}),\text{ where }\{x:U\mid Q\}=\operatorname{TtoR}(R)
\end{aligned} RtoR ( ℓ ) RtoR ( x / ( n > 0 , G ) ) RtoR ( ¬ r ) RtoR ( r 1 = r 2 ) RtoR ( sub ( r , R s , R t )) RtoR ( inj ( r , R )) : = Prim ( ℓ ) : = x pack : = ¬ RtoR ( r ) : = RtoR ( r 1 ) == ? RtoR ( r 2 ) : = TtoR ( R t ) ↩ TtoR ( R s ) RtoR ( r ) : = exist ( ∀ ( x : U ) , Q ) RtoR ( r ) ltac : ( solver ) , where { x : U ∣ Q } = TtoR ( R ) RtoR ( C ) RtoR ( x / ( _ , _ ) ) RtoR ( r 1 ⊙ r 2 ) : = C : = x : = RtoR ( r 1 ) ⊙ RtoR ( r 2 )
RtoR ( r 1 … r n ) ≔ { IH y ltac : ( solver ) ( p i ) i ≤ n if r = f / ( n , Y ( y , state ) ) f ⋅ ( def ) RtoR ( r 1 ) … RtoR ( r n ) if r = f / ( n , L ) f RtoR ( r 1 ) … RtoR ( r n ) if r = f / ( n , G ) RtoR ( r ) RtoR ( r 1 ) … RtoR ( r n ) otherwise \operatorname{RtoR}(r_1\ldots r_n)\coloneqq
\begin{cases}
\operatorname{IH}_y\;\operatorname{ltac}:(\operatorname{solver})\;(p_i)_{i\le n} & \text{if }r=f_{/(n,Y(y,\operatorname{state}))}\\
f_{\cdot(\operatorname{def})}\;\operatorname{RtoR}(r_1)\ldots\operatorname{RtoR}(r_n) & \text{if }r=f_{/(n,L)}\\
f\;\operatorname{RtoR}(r_1)\ldots\operatorname{RtoR}(r_n) & \text{if }r=f_{/(n,G)}\\
\operatorname{RtoR}(r)\;\operatorname{RtoR}(r_1)\ldots\operatorname{RtoR}(r_n) & \text{otherwise}
\end{cases} RtoR ( r 1 … r n ) : = ⎩ ⎨ ⎧ IH y ltac : ( solver ) ( p i ) i ≤ n f ⋅ ( def ) RtoR ( r 1 ) … RtoR ( r n ) f RtoR ( r 1 ) … RtoR ( r n ) RtoR ( r ) RtoR ( r 1 ) … RtoR ( r n ) if r = f / ( n , Y ( y , state )) if r = f / ( n , L ) if r = f / ( n , G ) otherwise
variable of these calls, an information that is used to translate the call (Figure 8 ). If such calls have been found, we use induction and otherwise fall back to destruct.
Figure 8. Refined translation of refinements; in the last equation, n > 0 n > 0 n > 0 is maximal
Before using induction, we must generalize the parameters that have not been matched upon. The set of those remaining parameters is here called Z Z Z , in practice it is determined with an additional parameter to elaboration that records which parameters have been destructed. Without this generalization, the induction hypotheses generated by induction would already be instantiated for all parameters of the functions, making it impossible to create the correct recursive calls.
The tactic induction also has as parameters a sequence of patterns des i \operatorname{des}_i des i that introduces the names of the datatype parameters. For each i ≤ n i \le n i ≤ n , des i \operatorname{des}_i des i is the sequence ( y j i ) j ≤ m i (y_j^i)_{j\le m_i} ( y j i ) j ≤ m i with names IH y j i \operatorname{IH}_{y_j^i} IH y j i inserted after y j i y_j^i y j i if this variable is used as an inductive variable later.
The definition of RtoR is in Figure 8 .
The translation of a literal is a builtin literal of Rocq. They have a base type (RtoR uses the same function Prim ( ℓ ) \operatorname{Prim}(\ell) Prim ( ℓ ) as RtoU), but are always surrounded by an injection to their singleton type, as explained in § 3.2. Data constructors, on the other hand, are the refined ones from Figure 2 . Operators are translated to the custom operators with refined types that we define in our Rocq library (and for which we use here the same symbols). Equality however is translated to the equality operator from our equality typeclass, which is defined for subset types as the equality of the projected terms.
Variables names, when higher-order, must always refer to a pack instance. The translation of a top-level function name x x x is thus its instance x p a c k x_{\mathrm{pack}} x pack , similar to what happens in RtoU. If x x x is local, it is already a pack by construction. An injection is translated as the construction of a dependent pair from the translation of the term and a proof generated by solver.
The translation of an application is subtle. A non-recursive call (second and third case) is translated recursively on the subterms. If the head term is a local variable f f f (second case), then f f f binds a pack, so RtoR projects on the field containing the actual refined function.
A recursive call (first case) is translated as a call to the induction hypothesis IH y \operatorname{IH}_y IH y obtained from the translation of pattern matching, where y y y is the inductive variable assigned during elaboration of pattern matching, appearing in the annotation of f f f . There must always be an inductive variable for our translation to succeed, which is why we are restricted to structural recursion.
The induction hypothesis IH y \operatorname{IH}_y IH y is already specialized to y y y . It is also specialized on the positions of parameters that have been destructed when IH y \operatorname{IH}_y IH y was introduced. To know how to translate the arguments r 1 r_1 r 1 to r n r_n r n , we use the second annotation in f f f , a sequence called state of length n n n . State is a snapshot of the destruction of the parameters when the induction hypothesis was created. For
D ‾ ⊢ r → λ r δ ( r ) if r ∈ { ¬ ℓ , ℓ 1 ⊙ ℓ 2 , v 1 = v 2 } D ‾ ⊢ r ? ( r h proves r p ) → λ r r ′ D ‾ ⊢ inj ( r , R ) → λ r r ′ D ‾ ⊢ sub ( r , R s , R t ) → λ r r ′ D ‾ ⊢ case C k v k of ( C i x i ↦ e i ) i ≤ n → λ r e k [ x k \ v k ] D ‾ ⊢ let x : : R : = v in e → λ r e [ x \ v ] D ‾ ⊢ f v 1 … v n → λ r e [ x i \ v i ] i ≤ n if reflect def f : : ( x i : R i ) i ≤ n → R : = e ∈ D ‾ \begin{aligned}
\overline{D} \vdash r &\to_{\lambda_r} \delta(r) && \text{if } r \in\{\neg\ell, \ell_1 \mathbin{\odot} \ell_2, v_1 = v_2\} \\
\overline{D} \vdash r\ ?\ (r_h\ \text{proves}\ r_p) &\to_{\lambda_r} r' \\
\overline{D} \vdash\operatorname{inj}(r,R) &\to_{\lambda_r} r' \\
\overline{D} \vdash\operatorname{sub}(r,R_s,R_t) &\to_{\lambda_r} r' \\
\overline{D} \vdash\operatorname{case}\ C_k\ v_k\ \operatorname{of}\ (C_i\ x_i \mapsto e_i)_{i\le n} &\to_{\lambda_r} e_k[x_k\backslash v_k] \\
\overline{D} \vdash\operatorname{let}\ x :: R := v\ \operatorname{in}\ e &\to_{\lambda_r} e[x\backslash v] \\
\overline{D} \vdash f\ v_1\ldots v_n &\to_{\lambda_r} e[x_i\backslash v_i]_{i\le n}
&& \text{if reflect def } f :: (x_i:R_i)_{i\le n} \to R := e \in\overline{D}
\end{aligned} D ⊢ r D ⊢ r ? ( r h proves r p ) D ⊢ inj ( r , R ) D ⊢ sub ( r , R s , R t ) D ⊢ case C k v k of ( C i x i ↦ e i ) i ≤ n D ⊢ let x :: R := v in e D ⊢ f v 1 … v n → λ r δ ( r ) → λ r r ′ → λ r r ′ → λ r r ′ → λ r e k [ x k \ v k ] → λ r e [ x \ v ] → λ r e [ x i \ v i ] i ≤ n if r ∈ { ¬ ℓ , ℓ 1 ⊙ ℓ 2 , v 1 = v 2 } if reflect def f :: ( x i : R i ) i ≤ n → R := e ∈ D
Where δ \delta δ is a function returning the correct value for primitive operations.
each parameter x i x_i x i , state tells us if x i x_i x i had been destructed and its arity. For each i ≤ n i \le n i ≤ n , we define
p i = { ∅ if parameter i is destructed; otherwise: RtoR ( r i ) if parameter i is higher-order ⌊ ∣ RtoR ( r i ) ∣ ⌋ ltac:(solver) if parameter i is first-order \mathbf{p}_i =
\begin{cases}
\varnothing& \text{if parameter } i \text{ is destructed; otherwise:} \\
\operatorname{RtoR}(r_i) & \text{if parameter } i \text{ is higher-order} \\
\lfloor|\operatorname{RtoR}(r_i)|\rfloor_{\text{ltac:(solver)}} & \text{if parameter } i \text{ is first-order}
\end{cases} p i = ⎩ ⎨ ⎧ ∅ RtoR ( r i ) ⌊ ∣ RtoR ( r i ) ∣ ⌋ ltac:(solver) if parameter i is destructed; otherwise: if parameter i is higher-order if parameter i is first-order
First-order values and their refinement proofs must be given separately to the induction hypothesis, which explains the last case. The first case is of a position that is already instantiated.
Proofs of the Properties
Preservation of Evaluation Semantics of Expressions
In this subsection, we look at the operational semantics of reflected functions. We define a reduction semantics for expressions with no higher-order let-bindings (that is, bound λ \lambda λ -abstractions). Like in § B, we encode conditionals as case expressions without loss of generality. The goal is to show that the operational semantics of the expression is preserved by the translation to the graph relation.
Definition C.1 (Small-steps reduction). We equip λ r \lambda_r λ r with a small-steps call-by-value reduction. The base reduction rules are given in Figure 9 . They are applied under an environment of declarations D ‾ \overline{D} D from which we retrieve the definition of free variables. Reduction is closed under all contexts. We write → λ r ∗ \to_{\lambda_r}^{*} → λ r ∗ for the recursive and transitive cloture of → λ r \to_{\lambda_r} → λ r .
Figure 9. Reduction relation
Values are defined as v F x ∣ ℓ ∣ C v v \mathrel{F} x \mid\ell\mid C\ v v F x ∣ ℓ ∣ C v , where x x x is not a constant of D ‾ \overline{D} D . Normal forms of the reduction of a term closed in the declaration environment are closed values.
We assume that the reduction terminates, as Liquid Haskell checks termination of programs. Therefore, we can prove confluence simply by proving local confluence.
Lemma C.2 (Confluence). The reduction on λ r \lambda_r λ r is confluent.
Proof. We show local confluence and conclude by termination of the reduction. There are three kinds of critical pairs. The first is when D ‾ ⊢ r ? ( r h proves r p ) → λ r r \overline{D} \vdash r\ ?\ (r_h\ \text{proves}\ r_p) \to_{\lambda_r} r D ⊢ r ? ( r h proves r p ) → λ r r and either r h r_h r h or r p r_p r p is reduced (as r h ′ r_h' r h ′ or r p ′ r_p' r p ′ ). We reduce D ‾ ⊢ r ? ( r h ′ proves r p ′ ) → λ r r \overline{D} \vdash r\ ?\ (r_h'\ \text{proves}\ r_p') \to_{\lambda_r} r D ⊢ r ? ( r h ′ proves r p ′ ) → λ r r . The second kind is trivial and comes from the reduction of two subterms that do not interact, as r 1 ⊙ r 2 r_1 \mathbin{\odot} r_2 r 1 ⊙ r 2 that can reduce both r 1 r_1 r 1 and r 2 r_2 r 2 .
The third kind concerns pattern matching and let, both cases are similar. We can reduce let x : : R : = v x :: R := v x :: R := v in e e e both as e [ x \ v ] e[x\backslash v] e [ x \ v ] and let x : : R : = v x :: R := v x :: R := v in e ′ e' e ′ . To close the diagram with e ′ [ x \ v ] e'[x\backslash v] e ′ [ x \ v ] , we show the following property: given an expression e e e such that D ‾ ⊢ e → λ r e ′ \overline{D} \vdash e \to_{\lambda_r} e' D ⊢ e → λ r e ′ and a substitution of variables to values σ \sigma σ , we have D ‾ ⊢ e σ → λ r e ′ σ \overline{D} \vdash e\sigma\to_{\lambda_r} e'\sigma D ⊢ e σ → λ r e ′ σ . This property is easily proved by induction on D ‾ ⊢ e → λ r e ′ \overline{D} \vdash e \to_{\lambda_r} e' D ⊢ e → λ r e ′ .
The graph relation of a function is created by reorganizing the structure of the term into the function paths. The function P \mathcal{P} P from B.3 only commutes pattern matchings, merges them and eliminate superfluous matchings. Since the reduction is confluent, these operations are harmless for the final result: we could define a function to convert the set of function paths back into an expression, and both the original function and that expression would reach the same normal form. We formalize this below with an additional detail: to be closer to the representation of the graph relation in Rocq, we build a λ r \lambda_r λ r expression from the function paths, but where we also extract function and operator calls into let-bindings.
Definition C.3. Consider a set P = { ( σ x s i , σ p i , r i ) } i ≤ n P = \{(\sigma_{xs}^{i},\sigma_{p}^{i},r_i)\}_{i\le n} P = {( σ x s i , σ p i , r i ) } i ≤ n of paths obtained by P \mathcal{P} P . We transform this set back into an expression with caseify ( P ) = caseify_aux ( { ( σ i , r i ) } i ≤ n , ∅ ) \operatorname{caseify}(P) = \operatorname{caseify\_aux}(\{(\sigma_i,r_i)\}_{i\le n},\emptyset) caseify ( P ) = caseify_aux ({( σ i , r i ) } i ≤ n , ∅ ) , where σ i \sigma_i σ i is the concatenation of σ x s i \sigma_{xs}^{i} σ x s i and σ p i \sigma_{p}^{i} σ p i where we destructed back the pattern: for instance, a tuple ( r , C ( C 1 x 1 ) x 2 C 2 ) (r,C\ (C_1\ x_1)\ x_2\ C_2) ( r , C ( C 1 x 1 ) x 2 C 2 ) is replaced by the sequence ( r , C y x 2 z ) , ( y , C 1 x 1 ) , ( z , C 2 ) (r,C\ y\ x_2\ z),(y,C_1\ x_1),(z,C_2) ( r , C y x 2 z ) , ( y , C 1 x 1 ) , ( z , C 2 ) , for fresh variables y y y and z z z .
To mimic the construction of the graph relation (definition 3.5), we consider the map R V \mathrm{RV} RV from calls to free variables and we assume a function repl ( r ) \operatorname{repl}(r) repl ( r ) that replaces the applications in r r r according to R V \mathrm{RV} RV .
caseify_aux ( { ( ∅ , r ) } ) ≔ let ( z : = repl ( r z ) ) ( z , r z ) ∈ H in repl ( r ) caseify_aux ( { ( ( r , r i ) : σ i j , r i j ) } j ≤ m i , i ≤ n ) ≔ let ( z : = repl ( r z ) ) ( z , r z ) ∈ H ′ in case r ′ of ( r i ↦ caseify_aux ( { ( σ i j , r i j ) } j ≤ m i , H ) ) i ≤ n \begin{aligned}
\operatorname{caseify\_aux}(\{(\emptyset,r)\}) &\coloneqq\text{let }(z := \operatorname{repl}(r_z))_{(z,r_z)\in H}\text{ in }\operatorname{repl}(r)\\
\operatorname{caseify\_aux}(\{((r,r_i):\sigma_i^j,r_i^j)\}_{j\le m_i,i\le n}) &\coloneqq\text{let }(z := \operatorname{repl}(r_z))_{(z,r_z)\in H'}\text{ in}\\
&\quad\text{case }r'\text{ of }(r_i\mapsto\operatorname{caseify\_aux}(\{(\sigma_i^j,r_i^j)\}_{j\le m_i},H))_{i\le n}
\end{aligned} caseify_aux ({( ∅ , r )}) caseify_aux ({(( r , r i ) : σ i j , r i j ) } j ≤ m i , i ≤ n ) : = let ( z := repl ( r z ) ) ( z , r z ) ∈ H in repl ( r ) : = let ( z := repl ( r z ) ) ( z , r z ) ∈ H ′ in case r ′ of ( r i ↦ caseify_aux ({( σ i j , r i j ) } j ≤ m i , H ) ) i ≤ n
Where in the second case r ′ = repl ( r ) r'=\operatorname{repl}(r) r ′ = repl ( r ) if r r r is an application and r ′ = x r'=x r ′ = x if r = x r=x r = x , and in both cases H = { ( z , r z ) ∈ R V ∣ r z is a subterm of r } H=\{(z,r_z)\in\mathrm{RV}\mid r_z\text{ is a subterm of }r\} H = {( z , r z ) ∈ RV ∣ r z is a subterm of r } .
Lemma C.4. Let D ‾ = D ′ ‾ \overline{D}=\overline{D'} D = D ′ ; reflect def f : : ( x i : R i ) i ≤ n → R : = e ∈ D ‾ f :: (x_i:R_i)_{i\le n}\to R := e \in\overline{D} f :: ( x i : R i ) i ≤ n → R := e ∈ D be a well-formed and annotated sequence of reflected declarations. Let v 1 , … , v n v_1,\ldots,v_n v 1 , … , v n be closed under D ‾ \overline{D} D . Then, D ‾ ⊢ e [ x i \ v i ] i ≤ n → λ r ∗ v \overline{D}\vdash e[x_i\backslash v_i]_{i\le n}\to_{\lambda_r}^{*}v D ⊢ e [ x i \ v i ] i ≤ n → λ r ∗ v if and only if D ‾ ⊢ caseify ( paths ( e ; ( x i ) i ≤ n ) ) [ x i \ v i ] i ≤ n → λ r ∗ v \overline{D}\vdash\operatorname{caseify}(\operatorname{paths}(e;(x_i)_{i\le n}))[x_i\backslash v_i]_{i\le n}\to_{\lambda_r}^{*}v D ⊢ caseify ( paths ( e ; ( x i ) i ≤ n )) [ x i \ v i ] i ≤ n → λ r ∗ v .
Proof. By confluence of the reduction, P \mathcal{P} P preserves the result of evaluation. Confluence is what allows us to extract applications inside let-bindings. □
Theorem 4.1. Let D ‾ \overline{D} D be a well-formed and elaborated sequence of declarations, with all functions reflected. Let reflect def f : : ( x i : R i ) i ≤ n → R : = e f ∈ D ‾ f :: (x_i:R_i)_{i\le n}\to R := e_f \in\overline{D} f :: ( x i : R i ) i ≤ n → R := e f ∈ D . Let Γ \Gamma Γ be the environment resulting from the elaboration into D ‾ \overline{D} D . Let v 1 , … , v n , v v_1,\ldots,v_n,v v 1 , … , v n , v be values (variables, literals and constructors) such that Γ ⊢ v ′ ⇐ R ⇝ v \Gamma\vdash v'\Leftarrow R\rightsquigarrow v Γ ⊢ v ′ ⇐ R ⇝ v and for all i ≤ n i\le n i ≤ n , Γ ⊢ v i ′ ⇐ R i ⇝ v i \Gamma\vdash v_i'\Leftarrow R_i\rightsquigarrow v_i Γ ⊢ v i ′ ⇐ R i ⇝ v i for some v ′ v' v ′ and v i ′ v_i' v i ′ . Then, D ‾ ⊢ f v 1 … v n → λ r ∗ v \overline{D}\vdash f\ v_1\ldots v_n\to_{\lambda_r}^{*}v D ⊢ f v 1 … v n → λ r ∗ v if and only if in Rocq and under DtoR ( D ‾ ) \operatorname{DtoR}(\overline{D}) DtoR ( D ) : f _ r e l RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) f\_\mathrm{rel}\ \operatorname{RtoU}(v_1)\ldots\operatorname{RtoU}(v_n)\ \operatorname{RtoU}(v) f _ rel RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) holds.
Moreover, if each RtoR ( v i ) \operatorname{RtoR}(v_i) RtoR ( v i ) is of type TtoR ( R i ) \operatorname{TtoR}(R_i) TtoR ( R i ) and RtoR ( v ) \operatorname{RtoR}(v) RtoR ( v ) is of type TtoR ( R ) \operatorname{TtoR}(R) TtoR ( R ) in Rocq, the two properties above are equivalent to ⌊ ∣ RtoR ( f v 1 … v n ) ∣ = RtoU ( v ) \lfloor\lvert\operatorname{RtoR}(f\ v_1\ldots v_n)\rvert=\operatorname{RtoU}(v) ⌊∣ RtoR ( f v 1 … v n )∣ = RtoU ( v ) .
Proof. The additional property concerning the refined translation of the function is proven directly in Rocq in the lemmas we described in § 3.3.2 relating the refined definition and the graph relation on the refined domain. We make an assumption that the arguments are well-typed in Rocq because the type system from this paper does not check refinements. However, we can ask that these values are of the correct underlying unrefined type, and this underlying type is preserved by the translation. The fact that refined types correspond in Liquid Haskell and Rocq is proved in theorem 4.2.
We now prove the equivalence between our semantics and the semantics of the graph relation. Notice that RtoU ( v ) \operatorname{RtoU}(v) RtoU ( v ) and each RtoU ( v i ) \operatorname{RtoU}(v_i) RtoU ( v i ) are of the correct type TtoU ( R ) \operatorname{TtoU}(R) TtoU ( R ) and TtoU ( R i ) \operatorname{TtoU}(R_i) TtoU ( R i ) in Rocq. Also, the equivalence holds for operators and their graph relation because the latter is defined using the corresponding Rocq’s builtin operators.
Let D ‾ ⊢ f v 1 … v n → λ r ∗ e f [ x i \ v i ] i ≤ n \overline{D}\vdash f\ v_1\ldots v_n\to_{\lambda_r}^{*}e_f[x_i\backslash v_i]_{i\le n} D ⊢ f v 1 … v n → λ r ∗ e f [ x i \ v i ] i ≤ n . According to the previous lemmas, we can reduce the reduction (in the hypothesis for ⇒ \Rightarrow ⇒ or that we construct for ⇐ \Leftarrow ⇐ ) from the instantiation of e f e_f e f above to a reduction on caseify ( paths ( e f ; ( x i ) i ≤ n ) ) \operatorname{caseify}(\operatorname{paths}(e_f; (x_i)_{i \le n})) caseify ( paths ( e f ; ( x i ) i ≤ n )) . For ⇒ \Rightarrow ⇒ , since f r 1 … r n f\ r_1 \ldots r_n f r 1 … r n reduces to a value, none of the intermediate computations is stuck on a pattern matching. Similarly for ⇒ \Rightarrow ⇒ , since there is a result to the application of the relation and by functionality of the relation, there is a unique constructor that can be traced from the arguments and the result.
Therefore, there is a path p = ( σ x s , σ r , r p ) ∈ paths ( e f ; ( x i ) i ≤ n ) p = (\sigma_{x_s}, \sigma_r, r_p) \in\operatorname{paths}(e_f; (x_i)_{i \le n}) p = ( σ x s , σ r , r p ) ∈ paths ( e f ; ( x i ) i ≤ n ) corresponding to the reduction, and a constructor f p f^p f p built from that path.
Both sides of the proof are by induction on the reduction. We start with ⇒ \Rightarrow ⇒ . The structure of the function caseify \operatorname{caseify} caseify models that of the constructor f p f^p f p . From the matching of the arguments, we obtain an instantiation of the free variables of the patterns in σ x s \sigma_{x_s} σ x s , with which we instantiate the first variables of f p f^p f p .
Then, let us consider the matchings on additional applications. The hypotheses introduced in f p f^p f p are in one-to-one correspondence with the let-bindings introduced in caseify \operatorname{caseify} caseify . These hypotheses are of the shape ∃ z , g 0 e 1 … e m e \exists z, g_0\ e_1 \ldots e_m\ e ∃ z , g 0 e 1 … e m e , where g 0 g_0 g 0 is g _ r e l g\mathord{\_}\mathrm{rel} g _ rel or getUPackRel g \operatorname{getUPackRel}\ g getUPackRel g or getPackRel g \operatorname{getPackRel}\ g getPackRel g and where e 1 e_1 e 1 to e n e_n e n and e e e are Rocq expressions obtained by translation. The encoding of a relation inside a pack preserves its operational semantics, so that in all three cases we can apply the induction hypothesis, since this specific application of g g g appears in the translation. This gives us an instantiation from the fresh variable of the hypothesis to the result of the application. The evaluation of the pattern matching itself gives us an instantiation of the free variables of the pattern.
Now, let σ \sigma σ be the substitution obtained by the series of pattern matching on additional applications and the let-bindings corresponding to function calls. Given z z z in dom ( σ ) \operatorname{dom}(\sigma) dom ( σ ) , there are two possibilities: either σ ( z ) = v z \sigma(z) = v_z σ ( z ) = v z for D ‾ ⊢ g v g → λ r ∗ v z \overline{D} \vdash g\ v_g \to_{\lambda_r}^{*} v_z D ⊢ g v g → λ r ∗ v z , where g g g was bound in a let-binding, and z z z comes from RV, or σ ( z ) \sigma(z) σ ( z ) is an arbitrary value, when z z z is a parameter or introduced with a pattern. For the return type of the constructor f p f^p f p , we instantiate the first n n n arguments of f _ r e l f\mathord{\_}\mathrm{rel} f _ rel correctly thanks to the previous instantiations (from σ x s \sigma_{x_s} σ x s and from σ \sigma σ above). For the n + 1 n+1 n + 1 th argument (the result), we use the fact that for every refinement r r r , if there is v v v such that D ‾ ⊢ r → λ r ∗ v \overline{D} \vdash r \to_{\lambda_r}^{*} v D ⊢ r → λ r ∗ v , then RtoU ( r ) σ \operatorname{RtoU}(r)\sigma RtoU ( r ) σ is equal modulo conversion to v v v in Rocq. This proof is direct by induction on D ‾ ⊢ r → λ r ∗ v \overline{D} \vdash r \to_{\lambda_r}^{*} v D ⊢ r → λ r ∗ v .
The proof for ⇐ \Leftarrow ⇐ is similar: there is a correspondence between the graph relation in Rocq and the reduction on the caseify term, from which, using lemma C.4, we get a reduction of the original λ r \lambda_r λ r function. By inversion on f _ r e l RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) f\mathord{\_}\mathrm{rel}\ \operatorname{RtoU}(v_1) \ldots\operatorname{RtoU}(v_n)\ \operatorname{RtoU}(v) f _ rel RtoU ( v 1 ) … RtoU ( v n ) RtoU ( v ) and by functionality of the relation (proved directly in Rocq), we get a unique branch of the relation corresponding to the correct path. This branch gives us instantiations for the parameters of the function and further hypothesis of the shape g _ r e l RtoU ( v 1 ′ ) … RtoU ( v m ′ ) RtoU ( v ′ ) g\mathord{\_}\mathrm{rel}\ \operatorname{RtoU}(v'_1) \ldots\operatorname{RtoU}(v'_m)\ \operatorname{RtoU}(v') g _ rel RtoU ( v 1 ′ ) … RtoU ( v m ′ ) RtoU ( v ′ ) . By induction, these hypothesis correspond to a computation D ‾ ⊢ g v 1 ′ … v m ′ v ′ → λ r ∗ \overline{D} \vdash g\ v'_1 \ldots v'_m v' \to_{\lambda_r}^{*} D ⊢ g v 1 ′ … v m ′ v ′ → λ r ∗ .
Preservation of Models of Types
Simply-typed λ \lambda λ -calculus
We take the simply-typed λ \lambda λ -calculus ( λ → ) (\lambda_{\to}) ( λ → ) as a common language to interpret terms of λ r \lambda_r λ r and Rocq.
We use terms of the simply-typed λ \lambda λ -calculus as a common interpretation for λ r \lambda_r λ r and Rocq.
Definition C.5 (Calculus λ → \lambda_{\to} λ → ). The grammar of λ → \lambda_{\to} λ → is given in Figure 10 . Operators and literals are taken to be the same as in λ r \lambda_r λ r .
Figure 10. Grammar of the simply-typed λ \lambda λ -calculus
We define two translations to the λ \lambda λ -calculus: from λ r \lambda_r λ r and from the image of the translation of type refinements. The image of the translation of refinement types of λ r \lambda_r λ r is formally defined in Figure 11 . In this section, we use the simplified definition of pack without encoding (see § D), as both versions have the same semantics.
Figure 11. Image of the translation of types in Rocq.
Definition C.6 (Translation from λ r \lambda_r λ r types to the λ \lambda λ -calculus). We define a translation ⌊ ⋅ ⌋ H λ \lfloor\cdot\rfloor_{H\lambda} ⌊ ⋅ ⌋ H λ from λ r \lambda_r λ r to λ → \lambda_{\to} λ → over:
Base types as the identity and refinement types as { x : B ∣ r } H λ ≐ B \left\{x:B \mid r\right\}_{H\lambda} \doteq B { x : B ∣ r } H λ ≐ B and [ x : R x → R ] H λ ≐ [ R x ] H λ → [ R ] H λ \left[x:R_x \to R\right]_{H\lambda} \doteq\left[R_x\right]_{H\lambda} \to\left[R\right]_{H\lambda} [ x : R x → R ] H λ ≐ [ R x ] H λ → [ R ] H λ .
Refinements as [ x / ( _ , _ ) ] H λ ≐ x \left[x/(\_,\_)\right]_{H\lambda} \doteq x [ x / ( _ , _ ) ] H λ ≐ x , [ inj ( r , _ ) ] H λ ≐ r \left[\operatorname{inj}(r,\_)\right]_{H\lambda} \doteq r [ inj ( r , _ ) ] H λ ≐ r , [ sub ( r , _ , _ ) ] H λ ≐ r \left[\operatorname{sub}(r,\_,\_)\right]_{H\lambda} \doteq r [ sub ( r , _ , _ ) ] H λ ≐ r , [ proj ( r ) ] H λ ≐ r \left[\operatorname{proj}(r)\right]_{H\lambda} \doteq r [ proj ( r ) ] H λ ≐ r and the identity for the rest.
Expressions as [ l e t x : : R : = e x i n e ] H λ ≐ l e t x : = [ e x ] H λ i n [ e ] H λ \left[\mathbf{let}\ x::R := e_x\ \mathbf{in}\ e\right]_{H\lambda} \doteq\mathbf{let}\ x := \left[e_x\right]_{H\lambda}\ \mathbf{in}\ \left[e\right]_{H\lambda} [ let x :: R := e x in e ] H λ ≐ let x := [ e x ] H λ in [ e ] H λ , [ i f r t h e n e t e l s e e f ] H λ ≐ c a s e [ r ] H λ o f T r u e ↦ [ e t ] H λ ; F a l s e ↦ [ e f ] H λ \left[\mathbf{if}\ r\ \mathbf{then}\ e_t\ \mathbf{else}\ e_f\right]_{H\lambda} \doteq\mathbf{case}\ \left[r\right]_{H\lambda}\ \mathbf{of}\ \mathbf{True}\mapsto\left[e_t\right]_{H\lambda};\mathbf{False}\mapsto\left[e_f\right]_{H\lambda} [ if r then e t else e f ] H λ ≐ case [ r ] H λ of True ↦ [ e t ] H λ ; False ↦ [ e f ] H λ , [ c a s e r o f ( C i x i ↦ e i ) i ≤ n ] H λ ≐ c a s e [ r ] H λ o f ( C i x i ↦ [ e i ] H λ ) i ∈ I \left[\mathbf{case}\ r\ \mathbf{of}\ (C_i\ x_i\mapsto e_i)_{i\le n}\right]_{H\lambda} \doteq\mathbf{case}\ \left[r\right]_{H\lambda}\ \mathbf{of}\ (C_i\ x_i\mapsto\left[e_i\right]_{H\lambda})_{i\in I} [ case r of ( C i x i ↦ e i ) i ≤ n ] H λ ≐ case [ r ] H λ of ( C i x i ↦ [ e i ] H λ ) i ∈ I where I = { i ≤ n ∣ e i ≠ u n r e a c h a b l e } I=\{i\le n\mid e_i\ne\mathbf{unreachable}\} I = { i ≤ n ∣ e i = unreachable } and [ e ? ( r h p r o v e s r p ) ] H λ ≐ [ e ] H λ \left[e\ ?\ (r_h\ \mathbf{proves}\ r_p)\right]_{H\lambda}\doteq\left[e\right]_{H\lambda} [ e ? ( r h proves r p ) ] H λ ≐ [ e ] H λ .
Typing environments with [ x : A ] H λ ≐ x : [ A ] H λ \left[x:A\right]_{H\lambda}\doteq x:\left[A\right]_{H\lambda} [ x : A ] H λ ≐ x : [ A ] H λ and [ ⟨ T C , ( C i : A i ) i ∈ I ⟩ ] H λ ≐ ⟨ T C , ( C i : [ A i ] H λ ) i ∈ I ⟩ \left[\left\langle T_C,(C_i:A_i)_{i\in I}\right\rangle\right]_{H\lambda}\doteq\left\langle T_C,(C_i:\left[A_i\right]_{H\lambda})_{i\in I}\right\rangle [ ⟨ T C , ( C i : A i ) i ∈ I ⟩ ] H λ ≐ ⟨ T C , ( C i : [ A i ] H λ ) i ∈ I ⟩ .
Definition C.7 (Translation from the target Rocq grammar to the λ \lambda λ -calculus). We define a translation [ ⋅ ] Q λ \left[\cdot\right]_{Q\lambda} [ ⋅ ] Q λ from the Rocq target to λ → \lambda_{\to} λ → over:
Base types: [ U n i t ] Q λ ≐ ( ) , [ b o o l ] Q λ ≐ B o o l , [ Z ] Q λ ≐ I n t \left[\mathsf{Unit}\right]_{Q\lambda}\doteq(),\ \left[\mathsf{bool}\right]_{Q\lambda}\doteq\mathsf{Bool},\ \left[\mathsf{Z}\right]_{Q\lambda}\doteq\mathsf{Int} [ Unit ] Q λ ≐ ( ) , [ bool ] Q λ ≐ Bool , [ Z ] Q λ ≐ Int and [ T C u ] Q λ ≐ T C \left[T_C{}_u\right]_{Q\lambda}\doteq T_C [ T C u ] Q λ ≐ T C .
Refinement types: [ { x : U ∣ e } ] Q λ ≐ [ U ] Q λ , [ ∀ ( x : S x ) , S ] Q λ ≐ [ S x ] Q λ → [ S ] Q λ \left[\{x:U\mid e\}\right]_{Q\lambda}\doteq\left[U\right]_{Q\lambda},\ \left[\forall(x:S_x),S\right]_{Q\lambda}\doteq\left[S_x\right]_{Q\lambda}\to\left[S\right]_{Q\lambda} [ { x : U ∣ e } ] Q λ ≐ [ U ] Q λ , [ ∀ ( x : S x ) , S ] Q λ ≐ [ S x ] Q λ → [ S ] Q λ and [ P a c k ( ∀ ( x : S i ) i ≤ n , S ) ] Q λ ≐ [ S 1 ] Q λ → ⋯ → [ S n ] Q λ → [ S ] Q λ \left[\mathsf{Pack}(\forall(x:S_i)_{i\le n},S)\right]_{Q\lambda}\doteq\left[S_1\right]_{Q\lambda}\to\cdots\to\left[S_n\right]_{Q\lambda}\to\left[S\right]_{Q\lambda} [ Pack ( ∀ ( x : S i ) i ≤ n , S ) ] Q λ ≐ [ S 1 ] Q λ → ⋯ → [ S n ] Q λ → [ S ] Q λ .
Literals: [ t t ] Q λ ≐ ( ) , [ t r u e ] Q λ ≐ T r u e , [ f a l s e ] Q λ ≐ F a l s e \left[\mathsf{tt}\right]_{Q\lambda}\doteq(),\ \left[\mathsf{true}\right]_{Q\lambda}\doteq\mathsf{True},\ \left[\mathsf{false}\right]_{Q\lambda}\doteq\mathsf{False} [ tt ] Q λ ≐ ( ) , [ true ] Q λ ≐ True , [ false ] Q λ ≐ False and [ ℓ ] Q λ ≐ ℓ \left[\ell\right]_{Q\lambda}\doteq\ell [ ℓ ] Q λ ≐ ℓ if ℓ \ell ℓ is an integer.
Refinements: [ x ] Q λ ≐ x , [ ⌊ ∣ x ∣ ⌋ ] Q λ ≐ x , [ C u p ] Q λ ≐ C [ p ] Q λ \left[x\right]_{Q\lambda}\doteq x,\ \left[\left\lfloor\lvert x\rvert\right\rfloor\right]_{Q\lambda}\doteq x,\ \left[C_u\ p\right]_{Q\lambda}\doteq C\left[p\right]_{Q\lambda} [ x ] Q λ ≐ x , [ ⌊ ∣ x ∣ ⌋ ] Q λ ≐ x , [ C u p ] Q λ ≐ C [ p ] Q λ and [ p 1 = = ? p 2 ] Q λ ≐ [ p 1 ] Q λ = [ p 2 ] Q λ \left[p_1==?p_2\right]_{Q\lambda}\doteq\left[p_1\right]_{Q\lambda}=\left[p_2\right]_{Q\lambda} [ p 1 == ? p 2 ] Q λ ≐ [ p 1 ] Q λ = [ p 2 ] Q λ .
Propositions: [ i s _ t r u e ( p ) ] Q λ ≐ [ p ] Q λ , [ Q 1 ⊙ Q 2 ] Q λ ≐ [ Q 1 ] Q λ ⊙ [ Q 2 ] Q λ \left[\mathsf{is\_true}(p)\right]_{Q\lambda}\doteq\left[p\right]_{Q\lambda},\ \left[Q_1\odot Q_2\right]_{Q\lambda}\doteq\left[Q_1\right]_{Q\lambda}\odot\left[Q_2\right]_{Q\lambda} [ is_true ( p ) ] Q λ ≐ [ p ] Q λ , [ Q 1 ⊙ Q 2 ] Q λ ≐ [ Q 1 ] Q λ ⊙ [ Q 2 ] Q λ (where ⊙ \odot ⊙ is the builtin binary operator in λ → \lambda_{\to} λ → ), [ Q 1 = = Q 2 ] Q λ ≐ [ Q 1 ] Q λ = [ Q 2 ] Q λ \left[Q_1==Q_2\right]_{Q\lambda}\doteq\left[Q_1\right]_{Q\lambda}=\left[Q_2\right]_{Q\lambda} [ Q 1 == Q 2 ] Q λ ≐ [ Q 1 ] Q λ = [ Q 2 ] Q λ , [ ∃ z , ( f _ r e l / f . ( r e l ) / f . ( r e l _ u ) ) p ‾ z ∧ Q ] Q λ ≐ l e t z : = f [ p ‾ ] Q λ i n [ Q ] Q λ \left[\exists z,(f\_\mathrm{rel}/f.(\mathrm{rel})/f.(\mathrm{rel\_u}))\ \overline{p}z\land Q\right]_{Q\lambda}\doteq\mathbf{let}\ z:=f\left[\overline{p}\right]_{Q\lambda}\ \mathbf{in}\ \left[Q\right]_{Q\lambda} [ ∃ z , ( f _ rel / f . ( rel ) / f . ( rel_u )) p z ∧ Q ] Q λ ≐ let z := f [ p ] Q λ in [ Q ] Q λ , [ w f T C x ] Q λ ≐ c a s e x o f ( C i x 1 … x m i ↦ [ Q i ] Q λ ) i ∈ I \left[\mathsf{wf}_{T_C}\ x\right]_{Q\lambda}\doteq\mathbf{case}\ x\ \mathbf{of}\ (C_i\ x_1\ldots x_{m_i}\mapsto\left[Q_i\right]_{Q\lambda})_{i\in I} [ wf T C x ] Q λ ≐ case x of ( C i x 1 … x m i ↦ [ Q i ] Q λ ) i ∈ I if w f T C x \mathsf{wf}_{T_C}\ x wf T C x is defined as m a t c h x w i t h ( C i x 1 … x m i ↦ Q i ) i ∈ I e n d \mathbf{match}\ x\ \mathbf{with}\ (C_i\ x_1\ldots x_{m_i}\mapsto Q_i)_{i\in I}\ \mathbf{end} match x with ( C i x 1 … x m i ↦ Q i ) i ∈ I end .
D ‾ ⊢ e → λ δ ( e ) if e ∈ { ¬ ℓ , ℓ 1 ⊙ ℓ 2 , v 1 = v 2 } \overline{D} \vdash e \to_{\lambda} \delta(e) \quad\text{if } e \in\{\neg\ell,\ell_1 \mathbin{\odot} \ell_2,v_1=v_2\} D ⊢ e → λ δ ( e ) if e ∈ { ¬ ℓ , ℓ 1 ⊙ ℓ 2 , v 1 = v 2 }
D ‾ ⊢ ( λ x . e 1 ) e 2 → λ e 2 [ x \ e 1 ] \overline{D} \vdash(\lambda x.e_1)e_2 \to_{\lambda} e_2[x\backslash e_1] D ⊢ ( λ x . e 1 ) e 2 → λ e 2 [ x \ e 1 ]
D ‾ ⊢ x → λ λ x 1 … x n . ⌊ e ⌋ H λ if reflect def f : : ( x i : R i ) i ≤ n → R : = e ∈ D ‾ \overline{D} \vdash x \to_{\lambda} \lambda x_1\ldots x_n.\lfloor e\rfloor_{H_\lambda}
\quad\text{if reflect def } f :: (x_i:R_i)_{i\le n}\to R := e\in\overline{D} D ⊢ x → λ λ x 1 … x n . ⌊ e ⌋ H λ if reflect def f :: ( x i : R i ) i ≤ n → R := e ∈ D
D ‾ ⊢ l e t x : = v i n e → λ e [ x \ v ] \overline{D} \vdash\mathop{\mathrm{let}} x:=v\mathop{\mathrm{in}}e \to_{\lambda} e[x\backslash v] D ⊢ let x := v in e → λ e [ x \ v ]
D ‾ ⊢ c a s e C k v k ‾ o f ( C i x i ↦ e i ) i ≤ n → λ e k [ x k \ v k ‾ ] \overline{D} \vdash\mathop{\mathrm{case}}\ C_k\overline{v_k}\ \mathop{\mathrm{of}}\ (C_i x_i\mapsto e_i)_{i\le n}
\to_{\lambda} e_k[\overline{x_k\backslash v_k}] D ⊢ case C k v k of ( C i x i ↦ e i ) i ≤ n → λ e k [ x k \ v k ]
Definition C.8. The reduction semantics of the calculus is called → λ \to_{\lambda} → λ and defined in Figure 12 . We reduce under all contexts except under abstractions. Evaluation is very similar to the one of λ r \lambda_r λ r . It is parametrized by an environment D ‾ \overline{D} D of elaborated λ r \lambda_r λ r declarations. We use a similar function δ \delta δ giving the result of the evaluation of operators.
In the translation from Rocq refinements, an existential hypothesis ∃ z , f r e l p ‾ z ∧ Q \exists z, f_{\mathit{rel}}\ \overline{p}\ z \land Q ∃ z , f rel p z ∧ Q (and variants) is translated back to the function f f f , that is a function defined in the context of the original Liquid Haskell function. From the point of view of reduction, this is correct thanks to theorem 4.1: both the graph relation and the Haskell function have the same input-output relation. We know that z z z exists iff the application of f f f results in a value v v v , a value for which we can instantiate z z z . On the other hand, if a result does not exist, this means that the application of f f f is stuck in the translation. If this is the case, the computation will be stuck, as we only reduce lets when they bind a value. Thus, the refinement will not evaluate to True .
We define two interpretations of types of λ r \lambda_r λ r and of the Rocq target respectively to sets of values of λ → \lambda_{\to} λ → . The interpretations are parametrized by an assignement ρ \rho ρ from variables to values of λ → \lambda_{\to} λ → and by a λ r \lambda_r λ r environment of declarations D ‾ \overline{D} D of elaborated datatypes and functions. The values in the interpretation are closed with respect to D ‾ \overline{D} D . We use the notation ρ [ x ← v ] \rho[x\leftarrow v] ρ [ x ← v ] to indicate the update of the value of x x x to v v v in ρ \rho ρ .
In the following, we use the name Γ D ‾ \overline{\Gamma_D} Γ D for the environment obtained from the elaboration into D ‾ \overline{D} D : Γ D ‾ \overline{\Gamma_D} Γ D contains the elaborated specifications of the functions and datatypes in D ‾ \overline{D} D .
Figure 12. Reduction relation for the simply-typed λ \lambda λ -calculus.
Definition C.9 (Interpretation of λ r \lambda_r λ r types).
⟦ B ⟧ ρ D ‾ : = { ℓ ∣ ∅ ⊢ L C ℓ : B } B is a builtin type \llbracket B\rrbracket^{\overline{D}}_{\rho} := \{\ell\mid\varnothing\vdash_{\mathcal{L C}} \ell:B\}
\qquad B\text{ is a builtin type} [ [ B ] ] ρ D := { ℓ ∣ ∅ ⊢ LC ℓ : B } B is a builtin type ⟦ T C ⟧ ρ D ‾ : = { v = C v 1 … v n ∣ ∃ ( R i ) i ≤ n , x , r . d a t a T C : = … , C : : ( x i : R i ) i ≤ n → { x : T C ∣ r } , … ∈ D ‾ ∧ ( ∀ i ∈ [ 1.. n ] . v i ∈ ⟦ R i ⟧ ρ [ x j ← v j ] j < i D ‾ ) ∧ D ‾ ⊢ r [ x i \ v i ] i ≤ n [ x \ v ] ρ → λ ∗ T r u e } \begin{aligned}
\llbracket TC\rrbracket^{\overline{D}}_{\rho}:={}&\{v=Cv_1\ldots v_n\mid\\
&\exists(R_i)_{i\le n},x,r.\ \mathop{\mathrm{data}}\ TC:=\ldots,C::(x_i:R_i)_{i\le n}\to\{x:TC\mid r\},\ldots\in\overline{D}\\
&\land(\forall i\in[1..n].\ v_i\in\llbracket R_i\rrbracket^{\overline{D}}_{\rho[x_j\leftarrow v_j]_{j<i}})\\
&\land\overline{D}\vdash r[x_i\backslash v_i]_{i\le n}[x\backslash v]_{\rho}\to_\lambda^{*}\mathbf{True}\}
\end{aligned} [ [ T C ] ] ρ D := { v = C v 1 … v n ∣ ∃ ( R i ) i ≤ n , x , r . data T C := … , C :: ( x i : R i ) i ≤ n → { x : T C ∣ r } , … ∈ D ∧ ( ∀ i ∈ [ 1.. n ] . v i ∈ [ [ R i ] ] ρ [ x j ← v j ] j < i D ) ∧ D ⊢ r [ x i \ v i ] i ≤ n [ x \ v ] ρ → λ ∗ True } ⟦ { x : B ∣ r } ⟧ ρ D ‾ : = { v ∣ v ∈ ⟦ B ⟧ ρ D ‾ ∧ D ‾ ⊢ r [ x \ v ] ρ → λ ∗ T r u e } \llbracket\{x:B\mid r\}\rrbracket^{\overline{D}}_{\rho}
:=\{v\mid v\in\llbracket B\rrbracket^{\overline{D}}_{\rho}\land\overline{D}\vdash r[x\backslash v]_{\rho}\to_\lambda^{*}\mathbf{True}\} [ [ { x : B ∣ r } ] ] ρ D := { v ∣ v ∈ [ [ B ] ] ρ D ∧ D ⊢ r [ x \ v ] ρ → λ ∗ True } ⟦ x : R x → R ⟧ ρ D ‾ : = { v ∣ ⌊ Γ D ‾ ⌋ H λ ⊢ L C v : ⌊ R x ⌋ H λ → ⌊ R ⌋ H λ ∧ ∀ v x ∈ ⟦ R x ⟧ ρ D ‾ . D ‾ ⊢ v v x → λ ∗ v ′ s.t. v ′ ∈ ⟦ R ⟧ ρ [ x ← v x ] D ‾ } \llbracket x:R_x\to R\rrbracket^{\overline{D}}_{\rho}
:=\{v\mid\lfloor\overline{\Gamma_D}\rfloor_{H_\lambda}\vdash_{\mathcal{L C}}v:\lfloor R_x\rfloor_{H_\lambda}\to\lfloor R\rfloor_{H_\lambda}
\land\forall v_x\in\llbracket R_x\rrbracket^{\overline{D}}_{\rho}.\ \overline{D}\vdash vv_x\to_\lambda^{*}v'\ \text{ s.t. }v'\in\llbracket R\rrbracket^{\overline{D}}_{\rho[x\leftarrow v_x]}\} [ [ x : R x → R ] ] ρ D := { v ∣ ⌊ Γ D ⌋ H λ ⊢ LC v : ⌊ R x ⌋ H λ → ⌊ R ⌋ H λ ∧ ∀ v x ∈ [ [ R x ] ] ρ D . D ⊢ v v x → λ ∗ v ′ s.t. v ′ ∈ [ [ R ] ] ρ [ x ← v x ] D }
Definition C.10 (Interpretation of Rocq types).
\left\llbracket\{x:U \mid Q\}\right\rrbracket_{\rho}^{\overline{D}} = \{v \mid\left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda} \vdash_L C\ v : \left\llbracket U\right\rrbracket_{Q\lambda} \land\overline{D} \vdash\left\llbracket Q\right\rrbracket_{Q\lambda}[x\backslash v]\to_{\lambda}^{*}\mathrm{True}\}
\left\llbracket\forall(x:S_x),S\right\rrbracket_{\rho}^{\overline{D}} = \{v \mid\left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda} \vdash_L C\ v : \left\llbracket S_x\right\rrbracket_{Q\lambda}\to\left\llbracket S\right\rrbracket_{Q\lambda}
\land\ \forall v_x\in\left\llbracket S_x\right\rrbracket_{\rho}^{\overline{D}}.\ \overline{D}\vdash v\ v_x\to_{\lambda}^{*}v'\ \text{ s.t. }v'\in\left\llbracket S\right\rrbracket_{\rho[x\leftarrow v_x]}^{\overline{D}}\}
\left\llbracket\operatorname{Pack}(\forall(x_i:S_i)_{i\le n},S)\right\rrbracket_{\rho}^{\overline{D}}=\left\llbracket\forall(x_i:S_i)_{i\le n},S\right\rrbracket_{\rho}^{\overline{D}}
Lemma C.11. For all refinement r r r , r=\left\llbracket r\right\rrbracket_{H\lambda}=\left\llbracket\operatorname{RtoP}(r)\right\rrbracket_{Q\lambda} .
Proof. The first equality is by definition. For the second, notice that \left\llbracket\operatorname{RtoP}(r)\right\rrbracket_{Q\lambda} does not contain a call to wf T C \operatorname{wf}_{TC} wf T C . It is easy to prove that \left\llbracket\operatorname{RtoP}(r)\right\rrbracket_{Q\lambda} is equal to r r r where the outmost applications of functions, operators and top-level constants have been replaced by the corresponding fresh variable in R V RV R V . Let ( Q 1 , … , Q n ) = Hyp ( r ) (Q_1,\ldots,Q_n)=\operatorname{Hyp}(r) ( Q 1 , … , Q n ) = Hyp ( r ) and for each i i i ,
Q i ≐ ∃ z i , ( f i _ r e l / f i . ( r e l ) / f i . ( r e l _ u ) ) RtoU ( r i ) ‾ z i Q_i \doteq\exists z_i,\ (f_i\_\mathrm{rel}/f_i.(\mathrm{rel})/f_i.(\mathrm{rel\_u}))\ \overline{\operatorname{RtoU}(r_i)}\ z_i Q i ≐ ∃ z i , ( f i _ rel / f i . ( rel ) / f i . ( rel_u )) RtoU ( r i ) z i Then, we have
\left\llbracket\operatorname{RtoP}(r)\right\rrbracket_{Q\lambda}
=
\left\llbracket Q_1\land\cdots\land Q_n\land\operatorname{is\_true}(\operatorname{RtoU}(r))\right\rrbracket_{Q\lambda}
=
\left\llbracket\operatorname{RtoU}(r)\right\rrbracket_{Q\lambda}
[z_i\backslash f_i\left\llbracket\operatorname{RtoU}(r_i)\right\rrbracket_{Q\lambda}]_{i\le n}
Lemma C.12. Let v\in\left\llbracket B\right\rrbracket_{\rho}^{\overline{D}} . Then, \left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda}\vdash_L C\ v:B .
Proof. By structural induction on B B B . The case B B B is builtin is direct by definition of the models. If B = T C B=TC B = T C , then v = C v 1 … v n v=C\ v_1\ldots v_n v = C v 1 … v n , we have C : : ( x i : { x i : B i ∣ r i } ) i ≤ n → { x : T C ∣ r } C::(x_i:\{x_i:B_i\mid r_i\})_{i\le n}\to\{x:TC\mid r\} C :: ( x i : { x i : B i ∣ r i } ) i ≤ n → { x : T C ∣ r } in T C TC T C . Let ρ i = ρ [ x j ← v j ] j < i \rho_i=\rho[x_j\leftarrow v_j]_{j<i} ρ i = ρ [ x j ← v j ] j < i . By hypothesis, for each i ≤ n i\le n i ≤ n , we have v_i\in\left\llbracket R_i\right\rrbracket_{\rho_i}^{\overline{D}} which implies by induction that \left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda}\vdash_L C\ v_i:B_i . Thus, by applications and since \left\llbracket\overline{\Gamma}_{D}(TC)(C)\right\rrbracket_{H\lambda}=B_1\to\cdots\to B_n\to TC , we derive \left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda}\vdash_L C\ v:TC .
Theorem 4.2. Let R R R be a refinement type in λ r \lambda_r λ r , D ‾ \overline{D} D be a λ r \lambda_r λ r declaration environment such that R R R is well-formed under D ‾ \overline{D} D . For any assignment ρ \rho ρ , \left\llbracket R\right\rrbracket_{\rho}^{\overline{D}}=\left\llbracket\operatorname{TtoR}(R)\right\rrbracket_{\rho}^{\overline{D}} .
Proof. By induction on the structure of the type. First, let R = { x : B ∣ r x } R=\{x:B\mid r_x\} R = { x : B ∣ r x } . If B B B is a builtin, we are done because \left\llbracket Q\right\rrbracket_{Q\lambda}=\left\llbracket\operatorname{RtoP}(r_x)\right\rrbracket_{Q\lambda}=r_x (by lemma C.11) and v\in\left\llbracket B\right\rrbracket_{\rho}^{\overline{D}} is clearly equivalent to \left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda}\vdash_L C\ v:B .
Let B = T C B=TC B = T C and thus TtoU ( R ) = { x : T C u ∣ wf T C x ∧ RtoP ( r x ) } \operatorname{TtoU}(R)=\{x:TC_u\mid\operatorname{wf}_{TC}\ x\land\operatorname{RtoP}(r_x)\} TtoU ( R ) = { x : T C u ∣ wf T C x ∧ RtoP ( r x )} . By lemma C.11, we have \left\llbracket\operatorname{RtoP}(r_x)\right\rrbracket_{Q\lambda}=r_x , so on both sides we have D ‾ ⊢ r x [ x \ v ] → λ ∗ T r u e \overline{D}\vdash r_x[x\backslash v]\to_{\lambda}^{*}\mathrm{True} D ⊢ r x [ x \ v ] → λ ∗ True . Then, we prove:
v\in\left\llbracket R\right\rrbracket_{\rho}^{\overline{D}}\Longrightarrow v\in\left\llbracket\operatorname{TtoU}(R)\right\rrbracket_{\rho}^{\overline{D}} : By definition, v v v is of the shape C v 1 … v n C\ v_1\ldots v_n C v 1 … v n . From lemma C.12, we have \left\llbracket\overline{\Gamma}_{D}\right\rrbracket_{H\lambda}\vdash_L C\ v:TC . We still need to prove
(\forall i\in[1..n].\ v_i\in\left\llbracket R_i\right\rrbracket_{\rho_i}^{\overline{D}})\land\overline{D}\vdash r_C\sigma\rho\to_{\lambda}^{*}\mathrm{True}
\Longrightarrow
\overline{D}\vdash\left\llbracket\operatorname{wf}_{TC}\ x\right\rrbracket_{Q\lambda}[x\backslash v]\rho\to_{\lambda}^{*}\mathrm{True}
Where for each i i i , ρ i = ρ [ x j ← v j ] j < i \rho_i=\rho[x_j\leftarrow v_j]_{j<i} ρ i = ρ [ x j ← v j ] j < i , where σ = [ x i \ v i ] i ≤ n [ x \ v ] \sigma=[x_i\backslash v_i]_{i\le n}[x\backslash v] σ = [ x i \ v i ] i ≤ n [ x \ v ] , and with ( C ) : : ( x i : R i ) i ≤ n → { x C : T C ∣ r C } (C)::(x_i:R_i)_{i\le n}\to\{x_C:TC\mid r_C\} ( C ) :: ( x i : R i ) i ≤ n → { x C : T C ∣ r C } in T C TC T C in D ‾ \overline{D} D .
Let R i = { x i : B i ∣ r i } R_i=\{x_i:B_i\mid r_i\} R i = { x i : B i ∣ r i } for each $i\le n (there are no higher-order arguments of data constructors in our system). By definition,
\overline{D}\vdash\left\llbracket\operatorname{wf}_{TC}\ x\right\rrbracket_{Q\lambda}[x\backslash v]\to_{\lambda}^{*}\left\llbracket\operatorname{RtoP}(r_C)\right\rrbracket_{Q\lambda}\sigma\land\bigwedge_{i\le n}\left\llbracket Q_i[x\backslash x_i]\right\rrbracket_{Q\lambda}\sigma
where Q i Q_i Q i is such that TtoU ( R i ) = { x i ′ : Prim ( B i ) ∣ Q i } \operatorname{TtoU}(R_i) = \{x_i': \operatorname{Prim}(B_i) \mid Q_i\} TtoU ( R i ) = { x i ′ : Prim ( B i ) ∣ Q i } . By lemma C.11, ⌊ RtoP ( r C ) ⌋ Q λ = r C \lfloor\operatorname{RtoP}(r_C)\rfloor_{Q_\lambda} = r_C ⌊ RtoP ( r C ) ⌋ Q λ = r C , and therefore D ‾ ⊢ ⌊ RtoP ( r C ) ⌋ Q λ σ ρ → λ ∗ T r u e \overline{D} \vdash\lfloor\operatorname{RtoP}(r_C)\rfloor_{Q_\lambda\sigma\rho} \to_\lambda^* \mathbf{True} D ⊢ ⌊ RtoP ( r C ) ⌋ Q λ σ ρ → λ ∗ True . For each i ≤ n i \le n i ≤ n , we obtain by induction hypothesis v i ∈ ⟦ R i ⟧ ρ i D ‾ v_i \in\llbracket R_i\rrbracket_{\rho_i}^{\overline{D}} v i ∈ [ [ R i ] ] ρ i D , and thus in particular D ‾ ⊢ ⌊ Q i [ x i \ v i ] ⌋ Q λ ρ i → λ ∗ T r u e \overline{D} \vdash\lfloor Q_i[x_i\backslash v_i]\rfloor_{Q_\lambda\rho_i} \to_\lambda^* \mathbf{True} D ⊢ ⌊ Q i [ x i \ v i ] ⌋ Q λ ρ i → λ ∗ True and we conclude since fv ( v i ) ∩ dom ( σ ) = dom ( ρ i ) \operatorname{fv}(v_i) \cap\operatorname{dom}(\sigma) = \operatorname{dom}(\rho_i) fv ( v i ) ∩ dom ( σ ) = dom ( ρ i ) .
v ∈ ⟦ TtoU ( R ) ⟧ ρ D ‾ ⟹ v ∈ ⟦ R ⟧ ρ D ‾ v \in\llbracket\operatorname{TtoU}(R)\rrbracket_{\rho}^{\overline{D}} \Longrightarrow v \in\llbracket R\rrbracket_{\rho}^{\overline{D}} v ∈ [ [ TtoU ( R ) ] ] ρ D ⟹ v ∈ [ [ R ] ] ρ D : By hypothesis, we have ⌊ Γ ‾ D ⌋ Q λ ⊢ L C v : T C \lfloor\overline{\Gamma}_{D}\rfloor_{Q_\lambda} \vdash_{\mathcal{L}_C} v : T_C ⌊ Γ D ⌋ Q λ ⊢ L C v : T C and D ‾ ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ T r u e \overline{D} \vdash\lfloor\operatorname{wf}_{T_C} x\rfloor_{Q_\lambda}[x\backslash v]_\rho\to_\lambda^* \mathbf{True} D ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ True . Since we have a derivation for v v v of type T C T_C T C , v v v can only be of the shape C v 1 … v n C\ v_1 \ldots v_n C v 1 … v n , and there must be ( R i ) i ≤ n (R_i)_{i\le n} ( R i ) i ≤ n , x x x and r C r_C r C such that Γ ( T C ) ( C ) = ( x i : R i ) i ≤ n → { x : T C ∣ r C } \Gamma(T_C)(C) = (x_i:R_i)_{i\le n} \to\{x:T_C \mid r_C\} Γ ( T C ) ( C ) = ( x i : R i ) i ≤ n → { x : T C ∣ r C } . We need to prove: (1) D ‾ ⊢ r C σ ρ → λ ∗ T r u e \overline{D} \vdash r_C\sigma\rho\to_\lambda^* \mathbf{True} D ⊢ r C σ ρ → λ ∗ True and (2) ∀ i ∈ [ 1.. n ] . v i ∈ ⟦ R i ⟧ ρ i D ‾ \forall i \in[1..n].\ v_i \in\llbracket R_i\rrbracket_{\rho_i}^{\overline{D}} ∀ i ∈ [ 1.. n ] . v i ∈ [ [ R i ] ] ρ i D .
For (1), D ‾ ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ T r u e \overline{D} \vdash\lfloor\operatorname{wf}_{T_C} x\rfloor_{Q_\lambda}[x\backslash v]_\rho\to_\lambda^* \mathbf{True} D ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ True implies as before D ‾ ⊢ ⌊ RtoP ( r C ) ⌋ Q λ σ ρ = r C σ ρ → λ ∗ T r u e \overline{D} \vdash\lfloor\operatorname{RtoP}(r_C)\rfloor_{Q_\lambda\sigma\rho} = r_C\sigma\rho\to_\lambda^* \mathbf{True} D ⊢ ⌊ RtoP ( r C ) ⌋ Q λ σ ρ = r C σ ρ → λ ∗ True by lemma C.11. For (2), let i ≤ n i \le n i ≤ n . Then R i R_i R i is a refinement of a base type by restriction of the system, let R i = { x i ′ : B i ∣ r i } R_i = \{x_i':B_i \mid r_i\} R i = { x i ′ : B i ∣ r i } and TtoU ( R i ) = { x i ′ : Prim ( B i ) ∣ Q i } \operatorname{TtoU}(R_i) = \{x_i':\operatorname{Prim}(B_i) \mid Q_i\} TtoU ( R i ) = { x i ′ : Prim ( B i ) ∣ Q i } . Again, D ‾ ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ T r u e \overline{D} \vdash\lfloor\operatorname{wf}_{T_C} x\rfloor_{Q_\lambda}[x\backslash v]_\rho\to_\lambda^* \mathbf{True} D ⊢ ⌊ wf T C x ⌋ Q λ [ x \ v ] ρ → λ ∗ True implies D ‾ ⊢ ⌊ Q i ⌋ Q λ σ ρ = r C σ ρ → λ ∗ T r u e \overline{D} \vdash\lfloor Q_i\rfloor_{Q_\lambda\sigma\rho} = r_C\sigma\rho\to_\lambda^* \mathbf{True} D ⊢ ⌊ Q i ⌋ Q λ σ ρ = r C σ ρ → λ ∗ True by lemma C.12. Moreover, since there is a derivation for v v v , there is also a derivation for v i v_i v i . Hence, v i ∈ ⟦ R i ⟧ ρ i D ‾ v_i \in\llbracket R_i\rrbracket_{\rho_i}^{\overline{D}} v i ∈ [ [ R i ] ] ρ i D , and thus by induction v i ∈ ⟦ R i ⟧ ρ D ‾ v_i \in\llbracket R_i\rrbracket_{\rho}^{\overline{D}} v i ∈ [ [ R i ] ] ρ D .
Now, consider R = x : R x → R ′ R = x:R_x \to R' R = x : R x → R ′ . If R x R_x R x is an arrow type, it is translated to an instance of Pack. The translations of Pack and dependent arrows of Rocq and of λ r \lambda_r λ r in λ → \lambda_{\to} λ → are all equal. So we conclude by induction hypothesis, since the interpretations are built in the same way. □
Implementation of Packs
Our description of Packs in the main text was simplified to records containing the refined definition, graph relation, the lemma relating them and functionhood for the graph relation. In reality, to be able to implement a generic Pack typeclass for all function arities and for higher-order arguments (becoming nested Packs), we encode function arguments as a list. In this appendix, we give more details about the implementation of Packs, and precise the translations.
Definition of Function Packs
The argument types of a function are stored in a dependent list, the type ArgListT is the type of dependent lists of argument types. The type ArgList represents a dependent list of terms of those types given by a term argTps : ArgListT. Analogously, the types UArgListT and UArgList represent respectively lists of unrefined types, and lists of arguments of a term uargTps : UArgListT. The relation projectsArgListT indicates whether an UArgListT contains exactly the unrefined types of a given ArgListT and the function prArgList extracts an UArgList out of an ArgList. We use these encodings of arrow types to define the typeclasses Pack (refined) and uPack (unrefined) in Rocq.
Definition D.1 (Function Packs). The definition of the typeclasses is given in listing 5. In the parameters, argTps is the dependent list of the refined argument types, uargsTps the list of unrefined argument types, related to argTps by the projection prJ. The unrefined return type is T, refined by the predicate p.
Because of the encoding, we use a tactic buildPackG to populate the typeclass instances and tactics to project individual fields. buildPackG takes the refined definition, the unrefined graph relation, and the two Pack lemmas as parameters. There is a similar tactic buildUPackG for an unrefined Pack. The field of the refined function is retrieved with the tactic getPackF implicitly doing the conversion from the encoding. The graph relation is retrieved with getPackRel from a refined Pack and getUPackRel from an unrefined Pack. There are also tactics to retrieve the lemmas.
Class Pack (argTps:ArgListT) {uargTps:UArgListT} {prj:projectsArgListT argTps uargTps}
(T: Type) (p: ∀ (args: ArgList argTps), T → Prop) := {
def (args:ArgList argTps): {v:T | p args v}; (* Refined definition *)
rel (uargs: UArgList uargTps) (v:T): Prop; (* Graph relation *)
f_f_rel (args:ArgList argTps) (v:T): ⌊def args⌋ = v ↔ rel (prArgList args uargTps
↪ prj) v;
funct (uargs: UArgList uargTps) v v': rel uargs v → rel uargs v' → v = v' }.
Class uPack (uargTps: UArgListT) (T: Type) := {
rel_u (uargs: UArgList uargTps): T → Prop; (* Graph relation *)
funct_u (uargs:UArgList uargTps) (v v':T): rel_u uargs v → rel_u uargs v'
→ v = v'
}.
The projection of a refined Pack x x x to an unrefined Pack can still be defined as projPack( x ) (x) ( x ) ≔ \coloneqq : = {|rel_u := x x x .(rel); funct_u := x x x _pack.(funct)|}, despite the encoding.
Translations. With the definition of Packs as using lists of arguments, the translations of function types as Pack are changed. In the unrefined translation, we have the following, using the list notation for an element of type UArgListT.
TtoU ( ( x i : R i ) i ≤ n → R ) = uPack [ TtoU ( R 1 ) , … , TtoU ( R n ) ] TtoU ( R ) \operatorname{TtoU}\left((x_i:R_i)_{i\le n} \to R\right) = \operatorname{uPack}\ [\operatorname{TtoU}(R_1),\ldots,\operatorname{TtoU}(R_n)]\ \operatorname{TtoU}(R) TtoU ( ( x i : R i ) i ≤ n → R ) = uPack [ TtoU ( R 1 ) , … , TtoU ( R n )] TtoU ( R )
In the refined translation, we have:
TtoR ( ( x i : R i ) i ≤ n → R ) \operatorname{TtoR}\left((x_i:R_i)_{i\le n} \to R\right) TtoR ( ( x i : R i ) i ≤ n → R )
≅ Pack [ x 1 : TtoR ( R 1 ) , … , TtoR ( R n ) ] U ( λ args v , ltac : ( flattenP p args v ) ) \mathrel{\cong}\operatorname{Pack}\ [x_1:\operatorname{TtoR}(R_1),\ldots,\operatorname{TtoR}(R_n)]_{\mathsf{U}}\ (\lambda\operatorname{args}\ v,\ \operatorname{ltac}:(\operatorname{flattenP}\ p\ \operatorname{args}\ v)) ≅ Pack [ x 1 : TtoR ( R 1 ) , … , TtoR ( R n ) ] U ( λ args v , ltac : ( flattenP p args v ))
Where { x : U ∣ p x 1 … x n x } = TtoR ( R ) \{x:\mathsf{U}\mid p\ x_1\ldots x_n\ x\} = \operatorname{TtoR}(R) { x : U ∣ p x 1 … x n x } = TtoR ( R ) and the first argument of Pack is a dependent list of type ArgList. We use a tactic flattenP to transform the n + 1 n+1 n + 1 -ary predicate p p p into a predicate that takes an argument list of length n n n as first parameter (if the predicate does not depend on its first n n n parameters, the translation avoids the tactic and directly inserts the n + 1 n+1 n + 1 -ary predicate).
Example D.2. Consider the type R R R of the function apply from the overview (§ 2.6 ).
R ≅ f : ( y s : IList → { v : Int ∣ 0 ≤ v } ) → x s : IList → { v : Int ∣ v = f x s } R \mathrel{\cong} f:(ys:\operatorname{IList}\to\{v:\operatorname{Int}\mid0\le v\})\to xs:\operatorname{IList}\to\{v:\operatorname{Int}\mid v=f\ xs\} R ≅ f : ( y s : IList → { v : Int ∣ 0 ≤ v }) → x s : IList → { v : Int ∣ v = f x s }
The type of the higher-order parameter f f f is translated as:
S f ≅ Pack [ IList ] Z ( λ args v , match args with [ y s ] ⇒ leqZ_rel 0 v true end ) S_f \mathrel{\cong}\operatorname{Pack}[\operatorname{IList}]\ \mathsf{Z}\ (\lambda\operatorname{args}\ v,\ \operatorname{match}\ \operatorname{args}\ \operatorname{with}\ [ys]\Rightarrow\operatorname{leqZ\_rel}\ 0\ v\ \operatorname{true}\ \operatorname{end}) S f ≅ Pack [ IList ] Z ( λ args v , match args with [ y s ] ⇒ leqZ_rel 0 v true end )
And the full translation of R R R as the type of the Rocq refined function is thus:
∀ ( f : S f ) ( x s : IList ) , { v : Z ∣ ∃ z , ( getPackRel f ) ⌊ ∣ x s ∣ ⌋ z ∧ v = = z } \forall(f:S_f)\ (xs:\operatorname{IList}),\ \{v:\mathsf{Z}\mid\exists z,\ (\operatorname{getPackRel}\ f)\ \lfloor|xs|\rfloor\ z\land v==z\} ∀ ( f : S f ) ( x s : IList ) , { v : Z ∣ ∃ z , ( getPackRel f ) ⌊ ∣ x s ∣ ⌋ z ∧ v == z }
Subsumption for Packs
In this section, we describe subsumption for Packs. In order to uniformly translate subsumption casts of subset types and of function Packs inside the list of arguments of a function, we define a typeclass for subsumption casts. This class has two fields: the cast result and a property that cast does not affect projection (since subtyping only affects refinements).
Class Subsumption {U: Type} (S T : Type) {prS: Projection S U} {prT: Projection T U}
→ (x: S) :=
{ sub_res: T; cast_pr: ⌊|sub_res|⌋ = ⌊|x|⌋ }.
The instance subRef for subset types is defined using the subsumption function given in § 3.6.1 . To define an instance for Packs, we first define an inductive type to record the requirements for a successful subsumption cast between ArgLists:
Inductive SubArgList: ArgListT → ArgListT → Type :=
| noArgsSub: SubArgList noArgsT noArgsT
| consArgsSub (U S T:Type) (prS: Projection S U) (prT: Projection T U)
(argsS: ∀(x:S), ArgListT) (argsT: ∀(x:T), ArgListT) (cast: ∀(x:S), Subsumption S T
→ x) (x:S)
: SubArgList (argsS x) (argsT (cast x).(sub_res)) → SubArgList (S ::RT argsS) (T
→ ::RT argsT).
Where noArgsT and ::RT are the constructor for ArgListT. The instance subPack for Packs has the signature below. Its definition is technical but follows from the signature.
Instance subPack {argTps: ArgListT} {T: Type} {p: ∀ x:ArgList argTps, T → Prop}
{argTps': ArgListT} {q: ∀(x:ArgList argTps'), T → Prop} {uargTps: UArgListT}
{z: projectsArgListT argTps uargTps} {z': projectsArgListT argTps' uargTps}
(fpack: Pack argTps uargTps z T p) (castArg: SubArgList argTps' argTps)
(castT: ∀ (args:ArgList argTps) (args':ArgList argTps'),
Subsumption {v:T | p args v} {v:T | q args' v} (fpack.(f) (subArgList castArg
→ args)))
: Subsumption (Pack argTps uargTps z T p) (Pack argTps' uargTps z' T q) fpack.
Detailed Explanations of Proof Automation
Full Explanation of Lemmas Relating Functions with their Graph Relations
In § 3.4 , we described part of the translation of a reflected function f f f : deriving the refined translation using tactics, the unrefined graph relation, and a lemma f__f_rel to relate both. On top of that, Lava generates numerous other lemmas that are required both to prove f__f_rel and for reasoning steps in automation tactics.
The lemmas generated are, in order:
the functionhood lemma f_rel_funct stating functionhood of the relation f_rel, that is, the relation relates a single output to a set of inputs;
the existence lemma f_rel_ex stating that the projection of the result of applying the function f f f lies in the image of its relation f_rel;
the theorem f__f_rel relating the function f f f with its relation;
the instantiating lemma frel_mk that takes terms t i ˉ \bar{t_i} t i ˉ and proof terms stating that the t i ˉ \bar{t_i} t i ˉ satisfy the refinements of the argument types of f f f and constructs a terms v v v satisfying f_rel t i ˉ \bar{t_i} t i ˉ v v v ;
Besides these four lemmas, the translation also generates a series of inversion lemmas for the graph relation. These lemmas state the equivalence of an application of f_rel with a different condition that is easier to prove. For example, the lemma for the successor case of the relation for multiplication of Peano natural numbers states
Ltac fast_done :=
solve [
quick_wff_wit (* automatically apply the wff lemmas *)
| unshelve eassumption
| symmetry; unshelve eassumption
| reflexivity (* works both for Leibniz equality and the translated equality *)
| destruct_unit (* destruct unit terms, useful to prove they are all equal *)
| lia (* solve integer arithmetic goals *)
| congruence (* solve goals in theory of equality and uninterpreted functions *)
| unshelve intuition (* simplify and apply tauto,
a decision procedure for constructive propositional logic *)
].
Ltac fast_solver := fast_done.
Listing 6. Simplified definition of the fast_done tactic.
∀ ( m n : N a t ) , ( ∃ v , m u l t _ r e l ( S u c m ) n v ) ↔ ( ∃ v ′ , m u l t _ r e l m n v ′ ∧ a d d _ r e l n v ′ v ) \forall(m\ n : \mathrm{Nat}),\ (\exists v,\ \mathrm{mult\_rel}\ (\mathrm{Suc}\ m)\ n\ v) \leftrightarrow(\exists v',\ \mathrm{mult\_rel}\ m\ n\ v' \mathbin{\land} \mathrm{add\_rel}\ n\ v'\ v) ∀ ( m n : Nat ) , ( ∃ v , mult_rel ( Suc m ) n v ) ↔ ( ∃ v ′ , mult_rel m n v ′ ∧ add_rel n v ′ v )
These lemmas are applied for backwards reasoning in the goal in the custom tactics, typically followed by instantiating the existential variables created by the lemmas.
The existence lemma is by far the hardest of these lemmas, for one simple reason: it is the only lemma that actually has to reason about the function definition itself (all other lemmas are either trivial corollaries or avoid reasoning about the functions (possibly utilizing the rewriting lemma for this purpose)).
Full Explanation of the Solver Tactic
As mentioned already in § 5 the solver tactic is build on four levels of simplification and solving tactics of increasing strength.
We will now describe these simplification and goal solving tactics in more detail.
The fast_done tactic. The fast_done tactic combines several native Rocq tactic to quickly solve a goal (or fail quickly).
The quick_simpl and quick_solve tactics. The quick_cleanup tactic contains several (if-then-else related) goal rewrite steps, autounfold and autorewrites with some hint databases. In particular, it will simplify projections of casts and fully applied refined binary operators. The shape_based tactic is defined as a big match (>100 branches) on the proof state doing a single simple forward reasoning step in a hypothesis or goal. It only makes reasoning steps that cannot interfere with our other automation tactics (i.e. they make proving the goal strictly easier) and they are all very simple and fast. Many of the branches simply rewrite with a number of lemmas regarding booleans, equality and boolean connectives. This tactic also serves as the main "plumbing" (along with a number of rewrite/unfold/resolve hints added to a number of hint databases which are applied automatically in many of the automation tactics described in this section) for the overall automation machinery bringing the proof state into a form the other automation tactics can (more easily) handle. The f_equal_ind tactic applies the f_equal tactic to goals that are equalities between applications of the same constructor of an inductive data type. It is useful in various other automation tactics involving backwards reasoning, in particular for reasoning about the automatically derived equality operators for inductive data types. The solving_loop tactic is the backbone of the quick_solve tactic. It corresponds to a single goal solving or simplification step. The quick_solve tactic is then
Ltac quick_simpl := try quick_cleanup; repeat shape_based.
Ltac solving_loop := first [
fast_solver
| f_equal_ind
| progress shape_based
| congruence
| progress subst
| progress (simpl in *)
| progress autorewrite with lia_rewrites fix_notation_hints in *
| left; simpl_loop
| right; simpl_loop
].
Ltac quick_solve := solve [ quick_wff_wit | quick_simpl; repeat solving_loop ].
Listing 7. Simplified definition of quicksolve tactic.
defined as (basically) running first quick_simpl and then running solving_loop until the goal is solved. We explicitly try solving the goal with quick_wff_wit first, to produce faster and simpler proofs for simple well-formedness goals.
The cleanup and quick_solve tactics. The unpack_all tactic destructs context variables of type Pack_ or uPack_.
The axiomatize_ih_specializations tactic replaces the projections of the applications of induction hypothesis with free variables of same type and adds a witnesses of the refinement of that application of the induction hypothesis to the context. It also unifies applications of an induction hypothesis that differ only in witnesses (using proof irrelevance). This helps remove complicated proof terms from the proof state and unify applications of induction hypothesis that differ only in refinement witnesses.
The axiomatize_terms tactic replaces the projections of full applications of reflected functions with variables axiomatized as result of the corresponding relations (and similarly for projections of applications of functions extracted from Packs). A refinement witness for the application is also added to the context.
The unify_vars tactic "unifies" variables axiomatized as the results of the same applications of graph relation using the functionhood lemma of the relation. It also removes duplicated assumptions from the context and substitutes variables by equalities in the context if possible.
The specialize_hyps tactic specialize uniquely axiomatized antecedents in hypothesis (result variables of graph relations that are given elsewhere in the context). It also tries to specialize hypothesis with proof terms generated by the quick_wff_wit or quick_solve tactics.
The invert_axiomatizations tactic inverts the hypothesis axiomatizing variables using the relations, whenever this inversion results in no additional subgoals.
The instantiate_hyps tactic creates terms for uniquely axiomatized variables in hypotheses satisfying the axiomatization. For this, we transitively traverse the relations and create the refined term (uniquely) axiomatized as that variable, then we replace the projection of that refined term with a variable axiomatized using a graph relation. Finally we specialize the hypothesis with it that variable and with it's defining axiom.
autorewrite with f_rel_back rewrites goals using the backwards reasoning lemmas.
Ltac quick_simple_cleanup_steps :=
try unify_vars; try (specialize_hyps; try unify_vars).
Ltac initial_simple_cleanup_steps :=
quick_simpl;
repeat progress autounfold with lia_unfold in *;
simpl_proj;
try unpack_all;
try (axiomatize_ih_specializations; quick_simpl);
try (axiomatize_terms; quick_simpl);
quick_simple_cleanup_steps.
Ltac inversion_cleanup :=
(* forward reasoning *)
try quick_simple_cleanup_steps;
(* backwards reasoning *)
(* backwards reasoning in hypotheses *)
try (invert_axiomatizations; quick_simple_cleanup_steps;
try instantiate_hyps; try specialize_hyps);
(* backwards reasoning in goal *)
try (autorewrite with f_rel_back; repeat instExistGoal);
quick_simple_cleanup_steps.
Ltac cleanup_recurse :=
quick_simpl;
try initial_simple_cleanup_steps;
try inversion_cleanup
Ltac cleanup :=
quick_simpl;
saturate_context;
try cleanup_recurse;
saturate_context;
repeat cleanup_recurse.
Ltac finish := solve [
quick_wff_wit
| first [quicksolve | repeat split; quicksolve] | timeout 5 verit ].
Ltac oracle := solve [ quick_wff_wit | quicksolve | cleanup; finish].
Listing 8. Simplified definition of the oracle tactic
The instExistGoal tactic creates (if not already present in the context) terms to specialize the free (existentially quantified) variables in the goal with, then specializes the goal with them (using the exists tactic). Due to the importance of instantiating existentials, this tactic might do substantial preprocessing (including saturate_context). There is even a version of the tactic included in lia_preprocessor that may use the oracle tactic for solving proof obligations when synthesizing terms with which to instantiate the existentials.
Ltac preprocessor :=
repeat progress (try cleanup; try lia_preprocessor).
Ltac solver := first [
quick_wff_wit
| lia_preprocessor; lia
| preprocessor; first [lia | oracle | shelve]
].
Ltac solver := first [
quick_wff_wit
| quicksolve
| quick_simpl; repeat progress first [simpl in *; cleanup | lia_preprocessor];
first [quicksolve | solve [unshelve verit] | shelve]
].
Listing 9. Simplified explanation of solver tactic.
The saturate_context tactic generates refinement witnesses for the variables axiomatized using graph relations in the context. This requires reconstructing refined terms for result variables of graph relations (similar to what is done in the instantiate_hyps tactic). As a consequence, it is frequently very slow, so in the actual tactic definitions, it is only run after a full (failed) run without it. As a consequence, in the few cases where context saturation is actually necessary to prove a goal, the verification time for the goal will be quite substantial.
The solver tactic. The lia_preprocessor tactic autounfolds and autorewrites with a number of lemmas to simplify integer reasoning goals to something the lia tactic can hopefully handle. It also tries to decide which branch of a disjunction in the goal to take using the oracle tactic to prove subgoals and it tries to specialize hypotheses with proof terms using the oracle tactic. The solver tactic then improves upon the oracle tactic merely by additionally preprocessing using the lia_preprocessor tactic and by shelving subgoals it cannot solve.