The Lose Track Rule for programmers:
If we're thinking of the cards as "little computer programs", then Dominion cards don't have $card->move($target_location) methods, rather, they have $card->move($current_location,$target_location) methods, which, if $card is in $current_location, move it to $target_location and return true, else do nothing and return false. They also don't have $card->get_current_location() methods, so $current_location has to be the place the card is expected to be in.
Examples:
Mining Village's last clause is, "you may { if($this->move($play_area,$trash_pile)) { +$2; } }". Hence, when Mining Village is not in the play area, Lose Track applies, the move() method returns false and therefore you don't get the +$2.
Inn/Watchtower: if you choose to resolve Inn's on-gain effect before Watchtower's reaction effect, then Inn's effect is "you may {$this->move($discard_pile,$deck); } $deck->shuffle();" which succeeds, and then Watchtower's effect is "$inn->move($discard_pile,$deck);" which fails because Inn isn't in the discard pile. If you resolve them in the other order, then Watchtower's effect succeeds, and then Inn's effect fails to move the Inn because it isn't in the discard pile, but then shuffles the deck anyway.