lapspython.types

Implements types for parsed primitives and lambda expressions.

Classes

ParsedType()

Abstract base class for program parsing.

ParsedPythonType()

Abstract base class for python parsing.

ParsedRType()

Abstract base class for R parsing.

ParsedPrimitive(primitive)

Class parsing primitives for translation to clean Python code.

ParsedRPrimitive(primitive)

Class parsing primitives for translation to clean R code.

ParsedInvented(invented, name)

Class parsing invented primitives for translation to Python.

ParsedRInvented(invented, name)

Class parsing invented primitives for translation to R.

ParsedProgramBase(name, source, args, ...)

Class parsing synthesized programs.

ParsedProgram(name, source, args, imports, ...)

Class parsing synthesized programs.

ParsedRProgram(name, source, args, imports, ...)

Class parsing synthesized programs.

ParsedGrammar(primitives, invented[, mode])

Data class containing parsed (invented) primitives.

CompactFrontier(frontier[, annotation])

Data class containing the important specs of extracted frontiers.

CompactResult(hit, miss)

Class containing (compact) extracted frontiers.

class lapspython.types.ParsedType[source]

Abstract base class for program parsing.

as_dict() dict[source]

Return member attributes as dict for json dumping.

classmethod parse_argument_types(arg_types: dreamcoder.type.TypeConstructor) list[source]

Flatten inferred nested type structure of primitive.

Parameters

arg_types (dreamcoder.type.TypeConstructor) – Inferred types.

Returns

Flat list of inferred types.

Return type

list

resolve_variables(args: list, return_name: str) str[source]

Substitute default arguments in source.

Parameters
  • args (list) – List of new argument names.

  • return_name (string) – Variable name to replace the return statement with

Returns

Source with replaced variable names

Return type

string

replace_return_statement(return_name, source)[source]

Substitute return statement with variable assignment.

Parameters
  • return_name (string) – Variable name to replace return with.

  • source (string) – Source code to apply substitution to.

Return type

string

class lapspython.types.ParsedPythonType[source]

Abstract base class for python parsing.

class lapspython.types.ParsedRType[source]

Abstract base class for R parsing.

class lapspython.types.ParsedPrimitive(primitive: dreamcoder.program.Primitive)[source]

Class parsing primitives for translation to clean Python code.

parse_source(implementation) str[source]

Resolve lambdas and arguments to produce cleaner Python code.

Parameters

implementation (callable) – The function referenced by primitive

Returns

New source code

Return type

string

get_imports(implementation) set[source]

Find import modules that might be required by primitives.

Parameters

implementation (function) – The function referenced by a primitive

Returns

A set of module names as strings

Return type

set

get_dependencies(implementation) list[source]

Find functions called by primitives that are not built-ins.

Parameters

implementation (function) – The function referenced by a primitive

Returns

A list of (function name, source) tuples

Return type

list

resolve_lambdas() lapspython.types.ParsedPrimitive[source]

Remove lambda functions from source and extend list of arguments.

Returns

New, cleaner parsed primitive

Return type

lapspython.types.ParsedPrimitive

replace_return_statement(return_name, source) str[source]

Substitute return statement with variable assignment.

Parameters
  • return_name (string) – Variable name to replace return with.

  • source (string) – Source code to apply substitution to.

Return type

string

class lapspython.types.ParsedRPrimitive(primitive: dreamcoder.program.Primitive)[source]

Class parsing primitives for translation to clean R code.

parse_source(name: str, path: str, is_dep=False) str[source]

Extract source code of primitive from R file.

Parameters

handle (string) – Function name in source file.

Returns

Source code of corresponding function.

Return type

string

get_imports(path) set[source]

Find import modules that might be required by primitives.

Parameters

implementation (function) – The function referenced by a primitive

Returns

A set of module names as strings

Return type

set

get_dependencies(implementation)[source]

Find functions called by primitives that are not built-ins.

Parameters

implementation (function) – The function referenced by a primitive

Returns

A list of (function name, source) tuples

Return type

list

get_args(header: str)[source]

Get list of arguments from function code.

Parameters

source (string) – Function code

resolve_lambdas() lapspython.types.ParsedRPrimitive[source]

No lambdas in R, but required for backwards compatibility.

replace_return_statement(return_name, source)[source]

Substitute return statement with variable assignment.

Parameters
  • return_name (string) – Variable name to replace return with.

  • source (string) – Source code to apply substitution to.

Return type

string

class lapspython.types.ParsedInvented(invented: dreamcoder.program.Invented, name: str)[source]

Class parsing invented primitives for translation to Python.

resolve_variables(args: list, return_name: str) str[source]

Instead arguments in function call rather than definition.

class lapspython.types.ParsedRInvented(invented: dreamcoder.program.Invented, name: str)[source]

Class parsing invented primitives for translation to R.

resolve_variables(args: list, return_name: str) str[source]

Instead arguments in function call rather than definition.

class lapspython.types.ParsedProgramBase(name: str, source: str, args: list, imports: set, dependencies: set)[source]

Class parsing synthesized programs.

abstract verify(examples: list) bool[source]

Verify code for a list of examples from task.

Parameters

examples (list) – A list of (input, output) tuples

Returns

Whether the translated program is correct.

Return type

bool

class lapspython.types.ParsedProgram(name: str, source: str, args: list, imports: set, dependencies: set)[source]

Class parsing synthesized programs.

verify(examples: list) bool[source]

Verify code for a list of examples from task.

Parameters

examples (list) – A list of (input, output) tuples

Returns

Whether the translated program is correct.

Return type

bool

class lapspython.types.ParsedRProgram(name: str, source: str, args: list, imports: set, dependencies: set)[source]

Class parsing synthesized programs.

verify(examples: list) bool[source]

Verify code for a list of examples from task.

Parameters

examples (list) – A list of (input, output) tuples

Returns

Whether the translated program is correct.

Return type

bool

class lapspython.types.ParsedGrammar(primitives: dict, invented: dict, mode: str = 'python')[source]

Data class containing parsed (invented) primitives.

as_dict()[source]

Return member attributes as dict for json dumping.

class lapspython.types.CompactFrontier(frontier: dreamcoder.frontier.Frontier, annotation: str = '')[source]

Data class containing the important specs of extracted frontiers.

class lapspython.types.CompactResult(hit: dict, miss: dict)[source]

Class containing (compact) extracted frontiers.

get_best() List[Dict][source]

Return the HIT frontiers as dict with best posteriors.

Returns

A list of minimal CompactFrontier dictionaries.

Return type

List[Dict]

sample() dict[source]

Return a random HIT frontier with valid translation.

Returns

A minimal CompactFrontier dictionary.

Return type

dict