prodigy.pgcl.compiler

Compiler Frontend

This module provides an API that combines parsing and type-checking all at once. It is just a thin wrapper around the parser, type-checker, and constant substitution.

Refer to prodigy.pgcl.parser and prodigy.pgcl.check for the parser and type-checker respectively. Variable/constant substituion is implemented in prodigy.pgcl.substitute.

compile_pgcl(code, *, substitute_constants=True, predefined_functions={'bernoulli': [VarDecl(var='p', typ=RealType())], 'binomial': [VarDecl(var='n', typ=NatType(bounds=None)), VarDecl(var='p', typ=RealType())], 'geometric': [VarDecl(var='p', typ=RealType())], 'iid': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'logdist': [VarDecl(var='p', typ=RealType())], 'poisson': [VarDecl(var='p', typ=NatType(bounds=None))], 'sample_pgf': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'unif': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))], 'unif_d': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))]})[source]

Parse and type-check a pGCL program.

>>> compile_pgcl("nat x; nat y; x := y")
Program(variables={'x': NatType(bounds=None), 'y': NatType(bounds=None)}, constants={}, parameters={}, functions={}, instructions=[AsgnInstr(lhs='x', rhs=VarExpr('y'))])

>>> compile_pgcl("x := y")
CheckFail(location=..., message='x is not a variable.')
Parameters:
  • code (str)

  • substitute_constants – Whether constant substitution is done on the program, defaults to True.

  • predefined_functions (Dict[str, List[VarDecl]])

Return type:

Program | CheckFail

compile_expr(program, code, *, substitute_constants=True, predefined_functions={'bernoulli': [VarDecl(var='p', typ=RealType())], 'binomial': [VarDecl(var='n', typ=NatType(bounds=None)), VarDecl(var='p', typ=RealType())], 'geometric': [VarDecl(var='p', typ=RealType())], 'iid': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'logdist': [VarDecl(var='p', typ=RealType())], 'poisson': [VarDecl(var='p', typ=NatType(bounds=None))], 'sample_pgf': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'unif': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))], 'unif_d': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))]})[source]

Parse and type-check an expression.

Parameters:
  • program (Program)

  • code (str)

  • substitute_constants – Whether constant substitution is done on the expression, defaults to True.

  • predefined_functions (Dict[str, List[VarDecl]])

Return type:

VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr | CheckFail

compile_expectation(program, code, *, substitute_constants=True, predefined_functions={'bernoulli': [VarDecl(var='p', typ=RealType())], 'binomial': [VarDecl(var='n', typ=NatType(bounds=None)), VarDecl(var='p', typ=RealType())], 'geometric': [VarDecl(var='p', typ=RealType())], 'iid': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'logdist': [VarDecl(var='p', typ=RealType())], 'poisson': [VarDecl(var='p', typ=NatType(bounds=None))], 'sample_pgf': [VarDecl(var='dist', typ=NatType(bounds=None)), VarDecl(var='var', typ=NatType(bounds=None))], 'unif': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))], 'unif_d': [VarDecl(var='start', typ=NatType(bounds=None)), VarDecl(var='end', typ=NatType(bounds=None))]})[source]

Parse and type-check an expectation.

Parameters:
  • program (Program)

  • code (str)

  • substitute_constants – Whether constant substitution is done on the expectation, defaults to True.

  • predefined_functions (Dict[str, List[VarDecl]])

Return type:

VarExpr | BoolLitExpr | NatLitExpr | RealLitExpr | UnopExpr | BinopExpr | CategoricalExpr | SubstExpr | TickExpr | FunctionCallExpr | InferExpr | SampleExpr | CheckFail