curry.typecheck.defaulting module

Defaulting of the class constraints of a goal, with the table of the PAKCS REPL.

A goal whose scheme has class constraints cannot run as it is: every constraint is a dictionary parameter. The REPL of PAKCS picks a type for each constrained type variable with a fixed table (defaultQualType in c2p.pl), and this module applies the same table:

  • Num and Integral default to Int.

  • Fractional and Floating default to Float.

  • Monad and MonadFail default to IO.

  • Data, Eq, Ord, Read and Show are dropped on a variable defaulted to Int or Float; Enum is dropped on a variable defaulted to Int; Monad is dropped on IO.

  • A variable that carries Data alone defaults to Bool.

  • Anything else is an error with the sentence of the oracle, Cannot handle arbitrary overloaded top-level expressions.

A type variable without a constraint stays polymorphic. A functional type is accepted. The table never substitutes ().

class curry.typecheck.defaulting.Defaulting(scheme, substitution, typeexpr, instances, data_defaults)[source]

Bases: object

The result of the table on one scheme.

Variables:
  • scheme – The scheme as it came in.

  • substitution – A dict from the index of a type variable to the name of the Prelude type it was defaulted to: Int, Float, IO or Bool.

  • typeexpr – The type with the substitution applied. Apply of a defaulted constructor is folded (see normalize()).

  • instances – One pair per constraint of the scheme, in the order of the scheme: the constraint and the qualified name of the type constructor whose instance satisfies it, e.g., Prelude.Int.

  • data_defaults – The indices of the variables that carried Data alone and were defaulted to Bool.

property changed

Whether the table defaulted anything.

data_defaults
instances
scheme
show_type(module=None)[source]

The defaulted type in Curry syntax, e.g., Maybe Int.

substitution
typeexpr
exception curry.typecheck.defaulting.DefaultingError(message, scheme, predicate=None)[source]

Bases: CurryTypeError

The table cannot default the constraints of a scheme.

Variables:
  • scheme – The scheme.

  • predicate – The constraint the table rejected.

curry.typecheck.defaulting.default_scheme(scheme, what=None, hint='add a type signature', type_arity=None)[source]

Applies the table of the PAKCS REPL to a scheme.

Parameters:
  • scheme – A Scheme.

  • what – How the error names the goal, e.g., "expression '1+2'". The default names the symbol of the scheme.

  • hint – The last line of the error.

  • type_arity – A function from the qualified name of a type constructor to its declared arity, or None; normalize() uses it to fold Apply.

Returns:

A Defaulting.

Raises:

DefaultingError – A constraint the table cannot handle.

curry.typecheck.defaulting.normalize(typeexpr, type_arity=None)[source]

Folds Apply (T a1 .. an-1) an into T a1 .. an where the head is a type constructor. A substitution can put a constructor at the head of an Apply: return 5 has the type m a and the table binds m to IO, so the type is IO Int, not Apply IO Int. With type_arity, a function from the qualified name of a constructor to its declared arity, an application that would exceed the arity stays an Apply; without it every such application is folded.

curry.typecheck.defaulting.substitute(typeexpr, mapping)[source]

Replaces the type variables of mapping (index to type) in a type.