<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div style="line-height:1.6;font-family:'苹方','微软雅黑','sans-serif';">Thanks, not exactly variadic I think, and later I find that <a href="http://hackage.haskell.org/package/named"><a href="http://hackage.haskell.org/package/named">http://hackage.haskell.org/package/named</a></a> seems closer to what I meant to do, only if I find a way to leverage its machinery in a programatic way.<br/> <br/>Named parameters feels like a good practice, especially for api functions intend to be called from heterogeneous environment, I wonder why `named` is not advocated by more Haskellers so I could get to know it earlier, do the language extensions it mandates include some bad ones?<br/><br/>At the first glance I feel my mind semanticically overloaded by its intensive use of `!` and `#`, conflicting with my intuition about bangpatterns and unboxed stuff, anyway I'm trying to grok it for sense making clicks. <br/><br/>Cheers,<br/>Compl<br/><br/><br/><br/><br/><br/><br /><br /></div><div class='ne-quoted'><div class='ne-quoted'><div class='ne-quoted'><div class='ne-quoted'>        <a href="https://maas.mail.163.com/dashi-web-extend/html/proSignature.html?ftlId=1&name=compl.yue&uid=compl.yue%40icloud.com&iconUrl=https%3A%2F%2Fmail-online.nosdn.127.net%2Fqiyelogo%2FdefaultAvatar.png&items=%5B%22%E9%82%AE%E7%AE%B1compl.yue%40icloud.com%22%5D" style="display:block;background:#fff; max-width: 400px; _width: 400px;padding:15px 0 10px 0;text-decoration: none; outline:none;-webkit-tap-highlight-color:transparent;-webkit-text-size-adjust:none !important;text-size-adjust:none !important;">
            <table cellpadding="0" style='width: 100%; max-width: 100%; table-layout: fixed; border-collapse: collapse;color: #9b9ea1;font-size: 14px;line-height:1.3;-webkit-text-size-adjust:none !important;text-size-adjust:none !important;'>
                <tbody style="font-family: 'PingFang SC', 'Hiragino Sans GB','WenQuanYi Micro Hei', 'Microsoft Yahei', '微软雅黑', verdana !important; word-wrap:break-word; word-break:break-all;-webkit-text-size-adjust:none !important;text-size-adjust:none !important;">
                    <tr>
                            <td width="38" style="padding:0; box-sizing: border-box; width: 38px;">
                                <img width="38" height="38" style="vertical-align:middle; width: 38px; height: 38px; border-radius:50%;" src="https://mail-online.nosdn.127.net/qiyelogo/defaultAvatar.png" />
                            </td>
                            <td style='padding: 0 0 0 10px; color: #31353b;'>
                                <div style="font-size: 16px;font-weight:bold; width:100%; white-space: nowrap; overflow:hidden;text-overflow: ellipsis;">compl.yue</div>
                            </td>
                    </tr>
                        <tr width="100%" style="font-size: 14px !important; width: 100%;">
                            <td colspan='2' style="padding:10px 0 0 0; font-size:14px !important; width: 100%;">
                                    <div style="width: 100%;font-size: 14px !important;word-wrap:break-word;word-break:break-all;">邮箱compl.yue@icloud.com</div>
                            </td>
                        </tr>
                </tbody>
            </table>
        </a><html><div><p style="border-top:1px solid #e5e5e5;padding-top: 8px; font-size: 12px; color:#b6b8bb;line-height: 1.833;">签名由 <a href="https://mail.163.com/dashi/dlpro.html?from=mail88" style="color:#6aa8f6;text-decoration: none">网易邮箱大师</a> 定制</p></div></html><blockquote id='ntes-andriodmail-quote' style="margin:0px !important;padding:0px !important;border: none !important;"><blockquote id='ntes-andriodmail-quote' style="margin:0px !important;padding:0px !important;border: none !important;"><div class="J-reply" style="background-color:#f2f2f2;color:black;padding-top:6px;padding-bottom:6px;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;margin-top:45px;margin-bottom:20px;">
    <div style="font-size:14px;line-height:1.5;word-break:break-all;margin-left:10px;margin-right:10px">On <span class="mail-date">09/11/2020 01:09</span>, <a class="mail-to" style="text-decoration:none;color:#2a97ff;" href="mailto:lysxia@gmail.com">Li-yao Xia</a> wrote:</div>
