code: def compile_funsor(expr: Funsor) -> OpProgram:
      """
      Compiles a symbolic :class:`~funsor.terms.Funsor` to an
      :class:`~funsor.ops.program.OpProgram` that runs on backend values.
  
      Example::
  
          # Create a lazy expression.
          a = Variable("a", Reals[3, 3])
          b = Variable("b", Reals[3])
          x = Variable("x", Reals[3])
          expr = a @ x + b
  
          # Evaluate via Funsor substitution.
          data = dict(a=randn(3, 3), b=randn(3), x=randn(3))
          expected = expr(**data).data
  
          # Alternatively evaluate via a program.
          program = compile_funsor(expr)
          actual = program(**data)
          assert (acutal == expected).all()
  
      :param Funsor expr: A funsor expression to evaluate.
      :returns: An op program.
      :rtype: ~funsor.ops.program.OpProgram
      """
      assert isinstance(expr, Funsor)
  
      # Lower and convert to A-normal form.
      lowered_expr = lower(expr)
      anf = list(funsor.interpreter.anf(lowered_expr))
      ids = {}
  
      # Collect constants (leaves).
      constants = []
      for f in anf:
          if isinstance(f, (Number, Tensor)):
              ids[f] = len(ids)
              constants.append(f.data)
  
      # Collect input variables (leaves).
      inputs = []
      for k, d in expr.inputs.items():
          f = Variable(k, d)
          ids[f] = len(ids)
          inputs.append(k)
  
      # Collect operations to be computed (internal nodes).
      operations = []
      for f in anf:
          if f in ids:
              continue  # constant or free variable
          ids[f] = len(ids)
          if isinstance(f, Unary):
              arg_ids = (ids[f.arg],)
              operations.append((f.op, arg_ids))
          elif isinstance(f, Binary):
              arg_ids = (ids[f.lhs], ids[f.rhs])
              operations.append((f.op, arg_ids))
          elif isinstance(f, Tuple):
              arg_ids = tuple(ids[arg] for arg in f.args)
              operations.append((make_tuple, arg_ids))
          elif isinstance(f, tuple):
              continue  # Skip from Tuple directly to its elements.
          else:
              raise NotImplementedError(type(f).__name__)
  
      return OpProgram(constants, inputs, operations)
  
file_path: funsor/funsor/compiler.py
imports: import functools
  import funsor
  from .cnf import Contraction
  from .ops.program import OpProgram, make_tuple
  from .tensor import Tensor
  from .terms import Binary, Funsor, Number, Tuple, Unary, Variable
label_desc: Use image node_id 1 for calling a global function with example usage: compile_funsor(expr) and returns: OpProgram