Skip to content

Commit b008aa7

Browse files
committed
sketch an implementation for reset
1 parent 271991e commit b008aa7

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

src/compiler/api/GF/Compile/Compute/Concrete2.hs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ data Value
6464
| VAlts Value [(Value, Value)]
6565
| VStrs [Value]
6666
| VMarkup Ident [(Ident,Value)] [Value]
67+
| VReset Control Value
6768
| VSymCat Int LIndex [(LIndex, (Value, Type))]
6869
| VError Doc
6970
-- These two constructors are only used internally
@@ -254,10 +255,7 @@ eval g env c (Markup tag as ts) [] =
254255
vas = mapC (\c (id,t) -> (id,eval g env c t [])) c1 as
255256
vs = mapC (\c t -> eval g env c t []) c2 ts
256257
in (VMarkup tag vas vs)
257-
eval g env c (Reset ctl t) [] =
258-
let limit All = id
259-
limit (Limit n) = fmap (genericTake n)
260-
in (VMarkup identW [] [eval g env c t []])
258+
eval g env c (Reset ctl t) [] = VReset ctl (eval g env c t [])
261259
eval g env c (TSymCat d r rs) []= VSymCat d r [(i,(fromJust (lookup pv env),ty)) | (i,(pv,ty)) <- rs]
262260
eval g env c t vs = VError ("Cannot reduce term" <+> pp t)
263261

