1. Fallbacks in Euphoria

Lua has something called a fallback, which is a powerful way of extending the functionality of Lua tables.

I think they could potentially map very closely to Euphoria's sequences. One of the reasons they are compelling is because they are only called on an error condition.

This means that there's essentially no runtime cost to adding them to the language.

From a coding point of view, it would mean adding a pointer to the sequence data structure. But since most cases would have a null for the pointer, it would just be the cost of checking the pointer, so there wouldn't be much impact. (Easy for me to say, I know. blink)

Now, imagine you wanted to a sequence to behave like a hash table:

person["name"] = "me" 
sequence s = person["name"] 

To get this to work with fallbacks, you would have to set the settable and index handlers for the fallbacks in the person sequence.

Similarly, this mechanism could be used to simulate structures, and even classes with inheritance.

Lua has a function handler, which obviously doesn't exist in Euphoria:

-- call the method bar in sequence the foo 
foo.bar() 

Again, it seems that this sort of functionality could be added to Euphoria without impacting the existing language negatively, but get a huge gain in language extensibility.

Thoughts?

- David

new topic     » topic index » view message » categorize

2. Re: Fallbacks in Euphoria

Definitely interesting. The delete_routine() is a bit like that, being only called when the reference count drops to 0.

Pete

new topic     » goto parent     » topic index » view message » categorize

3. Re: Fallbacks in Euphoria

petelomax said...

Definitely interesting. The delete_routine() is a bit like that, being only called when the reference count drops to 0.

I should have mentioned that Lua has a gc fallback, too. That might actually be very useful when trying to wrap objects from other libraries.

One difference is that you can deliberately keep a table empty of key values in Lua, so you can force indexing to happen. In Euphoria, it's not quite the same because indexing is done via a numeric value, not a key/value pair.

So you could start with an empty sequence in Euphoria, and something like:

s = foo[1] 

would fail the first time, calling the fallback. But as soon as you stored a value in the sequence,

s = foo[1] 

would trigger correctly, which is a bit of a conundrum. So... what to do?

One option would be to allow access to the sequence that holds the fallback list, calling it meta. You could do anything you'd want with that sequence - even store data in it.

Of course, you couldn't access it directly, so you'd need some way to get and set the meta sequence.

- David

new topic     » goto parent     » topic index » view message » categorize

4. Re: Fallbacks in Euphoria

It seems to me fallbacks are an exceptional case handling mechanism. Errors that don't happen often enough that they are not returned as an error code and handled but they still happen. If you want to change the behavior of [] for a class of sequences, you really want Operator Overloading. Something that is permanent for a given type.

I have come to think that a type, or class using parlance from another language here, has three optionally overloaded methods: One a validation method, this is our type-check routine, that has been around forever. Another is the Delete Routine, useful for sockets and file handles. if you need to return from a routine but have file handles and sockets that need to be closed? No problem. Use delete_routine. The last one is that we can overload how a constant is displayed using the alternative literals mechanism.

Of course there are two kinds of methods. In EUPHORIA, the validation routine is a non-virtual method where as the delete routine is a virtual method. Non-virtual methods are associated by the language front end and called according to the variable. Virtual method associates methods in the meta data of the object itself and must be carried by each object.

Shawn

new topic     » goto parent     » topic index » view message » categorize

5. Re: Fallbacks in Euphoria

SDPringle said...

It seems to me fallbacks are an exceptional case handling mechanism

Yes... But in Lua it's possible to make the "normal" case exceptional. For example, indexing by:

atom a = s[1] 

differs from Euphoria in that in Lua it's actually a hashtable lookup. So all you need to do in Lua is place your data one level deeper - in an element called data, for example. Since the data is actually in:

atom a = s["data"][1] 

calling the index directly will trigger the fallback.

One reason that it's attractive to have these as error fallbacks is speed: you only check on error, so "normal" cases don't have an additional cost to them.

You could choose instead to assume that if there are any fallbacks, you should execute them instead of the default behavior. There is some reason to argue that this might be a useful behavior.

