Skip to content

Commit

Permalink
End of day checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
vanschelven committed Sep 26, 2017
1 parent 097c6ad commit 6cae10b
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 33 deletions.
24 changes: 22 additions & 2 deletions doctests/lexical_addressing_x.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
>>> from dsn.form_analysis.structure import ValueForm, VariableForm, DefineForm, LambdaForm
>>> from dsn.form_analysis.structure import ValueForm, VariableForm, DefineForm, LambdaForm, SequenceForm
>>> from dsn.form_analysis.structure import FormList, Symbol, SymbolList

>>> from dsn.form_analysis.lexical_addressing_x import lexical_addressing_x
>>> from dsn.form_analysis.lexical_addressing_x import lexical_addressing_x, find_lambda_children

>>> the_lambda = LambdaForm(
... SymbolList([Symbol("param")]),
Expand All @@ -16,3 +16,23 @@

>>> sorted(lexical_addressing_x(the_context, the_lambda).items())
[('a', None), ('b', 3), ('definition', 0), ('param', 0)]

>>> nested_lambdas = SequenceForm(FormList([
... LambdaForm(SymbolList([]), FormList([
... LambdaForm(SymbolList([]), FormList([])),
... SequenceForm(FormList([LambdaForm(SymbolList([]), FormList([
... LambdaForm(SymbolList([]), FormList([])),
... ]))])),
... ]))
... ]))

>>> find_lambda_children(nested_lambdas)
[[<dsn.form_analysis.structure.LambdaForm object at 0x000000000000>]]

>>> [nested_lambdas.sequence.the_list[0]]

>>> find_lambda_children(nested_lambdas.sequence.the_list[0].body.the_list[0])
[[<dsn.form_analysis.structure.LambdaForm object at 0x000000000000>]]

>>> find_lambda_children(nested_lambdas.sequence.the_list[0].body.the_list[1])
asdf
30 changes: 1 addition & 29 deletions dsn/form_analysis/free_variables.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from dsn.form_analysis.structure import (
VariableForm,
LambdaForm,
IfForm,
DefineForm,
ApplicationForm,
SequenceForm,
)

from dsn.form_analysis.utils import general_means_of_collection
from dsn.form_analysis.somewhere import collect_definitions


Expand Down Expand Up @@ -53,30 +49,6 @@ def free_variables(form):
return general_means_of_collection(form, free_variables, lambda l: set.union(*l), set())


def general_means_of_collection(form, f, combine, identity):
# Apply thing_to_do on all this form's child (but no lower descendants) forms.

# The behavior for MalformedForm is TBD; in any case it has no child forms
# VariableForm, ValueForm & QuoteForm have no child-forms

if isinstance(form, IfForm):
return combine([f(form.predicate), f(form.consequent), f(form.alternative)])

if isinstance(form, DefineForm):
return f(form.definition)

if isinstance(form, LambdaForm):
return combine([f(child) for child in form.body])

if isinstance(form, ApplicationForm):
return combine([f(form.procedure)] + [f(child) for child in form.arguments])

if isinstance(form, SequenceForm):
return combine([f(child) for child in form.sequence])

return identity


# ## Incremental analysis
#
# Here are some thoughts about an incremental analysis of both `collect_definitions` and `free_variables`.
Expand Down
100 changes: 98 additions & 2 deletions dsn/form_analysis/lexical_addressing_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

from dsn.form_analysis.free_variables import free_variables
from dsn.form_analysis.somewhere import collect_definitions
from dsn.form_analysis.utils import general_means_of_collection
from dsn.form_analysis.structure import (
LambdaForm,
)


def set_union(l):
Expand All @@ -16,9 +20,20 @@ def dict_union(l):
return result


def add_lists(*args):
result = []
for arg in args:
result += arg
return result


def lexical_addressing_x(surrounding_scope, lambda_form):
# Note that the present free-variable-driven form has no values for unused variables. This is considered useless
# (but I might change my mind once I get to the incremental analysis)
"""
:: {symbol: scopes_up_count}, lambda_form -> {symbol: scopes_up_count}
Note that this version (free-variable-driven) contains no keys for unused variables.
(but I might change my mind once I get to the incremental analysis)
"""
result = {}

parameters = {p.symbol for p in lambda_form.parameters}
Expand All @@ -40,3 +55,84 @@ def lexical_addressing_x(surrounding_scope, lambda_form):
result[symbol] = looked_up + 1

return result


def lexical_addressing_downwards(lambda_form):
pass
# * finding all scopes (lambda-expressions) in the tree; constructing a scope-tree out of that.
# gebleven bij: do it as a Tree!

# HIER DAN
# Eerste vraag is direct: wat is je beginpunt? Let's assume (and write that down): a lambda;
# dan kunnen we dus de lambda-children van je body verzamelen.
# and for each of those, the lambda-children of their bodies.

# * following the scope-tree downwards, construct {symbol: scopes_up_count} ... this is the thing I already did.


def find_lambda_children(form):
# recurse into the form, identifying subscopes, but don't recurse into any lambdas
if isinstance(form, LambdaForm):
return [form]

return general_means_of_collection(form, find_lambda_children, add_lists, [])


class LambdaTree(object):
def __init__(self, lambda_form, children):
self.lambda_form = lambda_form
self.children = children


def some_lambda_tree(lambda_form):
lambda_children = add_lists([find_lambda_children(f) for f in lambda_form.body])
children = [some_lambda_tree(c) for c in lambda_children]
return LambdaTree(lambda_form, children)


class LambdaTreeWithLAStuff(object):
def __init__(self, lambda_form, la_stuff, children):
self.lambda_form = lambda_form
self.la_stuff = la_stuff
self.children = children


def construct_lexical_addresses_down_the_tree_initial(lambda_tree):
return construct_lexical_addresses_down_the_tree(lambda_tree, {})


def construct_lexical_addresses_down_the_tree(lambda_tree, surrounding_scope):
la = lexical_addressing_x(surrounding_scope, lambda_tree.lambda_form)

constructed_children = [
construct_lexical_addresses_down_the_tree(child, la)
for child in lambda_tree.children
]

return LambdaTreeWithLAStuff(
lambda_tree.lambda_form,
la,
constructed_children,
)


class LambdaTree4Prop(object):
# alternatively: some mechanism of decoration.
# alternatively: have a single point in your control-flow which applies all decorations at once.
# I like the above option... later though.

def __init__(self, lambda_form, la_stuff, name_dependencies, children):
self.lambda_form = lambda_form
self.la_stuff = la_stuff
self.name_dependencies = name_dependencies
self.children = children


class MostCompleteScope(object):

def __init__(self, parent, lambda_form, la_stuff, name_dependencies, children):
self.parent = parent
self.lambda_form = lambda_form
self.la_stuff = la_stuff
self.name_dependencies = name_dependencies
self.children = children
31 changes: 31 additions & 0 deletions dsn/form_analysis/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from dsn.form_analysis.structure import (
LambdaForm,
IfForm,
DefineForm,
ApplicationForm,
SequenceForm,
)


def general_means_of_collection(form, f, combine, identity):
# Apply thing_to_do on all this form's child (but no lower descendants) forms.

# The behavior for MalformedForm is TBD; in any case it has no child forms
# VariableForm, ValueForm & QuoteForm have no child-forms

if isinstance(form, IfForm):
return combine([f(form.predicate), f(form.consequent), f(form.alternative)])

if isinstance(form, DefineForm):
return f(form.definition)

if isinstance(form, LambdaForm):
return combine([f(child) for child in form.body])

if isinstance(form, ApplicationForm):
return combine([f(form.procedure)] + [f(child) for child in form.arguments])

if isinstance(form, SequenceForm):
return combine([f(child) for child in form.sequence])

return identity

0 comments on commit 6cae10b

Please sign in to comment.