@@ -325,6 +323,7 @@ bubble v = snd (bubble v)
325323
let (union1,attrs') = mapAccumL descend' Map.empty attrs
326324
(union2,vs') = mapAccumL descend union1 vs
327325
in (union2, VMarkup tag attrs' vs')
326+
bubble (VReset ctl v) = lift1 (VReset ctl) v
328327
bubble (VSymCat d i0 vs) =
329328
let (union,vs') = mapAccumL descendC Map.empty vs
330329
in (union, addVariants (VSymCat d i0 vs') union)
@@ -610,6 +609,14 @@ runEvalM g (EvalM f) = Check $ \(es,ws) ->
610609
where
611610
empty = State Map.empty Map.empty
612611

612+
reset :: EvalM a -> EvalM [a]
613+
reset (EvalM f) = EvalM $ \g k state r ws ->
614+
case f g (\x state xs ws -> Success (x:xs) ws) state [] ws of
615+
Fail msg ws -> Fail msg ws
616+
Success xs ws -> k (reverse xs) state r ws
617+
where
618+
empty = State Map.empty Map.empty
619+
613620
globals :: EvalM Globals
614621
globals = EvalM (\g k -> k g)
615622

@@ -784,6 +791,29 @@ value2termM flat xs (VMarkup tag as vs) = do
784791
as <- mapM (\(id,v) -> value2termM flat xs v >>= \t -> return (id,t)) as
785792
ts <- mapM (value2termM flat xs) vs
786793
return (Markup tag as ts)
794+
value2termM flat xs (VReset ctl v) = do
795+
ts <- reset (value2termM True xs v)
796+
case ctl of
797+
All -> case ts of
798+
[t] -> return t
799+
ts -> return (Markup identW [] ts)
800+
One -> case ts of
801+
[] -> mzero
802+
(t:ts) -> return t
803+
Limit n -> case genericTake n ts of
804+
[t] -> return t
805+
ts -> return (Markup identW [] ts)
806+
Coordination (Just mn) conj id ->
807+
case ts of
808+
[] -> mzero
809+
[t] -> return t
810+
ts -> do let cat = showIdent id
811+
t <- listify mn cat ts
812+
return (App (App (QC (mn,identS ("Conj"++cat))) (QC (mn,conj))) t)
813+
where
814+
listify mn cat [t1,t2] = do return (App (App (QC (mn,identS ("Base"++cat))) t1) t2)
815+
listify mn cat (t1:ts) = do t2 <- listify mn id ts
816+
return (App (App (QC (mn,identS ("Cons"++cat))) t1) t2)
787817
value2termM flat xs (VError msg) = evalError msg
788818
value2termM flat xs (VCRecType lbls) = do
789819
lbls <- mapM (\(lbl,_,v) -> fmap ((,) lbl) (value2termM flat xs v)) lbls

src/compiler/api/GF/Compile/Rename.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,22 @@ renameTerm env vars = ren vars where
238238
(p',_) <- renpatt p
239239
return $ EPatt minp maxp p'
240240

241+
Reset ctl t -> do
242+
ctl <- case ctl of
243+
Coordination _ conj cat ->
244+
checks [ do t <- renid' (Cn conj)
245+
case t of
246+
Q (mn,id) -> return (Coordination (Just mn) conj cat)
247+
QC (mn,id) -> return (Coordination (Just mn) conj cat)
248+
_ -> return (Coordination Nothing conj cat)
249+
, if showIdent conj == "one"
250+
then return One
251+
else checkError ("Undefined control" <+> pp conj)
252+
]
253+
ctl -> do return ctl
254+
t <- ren vs t
255+
return (Reset ctl t)
256+
241257
_ -> composOp (ren vs) trm
242258

243259
renid = renameIdentTerm env

src/compiler/api/GF/Compile/TypeCheck/ConcreteNew.hs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,21 @@ tcRho scope c (Markup tag attrs children) mb_ty = do
388388
c1 attrs
389389
res <- mapCM (\c child -> tcRho scope c child Nothing) c2 children
390390
instSigma scope c3 (Markup tag attrs (map fst res)) vtypeMarkup mb_ty
391-
tcRho scope c (Reset ctl t) mb_ty = do
391+
tcRho scope c (Reset ctl t) mb_ty =
392392
let (c1,c2) = split c
393-
(t,_) <- tcRho scope c1 t Nothing
394-
instSigma scope c2 (Reset ctl t) vtypeMarkup mb_ty
393+
in case ctl of
394+
All -> do (t,_) <- tcRho scope c1 t Nothing
395+
instSigma scope c2 (Reset ctl t) vtypeMarkup mb_ty
396+
One -> do (t,ty) <- tcRho scope c t mb_ty
397+
return (Reset ctl t,ty)
398+
Limit n -> do (t,_) <- tcRho scope c1 t Nothing
399+
instSigma scope c2 (Reset ctl t) vtypeMarkup mb_ty
400+
Coordination mb_mn@(Just mn) conj _
401+
-> do tcRho scope c1 (QC (mn,conj)) (Just (VApp (mn,identS "Conj") []))
402+
(t,ty) <- tcRho scope c2 t mb_ty
403+
case ty of
404+
VApp id [] -> return (Reset (Coordination mb_mn conj (snd id)) t, ty)
405+
_ -> evalError (pp "Needs atomic type"<+>ppValue Unqualified 0 ty)
395406
tcRho scope s t _ = unimplemented ("tcRho "++show t)
396407

397408
evalCodomain :: Scope -> Ident -> Value -> EvalM Value

src/compiler/api/GF/Grammar/Grammar.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ data Term =
407407

408408
data Control
409409
= All
410+
| One
410411
| Limit Integer
412+
| Coordination (Maybe ModuleName) Ident Ident
411413
deriving (Show, Eq, Ord)
412414

413415
-- | Patterns

src/compiler/api/GF/Grammar/Parser.y

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ ListMarkup :: { [Term] }
739739
Control :: { Control }
740740
: { All }
741741
| Integer { Limit (fromIntegral $1) }
742+
| Ident { Coordination Nothing $1 identW }
742743

743744
Attributes :: { [(Ident,Term)] }
744745
Attributes

src/compiler/api/GF/Grammar/Printer.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,21 @@ ppTerm q d (Markup tag attrs children)
256256
| otherwise = pp "<" <> pp tag <+> hsep (map (ppMarkupAttr q) attrs) <> pp ">" $$
257257
nest 3 (ppMarkupChildren q children) $$
258258
pp "</" <> pp tag <> pp ">"
259-
ppTerm q d (Reset c t)
260-
= pp "[:" <> ppControl c <+> pp "|" <> ppTerm q 0 t <> pp "]"
259+
ppTerm q d (Reset ctl t)
260+
= pp "[:" <> ppControl q ctl <+> pp "|" <> ppTerm q 0 t <> pp "]"
261261
ppTerm q d (TSymCat i r rs) = pp '<' <> pp i <> pp ',' <> ppLinFun (pp.fst) r rs <> pp '>'
262262
ppTerm q d (TSymVar i r) = pp '<' <> pp i <> pp ',' <> pp '$' <> pp r <> pp '>'
263263

264264
ppEquation q (ps,e) = hcat (map (ppPatt q 2) ps) <+> "->" <+> ppTerm q 0 e
265265

266266
ppCase q (p,e) = ppPatt q 0 p <+> "=>" <+> ppTerm q 0 e
267267

268-
ppControl All = empty
269-
ppControl (Limit n) = pp n
268+
ppControl q All = empty
269+
ppControl q One = pp "one"
270+
ppControl q (Limit n) = pp n
271+
ppControl q (Coordination mb_mn n _) = ppTerm q 0 (case mb_mn of
272+
Just mn -> QC (mn,n)
273+
Nothing -> Cn n)
270274

271275
instance Pretty Patt where pp = ppPatt Unqualified 0
272276

0 commit comments

Comments
 (0)