Skip to content

Instantly share code, notes, and snippets.

@dvberkel
Created December 24, 2017 08:26
Show Gist options
  • Save dvberkel/497badaf71eca56ff570566a0a58d75e to your computer and use it in GitHub Desktop.
Save dvberkel/497badaf71eca56ff570566a0a58d75e to your computer and use it in GitHub Desktop.
Minimal project demonstrating a problem with elm-community/parser-combinators
module CombineProblem exposing (..)
import Html
import Combine exposing (..)
import Combine.Num exposing (int)
main =
parse command "[5M]"
|> toString
|> Html.text
type Command
= Simple Action
| Repeat Int Command
type Action
= Move
command: Parser s Command
command =
choice [primitive, repetition]
primitive: Parser s Command
primitive =
string "M" $> Simple Move
repetition: Parser s Command
repetition =
let
repeatCommand : Parser s Command
repeatCommand =
int |> andThen what
what : Int -> Parser s Command
what n =
command |> map (Repeat n)
in
brackets repeatCommand
@dvberkel
Copy link
Author

This problem is similar to the problems expressed in #7, #14 and #23.

It is resolved by using the lazy combinator.

command: Parser s Command
command =
    lazy (\() -> choice [primitive, repetition])

primitive: Parser s Command
primitive =
    string "M" $> Simple Move

repetition: Parser s Command
repetition =
    let
        repeatCommand : Parser s Command
        repeatCommand =
            int |> andThen what

        what : Int -> Parser s Command
        what n =
            command |> map (Repeat n)
    in
        lazy (\() -> brackets repeatCommand)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment