Parsec: How to simply create a new parser out of one in the libary











up vote
1
down vote

favorite












Suppose I just want to create my own parser which is exactly the same with the char in Parsec, but when I run



import Text.Parsec
char1 c = char c


It gives me



? Non type-variable argument in the constraint: Stream s m Char
(Use FlexibleContexts to permit this)
? When checking the inferred type
char1 :: forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Cha


How can I solve it? Is there any other imports that I should include? Thanks










share|improve this question






















  • The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
    – chi
    Nov 8 at 10:10















up vote
1
down vote

favorite












Suppose I just want to create my own parser which is exactly the same with the char in Parsec, but when I run



import Text.Parsec
char1 c = char c


It gives me



? Non type-variable argument in the constraint: Stream s m Char
(Use FlexibleContexts to permit this)
? When checking the inferred type
char1 :: forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Cha


How can I solve it? Is there any other imports that I should include? Thanks










share|improve this question






















  • The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
    – chi
    Nov 8 at 10:10













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Suppose I just want to create my own parser which is exactly the same with the char in Parsec, but when I run



import Text.Parsec
char1 c = char c


It gives me



? Non type-variable argument in the constraint: Stream s m Char
(Use FlexibleContexts to permit this)
? When checking the inferred type
char1 :: forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Cha


How can I solve it? Is there any other imports that I should include? Thanks










share|improve this question













Suppose I just want to create my own parser which is exactly the same with the char in Parsec, but when I run



import Text.Parsec
char1 c = char c


It gives me



? Non type-variable argument in the constraint: Stream s m Char
(Use FlexibleContexts to permit this)
? When checking the inferred type
char1 :: forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Cha


How can I solve it? Is there any other imports that I should include? Thanks







parsing haskell parsec






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 9:34









Yellowsgg

373




373












  • The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
    – chi
    Nov 8 at 10:10


















  • The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
    – chi
    Nov 8 at 10:10
















The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
– chi
Nov 8 at 10:10




