-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent operations on variables from different contexts #1
Comments
What is possible today: Context*[T] = object
## Tape / Wengert list. Contains the list of applied operations
nodes: ref seq[Node[T]]
context_id: int And compare context_id for each operations I'm implementing The best way would be to have Nim typechecker check the One way to do this is by having a global counter, scoped to the module to avoid name conflict, and use this counter as a static[int] type. type
Node[T] = object
Context*[T; I: static[int]] = object
nodes: ref seq[Node[T]]
var CONTEXT_COUNTER {.compiletime.} = 0
proc ctx_handler(T: typedesc, I: static[int]): Context[T, I] {.noSideEffect.} =
result.nodes = new seq[Node[T]]
result.nodes[] = @[]
proc newContext*(T: typedesc): auto =
inc CONTEXT_COUNTER
return ctx_handler(T, CONTEXT_COUNTER)
let ctx = newContext(float32) # This line doesn't compile The other way would be to have type
Node[T] = object
Context*[T] = object
nodes: ref seq[Node[T]]
Variable*[T, CTX: Context] = object
## Wrapper for values
tape: CTX # Generic generic would be lively to add the T constraint: https://github.com/nim-lang/Nim/issues/3856
index: int
value: T
proc newContext*[T]: Context[T] {.noSideEffect.} =
## Initialize a context (Tape / Wengert list)
result.nodes = new seq[Node[T]]
result.nodes[] = @[]
let ctx = newContext[float32]
proc push_nullary[T](t: Context[T]): int {.noSideEffect.} =
## Append a nullary operation to the context
let len = t.len()
t.push(
Node[T](
weights: [0.T, 0.T],
deps: [len, len]
)
)
return len
proc variable*[T](CTX: Context, value: T): Variable[T, CTX] {.noSideEffect.} =
## Wrap a variable to the context
return Variable[T, CTX](
tape: t,
value: value,
index: t.push_nullary()
) |
Done by context being a |
This should not compile
The text was updated successfully, but these errors were encountered: