cdglabs / Shadershop

The Unlicense
910 stars 64 forks source link

Crashes on defining a recursive function #1

Closed mgold closed 9 years ago

mgold commented 9 years ago

Steps to reproduce:

  1. Create a custom function
  2. Drag the new function into itself

The result is Shadershop becomes unresponsive, and sometimes blanks the thumbnail plots, even on the primitives (top right), showing only the beige background. The symbolic readout says undefined( x ).

This action should be prohibited since it makes no sense mathematically (at least until you can define piecewise functions that allow for a base case).

Also, OH MY FUCKING GAWD THIS IS AMAZING.

electronicwhisper commented 9 years ago

Yeah, it crashes when you drag a function in to itself (or create any kind of circular dependency).

For my own reference, or if anyone wants to tackle it, here is a sketch for a fix:

Write a test that checks when you drag function A into function B that A does not contain B anywhere down the tree and B does not contain A anywhere down the tree.

Patch Actions.addChildFn to make sure that the above is not the case. If it is, don't add the function (and maybe display some sort of warning?).

mgold commented 9 years ago

Thanks for the sketch. This seems to work doesn't account for the case of the added tree containing a compound function, I guess I'll get to that tomorrow:

containsCircularDep = (fn_to_add, existing) ->                                      
  if fn_to_add instanceof C.BuiltInFn                                               
      return false                                                                  
  fn_to_add = fn_to_add.childFns[0].fn                                              
  recurse = (fn) ->                                                                 
    if fn.__id == fn_to_add.__id                                                    
        return true                                                                 
    if fn instanceof C.CompoundFn                                                   
        fn.childFns.some recurse                                                    
    else                                                                            
        false                                                                       
  recurse(existing) 

Then in Actions.addChildFn, right before creating the new childFn,

  if containsCircularDep fn, parent                                              
      console.log "CIRCULAR DEP"                                                 
      return 

What to do once the circular dependency is detected is up to you.

mgold commented 9 years ago

Okay, I've got this version. Let me know if you can break it.

containsCircularDep = (fn_to_add, existing) ->                                      
  if fn_to_add instanceof C.BuiltInFn                                               
      return false                                                                  
  recurse = (fn) ->                                                                 
    if fn instanceof C.DefinedFn or fn instanceof C.CompoundFn                      
        fn == existing or fn.childFns.some recurse                                  
    else if fn instanceof C.ChildFn                                                 
        recurse fn.fn                                                               
    else # C.BuiltInFn                                                              
        false                                                                       
  recurse(fn_to_add) 

The exact semantics are simple once stated: the function being add must not contain its soon-to-be parent anywhere in its tree.

electronicwhisper commented 9 years ago

I like your wording. I pushed some code based on your snippet.

mgold commented 9 years ago

I could have sworn there was a case that required handling C.DefinedFn but I can't break your version. Thanks for merging.

electronicwhisper commented 9 years ago

DefinedFn extends CompoundFn so that case handles it.

mgold commented 9 years ago

Nice!