betaFeedback

WorksComputer Science

Lava: Foundations for Liquid Haskell via Translation to Rocq

Abstract

Liquid Haskell is an extension of the Glasgow Haskell compiler (GHC) that adds support for refinement types: types restricted by logical predicates. Specifications are automatically checked by an SMT solver, thus offering lightweight verification directly within the language. However, the necessarily large trusted codebase behind Liquid Haskell (from GHC to the SMT solver to the refinement typechecker itself) raises the question of trust in the verification and the necessity for a formalized semantics of Liquid Haskell linked to its implementation. Grounding Liquid Haskell in theory is complicated by the interaction between two semantic levels in the language: a computational one and a logical one (for refinement predicates). While both are well understood separately, their interaction is subtle and has only been partially described. We give a semantics to Liquid Haskell by translation to Rocq. Both the computational and logical parts are translated to the same language, with a well-understood semantics; our translation makes the discrepancy between the two semantic levels explicit. The translation is implemented in Haskell and comes with a Rocq library that includes powerful proof automation standing in for the SMT solver. Thus, on the practical level, our tool Lava serves as a certificate checker for the supported subset of Liquid Haskell.

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 ee 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\}. Since 5<x5 < x implies 0<x0 < x, for all integers xx, ee can also be used at type {x:Int⁡∣0<x}\{x:\operatorname{Int} \mid0 < 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}
SMT-Valid⁡(⟦Γ⟧⇒⟦rl⟧⇒⟦rr⟧)Γ⊢{x:B∣rl}⪯{x:B∣rr}\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\}}

The left rule, T-SUB, allows for implicit subtyping: an expression ee of type τl\tau_l can be given the type τr\tau_r, if τl\tau_l is a subtype of τr\tau_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∣rl}\{x : B \mid r_l\} is a subtype of {x:B∣rr}\{x : B \mid r_r\} if the implication rl⇒rrr_l \Rightarrow 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\} 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:Int∣0<x}\{x:\mathrm{Int} \mid0 < 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 xx, 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 Int\mathrm{Int}, with a logical predicate. In div and below, we write Int\mathrm{Int} for the trivially refined {v:Int∣True}\{v:\mathrm{Int} \mid\mathrm{True}\}.

In this snippet, the binder xx has three different semantics. When the snippet is seen as a Haskell program, the binder xx is a a) program integer that can be evaluated to 8. But Liquid Haskell also attaches to xx the information that it is greater than 5. Thus, in type theoretical terms, xx is a b) dependent pair storing the evidence that the integer attached to xx 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 and check it using an SMT solver. When passed to the SMT solver, the binder xx is a c) logical integer that admits arithmetic operations.

Two parallel worlds coexist:

  • the refined world of the Liquid Haskell type checker, where xx is the b) dependent pair and

  • the unrefined world where xx 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 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:

  1. 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.

  1. 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.

  1. 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, a step not described in this paper. Second, typechecking the λr\lambda_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\} denote the set of elements ee of type BB such that r[x∖e]r[x \setminus e] holds. Rocq also supports subset types, using the same notation to denote a dependent pair of an element of e:Be:B and a proof of r[x∖e]r[x \setminus e].

Our list indexing example depends on integers. Liquid Haskell comes with a builtin integer type, Int\mathit{Int}, with over- and underflow left as undefined behavior. We translate these underspecified machine integers to Rocq’s unbounded integer type Z\mathbb{Z}. We translate the Liquid Haskell type of integers greater than five {x:Int∣5<x}\{x:\mathsf{Int} \mid5 < x\} to Rocq (definition 3.6) as {x:Z∣<rel 5 x true}\{x:\mathbb{Z} \mid<_{\mathrm{rel}}\ 5\ x\ \mathsf{true}\}.

