def percolate_declares(program: Program) -> Program:
    declare_program = Program()
    instrs_program = Program()
    for instr in program:
        if isinstance(instr, Declare):
            declare_program += instr
        else:
            instrs_program += instr
    p = declare_program + instrs_program
    p._defined_gates = program._defined_gates
    return p