record systems
play

Record Systems Dylan Just YOW LambdaJam 2015 Haskell Elm - PowerPoint PPT Presentation

Record Systems Dylan Just YOW LambdaJam 2015 Haskell Elm PureScript OCaml Idris Pascal Haskell Java classes Elm Scala classes PureScript C structs OCaml Database tables Idris Pascal Haskell Elm PureScript Haskell Elm


  1. type House = {color::String,rooms::Number} type Car = {color::String,model::String} myhouse = {color:"pink",rooms:3} mycar = {color:"red",model:"Corolla"} getColor :: forall r. {color::String | r} -> String getColor x = x.color purescript

  2. type House = {color::String,rooms::Number} type Car = {color::String,model::String} myhouse = {color:"pink",rooms:3} mycar = {color:"red",model:"Corolla"} lighten::forall r.{color::String|r} -> {color::String|r} lighten x = x { color = "light " ++ x.color } purescript

  3. type House = {color::String,rooms::Number} type Car = {color::String,model::String} myhouse = {color:"pink",rooms:3} mycar = {color:"red",model:"Corolla"} lighten::forall r.{color::String|r} -> {color::String|r} lighten x = x { color = "light " ++ x.color } > getColor (lighten myhouse) "light pink" purescript

  4. type House = {color::String,rooms::Number} type Car = {color::String,model::String} myhouse = {color:"pink",rooms:3} mycar = {color:"red",model:"Corolla"} lighten::forall r.{color::String|r} -> {color::String|r} lighten x = x { color = "light " ++ x.color } > getColor (lighten mycar) "light red" purescript

  5. Haskell strikes back!

  6. Type-level Strings

  7. data Person = Person {firstName::String, lastName::String} bob :: Person bob = Person {firstName="bob", lastName="cuttey"} bobFirstName = firstName bob bobLastName = lastName bob haskell

  8. type Person = Map String String bob' :: Person bob' = fromList [("firstName", "bob"), ("lastName", "cuttey")] bobFirstName' = bob' ! "firstName" bobLastName' = bob' ! "lastName" haskell

  9. bob = Person {firstName="bob", lastName="cuttey"} bob' = fromList [("firstName", "bob"), ("lastName", "cuttey")] haskell

  10. Field name bob = Person {firstName="bob", lastName="cuttey"} String bob' = fromList [("firstName", "bob"), ("lastName", "cuttey")] haskell

  11. Type-level information bob = Person {firstName="bob", lastName="cuttey"} Value-level information bob' = fromList [("firstName", "bob"), ("lastName", "cuttey")] haskell

  12. Kind type of Type type of Value haskell

  13. Kind * Type String Value "hello" haskell

  14. Kind * Symbol Type String "firstName" Value "hello" - haskell

  15. Kind * Symbol Type String "firstName" Value "hello" - Value-level String Type-level String haskell

  16. data Field (fieldName::Symbol) a = Field a bob :: Field "firstName" String bob = Field "bob" haskell

  17. data Field (fieldName::Symbol) a = Field a name type bob :: Field "firstName" String bob = Field "bob" value haskell

  18. Vinyl

  19. {-# LANGUAGE DataKinds, KindSignatures TypeOperators, GADTs, MultiParamTypeClasses #-} import Data.Vinyl get key rec = getField (rget key rec) haskell

  20. firstName = SField :: SField '("firstName", String) lastName = SField :: SField '("lastName", String) SField Constructor SField Type Constructor haskell

  21. firstName = SField :: SField '("firstName", String) lastName = SField :: SField '("lastName", String) Field Name Field Type haskell

Recommend


More recommend