Normalization by evaluation for typed lambda calculus with coproducts T. Altenkirch
P. Dybjer
M. Hofmann
P. Scott
April 10, 2001 Abstract We solve the decision problem for simply typed lambda calculus with strong binary sums, equivalently the word problem for free cartesian closed categories with binary coproducts. Our method is based on the semantical technique known as “normalization by evaluation” and involves inverting the interpretation of the syntax into a suitable sheaf model and from this extracting appropriate unique normal forms. There is no rewriting theory involved, and the proof is completely constructive, allowing program extraction from the proof.
1 Introduction
terms in and thereby extracting certain unique syntactic normal forms, from which a decision procedure easily follows (we outline the proof below). The proof uses no rewriting theory. Typed lambda calculi with (strong) sum types arise very naturally:
In programming language theory, coproducts model
variant and enumerative types. The added categorical equation for coproducts corresponds to a kind of uniqueness for pattern matching or Case construction [AC98, Mit96, GLT89]. In proof theory, under the Curry-Howard Isomorphism, terms correspond to natural deduction proofs in intuitionistic propositional logic. One then considers terms (proofs) modulo certain equations, which guarantee, for example, that the formula acts as a coproduct type (with copairing), as well as including the theory of commutative conversions (cf [GLT89], pp 80-81). In category theoretic terminology, such lambda theories correspond exactly to almost bicartesian closed categories, that is, cartesian closed categories with nonempty finite coproducts (generated by a set of atomic types) [LS86]. As proved by Dougherty and Subrahmanyam [DS95], a Friedman completeness theorem in Set holds for cartesian closed categories with binary coproducts. Therefore, the equality we decide is the natural extensional equality on proofs in intuitionistic propositional logic and on terms of the typed lambda calculus with sums.
In this paper we solve the decision problem for simply typed lambda calculus with categorical coproduct (strong disjoint sum) types. While this calculus is both natural and simple, the decision problem is a long-standing thorny issue in the subject. Our solution is based on normalization by evaluation (NBE) (also called “reductionfree normalisation”) introduced by Martin-L¨of [ML75] for weak typed lambda calculus, and by Berger and Schwichtenberg [BS91] for typed lambda calculus with conversion. The technique has been further refined by the authors and coworkers using category-theoretic meth ods [CD97, AHS95, CDS97]. It has also been extended to other systems, such as System F [AHS96]. As shown by Berger, Schwichtenberg, and Danvy [BS91, Da96], NBE techniques yield fast normalization algorithms, with applications in interactive proof systems [BBSSZ98] and type-directed partial evaluation [Da96, Da98, Fil01]. Here we show how to considerably extend the NBE techniques to take into account type systems with strong Much of traditional lambda calculus theory carries sums. The NBE method involves constructing a model through unscathed when we add products (and even weak and effectively “inverting” the evaluation of lambda School of Computer Science and IT, University of Nottingham, Not- categorical data types) to the simply typed case. Unfortunately, the addition of coproducts is considerably more tingham, UK, e-mail:
[email protected] subtle. The difficulties with adding coproducts are deDepartment of Computing Science, Chalmers University tailed in [Do93, DS95]: for example, the analog of Statof Technology, S-412 96 G¨oteborg, Sweden, e-mail: pe
[email protected] man’s 1-Section theorem fails in the presence of coprodDivision of Informatics (LFCS), University of Edinburgh, Edin- ucts, confluence (of various standard rewriting presentaburgh EH9 3JZ, UK, e-mail:
[email protected] tions) fails, and the proof of Friedman’s completeness theDepartment of Mathematics, University of Ottawa, 585 King Edorem for the case of coproducts uses difficult and involved ward, Ottawa, Ontario, K1N 6N5, Canada. Phone: 613-562-5800, ext. 3502. FAX: 613-562-5776. e-mail:
[email protected] syntactical arguments [DS95].
A decision procedure for cartesian closed categories with binary coproducts has been presented in Ghani’s thesis [Gh95a] (see [Gh95b] for a summary) although the proof involves intricate rewriting techniques whose details are daunting. Our method described here is quite different and we believe conceptually simpler. An algorithm for type-directed partial evaluation for a call-by-value typed lambda calculus with sums has been given by Danvy [Da96, Da98] and Filinski [Fil01]. This algorithm uses continuations and is therefore also quite different from ours. In particular, it does not decide equality in cartesian closed categories with binary coproducts. Like Ghani and Dougherty and Subrahmamyam, we only consider the case of finite non-empty coproducts, that is, an initial object (empty type) is not part of the structure. We conjecture that the present approach can be extended to full bicartesian closed categories including initial objects. However, this complicates the structure of our normal forms, and we have not yet completely checked that all properties hold for the extended language. Outline of Proof Let be a lambda theory. Our aim is to decide if
"!$#&%('#)+*
, #-%
#)
that is, if two possibly open terms and of type are equal wrt , where is a type environment . We associate # #&2 with each term a normal form .0/01 . In this paper, these normal forms are not themselves terms, but there is a function 3 mapping normal forms to terms in such a way that the following two properties hold (cf. [CD97, CDS97]): NF1 NF2
"!
#&2627'# 43 15.0/01 "!$#&%('8#)
9. /01 #?%@'A#) implies
#:%;27'
.0/01
#)02
.
#:%B2'
This implies that if and only if .9/01 #)02 .9/01 , so that comparing normal forms will yield a decision procedure for . When = the typed lambda calculus with conversion, the authors and coworkers showed in C AHS95 CDS97D how to obtain a function .9/ by inverting the presheaf interpretation C C of . One defines * 2 EGF (2 D DIH JLK>1M and two * natural transformations C C 2 N F JLOP1M H (D D , where J K>1M is the presheaf of 2 normal forms and JLOP1M is the presheaf of neutral terms #) simultaneously 1 T
A type environment is a finite _function #k* from variables # meaning has to types. The typing judgement
R g w2 "£ *:p e dom 1 "£ R* RV2 ¢ " :* p 1 e "¢V yf{x*
type in type environment is defined in the obvious way. For example, the rule for Case is:
R4* wI#*
2 M % I#,* % 1 @h j hB I~ R # 2 R"% #?%B2c#*- 1 hT h 1 T
¢V ¡ * h h ¢" y¡ ¡ h V"£P¡* ih,su V"£ª|4 ¡62(* 1 ¢V ¡* ¢" } ¡62x* 1 h j V"£ * e Ar V"£
l [ ' [ *
Definition 2.1 Equality between terms is the least (typed) congruence generated by the following rules (omitting types to improve readability): 2 1 2 1
Proj SP Unit In Coprod Distrib
vR # 25#?%('# C #?%B&R h Rg # 1 'T vZh R #cR + D #2 if T 1 |V yM# #&%B{627'# 1 : h #+'ty| #&2 |=% #&26{ #S'^yz{ h1 1 ~ R # 2 R"% #?%;2 } #)02'8#9 C #)R4 D ~ 1 R hT } h R 1 22 T R % } % 1 R % 2m2w#'8# 1 T 1 1 T 1 h h h # ~ R # 2 R % # % 2# ) 27' ~ 1 R 1 h #wT # h 2 1 R % T ## % 2c# ) R 1 h R T % gu h 1 #&T 2 1 if h
We will refer to this equational theory as BiCCC. The key categorical axiom (Coprod) is dual to (SP) and guarantees uniqueness of the co-pairing arrow out of a coproduct. BiCCC entails all the usual commutative conversions for sums, [GLT89], pp. 80-81. It can be shown (cf. [LS86, CDS97]) that the free almost bicartesian closed category h over one base object p can be obtained as the category whose objects are type 'R % * environments from % R4* and where%a* morphism % U * U BT0TBT= #?% to #U2 0TBTBT=m is a sequence of terms 1 BTBT0T , modulo BiCCC equality, vZR ¡x* T Ar
where Rgu in the 2 last rule we g have the¡62 variable condition that 1ze for each e ¤S¥Z¦:§¨Z©B1 . We have two rules for forming normal forms:
V ¢" k¡* ¡62'A® " ¡* and ¤S¥Z¦?§¨©91 ' % U (b) Let ¯
e 0TBT0T;e be a nonempty finite set of neutral terms (so we assume the e are pairwise dis* tinct). For each ° ¯± the abbreviw³'8 R"%&* ³:% ´nµ H² ¬BRVX: U=*weU³:use ation 0TBT0TB ´nµW· : dW¶ ¶ % 2 M% U- "£ * 1 ³ "e ¡ ³ h *-j
2 ³&¹ ºk» o¸o%¸o¸ 1 hB V" k¼ R"%¾0¾B¾mR4U ¡6³-2m³-2* 1z¯½01 T (a)
and
¼ R"%¾B¾0¾6R4U ¡6³26³G262' ¤S¥Z¦?§¨©01 1z¯½91 T ¯ ¡ ³ 2 ³¹ ºk» %m where 1 h0 is a family of normal forms
satisfying the following two side conditions: Variable-condition: for each e R=% RVU have 0TBTBT0 ¿ 1ze
g ¡³-2 we 2'A À ¤S® ¥Z¦:§¨©91 .
¡m³26³
Redundancy-freeness: g The family 1 ¡ 2 is not re ³ ³ ¯ , where * 1 dundant at any e is redun à ¯ @ Â
e lH dant at e whenever for all Á ¡WÄ9Å µ5ÆzÇ» ¡WÄ9Å µ5ÆMÇ»%
9¬ZBX: hmÈ and R È are equal and neither contains the variable .
R=%
R4U
¼
The variables become bound in the BT0TBTB construct. For brevity we shall often use the alterna¼ ¡6ɳ 2m³G2 ¡6ɳ tive notationR 1z¯½91 R ¡ , where the range over % U ³ T . abstractions BT0TBT;
¼
The idea is that performs a simultaneous case split category of constrained environments Û , where the mor¡ ³ Å µ6»Ç over all the “guards”. For example, phisms will be injective renamings. The constraints are of hmÈ corresponds RV2 ' } RVf2 to a branch to be taken when e is of the form } h1 . the form e and express which branch a certain 1 guard e takes. This is the idea behind our Grothendieck Example 3.1 The following examples show how the topology on Û : a “covering” expresses case-splitting. side-conditions ensure uniqueness of normal forms as This use of Grothendieck topologies is closely related to computed by .0/ in Section 1.1. Let for simplicity the vari[FS99] where they were used for proving a definability ables Ê (possibly with indices) in the examples below have p result for a language with coproducts. type , so that they are normal terms. Definition 4.1 A neutral constrained environment, envivZË ~ R% 2 R"% %B2 Pq5Ü 1. The normal form of T 1 T 9 Ê h 1 T Ê will be ronment for short, is a pair where is a type' environ¼ R"% ¡6³G26³2 ¡Å Ì Ç»
'vZË Ü }h 1R h 2 1W 9V-01 T vË where constraints of the form e ¼ R=% È ¡6³-2m³G2 T Ê . Note ¡ÍÅ Ì Ç»+ that ' ment ' and} % R is% 2 a set of ½ * R * V £ % the expression , where T 1W 9401 T È 1 % where e h j and h h or e R * % Ê , violates ) is contained in and moreover, v the side condition for (pure) normal (resp. forms of -form. no two distinct constraints involve the same neutral Ü ' R 2 2. The normal form of the term term, for example, cannot contain e } h-1 h and
~ =R % 1 R % 1 %
will be
~ R4) T 1 T Ê9hh ~ R ) % T 1 TÊ h
2 4R ) %;2 1 T Ê0h 2 R ) % % 2 1 TÊ
)92
)2
¼
e
' } % "R %02 1
no two distinct constraints refer to the same variable, Ü ' } R 2 É ' for example, cannot contain e h-1 h and e } h1 R h 2 unless e and e É are identical. qÞ Pq6Ü A morphism from environment Ý * to environment 2 2 is given by an injective function ß dom 1 RV2m2'à RV2 2' } H RV2m2 dom 1fÝ Þ 1Mß1 satisfying Ý1zß1 is in ' RV1 2 and Ü ß1fe for each constraint e } 1 in . In this way the environments form a category Û in which composition is composition of functions. Þ Ü * If Ý extends and extends then the inclusion ß w2Pá 2 qÞ dom 1 H dom 1zÝ defines a morphism from Ý to Iq5Ü
% ) R"%ÍR4) ¡6³26³2 15 0 6 -01 T ¡ Å Ì Ç»
ÌÎ Ç»Ï ' ÐÏ where ¼ ³ ³ Î that 26³ Î 26³ the2m2 expresd % R"% ¼ È ) Ê R4. ) ¡6Note sion 15 0 -01 T 1W 9 01 T dmÑ d is not a normal form since it violates the variable-condition: R=% ) ¼ is) notRVfree ) ¡6³ in³ Î the 26³ Î 2 guard of the normal form . 1W 9 01 T dmÑ ~ R 2 R 2 which we call a projection. 3. The normal form of 1 T Ê 1 T Ê will be Ê . Note ¼ R 2³ 2 R 2³ We are interested in studying equality of terms relative that 1W 9401 T Ê is not a normal form as 1 T Ê is to a neutral constrained environment. The following defiredundant at . nition is due to [DS95]. ~ 2 2 4. Note however, that the normal form of 1MÊ4T Ê 1zÊZT Ê ã Pq6Ü ¼ 2m³?2 Definition 4.2 Let be an environment and â be a list will be 15 0V-91MÊ4T Ê which is not redundant at Ü because of the variable condition in the definition of redundancy.
Ò¡@*
Definition 3.2 The function 3 mapping ÓÔg ¡* Ö7Õ 1z Yxäå
2 ' ¡Ùq V"£ * Õ×@1z Yxäå
e
2 ' w2 dom 1 1z Yxäå 2 ' ¡ÙqPq6Ü P¡* 0§1z Yxäå
¡ ¡ É Iq5Ü_ª¡'¡ É * where * qÞstands for Pq6Ü Iq5Ü "£ . ¡* Ý H then If ß and 2 ¡62Lg 2 Õ@×1M ù 1 R Õ@ס 1M û äR"ü 2 is defined by replacing each free variable in by ß1 . The morphism parts 0§ ù and Ö7Õ ù are defined analogously. RLg
2
2 R"27' RV2 ß1 . If ¡g 1M Y(ä2 å then ¡61M2 ù 1 Õ Yxäå 1z then R Õ ù ¡ 1 is defined If by first reR"2 placing each free variable in by ß1 and then plugÞ
ging in all the constraints mentioned in by repeatedly performing the following atomic restriction operation (an analogous operation appears in Ghani’s thesis [Gh95a] under the name “first and second residue”). . ¡+g
2 lV"£ * Definition 5.4 Let Õ 1 Yxäå and % ¡ C *o' } R4ñ2 e i¡ h j 1 D of to R4. Then * q Ü we' define RVf2 the restriction e e } 1 (along the projections) as follows.
3 defines natural transformations 2 2 2 2 M1 2 H 0§2 1M , Ö2 Õ 1M H 2 B§ 1z , Õ×i1M H B§ 1M , 1M H 0§u1M . * 2 +1zÝI is a morphism in the free BiCCC , If ° that is, a sequence of terms in type environment Ý , then C¡ C ¡ 2ì* DH ° 2 D defines awnatural transformation[ 0§1z° 2 2 B§ u1zÝ H B§ u1 . This makes B§ 1 a functor from to Û í preserving and cartesian products.
2 Proposition 5.6 The presheaf 0§1 is a sheaf. Proposition 5.7 Let m be types. There is a natural Proposition 5.5
Õ
transformation
v *
2 2m2 2 1M rÕ 1z HrÕ 1zAr ð 1 v When to a normal form ¼ R=% applying RVU ¡6³26³G2 ð , depending naturally on a variable R 1f¯½01 TBT0T T of type , we divide ¯ into two sets ¯ R h , which % contains the guards which do not depend on , and ¯ , which contains the guards which do. Then we return
"!$#&%')(+*-, ,',.(0/ , 12(+, 43 56"!7*'%8)(-*-,', ,9(0/ , :9; ; = ; .= = ; = a d a < d d a
Compare also example 1 in 3.1.
Proposition 5.8 The presheaf Õ 1M isomorphic to the sheafification * þV1z2 Ö7Õ 7 Ö Õ 1z H with the embedding ¡627'¡ . Yäå 1
à
* ¡ e g h j Õ 1M ~ R ¡ 2 R"% ¡m%B2 ð 1 hT h 1 T ò0> e
If forms
2
is am2 2 sheaf and is2 1z of2 ÖÕ 1M Õ 1M given by
% of two normal 2 ,Æf¹ then Æ µWthe êwë Æf´ pasting Æ Yg é F ä å42 é ¶ is the normal form Õ 1M x Y äå . 2 1\Û for the full subcategory of Û í con-
Let us write sisting of the sheaves. We know from Prop. 5.3 that ò?> 2 1 Û is a BiCCC. Since the category h of sequences of types and termsC C [ is a free BiCCC there is a unique inter* ò?> 2 1 Û , determined by pretation functor D D h H
¡ C Ð* ' } R"2 ' ¡ gÀ ¡62 C C p ' p?2 e 1 D ¤S¥Z¦?§¨©B1 , if e DD Õ 1 ¼ ¡ ³ 2 ³ 2 C *Ð' } R 2 ' ¼ ¡WÄ9Å µ5Ç»
2 Ä 2 1z¯²Ø e:-91 e 1 D È ð 1z¯½91 Concretely, this functor ò0> is 2 given by defining a canonical ¼ BiCCC structure on 1\Û . where ð computes a normal form to be defined be¼ ¼ low. Note that we cannot simply replace ð by be6 Inverting the interpretation function cause the set of guards can become empty upon plug-
ging in a constraint, new redundancies may be created, We will now define natural transformations * CC 2 and the may E F * (D D=Hr2 Õ C1MC ¼ variable ® ¡ 2 conditions ¡ ¼ be violated. ¡ ³ 2 ³ We 2 deN fine F Õ×@1M H (D D ~ R ð ¼ 1 ; ¡ to³ Å µ5be 2m2 R % 1f¯¼ Ø^ e:-¡ ³91Å µ5Ç»% 2m2m2 to be Ç» 2 ³ and È e. P#* ð 1 h T ð 1f¯½01 ~ hmR È ¡ 2 1 R % T ¡ % 2 ð 1z¯½91 , in such a way that for a term 1 T 1 T e To compute we first check h h ð ¡5 RV C C #2 ' # 26262 whether depend on and are different (see the .9/01 î0ïñð E YF 1 D DW1 N YY 15X Y ¡ ' ¡m%B2 definition of redundancy). If not, we return h 1 , or I#' #&2 ¼ % ¡Ä 2 will satisfy .9/01 (NF1): otherwise, we return 1W &e:Øu¯_h7Øu¯ , where
¯ for «
'
c'
&e
g
¤S¥Z¦:§¨©91 ¡Ä
¬ZBX , and the family
¡5f2Bq R4g
1ze
ñ2
is adjusted accordingly.
p?2 E2@ * Õ 1 p?2 r H Õ 1 is the identity function. p?2 ?p 2 N@ * @ Õ ×1 H Õ 1 is the injection from neutral terms to normal terms given by the obvious termformation rules.
EBA * tH
2 1f yf{ is the constant function returning It follows by straightforward calculations that all these are the normal form . indeed natural transformations. N A * Õ×@1ñ 2 Hú is the constant function return- Proposition 6.1 In order to establish NF1, that is, # ' CC # 26262 t#l* yz{@g for a family of we define . (As before we use the same 3V1ME F 1 D D51 N 15X0Y ing the element CC 2 subsheaves , such that S ( D D V s B
§ z 1 F ( Y ä å signs for corresponding syntactic and semantic noYZä å YZä åUT C C g I , # * tions.) (D D Yäå and : (i) For all þ E F aDC F d ' E ¦GFn§ o 1ME F a s^E F d 2 where E ¦GFn§ * ð # Pq6Ü 262'# 2 %02 ð %02 F äå F äå 1Mþ þBS Y 341zE Yx Õ 1Mih sLÕ E1M HÃ ¡m% Õ ¡5)021zi' h$ysL ¡m% ¡5)0{ is the unique map satisfying ¦GFn§ ð 1 for pure norg 2 ¡% ¡5) Õ@×@1z Yxäå mal forms . This map exists by Proposition 5.8 (ii) For all e 2 2 and the fact that þ preserves products. N Yx F äå 1ze S Yx F äå 3V1ze 7 2 ^ ' y | 6 2 2 | m 2 6 2 { % N F=a C Fcd 1ze N F=a 1 h 1fe N Fcd 1 1fe Yäå Yäå Yxäå type environments We' canR"% extend * % S R4to U * U [ Pq5Ü2 ' Let H g C C
D D Æ letting by B 0 T B T T Û^ í 1 Ût1 s YäMå % U2 Y % U2 CC CC 2 F iff z 1 þ B 0 T B T = T þ S z 1 ° B B T 0 T T ° þ S Yxäå YäMåN ° for iD D-
D D . Then XXW « WZY . Similarly, we can extend E and to type 2 ' v 2g 2 F IK E Y Õ 1MAr Yxäå äå J 1LH Yxð äå 1 Á environments as well. tl#