Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not import modules in Haskell

Hello i am trying to use Haskell with vscode and if i try to use a module inside another i get the following error:

Failed to load interface for **Client**
Use -v to see a list of the files searched for.

First Module:

module Main where 

import qualified Client as Cl

main ::IO ()
main = do
name<- getLine
sp

Second module:

    module Client 
    (sp)where

    sp::IO()
    sp=print "SecondModule"

After some reading i tried updating stack and installing the build tools at which point i get the following error:

   $ stack install gtk2hs-buildtools
    ←[0mprimitive-0.6.3.0: using precompiled package←[0m
    ←[0mCabal-2.0.1.1: using precompiled package←[0m
    ←[0mmtl-2.2.1: using precompiled package←[0m
    ←[0mrandom-1.1: using precompiled package←[0m
    ←[0mtext-1.2.2.2: using precompiled package←[0m
    ←[0mvector-0.12.0.1: using precompiled package←[0m
    ←[0mtf-random-0.5: configure←[0m
    ←[0mhashable-1.2.6.1: using precompiled package←[0m
    ←[0mhappy-1.19.9: download←[0m
    ←[0mtf-random-0.5: build←[0m
    ←[0mhashtables-1.2.2.1: download←[0m
    ←[0mhashtables-1.2.2.1: configure←[0m
    ←[0mhashtables-1.2.2.1: build←[0m
    ←[0mhappy-1.19.9: configure←[0m
    ←[0mtf-random-0.5: copy/register←[0m
    ←[0mQuickCheck-2.10.1: download←[0m
    ←[0mhappy-1.19.9: build←[0m
    ←[0mQuickCheck-2.10.1: configure←[0m
    ←[0mQuickCheck-2.10.1: build←[0m
    ←[0mhashtables-1.2.2.1: copy/register←[0m
    ←[0mQuickCheck-2.10.1: copy/register←[0m
    ←[0malex-3.2.3: download←[0m
    ←[0malex-3.2.3: configure←[0m
    ←[0malex-3.2.3: build←[0m
    ←[0mhappy-1.19.9: copy/register←[0m
    ←[0malex-3.2.3: copy/register←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: download←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: configure←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: build←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: copy/register←[0m
    Completed 13 action(s).
    removeDirectoryRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:DeleteFile "C:\\Users\\aita\\AppData\\Local\\Temp\\stack7744\\gtk2hs-buildtools-0.13.3.1\\.stack-work\\dist\\5c8418a7\\build\\gtk2hsC2hs\\gtk2hsC2hs.exe": permission denied (The process cannot access the file because it is being used by another process.)

I was careful not to have any other process use this folder but i don't get why the error.

Here is my stack.yaml file:

# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
# resolver: ghcjs-0.1.0_ghc-7.10.2
# resolver:
#  name: custom-snapshot
#  location: "./custom-snapshot.yaml"
resolver: lts-10.7

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# - location:
#    git: https://github.com/commercialhaskell/stack.git
#    commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#   extra-dep: true
#  subdirs:
#  - auto-update
#  - wai
#
# A package marked 'extra-dep: true' will only be built if demanded by a
# non-dependency (i.e. a user package), and its test suites and benchmarks
# will not be run. This is useful for tweaking upstream packages.
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=1.6"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
Main
Client

Cabal File.

name:                Hworld
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/Hworld#readme
license:             BSD3
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2018 Author name here
category:            Web
build-type:          Simple
cabal-version:       >=1.10
extra-source-files:  README.md

executable Hworld
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5
like image 293
Bercovici Adrian Avatar asked Dec 23 '22 09:12

Bercovici Adrian


1 Answers

First, stack install is a bit of a misnomer. Unlike npm install or bower install, it doesn't download anything or "add" anything to your project. Under stack, all packages are (potentially, logically) downloaded and installed all the time. stack will build a package on first use, but there is no need to explicitly ask stack to download it.

What stack install actually does, it builds the package and, if the package contains any executables, it puts them in a special place on the file system that is on your PATH, so that now you can run those executables. Seeing how you can always run executables with stack exec package without installing them, using stack install is generally discouraged unless you're really sure it's necessary.

To add a package to your project, you need to include it in your .cabal file in build-depends section or in your package.yaml file in the dependencies section (whichever one you're using). Here is an example of a package.yaml, and here's an example of a .cabal file.

But what you're trying to do, it seems, is to reference your own modules, not external dependencies. For that, stack install is even further from the truth.

To reference your own modules, you need two things: (1) correct file system structure and (2) add them to .cabal or package.yaml.

File system structure: in Haskell (or, more precisely, in GHC), modules must be located in a place on the file system that matches their hierarchical name. For example, module Main would be located in ./Main.hs, and module Client would be located in ./Client.hs, or, say, module Client.Internals would be located in ./Client/Internals.hs. A bit annoying, yes. But this is the way Haskell is.

Cabal file: all modules in a package/library must be listed in the .cabal file, either under exposed-modules or under other-modules. Just add them there, and the compiler will see them.

like image 182
Fyodor Soikin Avatar answered Jan 03 '23 08:01

Fyodor Soikin