Record Type Families Wolfgang Jeltsch Introduction A simple selfmade record system Record Type Families: Record type A Key to Generic Record Combinators families Record scheme induction Wolfgang Jeltsch TT¨ U K¨ uberneetika Instituut Teooriaseminar October 20, 2011
Record Type Overview Families Wolfgang Jeltsch Introduction A simple selfmade record system Introduction Record type families Record scheme induction A simple selfmade record system Record type families Record scheme induction
Record Type Overview Families Wolfgang Jeltsch Introduction A simple selfmade record system Introduction Record type families Record scheme induction A simple selfmade record system Record type families Record scheme induction
Record Type A typical system of extensible records Families Wolfgang Jeltsch ◮ records map names to values: Introduction wolfgang = { surname = "Jeltsch" , A simple selfmade record system age = 33 , Record type = "Cottbus" } place families Record scheme ◮ types of records map names to types: induction wolfgang :: { surname :: String , age :: Integer , :: String } place ◮ only field-related operations: ◮ selection ◮ modification ◮ addition ◮ removal ◮ no support for combinators, i.e., functions that work with complete records
Record Type An example combinator Families Wolfgang Jeltsch ◮ record of modifications: Introduction A simple selfmade mods = { surname = id , record system age = (+1) , Record type families place = const "Tallinn" } Record scheme induction ◮ type of the modification record: mods :: { surname :: String → String , :: Integer → Integer , age place :: String → String } ◮ function modify that performs the modification: modify mods wolfgang = { surname = "Jeltsch" , age = 34 , = "Tallinn" } place
Record Type Generic record combinators Families Wolfgang Jeltsch Introduction ◮ modify shall work with all modification and data records A simple selfmade record system whose types match: Record type ◮ modify must be generic families ◮ type of modify must be able to express necessary Record scheme induction relationships between the argument types ◮ modify works with complete records ◮ topic of this talk: a record system that allows us to define combinators like modify ◮ implemented as a Haskell library: ◮ works with standard GHC ◮ key to success are advanced type system features
Record Type Overview Families Wolfgang Jeltsch Introduction A simple selfmade record system Introduction Record type families Record scheme induction A simple selfmade record system Record type families Record scheme induction
Record Type Heterogeneous lists Families Wolfgang Jeltsch Introduction ◮ types for building heterogeneous lists: A simple selfmade record system ◮ the empty list: Record type data X = X families ◮ non-empty lists, each consisting of an initial list Record scheme induction and a last element: data δ :& ε = δ :& ε ◮ example list: X :& "Jeltsch" :& 33 :& "Cottbus" ◮ type of this list: X :& String :& Integer :& String
Record Type Records Families Wolfgang Jeltsch Introduction A simple selfmade record system ◮ record is heterogeneous list of fields: Record type families field a pair of a name and a value Record scheme field type a pair of a name and a type induction ◮ names appear at the value level and at the type level ◮ represent names by a type and a data constructor: data N = N ◮ type of fields: data ν ::: α = ν := α
Record Type The example data record Families Wolfgang Jeltsch ◮ field names: Introduction data Surname = Surname A simple selfmade record system data Age = Age Record type families data Place = Place Record scheme induction ◮ data record: wolfgang = X :& Surname := "Jeltsch" :& Age := 33 :& Place := "Cottbus" ◮ type of the data record: wolfgang :: X :& Surname ::: String :& Age ::: Integer :& Place ::: String
Record Type Overview Families Wolfgang Jeltsch Introduction A simple selfmade record system Introduction Record type families Record scheme induction A simple selfmade record system Record type families Record scheme induction
Record Type Record type families Families Wolfgang Jeltsch ◮ allow us to specify relationships between record types Introduction A simple selfmade ◮ record type now built from two ingredients: record system scheme a list of pairs, each consisting of a name Record type families and a so-called sort: Record scheme induction X :& ν 1 ::: ς 1 :& . . . :& ν n ::: ς n style a type-level function σ ◮ types of field values are generated on the fly by applying the style to the sorts: σ ς 1 , . . . , σ ς n ◮ families of related record types can be generated by combining the same scheme with different styles
Record Type Implementation Families Wolfgang Jeltsch Introduction A simple selfmade ◮ record scheme is a type with a sort parameter record system ◮ type ρ σ is the record type with scheme ρ and sort σ Record type families ◮ type declarations: Record scheme induction σ = X data X data ( ρ :& ϕ ) σ = ρ σ :& ϕ σ data ( ν ::: ς ) σ = ν := σ ς ◮ class Record of all record schemes: Record ρ class instance Record X instance ( Record ρ ) ⇒ Record ( ρ :& ν ::: ς )
Record Type The type of modify Families Wolfgang Jeltsch Introduction ◮ record styles: A simple selfmade record system data λα → α Record type modification λα → ( α → α ) families ◮ type of modify : Record scheme induction ( Record ρ ) ⇒ ρ ( λα → ( α → α )) → ρ ( λα → α ) → ρ ( λα → α ) ◮ problem: no λ -expressions at the type level ◮ solution: defunctionalization at the type level
Record Type Defunctionalization at the type level Families Wolfgang Jeltsch Introduction A simple selfmade record system ◮ type-level functions represented by (empty) types Record type ◮ type synonym family that describes function application: families Record scheme type family App ϕ α induction ◮ representation of a type-level function λα → τ (where α may occur free in τ ): data Λ type instance App Λ α = τ ◮ modified declaration of the type of record fields: data ( ν ::: ς ) σ = ν := App σ ς
Record Type The type of modify with defunctionalization Families Wolfgang Jeltsch Introduction A simple selfmade record system ◮ representations of the two record styles: Record type families data Σ Plain Record scheme induction data Σ Mod type instance App Σ Plain α = α type instance App Σ Mod α = α → α ◮ type of modify : ( Record ρ ) ⇒ ρ Σ Mod → ρ Σ Plain → ρ Σ Plain
Record Type Overview Families Wolfgang Jeltsch Introduction A simple selfmade record system Introduction Record type families Record scheme induction A simple selfmade record system Record type families Record scheme induction
Record Type Implementation of modify Families Wolfgang Jeltsch ◮ make modify a method of the Record class: Introduction class Record ρ where A simple selfmade record system modify :: ρ Σ Mod → ρ Σ Plain → ρ Σ Plain Record type families ◮ implement modify within the instance declarations Record scheme of Record : induction instance Record X where modify X X = X instance ( Record ρ ) ⇒ Record ( ρ :& ν ::: α ) where modify ( q :& := f ) ( r :& ν := x ) = modify q r :& ν := f x ◮ definition of modify uses induction over record schemes ◮ problem: impossible to add further methods later
Record Type A fold combinator for record schemes Families Wolfgang Jeltsch ◮ induction principles are captured by fold combinators Introduction A simple selfmade ◮ all inductive definitions on record schemes expressible record system as applications of a record scheme fold operator Record type families ◮ implement such a combinator: Record scheme induction class Record ρ where fold :: θ X → ( ∀ ρ ν ς. ( Record ρ ) ⇒ θ ρ → θ ( ρ :& ν ::: ς )) → θ ρ instance Record X where = f X fold f X instance ( Record ρ ) ⇒ Record ( ρ :& ν ::: ς ) where � � fold f X f (:&) = f (:&) fold f X f (:&)
Record Type Implementation of modify using fold Families Wolfgang Jeltsch ◮ replacement type for the θ -variable: Introduction type Θ modify ρ = ρ Σ Mod → ρ Σ Plain → ρ Σ Plain A simple selfmade record system ◮ implementation of modify : Record type families modify :: ( Record ρ ) ⇒ Record scheme ρ Σ Mod → ρ Σ Plain → ρ Σ Plain induction modify = fold f X f (:&) where f X :: Θ modify X f X X X = X f (:&) :: ( Record ρ ) ⇒ Θ modify ρ → Θ modify ( ρ :& ν ::: ς ) f (:&) g = λ ( q :& ν := f ) ( r :& := x ) = g q r :& ν := f x ◮ cheated a bit: Θ modify must be a proper type, not a type synonym
Recommend
More recommend