forked from GitHub/gf-core
operations in the abstract syntax
This commit is contained in:
@@ -88,7 +88,7 @@ instance Binary Options where
|
||||
|
||||
instance Binary Info where
|
||||
put (AbsCat x) = putWord8 0 >> put x
|
||||
put (AbsFun x y z) = putWord8 1 >> put (x,y,z)
|
||||
put (AbsFun w x y z) = putWord8 1 >> put (w,x,y,z)
|
||||
put (ResParam x y) = putWord8 2 >> put (x,y)
|
||||
put (ResValue x) = putWord8 3 >> put x
|
||||
put (ResOper x y) = putWord8 4 >> put (x,y)
|
||||
@@ -98,15 +98,15 @@ instance Binary Info where
|
||||
put (AnyInd x y) = putWord8 8 >> put (x,y)
|
||||
get = do tag <- getWord8
|
||||
case tag of
|
||||
0 -> get >>= \x -> return (AbsCat x)
|
||||
1 -> get >>= \(x,y,z) -> return (AbsFun x y z)
|
||||
2 -> get >>= \(x,y) -> return (ResParam x y)
|
||||
3 -> get >>= \x -> return (ResValue x)
|
||||
4 -> get >>= \(x,y) -> return (ResOper x y)
|
||||
5 -> get >>= \(x,y) -> return (ResOverload x y)
|
||||
6 -> get >>= \(x,y,z) -> return (CncCat x y z)
|
||||
7 -> get >>= \(x,y,z) -> return (CncFun x y z)
|
||||
8 -> get >>= \(x,y) -> return (AnyInd x y)
|
||||
0 -> get >>= \x -> return (AbsCat x)
|
||||
1 -> get >>= \(w,x,y,z) -> return (AbsFun w x y z)
|
||||
2 -> get >>= \(x,y) -> return (ResParam x y)
|
||||
3 -> get >>= \x -> return (ResValue x)
|
||||
4 -> get >>= \(x,y) -> return (ResOper x y)
|
||||
5 -> get >>= \(x,y) -> return (ResOverload x y)
|
||||
6 -> get >>= \(x,y,z) -> return (CncCat x y z)
|
||||
7 -> get >>= \(x,y,z) -> return (CncFun x y z)
|
||||
8 -> get >>= \(x,y) -> return (AnyInd x y)
|
||||
_ -> decodingError
|
||||
|
||||
instance Binary a => Binary (L a) where
|
||||
|
||||
@@ -110,7 +110,7 @@ cf2cat (L loc (_,(cat, items))) = map identS $ cat : [c | Left c <- items]
|
||||
cf2rule :: CFRule -> ((Ident,Info),(Ident,Info))
|
||||
cf2rule (L loc (fun, (cat, items))) = (def,ldef) where
|
||||
f = identS fun
|
||||
def = (f, AbsFun (Just (L loc (mkProd args' (Cn (identS cat)) []))) Nothing Nothing)
|
||||
def = (f, AbsFun (Just (L loc (mkProd args' (Cn (identS cat)) []))) Nothing Nothing (Just True))
|
||||
args0 = zip (map (identS . ("x" ++) . show) [0..]) items
|
||||
args = [((Explicit,v), Cn (identS c)) | (v, Left c) <- args0]
|
||||
args' = [(Explicit,identS "_", Cn (identS c)) | (_, Left c) <- args0]
|
||||
|
||||
@@ -76,8 +76,8 @@ mapSourceModule f (i,mi) = (i, f mi)
|
||||
-- and indirection to module (/INDIR/)
|
||||
data Info =
|
||||
-- judgements in abstract syntax
|
||||
AbsCat (Maybe (L Context)) -- ^ (/ABS/) context of a category
|
||||
| AbsFun (Maybe (L Type)) (Maybe Int) (Maybe [L Equation]) -- ^ (/ABS/) type, arrity and definition of a function
|
||||
AbsCat (Maybe (L Context)) -- ^ (/ABS/) context of a category
|
||||
| AbsFun (Maybe (L Type)) (Maybe Int) (Maybe [L Equation]) (Maybe Bool) -- ^ (/ABS/) type, arrity and definition of a function
|
||||
|
||||
-- judgements in resource
|
||||
| ResParam (Maybe [L Param]) (Maybe [Term]) -- ^ (/RES/) the second parameter is list of all possible values
|
||||
|
||||
@@ -156,9 +156,9 @@ lookupAbsDef gr m c = errIn (render (text "looking up absdef of" <+> ppIdent c))
|
||||
mo <- lookupModule gr m
|
||||
info <- lookupIdentInfo mo c
|
||||
case info of
|
||||
AbsFun _ a d -> return (a,fmap (map unLoc) d)
|
||||
AnyInd _ n -> lookupAbsDef gr n c
|
||||
_ -> return (Nothing,Nothing)
|
||||
AbsFun _ a d _ -> return (a,fmap (map unLoc) d)
|
||||
AnyInd _ n -> lookupAbsDef gr n c
|
||||
_ -> return (Nothing,Nothing)
|
||||
|
||||
lookupLincat :: SourceGrammar -> Ident -> Ident -> Err Type
|
||||
lookupLincat gr m c | isPredefCat c = return defLinType --- ad hoc; not needed?
|
||||
@@ -176,9 +176,9 @@ lookupFunType gr m c = do
|
||||
mo <- lookupModule gr m
|
||||
info <- lookupIdentInfo mo c
|
||||
case info of
|
||||
AbsFun (Just (L _ t)) _ _ -> return t
|
||||
AnyInd _ n -> lookupFunType gr n c
|
||||
_ -> Bad (render (text "cannot find type of" <+> ppIdent c))
|
||||
AbsFun (Just (L _ t)) _ _ _ -> return t
|
||||
AnyInd _ n -> lookupFunType gr n c
|
||||
_ -> Bad (render (text "cannot find type of" <+> ppIdent c))
|
||||
|
||||
-- | this is needed at compile time
|
||||
lookupCatContext :: SourceGrammar -> Ident -> Ident -> Err Context
|
||||
|
||||
@@ -624,7 +624,7 @@ allDependencies ism b =
|
||||
ResParam (Just ps) _ -> [Just (L loc t) | L loc (_,cont) <- ps, (_,_,t) <- cont]
|
||||
CncCat pty _ _ -> [pty]
|
||||
CncFun _ pt _ -> [pt] ---- (Maybe (Ident,(Context,Type))
|
||||
AbsFun pty _ ptr -> [pty] --- ptr is def, which can be mutual
|
||||
AbsFun pty _ ptr _ -> [pty] --- ptr is def, which can be mutual
|
||||
AbsCat (Just (L loc co)) -> [Just (L loc ty) | (_,_,ty) <- co]
|
||||
_ -> []
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ ModDef
|
||||
(mtype,id) = $2
|
||||
(extends,with,content) = $4
|
||||
(opens,jments,opts) = case content of { Just c -> c; Nothing -> ([],[],noOptions) }
|
||||
mapM_ (checkInfoType mtype) jments
|
||||
jments <- mapM (checkInfoType mtype) jments
|
||||
defs <- case buildAnyTree id jments of
|
||||
Ok x -> return x
|
||||
Bad msg -> fail msg
|
||||
@@ -233,19 +233,19 @@ CatDef
|
||||
|
||||
FunDef :: { [(Ident,Info)] }
|
||||
FunDef
|
||||
: Posn ListIdent ':' Exp Posn { [(fun, AbsFun (Just (mkL $1 $5 $4)) Nothing (Just [])) | fun <- $2] }
|
||||
: Posn ListIdent ':' Exp Posn { [(fun, AbsFun (Just (mkL $1 $5 $4)) Nothing (Just []) (Just True)) | fun <- $2] }
|
||||
|
||||
DefDef :: { [(Ident,Info)] }
|
||||
DefDef
|
||||
: Posn ListName '=' Exp Posn { [(f, AbsFun Nothing (Just 0) (Just [mkL $1 $5 ([],$4)])) | f <- $2] }
|
||||
| Posn Name ListPatt '=' Exp Posn { [($2,AbsFun Nothing (Just (length $3)) (Just [mkL $1 $6 ($3,$5)]))] }
|
||||
: Posn ListName '=' Exp Posn { [(f, AbsFun Nothing (Just 0) (Just [mkL $1 $5 ([],$4)]) Nothing) | f <- $2] }
|
||||
| Posn Name ListPatt '=' Exp Posn { [($2,AbsFun Nothing (Just (length $3)) (Just [mkL $1 $6 ($3,$5)]) Nothing)] }
|
||||
|
||||
DataDef :: { [(Ident,Info)] }
|
||||
DataDef
|
||||
: Posn Ident '=' ListDataConstr Posn { ($2, AbsCat Nothing) :
|
||||
[(fun, AbsFun Nothing Nothing Nothing) | fun <- $4] }
|
||||
[(fun, AbsFun Nothing Nothing Nothing (Just True)) | fun <- $4] }
|
||||
| Posn ListIdent ':' Exp Posn { -- (snd (valCat $4), AbsCat Nothing) :
|
||||
[(fun, AbsFun (Just (mkL $1 $5 $4)) Nothing Nothing) | fun <- $2] }
|
||||
[(fun, AbsFun (Just (mkL $1 $5 $4)) Nothing Nothing (Just True)) | fun <- $2] }
|
||||
|
||||
ParamDef :: { [(Ident,Info)] }
|
||||
ParamDef
|
||||
@@ -620,8 +620,8 @@ listCatDef (L loc (id,cont,size)) = [catd,nilfund,consfund]
|
||||
consId = mkConsId id
|
||||
|
||||
catd = (listId, AbsCat (Just (L loc cont')))
|
||||
nilfund = (baseId, AbsFun (Just (L loc niltyp)) Nothing Nothing)
|
||||
consfund = (consId, AbsFun (Just (L loc constyp)) Nothing Nothing)
|
||||
nilfund = (baseId, AbsFun (Just (L loc niltyp)) Nothing Nothing (Just True))
|
||||
consfund = (consId, AbsFun (Just (L loc constyp)) Nothing Nothing (Just True))
|
||||
|
||||
cont' = [(b,mkId x i,ty) | (i,(b,x,ty)) <- zip [0..] cont]
|
||||
xs = map (\(b,x,t) -> Vr x) cont'
|
||||
@@ -671,34 +671,34 @@ isOverloading t =
|
||||
Vr keyw | showIdent keyw == "overload" -> True -- overload is a "soft keyword"
|
||||
_ -> False
|
||||
|
||||
checkInfoType mt (id,info) =
|
||||
checkInfoType mt jment@(id,info) =
|
||||
case info of
|
||||
AbsCat pcont -> ifAbstract mt (locPerh pcont)
|
||||
AbsFun pty _ pde -> ifAbstract mt (locPerh pty ++ maybe [] locAll pde)
|
||||
CncCat pty pd ppn -> ifConcrete mt (locPerh pty ++ locPerh pd ++ locPerh ppn)
|
||||
CncFun _ pd ppn -> ifConcrete mt (locPerh pd ++ locPerh ppn)
|
||||
ResParam pparam _ -> ifResource mt (maybe [] locAll pparam)
|
||||
ResValue ty -> ifResource mt (locL ty)
|
||||
ResOper pty pt -> ifResource mt (locPerh pty ++ locPerh pt)
|
||||
ResOverload _ xs -> ifResource mt (concat [[loc1,loc2] | (L loc1 _,L loc2 _) <- xs])
|
||||
AbsCat pcont -> ifAbstract mt (locPerh pcont)
|
||||
AbsFun pty _ pde _ -> ifAbstract mt (locPerh pty ++ maybe [] locAll pde)
|
||||
CncCat pty pd ppn -> ifConcrete mt (locPerh pty ++ locPerh pd ++ locPerh ppn)
|
||||
CncFun _ pd ppn -> ifConcrete mt (locPerh pd ++ locPerh ppn)
|
||||
ResParam pparam _ -> ifResource mt (maybe [] locAll pparam)
|
||||
ResValue ty -> ifResource mt (locL ty)
|
||||
ResOper pty pt -> return (id,AbsFun pty (fmap (const 0) pt) (Just (maybe [] (\(L l t) -> [L l ([],t)]) pt)) (Just False))
|
||||
ResOverload _ xs -> ifResource mt (concat [[loc1,loc2] | (L loc1 _,L loc2 _) <- xs])
|
||||
where
|
||||
locPerh = maybe [] locL
|
||||
locAll xs = [loc | L loc x <- xs]
|
||||
locL (L loc x) = [loc]
|
||||
|
||||
illegal ((s,e):_) = failLoc (Pn s 0) "illegal definition"
|
||||
illegal _ = return ()
|
||||
illegal _ = return jment
|
||||
|
||||
ifAbstract MTAbstract locs = return ()
|
||||
ifAbstract MTAbstract locs = return jment
|
||||
ifAbstract _ locs = illegal locs
|
||||
|
||||
ifConcrete (MTConcrete _) locs = return ()
|
||||
ifConcrete (MTConcrete _) locs = return jment
|
||||
ifConcrete _ locs = illegal locs
|
||||
|
||||
ifResource (MTConcrete _) locs = return ()
|
||||
ifResource (MTInstance _) locs = return ()
|
||||
ifResource MTInterface locs = return ()
|
||||
ifResource MTResource locs = return ()
|
||||
ifResource (MTConcrete _) locs = return jment
|
||||
ifResource (MTInstance _) locs = return jment
|
||||
ifResource MTInterface locs = return jment
|
||||
ifResource MTResource locs = return jment
|
||||
ifResource _ locs = illegal locs
|
||||
|
||||
mkAlts cs = case cs of
|
||||
|
||||
@@ -78,9 +78,13 @@ ppJudgement q (id, AbsCat pcont ) =
|
||||
(case pcont of
|
||||
Just (L _ cont) -> hsep (map (ppDecl q) cont)
|
||||
Nothing -> empty) <+> semi
|
||||
ppJudgement q (id, AbsFun ptype _ pexp) =
|
||||
ppJudgement q (id, AbsFun ptype _ pexp poper) =
|
||||
let kind | isNothing pexp = "data"
|
||||
| poper == Just False = "oper"
|
||||
| otherwise = "fun"
|
||||
in
|
||||
(case ptype of
|
||||
Just (L _ typ) -> text (if isNothing pexp then "data" else "fun") <+> ppIdent id <+> colon <+> ppTerm q 0 typ <+> semi
|
||||
Just (L _ typ) -> text kind <+> ppIdent id <+> colon <+> ppTerm q 0 typ <+> semi
|
||||
Nothing -> empty) $$
|
||||
(case pexp of
|
||||
Just [] -> empty
|
||||
|
||||
Reference in New Issue
Block a user