How to create data types dynamically depending on the input












0
















Update for my original problems



Well, maybe I didn't describe my problem completely before. Sorry for all! The following is the real problem:



I have a txt file containing patent data, such as:




1/1523 DWPI



AP - JP29446999A 19991015



PN - JP2000188399 A 20000704 DW200044
JP4568930B2 B2 20101027 DW201071



AN - 2000495116



PA - (NPDE ) DENSO CORP



PR - JP1998000295406 19981016



MC - U11-C18A3,U12-D02A



OPD - 1998.10.16



ICAI - H01L29/12,H01L29/78,H01L21/265,H01L21/336



TI - Planar type metal oxide semiconductor field effect transistor



AB - <-contents eliminated for simplicity->



CPY - NPDE



FN - JP2000188399




There are 1523 items with the similar format. I want to analyze the patent data, so I have to parse the data. I have defined the data type for every field, such as:



data AP = AP String Day String



data PN = PN String Day String

data AN = AN String

data PD = PD day

.... -- many other data types are not shown just for simplicity.


Now I have written the parser for every field with megaparsec, such as apField, pnField, anField, etc.



However, not every record has the same field, for example, the 2nd item may only contain fields of AP, PN, PA, PR, OPD, TI, AB, CPY and FN, with AN, MC, and ICAI missing. Besides, someone may be interested in different fields, and
he just exports the txt file containing records only with fields of AP, PN, PA, OPD and CPY.



Now I want to write a generic code, which can parse the records with fields people are interested in, and write the parsing result into a SQLite database.



For example, if I want to parse records with fields of AP, PN, PA, OPD and CPY, I can construct a record parser according to the input, such as toParser "ap,pn,pa,opd,cpy", or toParser "ap,pa,cpy", which I have figured out. The parsed result should be Record AP PN PA OPD CPY or Record AP PA CPY respectively. Then I'd like to write the parsed results into a database. Since every record in the data corresponds to a Record data type, and the record to be parsed may be different, I have to construct a Record data type with different fields depending on the user's input. This is the problem that I have met.



I can work around it by defining all the field data types as data Field = Field [String] and the record as data Record = Record [Field]. However, I want more control over data type, such as a day as a Day type, and id number as a Int type.



If constructing Record data type with different fields depending on the input is impossible, maybe there are other ways to solve my problem. I appreciate any advices! And sorry for the long description of my problem and my ambiguous descriptions for my problem before!










share|improve this question




















  • 1




    Why would you want such a record? How would you use it?
    – Fyodor Soikin
    Nov 19 at 15:12






  • 8




    Types don't exist at runtime.
    – leftaroundabout
    Nov 19 at 15:13






  • 4




    This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
    – bergey
    Nov 19 at 15:17






  • 7




    This looks like an XY problem.
    – molbdnilo
    Nov 19 at 16:03










  • Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
    – molbdnilo
    Nov 19 at 16:08
















0
















Update for my original problems



Well, maybe I didn't describe my problem completely before. Sorry for all! The following is the real problem:



I have a txt file containing patent data, such as:




1/1523 DWPI



AP - JP29446999A 19991015



PN - JP2000188399 A 20000704 DW200044
JP4568930B2 B2 20101027 DW201071



AN - 2000495116



PA - (NPDE ) DENSO CORP



PR - JP1998000295406 19981016



MC - U11-C18A3,U12-D02A



OPD - 1998.10.16



ICAI - H01L29/12,H01L29/78,H01L21/265,H01L21/336



TI - Planar type metal oxide semiconductor field effect transistor



AB - <-contents eliminated for simplicity->



CPY - NPDE



FN - JP2000188399




There are 1523 items with the similar format. I want to analyze the patent data, so I have to parse the data. I have defined the data type for every field, such as:



data AP = AP String Day String



data PN = PN String Day String

data AN = AN String

data PD = PD day

.... -- many other data types are not shown just for simplicity.


Now I have written the parser for every field with megaparsec, such as apField, pnField, anField, etc.



However, not every record has the same field, for example, the 2nd item may only contain fields of AP, PN, PA, PR, OPD, TI, AB, CPY and FN, with AN, MC, and ICAI missing. Besides, someone may be interested in different fields, and
he just exports the txt file containing records only with fields of AP, PN, PA, OPD and CPY.



