Skip to content

Commit

Permalink
feat: update with foldable AE
Browse files Browse the repository at this point in the history
  • Loading branch information
jaschdoc committed May 5, 2024
1 parent 9d2b7b6 commit 6feaccd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/Parser.flix
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,17 @@ mod Parser {
///
/// This is generalization of `literal`.
///
pub def literalSequence(lit: m[a]): Parser[DelayList[a], a] with Eq[a], Foldable[m] =
pub def literalSequence(lit: m[a]): Parser[DelayList[a], a] \ Foldable.Aef[m] with Eq[a], Foldable[m] =
literalSequenceHelper(Foldable.toList(lit))

///
/// Helper for `literalSequence` which does not have the foldable associated effect.
///
def literalSequenceHelper(lit: List[a]): Parser[DelayList[a], a] with Eq[a] =
inp -> inp // Wrap in lambda so the recursive call does not immediately happen
|> match Foldable.toList(lit) {
|> match lit {
case Nil => succeed(ENil)
case x :: xs => (literal(x) `then` (literalSequence(xs))) `using` cons
case x :: xs => (literal(x) `then` (literalSequenceHelper(xs))) `using` cons
}

///
Expand Down Expand Up @@ -211,7 +217,7 @@ mod Parser {
///
/// Returns a parser that recognizes any of the elements in `syms`.
///
pub def any(f: a -> Parser[b, c], syms: m[a]): Parser[b, c] with Foldable[m] =
pub def any(f: a -> Parser[b, c], syms: m[a]): Parser[b, c] \ Foldable.Aef[m] with Foldable[m] =
Foldable.foldRight(f >> otherwise, fail, syms)

///
Expand All @@ -223,7 +229,7 @@ mod Parser {
///
/// Returns `chars` as a string.
///
pub def stringify(chars: m[Char]): String with Foldable[m] = region r {
pub def stringify(chars: m[Char]): String \ Foldable.Aef[m] with Foldable[m] = region r {
let sb = StringBuilder.empty(r);
let ap = sb |> flip(StringBuilder.append!);
Foldable.forEach(ap, chars);
Expand Down
2 changes: 1 addition & 1 deletion test/TestParser.flix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod TestParser {

def noInput(): Input[Char] = ENil

def l2d(l: m[a]): DelayList[a] with Foldable[m] = Foldable.toDelayList(l)
def l2d(l: m[a]): DelayList[a] \Foldable.Aef[m] with Foldable[m] = Foldable.toDelayList(l)

def d2l(l: DelayList[a]): List[a] = DelayList.toList(l)

Expand Down

0 comments on commit 6feaccd

Please sign in to comment.