I think being able to access the meta sequence independent of the fallback mechanism is potentially valuable, as language like JavaScript use this sort of functionality to extend their functionality.

- David

new topic     » goto parent     » topic index » view message » categorize

6. Re: Fallbacks in Euphoria

Another possible implementation could be to set a meta flag on the sequence structure instead of a separate sequence. If the flag is set, the fallback routine would be called instead, and directly passed the sequence.

Actually, it would need to be passed a copy of the sequence, without the meta flag set, or you'd be stuck in recursion forever.

Since the low-level operations would always check for the meta flag, there would be a (hopefully small) hit for all the sequence operations.

On the other hand, it simplifies the implementation quite a bit. The fallback always executes for a meta sequence, and there's no need to maintain a second sequence.

The sequence that's passed to the fallback is a copy without the meta field set, which means it's processed inside the fallback as a normal sequence. Of course, if that sequence contains other meta sequences, they are processed properly.

Here's the catch: how would the changes get saved back to the meta sequence?

That's why I suggested that meta holds a pointer to a sequence - you could have a fetch_meta() and set_meta() sort of call to access the metadata. But now that I think about it, I'm not sure that would work either - because only the meta sequence for the copy would get changed, not the calling sequence. sad

This sort of thing makes my head hurt! But I suspect that this is doable, I just need to wrap my head around it.

- David

new topic     » goto parent     » topic index » view message » categorize

7. Re: Fallbacks in Euphoria

Dear David (Cuny= sorry for misspelling your name into Cluny somewhere on theStrengthsofEuphoria)

You do come up with great and valuable additions to Eu.

Why don we divide into teams to tackle the issues?
And how should we decide on who's going to work on which part?

new topic     » goto parent     » topic index » view message » categorize

8. Re: Fallbacks in Euphoria

dcuny said...

This sort of thing makes my head hurt! But I suspect that this is doable, I just need to wrap my head around it.

The nuances involved in reference counting is like that.

Matt

new topic     » goto parent     » topic index » view message » categorize

9. Re: Fallbacks in Euphoria

Ekhnat0n said...

Why don we divide into teams to tackle the issues?

These things aren't as simple as it initially looks.

First, Euphoria passes sequences by value, not by reference. This means that if you alter the metadata for a sequence, you may just be modifying a local copy. The end result is that nothing changes.

On the other hand, using a global to hold the metadata has other potential issues - if the metadata applies to all the sequences, that means that modifying one particular sequence may potentially modify all of them.

So there's a lot of thinking that has to take place on this to even decide if it is practical.

If we were only talking about the interpreted version of Euphoria, that would make things a lot simpler. But the compiled version of Euphoria has to be considered as well.

So before dividing into teams to "tackle the issues", it's best to really understand the issues. Otherwise there's the very real possibility of having some unintended consequences paint us into a corner.

And who are the "teams" that this will be divided between? There are only a small number of people who really understand the inner workings of Euphoria. As far as I know, they've only got a limited amount of free time, and aren't getting paid to do any of this. So there's the job of selling the team that this feature (whatever it happens to be) is worth taking on next, instead of some other feature.

Heck, the reason I started working with Euphoria again is to create a vocal synthesis program, and I've barely had time to work on it at all. I can only imagine that other developers have "real life" to attend to as well.

- David

new topic     » goto parent     » topic index » view message » categorize

10. Re: Fallbacks in Euphoria

Dear David or Cher David as you would say chez toi

If a small group would enter into the discussion I would consider heading that group
These are my refs:

Programming ever since 1969, starting out on asm and BASIC soon followed by COBOL, FORTRAN and ALGOL.

During my programming-time I wrote a program called Megapod which did exactly what Windows 3.11 did BUT
as I was living in the Netherlands in those days and had no connections whatsoever with either Intel or IBM
Bill Gates swooped over my mate and me, who coded my algorythm, through his liaison with both of them.
I am known to have an IQ of well over 150 (proven by appr. 10 serious tests I did in the past)
nothing to be proud of as it was a gift I got from my birth on
and am specially equipped to see structures of and in anything, so this applies for Euphoria as well.

