Git Product home page Git Product logo

Comments (4)

ollef avatar ollef commented on June 21, 2024

Hello and welcome!

It sounds like that should be possible already using the Grammar interface (note that the Grammar module already exposes its internals if you import Text.Earley.Grammar). What is the problem with that approach?

Exposing the internals is sometimes a good idea, but note that the internals exposed in Parser are only used while parsing and are very implementation-specific, why I'd like to get some more information about your use-case before I merge your changes. :)

Thanks!

from earley.

sboosali avatar sboosali commented on June 21, 2024

My use case is to transform a free alternative into an Earley Prod. So I'm wrapping the types in this package. The sharing is observed by a function that caches another function by reference (via StableName, like https://hackage.haskell.org/package/vault or like http://packdeps.haskellers.com/reverse/data-reify ). See https://github.com/sboosali/commands-core/blob/observed-sharing/sources/Data/RefCache.hs#L117 if interested.

it would replace Earley's grammar, looking maybe like this (probably doesn't typecheck, and still learning about Rank2 types and stuff, but):

observeSharing :: (forall x. f x -> g x) -> Alter f a -> IO (Alter g a)

shareEarleyProduction :: Symbol a -> ST z (E.Prod z e t a)
shareEarleyProduction = \case
 NonTerminal rhs -> do
  let p = getEarleyProduction rhs
  c <- newSTRef =<< newSTRef mempty
  nr <- newSTRef Nothing
  return$ NonTerminal (Rule p nr c) (Pure id)
 Terminal t -> _

induceEarleyParser :: ListLike i t => (Alter Symbol a) -> i -> ST z (E.Result z e i a) 
induceEarleyParser ps xs = do
 qs <- observeSharing shareEarleyProduction ps
 s <- initialState (runAlter id qs)
 parse [s] [] [] (return ()) [] 0 xs

where Alter is the free alternative, and Symbol holds some grammatical primitives like terminals or wildcards, as well as a right-hand side that should be shared; observeSharing calls cacheByRef.

e.g. in the grammar:

root :: Alter Symbol Root
root = ReplaceWith <$> t"replace" <*> phrase <*> t"with" <*> phrase
   <|> Phrase      <$> phrase
   <|> ...

phrase = _

with this (non-monadic) flavor of observed sharing, each phrase (rather, the Earley Prod induced by it) shares the same STRefs, after calling induceEarleyParser on root.

I tried a simpler API with a MonadFix that observes sharing (like Earley's Grammar), but I couldn't convert it to an Earley Grammar.

I hope that made sense, I'm still figuring this all out.

p.s. here's some exposing-internals-on-general-principles propaganda https://www.reddit.com/r/haskell/comments/2uoton/edward_kmett_encapsulation_vs_code_reuse/ :-)

from earley.

ollef avatar ollef commented on June 21, 2024

OK. I still think that should be possible just with Grammar but either way it won't hurt to expose the internals.

I'd be interested to see what you come up with later. :)

from earley.

sboosali avatar sboosali commented on June 21, 2024

thanks!

On Thursday, July 2, 2015, Olle Fredriksson [email protected]
wrote:

Closed #8 #8.

โ€”
Reply to this email directly or view it on GitHub
#8 (comment).

(this message was composed with dictation: charitably interpret typos)Sam
Boosalis

from earley.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.