Challenge 3: A Task Only Bidirectional Unification Can Do — Possible Solution ==================================================================== // Concrete example: given a fact recording a completed addition, // like sum(3, 4, 7)., unification alone can be used to ask genuinely // different questions depending on which arguments are already known // and which are still variables -- with NO separate code written for // each direction. // // sum(3, 4, 7). // // ?- sum(3, 4, X). % "what is 3 + 4?" -> X = 7 // ?- sum(3, X, 7). % "what added to 3 gives 7?" -> X = 4 // ?- sum(X, 4, 7). % "what added to 4 gives 7?" -> X = 3 // // All three queries use the EXACT SAME single fact -- there is no // "forward" version and "reverse" version written separately. Each // query simply leaves a different argument position as an unbound // variable, and unification fills in whichever position is missing, // because unification doesn't care which positions are "inputs" and // which are "outputs" -- it only cares about making both sides // identical. // // Haskell's pattern matching genuinely cannot do this. A Haskell // function like `add :: Int -> Int -> Int` has a FIXED direction -- // two Ints go in, one Int comes out, always in that order. To answer // "what added to 3 gives 7?" in Haskell, a programmer would need to // write an entirely SEPARATE function (e.g. subtract 3 from 7) -- // Haskell's pattern matching can only ever extract pieces from an // already-fully-known value; it can never "run backwards" to solve // for a missing piece of that value the way unification just did // automatically, for free, from one single fact. // // This is exactly what "bidirectional" buys: one relation, usable to // answer multiple genuinely different questions, with zero extra code // written for each direction. WHY THIS WORKS AS AN ANSWER ------------------------------ This gives a concrete, working example (a single sum/3 fact answering three structurally different questions) and correctly explains that Haskell would require separate functions for each direction, directly demonstrating what bidirectionality provides that one-directional pattern matching cannot.