View Source cure_derive (cure v0.7.0)

Summary

Functions

Checks if a typeclass can be automatically derived for a type.

Derives an Eq instance for a record type.

Automatically derives a typeclass instance for a given type.

Derives an Ord instance for a record type.

Derives a Show instance for a record type.

Functions

can_derive/2

-spec can_derive(atom(), term()) -> boolean().

Checks if a typeclass can be automatically derived for a type.

Arguments

  • TypeclassName - Name of the typeclass
  • ForType - Type to check

Returns

  • true - Can be derived
  • false - Cannot be derived

derive_eq/3

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

Derives an Eq instance for a record type.

Generates equality check that compares all fields structurally.

derive_instance(TypeclassName, ForType, Constraints, TypeEnv)

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

Automatically derives a typeclass instance for a given type.

Arguments

  • TypeclassName - Name of the typeclass to derive (e.g., 'Show', 'Eq')
  • ForType - Type expression to derive instance for
  • Constraints - Required constraints for the instance
  • TypeEnv - Type environment with type definitions

Returns

  • {ok, InstanceDef} - Generated instance definition
  • {error, Reason} - Cannot derive instance

Example

{ok, ShowInstance} = cure_derive:derive_instance(
    'Show', 
    #record_type{name = 'Person'}, 
    [],
    TypeEnv
).

derive_ord/3

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

Derives an Ord instance for a record type.

Generates lexicographic ordering based on field order. Requires Eq instance (superclass).

derive_show/3

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

Derives a Show instance for a record type.

Generates a show method that displays the record as: RecordName { field1: value1, field2: value2 }