abau / co4

COmplexity COncerned COnstraint COmpiler
GNU General Public License v3.0
2 stars 3 forks source link

Illegal variable name: ‘enc[]Cons’ #115

Open jwaldmann opened 6 years ago

jwaldmann commented 6 years ago

(with today's PR, with ghc-8.2)

stack test
...
/home/waldmann/projekte/my-co4/test/CO4/Example/Binary.hs:18:4: error:
    Illegal variable name: ‘enc[]Cons’
    When splicing a TH declaration:
      enc[]Cons = CO4.EncodedAdt.encodedConstructor 0 2 []
   |         
18 | $( compileFile [Cache,ImportPrelude] "test/CO4/Example/BinaryStandalone.hs" )
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        
jwaldmann commented 6 years ago

The relevant source line in test/CO4/Example/BinaryStandalone.hs is

     17 mult a b = case a of
     18   []     -> ([],False)
     19   (x:xs) -> 

it looks like the name enc[]Cons is produced by Co4.CodeGen.Names

encodedConsName :: Namelike a => a -> a
encodedConsName = mapName (\(n:ns) -> "enc" ++ (toUpper n : ns) ++ "Cons") 

this is called in CO4.CodeGen

instance (MonadUnique u,MonadConfig u) => MonadCollector (AdtInstantiator u) where
  collectAdt adt = do
    is OnlyAllocators >>= \case
      False -> do
        zipWithM_ mkEncodedConstructor [0..] $ adtConstructors adt
...

    where 
      mkEncodedConstructor i (CCon name args) = do
        paramNames <- forM args $ const $ newName ""

        let exp = appsE (TH.VarE 'encodedConstructor) 
                      [ intE i
                      , intE $ length $ adtConstructors adt
                      , TH.ListE $ map varE paramNames ]
        tellOne $ valD' (encodedConsName name) 
                $ if null args 
                  then exp
                  else lamE' paramNames exp

Indeed, when we dump intermediate code (put $( compileFile [Dump "b.dump", Cache,ImportPrelude] ... in test/CO4/Example/Binary.hs) there are two lines

     27 enc[]Cons = encodedConstructor 0 2 []
     28 enc:Cons = \_3242 _3243 -> encodedConstructor 1 2 [_3242, _3243]

so it's names of Nil and Cons. In CO4.Names, we have

nilName :: Namelike n => n
nilName = convertName $ TH.nameBase '[]

consName :: Namelike n => n
consName = convertName $ TH.nameBase '(:)
...

toValidDataIdentifier :: (Eq n, Namelike n) => n -> n
toValidDataIdentifier name = case name of
  n | n == nilName         -> readName "Nil"
  n | n == consName        -> readName "Cons"

so the error seems to be that toValidDataIdentifier is not called.