View Source cure_typeclass (cure v0.7.0)

Summary

Functions

Checks if typeclass constraints are satisfied.

Checks if a new instance would create overlapping instances.

Finds an instance by unification (supports type variables).

Gets all instances for a given typeclass.

Lists all registered typeclass names in the environment.

Looks up an exact instance by typeclass name and type arguments.

Looks up a typeclass by name.

Creates a new empty typeclass environment.

Registers a typeclass instance in the environment.

Registers a typeclass definition in the environment.

Resolves a method call to its implementation.

Validates where clause constraints for a function.

Types

instance_info()

-type instance_info() ::
          #instance_info{typeclass :: atom(),
                         type_args :: [term()],
                         constraints :: [typeclass_constraint()],
                         method_impls ::
                             #{atom() =>
                                   #function_def{name :: term(),
                                                 type_params :: term(),
                                                 clauses :: term(),
                                                 params :: term(),
                                                 return_type :: term(),
                                                 constraint :: term(),
                                                 body :: term(),
                                                 where_clause :: term(),
                                                 is_private :: term(),
                                                 location :: term()}},
                         source_location :: term()}.

typeclass_constraint()

-type typeclass_constraint() ::
          #typeclass_constraint{typeclass :: term(), type_args :: term(), location :: term()}.

typeclass_env()

-type typeclass_env() ::
          #typeclass_env{classes :: #{atom() => typeclass_info()},
                         instances :: #{{atom(), [term()]} => instance_info()},
                         instance_index :: #{atom() => [{atom(), [term()]}]},
                         derive_rules :: #{atom() => fun()}}.

typeclass_info()

-type typeclass_info() ::
          #typeclass_info{name :: atom(),
                          type_params :: [atom()],
                          superclasses ::
                              [#typeclass_constraint{typeclass :: term(),
                                                     type_args :: term(),
                                                     location :: term()}],
                          methods :: #{atom() => term()},
                          default_impls ::
                              #{atom() =>
                                    #function_def{name :: term(),
                                                  type_params :: term(),
                                                  clauses :: term(),
                                                  params :: term(),
                                                  return_type :: term(),
                                                  constraint :: term(),
                                                  body :: term(),
                                                  where_clause :: term(),
                                                  is_private :: term(),
                                                  location :: term()}},
                          kind :: term()}.

Functions

check_constraints(Constraints, TypeEnv, TypeclassEnv)

-spec check_constraints([typeclass_constraint()], term(), typeclass_env()) ->
                           ok | {error, [term()]} | {pending, [term()]}.

Checks if typeclass constraints are satisfied.

Arguments

  • Constraints - List of typeclass constraints to check
  • TypeEnv - Type environment (for type variable resolution)
  • TypeclassEnv - Typeclass environment

Returns

  • ok - All constraints satisfied
  • {error, UnsatisfiedConstraints} - Some constraints not satisfied
  • {pending, PendingConstraints} - Constraints depend on type variables

check_instance_coherence/2

-spec check_instance_coherence(#instance_def{typeclass :: term(),
                                             type_args :: term(),
                                             constraints :: term(),
                                             methods :: term(),
                                             location :: term()},
                               typeclass_env()) ->
                                  ok | {error, term()}.

Checks if a new instance would create overlapping instances.

Ensures the global uniqueness property required for type class coherence.

Arguments

  • NewInstance - The instance to check
  • Env - Current typeclass environment

Returns

  • ok - No overlap detected
  • {error, {overlapping_instance, ExistingInstance}} - Overlap detected

find_instance/3

-spec find_instance(atom(), term(), typeclass_env()) ->
                       {ok, instance_info(), map()} | not_found | {ambiguous, [instance_info()]}.

Finds an instance by unification (supports type variables).

Arguments

  • TypeclassName - Name of the typeclass
  • Type - Type to find instance for (may contain type variables)
  • Env - Typeclass environment

Returns

  • {ok, InstanceInfo, Substitution} - Instance found with type variable substitution
  • not_found - No matching instance
  • {ambiguous, [InstanceInfo]} - Multiple instances could match

get_all_instances/2

