Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom generator getting all kind of datatypes #50

Open
asmodehn opened this issue Mar 7, 2023 · 1 comment
Open

custom generator getting all kind of datatypes #50

asmodehn opened this issue Mar 7, 2023 · 1 comment

Comments

@asmodehn
Copy link

asmodehn commented Mar 7, 2023

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 !

@cognivore
Copy link

I think it should be

TypeClass.Property.Generator.generate(42) |> IO.inspect()

Try it and see if it works. It doesn't know that you're defining some instance for an Integer, but if you give it a sample of the datatype you're working with, it should dispatch correctly.

In general, that's how a lot of things are treated in Witchcraft library family: by sampling!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants