computation by prophecy
play

Computation by Prophecy Venanzio Capretta University of Ottawa - PDF document

Computation by Prophecy Venanzio Capretta University of Ottawa with Ana Bove Chalmers University of Technology TYPES 2006 Nottingham, 1821 April 2006 1 How do we define general recursive functions in Type Theory? Only structurally


  1. Computation by Prophecy Venanzio Capretta University of Ottawa with Ana Bove Chalmers University of Technology TYPES 2006 Nottingham, 18–21 April 2006 1

  2. How do we define general recursive functions in Type Theory? Only structurally recursive functions are directly definable. Three differ- ent solutions: • Bove/Capretta 2001: Inductive Domain Predicate • Capretta 2005: Coinductive Partial Codomain • Prophecy Method: Coinductive Abstract Output 2

  3. Informal definition of the quicksort algorithm: qs : [ N ] → [ N ] qs [ ] = [ ] qs ( x :: l ) = ( qs l ≤ x ) + + x :: ( qs l >x ) where l ≤ x = [ y ← l | y ≤ x ] l >x = [ y ← l | y > x ] Example: qs [7 , 9 , 1 , 8 , 5 , 2] = ( qs [1 , 5 , 2]) + + 7 :: ( qs [9 , 8]) = [1 , 2 , 5] + + 7 :: [8 , 9] = [1 , 2 , 5 , 7 , 8 , 9] Not acceptable in Type Theory: l ≤ x and l >x not subterms of l . 3

  4. First Method (Bove) Inductive Domain Predicate: D qs : [ N ] → Prop x : N l : [ N ] h 1 : D qs l ≤ x h 2 : D qs l >x d nil : D qs [ ] d cons x l h 1 h 2 : D qs ( x :: l ) qs : ( l : [ N ])( D qs l ) → [ N ] qs [ ] d nil = [ ] qs ( x :: l ) ( d cons x l h 1 h 2 ) = ( qs l ≤ x h 1 ) + + x :: ( qs l >x h 2 ) In this case we can prove that D qs is always satisfied: p : ∀ l. D qs l QS : [ N ] → [ N ] QS l = qs l ( p l ) 4

  5. Advantages: • Close to Informal Definition • Recursive Equations (erase proofs) • Easy Proofs Disadvantages: • No Partial Application • We must always give a proof of the domain predicate (Proof = Trace of Computation) • No Type of Partial Recursive Functions 5

  6. Second Method (Capretta 2005) Coinductive type of partial elements: x : B ν CoInductive B : Type b : B B ν : Type � b � : B ν ⊲ x : B ν Examples of elements in N ν : � 5 � ⊲ ⊲ ⊲ � 3 � infinite ⊲ ⊲ ⊲ ⊲ ⊲ ⊲ · · · Partial functions: f : A ⇀ B means f : A → B ν All general recursive functions can be formal- ized in this type. Partial recursive functions are the arrows of the Kleisli category of the strong monad − ν . 6

  7. Advantages: • No Proofs Needed • Partial Application • Type of Partial Recursive Functions Disadvantages: • Difficult to Program • No Recursive Equations • Difficult Proofs 7

  8. � Prophecy Method Abstract coinductive representation of outputs: CoInductive [ N ] qs : Type y 1 , y 2 : [ N ] qs x : N qsln nil : [ N ] qs qsln cons x y 1 y 2 : [ N ] qs Elements of [ N ] qs are binary trees (possibly non-wellfounded) qsln cons x y 1 y 2 = x � � � � � � � � � � � � � � � � � y 1 y 2 Abstract definition of quicksort: qs ⋆ : [ N ] → [ N ] qs qs ⋆ [ ] = qsln nil qs ⋆ ( x :: l ) = qsln cons x ( qs ⋆ l ≤ x ) ( qs ⋆ l >x ) Recursive calls guarded by constructor qsln cons 8

  9. � � � � � � Example qs ⋆ [7 , 9 , 1 , 8 , 5 , 2] gives the tree: 7 � � ��������������� � � � � � � � � � � � � � � 1 9 � � � � ������� � � � � � � � � � � � � � • • 5 8 � ������� � � � � � � � � � • • • 2 � � ������� � � � � � � � • • We must evaluate the tree to get a (partial) list, an element of [ N ] ν : evaluate qs : [ N ] qs → [ N ] ν • Turn an element of [ N ] qs into a partial well- founded tree. • Evaluate well-founded trees by structural recursion. 9

  10. Well-founded trees are characterized by an in- ductive predicate: y : [ N ] qs Inductive Finite qsln y : Prop finite nil : Finite qsln qsln nil h 1 : Finite qsln y 1 h 2 : Finite qsln y 2 finite cons x y 1 y 1 h 1 h 2 : Finite qsln ( qsln cons x y 1 y 2 ) Evaluation of finite trees: eval Finite : ( y : [ N ] qs )( Finite qsln y ) → [ N ] eval Finite qsln nil finite nil = [ ] eval Finite ( qsln cons x y 1 y 2 ) ( finite cons x y 1 y 2 h 1 h 2 ) = ( eval Finite y 1 h 1 ) + + x :: ( eval Finite y 2 h 2 ) 10

  11. Trees are potentially infinite (if the computa- tion doesn’t terminate). So eval Finite is not always applicable. Idea: scan the tree at progressively higher depths, letting the clock tick ⊲ at each step. scan depth : ( y : [ N ] qs ) N → Maybe ( Finite qsln y ) Apply scan depth at progressively increasing depths: If we get Some , use eval Finite ; If we get None , tick ⊲ and try at higher depth. We obtain: evaluate qs : [ N ] qs → [ N ] ν qs : [ N ] → [ N ] ν qs l = evaluate qs ( qs ⋆ l ) 11

  12. Recursive Equations: qs [ ] � [ ] qs l ≤ x � r 1 qs l >x � r 2 qs ( x :: l ) � r 1 + + x :: r 2 12

  13. Advantages: • Partial Application • Type of Partial Recursive Functions • Recursive Equations Disadvantages: • Inefficient Evaluation • Proof of Equations Very Hard 13

  14. � � � Example with a partial function: f : N → N f 0 = 0 f ( S n ) = f ( S n ) + f n CoInductive C f : Type y 1 , y 2 : C f c 0 : C f c S y 1 y 2 : C f f ⋆ : N → C f f ⋆ 0 = c 0 f ⋆ ( S n ) = c S ( f ⋆ ( S n )) ( f ⋆ n ) f ⋆ 1 = c S � � � � � � � � � � � � � � � � � c S c 0 � � � � � � � � � � � � � � � � � c S c 0 � � �������� � � � � � � � . c 0 . . 14

Recommend


More recommend