</div>Hi Compl, <br/> <br/> From my limited understanding of your problem, you might be looking for  <br/>techniques revolving around "variadic functions", that is "functions  <br/>with variable number of arguments". I don't have any concrete resources  <br/>to point to, but it's a pretty recurrent topic of discussion. Below are  <br/>two relevant Q&A on Stack Overflow to start from [1,2]; you might find  <br/>especially interesting ideas from reading the implementation of  <br/>Text.Printf [3] and looking for other explanations of it online. <br/> <br/>Cheers, <br/>Li-yao <br/> <br/>[1]:  <br/><a href="https://stackoverflow.com/questions/7828072/how-does-haskell-printf-work">https://stackoverflow.com/questions/7828072/how-does-haskell-printf-work</a> <br/>[2]:  <br/><a href="https://stackoverflow.com/questions/8353845/how-to-write-a-haskell-function-that-takes-a-variadic-function-as-an-argument">https://stackoverflow.com/questions/8353845/how-to-write-a-haskell-function-that-takes-a-variadic-function-as-an-argument</a> <br/>[3]:  <br/><a href="http://hackage.haskell.org/package/base-4.14.0.0/docs/src/Text.Printf.html">http://hackage.haskell.org/package/base-4.14.0.0/docs/src/Text.Printf.html</a> <br/> <br/>On 9/10/2020 12:50 PM, YueCompl wrote: <br/>> Then any better approach, to auto (or at least semi-auto) adapt an ArgsPack toward applying an arbitrary Haskell function? <br/>>  <br/>>> On 2020-09-11, at 00:35, Li-yao Xia <lysxia@gmail.com> wrote: <br/>>> <br/>>> This doesn't sound like a use case for generics then. Just to spare you the trouble of following a red herring. <br/>>> <br/>>> On 9/10/2020 12:26 PM, YueCompl wrote: <br/>>>> Li-yao, thanks for the pointer. And my case is not really about ADTs, but to introspect the arguments an arbitrary Haskell function takes, including how many and what type each argument is, so as to extract proper values from a given ArgsPack, then call that Haskell function with those values as args it expects. <br/>>>> I'm not sure at a glance, that generics-eot has demonstrated how to obtain argument list with type info for a function, and will look into the details as I can. <br/>>>> Thanks with regards, <br/>>>> Compl <br/>>>>> On 2020-09-10, at 23:08, Li-yao Xia <lysxia@gmail.com <mailto:lysxia@gmail.com>> wrote: <br/>>>>> <br/>>>>> Hi Compl, <br/>>>>> <br/>>>>> I couldn't tell what's generic (in the sense of GHC.Generics) about this example. A clearer example would be to give two applications with different algebraic data types, and to show how they consist of the same boilerplate, where the differences are only due to the differing numbers of fields and constructors. <br/>>>>> <br/>>>>> As for tutorials on generics, a good starting point might be generics-eot. Its documentation comes with a series of tutorials: <br/>>>>> <br/>>>>> <a href="https://generics-eot.readthedocs.io/en/stable/">https://generics-eot.readthedocs.io/en/stable/</a> <br/>>>>> <br/>>>>> Li-yao <br/>>>>> <br/>>>>> On 9/10/2020 9:44 AM, YueCompl via Haskell-Cafe wrote: <br/>>>>>> Dear Cafe, <br/>>>>>> I'm tinkering with the idea for arbitrary Haskell functions to be easily called from scripting code, I see auto derive with GHC.Generics might be the most promising tool, but I'm lost after read <a href="https://wiki.haskell.org/GHC.Generics">https://wiki.haskell.org/GHC.Generics</a> and hackage docs. I have no clue so far with how to start with it. <br/>>>>>> Specifically I want the section highlighted in blue get auto generated, within the following `runghc` ready example: <br/>>>>>> ``` <br/>>>>>> {-# LANGUAGEBangPatterns#-} <br/>>>>>> moduleMain where <br/>>>>>> importPrelude <br/>>>>>> importGHC.Generics <br/>>>>>> importData.Dynamic <br/>>>>>> -- * minimum data structures as interface with scripting code <br/>>>>>> typeAttrKey=String <br/>>>>>> dataAttrVal=NilValue <br/>>>>>> |IntValue!Integer <br/>>>>>> |StrValue!String <br/>>>>>> deriving(Eq,Ord,Typeable) <br/>>>>>> instanceShowAttrValwhere <br/>>>>>> show NilValue="nil" <br/>>>>>> show (IntValue!x)=show x <br/>>>>>> show (StrValue!x)=show x <br/>>>>>> dataArgsPack=ArgsPack{ <br/>>>>>> positional'args::[AttrVal] <br/>>>>>> ,keyword'args::[(AttrKey,AttrVal)] <br/>>>>>> } <br/>>>>>> instanceSemigroupArgsPackwhere <br/>>>>>> (ArgsPackp1 kw1)<>(ArgsPackp2 kw2)=ArgsPack(p1 ++p2)(kw1 ++kw2) <br/>>>>>> instanceMonoidArgsPackwhere <br/>>>>>> mempty =ArgsPack[][] <br/>>>>>> classCallableawhere <br/>>>>>> call::a->ArgsPack->(AttrVal->IO())->IO() <br/>>>>>> -- * functions to be callable from scripting code <br/>>>>>> newtypeAssert=Assert( <br/>>>>>> Expect->MaybeTarget->Message->IOMessage <br/>>>>>> ) <br/>>>>>> typeExpect=AttrVal <br/>>>>>> typeTarget=AttrVal <br/>>>>>> typeMessage=String <br/>>>>>> instanceCallableAssertwhere <br/>>>>>> -- can this get auto-generated ? with <a href="https://wiki.haskell.org/GHC.Generics">https://wiki.haskell.org/GHC.Generics</a> <br/>>>>>> call (Assert!assert)(ArgsPack!args !kwargs)!exit =do <br/>>>>>> (expect,target,message)<-parseApk <br/>>>>>> result <-assert expect target message <br/>>>>>> exit $StrValueresult <br/>>>>>> where <br/>>>>>> parseApk::IO(Expect,MaybeTarget,Message) <br/>>>>>> parseApk =goParse <br/>>>>>> (Left"missing arg: expect",Nothing,Left"missing arg: message") <br/>>>>>> args <br/>>>>>> kwargs <br/>>>>>> goParse (got'expect,got'target,got'message)[][]=casegot'expect of <br/>>>>>> Leftmsg ->error msg <br/>>>>>> Rightexpect ->casegot'message of <br/>>>>>> Leftmsg ->error msg <br/>>>>>> Rightmessage ->return (expect,got'target,message) <br/>>>>>> goParse (got'expect,got'target,got'message)args' ((name,val):kwargs') <br/>>>>>> =casename of <br/>>>>>> "expect"->casegot'expect of <br/>>>>>> Right{}->error "duplicate arg: expect" <br/>>>>>> Left{}->goParse (Rightval,got'target,got'message)args' kwargs' <br/>>>>>> "target"->casegot'target of <br/>>>>>> Just{}->error "duplicate arg: target" <br/>>>>>> Nothing->goParse (got'expect,Justval,got'message)args' kwargs' <br/>>>>>> "message"->casegot'message of <br/>>>>>> Right{}->error "duplicate arg: message" <br/>>>>>> Left{}->caseval of <br/>>>>>> StrValuemessage -> <br/>>>>>> goParse (got'expect,got'target,Rightmessage)args' kwargs' <br/>>>>>> _ ->error "bad arg type for: message" <br/>>>>>> _ ->error "unexpected keyword args" <br/>>>>>> goParse (got'expect,got'target,got'message)(val :args')[]= <br/>>>>>> casegot'expect of <br/>>>>>> Left{}->goParse (Rightval,got'target,got'message)args' [] <br/>>>>>> Right{}->casegot'target of <br/>>>>>> Nothing->goParse (got'expect,Justval,got'message)args' [] <br/>>>>>> Just{}->casegot'message of <br/>>>>>> Left{}->caseval of <br/>>>>>> StrValuemessage -> <br/>>>>>> goParse (got'expect,got'target,Rightmessage)args' [] <br/>>>>>> _ ->error "bad arg type for: message" <br/>>>>>> Right{}->error "extranous positional args" <br/>>>>>> -- mockup & test out <br/>>>>>> main::IO() <br/>>>>>> main = <br/>>>>>> call <br/>>>>>> (Assertassert) <br/>>>>>> (ArgsPack[IntValue333,StrValue"as good will"] <br/>>>>>> [("target",IntValue333)] <br/>>>>>> ) <br/>>>>>> $\result ->putStrLn $"Got result: "<>show result <br/>>>>>> -- | plain Haskell function meant to be easily called by scripting code <br/>>>>>> assert::Expect->MaybeTarget->Message->IOMessage <br/>>>>>> assert !expect !maybeTarget !message =casemaybeTarget of <br/>>>>>> Nothing->return $"* assertion not applicable: "<>message <br/>>>>>> Justtarget ->ifexpect ==target <br/>>>>>> thenreturn $"* assertion passed: "<>message <br/>>>>>> elseerror $"* assertion failed: "<>message <br/>>>>>> ``` <br/>>>>>> I tried to understand how <br/>>>>>>   * The compiler can provide a default generic implementation for <br/>>>>>>     |parseJSON <br/>>>>>>     <<a href="https://hackage.haskell.org/package/aeson-1.5.4.0/docs/Data-Aeson.html#v:parseJSON>|">https://hackage.haskell.org/package/aeson-1.5.4.0/docs/Data-Aeson.html#v:parseJSON>|</a>. <br/>>>>>> is implemented in [aeson](<a href="https://hackage.haskell.org/package/aeson">https://hackage.haskell.org/package/aeson</a>) and it is overwhelming to me at the moment ... <br/>>>>>> Is there easier scaffold template for me to start with GHC.Generics? Or there're even better techniques to achieve my final goal? <br/>>>>>> Help please! <br/>>>>>> Best regards, <br/>>>>>> Compl <br/>>>>>> _______________________________________________ <br/>>>>>> Haskell-Cafe mailing list <br/>>>>>> To (un)subscribe, modify options or view archives go to: <br/>>>>>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a> <br/>>>>>> Only members subscribed via the mailman list are allowed to post. <br/>>  <br/></blockquote></blockquote></div></div></div></div>
</body>
</html>