Towards a formal theory of program construction
Christoph Kreitz
Abstract Principles of a unified formal theory on reasoning about programs and program con- struction built on top of Intuitionistic Type Theory.
1
Towards a formal theory of program construction Christoph Kreitz - - PDF document
Towards a formal theory of program construction Christoph Kreitz Abstract Principles of a unified formal theory on reasoning about programs and program con- struction built on top of Intuitionistic Type Theory. 1 OVERVIEW 1. Motivation and
Abstract Principles of a unified formal theory on reasoning about programs and program con- struction built on top of Intuitionistic Type Theory.
1
“Declare x of type T”
“The term t belongs to type T”
“T is inhabited”
“Under the assumptions xi : T i we can prove that conclusion C is inhabited” (by some yet unknown member m)
“Start with an empty hypothesis list”
“H ⊢ C is provable if the subgoals Hi ⊢ Ci can be proven” If proofs of the subgoals yield witnesses mi for Ci being inhabited then a witness m ∈ C for the main goal is constructed by the rule.
atomic predicates
if a = a′, (no members otherwise )
if i, j ∈ int, i < j, (no members otherwise)
if a ∈ A, b ∈ B
if a ∈ A, b ∈ B
if b ∈ B
if a ∈ A, l ∈ A list
if a ∈ A, b ∈ B[a/x]
if b ∈ B[x]
if a ∈ A, B[a/x]
if a ∈ A (Equality changed: a = a′ iff B[a, a′/x, y] )
if a ∈ T[λx.rec(z, x.T; x), A/z, x]
(partial functions from A to B )
TYPES
FORMULAE
FORMULAE(T)
DTYPES
DFORMULAE
DFORMULAE(T)
“Guess some hopefully correct output g or at least partial information about the
chosen from among the subsets of the conjuncts in OC(i, g). It must be possible to compute some g with dom(i, g).”
⊢ ∀IN,OUT,A:TYPES.∀IC:FORMULAE(IN).∀OC:FORMULAE(IN#OUT). ∀dom:FORMULAE(IN#A).∀t:FORMULAE(A#OUT). ∀i:IN. IC(i) ⇒ ∃y:OUT. OC(i,y) ⇐ ∀i:IN. IC(i) ⇒ ∃g:A. dom(i,g) & ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ( t(g,y) | ¬t(g,y))
Extracted Algorithm: Let P1, P 2 be algorithms extracted from proofs of the subgoals, i.e. P 1 computes for i ∈ IN with IC(i) some g ∈ A with dom(i, g) and P 2calculates for appropriate i, g some y ∈ OUT with OC(i, y)&(t(g, y)|¬t(g, y)). Then for all i ∈ IN return P2(i, P1(i)).
⊢ ∀IN,OUT,A:TYPES.∀IC:FORMULAE(IN).∀OC:FORMULAE(IN#OUT). ∀dom:FORMULAE(IN#A).∀t:FORMULAE(A#OUT). ∀i:IN. IC(i) ⇒ ∃y:OUT. OC(i,y) ⇐ ∀i:IN. IC(i) ⇒ ∃g:A. dom(i,g) & ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ( t(g,y) | ¬t(g,y))
Move all assumptions to the hypothesis list 1.-3. IN,OUT,A:TYPES 4. IC:FORMULAE(IN) 5. OC:FORMULAE(IN#OUT) 6. dom:FORMULAE(IN#A) 7. t:FORMULAE(A#OUT) 8. ∀i:IN. IC(i) ⇒ ∃g:A. dom(i,g) 9. ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ( t(g,y) | ¬t(g,y)) 10. i:IN 11. IC(i) ⊢ ∃y:OUT. OC(i,y) Instantiate 8. on i (we show new hypotheses only) 12. ∃g:A. dom(i,g) ⊢ ∃y:OUT. OC(i,y) Eliminate 12. (give the existing object a name) 13. g:A 14. dom(i,g) ⊢ ∃y:OUT. OC(i,y) Instantiate 9. on i, g, eliminate the result 15. ∃y:OUT. OC(i,y) & ( t(g,y) | ¬t(g,y)) 16. y:OUT 17. OC(i,y) 18. ( t(g,y) | ¬t(g,y)) ⊢ ∃y:OUT. OC(i,y) Choose the y in hypothesis 16 as solution.
⊢ ∀IN,OUT,A:TYPES.∀IC:FORMULAE(IN).∀OC:FORMULAE(IN#OUT). ∀dom:FORMULAE(IN#A).∀t:FORMULAE(A#OUT). ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ( t(g,y) | ¬t(g,y) ) ⇐ ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & t(g,y) | ∃y:OUT. OC(i,y) & ¬t(g,y)
⊢ ∀ A,B,p:Umax. ( A | B ) ⇐ (p | ¬p) & (p ⇒ A & ¬p ⇒ B ) Extracted algorithm: Let P 1 decide p|¬p, P 2 compute a proof for A from one for p, and P 3 prove B from ¬p. If P 1 decides for p return P2(p1) as a witness for A. Return P3(p1) proving B, otherwise.
⊢ ∀IN,OUT,A:TYPES. ∀IC:FORMULAE(IN).∀OC:FORMULAE(IN#OUT). ∀p, dom:FORMULAE(IN#A).∀t:FORMULAE(A#OUT). ∀i:IN.∀g:A. dom(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & t(g,y) |∃y:OUT. OC(i,y) & ¬t(g,y) ⇐ ∀i:IN.∀g:A. dom(i,g) ⇒ ( p(i,g) | ¬p(i,g) ) & ∀i:IN.∀g:A. dom(i,g) ⇒ p(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & t(g,y) & ∀i:IN.∀g:A. dom(i,g) ⇒ ¬p(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ¬t(g,y)
ByA STRUCT denote a recursive type of structures generalizing Graphs, Trees, Sets, Lists, .. over A, base be a zero objectA STRUCT (e.g. the empty graph) and S \x result the structured object S reduced by x ∈ A (e.g. a node is taken out of some graph).
⊢ ∀A:TYPES.∀P, pre:FORMULAE(A STRUCT # A). ∀i:A Struct.∀g:A. pre(i,g) ⇒ P(i,g) ⇐ ∀i:A Struct.∀g:A. pre(i,g) ⇒ P(base,g) & ∀i:A Struct.∀g:A. pre(i,g) ⇒ (P(i\g,g) ⇒ P(i,g)) Extracted Algorithm: Let P 1 compute a solution for the base case, P 2 one for i from a solution for i\g. Compute a solution for i by recursively applying P 2 to previous results until the base case P 1 is reached.
⊢ ∀OUT:TYPES.∀IC:FORMULAE(OUT STRUCT).∀OC:FORMULAE(OUT STRUCT#OUT). ∀dom:SUB-FORMULAE(OC,OUT STRUCT#OUT). ∀i:OUT STRUCT.∀g:OUT. dom(i,g) ⇒ ¬p(i,g) ⇒ IC(i) ⇒ ∃y:OUT. OC(i,y) & ¬g=y ⇐ ∀i:OUT STRUCT.∀g:OUT. dom(i,g) ⇒ ¬p(i,g) ⇒ IC(base) ⇒ ∃y:OUT. OC(base,y) & ¬g=y & ∀i:OUT STRUCT.∀g:OUT. dom(i,g) ⇒ ¬p(i,g) ⇒ (IC(i) ⇒ ∃y:OUT. OC(i,y) & ¬g=y ⇐ IC(i\g) ⇒ ∃y:OUT. OC(i\g,y) & ¬g=y ) with p := OC\dom in T f subformula of F in T
SUB-FORMULAE(F,T)
F\f in T
SPECIFICATIONS
# FORMULAE (IN) # FORMULAE (IN#OUT) PROGRAMS
FULFILLS (spec, program)
& OUT(spec) = OUT(program) in TYPES & ∀x:IN(spec). IC(spec)(x) ⇒ IOR(spec)(x, body(program)(x)) SOLUTIONS(spec)
SOLVABLE(spec)
SYNTHSPEC
λp,spec.FULFILLS(spec,p)> SYNTHESIZERS
|∀spec:SPECIFICATIONS. problem-class(spec) ⇒ FULFILLS(spec,synth(spec))}
M-PROGRAMS
M-FULFILLS (spec,p)
& OUT(spec) = OUT(p) in TYPES &∀x:IN(spec). IC(spec)(x) ⇒ body(p)(x) = {y:OUT(spec)|IOR(spec)(x,y)} M-SOLUTIONS(spec)
M-SOLVABLE(spec)
⊢ ∀spec: SPECIFICATIONS. ∀x:IN(spec).∃y:OUT(spec). IC(spec)(x) ⇒ IOR(spec)(x,y) ⇔ SOLVABLE( spec ) ⊢ ∀spec: SPECIFICATIONS. ∀x:IN(spec).∃o:P(OUT(spec)). IC(spec)(x) ⇒ o={y:OUT(spec)|IOR(spec)(x,y)} ⇔ M-SOLVABLE( spec ) By applying these theorems one may switch representations.
∃p:M-PROGRAMS.∀x:IN(p). IC(x) ⇒ body(p)(x)={y:OUT(p)|IOR(x,y)}
⊢ ∀IN,OUT:TYPES.∀IC:FORMULAE(IN).∀ior,ior’:FORMULAE(IN#OUT). ∃p:M-PROGRAMS.∀x:IN(p). IC(x) ⇒ body(p)(x)={y:OUT(p)|ior(x,y)} ⇐ ∃p:M-PROGRAMS.∀x:IN(p). IC(x) ⇒ body(p)(x)={y:OUT(p)|ior’(x,y)} & ∀x:IN.∀y:OUT. IC(x) ⇒ (ior’(x,y) ⇔ ior(x,y) ) Still too much context hiding the true effect. Formalize the essence of the theorem.
T is a transformation
IN(T(s))=IN(s) in TYPES & OUT(T(s))=OUT(s) in TYPES & IC(T(s))=IC(s) in TYPES TRANSFORMATIONS
t is a transformation} T correctness preserving
IC(s)(x) ⇒ (IOR(s)(x,y) ⇐ IOR(T(s))(x,y)) T equivalence preserving
IC(s)(x) ⇒ (IOR(s)(x,y) ⇔ IOR(T(s))(x,y)) C-TRANSFORMATIONS
T correctness preserving} EQ-TRANSFORMATIONS
T equivalence preserving}
⊢ ∀T:C-TRANSFORMATIONS.∀specification:SPECIFICATIONS. SOLVABLE(specification) ⇐ SOLVABLE(T(specification)) ⊢ ∀T:EQ-TRANSFORMATIONS.∀specification:SPECIFICATIONS. SOLVABLE(specification) ⇔ SOLVABLE( T(specification) & M-SOLVABLE(specification) ⇔ M-SOLVABLE(T(specification)) For each transformation these meta-theorems return verified deduction rules for program synthesis based on applying T.
T guess(A,dom,t)(<IN,OUT,IC,IOR>) ⇔ <IN, OUT, IC, λi,y.∀g:A. dom(i,g) ⇒ IOR(i,y) & (t(g,y)|¬t(g,y))>
⊢ ∀spec:SPECIFICATIONS.∀A:TYPES.∀dom:FORMULAE(IN(spec)#A). SOLVABLE(<IN(spec),A,IC(spec),dom>) ⇒ ∀t:FORMULAE(A#OUT(spec)). T guess(A,dom,t) in C-TRANSFORMATIONS & ∀t:DFORMULAE(A#OUT(spec)). T guess(A,dom,t) in EQ-TRANSFORMATIONS The previous version is a corollary of this theorem and the above justification theorems:
(metatheorem) for each C-/EQ-Transformation.
SOLVABLE predicate and the AE form.
Open Question: How to hide as much formalism as possible (using NuPRL’s defini- tion facility) to have a formal theory (with mechanized proofs) intellegible for humans (not just NuPRL-Experts).