I did 68000-asm as well for Sinclair computers and Atari TT, so I guess I am quite versatile.

Please don't get me wrong, I am just a man with a mission and that mission includes
to make Euphoria the leading language because of it's readability and simplicity

Mes voeux les plus sincères pour la nouvelle année et pour toute ta vie et les vies de ceux qui tu aîmes.

Ton ami et frère en Euphoria, Antoine

new topic     » goto parent     » topic index » view message » categorize

11. Re: Fallbacks in Euphoria

Ekhnat0n said...

Dear David or Cher David as you would say chez toi

There was a David Cuny who lived in Paris, but that isn't me.

Ekhnat0n said...

If a small group would enter into the discussion I would consider heading that group
These are my refs:

Hey, I'm just a guy posting stuff on this board - not a maintainer. And I'm not half as clever as some of them by far.

When I said there were issues with fallbacks, that had to be figured out, it's because I'm still puzzling them out. If you can solve it... [i]great![/i]

And you don't need any refs, you just need to code up a patch.

But thanks for the kind words! smile

- David

new topic     » goto parent     » topic index » view message » categorize

12. Re: Fallbacks in Euphoria

Please David,
Explain me in simple words what fallbacks are
and I will try and come up with a scheme how to tackle them
probably by the end of the week
as I have a very busy schedule right now
working on a task my deceased wife begged me to complete
(and made me give my solemn promise to do so),
while on her sickbed, terminally ill of metastases of melanoma in her brain.
She died on oct 14th 2012 and I have been preparing for that specific task
ever since that painful day

What that specific task is I will tell everybody on this site soon
as some or all of you might be helpful in accomplishing it.

new topic     » goto parent     » topic index » view message » categorize

13. Re: Fallbacks in Euphoria

Ekhnat0n said...

Please David,
Explain me in simple words what fallbacks are...

In Lua, the core datatype is a table - a dictionary of key/value pairs.

A metatable in Lua is a regular table with the addition of key/value pairs. These values are ordinarily not accessible, except with the special methods setmetatable( table, function ) and getmetatable( table ).

When Lua performs an operation on a table that then fails, it checks to see if there is an overloaded function in the metatable. If there is, the operation is performed using function in the metatable.

Here are the keys that Lua supports. Be aware that not all of these map to operations in Euphoria, and there are operations that Euphoria supports that don't have equivalents in Lua:

  • __index
  • __newindex
  • __mode
  • __call
  • __metatable
  • __tostring
  • __gc
  • __unm
  • __add
  • __sub
  • __mul
  • __div
  • __pow
  • __concat
  • __eq

Additionally, there are "raw" routines for accessing Lua tables without triggering the fallback behavior.

For more details, Google is your friend.

Mapping this to Euphoria requires a number of low-level modifications, including:

  1. Associate metatable data with a sequence.
  2. Determine what Euphoria opcodes map to actions.
  3. Modify the failure behavior to call the metatable fallback if it exists.

- David

new topic     » goto parent     » topic index » view message » categorize

14. Re: Fallbacks in Euphoria

Thanks a lot, it is clear to me now what fall-backs are and what they are supposed to do.
So please give me a couple of days to ponder over the way to apply them in Euphoria.
I guess I might be able to come up with an algorhythm that does the trick.

I will go and GOOGLE Lua and the way it implements these fallbacks
which in a way of speaking simply are dedicated error-handling routines if I get the drift correctly.
BTW where are you from? I am from the Netherlands but currently on the move all over Europe
visiting old friends to get their support and assistance for the plan I am about to roll out in the near future
(probably 2015-02-03 (febr 3rd) being the 74th birthday of my deceased wife).

new topic     » goto parent     » topic index » view message » categorize

15. Re: Fallbacks in Euphoria

