# Creating a parser for logical propositions

**URL:** https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291
**Category:** Learn
**Created:** [April 1, 2022, 10:56pm UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291 "2022-04-01T22:56:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![half\_empty\_half\_full](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/half_empty_half_full/32/4522_2.png) [@half\_empty\_half\_full](https://discourse.elm-lang.org/u/half_empty_half_full)
#### Post date: [April 1, 2022, 10:56pm UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/1 "2022-04-01T22:56:03Z")

</div>

Hello!  
I am quite new to Elm and 1 week ago I was tasked to create a parser in Elm that takes in a string  
“& (A, B)” and parses it into: And (Var “A”) (Var “B”). This is quite trivial to me, as we have only been doing much simpler operations and parsing in Elm. The assignment is a huge step ahead of my current knowledge and after reading and researching for like 7 hours straight today, I realized that this would be nearly impossible given my current ability. I am sitting at almost no progress, so really, any advice/suggestions would be greatly appreciated. I have already looked at the Parser library, but I didn’t see anything for custom types/custom variables. Thank you in advance!

---

<div class="post-metadata">

### Author: ![matt.cheely](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/matt.cheely/32/4524_2.png) [@matt.cheely](https://discourse.elm-lang.org/u/matt.cheely)
#### Post date: [April 2, 2022, 12:54am UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/2 "2022-04-02T00:54:16Z")

</div>

Interestingly, I just published a [package](https://package.elm-lang.org/packages/MattCheely/boon/latest/) for parsing a different style of boolean logic expression yesterday. I did use [elm/parser](https://package.elm-lang.org/packages/elm/parser/latest/) for the parsing, but I have found that it can take some time to ramp up on how to use the library.

What I found particularly helpful were the [examples](https://github.com/elm/parser/tree/master/examples) in the parser repo, in particular the [math example](https://github.com/elm/parser/blob/master/examples/Math.elm).

It might also help to start with creating types for your parser output if you haven’t already.

I know that’s not much, but maybe it can help you get started enough to narrow your focus to smaller more specific problems. For me, that’s often key to making progress.

---

<div class="post-metadata">

### Author: ![perty](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/perty/32/4459_2.png) [@perty](https://discourse.elm-lang.org/u/perty)
#### Post date: [April 5, 2022, 3:15pm UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/3 "2022-04-05T15:15:49Z")

</div>

Not sure what it is that you need. But I decided to learn more about the elm/parser during Advent of Code 2021.

Eg: on day 4: [elm-aoc-template/Day4.elm at 2021-perty · perty/elm-aoc-template · GitHub](https://github.com/perty/elm-aoc-template/blob/2021-perty/src/Day4.elm)

Also days 2,5,6,8,10,12,13 and 14 - I find `import Parser`.

---

<div class="post-metadata">

### Author: ![andre-dietrich](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andre-dietrich/32/4106_2.png) [@andre-dietrich](https://discourse.elm-lang.org/u/andre-dietrich)
#### Post date: [April 7, 2022, 9:01am UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/4 "2022-04-07T09:01:18Z")

</div>

Hi, I would recommend elm-community parser:

[https://package.elm-lang.org/packages/andre-dietrich/parser-combinators/latest/Combine](https://package.elm-lang.org/packages/andre-dietrich/parser-combinators/latest/Combine)

This is “pretty” easy to use and there are some examples included:

> **[parser-combinators/examples at 4.1.0 · andre-dietrich/parser-combinators](https://github.com/andre-dietrich/parser-combinators/tree/4.1.0/examples)**
>
> 4.1.0/examples

I also used it for testing and build a package that generates random strings based on regex expressions:

[https://package.elm-lang.org/packages/andre-dietrich/elm-random-regex/latest/](https://package.elm-lang.org/packages/andre-dietrich/elm-random-regex/latest/)

---

<div class="post-metadata">

### Author: ![andre-dietrich](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andre-dietrich/32/4106_2.png) [@andre-dietrich](https://discourse.elm-lang.org/u/andre-dietrich)
#### Post date: [April 7, 2022, 9:11am UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/5 "2022-04-07T09:11:34Z")

</div>

and basically you require something like this:

```elm
and_expression =
   string "and"
   |> parenthesis (
            expression
            |> map Tuple.pair
            |> ignore (string ",")
            |> andMap expression
    )

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)
#### Post date: [April 17, 2022, 9:12am UTC](https://discourse.elm-lang.org/t/creating-a-parser-for-logical-propositions/8291/6 "2022-04-17T09:12:00Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
