<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def add_derived_identity(self, id_stmt):
 """Add pattern def for `id_stmt` and all derived identities. The corresponding "ref" pattern is returned. """

        p = self.add_namespace(id_stmt.main_module())
        if id_stmt not in self.identities:   # add named pattern def
            self.identities[id_stmt] = SchemaNode.define("__%s_%s" %
                                                         (p, id_stmt.arg))
            parent = self.identities[id_stmt]
            if id_stmt in self.identity_deps:
                parent = SchemaNode.choice(parent, occur=2)
                for i in self.identity_deps[id_stmt]:
                    parent.subnode(self.add_derived_identity(i))
            idval = SchemaNode("value", parent, p+":"+id_stmt.arg)
            idval.attr["type"] = "QName"
        res = SchemaNode("ref")
        res.attr["name"] = self.identities[id_stmt].attr["name"]
        return res