h3rald / min

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

Constraint Adding #73

Closed ghost closed 3 years ago

ghost commented 3 years ago

Now, there are some native types (int float string ...) . However sometimes these aren't types what we needed. For example natural type. It accepts only natural numbers not negative numbers.

How can be adding special types in Min? How can apply our constraints within operator definition?

h3rald commented 3 years ago

Ohhh nice idea! It would be cool to define your own type definitions, for example with a quotation containing the checks to perform. This would be similar to Haskell's typeclasses I guess.

Is that what you are after? Maybe something like this:

(:n ((n int?) (n 0 >)) &&) 'natural typedef

so that you could write:

(
  symbol natural-sum
  (natural :n natural :m == natural :result)
  (n m + @result)
) ::

It could be a nice feature indeed!

ghost commented 3 years ago

Yes, you true. Special types are really need for Min. And your natural definition could be my definition probably. I think as you about natural definition and special type definition. You must be quick, i'm impatient at this time. I'm waiting you with my tea 🍵 You can add the feature until i am drinking my tea (i say you're very fast.) 😀

h3rald commented 3 years ago

Haha not that fast! 🤣

I am developing my OSS projects in my free time, so that basically means a bit during weekends and at night when my daughter wakes up for feeding -- and that coding is done mostly by ssh'ing into my MacBook from my iPhone!

But yes, I'll try to implement it soon. It is doable, it's a matter of exposing some way to define constraints (basically via a quotation) and wiring them up with the operator and expect symbols. It can be done. Not sure how much performance overhead all these checks will add though... but that's another problem 😊

ghost commented 3 years ago

@h3rald

Hey h3rald! How can we use special types as constraints? Can you give an example please? I am preparing an example for you, for an opened issue. But this will be a future-included example so i must know how can i use constraints in a operator symbol definition.

Another que.: If we use constraints then Must we delete them via delete symbol??

h3rald commented 3 years ago

Sure, here:

; The following defines a special type:natural symbol 
(:n ((n integer?) (n 0 >)) &&) 'natural typeclass

; type: prefixed symbols (which can only be created with the typeclass operator) 
; can be used as types inside a signature quotation, without the type: prefix
(
  symbol natural-sum
  (natural :n natural :m ==> natural :result)
  (n m + @result)
) ::

Hopefully I will find some time to document all this properly soon!

h3rald commented 3 years ago

And well.. they are symbols, like any other... so you can keep them in the scope or delete them, up to you.

ghost commented 3 years ago

@h3rald

(:n ( (n integer?) (n 0 >) ) && ) 'natural typeclass

( symbol natural-sum
    (natural :n natural :m ==> natural :result)
    (n m + @result)
) ::

; But now i don't use the natural symbol.
; Because i use it for constrainting.
; This is a scrap at the moment:
'type:natural delete
ghost commented 3 years ago

I think this would be better than before:

( symbol natural-sum
    (
        ((:n ( (n integer?) (n 0 >) ) && ) 'natural typeclass) :n
        natural :m ==> natural :result
    )
    (n m + @result)
) ::
h3rald commented 3 years ago

Noooo it has to exist ins the same scope as the operator(s) that use it for the constraint to work! If you delete it and you then use natural in a signature, you will get an error saying invalid type!

The idea is that you probably want to reuse it... those could be general constraints as well, like natural.

ghost commented 3 years ago

Noooo it has to exist for the constraint to work! If you delete it and you then use natural in a signature, you will get an error saying invalid type!

Some types only declared for some symbols because of their specialities. When we delete it at the end of operator definition, then we are blocking ambiguity of calling it mistakenly.

ghost commented 3 years ago

I think this would be better than before:

( symbol natural-sum
  (
      ((:n ( (n integer?) (n 0 >) ) && ) 'natural typeclass) :n
      natural :m ==> natural :result
  )
  (n m + @result)
) ::

Isn't this good enough? We're extending constraint application(current: general constraints as you say) via special constraints.

ghost commented 3 years ago

Parameters section(quotation) of operator signature should have a typeface. That makes parameters section could have its own types too. ---> Am i false again????? 😫

h3rald commented 3 years ago

Well, let's put it in this way:

What you are showing in the last example doesn't work right now, but it will hopefully work when I implement generics... 😊👍

h3rald commented 3 years ago

What you are showing in the last example doesn't work right now, but it will hopefully work when I implement generics... 😊👍

No wait, I am getting confused now... generics should be simpler, more more like this:

(string|num|quot :t) :a

Not full quotations... I think 🤔

ghost commented 3 years ago

What you are showing in the last example doesn't work right now.

I gave a example from FUTURE-USAGE of Min with implementing your ideas IN.

You REALLY don't understand??? Is there anybody hearing my voice??? AAAAAAAAAA..!!!!

I'm I'm very tired of this topic. Do it as you thought. We say our solutions for the topic to each other. I am dropping talking for the topic. You are the author and you can improve Min lang. I don't want to be a block on your road. You must go on. 👍 👍 👍 OKAY??

h3rald commented 3 years ago

Added support for type classes in v0.29.0