on master help sign in
Text.Lexer: add `concatMap`, move `concat` out of core

definitions of `exact` and `approx` updated to use `concatMap`

commit8656531b1fcab5501abf75e0cf8c1d3ed4951c57 authorMichael Morgan <morgan.michael@me.com> dateSat 28 Oct 2017, 12:05 (9 years ago) parent85a7676
2 files changed+18−10
libs/contrib/Text/Lexer.idr+9−4 libs/contrib/Text/Lexer/Core.idr+9−6
libs/contrib/Text/Lexer.idr+9−4
@@ -23,6 +23,11 @@ non l = reject l <+> any
2323choice : Foldable t => t Lexer -> Lexer
2424choice xs = foldr (<|>) fail xs
2525
26+||| Sequence a list of recognisers. Guaranteed to consume input if the list is
27+||| non-empty and the recognisers consume.
28+concat : (xs : List (Recognise c)) -> Recognise (c && isCons xs)
29+concat = concatMap id
30+
2631||| Recognise a specific character.
2732is : Char -> Lexer
2833is x = pred (==x)
@@ -42,16 +47,16 @@ notLike x = pred (\y => toUpper x /= toUpper y)
4247||| Recognise a specific string.
4348||| Fails if the string is empty.
4449exact : String -> Lexer
45−exact str = case map is (unpack str) of
50+exact str = case unpack str of
4651 [] => fail
47− (x :: xs) => concat (x :: xs)
52+ (x :: xs) => concatMap is (x :: xs)
4853
4954||| Recognise a specific string (case-insensitive).
5055||| Fails if the string is empty.
5156approx : String -> Lexer
52−approx str = case map like (unpack str) of
57+approx str = case unpack str of
5358 [] => fail
54− (x :: xs) => concat (x :: xs)
59+ (x :: xs) => concatMap like (x :: xs)
5560
5661||| Recognise any of the characters in the given string
5762oneOf : String -> Lexer
libs/contrib/Text/Lexer/Core.idr+9−6
@@ -63,13 +63,16 @@ export
6363reject : Recognise c -> Recognise False
6464reject = Lookahead False
6565
66−||| Sequence a list of recognisers.
66+||| Sequence the recognisers resulting from applying a function to each element
67+||| of a list. The resulting recogniser will consume input if the produced
68+||| recognisers consume and the list is non-empty.
6769export
68−concat : {c : Bool} -> (xs : List (Recognise c)) -> Recognise (c && isCons xs)
69−concat {c} [] = rewrite andFalseFalse c in Empty
70−concat {c} (x :: xs) = rewrite andTrueNeutral c in
71− rewrite sym (orSameAndRightNeutral c (isCons xs)) in
72− SeqEmpty x (concat xs)
70+concatMap : {c : Bool} ->
71+ (a -> Recognise c) -> (xs : List a) -> Recognise (c && isCons xs)
72+concatMap {c} _ [] = rewrite andFalseFalse c in Empty
73+concatMap {c} f (x :: xs) = rewrite andTrueNeutral c in
74+ rewrite sym (orSameAndRightNeutral c (isCons xs)) in
75+ SeqEmpty (f x) (concatMap f xs)
7376
7477data StrLen : Type where
7578 MkStrLen : String -> Nat -> StrLen