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
-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()}.
-type typeclass_env() :: #typeclass_env{classes :: #{atom() => typeclass_info()}, instances :: #{{atom(), [term()]} => instance_info()}, instance_index :: #{atom() => [{atom(), [term()]}]}, derive_rules :: #{atom() => fun()}}.
-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
-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 checkTypeEnv- 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
-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 checkEnv- Current typeclass environment
Returns
ok- No overlap detected{error, {overlapping_instance, ExistingInstance}}- Overlap detected
-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 typeclassType- Type to find instance for (may contain type variables)Env- Typeclass environment
Returns
{ok, InstanceInfo, Substitution}- Instance found with type variable substitutionnot_found- No matching instance{ambiguous, [InstanceInfo]}- Multiple instances could match
-spec get_all_instances(atom(), typeclass_env()) -> [instance_info()].
Gets all instances for a given typeclass.
Arguments
TypeclassName- Name of the typeclassEnv- Typeclass environment
Returns
[InstanceInfo]- List of all instances for the typeclass
-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).
-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 typeclassTypeArgs- List of type argumentsEnv- Typeclass environment
Returns
{ok, InstanceInfo}- Instance foundnot_found- No matching instance
-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 foundnot_found- Typeclass not registered
-spec new_env() -> typeclass_env().
Creates a new empty typeclass environment.
Returns
typeclass_env()- New empty environment
Example
Env = cure_typeclass:new_env().
-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 nodeEnv- 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).
-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 nodeEnv- 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).
Resolves a method call to its implementation.
Arguments
TypeclassName- Name of the typeclassMethodName- Name of the methodReceiverType- Type of the receiverEnv- Typeclass environment
Returns
{ok, Implementation}- Method implementation found{error, Reason}- Resolution failed
-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 undefinedEnv- 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).