Now I want to write a generic code, which can parse the records with fields people are interested in, and write the parsing result into a SQLite database.



For example, if I want to parse records with fields of AP, PN, PA, OPD and CPY, I can construct a record parser according to the input, such as toParser "ap,pn,pa,opd,cpy", or toParser "ap,pa,cpy", which I have figured out. The parsed result should be Record AP PN PA OPD CPY or Record AP PA CPY respectively. Then I'd like to write the parsed results into a database. Since every record in the data corresponds to a Record data type, and the record to be parsed may be different, I have to construct a Record data type with different fields depending on the user's input. This is the problem that I have met.



I can work around it by defining all the field data types as data Field = Field [String] and the record as data Record = Record [Field]. However, I want more control over data type, such as a day as a Day type, and id number as a Int type.



If constructing Record data type with different fields depending on the input is impossible, maybe there are other ways to solve my problem. I appreciate any advices! And sorry for the long description of my problem and my ambiguous descriptions for my problem before!










share|improve this question




















  • 1




    Why would you want such a record? How would you use it?
    – Fyodor Soikin
    Nov 19 at 15:12






  • 8




    Types don't exist at runtime.
    – leftaroundabout
    Nov 19 at 15:13






  • 4




    This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
    – bergey
    Nov 19 at 15:17






  • 7




    This looks like an XY problem.
    – molbdnilo
    Nov 19 at 16:03










  • Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
    – molbdnilo
    Nov 19 at 16:08














0












0








0


1







Update for my original problems



Well, maybe I didn't describe my problem completely before. Sorry for all! The following is the real problem:



I have a txt file containing patent data, such as:




1/1523 DWPI



AP - JP29446999A 19991015



PN - JP2000188399 A 20000704 DW200044
JP4568930B2 B2 20101027 DW201071



AN - 2000495116



PA - (NPDE ) DENSO CORP



PR - JP1998000295406 19981016



MC - U11-C18A3,U12-D02A



OPD - 1998.10.16



ICAI - H01L29/12,H01L29/78,H01L21/265,H01L21/336



TI - Planar type metal oxide semiconductor field effect transistor



AB - <-contents eliminated for simplicity->



CPY - NPDE



FN - JP2000188399




There are 1523 items with the similar format. I want to analyze the patent data, so I have to parse the data. I have defined the data type for every field, such as:



data AP = AP String Day String



data PN = PN String Day String

data AN = AN String

data PD = PD day

.... -- many other data types are not shown just for simplicity.


Now I have written the parser for every field with megaparsec, such as apField, pnField, anField, etc.



However, not every record has the same field, for example, the 2nd item may only contain fields of AP, PN, PA, PR, OPD, TI, AB, CPY and FN, with AN, MC, and ICAI missing. Besides, someone may be interested in different fields, and
he just exports the txt file containing records only with fields of AP, PN, PA, OPD and CPY.



Now I want to write a generic code, which can parse the records with fields people are interested in, and write the parsing result into a SQLite database.



For example, if I want to parse records with fields of AP, PN, PA, OPD and CPY, I can construct a record parser according to the input, such as toParser "ap,pn,pa,opd,cpy", or toParser "ap,pa,cpy", which I have figured out. The parsed result should be Record AP PN PA OPD CPY or Record AP PA CPY respectively. Then I'd like to write the parsed results into a database. Since every record in the data corresponds to a Record data type, and the record to be parsed may be different, I have to construct a Record data type with different fields depending on the user's input. This is the problem that I have met.



I can work around it by defining all the field data types as data Field = Field [String] and the record as data Record = Record [Field]. However, I want more control over data type, such as a day as a Day type, and id number as a Int type.



If constructing Record data type with different fields depending on the input is impossible, maybe there are other ways to solve my problem. I appreciate any advices! And sorry for the long description of my problem and my ambiguous descriptions for my problem before!










share|improve this question

















Update for my original problems



Well, maybe I didn't describe my problem completely before. Sorry for all! The following is the real problem:



I have a txt file containing patent data, such as:




