Skip to content

Commit

Permalink
Added form_analysis/from_s_expr
Browse files Browse the repository at this point in the history
And used this in one test

(As part of this, I needed to convert ... to an actually well-formed Lambda expression, i.e.
one with some body - when constructing the Python objects by hand this is not a requirement)
  • Loading branch information
vanschelven committed Oct 3, 2017
1 parent f38f597 commit ac593a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
20 changes: 9 additions & 11 deletions doctests/lexical_addressing_x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
>>> from dsn.form_analysis.structure import FormList, Symbol, SymbolList

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

>>> the_lambda = LambdaForm(
... SymbolList([Symbol("param")]),
Expand All @@ -18,23 +19,20 @@
>>> 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([])),
... ]))])),
... ]))
... ]))
>>> nested_lambdas = from_s_expr(s_expr_from_python(
... ("begin",
... ("lambda", (), ("lambda", (), "1"),
... ("begin",
... ("lambda", (), ("lambda", (), "2"),),),),)))

>>> find_lambda_children(nested_lambdas)
[(lambda () (lambda ()) (begin (lambda () (lambda ()))))]
[(lambda () (lambda () 1) (begin (lambda () (lambda () 2))))]

>>> find_lambda_children(nested_lambdas.sequence.the_list[0].body.the_list[0])
[(lambda ())]
[(lambda () 1)]

>>> find_lambda_children(nested_lambdas.sequence.the_list[0].body.the_list[1])
[(lambda () (lambda ()))]
[(lambda () (lambda () 2))]

>>> some_lambda_tree(nested_lambdas.sequence.the_list[0])
(L [(L []), (L [(L [])])])
Expand Down
38 changes: 38 additions & 0 deletions dsn/form_analysis/from_s_expr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Debug only: a mechanism to construct Form-structures i.e. directly out of s-expressions.
Implemented by concocting a history for the s-expression, and sending that through the usual pipeline.
Some reasons this is debug-only:
* fundamentally, the idea that we work on structures directly, while ignoring their histories
* the fact that we use other debug-only mechanisms, i.e. concoct_history
* the ad hoc, non-shard, introduction of memoization and stores.
At some point in the future we might implement a direct mechanism, which entirely skips the historical view.
Disadvantage: having to duplicate the effort; however, this disadvantage might double as an advantage (the redundant
effort may serve as a reference-implementation for comparison in tests)"""

from dsn.s_expr.concoct import concoct_history

from hashstore import NoutHashStore
from memoization import Stores, Memoization

from dsn.s_expr.legato import NoteNout, NoteCapo, NoteNoutHash

from dsn.historiography.legato import HistoriographyNoteNoutHash, HistoriographyNoteNout, HistoriographyNoteCapo
from dsn.form_analysis.into import construct_form_note
from dsn.form_analysis.construct import construct_form


def from_s_expr(s_expr):
m = Memoization()
p = NoutHashStore(NoteNoutHash, NoteNout, NoteCapo)
historiography_note_nout_store = NoutHashStore(
HistoriographyNoteNoutHash, HistoriographyNoteNout, HistoriographyNoteCapo)
stores = Stores(p, historiography_note_nout_store)

nout_and_hash_list = concoct_history(m, stores, s_expr)

form_note, form_note_nout_hash = construct_form_note(m, stores, nout_and_hash_list[-1].nout_hash)
form = construct_form(m, stores, form_note_nout_hash)
return form

0 comments on commit ac593a3

Please sign in to comment.