Doing Melacha on Shabbos is a capital crime. But doing melacha to save a life is not only allowed, it is a mitzva. Two questions arise. Why doesn't the rule of Ho'il allow all melacha on Shabbos since the fruit of the melacha might be used for a sick person? Also, even assuming that Ho'il does not allow melacha on Shabbos, what if a person was intentionally mechallel Shabbos, but in fact the fruit of his melacha was needed for and used by a sick person? Would this retroactively kasher/excuse his chillul Shabbos?
The answers aren't compressible.
Our parsha tells us of the M'kosheish, a man that openly and rebelliously was mechallel Shabbos, and that he was found guilty of this crime and executed.
As we see, Chillul Shabbos is a capital crime. More importantly, the capital punishment is not merely theoretical. It can actually be carried out. There are two fundamental questions that this raises.
A. In Menachos 64a DH Le'haalos he says
Let's assume that Ho'il does not apply to Melacha on Shabbos. Even so, there ought to be a way to kasher chillul Shabbos.
If a person was willfully mechallel Shabbos, and as it turned out, the fruit of his chillul Shabbos was actually needed to save someone's life, such that if he hadn't done the melacha, it would have been a mitzva to do it, is he pattur? Does this circumstance, which the malefactor was not aware of but which did exist at the time of the chillul kasher his chillul Shabbos, or does pikuach nefesh require awareness of the hetter to be chall?
A. Rav Yosef Dovid Zintsheim (in his Yad Dovid on Menachos, available from Machon Yerushalayim,) writes that this hangs on the Machlokes Rabba and Rava in Menachos 64a, and that according to Rabba, if you end up using the cooked food for the sick person, the chillul Shabbos will be kashered. In other words, it will lemafrei'a not be called chillul.
B. Reb Meir Simcha (Ohr Sameiach 2 Shabbos 16) modifies this slightly, and says that our Gemara is only talking about Skila, that there won't be missas beis din, but certainly the person needs kapara no less than the woman in "va'hashem yislach lah," in Nazir 23a, where the Gemara says that a person who thought the food was treif and ate it anyway, and it turned out it was kosher, this person needs kapara and even is chayav Makkas Mardus.
C. Reb Meir Don Plotzki (Kli Chemda in Vayechi) says that our Gemara is talking about the Korbon for a Shogeig, but even Rabba agrees that the malefactor is chayav Missas Beis Din, or at least Kareis.
D. Reb Elchonon in Kovetz Shiurim Pesachim 193, to Daf 46b, and the Meiri he brings there, who says that even according to Rav Chisda, who doesn't hold of Ho'il, if guests do show up he's pattur; the same would apply to Shabbos and Pikuach Nefesh. (Don't tell me that there's a difference between cooking on Yomtov and pikuach nefesh on Shabbos. The Gemara in Pesachim 47b makes it clear that the rationale of Ho'il applies to cases of Dechiya.) In any case, Reb Elchonon disagrees with the Meiri.
Question two has an interesting permutation. Borer, separating good from bad, is muttar only if you are going to eat/use the good now. If you are putting it away for another time, e.g., you are being borer Friday night for use at Seuda Shlishis, that is pure Borer and is Chillul Shabbos. Reb Yosef Chaim of Baghdad (Rav P'alim OC 1:12) discusses a case where the person was borer good from bad to eat later, but then he changed his mind and ate it right away. Pattur or Chayav? Also, what if he was borer to eat right away, but then didn't. Pattur or Chayav? You can see that the issue is similar.
A. Rav Yosef Chaim says that all that matters is what he was intending when he did the breira. Unlike Yomtov, there is a din of M'leches Machsheves on Shabbos. This rule can apply to say that if he did the melacha with the intent that is part of the definition of the melacha, then it is a melacha, and nothing that happens later will change that. Furthermore, this case is not exactly like the Gemara in Menachos. In Menachos, what he did was an act that was muttar, although he did not know it. Here, the act is assur. What he does later does not change the nature of the act retroactively.
B. He says that if not for this answer, we would have a conundrum. How can any borer be chayav when the rule of Ho'il would say that since he might eat it right away, it should be muttar whether he does or doesn't eat it now. Tosfos' in Pesachim will not help us, because this is obviously Shchiach. We must say, therefore, that Ho'il is totally inapplicable to Shabbos, because the rule of M'leches Machsheves defines the melacha on the basis of intent, irrespective of what he could have planned and what might happen.
There was a Rov in Chicago many years ago by the name of Rabbi Yosef Kagan. He was a grandson of the Chafetz Chaim, whose daughter married a man named Kagan, just like her own name. He was a great talmid chacham, but was not the type to seek out people to talk to. In 1961 he printed a little sefer called עיוני הלכה והגיונות, and one of the divrei Torah is on our sugya. He also talks about how Meleches Machsheves plays out in the Gemara in Menachos. He reads the following into the Binyan Shlomo: that even Rava who holds that one who caught fish and saved a drowning child is chayav, that is only when he intended to catch fish and he caught fish, and the child was not what he had in mind. Only then does Rava hold that Meleches Machsheves makes him chayav. But if he cooked something to eat on Shabbos, and it turned out to be needed by a choleh, everyone would agree that he is pattur. To the omniscient view, his entire act was muttar. Only where he also caught fish, or by korbanos, where the din of Hiddur requires awareness of Hiddur, does Rava argue. He then uses Meleches Machsheves just like Rav Yosef Chaim, to eliminate Ho'il from Shabbos calculations. He reads this pshat into the Tosfos there DH L'haalos.
The Gemara in Pesachim 47b asks, if we use Ho'il, then why is a person who plows on Yomtov chayav? Plowing would be muttar if he were doing it to turn over soil to use for Kisui HaDam, so even if he's doing it for a different reason, ho'il should be mattir- Ho'il if he were to shecht a bird he would need it for kisui, under which circumstances charisha would be muttar.
Tosfos brings that the Rashbam understands the Gemara to mean that Charisha for Kisui is a Melacha She'eina Tzricha LeGufa. If he would use the dirt for Kisui, it would turn out that the Charisha was muttar because it was a Melacha She'eina Tzricha LeGufa. The R'i argues on the Rashbam. He says that if you say "Ho'il that I could have done it as an eina tzricha legufa it would be muttar, so it's muttar now too," then "ביטלת כל מלאכת שבת" once again. So Tosfos says a different pshat. But this leaves us to wonder, why indeed don't you say a hetter of Ho'il on all melachos Shabbos, Ho'il I could end up using the fruit of the melacha in a way that makes the melacha eina tzricha le'gufa?
Reb Meir Simcha in the Ohr Sameach (1 Yomtov 7, last paragraph) says a yesod:
Ho'il only works when in either case it is a melacha, but in the ho'il case, it is a melacha that is muttar. But kavana of eina tzricha le'gufa is not a melacha at all; what you did was a melacha, and ho'il cannot reclassify what you did as a non-melacha.
Perhaps we can say the same thing to explain why ho'il won't work to be mattir on the basis of using it for a choleh. It could be that just like hana'a from a mitzva is not called hana'a, so too, melacha for a choleh is not in the hagdara of melacha at all. Ho'il cannot take a melacha and make it into a non-melacha. I know this is highly debatable, but if Rav Shach's Avi Ezri can be full of svaros like this, why can't I say them once in a while?
Reb Akiva Eiger in Tshuvos 5, Chasam Sofer in Tshuvos 79. If you've gotten this far, look them up yourself.