Refinement types translate to Rocq’s subset types, so refinements translate to Rocq’s propositions, in Prop. Here, 5<x5 < x turns into a proposition about <rel<_{\mathrm{rel}}, the three-way relation modeling << on integers: (5,x,true)∈<rel(5,x,\mathsf{true}) \in<_{\mathrm{rel}} if and only if 5<x5 < 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:Int∣5<x}\{x:\mathsf{Int} \mid5 < x\} is instantiated by a Haskell integer expression, as verification of the refinement is external. In Rocq, given x:Zx:\mathbb{Z} and px:<rel 5 x truep_x: <_{\mathrm{rel}}\ 5\ x\ \mathsf{true}, we build a value of the type {x:Z∣<rel 5 x true}\{x:\mathbb{Z} \mid<_{\mathrm{rel}}\ 5\ x\ \mathsf{true}\} as exist⁡ (∀(x:Z),<rel 5 x) true x px\operatorname{exist}\ (\forall(x:\mathbb{Z}), <_{\mathrm{rel}}\ 5\ x)\ \mathsf{true}\ x\ p_x, where exist is the constructor for dependent pairs, requiring both an integer expression xx and a proof pxp_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⁡ Nil0 \le i \land i < \operatorname{llen}\ \mathrm{Nil}, but llen⁡ Nil=0\operatorname{llen}\ \mathrm{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, 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, 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=0i = 0 is annotated with an injection to a singleton type: inj⁡ (x, {v:Int∣5<v})\operatorname{inj}\ (x,\ \{v:\mathsf{Int}\mid5 < 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 IList\mathsf{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:

get::xs:IList→i:{Int∣0≤i∧i<llen (proj xs)}→{v:Int∣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\}

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:

get Nil::i:{Int∣0≤i∧i<llen (proj Nil)}→{v:Int∣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\}

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 nn-ary function into an n+1n+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:

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 v→get_rel (Cons_gt5_u x xs′) 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}

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⁡ xs⟧⇒⟦u==()⟧)⇒⟦get⁡ xs i==get⁡ (Cons⁡ x xs) (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

In our intermediate calculus λr\lambda_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 ff 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 ff 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.(rel)f.(\mathrm{rel}) from the pack, while in the body we use the refined function f.(def)f.(\mathrm{def}).2 The function apply can be applied to the pack get_pack. Then, the projection f.(rel)f.(\mathrm{rel}) is instantiated to get_rel, while f.(def)f.(\mathrm{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.(rel_u)f.(\mathrm{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∣≤rel0 v true}\{v:Z \mid\leq_{\mathrm{rel}} 0\ v\ \mathrm{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 (presented below).

The second step elaborates the terms of λr\lambda_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-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

Figure 1 presents the syntax of λr\lambda_r, a core λ\lambda-calculus with refinement types, user-defined first-order inductive data types and functions. The core grammar of λr\lambda_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.

Syntax of lambda sub r

Figure 1. Syntax of λr\lambda_r; its monomorphic base types BB are built into more general refined types RR. 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 uses the metavariables TCTC and CC 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\}. The name vv is reserved for binders in refinement types. In the extended syntax, variables are annotated with their arity (nn) and their scope: whether they are top-level (GG), local (LL), or the name of the function being defined (YY).

References

  1. [1]Andreas Abel, Marcin Benke, Ana Bove, John Hughes, and Ulf Norell. 2005. Verifying Haskell Programs Using Constructive Type Theory. In Proceedings of the 2005 ACM SIGPLAN workshop on Haskell (2005-09) (Haskell’05). ACM, 62–73. doi:10.1145/1088348.1088355DOI
  2. [2]Michael Armand, Germain Faure, Benjamin Grégoire, Chantal Keller, Laurent Théry, and Benjamin Werner. 2011. A Modular Integration of SAT/SMT Solvers to Coq through Proof Witnesses. In Lecture Notes in Computer Science (2011). Springer Berlin Heidelberg, 135–150. doi:10.1007/978-3-642-25379-9_12DOI
  3. [3]Robert Atkey, Patricia Johann, and Neil Ghani. 2011. When Is a Type Refinement an Inductive Type?. In Foundations of Software Science and Computational Structures (2011) (Lecture Notes in Computer Science, Vol. 6604), Martin Hofmann (Ed.). Springer Berlin Heidelberg, 72–87. doi:10.1007/978-3-642-19805-2_6DOI
  4. [4]Robert Atkey, Patricia Johann, and Neil Ghani. 2012. Refining Inductive Types. Logical Methods in Computer Science Volume 8, Issue 2 (June 2012). doi:10.2168/lmcs-8(2:9)2012DOI
  5. [5]Clark Barrett, Christopher L. Conway, Morgan Deters, Liana Hadarean, Dejan Jovanović, Tim King, Andrew Reynolds, and Cesare Tinelli. 2011. CVC4. In Computer Aided Verification (2011). Springer Berlin Heidelberg, 171–177. doi:10.1007/978-3-642-22110-1_14DOI
  6. [6]Lasse Blaauwbroek, Josef Urban, and Herman Geuvers. 2020. The Tactician: A Seamless, Interactive Tactic Learner and Prover for Coq. Springer International Publishing, 271–277. doi:10.1007/978-3-030-53518-6_17DOI
  7. [7]Jasmin C. Blanchette, Cezary Kaliszyk, Lawrence C. Paulson, and Josef Urban. 2016. Hammering Towards QED. Journal of Formalized Reasoning Vol 9 (2016), No 1 (2016): Special Issue: Twenty Years of the QED Manifesto. doi:10.6092/ISSN.1972-5787/4593
  8. [8]Valentin Blot, Denis Cousineau, Enzo Crance, Louise Dubois de Pris­que, Chantal Keller, Assia Mahboubi, and Pierre Vial. 2023. Compositional Pre-processing for Automated Reasoning in Dependent Type Theory. In Proceedings of the 12th ACM SIGPLAN International Conference on Certified Programs and Proofs (Boston, MA, USA, 2023-01) (CPP ’23). ACM, New York, NY, USA, 63–77. doi:10.1145/3573105.3575676DOI
  9. [9]Valentin Blot, Louise Dubois de Pris­que, Chantal Keller, and Pierre Vial. 2021. General Automation in Coq through Modular Transformations. (July 2021). doi:10.48550/arXiv.2107.01235DOI
  10. [10]Michael H. Borkowski, Niki Vazou, and Ranjit Jhala. 2024. Mechanizing Refinement Types. 8, POPL (2024), 2099–2128. doi:10.1145/3632912DOI
  11. [11]Thomas Bouton, Diego Caminha B. de Oliveira, David Déharbe, and Pascal Fontaine. 209. veriT: An Open, Trustable and Efficient SMT-Solver. In Automated Deduction – CADE-22 (2009). Springer Berlin Heidelberg, 151–156. doi:10.1007/978-3-642-02959-2_12
  12. [12]Joachim Breitner, Antal Spector-Zabusky, Yao Li, Christine Rizkallah, John Wiegley, and Stephanie Weirich. 2018. Ready, Set, Verify! Applying hs-to-coq to real-world Haskell code. Proceedings of the ACM on Programming Languages 2, ICFP, Article 89 (July 2018), 16 pages. doi:10.1145/32536784
  13. [13]Arthur Charguéraud. 2010. Program Verification Through Characteristic Formulae. ACM SIGPLAN Notices 45, 9 (2010), 321–332. doi:10.1145/1932681.1863590DOI
  14. [14]Arthur Charguéraud, Jean-Christophe Filliâtre, Cláudio Lourenço, and Mário Pereira. 2019. GOSPEL - Providing OCaml with a Formal Specification Language. In Formal Methods - The Next 30 Years - Third World Congress, FM 2019, Porto, Portugal, October 7-11, 2019, Proceedings (Lecture Notes in Computer Science, Vol. 11800), Maurice H. ter Beek, Annabelle McIver, and José N. Oliveira (Eds.). Springer, 484–501. doi:10.1007/978-3-030-30942-8_29DOI
  15. [15]Jan Christiansen, Sandra Dylus, and Niels Bunkenburg. 2019. Verifying Effectful Haskell Programs in Coq. In Proceedings of the 12th ACM SIGPLAN International Symposium on Haskell (2019-08) (ICFP ’19). ACM, 125–138. doi:10.1145/3331545.3342592DOI
  16. [16]Guillaume Claret. 2018. Program in Coq. Ph. D. Dissertation. Université Sorbonne Paris Cité. https://inria.hal.science/tel-01890838v1/
  17. [17]Łukasz Czajka and Cezary Kaliszyk. 2018. Hammer for Coq: Automation for Dependent Type Theory. Journal of Automated Reasoning 61, 1–4 (2018), 423–453. doi:10.1007/s10817-018-9458-4DOI
  18. [18]Sandra Dylus, Jan Christiansen, and Finn Teegen. 2018. One Monad to Prove Them All. The Art, Science, and Engineering of Programming 3, 3 (Feb. 2018). doi:10.22152/programming-journal.org/2019/3/8 arXiv:1805.08059 [cs.PL]DOI
  19. [19]EPFL/LARA. 2024. Translation from Stainless to Coq. https://epfl-lara.github.io/stainless/coq.html Accessed: 2025-07-07.
  20. [20]Jacques Garrigue and Takafumi Saikawa. 2022. Validating OCaml Soundness by Translation into Coq. (2022). https://types22.inria.fr/files/2022/06/TYPES_2022_paper_15.pdf Abstract for Types 2022.
  21. [21]Jad Elkhalleyi Ghalayini and Neel Krishnaswami. 2023. Explicit Refinement Types. In Proceedings of the ACM on Programming Languages (2023-08), Vol. 7, ACM, 187–214. doi:10.1145/3607837
  22. [22]Jad Hamza, Nicolas Voirol, and Viktor Kunčak. 2019. System FR: formalized foundations for the stainless verifier. 3, OOPSLA (2019), 1–30. doi:10.1145/3360592DOI
  23. [23]Graham Hutton. 1999. A Tutorial on the Universality and Expressiveness of Fold. Journal of Functional Programming 9 (09 1999). doi:10.1017/S0956796899003500DOI
  24. [24]Kenneth Knowles and Cormac Flanagan. 2010. Hybrid Type Checking. ACM Transactions on Programming Languages and Systems 32, 2 (2010), 1–34. doi:10.1145/1667048.1667051DOI
  25. [25]Nico Lehmann, Adam T. Geller, Niki Vazou, and Ranjit Jhala. 2023. Flux: Liquid Types for Rust. Proc. ACM Program. Lang. 7, PLDI, Article 169 (June 2023), 25 pages. doi:10.1145/3591283DOI
  26. [26]Xinming Ou, Gang Tan, Yitzhak Mandelbaum, and David Walker. 2004. Dynamic Typing with Dependent Types. In Exploring New Frontiers of Theoretical Informatics, Jean-Jacques Levy, Ernst W. Mayr, and John C. Mitchell (Eds.). Kluwer Academic Publishers, Boston, MA, 437–450. doi:10.1007/1-4020-8141-3_34
  27. [27]Benjamin C. Pierce, Arthur Azevedo de Amorim, Chris Casinghino, Marco Gaboardi, Michael Greenberg, Catalin Hrițcu, Vilhelm Sjöberg, and Brent Yorgey. 2021. Logical Foundations (6.0 ed.). Software Foundations, Vol. 1. Electronic textbook, University of Pennsylvania. https://softwarefoundations.cis.upenn.edu/lf-current.html
  28. [28]J. Rushby, S. Owre, and N. Shankar. 1998. Subtypes for specifications: predicate subtyping in PVS. IEEE Transactions on Software Engineering 24, 9 (1998), 709–720. doi:10.1109/32.713327DOI
  29. [29]Amr Sabry and Matthías Felleisen. 1992. Reasoning About Programs in Continuation-Passing Style. In Proceedings of the 1992 ACM conference on LISP and functional programming (San Francisco, California, USA) (LFP ’92). Association for Computing Machinery, New York, NY, USA, 288–298. doi:10.1145/141471.141563
  30. [30]Taro Sekiyama, Atsushi Igarashi, and Michael Greenberg. 2017. Polymorphic Manifest Contracts, Revised and Resolved. ACM Transactions on Programming Languages and Systems 39, 1 (2017), 1–36. doi:10.1145/2994594
  31. [31]Taro Sekiyama, Yuki Nishida, and Atsushi Igarashi. 2015. Manifest Contracts for Datatypes. In Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (Mumbai, India, 2015-01) (POPL ’15). ACM, New York, NY, USA, 195–207. doi:10.1145/2676726.2676996
  32. [32]Matthieu Sozeau. 2007. Subset Coercions in Coq. In Types for Proofs and Programs (2007), Thorsten Altenkirch and Conor McBride (Eds.). Springer Berlin Heidelberg, 237–252. doi:10.1007/978-3-540-74464-1_16DOI
  33. [33]Matthieu Sozeau and Cyprien Mangin. 2019. Equations Reloaded: High-level Dependently-Typed Functional Programming and Proving in Coq. In Proceedings of the ACM on Programming Languages (2019-07), Vol. 3. ACM, 1–29. doi:10.1145/3341690
  34. [34]Antal Spector-Zabusky, Joachim Breitner, Christine Rizkallah, and Stephanie Weirich. 2018. Total Haskell is Reasonable in Coq. In Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and Proofs (Los Angeles, CA, USA, 2018-01) (CPP ’18). ACM, New York, NY, USA, 14–27. doi:10.1145/3167092
  35. [35]Martin Sulzmann, Manuel M. T. Chakravarty, Simon Peyton Jones, and Kevin Donnelly. 2007. System F with type equality coercions. In Proceedings of the 2007 ACM SIGPLAN International Workshop on Types in Languages Design and Implementation (Nice, Nice, France) (TLDI ’07). Association for Computing Machinery, New York, NY, USA, 53–66. doi:10.1145/1190315.1190324DOI
  36. [36]Niki Synek, Cătălin Hrițcu, Chantal Keller, Aseem Rastogi, Antoine Delignat-Lavaud, Simon Forest, Karthikeyan Bhargavan, Cédric Fournet, Pierre-Yves Strub, Markulf Kohlweiss, Jean-Karim Zinzindohoue, and Santiago Zanella-Béguelin. 2016. Dependent types and multi-modal effects in F*. In Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (St. Petersburg, FL, USA) (POPL ’16). Association for Computing Machinery, New York, NY, USA, 256–270. doi:10.1145/2837614.2837655
  37. [37]Sam Tobin-Hochstadt and Matthias Felleisen. 2010. Logical Types for Untyped Languages. In Proceedings of the 15th ACM SIGPLAN international conference on Functional programming (2010-09) (ICFP ’10). ACM, 117–128.
  38. [38]Niki Vazou, Eric L. Seidel, Ranjit Jhala, Dimitrios Vytiniotis, and Simon Peyton-Jones. 2014. Refinement Types for Haskell. In Proceedings of the 19th ACM SIGPLAN international conference on Functional programming (Gothenburg, Sweden, 2014-08) (ICFP’14). ACM, New York, NY, USA, 269–282. doi:10.1145/2628136.2628161DOI
  39. [39]Niki Vazou, Anish Tondwalkar, Vikram Choudhury, Ryan G. Scott, Ryan R. Newton, Philip Wadler, and Ranjit Jhala. 2017. Refinement Reflection: Complete Verification with SMT. Proceedings of the ACM on Programming Languages 2, POPL, Article 53 (Dec. 2017), 31 pages. doi:10.1145/3158411DOI

Paper details

vlnc:W_zbtydkf3
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 10

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents