Skip to content

custom generator getting all kind of datatypes #50

@asmodehn

Description

@asmodehn

This is an error similar to what I encountered while trying to implement a linearspace typeclass on top of Witchcraft.Monoid.

Here is a minimal example to reproduce the issue:

# -- type_class_check.ex --
#
# HOW TO USE:
# From iex, enter these lines (minus the prompt)
# 
# iex> Mix.install([ {:witchcraft, "~> 1.0"}, {:type_class, "~> 1.2"} ])
# :ok
# iex> c "type_class_check.ex"

import TypeClass

defclass SomeClass do
  extend Witchcraft.Monoid

  alias Witchcraft.Semigroup

  where do
    def merge(arg1, arg2)
  end

  properties do
    def commutativity(data) do
      a = generate(data)
      b = generate(data)
      Semigroup.append(a, b) == Semigroup.append(b, a)
    end
  end
end

definst SomeClass, for: Integer do
  # custom_generator(data) do
  #   data |> IO.inspect()
  # end

  def merge(arg1, arg2) do
    arg1 * arg2
  end
end

This should compile without problem, as expected.

However uncommenting the custom generator will reveal all the different sort of data that is passed to it. It doesn't match the current implementation (Integer) as I would have expected...


&:erlang.is_number/1
%{
  -262 => %{-239 => <<23, 44, 40, 86, 8, 28, 19, 9, 23, 51, 52, 15>>},
  618 => -1.0225806451612902,
  "\eSJ)A,>PS" => 3.297872340425532,
  <<82, 23, 77, 79, 72, 26, 88, 5, 46, 33, 8, 6, 79, 14, 68, 23, 74, 81, 31, 6>> => "6"
}

== Compilation error in file type_class_check.ex ==
** (FunctionClauseError) no function clause matching in Witchcraft.Semigroup.Proto.Function.append/2    
    
    The following arguments were given to Witchcraft.Semigroup.Proto.Function.append/2:
    
        # 1
        &:erlang.is_number/1
    
        # 2
        %{-262 => %{-239 => <<23, 44, 40, 86, 8, 28, 19, 9, 23, 51, 52, 15>>}, 618 => -1.0225806451612902, "\eSJ)A,>PS" => 3.297872340425532, <<82, 23, 77, 79, 72, 26, 88, 5, 46, 33, 8, 6, 79, 14, 68, 23, 74, 81, 31, 6>> => "6"}
    
    Attempted function clauses (showing 1 out of 1):
    
        def append(f, g) when is_function(g)
    
    (witchcraft 1.0.4) lib/witchcraft/semigroup.ex:120: Witchcraft.Semigroup.Proto.Function.append/2
    type_class_check.ex:23: SomeClass.Property.commutativity/1
    (type_class 1.2.8) lib/type_class/property.ex:32: anonymous fn/5 in TypeClass.Property.run!/4
    (elixir 1.14.3) lib/stream.ex:1557: Stream.do_repeatedly/3
    (elixir 1.14.3) lib/enum.ex:3448: Enum.take/2
    type_class_check.ex:37: anonymous fn/2 in :elixir_compiler_6.__FILE__/1
    (elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    type_class_check.ex:37: (file)
** (CompileError)  compile error
    (iex 1.14.3) lib/iex/helpers.ex:204: IEx.Helpers.c/2
    iex:3: (file)

I was actually trying this as a simple workaround for #19 ...

Now I am guessing the error is related to this package and the design of custom_generator, but I am not used to type_class, so feel free to let me know if I am just holding this wrong, or if I should post this somewhere else (witchcraft itself ?).

Cheers !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions