% Beluga Development
% Author: Brigitte Pientka
%{{
# Algorithmic Equality for the Polymorphic Lambda-calculus (G-version)
We discuss completeness of algorithmic equality for typed lambda-terms with respect to declarative equality of lambda-terms.
This case-study is part of ORBI, Open challenge problem Repository for systems reasoning with BInders. For a detailed description of the proof and a discussion regarding other systems see (Felty et al, 2014).
The mechanization highlights several aspects:
- Context schemas with alternative assumptions
- Induction on universally quantified objects
- Stating and proving properties in a generalized context
- Reasoning using context subsumption
## Syntax
The polymorphic lambda-calculus is introduced with the following declarations: }}%
tp : type. --name tp T a.
arr : tp -> tp -> tp.
all : (tp -> tp) -> tp.
term: type. --name term M x.
app : term -> term -> term.
lam : (term -> term) -> term.
tlam: (tp -> term) -> term.
tapp: term -> tp -> term.
%{{## Judgements and Rules
We describe algorithmic and declarative equality for the polymorphic lambda-calculus as judgements using axioms and inference rules. The Beluga code is a straightforward HOAS encoding of the associated rules.}}%
% -----------------------------------------------------------------
%{{### Algorithmic Equality for types
We add the judgement for type equality atp
of type tm -> tm -> type
along with inference rules for universal quantifiers at_al
and arrow types at_arr
.}}%
atp: tp -> tp -> type. --name atp Q u.
at_al : ({a:tp} atp a a -> atp (T a) (S a))
-> atp (all T) (all S).
at_arr: atp T1 T2 -> atp S1 S2
-> atp (arr T1 S1) (arr T2 S2).
%{{### Algorithmic Equality for terms
We extend the term equality judgement given for the untyped lambda-calculus with rules for type abstraction ae_tl
and type application ae_ta
.}}%
aeq: term -> term -> type. --name aeq D u.
ae_a : aeq M1 N1 -> aeq M2 N2
-> aeq (app M1 M2) (app N1 N2).
ae_l : ({x:term} aeq x x -> aeq (M x) (N x))
-> aeq (lam (\x. M x)) (lam (\x. N x)).
ae_tl: ({a:tp} atp a a -> aeq (M a) (N a))
-> aeq (tlam (\a. M a)) (tlam (\a. N a)).
ae_ta : aeq M N -> atp T S
-> aeq (tapp M T) (tapp N S).
%{{Note that type equality atp A B
can be defined independently of term equality aeq M N
. In other words, aeq M N
depends on atp A B
, but not vice-versa.}}%
% ----------------------------------------------------------------- %
%{{### Declarative Equality for types
We define declarative equality for types in order to establish its equivalence with algorithmic equality and prove completeness. Rules for reflexivity, transitivity, and symmetry are explicitly derived.}}%
dtp: tp -> tp -> type. --name atp P u.
dt_al : ({a:tp}dtp a a -> dtp (T a) (S a))
-> dtp (all T) (all S).
dt_arr: dtp T1 T2 -> dtp S1 S2
-> dtp (arr T1 S1) (arr T2 S2).
dt_r: dtp T T.
dt_t: dtp T R -> dtp R S
-> dtp T S.
dt_s: dtp T S -> dtp S T.
%{{### Declarative Equality for terms
Declarative equality for terms is encoded similarly to its counterpart. Again, we are extending the Untyped Equality case study to account for polymorphism with constructors for type abstraction de_tl
and type application de_ta
}}%
deq: term -> term -> type.
de_l: ({x:term} deq x x -> deq (M x) (N x))
-> deq (lam (\x. M x)) (lam (\x. N x)).
de_a: deq M1 N1 -> deq M2 N2
-> deq (app M1 M2) (app N1 N2).
de_tl: ({a:tp} dtp a a -> deq (M a) (N a))
-> deq (tlam (\a. M a)) (tlam (\a. N a)).
de_ta : deq M N -> dtp T S
-> deq (tapp M T) (tapp N S).
de_r: deq M M.
de_t: deq M L -> deq L N
-> deq M N.
de_s: deq T S -> deq S T.
% ----------------------------------------------------------------- %
%{{## Context declarations
Just as types classify expressions, contexts are classified by context schemas.}}%
schema atpCtx = block a:tp , _t:atp a a;
%{{Since the case for lambda-abstraction lam
deals with term assumptions while the type abstraction tlam
introduces type assumptions, we need to specify alternating assumptions. This alternation of blocks is described by using +
in Beluga's concrete syntax.}}%
schema aeqCtx = block (x:term, _u:aeq x x) + block (a:tp , _t:atp a a);
schema dtpCtx = block a: tp, u:atp a a , _t:dtp a a ;
schema deqCtx = block x: term, u:aeq x x , _t:deq x x
+ block a: tp, u:atp a a , _t:dtp a a ;
% ----------------------------------------------------------------- %
% Admissibility of Reflexivity
%{{## Proof of Reflexivity for Types
The reflexivity for types is implemented as a recursive function called reftp
of type: {gamma:atpCtx}{T:[gamma |- tp ]}[gamma |- atp T T]
. This can be read as: for all contexts g
that have schema atpCtx
, for all types T
, we have a proof that [ g |- atp T T]
. Quantification over contexts and contextual objects in computation-level types is denoted between braces {}
; the corresponding abstraction on the level of expressions is written as mlam g => mlam T1 => e
.}}%
rec reftp : {gamma:atpCtx} {T:[gamma |- tp]} [gamma |- atp T T] =
mlam gamma => mlam T => case [gamma |- T] of
| [gamma |- #p.1] => [gamma |- #p.2]
| [gamma |- all \x. T] =>
let [gamma,b:block a:tp , _t:atp a a |- D[..,b.1,b.2]] =
reftp [gamma, b:block a:tp , _t:atp a a] [gamma, b |- T[..,b.1]]
in
[gamma |- at_al \x. \w. D]
| [gamma |- arr T S] =>
let [gamma |- D1] = reftp [gamma] [gamma |- T ] in
let [gamma |- D2] = reftp [gamma] [gamma |- S ] in
[gamma |- at_arr D1 D2]
;
%{{In the proof for refltp
we begin by introducing and T
followed by a case analysis on [gamma |- T]
using pattern matching. There are three possible cases for T
:
T
is a variable from g
, we write [gamma |- #p.1]
where #p
denotes a parameter variable declared in the context g
. Operationally, #p
can be instantiated with any bound variable from the context g
. Since the context g
has schema atpCtx
, it contains blocks a:tp , _t:atp a a;
. The first projection allows us to extract the type component, while the second projection denotes the proof of _t:atp a a;
.
-Existential case. If T
is an existential quantification, then we extend the context and appeal to the induction hypothesis by making a recursive call. Beluga supports declaration weakening which allows us to use T
that has type [gamma, a:tp |- tp ]
in the extended context [gamma, b:block a:tp , _t: atp a a]
. We simply construct a weakening substitution .. b.1
with domain g,a:tp
and range g, b:block a:tp , _t: atp a a
that essentially renames a
to b.1
in T
. The recursive call returns [gamma,b:block a:tp , _t:atp a a |- D[.., b.1 ,b.2]]
. Using it together with rule at_la
we build the final derivation.
-Arrow case. If T
is an arrow type, we appeal twice to the induction hypothesis and build a proof for [gamma |- atp (arr T S) (arr T S)]
.
ref
encodes the proof reflexivity for terms. The type signature reads: for all contexts g
that have schema aeqCtx
, for all terms M
, we have a proof that [ g |- aeq M M]
.}}%
rec ref : {gamma:aeqCtx} {M:[gamma |- term]} [gamma |- aeq M M] =
mlam gamma => mlam M => case [gamma |- M] of
| [gamma |- #p.1] => [gamma |- #p.2]
| [gamma |- lam \x. M] =>
let [gamma,b:block y:term , _t:aeq y y |- D[..,b.1,b.2]] =
ref [gamma, b:block y:term , _t:aeq y y] [gamma, b |- M[..,b.1]]
in
[gamma |- ae_l \x. \w. D]
| [gamma |- app M1 M2] =>
let [gamma |- D1] = ref [gamma] [gamma |- M1 ] in
let [gamma |- D2] = ref [gamma] [gamma |- M2 ] in
[gamma |- ae_a D1 D2]
| [gamma |- tlam \a. M] =>
let [gamma,b:block a:tp , _t:atp a a |- D[..,b.1,b.2]] =
ref [gamma, b:block a:tp , _t:atp a a] [gamma, b |- M[..,b.1]]
in
[gamma |- ae_tl \x. \w. D]
| [gamma |- tapp M T] =>
let [gamma |- D1] = ref [gamma] [gamma |- M ] in
let [gamma |- D2] = reftp [gamma] [gamma |- T ] in
[gamma |- ae_ta D1 D2]
;
%{{This time, there are five possible cases for our meta-variable M
:
M
is a variable from g
, we write [gamma |- #p.1]
where #p
denotes a parameter variable declared in the context g
. Operationally, #p
can be instantiated with any bound variable from the context g
. Since the context g
has schema aeqCtx
, it contains blocks x:tm , ae_v:aeq x x.
The first projection allows us to extract the term component, while the second projection denotes the proof of aeq x x
.
-Lambda-abstraction case. If M
is a lambda-term, then we extend the context and appeal to the induction hypothesis by making a recursive call. Automatic context subsumption comes into play again, allowing us to use M that has type [gamma, x:tm |- tm ]
in the extended context [gamma, b:block y:tm , ae_v: aeq y y]
. We simply construct a weakening substitution .. b.1
with domain g,y:tm
and range g, b:block y:tm , ae_v:aeq y y.
that essentially renames y
to b.1
in M
. The recursive call returns [gamma,b:block y:tm ,ae_v:aeq y y |- D[.., b.1 b.2]]
. Using it together with rule ae_l
we build the final derivation.
-Term application case. If M
is an application, we appeal twice to the induction hypothesis and build a proof for [gamma |- aeq (app M1 M2) (app M1 M2)]
.
-Type abstraction case. If M
is a type abstraction, then we extend the context and appeal to the induction hypothesis by making a recursive call. We use M
that has type [gamma, a:tp |- tp ]
in the extended context [gamma, b:block a:tp , _t: atp a a]
and construct a weakening substitution .. b.1
with domain g,a:tp
and range g, b:block a:tp , _t: atp a a
that essentially renames a
to b.1
in T
. The recursive call returns [gamma,b:block a:tp , _t:atp a a |- D[.., b.1, b.2]]
. Using it together with rule at_la
we build the final derivation.
-Type application case. If M
is a type application, we appeal twice to the induction hypothesis and build a proof for [gamma |- aeqCtx (tapp M T) (tapp M T)]
.