-spec get_all_instances(atom(), typeclass_env()) -> [instance_info()].

Gets all instances for a given typeclass.

Arguments

  • TypeclassName - Name of the typeclass
  • Env - Typeclass environment

Returns

  • [InstanceInfo] - List of all instances for the typeclass

list_typeclasses/1

-spec list_typeclasses(typeclass_env()) -> [atom()].

Lists all registered typeclass names in the environment.

Arguments

  • Env - Current typeclass environment

Returns

  • [atom()] - List of typeclass names

Example

Names = cure_typeclass:list_typeclasses(Env).

lookup_instance/3

-spec lookup_instance(atom(), [term()], typeclass_env()) -> {ok, instance_info()} | not_found.

Looks up an exact instance by typeclass name and type arguments.

Arguments

  • TypeclassName - Name of the typeclass
  • TypeArgs - List of type arguments
  • Env - Typeclass environment

Returns

  • {ok, InstanceInfo} - Instance found
  • not_found - No matching instance

lookup_typeclass/2

-spec lookup_typeclass(atom(), typeclass_env()) -> {ok, typeclass_info()} | not_found.

Looks up a typeclass by name.

Arguments

  • Name - Typeclass name (atom)
  • Env - Typeclass environment

Returns

  • {ok, TypeclassInfo} - Typeclass found
  • not_found - Typeclass not registered

new_env()

-spec new_env() -> typeclass_env().

Creates a new empty typeclass environment.

Returns

  • typeclass_env() - New empty environment

Example

Env = cure_typeclass:new_env().

register_instance/2

-spec register_instance(#instance_def{typeclass :: term(),
                                      type_args :: term(),
                                      constraints :: term(),
                                      methods :: term(),
                                      location :: term()},
                        typeclass_env()) ->
                           {ok, typeclass_env()} | {error, term()}.

Registers a typeclass instance in the environment.

Arguments

  • InstanceDef - The instance definition AST node
  • Env - Current typeclass environment

Returns

  • {ok, NewEnv} - Updated environment
  • {error, Reason} - Registration failed (e.g., overlapping instance)

Example

InstanceDef = #instance_def{typeclass = 'Show', ...},
{ok, NewEnv} = cure_typeclass:register_instance(InstanceDef, Env).

register_typeclass/2

-spec register_typeclass(#typeclass_def{name :: term(),
                                        type_params :: term(),
                                        constraints :: term(),
                                        methods :: term(),
                                        default_methods :: term(),
                                        location :: term()},
                         typeclass_env()) ->
                            {ok, typeclass_env()} | {error, term()}.

Registers a typeclass definition in the environment.

Arguments

  • TypeclassDef - The typeclass definition AST node
  • Env - Current typeclass environment

Returns

  • {ok, NewEnv} - Updated environment
  • {error, Reason} - Registration failed

Example

TypeclassDef = #typeclass_def{name = 'Show', ...},
{ok, NewEnv} = cure_typeclass:register_typeclass(TypeclassDef, Env).

resolve_method(TypeclassName, MethodName, ReceiverType, Env)

-spec resolve_method(atom(), atom(), term(), typeclass_env()) -> {ok, term()} | {error, term()}.

Resolves a method call to its implementation.

Arguments

  • TypeclassName - Name of the typeclass
  • MethodName - Name of the method
  • ReceiverType - Type of the receiver
  • Env - Typeclass environment

Returns

  • {ok, Implementation} - Method implementation found
  • {error, Reason} - Resolution failed

validate_where_constraints/2

-spec validate_where_constraints(#where_clause{constraints :: term(), location :: term()} | undefined,
                                 typeclass_env()) ->
                                    ok | {error, term()}.

Validates where clause constraints for a function.

Verifies that:

  • All referenced typeclasses exist
  • Type arguments in constraints are valid
  • Arity of constraint matches typeclass definition

Arguments

  • WhereClause - The where_clause record or undefined
  • Env - Typeclass environment

Returns

  • ok - All constraints are valid
  • {error, Reason} - Validation failed

Example

WhereClause = #where_clause{constraints = [...]},
{ok} = cure_typeclass:validate_where_constraints(WhereClause, Env).