hypertypes-0.2.2: Typed ASTs
Safe HaskellSafe-Inferred
LanguageHaskell2010

Hyper.Infer.Blame

Description

Hindley-Milner type inference with ergonomic blame assignment.

blame is a type-error blame assignment algorithm for languages with Hindley-Milner type inference, but without generalization of intermediate terms. This means that it is not suitable for languages with let-generalization. Let is an example of a term that is not suitable for this algorithm.

With the contemporary knowledge that "Let Should Not Be Generalised", as argued by luminaries such as Simon Peyton Jones, optimistically this limitation shouldn't apply to new programming languages. This blame assignment algorithm can also be used in a limited sense for existing languages, which do have let-generalization, to provide better type errors in specific definitions which don't happen to use generalizing terms.

The algorithm is pretty simple:

  • Invoke all the inferBody calls as infer normally would, but with one important difference: where inferBody would normally get the actual inference results of its child nodes, placeholders are generated in their place
  • Globally sort all of the tree nodes according to a given node prioritization (this prioritization would be custom for each language)
  • According to the order of prioritization, attempt to unify each infer-result with its placeholder using inferOfUnify. If a unification fails, roll back its state changes. The nodes whose unification failed are the ones assigned with type errors.

Lamdu uses this algorithm for its "insist type" feature, which moves around the blame for type mismatches.

Note: If a similar algorithm already existed somewhere, I would very much like to know!

Synopsis

Documentation

blame :: forall priority err m exp a. (Ord priority, MonadError err m, Blame m exp) => (forall n. (a # n) -> priority) -> (InferOf exp # UVarOf m) -> (Ann a # exp) -> m (Ann (a :*: BlameResult (UVarOf m)) # exp) Source #

Perform Hindley-Milner type inference with prioritised blame for type error, given a prioritisation for the different nodes.

The purpose of the prioritisation is to place the errors in nodes where the resulting errors will be easier to understand.

The expected MonadError behavior is that catching errors rolls back their state changes (i.e StateT s (Either e) is suitable but EitherT e (State s) is not)

Gets the top-level type for the term for support of recursive definitions, where the top-level type of the term may be in the scope of the inference monad.

class (Infer m t, RTraversable t, HTraversable (InferOf t), HPointed (InferOf t)) => Blame m t where Source #

Class implementing some primitives needed by the blame algorithm

The blamableRecursive method represents that Blame applies to all recursive child nodes. It replaces context for Blame to avoid UndecidableSuperClasses.

Minimal complete definition

inferOfUnify, inferOfMatches

Methods

inferOfUnify :: Proxy t -> (InferOf t # UVarOf m) -> (InferOf t # UVarOf m) -> m () Source #

Unify the types/values in infer results

inferOfMatches :: Proxy t -> (InferOf t # UVarOf m) -> (InferOf t # UVarOf m) -> m Bool Source #

Check whether two infer results are the same

blamableRecursive :: Proxy m -> RecMethod (Blame m) t Source #

data BlameResult v e Source #

Constructors

Good (InferOf' e v) 
Mismatch (InferOf' e v, InferOf' e v) 

Instances

Instances details
Generic (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Associated Types

type Rep (BlameResult v e) :: Type -> Type #

Methods

from :: BlameResult v e -> Rep (BlameResult v e) x #

to :: Rep (BlameResult v e) x -> BlameResult v e #

Constraints (BlameResult v e) Show => Show (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Methods

showsPrec :: Int -> BlameResult v e -> ShowS #

show :: BlameResult v e -> String #

showList :: [BlameResult v e] -> ShowS #

Constraints (BlameResult v e) Binary => Binary (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Methods

put :: BlameResult v e -> Put #

get :: Get (BlameResult v e) #

putList :: [BlameResult v e] -> Put #

Constraints (BlameResult v e) NFData => NFData (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Methods

rnf :: BlameResult v e -> () #

Constraints (BlameResult v e) Eq => Eq (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Methods

(==) :: BlameResult v e -> BlameResult v e -> Bool #

(/=) :: BlameResult v e -> BlameResult v e -> Bool #

Constraints (BlameResult v e) Ord => Ord (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

Methods

compare :: BlameResult v e -> BlameResult v e -> Ordering #

(<) :: BlameResult v e -> BlameResult v e -> Bool #

(<=) :: BlameResult v e -> BlameResult v e -> Bool #

(>) :: BlameResult v e -> BlameResult v e -> Bool #

(>=) :: BlameResult v e -> BlameResult v e -> Bool #

max :: BlameResult v e -> BlameResult v e -> BlameResult v e #

min :: BlameResult v e -> BlameResult v e -> BlameResult v e #

type Rep (BlameResult v e) Source # 
Instance details

Defined in Hyper.Infer.Blame

type Rep (BlameResult v e) = D1 ('MetaData "BlameResult" "Hyper.Infer.Blame" "hypertypes-0.2.2-9g9pX7Hb2mGI4yyssTDpOd" 'False) (C1 ('MetaCons "Good" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (InferOf' e v))) :+: C1 ('MetaCons "Mismatch" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (InferOf' e v, InferOf' e v))))

_Good :: forall v e. Prism' (BlameResult v e) (InferOf' e v) Source #

_Mismatch :: forall v e. Prism' (BlameResult v e) (InferOf' e v, InferOf' e v) Source #

type InferOf' e v = InferOf (GetHyperType e) # v Source #

A type synonym to help BlameResult be more succinct