Is anyone here using Witchcraft?

I am curious because Witchcraft (and related projects like Algae) can be super useful when for example you are working with complex data structures, yet I almost never see people mentioning it in this forum which makes me wonder, why not? :slightly_smiling_face:

For those who are not familiar with this project, here is a great introduction to it by its author:

6 Likes

Witchcraft is a very cool library but without a static type system I personally don’t enjoy these design patterns as much as I do in languages like Haskell. I’d love to see this alongside a statically typed Elixir :slight_smile:

4 Likes

super useful when for example you are working with complex data structures

Do you have an example of such a data structure and how this would apply?

if you not composing multiple function
most of the time just pattern matching is easier to write and understand
also agree with @lpil that without static type, these structure will be more difficult to understand at first glance

1 Like

Seconding @hassan’s query: are there examples of this?

The examples in the documentation mostly use numbers, which are simple to use in doctests and automatically satisfy a bunch of properties - but that obscures why a user would want to distinguish between (say) a semigroup and a monoid (since numbers always have zero).

IMO the library would get more attention if it was clearer what problems it makes easier to solve - right now, it mostly seems like using Witchcraft is a way to make Elixir look more like Haskell.

3 Likes

At times I do, both in using and mentioning it, lol.

I don’t really push it out hard because it’s not what most people here are familiar with, Elixir is functional’ish, but it doesn’t really follow the modern functional styles, so it doesn’t fit with ‘other’ things very cleanly.

Yeah this is the big big thing.

2 Likes

Apologies for the delayed response, it was an intense week…

Yeah, I mean I agree that having a type checker is helpful, but I found some joy in using these sort of linguistic tools in a language that’s not (a) Haskell :slight_smile:

When one is working for instance with recursive data types in languages where immutability is the norm (like in most functional programming languages) many ideas from category theory come very useful as they let you create sound generalizations on how to process data which can be independent from their types and also easy to compose. I would love to write an article on “categorical elixir” full of real world examples but right now I barely have the time to type this post, and besides, I would need to find a very patient reviewer as I’m likely not the right person for such task :sweat_smile:

But if you are OK with some (simple) Haskell you may like Patrick Thomson’s introductory series on Recursion Schemes.

Cheers!

2 Likes

We are currently experimenting with replacing standard stack with Witchcraft.

In our experience, it reduces boilerplate so much and allows to get straight to the point, as well as significantly increasing maintainability of the code.

Of course, we’re looking forward and intend to continue to contribute to Gleam ecosystem and we’re keeping a keen eye on purerl, but in the meantime and on “lean startup” budgets, we think that Witchcraft is a good foundation that gives us the best of both worlds.

P.S.
We found that as far as typeclasses are concerned, witchcraft strongly suggesting on putting proptests close to typeclass definitions makes us write way less nonsense than we would in Haskell.

And we wrote a lot of nonsense in Haskell over the course of our careers :slight_smile:

Eager to see a real world example.

I’d personally never (or only with very obvious benefits) use it my code. It’s hard enough to find Elixir-devs like it is, if you add that to your code It’ll become impossible (… or maybe I’m wrong and you attract all the geniuses…?)

2 Likes

real world example

Here are three examples from the devops engineering project we’re currently working on:

Any time you write Enum.map followed by Enum.into, you should instead use map from Witchcraft.

Sometimes, you need to pipe into second argument of a binary function, having flip is convenient.

Sometimes, you work with a macro that wants to have a single do-block. Having fix at hand is convenient for easy implementaiton of anonymous recursion.


only with very obvious benefits

The first real world example is enough of a reason, according to you, to use Witchcraft! :wink:


Regarding hiring: I feel like we’re in the market for different kind of Elixir developers than most, but I wouldn’t label them as “geniuses”. It’s just a matter of taste in engineering and a matter of skill in different methodologies of building systems. Most of us are coming to Elixir from Haskell, which, arguably, harbours less “genius” people, as instead of working out everything oneself, Haskellers heavily rely on their compiler to aid them in figuring out what does make sense and what doesn’t. But I think it’s really not on-topic here. Witchcraft is practical and should be used by everyone in one capacity or another.

map, flip and fix are handy, but I can live with into and then.

What I find intimidating and interesting about witchcraft is more this stuff:

Semigroupoid  Semigroup  Setoid   Foldable   Functor -----------┐
     ↓           ↓         ↓         ↓      ↙   ↓   ↘           |
  Category     Monoid     Ord    Traversable  Apply  Bifunctor  |
     ↓                                       ↙    ↘             ↓
   Arrow                            Applicative   Chain       Extend
                                             ↘    ↙             ↓
                                              Monad           Comonad
1 Like

You’re already using all that stuff, what’s intimidating is that you need some background to know their formal names.

For example, say you want to perform some task in parallel, so you need to split the work, but in order to split it and make it the same as doing it serially, you need to satisfy that splitting [abc] into [ab][c] is the same as [a][bc] under the operation you want to perform. In other words, you can arbitrarily split the task and it’s not order dependent.That’s a practical application of a monoid. There’s a Rich Hickey talk about this but I can’t recall which one, either the one about transducers or the one about reducers, he mentions this property about parallelizable work and says something like “and this is a monoid” and chuckles.

Or you have stuff inside some context, like [a, b] or {:ok, a}, and you want a function that can apply a function to these wrapped values. That’s map, and the map+context is what defines a functor. If the context can be 2 values, like {:ok, a} and {:error, b}, and map takes a function for each case, then it’s a bifunctor.

If you have a wrapped value {:ok, a} and a function that takes a value a and returns {:ok, b}, applying map twice would return {:ok, {:ok, b}}, so you want a function that applies the function and unwraps the result, that’s flat_map/bind, and it’s what characterizes a monad.

Foldables are comparable to an Enum that you can reduce, and it’s why in Erlang it’s named :lists.foldl.

And so on and so forth. If you know the background for these names, then it’s simple and useful. If you don’t, then you end up implementing ad-hoc versions of them all the time.

What IS weird about them in Elixir is that they are mostly formalized with regards to functions, not data, so a lot of it is about composing functions, and more so in environments where functional composition is the only way you have to write a program(Haskell works this way). In Elixir we have higher order functions but we don’t have auto currying so it becomes weird to properly implement that programming style.

2 Likes