Pattern Matching - Prolog vs. Haskell

前端 未结 5 1856
再見小時候
再見小時候 2021-02-02 07:42

This is not a homework question, rather an exam study guide question. What is the difference between pattern matching in Prolog Vs Haskell?

I\'ve done some research and

5条回答
  •  滥情空心
    2021-02-02 07:50

    The difference between pattern matching as in Haskell and Prolog's unification stems from the fundamentally different rôle of variables in both languages.

    In Haskell, variables hold values. Concrete values. Such a value might not have been computed yet, and it might even be ⊥, but otherwise it is one concrete value. In Haskell you cannot take a variable and only state some properties of its value first.

    So pattern matching always means that a concrete value is matched against a pattern which contains some variables. The outcome of such a matching is either failure or a binding of the variables to concrete values. In Haskell this is further restricted to avoid the need of general comparison, which would imply that class Eq is defined for the terms being matched.

    In Prolog, however, variables may refer to a set of possible solutions. Variables may occur anywhere - also somewhere between other values. Unification now ensures that the stated equalities still hold and the result is represented optimally, i.e. the most general unifier is computed.

    
    | ?- length(L,5).                      
    L = [_,_,_,_,_]
    | ?- length(L,5), maplist(=(E),L).
    L = [E,E,E,E,E]
    

    So Prolog does not answer here with concrete values like L = [1,1,1,1,1] or L = [[],[],[],[],[]] but gives the most general unifier as an answer which contains all those concrete values.

提交回复
热议问题