Dependent Types in Gradual Development

an implementation strategy for an implementation n.w
1 / 47
Embed
Share

Explore the implementation strategy for gradual dependent types through insightful joint work, addressing the functionality of gradual dependent types, bug fixes, and advancing towards unblocking development while catching bugs effectively.

  • Development
  • Types
  • Bug Fixes
  • Gradual Types
  • Implementation

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. An Implementation Strategy for An Implementation Strategy for Gradual Dependent Types Gradual Dependent Types Joey Eremondi WITS 2023 August 28, 2023 Joint work with Ron Garcia and ric Tanter 1

  2. Gradual Dependent Gradual Dependent Types: What and Why? Types: What and Why? 2

  3. A Buggy Quicksort sort : List Nat -> List Nat Removes duplicates ! sort Nil = Nil sort [9, 8, 9] = [8,9] sort (Cons h t) = let lower = sort (filter (< h) t) higher = sort (filter (> h) t) in lower ++ [h] ++ higher Silently incorrect 3

  4. Adding the Spec To the Types (with the magic of dependent types) Fixed-length Lists: Length index FList : Type -> Nat -> Type FList a n = (lst : List a) (length lst = n) Dependent pair Propositional equality: length matches index Underlying list 4

  5. Dependent Types Catch the Bug? To run, must prove: length (lower ++ [h] ++ higher) = 1 + length t sort : List Nat -> List Nat sort : FList n Nat -> FList n Nat sort Nil = Nil sort (Nil , pf) = Nil (Nil , pf) sort (Cons h t , pf) = sort (Cons h t) = let lower = sort (filter (< h) t) higher = sort (filter (> h) t) in lower ++ [h] ++ higher in (lower ++ [h] ++ higher , ) vs Sort accidentally removes duplicates 5

  6. Unblocking Development ? as value not type sort : List Nat -> List Nat sort : FList n Nat -> FList ? ? Nat sort Nil = Nil sort (Nil , pf) = Nil (Nil , pf) sort (Cons h t , pf) = sort (Cons h t) = let lower = sort (filter (< h) t) higher = sort (filter (> h) t) in (lower ++ [h] ++ higher , refl) Actual length is consistent with ? 6

  7. Going Further: Catching Bugs Keep desired invariant in types sort : List Nat -> List Nat sort : FList n Nat -> FList n Nat sort Nil = Nil sort (Nil , pf) = Nil (Nil , pf) sort (Cons h t , pf) = sort (Cons h t) = let lower = sort (filter (< h) t) higher = sort (filter (> h) t) in (lower ++ [h] ++ higher , ? ?) ? as proof Track at runtime to catch error 7

  8. Degrees of Imprecision ? As a type Types Affects type-checking computations containing ?- typed terms ? As a term in a type Need semantics for ? ? As a term in at run time 8

  9. Static-Dynamic Tension Dependent (Rigid) Gradual (Flexible) Possibility of dynamic type errors Well-typed at each step Approximate Normalization Normalize for decidable type checking Allows non- termination Dynamic vs static consistency Syntactic compile-time type comparison Run-time type comparison 9

  10. Effectful Nature of Gradual Types ? ?:? .? ? (? ?:? .? ?) 1 + ????:? Non- Dynamic errors termination During type checking 10

  11. Implementation Strategy Idea Challenges Effects in gradual code Translate to non- gradual dependent types Allows non- termination Re-use optimizations, unification, conversion checking Can be done as a compiler-pass or even with macros Run-time type comparison 11

  12. Approximate Approximate Normalization Normalization 12

  13. Managing non-termination Approximate normalization Missing type information Can't guarantee termination ? as result Compile time Approx but terminating Run time Exact, might run forever 13

  14. Managing non-termination Approximate normalization Missing type information Can't guarantee termination ? as result Compile time Approx but terminating Run time Exact, might run forever 14

  15. The Idea (? ?) embedded in ? ? contains values of any type Essentially non-positive Idea: Approx functions as bound ( x . f[x]) becomes ( x . f[?]) When converting to ? Compile time: Only embed ? ? 15

  16. Why Bother? Is undecidable checking that bad? Transparency Non-termination in run-time check Hard to debug code that the programmer didn t write Semantics/Implementation Translate to static dependent types 16

  17. Gradual Propositional Gradual Propositional Equality Equality 17

  18. Propositional Equality ?1=??2 ????:? =?? Type of all proofs ?1 and ?2are equal Only proof: x equal to self Pre- and post-conds Prove branch unreachable Rewrite goal type 18

  19. Gradual Propositional Woes 1. Extensionality Conflict Forall ?, ? ? = ? ? 2. Can we have ? consistent with ?? Yes -> accept too many programs No -> violate static equiv. 19

  20. Extensional Tension f := ( x. y. x + x ) Should be statically distinguishable Should be dynamically indistinguishable fsame := ( x. y. 2 x) fdiff :=( x. y. y + y ) 20

  21. Distinguishing Context zeroes ? ??? ??? ??? ? f = ?.? + ? g = ?.2 ? Statically indistinguishable Ctx = .(((Refl f f ?) (f h)) Ctx g Error violates static equiv. 21

  22. Problematic Consequence Lennon-Bertrand et al 2022 ???? is the only constructor + Gradual guarantees = Make refl NOT the only constructor! distinguish some observationally equivalent programs 22

  23. Equality Witnesses ????not only constructor Dynamically track consistency info Consistency witness Spectrum of equality proofs Value as precise as equated terms Composition operator to combine ?:?1= ?2witnessed by ?1 ?2 23

  24. Key Idea Gradual Static Single constructor refl : x = x Family of constructors refl(w) : x = y for any consistent x, y x y space of possible witnesses w 24

  25. Creating Equality Proofs Fully static refl(x) : x = x Initial Witness ? : x = y refl(x y) x y 25

  26. Transforming Equality Proofs refl(w) :: x y refl(w x y) Cast between equality types Remembers x , y , and all previous constraints x y x y z 26

  27. Eliminating Equality transport(eq : x = y) P(x) P(y) Gradual Static transport(refl(w)) transport(refl) P(x) P(y) P(x) P(x) Retains info from witness Cast Cast P(w) 27

  28. OTT Again? Dynamic version of Observational Type Theory transport has computational content Type-directed defn. of equality Gradual version of K, funext Hope to make connection deeper in future 28

  29. Back to Our Example Keep desired invariant in types sort : List Nat -> List Nat sort : FList n Nat -> FList n Nat sort Nil = Nil sort (Nil , pf) = Nil (Nil , pf) sort (Cons h t , pf) = sort (Cons h t) = let lower = sort (filter (< h) t) higher = sort (filter (> h) t) in (lower ++ [h] ++ higher , ? ?) ? as proof Track at runtime to catch error 29

  30. Back To Our Example sort [9, 8, 9] Goal was impossible to prove lower := [8] higher := [] returns ([8, 9], ? ?) ? ? : 2 = 3 refl(error) refl(2 3) 30

  31. Not Just Safety ??? (? ?) ????? ? ? ????? ? ? ????? (? ?) ? ???? (? ?) (? ?) ????? ? (? + ?) ????? ? ? ????1 ????? ? ?? ? ????1= ? , ? ,???? 2 ? ????2 ????? ? ?? ? ????2= ???? ? ?,???? 1+? ? 31

  32. Not Just Safety ctd. ???? 3 (??? ?????1 ????2) ???? 3 (??? ?( ?,? ,????(2)) (???? ? ?,????(1+?)) ) ???? 3 (???? (A,C) ?,refl(2 1+?)) From cast, transport ???? 3 (???? (A,C) ?,refl(2)) (???? (A,C) ?,refl(3 2)) Code was safe but wrong! (???? (A,C) ?,refl(?????)) 32

  33. Importance of Composition Term type T, Ascribed type S ?:? ? ? if? ? = Otherwise reduces What should ( ?.? + ?) ( ?.2 ?) be? E.g. for ( ?.? + ? ?.? + ? ) ( ?.? + ? ?.2 ? ) 33

  34. Meet for Functions Bound variables May contain neutrals for distinct neutrals? Throw error? Can distinguish extensional functions No error Can compose inconsistent terms 34

  35. Completing the Semilattice Add terms to complete the semilattice as first-class op Not meta-op Neutrals compose to neutrals E.g. ( ?.? + ?) ( ?.2 ?) reduces to ?. ? + ? 2 ? Then blocked! 35

  36. Resolving the tension Static vs Dynamic Consistency Static: Distinct neutrals inconsistent Prevents typing too many programs Dynamic: compose to non-error all neutrals dynamically consistent Respects static equivalences 36

  37. Semantics Semantics 37

  38. General Approach Syntactic model/shallow embedding Translate gradual to static dependent types Termination via simulation Model types using codes Universe ala Tarski Modelling exact execution Guarded type theory Logically consistent model of non-termination 38

  39. Modelling the Unknown Type Recursive sum Constructor for Pi, Sigma, Eq, inductive, etc. Strictly positive due to approx. norm. Hard part: including *all* inductives WANTED: Parameter-aware version of Containers/W-Types/Descriptions Separate + and parameter uses 39

  40. Gradual Operation Each semantic type: semilattice ???? ? ???? ?? ? ?? ? ?? ? ??? ? ???? ?? ? (repr of ?) ???: ? ???? ?? ? (error at a type) Types connected by casts ???? ?1 ?2: ???? ?? ?1 ?? ?2 Codes must also be a semilattice 40

  41. Mutual Dependency Mutual ops ? Uses Equality witnesses uses cast Cast uses ? 41

  42. Ordinals for Termination Custom Brouwer trees Strictly-monotone idempotent max a < b and c < d implies (max a c) < (max b d) Closed under limits over arbitrary codes Lim c f for any (c : Code) and (f : El c -> Ord) Trickery to ensure idempotence Ensure limits are always max-ed with themselves an infinite number of times 42

  43. Decreasing Metric Ordinal for each code Lim needed for Pi/Sigma types Meet and ? Decreasing in size of code Code meet and cast Decreasing in size of max(c1, c2) 43

  44. Future Work Future Work 44

  45. Model metatheory Current proofs: Simulations Syntactic, complicated Guarded Type Theory Prove semantically in model Prove in GTT itself 45

  46. Inductive families Equality Way to model indexed types Doesn't propagate constraints right Over time, not over space Monotone references Shared mutable witnesses Run-time unification 46

  47. Implementation Idris 2 Dependent types Programming-focused Escape hatch for non-termination Syntactic model Same translation Gradual surface -> Idris core Challenges Type classes Implicits/unification 47

More Related Content