timCF
Hello guys! I implemented another way to create new data types (which are not structs or records) in Elixir. Please check out readme file if you are interested, everything is described in detail with examples:
https://github.com/timCF/calculus
Thanks!
Trending in Announcing
Hey everyone!
Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application.
This library uses Erlang esaml to provide
plug enabl...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Other Trending Topics
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./
The firs...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Oh hey, this looks awesome!
I so agree, it is, by definition, A Constructor of the type.
This is mostly caused by elixir being deficient in its typing system, which comes from it modeling after erlang instead of trying to fix that bug in erlang.
And then Full Lambda Calculus! Church Encoding! Lol. As I’m reading through I’m curious when church encoding of integers will come up, or the various combinators. ^.^
This looks like a data driven DSEL I saw in Elixir, though function wrapping.
Heh, cute, I’m guessing you are using a
make_ref()for the security key as they are unique references in the beam world? I’ve not looked at the code yet.Yep definitely figured that at the start.
Not ‘as’ bad as expected though, that’s within the realm of usability considering structs are raw beam code…
Ooo, my favorite things! ^.^
I have a couple of ideas for it to work with my ProtocolEx library though, hmm… If only Tuple Calls weren’t removed from the BEAM! That still bugs me to no end… >.>
Eh, there are ways to but it would slow down the implementation more so not sure it is worth it…
Ehhhh, actually you can… ^.^;
I’m leaving work right now or I’d show you how you can break this.
Ping me tomorrow if you are curious, but in essence you can create almost any internal beam type with
:erlang.binary_to_term, including picking it apart with the inverse.This is a fun looking project though, no church encoding, just lambda encapsulation though, lol. ^.^
timCF
Thanks for review
No, it is just 64 random bytes converted to atom and inlined to all methods and private expressions in compile-time:
https://github.com/timCF/calculus/blob/0004cc95e6cfb9317bbbaf4621256d8fd937764b/lib/calculus.ex#L69
I think pattern matching on inlined atom is the most performant way how I can reach desired behaviour
I’ll check it out
Oh, this is probably real if you know binary format of erlang terms) And analysis of beam bytecode of the module is real thing as well.
But anyway, this is pretty hard if we compare it with usage of default constructor for pattern mathcing or updating internal data
Yes, but I got inspiration from Church Encoding - it’s soo good thing. Gives feeling that you have the POWER hahaha
Eiji
I really do not recommend this as BEAM have limit of atoms (
1_048_576by default) and this could cause problems in large scaling.I think it’s bad practice to add extra use if only import is called.
Consider using this:
instead of:
Generic code could be imported instead.
Here is my proposition how it could look like:
In my example
use Calculusshould do:@before_compilemodule attributecalculusmacro accepts 2 arguments:Ecto.QueryAPI)a)
arg: passed extra argument (we could optionally add args option here as well)b)
return: returns binding or literalc)
state: changes stated)
when: defines a guard(s)Finally
@before_compilecallback would finalize all definedcalculusesPlease let me know what do you think about it.
lpil
This is the same cost of a single atom per type as with structs or records, so probably not a problem
OvermindDL1
I’d say just use
make_ref, it already makes a beam-wide unique value and it’s fast.Although you’ll need to make it in the construct call then, it can’t be put in a module (as it’s unique, it’s not reloadable without term_to_binary/binary_to_term’ing it).
It’s basically like Elixir’s Protocol’s, except it works with matchspecs, in addition to a variety of other features such as controlling ordering, various types of fallbacks, can even write compile-time tests to ensure that the implementations implement it properly.
Very true. ^.^
You know, a full proper unique type that could not be broken apart or anything would be a NIF resource.
Hehe, raw lambda work is so much fun, it’s like a puzzle on how to encode so many things. ^.^
Eh, but it’s only once per ‘module’ so not really an issue. A
make_ref‘per’ constructor wrapper would work very well though!timCF
I don’t think that
make_reffunction call is faster then inlined atom literal, pattern matching on atom literals is extremely fast thingAnyway, I don’t know how I can use
make_refin runtime for encapsulation, because I need thing which is known by value of λ-type and which is known by method (just function in module, thisevalprivate expression). There are actually 2 checks:first checks origin of
λ-term(fact that is was created in correct module), it’s for encapsulation of module itself calculus/lib/calculus.ex at 0004cc95e6cfb9317bbbaf4621256d8fd937764b · 21it/calculus · GitHubsecond check happens when
λ-termis called with @security_key as 2nd argument, it’s for encapsulation of termcalculus/lib/calculus.ex at 0004cc95e6cfb9317bbbaf4621256d8fd937764b · 21it/calculus · GitHub
timCF
Can I write NIFs in Haskell?
OvermindDL1
As are REF’s, they are just essentially just boxed integers, so it’s a single extra unboxing cost (which will likely to be optimized out anyway because identical pointers).
It would be saved ‘into’ the closure itself.
As long as Haskell’s GC doesn’t get in the way and Haskell can write C-style dynamic libraries with C-style calls.
mindriot
Way out of my depth here as I’ve only been learning the basics of Haskell however I asked myself the same question about writing NIFs and I through a bit of googling I found an interesting package for writing Erlang nodes in Haskell. I’m a bit too early on to really try it out but if you are looking at trying to bring the two together it might be worth looking into as an alternative to NIFs hinterface: Haskell / Erlang interoperability library
timCF
But these code examples are not the same things. Your example is less performant because there is call of access protocol in runtime. And it’s also less safe because something like
calsulus(state: state, returnnnnn: return)will survive compilation. And the worst thing will happen in runtime - access protocol will return implicitnilfor this value, and consequences of this are unpredictable, depends on other code - sometimes it can behave correct, sometimes can behave incorrect, sometimes can raise exceptions.So my code is just “inline implementation” of named arguments (which not exist in Elixir by default). I did another library for this, but don’t want extra dependency just for one expression:
https://github.com/coingaming/defnamed
About other your suggestions about syntax sugar - it’s really sugar, question of preferences in design. Maybe it make sense for someone, maybe not, I don’t know. I thought about DSL with
type,private,public,immutableandmethodkeywords - to avoid boilerplate and auto-generate at least getters, maybe some setters. But finally just decided to make interface as much simple and explicit as possible, with smallest possible amount of abstractions. If I, or someone else needs more high-level DSL - he can build it on top of my library pretty easy, because interface is straightforward.