class_docstr: Functor for translating Relay programs into Python ASTs.
class_docstr_tok: ["Functor","for","translating","Relay","programs","into","Python","ASTs","."]
docstr: Given a match clause pattern and a clause body,
  generates a Python function that when called with an ADT
  that matches the pattern, returns the result of evaluating
  the clause body. This function returns a function definition
  and the name of the generated function.
docstr_tok: ["Given","a","match","clause","pattern","and","a","clause","body",",","generates","a","Python","function","that","when","called","with","an","ADT","that","matches","the","pattern",",","returns","the","result","of","evaluating","the","clause","body",".","This","function","returns","a","function","definition","and","the","name","of","the","generated","function","."]
code: def create_match_clause_body(self, pattern: Pattern, body: Expr):
      """Given a match clause pattern and a clause body,
      generates a Python function that when called with an ADT
      that matches the pattern, returns the result of evaluating
      the clause body. This function returns a function definition
      and the name of the generated function."""
  
      def collect_var_assignments(pat, val):
          """This helper function ensures that the pattern is used to
          properly assign all subfields of the given AST for use
          in the clause body
  
          E.g., for PatternConstructor(A, PatternVar(v), PatternWildcard(),
          PatternConstructor(B, PatternVar(w)))
          we would want to have
          v = a.fields[0]
          w = a.fields[2].fields[0]
          """
          if isinstance(pat, relay.PatternWildcard):
              return []
          if isinstance(pat, relay.PatternVar):
              return [
                  Assign([self.include_var(pat.var, assign=True)], val)
              ]
          # constructor pattern: assign each field of the value
          # based on subpatterns
          assignments = []
          for i in range(len(pat.patterns)):
              # we want the assignments for val.fields[i]
              field = ast.Subscript(
                  ast.Attribute(val, "fields", Load()),
                  ast.Index(Num(i)),
                  Load(),
              )
              assignments += collect_var_assignments(
                  pat.patterns[i], field
              )
          return assignments
  
      func_name = self.generate_function_name("_match_clause_body")
      arg_name = self.generate_var_name("_match_clause_body")
  
      clause_body, defs = self.visit(body)
      assignments = collect_var_assignments(
          pattern, Name(arg_name, Load())
      )
  
      func_def = self.create_def(
          func_name,
          [arg_name],
          defs + assignments + [Return(clause_body)],
      )
      return (func_def, func_name)
  
imports: import sys
  import ast
  from ast import alias, Assign, Load, Name, NameConstant, Num, Return, Store, Str
  import re
  import tvm
  from tvm import relay
  from tvm.relay.adt import Pattern
  from tvm.relay.backend import te_compiler
  from tvm.relay.expr import Expr, GlobalVar, Var
  from tvm.relay.function import Function
  from tvm.relay.expr_functor import ExprFunctor
label_desc: Use image node_id 20 for calling the PythonConverter obj's underlying member method code with example usage: obj.create_match_clause_body(pattern, body) and returns: func_def,  func_name, assignments, list, list