h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
317 stars 23 forks source link

extra operations in typeclasses #121

Closed ghost closed 3 years ago

ghost commented 3 years ago
( typeclass anum
    (int|float :n ==> bool :r)
    (true @r)
) ::

Above signature seems extra operations that does. Only one must control the value for int or float. Its needless part for me: true @r -> It may hinder execution speed of code.

h3rald commented 3 years ago

Uhm... yes, two possibilities here:

Something like this:

 (
   typealias my-num
   (int|flt ==> bool)
 )

Note that in this case there wouldn't be any need for capturing symbols in the signature either, I guess.

ghost commented 3 years ago

there wouldn't be any need for capturing symbols in the signature either, I guess.

Typealiases would be extra option and truthly you don't wanna use all builtin types(for example) within operator signatures, this would be nasty.

I prefer (builtin :b) to (int|float|string|...).

allow you to omit the body quotation in typeclass operator (and in that case return true if the input is processed correctly)

This method looks more perfect. Because of eval performings. Do you know lifters? You may see them in nim. There is possible to make them in min, too. typeclass and typealias keywords would be nightmare of lifters. Because two symbols means extra handlings for lifters: "Lifter: Hmm... Which one must i choose of them? typeclass or typealias?"

h3rald commented 3 years ago

Lifters? You mean lambda lifting?

But yes maybe a typealias would be a lightweight operator to rename type unions, or even standard types... it could be useful in certain contexts perhaps.

ghost commented 3 years ago

Lifters? You mean lambda lifting?

Yep see #123 (there is an example about the topic)

h3rald commented 3 years ago

I was thinking of allowing an even shorter syntax for type classes' instead of introducing type aliases.

This would keep the same type of operator (typeclass) for essentially the same thing (defining a type constraint).

I thought of an even shorter syntax:

(
  typeclass warm-temperature 
  twenty-plus&thirty-minus
) :: 

where for example twenty-plus and thirty-minus are type classes defined as:

(
  typeclass twenty-plus
  (int :t ==> book: :r)
  (t 20 >= @r) 
) ::

(
  typeclass thirty-minus
  (int :t ==> book: :r)
  (t 30 <= @r) 
) ::

Thoughts? Actually I could just use a symbol, but because the result is still a typeclass, I prefer to keep the same type of operator with a simplified syntax.

The obvious alternative would be something like:

'twenty-plus&thirty-minus 'warm-temperature typealias

Much simpler to implement... the only thing that I don't like is that it would return typeclass:warm-temperature. But it's much shorter.

Which syntax would you like the most?

ghost commented 3 years ago

I'd like two of syntaxes(all)! 🙃

h3rald commented 3 years ago

Well, the typealias syntax won. Very lightweight implementation, and you can potentially keep creating aliases of aliases and they will be resolved hopefully (and yes of course there's also possibility of infinite recursion if you are not careful).

In the end it generates a special typealias: symbol that is set to a string corresponding to the specified type expression. Then all lookups are performed in expects and operator signatures as needed.

Did some quick tests and it seems to work alright, now I just need to document it.

h3rald commented 3 years ago

Implemented in v0.32.0.