Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,263
    • Issues 4,263
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 417
    • Merge Requests 417
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Wiki
  • infer datatype contexts

Last edited by Ben Gamari Apr 01, 2019
Page history New page

infer datatype contexts

Infer DatatypeContexts

Haskell prime has decided to depricate DatatypeContexts.

Running Example:

data (Eq e) => HasEq e = HasEq e

In haskell98, 2010 these are not typically usable as you might expect

eq :: HasEq e -> HasEq e -> Bool
eq (HasEq e) (HasEq e') = e == e'

Would give a missing context error, so contexts always had to be specified.

eq :: (Eq e) => HasEq e -> HasEq e -> Bool
eq (HasEq e) (HasEq e') = e == e'

GADT alternative

An alternative in terms of GADTs that works as expected has been suggested.

data HasEq e where
    HasEq :: (Eq e) => e -> HasEq e

eq :: HasEq e -> HasEq e -> Bool
eq (HasEq e) (HasEq e') = e == e'

Problems with GADT alternative

  • For performance sensitive types this might be problematic since they are always stored in a box that could be bottom and that needs to be checked at runtime.

  • If you have a num container,

    data IsNum n where
        IsNum :: (Num n) => n -> IsNum n

    you can not write the type of the following expression without stating the type class

    IsNum $ fromInteger 1 :: (Num n) => IsNum n

Suggested Extensions

Syntactic sugar for explicit type annotations + Constructor type change

How can you get a value of type HasEq a.

  1. Using a Constructor (also in a pattern): Use the same trick as with GADTs? Just make the type signature of the constructor (Eq a) => a -> HasEq a
  2. Explicit type signature (undefined :: HasEq a): For all free variables in HasEq (...) add Eq (...) to the context where the variable is bound.

Listing of all places where explicit types appear.

  • Haskell98

    • type simpletype = type just a simple type synonym no need to do anything
    • data [context =>] simpletype = constrs [deriving] check the constructors for types with DatatypeContexts. Place all found contexts in context. If the context is not empty? Change the constructor types to: Constr :: context => ...
    • same for newtype
    • class [scontext =>] tycls tyvar [where cdecls] Nothing to do really.
    • instance [scontext =>] qtycls inst [where idecls] Make InferDatatypeContext imply FlexibleContexts. Check inst for types with DatatypeContexts. Add contex to scontext.
    • default (type1 , ... , typen) ignore
    • gendecl -> vars :: [context =>] type Check type. Add to context
    • context -> class | ( class1 , ... , classn ) Check types in class1 ... Add to context
    • exp -> exp0 :: [context =>] type Check type. Add to context
  • Extensions:

    • Explicit forall RankNTypes, Imporedicative types. For all free variables in HasEq (...) add Eq (...) to the context where the variable is bound e.g.

      Maybe (
          forall b. (
              forall c. HasEq (a,b,c,forall d.d)
              ) -> ()
          ) -> a

      becomes

      Eq (a,b',c',d') => Maybe (
          forall b. (Eq (a,b,c'',d'')) => (
              forall c. (Eq (a,b,c,d''')) => HasEq (a,b,c,forall d.d)
              ) -> ()
          ) -> a
    • Type families: like with type synonyms contexts can be safely ignored here. What about newtype type families?

    • Functional Dependencies: TODO

    • GADTs: TODO

  1. Functions in modules without this extension. Maybe it's possible to add the contexts when the functions are imported. I don't know.

Uses

Related

#8026 (closed)

http://www.haskell.org/pipermail/haskell-prime/2010-July/003249.html

Clone repository

GHC Home
GHC User's Guide

Joining In

Newcomers info
Mailing Lists & IRC
The GHC Team

Documentation

GHC Status Info
Working conventions
Building Guide
Debugging
Commentary

Wiki

Title Index
Recent Changes