Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dependent Types make bad data unrepresentable

677 views

Published on

Dependent Types in F#

Published in: Software
  • Be the first to comment

Dependent Types make bad data unrepresentable

  1. 1. Make Unacceptable Data Unrepresentable Jack Fox @foxyjackfox Engineer at Visual Studio and Development Technologies MVP
  2. 2. Ideally… Front End Business Logic Incoming data properly edited Database imposes constraints
  3. 3. Perspective of the Applications Programmer ? ? ? ? ? ? ? ? ?
  4. 4. Static Types • String • Int • Float • Datetime • List
  5. 5. What if you could easily create custom types implementing the required business rule constraints?
  6. 6. What if you could easily create custom types implementing the required business rule constraints? …and give the type a meaningful name.
  7. 7. What if you could easily create custom types implementing the required business rule constraints? …and give the type a meaningful name. • Assurance the data is always acceptable
  8. 8. What if you could easily create custom types implementing the required business rule constraints? …and give the type a meaningful name. • Assurance the data is always acceptable • No worries about whether edits performed
  9. 9. What if you could easily create custom types implementing the required business rule constraints? …and give the type a meaningful name. • Assurance the data is always acceptable • No worries about whether edits performed • No worries about meeting DB constraints (if they exist)
  10. 10. Dependent Types U A universe of types (i.e. a type of types) A:U A is a type inhabiting a universe of types B: B is a collection of types (depending on the element, x : A) We call this a family of types Π-type, or dependent typeA B A U
  11. 11. Dependent Function /// type hint not necessary, enhances intellisense let f n : DependentType<_, _, int, int> = match n with | n' when n' < -100 -> LTminus100.Create n |> box | n' when n' > 100 -> GT100.Create n |> box | _ -> Minus100To100.Create n |> box |> unbox
  12. 12. The DependentType type type LTminus100 = DependentType<SetUp.MaxMinus101, int, int, int> type Minus100To100 = DependentType<SetUp.RangeMinus100To100, int * int, int, int> Π function type Π function parm type ‘T1 input ‘T2 underlying
  13. 13. Idris Definitional equality + propositional equality Constructor + proof ‘T Constrained to A element matching specific type in B family User writes functions operating directly on ‘T, but (probably) implicitly on A F# Definitional equality (types are equal by construction) Constructor + Π-function ‘T option Π-function is explicit type of same scope as dependent type Π-function output to singleton B family User writes functions operating explicitly on A
  14. 14. Some Use Cases • trimmed, non-empty, non-null string • non-empty generic set • utc datetime, or any particular timezone • uppercase Latin string of undetermined or static length • digit string of undetermined or static length • integer restricted to a range • collection of length within a range • any string validated by a RegEx • anything validated by a predicate
  15. 15. Dependent Types – Future Directions • Support extension methods https://github.com/jackfoxy/DependentTypes/issues/1 • Literal type parameters https://github.com/jackfoxy/DependentTypes/issues/3 • Add to F# core library • C# / VB.NET support
  16. 16. Bibliography F# Dependent Types https://jackfoxy.github.io/DependentTypes Looking Outward: When Dependent Types Meet I/O David Raymond Christiansen, M.Sc. Thesis http://itu.dk/people/drc/david-christiansen-thesis.pdf An intuitionistic theory of types, Per Martin-Löf http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.131.926&rep=rep1&type=pdf Type Theory: A Modern Computable Paradigm for Math http://www.science4all.org/article/type-theory/

×