I suspect David Cuny is being somewhat modest here -
if you search Euphoria archives and mail history, he is well represented ;)
For example, the very commonly used win32lib was created by David IIRC

new topic     » goto parent     » topic index » view message » categorize

16. Re: Fallbacks in Euphoria

Ekhnat0n said...

I will go and GOOGLE Lua and the way it implements these fallbacks

You might also have a look at Python, since it implements a similar mechanism.

The more I consider it, the more this seems to me as something that would be better implemented as a special type of sequence.

That is, if the meta flag is set, it executes the user-defined fallback (as in Python or Lua), rather than only being triggered on a failure.

That still leaves the question of how the meta information gets updated when Euphoria passes the sequence by copy, which strikes me as the real problem here.

- David

new topic     » goto parent     » topic index » view message » categorize

17. Re: Fallbacks in Euphoria

dcuny said...
Ekhnat0n said...

I will go and GOOGLE Lua and the way it implements these fallbacks

You might also have a look at Python, since it implements a similar mechanism.

The more I consider it, the more this seems to me as something that would be better implemented as a special type of sequence.

That is, if the meta flag is set, it executes the user-defined fallback (as in Python or Lua), rather than only being triggered on a failure.

That still leaves the question of how the meta information gets updated when Euphoria passes the sequence by copy, which strikes me as the real problem here.

- David

I do know a thing or two about Python already, but will have to read up on it
to refresh knowledge I acquired some odd 25 or more years ago.

As to implemnting it as a sequence: you are absolutely right
because it gives most freedom for the programmer Again for the meta-flag you hit the bulls'eye.

And now for your problem:
There will be no problem at all because EXACTLY WHILE Euphoria PASSES IT BY COPY
you can compare it to any sequence in the variable area
and find out
exactly which sequence is meant
AND what the differences are
AND correct those in the actual variable.

It is not that hard isn't it blink

new topic     » goto parent     » topic index » view message » categorize

18. Re: Fallbacks in Euphoria

dcuny said...

These things aren't as simple as it initially looks.

if you alter the metadata for a sequence, you may just be modifying a local copy. The end result is that nothing changes.

On the other hand, using a global to hold the metadata has other potential issues - if the metadata applies to all the sequences, that means that modifying one particular sequence may potentially modify all of them.

So there's a lot of thinking that has to take place on this to even decide if it is practical.

A quick recap on how delete_routine() works may help.

  data = delete_routine(data,rid) 

So yes, (without pbr) it modifies a local copy but then overwrites the original anyway.

The natural instinct is to store a routine_id on the sequence. However delete_routines may be daisy-chained together, so instead (Phix) stores an index to a table of delete_routine collections. In Phix, builtins/pDelete.e contains sequence delete_sets which holds those sets entirely independently of the data, which only has an index attached to it. Naturally, builtins/pDelete.e does not clobber delete_sets[idx] as that may be in use on other data but instead crafts a new entry and replaces the index on the data.

It also strikes me that when associating multiple fallbacks to a value, rather than adding a new index for each have just the one index that refers to some fallback collection held elsewhere. In fact delete_routine could and probably should be demoted from it's current pole position to fallback_collections[idx][K_DELETE].

Pete

new topic     » goto parent     » topic index » view message » categorize

19. Re: Fallbacks in Euphoria

petelomax said...

A quick recap on how delete_routine() works may help.

Can delete_routine be run backwards and thereby instanciate a routine? If so, there's your execute(string).

useless

new topic     » goto parent     » topic index » view message » categorize

20. Re: Fallbacks in Euphoria

I wonder if perhaps I'm taking an "too complex" sort of approach to this.

Functionally, meta serves as an override a sequence's default behavior. So perhaps the sequence that holds the meta data shouldn't be directly edited, anyway. After all, you don't generally override class declarations on the fly.

With that thought, setmeta(s, metadata) could be used to set the metadata sequence for a sequence. Every copy of that sequence has that sequence associated with it.

- David

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu