recursivelyImportExternalClass :: L2.L2Model -> L2.Identifier -> L2.L2ClassID -> ModelTranslation L2.L2ClassID
recursivelyImportExternalClass =
  cacheWrapExternalImport mtsForeignToLocalClass (\x m -> m{mtsForeignToLocalClass=x}) $
    \foreignMod foreignURL foreignClassID -> do
      newClassID <- L2.l2NextClassID <$> getL2Model
      let Just (L2.L2ClassContents ss p df vals) = M.lookup foreignClassID . L2.l2AllClasses $ foreignMod
      localClassContents <- L2.L2ClassContents ss 
                              <$> (mapM (\(sdid, k) ->
                                          (,) <$>
                                          recursivelyImportExternalScopedDomain foreignMod foreignURL sdid 
                                          <*> pure k) p)
                              <*> T.mapM (\dfid ->
                                           recursivelyImportExternalDomainFunction foreignMod
                                                                                   foreignURL dfid) df
                              <*> T.mapM (\valueId ->
                                           recursivelyImportExternalClassValue foreignMod foreignURL valueId) vals
      modifyL2Model $ \mod -> mod {
        L2.l2NextClassID = (\(L2.L2ClassID i) -> L2.L2ClassID (i + 1)) newClassID,
        L2.l2AllClasses = M.insert newClassID localClassContents
                            (L2.l2AllClasses mod)
        }
      return newClassID
recursivelyImportExternalClass :: L2.L2Model -> L2.Identifier -> L2.L2ClassID -> ModelTranslation L2.L2ClassID
recursivelyImportExternalClass =
  cacheWrapExternalImport mtsForeignToLocalClass (\x m -> m{mtsForeignToLocalClass=x}) $
    \foreignMod foreignURL foreignClassID -> do
      newClassID <- L2.l2NextClassID <$> getL2Model
      let Just (L2.L2ClassContents ss p df vals) = M.lookup foreignClassID . L2.l2AllClasses $ foreignMod
      localClassContents <- L2.L2ClassContents ss 
                              <$> (mapM (\(sdid, k) ->
                                          (,) <$>
                                          recursivelyImportExternalScopedDomain foreignMod foreignURL sdid 
                                          <*> pure k) p)
                              <*> T.mapM (\dfid ->
                                           recursivelyImportExternalDomainFunction foreignMod
                                                                                   foreignURL dfid) df
                              <*> T.mapM (\valueId ->
                                           recursivelyImportExternalClassValue foreignMod foreignURL valueId) vals
      modifyL2Model $ \mod -> mod {
        L2.l2NextClassID = (\(L2.L2ClassID i) -> L2.L2ClassID (i + 1)) newClassID,
        L2.l2AllClasses = M.insert newClassID localClassContents
                            (L2.l2AllClasses mod)
        }
      return newClassID
recursivelyImportExternalClass =
  cacheWrapExternalImport mtsForeignToLocalClass (\x m -> m{mtsForeignToLocalClass=x}) $
    \foreignMod foreignURL foreignClassID -> do
      newClassID <- L2.l2NextClassID <$> getL2Model
      let Just (L2.L2ClassContents ss p df vals) = M.lookup foreignClassID . L2.l2AllClasses $ foreignMod
      localClassContents <- L2.L2ClassContents ss 
                              <$> (mapM (\(sdid, k) ->
                                          (,) <$>
                                          recursivelyImportExternalScopedDomain foreignMod foreignURL sdid 
                                          <*> pure k) p)
                              <*> T.mapM (\dfid ->
                                           recursivelyImportExternalDomainFunction foreignMod
                                                                                   foreignURL dfid) df
                              <*> T.mapM (\valueId ->
                                           recursivelyImportExternalClassValue foreignMod foreignURL valueId) vals
      modifyL2Model $ \mod -> mod {
        L2.l2NextClassID = (\(L2.L2ClassID i) -> L2.L2ClassID (i + 1)) newClassID,
        L2.l2AllClasses = M.insert newClassID localClassContents
                            (L2.l2AllClasses mod)
        }
      return newClassID