1/1523 DWPI



AP - JP29446999A 19991015



PN - JP2000188399 A 20000704 DW200044
JP4568930B2 B2 20101027 DW201071



AN - 2000495116



PA - (NPDE ) DENSO CORP



PR - JP1998000295406 19981016



MC - U11-C18A3,U12-D02A



OPD - 1998.10.16



ICAI - H01L29/12,H01L29/78,H01L21/265,H01L21/336



TI - Planar type metal oxide semiconductor field effect transistor



AB - <-contents eliminated for simplicity->



CPY - NPDE



FN - JP2000188399




There are 1523 items with the similar format. I want to analyze the patent data, so I have to parse the data. I have defined the data type for every field, such as:



data AP = AP String Day String



data PN = PN String Day String

data AN = AN String

data PD = PD day

.... -- many other data types are not shown just for simplicity.


Now I have written the parser for every field with megaparsec, such as apField, pnField, anField, etc.



However, not every record has the same field, for example, the 2nd item may only contain fields of AP, PN, PA, PR, OPD, TI, AB, CPY and FN, with AN, MC, and ICAI missing. Besides, someone may be interested in different fields, and
he just exports the txt file containing records only with fields of AP, PN, PA, OPD and CPY.



Now I want to write a generic code, which can parse the records with fields people are interested in, and write the parsing result into a SQLite database.



For example, if I want to parse records with fields of AP, PN, PA, OPD and CPY, I can construct a record parser according to the input, such as toParser "ap,pn,pa,opd,cpy", or toParser "ap,pa,cpy", which I have figured out. The parsed result should be Record AP PN PA OPD CPY or Record AP PA CPY respectively. Then I'd like to write the parsed results into a database. Since every record in the data corresponds to a Record data type, and the record to be parsed may be different, I have to construct a Record data type with different fields depending on the user's input. This is the problem that I have met.



I can work around it by defining all the field data types as data Field = Field [String] and the record as data Record = Record [Field]. However, I want more control over data type, such as a day as a Day type, and id number as a Int type.



If constructing Record data type with different fields depending on the input is impossible, maybe there are other ways to solve my problem. I appreciate any advices! And sorry for the long description of my problem and my ambiguous descriptions for my problem before!







haskell dynamic custom-data-type






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 14:38

























asked Nov 19 at 14:53









Z-Y.L

301110




301110








  • 1




    Why would you want such a record? How would you use it?
    – Fyodor Soikin
    Nov 19 at 15:12






  • 8




    Types don't exist at runtime.
    – leftaroundabout
    Nov 19 at 15:13






  • 4




    This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
    – bergey
    Nov 19 at 15:17






  • 7




    This looks like an XY problem.
    – molbdnilo
    Nov 19 at 16:03










  • Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
    – molbdnilo
    Nov 19 at 16:08














  • 1




    Why would you want such a record? How would you use it?
    – Fyodor Soikin
    Nov 19 at 15:12






  • 8




    Types don't exist at runtime.
    – leftaroundabout
    Nov 19 at 15:13






  • 4




    This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
    – bergey
    Nov 19 at 15:17






  • 7




    This looks like an XY problem.
    – molbdnilo
    Nov 19 at 16:03










  • Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
    – molbdnilo
    Nov 19 at 16:08








1




1




Why would you want such a record? How would you use it?
– Fyodor Soikin
Nov 19 at 15:12




Why would you want such a record? How would you use it?
– Fyodor Soikin
Nov 19 at 15:12




8




8




Types don't exist at runtime.
– leftaroundabout
Nov 19 at 15:13




Types don't exist at runtime.
– leftaroundabout
Nov 19 at 15:13




4




4




This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
– bergey
Nov 19 at 15:17




This is not possible, because types definitions are used at compile time, and removed as part of compilation. There is probably a way to approach your actual problem, but it's not clear from your question what that problem is.
– bergey
Nov 19 at 15:17




7




7




This looks like an XY problem.
– molbdnilo
Nov 19 at 16:03




This looks like an XY problem.
– molbdnilo
Nov 19 at 16:03












Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
– molbdnilo
Nov 19 at 16:08




