HQL’s language specification

H. Hern´n Moraldo a http://www.hhm.com.ar/ mg@moraldo.com.ar June 2005


About HQL

HQL stands for ‘HHM’s Quantified Lambda’ and it’s a small programming language I created in 2004, for an Advanced Functional Programming class I had with professor Pablo E. Martinez Lopez. It’s a very simple functional language, whose expressions can involve the use of quantifier operators (for all, exists, etc). The following document presents the language formal specification, that is, the type inference for the language, and its semantics. It’s the copy of the original hand-written specification, so it might be incomplete. Visit http://www.hhm.com.ar/ to download the interpreter (with no parser) source code, and for any questions and comments, send mail to mg@moraldo.com.ar


A sample program

We want to have an example of the use of HQL, that shows what kind of language it is, and what kind of programs can be written with it. Next code, shows a program written in HQL’s abstract syntax (as there is no parser yet, you might have to convert it to Haskell data before executing it with the interpreter). program = ∀x ∈ Bool . (∀y ∈ Bool . ((¬ (x ∨ y)) ↔ (x → y))) To make this program a real HQL program, all ∀ have to be converted to ∃ form, and binary operators like ↔, ∨ and ¬ have to be converted either to BBBOp form, or to applications of functions as defined in the library (library


is a non empty initial environment, as the one used by the interpreter). The full converted program would be: program = ∀x ∈ Bool .∀y ∈ Bool . app (app iif (app not (app (app or x ) y))) (app (app if x ) y) Where code like app orx is applying the function contained in variable or to the expression x. The same program is presented in Haskell syntax as follows: xTerm6=(tForAllB "x" (tForAllB "y" (tIif (tOr (tNot (TVar "x")) (TVar "y")) (tLogicIf (TVar "x") (TVar "y")) ) ) ) Where functions like tF orAllB and tIif have been previously defined to make programming much faster. It’s interesting to ask what type has this program. According to the type inference algorithm, it’s a Bool. The program semantics is in this case T rue, as the program is describing a truth that does not depend on any external variables (excepting those of the library).


HQL’s language specification
Type inference
Definitions: T ype = Bool | Int | T ype → T ype e := v | ci | cb | app e e | lam t v e | let v = e in e| explicitLet v = e ∈ e in e| ∃v ∈ t.e|¬e| if e e e


Base: v:s∈Γ Γ v:s 2



Γ 3.1.4 Booleans:

ci : Int

Γ 3.1.5 Application: Γ

cb : Bool

e1 : t1 → t2 Γ e2 : t1 Γ app e1 e2 : t2


Lambda: Γv, v : t1 e : t2 lam v ∈ t1 .e : t1 → t2

Γ 3.1.7 Let .. in:

Γ e1 : t1 Γv, v : t1 e2 : t2 Γ let v = e1 in e2 : t2 3.1.8 Explicit let .. in Γ e1 : t Γv, v : t e2 : t2 explicitLet v = e1 ∈ t in e2 : t2

Γ 3.1.9

If .. then .. else: Γ e1 : Bool Γ e2 : t Γ Γ if thenelse e1 e2 e3 : t e3 : t



Exists: Γv, v : ht e : Bool Γ ∃v ∈ t.e : Bool

h would require some extra detail to be thrown in this specification. For now, let’s just say that the t in ∃v ∈ t.e is a special type, that can be bool or a restricted integer, so h is a function that converts these special types into the ones accepted by the system. 3.1.11 Not: Γ Γ 3.1.12 e : Bool not e : Bool

IIIOp (others operators are similar): Γ e1 : Int Γ e2 : Int Γ IIIOp s e1 e2 : Int



Environments are sets of triples like this: (β, v, t) , with v being a variable name, t the expression associated to v (its value), and β the environment where v is defined. 3.2.2 Big K definition: K (α, v, e) = (α + K (α, v, e) , v, e) 3.2.3 Integer constants: [[ci ]]α := ci 3.2.4 Boolean constants: [[cb ]]α := cb 4


Variables: [[v]]α := [[t]]β , if (β, v, t) ∈ α


Lambdas: [[lam v ∈ s.e]]α := lam v e α


Let .. in: [[let v e1 e2 ]]α := [[e2 ]]α+K(α,v,e1 )


App: [[app e1 e2 ]]α := [[e3 ]]β+K(α,v,e2 ) if [[e1 ]]α = lam v e3 β


If .. then .. else:
  [[e2 ]]α , if [[e1 ]]α = true   

[[if thenelse e1 e2 e3 ]]α :=

[[e3 ]]α , otherwise


Not: [[not e]]α := ¬ [[e]]α


Exists in booleans: [[∃v ∈ Bool.e]]α := [[e]]α+(α,v,T rue) ∨ [[e]]α+(α,v,F alse)


Exists in Range of Integers:
  [[e]]α+(α,v,i1 ) ∨ [[∃v ∈ [i1 + 1..i2 ].e]]α , if e1 ≤ e2   

[[∃v ∈ [i1 ..i2 ].e]]α :=

F alse, otherwise



  [[n1 ]]α + [[n2 ]]α , if id = ’+’      

[[IIIOp id n1 n2 ]]α :=

[[n1 ]]α ∗ [[n2 ]]α , if id = ’∗’ et cetera

      



Reynolds, J. C. (1998). Theories of Programming Languages. Cambridge University Press. New York.


Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.