Template types #31

Closed
opened 2022-07-27 15:09:16 +00:00 by glen · 4 comments
Owner

It should be possible to add template types. Can experiment with a homogeneous Tuple
type that operates elementwise.

The idea is it would look like

pm.installTemplate('Tuple', {
   test: t => t && typeof t === 'object' && 'tuple' in t && <return common type>,
   from: {
      T: oneT => ({tuple: [oneT]})
   }
}

to create the type and then to use it:

pm.installOp('negate', {
  'Tuple<T>': ({'self(T)': selfT}) => t => ({tuple: t.tuple.map(selfT)})
})

which of course can just be accomplished via

export const negate = {
  'Tuple<T>': ({'self(T)': selfT}) => t => ({tuple: t.tuple.map(selfT)}
}
It should be possible to add template types. Can experiment with a homogeneous Tuple type that operates elementwise. The idea is it would look like ``` pm.installTemplate('Tuple', { test: t => t && typeof t === 'object' && 'tuple' in t && <return common type>, from: { T: oneT => ({tuple: [oneT]}) } } ``` to create the type and then to use it: ``` pm.installOp('negate', { 'Tuple<T>': ({'self(T)': selfT}) => t => ({tuple: t.tuple.map(selfT)}) }) ``` which of course can just be accomplished via ``` export const negate = { 'Tuple<T>': ({'self(T)': selfT}) => t => ({tuple: t.tuple.map(selfT)} } ```
Author
Owner

So it's all about the infrastructure to make that work. The basic idea is that the template install would install a Tuple<any> type with the property that when it matches, say with type 'number', it installs a Tuple<number> type and invalidates everything that depends on Tuple; and the _bundle of an op generates implementations for all Tuple<XXX> types that exist at that time (so initially just the any type). Also in those implementations, the any type is handled specially in that specific signature references are ignored, only when the template matching type is the any type.

This should actually not be that bad. Definitely worth implementing, and if it works for Tuple, then Complex should be converted to a template.

So it's all about the infrastructure to make that work. The basic idea is that the template install would install a `Tuple<any>` type with the property that when it matches, say with type 'number', it installs a `Tuple<number>` type and invalidates everything that depends on Tuple; and the `_bundle` of an op generates implementations for all `Tuple<XXX>` types that exist at that time (so initially just the any type). Also in those implementations, the any type is handled specially in that specific signature references are ignored, only when the template matching type is the any type. This should actually not be that bad. Definitely worth implementing, and if it works for Tuple, then Complex should be converted to a template.
Author
Owner

the difficulty with only adding specializations of the template when they match an "any" version is suppose Tuple<Complex> was matched first. then there could never be a Tuple<number> because every instance would match Tuple<Complex>. so i think unfortunately you have to add all of the instantiations, and then update them when a new type is added.

template operations are useful even without template types and should be implemented first

the difficulty with only adding specializations of the template when they match an "any" version is suppose `Tuple<Complex>` was matched first. then there could never be a `Tuple<number>` because every instance would match `Tuple<Complex>`. so i think unfortunately you have to add all of the instantiations, and then update them when a new type is added. template operations are useful even without template types and should be implemented first
Author
Owner

oh or whenever you add an instantiation you add for all subtypes and all types that can be converted to it.

oh or whenever you add an instantiation you add for all subtypes and all types that can be converted to it.
glen added the
enhancement
label 2022-07-30 15:31:55 +00:00
Author
Owner

Done in #45 and #46.

Done in #45 and #46.
glen closed this issue 2022-08-06 15:43:38 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: glen/pocomath#31
No description provided.