Even if you could create a type at runtime, you couldn't write a function that uses that type. It looks more like you want a sum type T and Record [T].
– molbdnilo
Nov 19 at 16:08












1 Answer
1






active

oldest

votes


















1














Well, if I got your question right, no you can't write a single function which returns different data types depending on the input. However what you can do is write a function that returns a single data type that can be constructed in different ways depending on input.. i.e. like:



data PatentRecord = PN String Day String
| AN String
| PD day


so now you can write a function parseRecord :: String -> Maybe PatentRecord for example which parses your input and depending on what it matches returns a PatentRecord built using the PN constructor, or the AN constructor, etc...



PS: Implementation Tip: use rather an Either SomeErrorType instead of Maybe to provide richer information upon parsing errors ;-)






share|improve this answer





















  • Thanks! It's a way to solve my problem.
    – Z-Y.L
    Nov 22 at 13:31











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',
autoActivateHeartbeat: false,
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%2f53377182%2fhow-to-create-data-types-dynamically-depending-on-the-input%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









1














Well, if I got your question right, no you can't write a single function which returns different data types depending on the input. However what you can do is write a function that returns a single data type that can be constructed in different ways depending on input.. i.e. like:



data PatentRecord = PN String Day String
| AN String
| PD day


so now you can write a function parseRecord :: String -> Maybe PatentRecord for example which parses your input and depending on what it matches returns a PatentRecord built using the PN constructor, or the AN constructor, etc...



PS: Implementation Tip: use rather an Either SomeErrorType instead of Maybe to provide richer information upon parsing errors ;-)






share|improve this answer





















  • Thanks! It's a way to solve my problem.
    – Z-Y.L
    Nov 22 at 13:31
















1














Well, if I got your question right, no you can't write a single function which returns different data types depending on the input. However what you can do is write a function that returns a single data type that can be constructed in different ways depending on input.. i.e. like:



data PatentRecord = PN String Day String
| AN String
| PD day


so now you can write a function parseRecord :: String -> Maybe PatentRecord for example which parses your input and depending on what it matches returns a PatentRecord built using the PN constructor, or the AN constructor, etc...



PS: Implementation Tip: use rather an Either SomeErrorType instead of Maybe to provide richer information upon parsing errors ;-)






share|improve this answer





















  • Thanks! It's a way to solve my problem.
    – Z-Y.L
    Nov 22 at 13:31














1












1








1






Well, if I got your question right, no you can't write a single function which returns different data types depending on the input. However what you can do is write a function that returns a single data type that can be constructed in different ways depending on input.. i.e. like:



data PatentRecord = PN String Day String
| AN String
| PD day


so now you can write a function parseRecord :: String -> Maybe PatentRecord for example which parses your input and depending on what it matches returns a PatentRecord built using the PN constructor, or the AN constructor, etc...



PS: Implementation Tip: use rather an Either SomeErrorType instead of Maybe to provide richer information upon parsing errors ;-)






share|improve this answer












Well, if I got your question right, no you can't write a single function which returns different data types depending on the input. However what you can do is write a function that returns a single data type that can be constructed in different ways depending on input.. i.e. like:



data PatentRecord = PN String Day String
| AN String
| PD day


so now you can write a function parseRecord :: String -> Maybe PatentRecord for example which parses your input and depending on what it matches returns a PatentRecord built using the PN constructor, or the AN constructor, etc...



PS: Implementation Tip: use rather an Either SomeErrorType instead of Maybe to provide richer information upon parsing errors ;-)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 13:00









Erick Gonzalez

1444




1444












  • Thanks! It's a way to solve my problem.
    – Z-Y.L
    Nov 22 at 13:31


















  • Thanks! It's a way to solve my problem.
    – Z-Y.L
    Nov 22 at 13:31
















Thanks! It's a way to solve my problem.
– Z-Y.L
Nov 22 at 13:31




Thanks! It's a way to solve my problem.
– Z-Y.L
Nov 22 at 13:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





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%2fstackoverflow.com%2fquestions%2f53377182%2fhow-to-create-data-types-dynamically-depending-on-the-input%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

Costa Masnaga

Fotorealismo

Sidney Franklin