Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 4a15816

Browse files
committed
WIP StateT operations for Grammar
1 parent 86e5546 commit 4a15816

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

libs/contrib/Text/Parser/Core.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Functor (Grammar tok c) where
8888
||| from the first grammar to the value from the second grammar.
8989
||| Guaranteed to consume if either grammar consumes.
9090
export
91-
(<*>) : Grammar tok c1 (a -> b) ->
91+
(<*>) : Grammar tok c1 (inf c1 (a -> b)) ->
9292
Grammar tok c2 a ->
9393
Grammar tok (c1 || c2) b
9494
(<*>) x y = SeqEmpty x (\f => map f y)

libs/contrib/Text/Parser/State.idr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Text.Parser.Stateful
2+
3+
import Control.Monad.State
4+
import Control.ST
5+
import Text.Parser
6+
7+
import public Control.Delayed
8+
9+
%access public export
10+
%default total
11+
12+
get : StateT stateType (Grammar tok False) stateType
13+
get = ST (\x => pure (x, x))
14+
15+
put : stateType -> StateT stateType (Grammar tok False) ()
16+
put x = ST (\y => pure ((), x))
17+
18+
modify : (stateType -> stateType) -> StateT stateType (Grammar tok False) ()
19+
modify f = ST (\x => pure ((), f x))
20+
21+
lemma : (a, stateType) -> Inf (a -> StateT stateType (Grammar tok c2) b) -> Grammar tok c2 (b, stateType)
22+
lemma (v, st') k = let ST kv = k v in
23+
kv st'
24+
25+
(>>=) : StateT stateType (Grammar tok c1) a ->
26+
inf c1 (a -> StateT stateType (Grammar tok c2) b) ->
27+
StateT stateType (Grammar tok (c1 || c2)) b
28+
(>>=) {c1 = False} (ST f) k = ST (\st => do (v, st') <- f st
29+
let ST kv = k v
30+
kv st')
31+
(>>=) {c1 = True} (ST f) k = ST (\st => do x <- f st
32+
lemma x k)
33+
34+
pure : a -> StateT stateType (Grammar tok False) a
35+
pure x = ST (\st => pure (x, st))
36+
37+
38+
map : (a -> b) -> StateT stateType (Grammar tok c) a ->
39+
StateT stateType (Grammar tok c) b
40+
map f (ST g) = ST (\st => map (mapFst f) (g st))
41+
where mapFst : (a -> x) -> (a, s) -> (x, s)
42+
mapFst fn (a, b) = (fn a, b)
43+
44+
-- (>>=) {c1 = True} {c2 = True} (ST f) k = ST (\st => (f st) >>= Delay (\x => ?whatNow3))
45+
-- kv st')
46+
47+
48+
-- gets : (stateType -> a) -> StateT stateType (Grammar tok False) a
49+
-- gets f = do s <- get
50+
-- pure (f s)
51+
52+
53+
GrammarTrans : Type -> Type -> Bool -> Type -> Type
54+
GrammarTrans st tok consumes result = st -> Grammar tok consumes (result, st)
55+
56+
identity : Grammar tok True a -> GrammarTrans st tok True a
57+
identity g state = map (\x => (x, state)) g
58+
59+
comma : Grammar Char True ()
60+
comma = terminal (\c => if c == ',' then Just () else Nothing)
61+
62+
commaCounter : GrammarTrans Nat Char True ()
63+
commaCounter n = do comma
64+
commaCounter (S n) <|> pure ((), S n)
65+
66+
countCommas : Grammar Char True Nat
67+
countCommas = map (\(_, n) => n) (commaCounter Z)
68+

libs/contrib/contrib.ipkg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ modules = CFFI
9090

9191
, Text.Parser
9292
, Text.Parser.Core
93+
, Text.Parser.State
9394

9495
, Text.PrettyPrint.WL.Core
9596
, Text.PrettyPrint.WL.Combinators

0 commit comments

Comments
 (0)