haskell - Function "who am I" accessible from function and usable as key? -
i use collection of function names data.map keys, , have key each function automatically available within function. willing consider "unsafe" operations, if necessary.
my specific programming problem this: i've been developing simple parser library textual transformations, use in particular preprocessing , grooming of haskell code. has virtue of being small enough hack, constructs not requiring explicit monadic notation. instead, grammars read verbatim formal descriptions.
in particular, i've entered lexical structure of haskell2010 once, idea of modifying grammar in various ways different applications. creating labels corresponding each grammar construct, , passing these labels each construct. use these labels keys data.map. allows me provide post-processing various labels, in different applications of grammar.
the details of code don't matter; gist of question should clear, similar application describe here. here (draft) fragment of grammar haskell2010:
program = program § many $ lexeme ∨ whitespace lexeme = lexeme § reservedop ∨ reservedid ∨ qvarid ∨ qconid ∨ qvarsym ∨ qconsym ∨ literal ∨ special literal = literal § integer ∨ float ∨ hchar ∨ hstring special = special § oneof "(),;[]`{}" whitespace = whitespace § many1 whitestuff whitestuff = whitestuff § whitechar ∨ comment ∨ ncomment whitechar = whitechar § newline ∨ vertab ∨ space ∨ tab ∨ uniwhite newline = newline § hreturn ◊ linefeed ∨ hreturn ∨ linefeed ∨ formfeed
having worked hard make grammars concise, find duplicate boilerplate ugly. why can't use function names themselves, , have each function access own name needed? (the operators monadic, there's chance like, behind scenes.)
i understand , enjoy meta-game of arguments explain why construct "x" in lisp (scheme) or object-oriented language isn't necessary in haskell, because 1 "y". in comparison haskell, lisp both less disciplined , seemingly more flexible. lisp reminds me of "out of inkwell" cartoons @ birth of animation, hand come out behind screen , redraw bend rules needed.
from instead object-oriented point of view (which religions try avoid) rather routine use of "self". however, don't want create entire object-oriented framework here, want tiny bit of direct access symbol table (or equivalent, whatever gets job done), these other languages offer 1 way or another.
so i'm embarrassed can't play meta-game here. appears me haskell, tidily , great discipline, seals off access symbol tables , runtime environments let me golf code, rid of eyesore boilerplate. or not? ask here.
i think can't in standard-compliant haskell because breaks referential transparency, using ghc extension named template haskell, can done. template haskell allows run custom code in compile-time generate boilerplate code or more interesting (you may want transform existing code achieve goal).
Comments
Post a Comment