Translating linearization functions to Haskell: use qualified names to avoid name clashes

All languages in the Phasebook can now be converted to compilable Haskell
code.

STILL TODO:
  
  	- variants
  	- pre { ... }
This commit is contained in:
hallgren
2015-01-07 16:13:28 +00:00
parent 51a233b2f1
commit 6db2845375

View File

@@ -28,12 +28,18 @@ concretes2haskell opts absname gr =
concrete2haskell opts gr cenv absname cnc modinfo = concrete2haskell opts gr cenv absname cnc modinfo =
render $ render $
haskPreamble absname cnc $+$ "" $+$ haskPreamble absname cnc $$ "" $$
vcat (neededParamTypes S.empty (params defs)) $+$ "" $+$ "--- Parameter types ---" $$
vcat (map signature (S.toList allcats)) $+$ "" $+$ vcat (neededParamTypes S.empty (params defs)) $$ "" $$
vcat emptydefs $+$ "--- Type signatures for linearization functions ---" $$
vcat (map ppDef defs) $+$ "" $+$ vcat (map signature (S.toList allcats)) $$ "" $$
vcat (map labelClass (S.toList (S.unions (map S.fromList rs)))) $+$ "" $+$ "--- Linearization functions for empty categories ---" $$
vcat emptydefs $$ "" $$
"--- Linearization types and linearization functions ---" $$
vcat (map ppDef defs) $$ "" $$
"--- Type classes for projection functions ---" $$
vcat (map labelClass (S.toList (S.unions (map S.fromList rs)))) $$ "" $$
"--- Record types ---" $$
vcat (map recordType rs) vcat (map recordType rs)
where where
rs = S.toList (S.insert [ident2label (identS "s")] (records rhss)) rs = S.toList (S.insert [ident2label (identS "s")] (records rhss))
@@ -225,8 +231,8 @@ convert' atomic gId gr = if atomic then ppA else ppT
EInt n -> pp n EInt n -> pp n
Q (m,n) -> if m==cPredef Q (m,n) -> if m==cPredef
then ppPredef n then ppPredef n
else pp n else pp (qual m n)
QC (m,n) -> gId n QC (m,n) -> gId (qual m n)
K s -> token s K s -> token s
Empty -> pp "[]" Empty -> pp "[]"
FV (t:ts) -> "{-variants-}"<>ppA t -- !! FV (t:ts) -> "{-variants-}"<>ppA t -- !!
@@ -309,8 +315,8 @@ convType' atomic gId = if atomic then ppA else ppT
Sort k -> pp k Sort k -> pp k
EInt n -> parens ("{-"<>n<>"-}") -- type level numeric literal EInt n -> parens ("{-"<>n<>"-}") -- type level numeric literal
FV (t:ts) -> "{-variants-}"<>ppA t -- !! FV (t:ts) -> "{-variants-}"<>ppA t -- !!
QC (m,n) -> gId n QC (m,n) -> gId (qual m n)
Q (m,n) -> gId n Q (m,n) -> gId (qual m n)
_ -> {-trace (show t) $-} parens (ppT' True t) _ -> {-trace (show t) $-} parens (ppT' True t)
fields = map (ppA.snd) . sort . filter (not.isLockLabel.fst) fields = map (ppA.snd) . sort . filter (not.isLockLabel.fst)
@@ -354,28 +360,32 @@ paramType gId gr q@(_,n) =
Ok (m,ResParam (Just (L _ ps)) _) Ok (m,ResParam (Just (L _ ps)) _)
{- - | m/=cPredef && m/=moduleNameS "Prelude"-} -> {- - | m/=cPredef && m/=moduleNameS "Prelude"-} ->
((S.singleton (m,n),argTypes ps), ((S.singleton (m,n),argTypes ps),
"data"<+>gId n<+>"="<+> "data"<+>gId (qual m n)<+>"="<+>
sep [fsep (punctuate " |" (map param ps)), sep [fsep (punctuate " |" (map (param m) ps)),
pp "deriving (Eq,Ord,Show)"] $$ pp "deriving (Eq,Ord,Show)"] $$
hang ("instance EnumAll"<+>gId n<+>"where") 4 hang ("instance EnumAll"<+>gId (qual m n)<+>"where") 4
("enumAll"<+>"="<+>sep (punctuate "++" (map enumParam ps))) ("enumAll"<+>"="<+>sep (punctuate " ++" (map (enumParam m) ps)))
) )
Ok (m,ResOper _ (Just (L _ t))) Ok (m,ResOper _ (Just (L _ t)))
| m==cPredef && n==cInts -> | m==cPredef && n==cInts ->
((S.singleton (m,n),S.empty),pp "type GInts n = Int") ((S.singleton (m,n),S.empty),
"type"<+>gId (qual m n)<+>"n = Int")
| otherwise -> | otherwise ->
((S.singleton (m,n),paramTypes gr t), ((S.singleton (m,n),paramTypes gr t),
"type"<+>gId n<+>"="<+>convType gId t) "type"<+>gId (qual m n)<+>"="<+>convType gId t)
_ -> ((S.empty,S.empty),empty) _ -> ((S.empty,S.empty),empty)
where where
param (n,ctx) = gId n<+>[convTypeA gId t|(_,_,t)<-ctx] param m (n,ctx) = gId (qual m n)<+>[convTypeA gId t|(_,_,t)<-ctx]
argTypes = S.unions . map argTypes1 argTypes = S.unions . map argTypes1
argTypes1 (n,ctx) = S.unions [paramTypes gr t|(_,_,t)<-ctx] argTypes1 (n,ctx) = S.unions [paramTypes gr t|(_,_,t)<-ctx]
enumParam (n,ctx) = enumCon (gId n) (length ctx) enumParam m (n,ctx) = enumCon (gId (qual m n)) (length ctx)
enumCon name arity = enumCon name arity =
if arity==0 if arity==0
then brackets name then brackets name
else parens $ else parens $
fsep ((name<+>"<$>"):punctuate "<*>" (replicate arity (pp "enumAll"))) fsep ((name<+>"<$>"):punctuate " <*>" (replicate arity (pp "enumAll")))
qual :: ModuleName -> Ident -> Ident
qual m = prefixIdent (render m++"_")