Beispiel #1
0
    return asker.reply(answer=object)

@fast_updater(become.head)
def become_update(asker, object, target):
    return asker.reply(answer=target)

#Reintroducing modifiers--------------------------

@builtin("of [property], what property should be removed from [object] before applying [update] (and restored after), "
    "and which should be kept during the update?")
def modifiers_to_strip(asker, update, property, object):
    return modifier_stripper.dispatch(asker, update, property, object)

modifier_stripper = Dispatcher("modifier stripper", ("update", "property", "object"))

to_keep = fields.atomic_field("the function that sends a question to the property that should be retained in the referenced situation")
to_strip = fields.atomic_field("the function that sends a question to the property "
        "that should be restored later in the referenced situation")

def keep_strip(keep=None, strip=None):
    if keep is None:
        keep = properties.trivial()
    if strip is None:
        strip = properties.trivial()
    return properties.both(
            fields.field_value(to_keep(), keep),
            fields.field_value(to_strip(), strip)
    )

#TODO in the long run we should do something more sophisticated
@modifier_stripper()
#which I think can be done by having a translator that pulls "will have children"
#across the toggl expanded update
@builtin("what is the outline, collapsed to its root, that best expresses [quoted_term]?")
def node_from_term(asker, quoted_term):
    head = asker.ask(fields.get_field(representations.head(), quoted_term)).firm_answer
    bindings = asker.ask(fields.get_field(representations.bindings(), quoted_term)).firm_answer
    return asker.reply(answer=properties.simple_add_modifier(
        node(head, T.empty_list()),
        children_on_expanded(bindings)
    ))

#Outlines to lines--------------------------------

#TODO I should implement caching in a generic way...
cached_lines = fields.atomic_field(
    "the function that maps a node to a sequence of lines that represents it"
)

to_lines_Q = "what sequence of lines should represent the outline with root [root]"
@builtin(to_lines_Q)
def outline_to_lines(asker, root):
    debug()
    result = asker.ask(fields.get_field(cached_lines(), root)).answer
    if result is not None:
        return asker.reply(answer=result)
    base_headline = asker.ask(fields.get_field(headline(), root)).firm_answer
    root = asker.refresh(root)
    prefix = "* " if convert.check_hard(asker, is_pointer(), root) else "  "
    full_headline = strings.string_concat(T.from_str(prefix), base_headline)
    root = asker.refresh(root)
    children = asker.ask(fields.get_field(visible_children(), root)).firm_answer