1
play

1 Datatypes Generally This Scheme Does Not Always Work datatype ( - PDF document

Types in Isabelle Course 2D1453, 2006-07 Types: T ::= A | X | X :: C | T T | (T 1 ,...,T n ) K where: Advanced Formal Methods A {bool, int, ...} base type X { , ,...} type variable Lecture 4: Isabelle Types


  1. Types in Isabelle Course 2D1453, 2006-07 Types: T ::= A | X | X :: C | T � T | (T 1 ,...,T n ) K where: Advanced Formal Methods • A � {bool, int, ...} base type • X � {’ α , ’ β,...} type variable Lecture 4: Isabelle – Types and Terms • K � {set, list,...} type constructor Used for defining new types Mads Dam • C � {order, linorder, type,...} type classes KTH/CSC Used for associating axioms to types Examples: • int list, int set ,... Some material from Paulson • nat :: order, int :: field, ... Introducing New Types Type Declarations Types in Isabelle are nonempty Syntax: typedecl K Theorem in HOL: �� x :: T . x = x Example: typedecl addr So all types must be inhabited Three basic mechanisms: Introduces an abstract type of addresses • Type declarations • Type abbreviations Nothing known of an x :: addr • Recursive type definitions But: Some x :: addr exists Type Abbreviations Recursive Type Definitions datatype ’ α list = Nil | Cons ’ α (’ α list) Syntax: types (’ α 1 ,...,’ α n ) K = T Defines a recursive datatype with associated constants: Nil :: ’ α list Examples: types number = nat Cons :: ’ α � ’ α list � ’ α list tag = string ’ α taglist = (’ α � tag) list Plus axioms: Distinctness: Nil ≠ Cons x xs Injectivity: (Cons x xs = Cons y ys) = (x = y � xs = ys) All type abbreviations are expanded in Isabelle Not visible in internal representation or Isabelle output Also axioms for induction 1

  2. Datatypes Generally This Scheme Does Not Always Work datatype (’ α 1 ,...,’ α n ) K = Consider datatype lam = mkfun (lam � lam) constr 1 T 1,1 ... T 1,n1 ... constr m T m,1 ... T m,nm Note: Can interpret untyped lambda calculus using lam! Constants and types as previous slide Problematic definition: Cardinality of T � T as set is strictly greater than that of Note: T, for any T Simplifier automatically extended with distinctness and So need to rule out most functions injectivity LCF and domain theory: T � T is set of continuous Induction must be handled explicitly functions on complete lattice or cpo Not trivial that (T 1 ,...,T n ) K exists! LCF embedding in Isabelle exists Proof goals automatically added and discharged Simple Recursion Mutual Recursion datatype (’ α 1 ,...,’ α n ) K = datatype (’ α 1 ,...,’ α n ) K = constr 1 T 1,1 ... T 1,n1 ... constr 1 T 1,1 ... T 1,n1 constr m T m,1 ... T m,nm ... constr m T m,1 ... T m,nm Each T i,j , T i,j ’ is either and non-recursive or of Each type T i,j can be either: (’ α 1 ’,...,’ α n’ ’) K’ the form ... K or ... K’ • Non-recursive: All type constants K’ in T i,j are defined ”prior” to the definition of K constr 1 ’ T 1,1 ’ ... T 1,n1’ ’ • An expression of the form (T 1 ’,...,T n ’) K where each T k ’ is ... non-recursive constr m’ ’ T m’,1 ’ ... T m’,nm’’ ’ Covariance and Contravariance Nested Recursion Introduce relations X � + T and X � - T datatype (’ α 1 ,...,’ α n ) K = • X � + T: T is covariant in X constr 1 T 1,1 ... T 1,n1 • X � - T: T is contravariant in X ... constr m T m,1 ... T m,nm X � + T 1 X � - T 2 X � - T 1 X � + T 2 - X � + X X � - T 1 � T 2 X � + T 1 � T 2 Each type T i,j is of form X � + T i X � - T i 1 � i � n 1 � i � n T[(T 1,1 ’,...,T 1,n ’) K/X 1 ,..., [(T k,1 ’,...,T k,n ’) K/X k ] X � + (T 1 ,...,T n ) K X � - (T 1 ,...,T n ) K such that Covariance = monotonicity: As sets , if X � + T then A � B • X i � + T for all i: 1 � i � k implies T[A/X] � T[B/X] • Any K’ occurring in T is defined prior to K Contravariance = antimonotonicity: If X � - T then A � B implies T[B/X] � T[A/X] Note: Simple recursion is special case Mutual, nested recursion possible too 2

  3. Type Classes Terms in Isabelle Used to associate axioms with types Terms: t ::= x | c | ?x | t t | λ x. t Example: Preorders where: • x � Var – variables • C � Con – constants axclass ordrel < type consts le :: (’ α :: ordrel) � ’ α � bool • ?x – schematic variable λ x. t - must be typable • axclass preorder < ordrel orderrefl: le x x Schematic variables: ordertrans: (le x y) � (le y z) � le x z • Free variables are fixed • Schematic variables can be instantiated during proof Advanced topic – return to this later Schematic Variables Defining Terms State lemma with free variables Three basic mechanisms: lemma foobar : f(x,y) = g(x,y) • Defining new constants non-recursively ... No problems done Constructs: defs , constdefs • Defining new constants by primitive recursion During proof: x, y must never be instantiated! Termination can be proved automatically Constructs: primrec After proof is finished, Isabelle converts free var’s to • General recursion schematic var’s f(?x,?y) = g(?x,?y) Termination must be proved Constructs: recdef Now can use foobar with ?x � f and ?y � a, say Non-Recursive Definitions Unfolding Definitions Declaration: Definitions are not always unfolded automatically by consts Isabelle sq :: nat � nat To unfold definition of sq: apply (unfold sqdef) Definition: defs Tactics such as simp and auto do unfold constant sqdef: sq n = n * n definitions Or combined: constdefs sq :: nat � nat sq n = n * n 3

  4. Definition by Primitive Recursion Primitive Recursion, General Scheme consts Assume data type definition of T with constructors append :: ’ α list � ’ α list � ’ α list constr 1 ,..., constr m primrec Let f :: T 1 � ... � T n � T’ and T i = T append Nil ys = ys append (Cons x xs) ys = Cons x (append xs ys) Primitive recursive definition of f: f x 1 ... (constr 1 y 1 ... y k1 ) ... x n = t 1 Append applied to strict subterm xs of Cons x xs: ... Termination is guaranteed f x 1 ... (constr m y 1 ... y km ) ... x n = t m Each application of f in t 1 ,...,t m of the form f t 1 ’ ... y kj .. t n ’ Partial Functions The Case Construct datatype ’ α option = None | Some ’ α Every datatype introduces a case construct, e.g. (case xs of Nil � . . . | (Cons y ys) � ... y ... ys ...) Important application: T � ’ α option � partial function: In general: one case per constructor None � no result • No nested patterns, e.g. Cons y 1 (Cons y 2 ys) Some t � result t • But cases can be nested Example: consts lookup :: ’ α � (’ α × ’ β ) list � ’ β option Case distinctions: primrec apply (case tac t ) lookup k [] = None creates k subgoals t = constr i y 1 . . . y ki � . . . lookup k (x#xs) = (if fst x = k then Some(snd x) else lookup k xs) one for each constructor constr i Mutual and Nested Primitive Recursion Mutual and Nested Recursion, II Primitive recursion scheme applies also for mutual and Mutual, primitive recursive definition of f and g: nested recursion 1 y 1 ... y k1,1 ) ... x nf = t 1,f f x 1 ... (constr 1 ... Assume data type definition of T 1 and T 2 with constructors 1 y 1 ... y km1,1 ) ... x nf = t m1,f f x 1 ... (constr m1 constr 1 1 ,..., constr m1 1 , constr 1 2 ,...,constr m{2} 2 , respectively 2 y 1 ... y k1,2 ) ... x ng = t 1,g g x 1 ... (constr 1 Let: ... f :: T 1 � ... � T nf � T f ’, T i = T 1 , 2 y 1 ... y km2,2 ) ... x ng = t m2,g g x 1 ... (constr m g :: T 1 � ... � T ng � T g ’, T j = T 2 Each application of f or g in t 1,f ,...,t m1,f , t 1,g ,...,t m2,g of the form h t 1 ’ ... y k ... t n ’ , h � {f,g} Slightly more general schemes possible too 4

Recommend


More recommend