Is Pattern Matching as expressive as Case Expression in Haskell?











up vote
1
down vote

favorite












Nomenclature



The term expressive in the question shall bear the same meaning as in the following sentence:




A Turing Machine is as expressive as Lambda Calculus.




Introduction



While learning Haskell, I had noticed that there are 2 ways to achieve the same effect, namely Pattern Matching and Case Expression. For example,



-- Pattern Matching
not True = False
not False = True

-- Case Expression
not x = case x of True -> False
False -> True


I am also fully aware that Pattern Matching is actually a form of syntax sugar that will be compiled into Case Expression.



Therefore, we can be very sure that every Pattern Matching expression can be converted to Case Expression.



Question



So, this is my question: Is the following statement true?




Every Case Expression can be re-written using Pattern Matching.




Regardless of the truthiness of the statement above, I would be glad if you could provide a formal proof to support your answer.



P/S



The motivation that drove me to seek answer for this question is because I'm currently designing my own language, which I wish to have as less feature as possible, but not too minimal like Lambda Calculus which only have 3 features.










share|cite|improve this question
























  • Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
    – pdexter
    Nov 20 at 8:17










  • Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
    – chi
    Nov 20 at 10:04






  • 1




    You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
    – Derek Elkins
    Nov 20 at 21:15










  • @Derek Thanks for the white paper !
    – Wong Jia Hau
    Nov 21 at 0:25















up vote
1
down vote

favorite












Nomenclature



The term expressive in the question shall bear the same meaning as in the following sentence:




A Turing Machine is as expressive as Lambda Calculus.




Introduction



While learning Haskell, I had noticed that there are 2 ways to achieve the same effect, namely Pattern Matching and Case Expression. For example,



-- Pattern Matching
not True = False
not False = True

-- Case Expression
not x = case x of True -> False
False -> True


I am also fully aware that Pattern Matching is actually a form of syntax sugar that will be compiled into Case Expression.



Therefore, we can be very sure that every Pattern Matching expression can be converted to Case Expression.



Question



So, this is my question: Is the following statement true?




Every Case Expression can be re-written using Pattern Matching.




Regardless of the truthiness of the statement above, I would be glad if you could provide a formal proof to support your answer.



P/S



The motivation that drove me to seek answer for this question is because I'm currently designing my own language, which I wish to have as less feature as possible, but not too minimal like Lambda Calculus which only have 3 features.










share|cite|improve this question
























  • Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
    – pdexter
    Nov 20 at 8:17










  • Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
    – chi
    Nov 20 at 10:04






  • 1




    You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
    – Derek Elkins
    Nov 20 at 21:15










  • @Derek Thanks for the white paper !
    – Wong Jia Hau
    Nov 21 at 0:25













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Nomenclature



The term expressive in the question shall bear the same meaning as in the following sentence:




A Turing Machine is as expressive as Lambda Calculus.




Introduction



While learning Haskell, I had noticed that there are 2 ways to achieve the same effect, namely Pattern Matching and Case Expression. For example,



-- Pattern Matching
not True = False
not False = True

-- Case Expression
not x = case x of True -> False
False -> True


I am also fully aware that Pattern Matching is actually a form of syntax sugar that will be compiled into Case Expression.



Therefore, we can be very sure that every Pattern Matching expression can be converted to Case Expression.



Question



So, this is my question: Is the following statement true?




Every Case Expression can be re-written using Pattern Matching.




Regardless of the truthiness of the statement above, I would be glad if you could provide a formal proof to support your answer.



P/S



The motivation that drove me to seek answer for this question is because I'm currently designing my own language, which I wish to have as less feature as possible, but not too minimal like Lambda Calculus which only have 3 features.










share|cite|improve this question















Nomenclature



The term expressive in the question shall bear the same meaning as in the following sentence:




A Turing Machine is as expressive as Lambda Calculus.




Introduction



While learning Haskell, I had noticed that there are 2 ways to achieve the same effect, namely Pattern Matching and Case Expression. For example,



-- Pattern Matching
not True = False
not False = True

-- Case Expression
not x = case x of True -> False
False -> True


I am also fully aware that Pattern Matching is actually a form of syntax sugar that will be compiled into Case Expression.



Therefore, we can be very sure that every Pattern Matching expression can be converted to Case Expression.



Question



So, this is my question: Is the following statement true?




Every Case Expression can be re-written using Pattern Matching.




Regardless of the truthiness of the statement above, I would be glad if you could provide a formal proof to support your answer.



P/S



The motivation that drove me to seek answer for this question is because I'm currently designing my own language, which I wish to have as less feature as possible, but not too minimal like Lambda Calculus which only have 3 features.







functional-programming haskell






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Nov 20 at 8:22

























