@@ -86,7 +86,7 @@ data Value
8686 | VAlts Value [(Value , Value )]
8787 | VStrs [Value ]
8888 | VMarkup Ident [(Ident ,Value )] [Value ]
89- | VReset Control Value
89+ | VReset Ident ( Maybe Value ) Value QIdent
9090 | VSymCat Int LIndex [(LIndex , (Value , Type ))]
9191 | VError Doc
9292 -- These two constructors are only used internally
@@ -124,7 +124,7 @@ isCanonicalForm False (VFV c vs) = all (isCanonicalForm False) (unvaria
124124isCanonicalForm flat (VAlts d vs) = all (isCanonicalForm flat . snd ) vs
125125isCanonicalForm flat (VStrs vs) = all (isCanonicalForm flat) vs
126126isCanonicalForm flat (VMarkup tag as vs) = all (isCanonicalForm flat . snd ) as && all (isCanonicalForm flat) vs
127- isCanonicalForm flat (VReset ctl v) = isCanonicalForm flat v
127+ isCanonicalForm flat (VReset ctl cv v _) = maybe True (isCanonicalForm flat) cv && isCanonicalForm flat v
128128isCanonicalForm flat _ = False
129129
130130data ConstValue a
@@ -324,7 +324,7 @@ eval g env c (Markup tag as ts) [] =
324324 vas = mapC (\ c (id ,t) -> (id ,eval g env c t [] )) c1 as
325325 vs = mapC (\ c t -> eval g env c t [] ) c2 ts
326326 in (VMarkup tag vas vs)
327- eval g env c (Reset ctl t ) [] = VReset ctl (eval g env c t [] )
327+ eval g env c (Reset ctl mb_ct t qid ) [] = VReset ctl (fmap ( \ t -> eval g env c t [] ) mb_ct) (eval g env c t [] ) qid
328328eval g env c (TSymCat d r rs) [] = VSymCat d r [(i,(fromJust (lookup pv env),ty)) | (i,(pv,ty)) <- rs]
329329eval g env c t@ (Opts n cs) vs = if null cs
330330 then VError (" No options in expression:" $$ ppTerm Unqualified 0 t)
@@ -422,7 +422,7 @@ bubble v = snd (bubble v)
422422 let (union1,attrs') = mapAccumL descend' Map. empty attrs
423423 (union2,vs') = mapAccumL descend union1 vs
424424 in (union2, VMarkup tag attrs' vs')
425- bubble (VReset ctl v ) = lift1 (VReset ctl) v
425+ bubble (VReset ctl mb_cv v id ) = lift1 (\ v -> VReset ctl mb_cv v id ) v
426426 bubble (VSymCat d i0 vs) =
427427 let (union,vs') = mapAccumL descendC Map. empty vs
428428 in (union, addVariants (VSymCat d i0 vs') union)
@@ -932,26 +932,42 @@ value2termM flat xs (VMarkup tag as vs) = do
932932 as <- mapM (\ (id ,v) -> value2termM flat xs v >>= \ t -> return (id ,t)) as
933933 ts <- mapM (value2termM flat xs) vs
934934 return (Markup tag as ts)
935- value2termM flat xs (VReset ctl v ) = do
935+ value2termM flat xs (VReset ctl mb_cv v qid ) = do
936936 ts <- reset (value2termM True xs v)
937- case ctl of
938- All -> case ts of
939- [t] -> return t
940- ts -> return (Markup identW [] ts)
941- One -> case ts of
942- [] -> mzero
943- (t: ts) -> return t
944- Limit n -> case genericTake n ts of
945- [t] -> return t
946- ts -> return (Markup identW [] ts)
947- Coordination (Just mn) conj id ->
948- case ts of
949- [] -> mzero
950- [t] -> return t
951- ts -> do let cat = showIdent id
952- t <- listify mn cat ts
953- return (App (App (QC (mn,identS (" Conj" ++ cat))) (QC (mn,conj))) t)
937+ reduce ctl mb_cv ts
954938 where
939+ reduce ctl mb_cv ts
940+ | ctl == cConcat = do
941+ ts' <- case mb_cv of
942+ Just (VInt n) -> return (genericTake n ts)
943+ Nothing -> return ts
944+ _ -> evalError (pp " [concat: .. | ..] requires an integer constant" )
945+ case ts of
946+ [t] -> return t
947+ ts -> return (Markup identW [] ts)
948+ | ctl == cOne =
949+ case (ts,mb_cv) of
950+ ([] ,Nothing ) -> mzero
951+ ([] ,Just v) -> value2termM flat xs v
952+ (t: ts,_) -> return t
953+ | ctl == cDefault =
954+ case (ts,mb_cv) of
955+ ([] ,Nothing ) -> mzero
956+ ([] ,Just v) -> value2termM flat xs v
957+ (ts,_) -> msum (map pure ts)
958+ | ctl == cList =
959+ case (ts,mb_cv) of
960+ ([] , _) -> mzero
961+ ([t], _) -> return t
962+ (ts,Just cv) ->
963+ do let cat = showIdent (snd qid)
964+ mn = fst qid
965+ ct <- value2termM flat xs cv
966+ t <- listify mn cat ts
967+ return (App (App (QC (mn,identS (" Conj" ++ cat))) ct) t)
968+ _ -> evalError (pp " [list: .. | ..] requires an argument" )
969+ | otherwise = evalError (pp " Operator" <+> pp ctl <+> pp " is not defined" )
970+
955971 listify mn cat [t1,t2] = do return (App (App (QC (mn,identS (" Base" ++ cat))) t1) t2)
956972 listify mn cat (t1: ts) = do t2 <- listify mn cat ts
957973 return (App (App (QC (mn,identS (" Cons" ++ cat))) t1) t2)
0 commit comments