View Source cure_typeclass_codegen (cure v0.7.0)

Summary

Functions

Compiles an instance definition to BEAM code.

Compiles a typeclass definition to BEAM code.

Generates a callable function for a typeclass method dispatch.

Generates instance registration code for automatic registration on module load.

Processes a derive clause and generates instances.

Functions

compile_instance/2

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

Compiles an instance definition to BEAM code.

Instances compile to regular Erlang functions with specialized naming:

  • show_Int/1 for Show(Int).show
  • eq_Point/2 for Eq(Point).==
  • compare_Person/2 for Ord(Person).compare

Arguments

  • InstanceDef - Instance AST node
  • State - Codegen state

Returns

  • {ok, CompiledFunctions, NewState} - List of compiled functions
  • {error, Reason} - Compilation error

compile_typeclass/2

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

Compiles a typeclass definition to BEAM code.

Typeclasses compile to Erlang behaviour modules with:

  • behaviour_info/1 callback specification
  • Default method implementations
  • Method signature documentation

Arguments

  • TypeclassDef - Typeclass AST node
  • State - Codegen state

Returns

  • {ok, CompiledModule, NewState} - Compiled typeclass module
  • {error, Reason} - Compilation error

generate_instance_function(TypeclassName, MethodName, Instances, State)

-spec generate_instance_function(atom(), atom(), [term()], term()) -> {ok, term()} | {error, term()}.

Generates a callable function for a typeclass method dispatch.

Creates wrapper functions that dispatch to the correct instance:

show(X) when is_integer(X) -> show_Int(X);
show(X) when is_float(X) -> show_Float(X);
show(X) when is_list(X) -> show_List(X).

Arguments

  • TypeclassName - Name of the typeclass
  • MethodName - Name of the method
  • Instances - List of registered instances
  • State - Codegen state

Returns

  • {ok, DispatchFunction} - Generated dispatch function
  • {error, Reason} - Generation error

generate_instance_registration(TypeclassName, TypeName, CompiledMethods, State)

-spec generate_instance_registration(atom(), atom(), [term()], term()) ->
                                        {ok, term(), term()} | {error, term()}.

Generates instance registration code for automatic registration on module load.

This creates an -on_load attribute and registration function that automatically registers the instance with cure_instance_registry when the module is loaded.

Arguments

  • TypeclassName - Name of the typeclass (e.g., 'Show', 'Eq')
  • TypeName - Name of the type (e.g., 'Int', 'List')
  • CompiledMethods - List of compiled method functions
  • State - Codegen state

Returns

  • Registration code structure with on_load hook

process_derive_clause/3

-spec process_derive_clause(#derive_clause{typeclass :: term(),
                                           typeclasses :: term(),
                                           for_type :: term(),
                                           constraints :: term(),
                                           location :: term()},
                            #record_def{name :: term(),
                                        type_params :: term(),
                                        fields :: term(),
                                        derive_clause :: term(),
                                        location :: term()},
                            term()) ->
                               {ok, [term()], term()} | {error, term()}.

Processes a derive clause and generates instances.

Called during record definition compilation to automatically generate typeclass instances using the derive mechanism.

Arguments

  • DeriveClause - Derive clause AST node
  • RecordDef - Record definition being derived for
  • State - Codegen state

Returns

  • {ok, GeneratedInstances, NewState} - List of generated instance functions
  • {error, Reason} - Derivation error