201 points · by nnx · 2y ago · 81 comments
authority = [userinfo "@"] host [":" port]
The simple extension of allowing a port makes it a more broadly useful type, since authorities like above appear all over the place. Email is just one protocol after all.
There is also a url type, but it is unclear how general it is (just http/s?). There is a cpath type which corresponds to the path in a URI as well.
URI = scheme ":" ["//" authority] path ["?" query] ["#" fragment]
In Rye typing, authority _could_ be the authority described above, and cpath already covers path. The rest are strings as they don't have generically defined syntax.
Perhaps this is a bit of a quibble but it seems to me like if URIs and their structured components (cpath is already there, and email is so close!) were core types rather than just email and url, it opens up a lot of use cases.
The idea of making them more general certainly makes sense. Rye is "end-user" focused so it would maybe still be called email. URI-s right now can have different schemes. Scheme defines the kind of url and Rye has generic functions that dispatch on the kind of first argument. So there is:
open file://readme.txt and open sqlite://db.s3db and open postgres://user@localhost/database "pwd" ...
%path/notes.txt is also Rebolism and it's a shorthand for file://path/notes.txt
Thank you. I will think about this
cpath is currently a little specific, at least in behaviour as it's a context path to a word.
Thank you for making this comment. I will think more about it and certanly reread it when I work on there datatypes next time.
The Python version uses intermediate variables so the author of the code is to blame for verbosity, not the language.
I think I wrote a script I needed in Rye and I found it interesting, then I went to Fiver and paid someone to write a script in Python that does the same.
2021 were different times, and I hope I've grown a little too... and now I would ask ChatGPT or Gemini :)
The code examples are a tad too verbose but actually better quality than most real-world Python codebases, I would say.
Python is not very verbose but it's not very concise either (especially compared to Lisp families)
Variables also help readability, because the name can help you discern what those functions return.
But using long chains of expressions is the same as one-liners or point-free style in Haskell. It saves some typing and also you can skim the code more easily, but only if you're extremely familiar with what's going on there.
I wonder how much you can benefit from this, if you return to the codebase after a 6-month break though. Maybe some people do manage to really memorise these details, but for some of us the effect is more like "wtf is this code doing?"
How does it implement and, or, or if?
https://ryelang.org/meet_rye/basics/if_either/
The tricky thing about if, and, and or --- the reason you can't implement them as functions in most languages --- is that they need to not evaluate all their arguments immediately. Otherwise:
// Would print!
if(false, print("oops!"))
// Would throw an error if the key is not present
and(my_hashmap.has_key("key"), my_hashmap["key"])
The way that ryelang gets around this is that you pass the arguments in a "code block" surrounded by "{}", which delays its evaluation. So you write: // Does not print, because *if* never runs its code block arg
if 0 { print("oops!") }
// There's no example of *and* anywhere but my guess is you'd write this:
and { my_hashmap.has_key("key") } { my_hashmap["key"] }Like Lisp’s QUOTE which has to be a special form.
[1] https://maxima.sourceforge.io/
[2] https://feb.kuleuven.be/public/u0003131/WBT23/wxMaxima/wxM_i...
[3] https://feb.kuleuven.be/public/u0003131/WBT23/wxMaxima/wxM_i...
[4] although (in my limited experience) maxima sometimes refuses to evaluate anyway even if you do the double quote thing
The code block approach is widely applied in two massively used industrial languages though: Ruby and Kotlin. In Kotlin specifically it's one of the very central features.
loop is a function that also accepts two, integer for number of loops and again a block of code.
Even fn that creates functions is a function that accepts two blocks, first is a list of arguments and second is a block of code. There is no big difference between function fn and function print, they are both builtin functions defined in same manner and there are multiple fns for special cases and you can create your own on "library" level.
It looks like that's how Rye does it as well, blocks can be conditionally evaluated: https://ryelang.org/meet_rye/basics/if_either/
"In REBOL, contrary to Lisps, blocks or lists don’t evaluate by default. For better or for worse, this little difference is what makes REBOL - REBOL." https://ryelang.org/meet_rye/basics/doing_blocks/
It's difficult for a language with this semantics to be made efficient, but efficiency isn't everything.
That makes it potential interesting to me.
It is interesting to imagine inverting the sense of execution, but as you say it's hard to do much optimization.
In a language where functions don't do that, they're just functions. Rye appears to be one of those languages. One could quibble about whether that's the right thing to call them, but if we say they're vau or whatever, "everything in Rye is a vau" is still true. I think calling them functions is reasonable though.
Since code-is-data, you can swap the if and else clauses around. This applies to all the functions where you've inserted the if statement.
This kind of thing makes it difficult to compile a function to a fast set of machine instructions with the same effect. Not impossible, but difficult.
You can't even assume `+` means "add the arguments", because `+` may have been bound to something completely different prior to evaluating the expression containing `+`.
Turns out the language does have special forms, which is OK; it's just weird to say there aren't (though it's an understandable goal).
When I worked on 3Lisp (so many decades ago) it became clear to me how many special forms there are (a small number, but more than I thought) and, honestly, how few there really are, so the "benefit" of a 3Lisp turns out to be negligible in practice. Oddly enough I didn't really notice that when writing interpreters because I thought of most special forms as simply compiler hacks ("eventually we can get rid of this").
So if your built-in functions were implemented in Python, you'd use:
def if_(cond, t, e):
if cond:
return t()
else:
return e()and(x, y) -> bool
or(x, y) -> bool
if(cond, funcTrue, funcFalse) -> void
if(and(or(cond1, cond2), cond3), effect1(), effect2())
In most languages, if `cond1` evaluates to true, you would not evaluate `cond2`. If `cond1` and `cond2` evaluate to false, you would not evaluate `cond3`. If all conds evaluate to false, you would not evaluate `effect1()`, and if `cond3` and either `cond1` or `cond2` are true, you would not evaluate `effect2()`They're not functions because they don't evaluate their arguments before evaluating their body. Their operands are passed verbatim and evaluated explicitly by the body on demand.
In Kernel, for example, we can define these as operatives, which don't evaluate their operands. Assuming we have some primitive operatives `$cond`, `$define!` and `$vau` (the constructor of operatives), and an applicative `eval`:
($define! and
($vau (lhs rhs) env
($cond ((eval lhs env) (eval rhs env)
(#t #f)))))
($define! or
($vau (lhs rhs) env
($cond ((eval lhs env) #t)
(#t (eval rhs env)))))
($define! if
($vau (condition consequent antecedent) env
($cond ((eval condition env) (eval consequent env))
(#t (eval antecedent env)))))
These aren't the definitions Kernel uses in its standard environment. It uses recursive definitions of `$and?` and `$or?` which take arbitrary number of operands, and `$cond` is defined in terms of `$if`, which is primitive: ($define! $cond
($vau clauses env
($if (null? clauses) #inert
($let ((((test . body) . rest) clauses))
($if (eval test env)
(apply (wrap $sequence) body env)
(apply (wrap $cond) rest env))))))
($define! $and?
($vau x env
($cond ((null? x) #t)
((null? (cdr x)) (eval (car x) env))
((eval (car x) env) (apply (wrap $and?) (cdr x) env))
(#t #f))))
($define! $or?
($vau x env
($cond ((null? x) #f)
((null? (cdr x)) (eval (car x) env))
((eval (car x) env) #t)
(#t (apply (wrap $or?) (cdr x) env)))))It's doing if(cond, effect1, effect2) where effect1 and effect2 are functions, and only evaluating the matching effect function. But everything is functions.
fac: fn { x } { either x = 1 { 1 } { x * fac x - 1 } }
; function that calculates factorial
So presumably "either" is the "if" expression.Rebol was known for its small set of moving parts and behaviors but with a lot of depth and flexibility with them. I've added some moving parts with left to right code flow, but I still hope it's a limited set that fits well together. I am adding new behaviors very conservatively now, and I will remove some. Thanks for heads up about Perl! ;)
Here are unit tests / reference for the validation dialect, but they aren't the best source of information: https://ryelang.org/validation.html
Here is an old blogpost with another example: https://ryelang.blogspot.com/2021/01/added-intro-pages-for-h...
It's a dialect with static/builtin rules that can be combined and two rules that accpet blocks of Rye code so they can do anything (check and calc, first checks value for certain condition, second changes / calculates new value from the old)
I've written about validation dialect in previous attempts, but current one "Meet Rye" is still in the process of being written and I will get to this.
I guess it’s easy to express in a dependently typed language to an extent.
I am still learning to explain or even name things, but various examples of using contexts in different ways are currently what excites me the most. I will write "Meet Rye" further and contexts are one of next subjects to be written about.
Could you perhaps summarise how contexts work in Rye? On the Ren-C forum we’ve recently been discussing binding a lot, and I’d be interested to learn how Rye does it.
Context is just a namespace of "word: value"-s and an optional link to a parent context. When a word is being searched it looks in the current context and if it's not found it goes up to parent, etc.
One interesting things about contexts are what I called "isolates", a context that is isolated from anything else, but can hold a set of functions specialized for a certain task. Or you can construct and string together custom contexts to create a desired environment for certain code.
When you have a context, you can evaluate code in context, you can call functions in context from outside, you can create functions that are evaluated is a specified context or with a specified parent context, etc ...
You can also move through contexts and list them in Rye console like you would through directories.
I don't know if I answered your question well ... I'm maybe better at showing than explaining. I'm preparing an asciinema demo called "contextplay".
name: Janko ; name: is a set-word - it binds value to a word
?print ; ?print is a get-word - it get's value word is bound to in this case a print builtin function
:age ; left leaning set-word (this is get-word in Rebol)
what? ; just a regular word
...
name get's the value anyway, so we don't need to use ?name but if word is bound to a function just invoking a word will evaluate a function and if we want to return a funtion we use get-word. which has ? in front.? at the end is just a regular word and a (currently accepted) naming convention where noun? means get-noun. so length? in instead of get-length etc.
Rebol used ? at the end convention for more things, a lot for boolean results, testing of types, like string? and positive? but also for lenght?
For booleans current Rye's naming convention is that we use is-adjective. Rebol used positive? to test if value is positive. We would in this way use is-positive.
The conventions might change if we see that there are ways that make more sense and are also consistent.
From a past romance with Forth I see in general how concatenative languages seem to be underappreciated a bit