asked Nov 20 at 8:12









Wong Jia Hau

1605




1605












  • Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
    – pdexter
    Nov 20 at 8:17










  • Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
    – chi
    Nov 20 at 10:04






  • 1




    You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
    – Derek Elkins
    Nov 20 at 21:15










  • @Derek Thanks for the white paper !
    – Wong Jia Hau
    Nov 21 at 0:25


















  • Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
    – pdexter
    Nov 20 at 8:17










  • Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
    – chi
    Nov 20 at 10:04






  • 1




    You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
    – Derek Elkins
    Nov 20 at 21:15










  • @Derek Thanks for the white paper !
    – Wong Jia Hau
    Nov 21 at 0:25
















Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
– pdexter
Nov 20 at 8:17




Pattern matching at the function head is compiled, more or less, to case expressions in the function body. Further, your use of case expressions involves pattern matching; pattern matching vs case expressions is a bit of an apples-to-oranges comparison.
– pdexter
Nov 20 at 8:17












Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
– chi
Nov 20 at 10:04




Note that Haskell-specific questions are off-topic here. However, pattern matching is a concept general enough that this question can be made on-topic, removing the Haskell-specific parts.
– chi
Nov 20 at 10:04




1




1




You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
– Derek Elkins
Nov 20 at 21:15




You definitely don't want the meaning of "expressive" that makes Turing machines and the untyped lambda calculus equally "expressive". Haskell without pattern matching or Haskell without case statements are both Turing-complete and so would be equally as "expressive" by that meaning. Haskell without either is Turing-complete. It is very rare that you want to compare programming languages based on what functions they can compute. See On the expressive power of programming languages by Matthias Felleisen for an alternative.
– Derek Elkins
Nov 20 at 21:15












@Derek Thanks for the white paper !
– Wong Jia Hau
Nov 21 at 0:25




@Derek Thanks for the white paper !
– Wong Jia Hau
Nov 21 at 0:25










1 Answer
1






active

oldest

votes

















up vote
4
down vote













The expression



case e of
p1 -> e1
p2 -> e2
...


can be rewritten as



let k p1 = e1
k p2 = e2
...
in k e


where k is a fresh identifier.



This translation assumes that the language has some way to define a local function by pattern matching (e.g. let).





If local functions can not be defined, there's always the possibility of lifting the definition to the top level. Indeed, any local definition



let f x1 ... xk = e in e'


having free variables v1 .. vn can be rewritten written as