The error states: "Use FlexibleContexts to permit this". Your code is OK, but you need to enable the extension. (I'd also add a type signature)
– chi
Nov 8 at 10:10












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Parsec parsers have rather complicated types due to their flexibility. You can get around this in two ways:




  1. Put {-# Language FlexibleContexts #-} at the top of your source file. This pragma tells GHC to do some extra type inference.



  2. (Recommended) Give char1 an explicit type.



    char1 :: Char -> Parsec String () Char   
    char1 c = char c



This is recommended because you should always give any top-level declaration an explicit type. If you don't then all the compiler can tell you is that there is a type mismatch somewhere in your code (it will tell you where it found the contradiction, but that's not likely to be where the error is). With explicit type declarations the compiler can narrow it down.



In this case, Parsec defines the Parsec type as



type Parsec s u = ParsecT s u Identity


You will recognise ParsecT from your error message. ParsecT is a monad transformer: it lets you have the parser do things in other monads (like IO) when it recognises the text. In this case we only want a parser, so we use the Identity monad, which does nothing.



The s parameter is the stream of inputs. In this case we will say that the parser is taking its input from a String.



The u parameter is a state. This allows you to do stuff like recording variable names in a symbol table as you encounter them. In this case we don't use a state so its just ().



If you were writing a real parser then you would generally define your own type synonym, something like



type FooParser a = Parsec Text FooState a

char1 :: Char -> FooParser Char
char1 = char





share|improve this answer





















  • Super! Thank you so much!
    – Yellowsgg
    Nov 8 at 10:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53204934%2fparsec-how-to-simply-create-a-new-parser-out-of-one-in-the-libary%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Parsec parsers have rather complicated types due to their flexibility. You can get around this in two ways:




  1. Put {-# Language FlexibleContexts #-} at the top of your source file. This pragma tells GHC to do some extra type inference.



  2. (Recommended) Give char1 an explicit type.



    char1 :: Char -> Parsec String () Char   
    char1 c = char c



This is recommended because you should always give any top-level declaration an explicit type. If you don't then all the compiler can tell you is that there is a type mismatch somewhere in your code (it will tell you where it found the contradiction, but that's not likely to be where the error is). With explicit type declarations the compiler can narrow it down.



In this case, Parsec defines the Parsec type as



type Parsec s u = ParsecT s u Identity


You will recognise ParsecT from your error message. ParsecT is a monad transformer: it lets you have the parser do things in other monads (like IO) when it recognises the text. In this case we only want a parser, so we use the Identity monad, which does nothing.



The s parameter is the stream of inputs. In this case we will say that the parser is taking its input from a String.



The u parameter is a state. This allows you to do stuff like recording variable names in a symbol table as you encounter them. In this case we don't use a state so its just ().



If you were writing a real parser then you would generally define your own type synonym, something like



type FooParser a = Parsec Text FooState a

char1 :: Char -> FooParser Char
char1 = char





share|improve this answer





















  • Super! Thank you so much!
    – Yellowsgg
    Nov 8 at 10:25















up vote
2
down vote













Parsec parsers have rather complicated types due to their flexibility. You can get around this in two ways:




  1. Put {-# Language FlexibleContexts #-} at the top of your source file. This pragma tells GHC to do some extra type inference.



  2. (Recommended) Give char1 an explicit type.



    char1 :: Char -> Parsec String () Char   
    char1 c = char c



This is recommended because you should always give any top-level declaration an explicit type. If you don't then all the compiler can tell you is that there is a type mismatch somewhere in your code (it will tell you where it found the contradiction, but that's not likely to be where the error is). With explicit type declarations the compiler can narrow it down.



In this case, Parsec defines the Parsec type as



type Parsec s u = ParsecT s u Identity


You will recognise ParsecT from your error message. ParsecT is a monad transformer: it lets you have the parser do things in other monads (like IO) when it recognises the text. In this case we only want a parser, so we use the Identity monad, which does nothing.



The s parameter is the stream of inputs. In this case we will say that the parser is taking its input from a String.



The u parameter is a state. This allows you to do stuff like recording variable names in a symbol table as you encounter them. In this case we don't use a state so its just ().



If you were writing a real parser then you would generally define your own type synonym, something like



type FooParser a = Parsec Text FooState a

char1 :: Char -> FooParser Char
char1 = char





share|improve this answer





















  • Super! Thank you so much!
    – Yellowsgg
    Nov 8 at 10:25













up vote
2
down vote










up vote
2
down vote









Parsec parsers have rather complicated types due to their flexibility. You can get around this in two ways:




  1. Put {-# Language FlexibleContexts #-} at the top of your source file. This pragma tells GHC to do some extra type inference.



  2. (Recommended) Give char1 an explicit type.



    char1 :: Char -> Parsec String () Char   
    char1 c = char c



This is recommended because you should always give any top-level declaration an explicit type. If you don't then all the compiler can tell you is that there is a type mismatch somewhere in your code (it will tell you where it found the contradiction, but that's not likely to be where the error is). With explicit type declarations the compiler can narrow it down.



In this case, Parsec defines the Parsec type as



type Parsec s u = ParsecT s u Identity


You will recognise ParsecT from your error message. ParsecT is a monad transformer: it lets you have the parser do things in other monads (like IO) when it recognises the text. In this case we only want a parser, so we use the Identity monad, which does nothing.



The s parameter is the stream of inputs. In this case we will say that the parser is taking its input from a String.



The u parameter is a state. This allows you to do stuff like recording variable names in a symbol table as you encounter them. In this case we don't use a state so its just ().



If you were writing a real parser then you would generally define your own type synonym, something like



type FooParser a = Parsec Text FooState a

char1 :: Char -> FooParser Char
char1 = char





share|improve this answer












Parsec parsers have rather complicated types due to their flexibility. You can get around this in two ways:




  1. Put {-# Language FlexibleContexts #-} at the top of your source file. This pragma tells GHC to do some extra type inference.



  2. (Recommended) Give char1 an explicit type.



    char1 :: Char -> Parsec String () Char   
    char1 c = char c



This is recommended because you should always give any top-level declaration an explicit type. If you don't then all the compiler can tell you is that there is a type mismatch somewhere in your code (it will tell you where it found the contradiction, but that's not likely to be where the error is). With explicit type declarations the compiler can narrow it down.



In this case, Parsec defines the Parsec type as



type Parsec s u = ParsecT s u Identity


You will recognise ParsecT from your error message. ParsecT is a monad transformer: it lets you have the parser do things in other monads (like IO) when it recognises the text. In this case we only want a parser, so we use the Identity monad, which does nothing.



The s parameter is the stream of inputs. In this case we will say that the parser is taking its input from a String.



The u parameter is a state. This allows you to do stuff like recording variable names in a symbol table as you encounter them. In this case we don't use a state so its just ().



If you were writing a real parser then you would generally define your own type synonym, something like



type FooParser a = Parsec Text FooState a

char1 :: Char -> FooParser Char
char1 = char






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 10:15









Paul Johnson

13.4k23349




13.4k23349












  • Super! Thank you so much!
    – Yellowsgg
    Nov 8 at 10:25


















  • Super! Thank you so much!
    – Yellowsgg
    Nov 8 at 10:25
















Super! Thank you so much!
– Yellowsgg
Nov 8 at 10:25




Super! Thank you so much!
– Yellowsgg
Nov 8 at 10:25


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204934%2fparsec-how-to-simply-create-a-new-parser-out-of-one-in-the-libary%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)