28
NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com [email protected] October 10, 2006 Copyright © 2006 Samuel Howse. All rights reserved.

NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com [email protected] October 10, 2006 Copyright

Embed Size (px)

Citation preview

Page 1: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NummSquareda new well-founded, functional foundation for formal methods

Samuel [email protected] 10, 2006

Copyright © 2006 Samuel Howse. All rights reserved.

Page 2: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Formal methods

• Logic and mathematics applied to computer science

• Prove that implementation is “correct”– Compliant with specification

• Incorrect specification – a separate issue

– Well-behaved• Terminates, i.e. does not run forever• Memory safety – easier to solve with automatic

memory management

Page 3: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Some programming paradigms

• Typical operating system – programmer view:– File system– Interprocess communication– Global state – indeterminate, insecure

• Imperative paradigm– Side-effects – memory can change unexpectedly

• Functional paradigm– No side-effects

• No global state + functional paradigm– More secure– Easier starting point for formal methods

Page 4: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Some problems in formal methods

• Assuming no global state + functional paradigm, ≥ 2 remaining problems:1. Specification

• Express specification precisely (math)• Express implementation behavior (math)• Prove imp. satisfies spec. (logic)

2. Termination – does a program run forever?• Turing: general answer cannot be computed• Prove special cases (math + logic)

Page 5: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

1. Specification

• Integrated approach is 1 language for:– Specification– Implementation– Proof

• 1 language must be:– A mathematical foundation– A programming language– A logic

• A typical programming language is too complex to be a mathematical foundation or a logic.

Page 6: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

2. Termination

• Does it matter? Just hit Ctrl-C (or Ctrl-Alt-Del and End Process).

• Non-termination is closely related to (but does not always imply) inconsistency, i.e. exists a proposition P such that P and not(P) both provable.

• Inconsistency is a critical bug in a logic.

Page 7: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Untyped lambda calculus

• Everything is a function.

• Everything can be used as an argument.

• Non-termination:– Let f(x) = x(x).– f(f) → f(f) → f(f) → …

Page 8: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Russell’s paradox

• Russell’s paradox: [Seldin]– In naïve set theory: Let R be the set of all sets that

do not contain themselves. Is R in R?– In untyped lambda calculus plus negation– Let R(x) = not(x(x)).– R(R) = not(R(R)).– R(R) is neither true nor false – inconsistency.

• Can avert paradox by a somehow non-classical logic (e.g. Andrews, Gilmore’s NaDSyL, Grue’s map theory).

Page 9: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Type theory

• Types can ensure termination and avoid paradoxes.• But more complex – 2 fundamental concepts:

– Type– Function

• Compile-time type checking is computable.– Benefit – catch some bugs early and automatically– Cost – additional constraints on programmer

• my research addresses by using runtime coercion instead of compile-time type checking

• Coq proof assistant– nice mix of computation and proof checking– practical tools, including extraction to OCaml– highly developed

Page 10: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Set theory

• Everything is a set.• Well-founded set theory

– Membership (ε) is well-founded, i.e. no infinite descending ε chains.

– A set is formed “after” its elements.

• Some well-founded set theories:– Zermelo Fraenkel (ZF)– von Neumann-Bernays-Gödel (NBG)

• Usually no reduction (computation)

Page 11: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Well-founded functions

• Everything is a function.• Well-founded

– A function is formed “after” elements of its domain and range. [Jones]

– Membership in field of function a well-founded relation

• Few foundations of this kind, and not popular• von Neumann in 1925

– Others changed functions to sets – became NBG• Jones’s Pure Functions in 1998• von Neumann and Jones – do not define

reduction (computation) or proof

Page 12: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NummSquared

• Everything a function, no types, no side-effects, no global state, well-founded

• Reduction (computation) and proof• Sound: the proposition of a valid proof is true• Termination ensured without proofs by programmer• Proofs as desired, but not required• Classical logic• Follows set theory as much as possible• Simple variable-free syntax• Reflection

– NummSquared is its own macro language.– NummSquared is used to manipulate NummSquared

proofs.

Page 13: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NummSquared coercion for domain membership• When calling a function, how to ensure that

the argument belongs to the function’s domain?

• NummSquared coercion: if it isn’t so, then make it so!

• Type conversion generalized to higher-order (function with function argument)

• Coerce a function to domain/codomain by applying pre-coercion/post-coercion.

• NummSquared coercion somewhat related to Howe’s restriction of untyped lambda terms.– Howe does not ensure termination.

Page 14: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: small function extensions

p<zero>= left(p)

p<one>= right(p)

zero one

pair p

p<null>= null

null

zero

zero<null>= null

null

one

one<null>= null

one<zero>= zero

zero

leaf

null

null (base case)

null<null>= null

rule f

f<x>

x

dom(f)small

(no largerthan a ZFC set)

null

simple

Page 15: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: domain extensions

• For a rule f, dom(f) is not, in general, amenable to coercion.