(f -> e') (globalF v1 ... vn)


provided that we also define a global function



globalF v1 ... vn x1 ... xk = e


Note that globalF has no free variables, by construction. I believe it is sometimes called a "supercombinator".



This naturally extends to the case where x1 ... xk are patterns, and we have multiple defining equations for f / globalF.





For example,



case x of True  -> False
False -> True


cab be rewritten as



let f True  = False
f False = True
in f x





share|cite|improve this answer























  • Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
    – Wong Jia Hau
    Nov 20 at 10:46












  • @WongJiaHau See the last edit for an example.
    – chi
    Nov 20 at 10:50










  • Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
    – Wong Jia Hau
    Nov 20 at 13:09










  • @WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
    – chi
    Nov 20 at 13:26











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "419"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f100327%2fis-pattern-matching-as-expressive-as-case-expression-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













The expression



case e of
p1 -> e1
p2 -> e2
...


can be rewritten as



let k p1 = e1
k p2 = e2
...
in k e


where k is a fresh identifier.



This translation assumes that the language has some way to define a local function by pattern matching (e.g. let).





If local functions can not be defined, there's always the possibility of lifting the definition to the top level. Indeed, any local definition



let f x1 ... xk = e in e'


having free variables v1 .. vn can be rewritten written as



(f -> e') (globalF v1 ... vn)


provided that we also define a global function



globalF v1 ... vn x1 ... xk = e


Note that globalF has no free variables, by construction. I believe it is sometimes called a "supercombinator".



This naturally extends to the case where x1 ... xk are patterns, and we have multiple defining equations for f / globalF.





For example,



case x of True  -> False
False -> True


cab be rewritten as



let f True  = False
f False = True
in f x





share|cite|improve this answer























  • Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
    – Wong Jia Hau
    Nov 20 at 10:46












  • @WongJiaHau See the last edit for an example.
    – chi
    Nov 20 at 10:50










  • Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
    – Wong Jia Hau
    Nov 20 at 13:09










  • @WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
    – chi
    Nov 20 at 13:26















up vote
4
down vote













The expression



case e of
p1 -> e1
p2 -> e2
...


can be rewritten as



let k p1 = e1
k p2 = e2
...
in k e


where k is a fresh identifier.



This translation assumes that the language has some way to define a local function by pattern matching (e.g. let).





If local functions can not be defined, there's always the possibility of lifting the definition to the top level. Indeed, any local definition



let f x1 ... xk = e in e'


having free variables v1 .. vn can be rewritten written as



(f -> e') (globalF v1 ... vn)


provided that we also define a global function



globalF v1 ... vn x1 ... xk = e


Note that globalF has no free variables, by construction. I believe it is sometimes called a "supercombinator".



This naturally extends to the case where x1 ... xk are patterns, and we have multiple defining equations for f / globalF.





For example,



case x of True  -> False
False -> True


cab be rewritten as



let f True  = False
f False = True
in f x





share|cite|improve this answer























  • Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
    – Wong Jia Hau
    Nov 20 at 10:46












  • @WongJiaHau See the last edit for an example.
    – chi
    Nov 20 at 10:50










  • Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
    – Wong Jia Hau
    Nov 20 at 13:09










  • @WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
    – chi
    Nov 20 at 13:26













up vote
4
down vote










up vote
4
down vote









The expression



case e of
p1 -> e1
p2 -> e2
...


can be rewritten as



let k p1 = e1
k p2 = e2
...
in k e


where k is a fresh identifier.



This translation assumes that the language has some way to define a local function by pattern matching (e.g. let).





If local functions can not be defined, there's always the possibility of lifting the definition to the top level. Indeed, any local definition



let f x1 ... xk = e in e'


having free variables v1 .. vn can be rewritten written as



(f -> e') (globalF v1 ... vn)


provided that we also define a global function



globalF v1 ... vn x1 ... xk = e


Note that globalF has no free variables, by construction. I believe it is sometimes called a "supercombinator".



This naturally extends to the case where x1 ... xk are patterns, and we have multiple defining equations for f / globalF.





For example,



case x of True  -> False
False -> True


cab be rewritten as



let f True  = False
f False = True
in f x





share|cite|improve this answer














The expression



case e of
p1 -> e1
p2 -> e2
...


can be rewritten as



let k p1 = e1
k p2 = e2
...
in k e


where k is a fresh identifier.



This translation assumes that the language has some way to define a local function by pattern matching (e.g. let).





If local functions can not be defined, there's always the possibility of lifting the definition to the top level. Indeed, any local definition



let f x1 ... xk = e in e'


having free variables v1 .. vn can be rewritten written as



(f -> e') (globalF v1 ... vn)


provided that we also define a global function



globalF v1 ... vn x1 ... xk = e


Note that globalF has no free variables, by construction. I believe it is sometimes called a "supercombinator".



This naturally extends to the case where x1 ... xk are patterns, and we have multiple defining equations for f / globalF.





For example,



case x of True  -> False
False -> True


cab be rewritten as



let f True  = False
f False = True
in f x






share|cite|improve this answer














share|cite|improve this answer



share|cite|improve this answer








edited Nov 20 at 10:50

























answered Nov 20 at 10:02









chi

11.2k1629




11.2k1629












  • Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
    – Wong Jia Hau
    Nov 20 at 10:46












  • @WongJiaHau See the last edit for an example.
    – chi
    Nov 20 at 10:50










  • Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
    – Wong Jia Hau
    Nov 20 at 13:09










  • @WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
    – chi
    Nov 20 at 13:26


















  • Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
    – Wong Jia Hau
    Nov 20 at 10:46












  • @WongJiaHau See the last edit for an example.
    – chi
    Nov 20 at 10:50










  • Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
    – Wong Jia Hau
    Nov 20 at 13:09










  • @WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
    – chi
    Nov 20 at 13:26
















Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
– Wong Jia Hau
Nov 20 at 10:46






Apologies I don’t really understand how this answers my question ? Do you mind to be more concise ? For example provide your stance upon the statement I thrown out?
– Wong Jia Hau
Nov 20 at 10:46














@WongJiaHau See the last edit for an example.
– chi
Nov 20 at 10:50




@WongJiaHau See the last edit for an example.
– chi
Nov 20 at 10:50












Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
– Wong Jia Hau
Nov 20 at 13:09




Thanks, but does that mean any other Case Expression can be rewritten eith pattern matching ? For example, how would you write the definition of fmap using only Pattern Matching?
– Wong Jia Hau
Nov 20 at 13:09












@WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
– chi
Nov 20 at 13:26




@WongJiaHau Above I showed how to translate any case into a let expression. You should be able to apply it to any case expression.
– chi
Nov 20 at 13:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Computer Science Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcs.stackexchange.com%2fquestions%2f100327%2fis-pattern-matching-as-expressive-as-case-expression-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

AnyDesk - Fatal Program Failure

How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

QoS: MAC-Priority for clients behind a repeater