% 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: }}% %{{## Proof of Reflexivity for Terms The recursive function 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: }}% % ----------------------------------------------------------------- % % General transitivity is admissible %{{## Proof of Transitivity for Types}}% rec transtp: (gamma:atpCtx) [gamma |- atp T R] -> [gamma |- atp R S] -> [gamma |- atp T S] = fn ae1 => fn ae2 => case ae1 of | [gamma |- #p.2] => ae2 | [gamma |- at_al \a.\u. D1[..,a,u]] => let [gamma |- at_al \a.\u. D2[..,a,u]] = ae2 in let [gamma, b:block a:tp , _t:atp a a |- D[..,b.1,b.2]] = transtp [gamma, b:block a:tp , _t:atp a a |- D1[..,b.1,b.2]] [gamma, b |- D2[..,b.1,b.2]] in [gamma |- at_al \a. \u. D[..,a,u]] | [gamma |- at_arr D1 D2] => let [gamma |- at_arr D3 D4] = ae2 in let [gamma |- D] = transtp [gamma |- D1] [gamma |- D3] in let [gamma |- D'] = transtp [gamma |- D2] [gamma |- D4] in [gamma |- at_arr D D'] ; %{{## Proof of Transitivity for Terms}}% rec trans: (gamma:aeqCtx) [gamma |- aeq T R] -> [gamma |- aeq R S] -> [gamma |- aeq T S] = fn ae1 => fn ae2 => case ae1 of | [gamma |- #p.2] => ae2 | [gamma |- ae_l \x.\u. D1] => let [gamma |- ae_l \x.\u. D2] = ae2 in let [gamma, b:block x:term , _t:aeq x x |- D[..,b.1,b.2]] = trans [gamma, b:block x':term , _t:aeq x' x' |- D1[..,b.1,b.2]] [gamma, b |- D2[..,b.1,b.2]] in [gamma |- ae_l \x. \u. D] | [gamma |- ae_a D1 D2] => let [gamma |- ae_a D3 D4] = ae2 in let [gamma |- D] = trans [gamma |- D1] [gamma |- D3] in let [gamma |- D'] = trans [gamma |- D2] [gamma |- D4] in [gamma |- ae_a D D'] | [gamma |- ae_tl \a.\u. D1[..,a,u]] => let [gamma |- ae_tl \a.\u. D2[..,a,u]] = ae2 in let [gamma, b:block a:tp , _t:atp a a |- D[..,b.1,b.2]] = trans [gamma, b:block a:tp , _t:atp a a |- D1[..,b.1,b.2]] [gamma, b |- D2[..,b.1,b.2]] in [gamma |- ae_tl \x. \u. D] | [gamma |- ae_ta D1 Q1] => let [gamma |- ae_ta D2 Q2] = ae2 in let [gamma |- D] = trans [gamma |- D1] [gamma |- D2] in let [gamma |- Q] = transtp [gamma |- Q1] [gamma |- Q2] in [gamma |- ae_ta D Q] ; % ----------------------------------------------------------------- % % General symmetry is admissible %{{## Proof of Symmetry for Types}}% rec symtp: (gamma:atpCtx) [gamma |- atp T R] -> [gamma |- atp R T] = fn ae => case ae of | [gamma |- #p.2] => ae | [gamma |- at_al \x.\u. D] => let [gamma, b:block a:tp , _t:atp a a |- D'[..,b.1,b.2]] = symtp [gamma, b:block a:tp, _t:atp a a |- D[..,b.1,b.2]] in [gamma |- at_al \x.\u. D'] | [gamma |- at_arr D1 D2] => let [gamma |- D1'] = symtp [gamma |- D1] in let [gamma |- D2'] = symtp [gamma |- D2] in [gamma |- at_arr D1' D2'] ; %{{## Proof of Symmetry for Terms}}% rec sym: (gamma:aeqCtx) [gamma |- aeq T R] -> [gamma |- aeq R T] = fn ae => case ae of | [gamma |- #p.2] => ae | [gamma |- ae_l \x.\u. D] => let [gamma, b:block x:term , _t:aeq x x |- D'[..,b.1,b.2]] = sym [gamma, b:block x:term, _t:aeq x x |- D[..,b.1,b.2]] in [gamma |- ae_l \x.\u. D'] | [gamma |- ae_a D1 D2] => let [gamma |- D1'] = sym [gamma |- D1] in let [gamma |- D2'] = sym [gamma |- D2] in [gamma |- ae_a D1' D2'] | [gamma |- ae_tl \x.\u. D] => let [gamma, b:block a:tp , _t:atp a a |- D'[..,b.1,b.2]] = sym [gamma, b:block a:tp, _t:atp a a |- D[..,b.1,b.2]] in [gamma |- ae_tl \x.\u. D'] | [gamma |- ae_ta D Q] => let [gamma |- D'] = sym [gamma |- D] in let [gamma |- Q'] = symtp [gamma |- Q] in [gamma |- ae_ta D' Q'] ; % ----------------------------------------------------------------- % % Completeness %{{## Proof of Completeness for Types}}% rec ctp: (gamma:dtpCtx) [gamma |- dtp T S] -> [gamma |- atp T S] = fn e => case e of | [gamma |- #p.3] => [gamma |- #p.2] | [gamma |- dt_r] => reftp [gamma] [gamma |- _ ] | [gamma |- dt_arr F1 F2] => let [gamma |- D1] = ctp [gamma |- F1] in let [gamma |- D2] = ctp [gamma |- F2] in [gamma |- at_arr D1 D2] | [gamma |- dt_al (\x.\u. F)] => let [gamma,b:block a:tp,u:atp a a , _t:dtp a a |- D[..,b.1,b.2]] = ctp [gamma, b:block a:tp, u:atp a a , _t:dtp a a |- F[..,b.1,b.3]] in [gamma |- at_al (\x.\v. D)] | [gamma |- dt_t F1 F2] => let [gamma |- D2] = ctp [gamma |- F2] in let [gamma |- D1] = ctp [gamma |- F1] in transtp [gamma |- D1] [gamma |- D2] | [gamma |- dt_s F] => let [gamma |- D] = ctp [gamma |- F] in symtp [gamma |- D] ; %{{## Proof of Completeness for Terms}}% rec ceq: (gamma:deqCtx) [gamma |- deq T S] -> [gamma |- aeq T S] = fn e => case e of | [gamma |- #p.3] => [gamma |- #p.2] | [gamma |- de_r] => ref [gamma] [gamma |- _ ] | [gamma |- de_a F1 F2] => let [gamma |- D1] = ceq [gamma |- F1] in let [gamma |- D2] = ceq [gamma |- F2] in [gamma |- ae_a D1 D2] | [gamma |- de_l (\x.\u. F)] => let [gamma,b:block x:term,u:aeq x x , _t:deq x x |- D[..,b.1,b.2]] = ceq [gamma, b:block x:term, u:aeq x x , _t:deq x x |- F[..,b.1,b.3]] in [gamma |- ae_l (\x.\v. D)] | [gamma |- de_t F1 F2] => let [gamma |- D2] = ceq [gamma |- F2] in let [gamma |- D1] = ceq [gamma |- F1] in trans [gamma |- D1] [gamma |- D2] | [gamma |- de_s F] => let [gamma |- D] = ceq [gamma |- F] in sym [gamma |- D] | [gamma |- de_tl (\a.\u. F[..,a,u])] => let [gamma,b:block a:tp,u:atp a a , _t:dtp a a |- D[..,b.1,b.2]] = ceq [gamma, b:block a:tp, u: atp a a , _t:dtp a a |- F[..,b.1,b.3]] in [gamma |- ae_tl (\x.\v. D)] | [gamma |- de_ta F1 P2] => let [gamma |- Q2] = ctp [gamma |- P2] in let [gamma |- D1] = ceq [gamma |- F1] in [gamma |- ae_ta D1 Q2] ;