Skip to content

Commit

Permalink
Some thoughts
Browse files Browse the repository at this point in the history
  • Loading branch information
FadiShawki committed Feb 21, 2024
1 parent 0c8eb45 commit 4bb8c92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 84 deletions.
73 changes: 2 additions & 71 deletions environments/python/_temp/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ namespace Ray {
/** Storage/Movement operations which need to be implemented. */
& { [TKey in keyof Ray.Op.Impl<Ray.Any>]: Ray.Any }

export namespace Debug {
export type Event = { event: string, self: Ray.Any, context: any };
export type Listener = (event: Event) => void;
}

/** A simplistic compiler for Ray */
export namespace Compiler { // TODO Ray is Compiler

Expand All @@ -53,7 +48,6 @@ namespace Ray {
* - a.orbit() -> a.orbit(a)
* // TODO: Should be automatic? is_orbit() or any method without arguments is a.is_orbit(a.self()) ?? not a.is_orbit(a) ; ???
*
* - .initial/.terminal can be seen as a particular connection on .self, which .self ignores?
*
* TODO: Allow hooking
*
Expand All @@ -69,44 +63,6 @@ namespace Ray {
*/
}

export type Constructor = { proxy?: ProxyHandler<Ray.Any>, debug?: Debug.Listener, }

export class Instance extends JS.Class.Instance<Ray.Any> {

listeners: Debug.Listener[];

constructor({
proxy,
debug,
}: Ray.Constructor = {}) {
super({ proxy: proxy ?? Ray.ProxyHandlers.Default });

this.listeners = debug ? [debug] : [];
}

/** Used to jump out of the proxy. */
get ___instance(): Instance { return this; }

debug = <T>(
on: Debug.Listener,
func: JS.ParameterlessFunction<T>
): T => {
this.listeners.push(on);
const ret = func();
this.listeners.pop(); // TODO?

return ret;
}

/** Simple debug/traversal mechanism. */
on = (event: Omit<Debug.Event, 'self'>) => {
if (!this.listeners) return;

this.listeners.forEach(listener => listener({ ...event, self: this.proxy }));
}

}

/** Ray is an Enum(eration) */
export namespace Enum {

Expand All @@ -125,33 +81,8 @@ namespace Ray {

}

/** Ray is a function (.next) */
export namespace Function {

// /**
// * Implement a function from the perspective of 'this' for 'this.self'.
// */
// // static Ref = <TResult>(impl: (ref: Ray.Any) => TResult): Function<TResult> => Ray.Function.Self(self => impl(self.as_reference()));

export namespace Self {

// export const If = (impl: Op.Unary.Type<Ray.Any>): Ray.Any => {
// return Impl(impl); // TODO: GENERIC collapse to boolean implemented and overridable
// }

/**
* TODO:
* - Maybe replace switch with 'zip'?, What are the practical differences?
*/
// export const Match = (cases: MatchCases): Ray.Any => {
// return Impl(self => self); // TODO
// }
}
}
export const traverse = Ray.Function.Self.Impl(
(a) => { throw new NotImplementedError(); }
);

}

// TODO

export default Ray;
7 changes: 7 additions & 0 deletions environments/python/orbitmines/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test() -> Ray:
test.run(lambda ray: ray)
# Ray.runtime(lambda ray: ray).run(test)

Ray(
initial = lambda ray: ray,
self = lambda ray: ray,
terminal = lambda ray: ray,
is_orbit = lambda ray: ray
)
# class Object(Ray):
# @property
# def initial(self) -> Ray: raise NotImplementedError
Expand All @@ -52,3 +58,4 @@ def test() -> Ray:
# add = -sub
# sub = -add
# [add, sub].orbit

31 changes: 18 additions & 13 deletions environments/python/orbitmines/ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

# TODO: Better python solution than just @ray everywhere (for typechecker)

# TODO: match, switch
# TODO: zip, tensor (are these the same as match/switch?)

def ray(func: Callable[[Any, ...], Any]) -> Ray:
pass

Expand All @@ -37,8 +40,12 @@ def __setattr__(self, key, value) -> Any:
# method(self): Implement a function from the perspective of 'this'
class Ray:
def __init__(self, *args, **kwargs):
# TODO: Named args in the sense, similar to class definition, in the sense that they equivalences on the existing functions. Again this thing of assign.

pass

# TODO: DEBUG/LISTENER/OBSERVER/WRAPPER IS A WRAPPER AROUND EVERY FIELD, callbacks. "Ignorant of how it effects, ..., doesn't effect the function."

#
# Basic Ray operators
#
Expand All @@ -63,26 +70,21 @@ def free(self): raise NotImplementedError
destroy = clear = delete = pop \
= free

# TODO: Like any method, .initial/.terminal could be seen as a particular section of .self, which .self itself ignores. - This should be generalizable to other things setup on .self.

# An arbitrary Ray, defining what continuing in the reverse of this direction is equivalent to.
@ray
def initial(self) -> Ray: return (-self).terminal
previous = backward \
previous = backward = decompile = predecessor \
= initial
# An arbitrary Ray, defining what continuing in this direction is equivalent to.
@ray
def terminal(self) -> Ray: return (-self).initial
next = forward = step \
= terminal

# Ray is a function (.next)
# TODO: In the case of tinygrad this is similar to .realize() ?
def __call__(self, *args, **kwargs) -> Ray:
def terminal(self, *args, **kwargs) -> Ray:
print(f'{self.name}.__call__ {args} {kwargs}')
# raise NotImplementedError
return self
map = render = compile = run \
= __call__ # TODO SHOULD BE __call__ = next

return (-self).initial
next = __call__ = forward = step = map = render = compile = run = realize = successor \
= terminal
# Todo: slightly different perspectives in cases of map/render etc..., where certain aliases of these are expected not to have alternative behaviors based on binary/ternary calls to this... ; Basically; some of these aliases are probably more appropriate as separate perspectives.

# @see "Reversibility after ignoring some difference": https://orbitmines.com/papers/on-orbits-equivalence-and-inconsistencies#:~:text=Another%20example%20of%20this%20is%20reversibility
# @see "More accurately phrased as the assumption of Reversibility: with the potential of being violated.": https://orbitmines.com/papers/on-orbits-equivalence-and-inconsistencies#:~:text=On%20Assumptions%20%26%20Assumption%20Violation
Expand Down Expand Up @@ -150,6 +152,7 @@ def as_terminal(self) -> Ray: return (
def is_orbit(a, b: Arbitrary) -> Ray: raise NotImplementedError # a.___instance === b.___instance
__eq__ \
= is_orbit
# TODO: Constant as local orbit.

# -__eq__ == __ne__
# @ray
Expand All @@ -171,6 +174,7 @@ def is_terminal(self) -> Ray: return self.terminal().is_none
def is_vertex(self) -> Ray: return self.is_initial().nor(self.is_terminal()) # [--|--]
@ray
def is_reference(self) -> Ray: return self.is_initial() & self.is_terminal() # [ | ]
# TODO: reference = pointer ...
@ray
def is_boundary(self) -> Ray: return self.is_initial() ^ self.is_terminal() # [?-| ] or [ |-?]

Expand Down Expand Up @@ -224,6 +228,7 @@ def orbit(a, b: Arbitrary) -> Ray:

# "Applying the same thing in a different context"
# TODO: Somewhat related to Functors?
# TODO: .ref perspecctive: self.as_reference & self.dereference
@ray
def from_perspective_of(a, b):
raise NotImplementedError
Expand Down

0 comments on commit 4bb8c92

Please sign in to comment.