• So dom(f) represented by domain extension (tag):– Same information as a type in type theory– Different purpose – not compile-time type checking, but

runtime coercion– Domain extensions never appear directly in NummSquared

programs, but are available as small function extensions.

• Domain extension:– Constant: Null, Nuro (Null or Zero), Leaf, Tree– Combination:

• dependent sum A – dom(A) contains null and certain pairs• dependent product A – dom(A) contains null and certain rules

Page 16: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: coercion

• For a valid domain extension A and a tagged small function extension f, coercion to A of f, denoted by coer(A, f), is in dom(A).

• If A is a dependent sum, and f is a pair, coerce left(f) first, then right(f).

• If A is a dependent product, and f is a rule, add pre-coercion and post-coercion to f:– A contains domain extension family F.– coer(A, f) is the rule r where dom(r) = dom(F) and

r<x> = coer(F<x>, f<coer(domExt(f), tagged(F, x))>)

Page 17: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: result

• See NummSquared Formally for:– The precise recursive definition of coercion– Justification of that recursive definition by a

well-founded relation

• Use coercion to define tagged small function extensions over all tagged small function extensions

• For tagged small function extensions f and x, f(x) = f<coer(domExt(f), x)>

• Coercion and result are computable.

Page 18: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: large function extensions

• Abstract over all tagged small function extensions

• f such that, for each tagged small function extension x, f(x) is a tagged small function extension

• A large function extension is also a proposition extension:– f is true iff, for each tagged small function

extension x, f(x) = one

Page 19: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NsGo

• NummSquared interpreter• Work in progress• Mostly automatically extracted from a

Coq program– Enhanced reliability

• F# and C# .NET assembly– Automatic memory management for free

• Complete: integrated Coq with MSBuild (Visual Studio build system)

Page 20: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NsGo components

NummSquaredprogram(string)

Parser (ANTLR) NummSquaredabstractprogram

(Coq)

NummSquarednormalized

abstractprogram

(Coq)

Normalization(Coq)

NummSquarednormalized

program(string) Printer

Printer

Parser (ANTLR)

Page 21: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

The Future

• Continue work on the NsGo, the NummSquared interpreter

• Equality for rules:– Currently extensional:

• Functions equal when domains and results equal• Equates different algorithms• Not computable

– Future:• Gilmore’s intensional equality and HiLog equality distinguish

functions by name.• Gilmore’s “use” vs. “mention” distinction.

• Lambda syntactic sugar• Reduction under lambdas• Efficient reduction algorithms for coercion

Page 22: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

References

• NummSquared 2006a0 Done Formally– poohbist.com– Click on NummSquared 2006a0.

• My thesis, NummSquared 2006a0 Explained– poohbist.com

• Other references are in the bibliography of NummSquared Formally.

• These slides– poohbist.com

Page 23: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Supplementary material

Page 24: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Domain membership

• When calling a function, how to ensure that the argument belongs to the function’s domain?

• Unrestricted domain – untyped lambda calculus

• Type constraints on programmer• Set theory: higher-order domain membership

not computable – need proofs by programmer

• NummSquared: coercion

Page 25: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NummSquared coercion summary

• Coercion to a valid domain extension of a tagged small function extension:– Ensures termination– Avoids paradoxes– Untyped– Supports computation

Page 26: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Semantics: small function extensions• Well-founded to ensure termination and avoid

paradoxes• Defined inductively• Rule f with:

– dom(f) a small subset of small function extensions• Small means no larger than a ZF set.

– for x in dom(f), f<x> is a small function extension• Leaf: null, zero, one

– null means absence of relevant information – does not mean 0, false, undefined or non-termination

• Pair p such that left(p) and right(p) are small function extensions

• A tree is a small function extension recursively containing only leaves and pairs.

Page 27: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

NummSquared syntax

• Variable-free• Large functions

– Constants– Combinations

• Proofs– Axioms– Inferences

• Reflection– Quoting and unquoting large functions– Quoting and unquoting proofs– The quoted representation is a tree.– Quoting is easy because NummSquared is variable-free.

• See NummSquared Formally for details.

Page 28: NummSquared a new well-founded, functional foundation for formal methods Samuel Howse poohbist.com samuelhowse@poohbist.com October 10, 2006 Copyright

Small function extensions in Coq• Leaves and pairs are easy.• Rules are somewhat similar to Benjamin Werner’s

“Sets in Types”.• Inductive Func_Sm_Ext : Type :=

| Func_Sm_Ext_null : Func_Sm_Ext| Func_Sm_Ext_zero : Func_Sm_Ext| Func_Sm_Ext_one : Func_Sm_Ext| Func_Sm_Ext_pair :

Func_Sm_Ext -> Func_Sm_Ext -> Func_Sm_Ext| Func_Sm_Ext_rule :

forall(A : Type),(A -> Func_Sm_Ext) -> domain: must be 1-1(A -> Func_Sm_Ext) -> resultFunc_Sm_Ext.

• Define appropriate equality on small function extensions