import handlers import properties from builtins import builtin import fields import relayer import updates import context state = term.simple("a state of the interpeter where [history] maps " "strings to the user's historical response to those strings, " "and where [computation] is invoked to handle computations", 'history', 'computation') history = fields.named_binding( "the function that maps an interpreter state to the history of commands that " "have been entered in that state", state.head, 'history' ) handler = fields.named_binding( "the function that maps an interpreter state to the computation invoked " "to handle questions in that state", state.head, 'computation' ) def starting_state(): return state(T.from_dict({}), handlers.view_handler()) #TODO it seems like somehow history should be a composite resource locator #(almost a composite field, but there is no object you are accessing it on...)
import term from term import Term as T import fields import termtypes pair_type = termtypes.new_type("the type of pairs") termtypes.set_type(pair_type, T.pair) def to_pair(asker, p): return (fields.get(asker, first(), p), fields.get(asker, second(), p)) first = fields.named_binding( "the function that maps a pair to its first element", T.pair.head, 'a' ) second = fields.named_binding( "the function that maps a pair to its second element", T.pair.head, 'b' )
#Representing terms-------------------------------- #TODO I could probably make Representation a function, #then make a decorator that turns any such function into a "held" #version... quoted_term = term.simple("a term with head [head] head and bindings [bindings]", 'head', 'bindings') term_type = termtypes.new_type("the type of terms") termtypes.set_type(term_type, quoted_term) head = fields.named_binding( "the function that maps a term to its head", quoted_term.head, 'head' ) bindings = fields.named_binding( "the function that maps a term to its bindings", quoted_term.head, 'bindings' ) #referent_of = term.simple("the function that maps a term to the referent of [s] in that term's head", "s") #FIXME this is a dumb hack to cut down on computational time #(though I think that in the real thing these changes would propagate back to the computations) @term.as_head("the function that maps a term to the referent of [s] in that term's head") def referent_of(s): import dictionaries
@list_iterator(snoc.head) def iterate_snoc(asker, init, last): for x in iterator(asker, init): yield x yield last @list_iterator(concat.head) def iterate_concat(asker, a, b): for x in iterator(asker, a): yield x for y in iterator(asker, b): yield y first = fields.named_binding( "the function that maps a list to its first element", cons.head, 'head' ) #FIXME should probably assert that the length is 1 or something? #also maybe there should be a singleton type? only = first tail = fields.named_binding( "the function that maps a list to all but its first element", cons.head, 'tail' ) #Properties---------------------------------------
def visible_children(asker, object): expanded = convert.check_hard(asker, is_expanded(), object) object = asker.refresh(object) if not expanded: return asker.reply(answer=T.from_list([])) else: children = asker.ask(fields.get_field(all_children(), object)).firm_answer return asker.reply(answer=children) @setter(visible_children.head) def set_visibile_children(asker, object, new_value): return asker.ask_tail(fields.set_field(all_children(), object, new_value)) all_children = fields.named_binding( "the function that maps a node of an outline to the list of nodes which are its children", node.head, 'children' ) #Printing lines----------------------------------- #FIXME this should probably emit a string or list of strings rather than just print them... #FIXME this should probably use the conversions machinery line_printer = Dispatcher("line printer", ("lines", "k", "tab")) #tab: the string to use for a tab #k: the number of tabs to use for the outermost line @builtin("print the lines [lines]") def print_lines_simple(asker, lines): return asker.ask_tail(print_lines(lines, T.from_int(0), T.from_str(" ")))
import state import computations import functions from ipdb import set_trace as debug import inspect from decorator import decorator import string @as_head("an interactive environment with sequence of lines [lines] and variable bindings [binding]") def view(lines, bindings): return T(view.head, lines=lines, bindings=bindings) lines_field = fields.named_binding( "the function that maps a view to the sequence of lines rendered in that view", view.head, 'lines' ) bindings_field = fields.named_binding( "the function that maps a view to the variable bindings that should be applied in that view", view.head, 'bindings' ) @builtin("what value will ultimately be returned if the user interacts with [view]?") def predict_output(asker, view): input = asker.ask(predict_input(view)).firm_answer class context: result = None
else: chars = fields.get(asker, list_of_chars(), s) return ''.join(to_char(c) for c in lists.iterator(asker, chars)) literalizer = Dispatcher("string literalizer", ("string",)) #FIXME characters might not be literal, should probably have a character literalizer as well @is_type(string_type) @literalizer(T.simple_string.head) def literalize_simple(asker, list): import lists return ''.join(to_char(asker, c) for c in lists.to_list(asker, list)) list_of_chars = fields.named_binding( "the function that maps a string to its list of characters", T.simple_string.head, 'list' ) @is_type(string_type) @literalizer("the string formed by concatenating [a] with [b]") def concat(asker, a, b): return to_str(asker, a) + to_str(asker, b) #Converting to chars------------------------------ def to_char(asker, c): if c.to_char() is not None: return c.to_char() result = char_literalizer.dispatch(asker, c)