@@ -4,22 +4,22 @@ function __init__()
44 push! (Base. package_callbacks, pkgid -> refresh_rules ())
55end
66
7-
8- const NEW_RRULE_HOOKS = Function[]
9- const NEW_FRULE_HOOKS = Function[]
10- _hook_list (:: typeof (rrule)) = NEW_RRULE_HOOKS
11- _hook_list (:: typeof (frule)) = NEW_FRULE_HOOKS
7+ # Holds all the hook functions that are invokes when a new rule is defined
8+ const RRULE_DEFINITION_HOOKS = Function[]
9+ const FRULE_DEFINITION_HOOKS = Function[]
10+ _hook_list (:: typeof (rrule)) = RRULE_DEFINITION_HOOKS
11+ _hook_list (:: typeof (frule)) = FRULE_DEFINITION_HOOKS
1212
1313"""
1414 on_new_rule(hook, frule | rrule)
1515
1616Register a `hook` function to run when new rules are defined.
1717The hook receives a signature type-type as input, and generally will use `eval` to define
18- and overload of AD systems overloaded type.
18+ an overload of an AD system's overloaded type
1919For example, using the signature type `Tuple{typeof(+), Real, Real}` to make
2020`+(::DualNumber, ::DualNumber)` call the `frule` for `+`.
2121A signature type tuple always has the form:
22- `Tuple{typeof(operation), typeof{pos_arg1}, typeof{pos_arg2...} }`, where `pos_arg1` is the
22+ `Tuple{typeof(operation), typeof{pos_arg1}, typeof{pos_arg2} ...}`, where `pos_arg1` is the
2323first positional argument.
2424
2525The hooks are automatically run on new rules whenever a package is loaded.
@@ -76,8 +76,13 @@ It is *automatically* run when ever a package is loaded.
7676It can also be manually called to run it directly, for example if a rule was defined
7777in the REPL or within the same file as the AD function.
7878"""
79- refresh_rules () = (refresh_rules (frule); refresh_rules (rrule))
79+ function refresh_rules ()
80+ refresh_rules (frule);
81+ refresh_rules (rrule)
82+ end
83+
8084function refresh_rules (rule_kind)
85+ isempty (_rule_list (rule_kind)) && return # if no hooks, exit early, nothing to run
8186 already_done_world_age = last_refresh (rule_kind)[]
8287 for method in _rule_list (rule_kind)
8388 _defined_world (method) < already_done_world_age && continue
0 commit comments