Hard labor wasn’t really used in the Middle Ages, because it involves a lot of the same problems as imprisonment – you still need to confine people when they’re not working, still have to feed, clothe them, you need to hire people to oversee their work, etc.
Hard labor tended to come about during the Early Modern period, when larger, more powerful nation-states needed cheap labor to build fortifications, provide a labor force for overseas colonies, etc. and could more easily manage a penal labor system. Larger armies and police forces to keep prisoners under guard, bigger tax base to afford prisons to house them in, more bureaucrats to manage them, etc.