Haskell/moduły

Z Wikibooks, biblioteki wolnych podręczników.

Definicje[edytuj]

  • moduł jest to plik zawierający kilka funkcji. Nazwa modułu odpowiada nazwie pliku, z wyjątkiem modułu głównego
  • biblioteka ( ang. library ) jest to zestaw kilku modułów[1]
  • biblioteka jest dostarczana w postaci pakietu ( ang. package) . Pakietów szukamy na Hackage
  • program

moduł[edytuj]

"A module defines a collection of values, datatypes, type synonyms, classes, etc., in an environment created by a set of imports (resources brought into scope from other modules). It exports some of these resources, making them available to other modules. We use the term entity to refer to a value, type, or class defined in, imported into, or perhaps exported from a module." haskell 2010 online report

program[edytuj]

"A Haskell program is a collection of modules, one of which, by convention, must be called Main and must export the value main. The value of the program is the value of the identifier main in module Main, which must be a computation of type IO τ for some type τ. When the program is executed, the computation main is performed, and its result (of type τ) is discarded."

Podział[edytuj]

Moduły:

  • standardowe ( są dostarczane z kompilatorem)
  • niestandardowe

Co trzeba zrobić aby użyć niestandardowego modułu?

  • pobrać
  • zainstalować
  • zaimportować

Instalowanie[edytuj]

 cabal update 
 cabal install <package_name>

Szukamu modułu[edytuj]

Za pomocą

 ghc-pkg find-module NazwaModułu


lub :

 ghc-pkg list nazwa

Ścieżki[edytuj]

Dla ghci użyj

 :show paths
ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> :show path
syntax:  :show [ args | prog | prompt | prompt2 | editor | stop | modules
               | bindings | breaks | context | packages | language ]
Prelude> :show paths
current working directory: 
  /home/a/haskell/romera03
module import search paths:
  .

Dla ghc

  ghc --print-libdir

Zawartość modułu[edytuj]

Sprawdzmy listę funkcji w module :

 :browse nazwa_modułu

Lista[edytuj]

A graph containing all installed Haskell packages

zaimportowane[edytuj]

:show imports

przykładowy wynik:

 import Prelude -- implicit


:show modules

zainstalowane[edytuj]

Lista zainstalowanych modułów:[2]

 ghc-pkg list

Przykładowy wynik: lista z podziałem na lokalizacje:


/usr/lib/ghc/package.conf.d
   Cabal-1.22.5.0
   array-0.5.1.0
   base-4.8.2.0
   bin-package-db-0.0.0.0
   binary-0.7.5.0
   bytestring-0.10.6.0
   containers-0.5.6.2
   deepseq-1.4.1.1
   directory-1.2.2.0
   filepath-1.4.0.0
   ghc-7.10.3
   ghc-prim-0.4.0.0
   haskeline-0.7.2.1
   hoopl-3.10.0.2
   hpc-0.6.0.2
   integer-gmp-1.0.0.0
   pretty-1.1.2.0
   process-1.2.3.0
   rts-1.0
   template-haskell-2.10.0.0
   terminfo-0.4.0.1
   time-1.5.0.1
   transformers-0.4.2.0
   unix-2.7.1.0
   xhtml-3000.2.1
/home/a/.ghc/x86_64-linux-7.10.3/package.conf.d
   Cabal-1.24.2.0
   FontyFruity-0.5.3.2
   JuicyPixels-3.2.8
   MonadRandom-0.4.2.3
   QuickCheck-2.10.0.1
   StateVar-1.1.0.4
   active-0.2.0.12
   adjunctions-4.3
   ansi-terminal-0.6.2.3
   ansi-wl-pprint-0.6.7.3
   async-2.1.1
   base-orphans-0.5.4
   base64-bytestring-1.0.0.1
   bifunctors-5.4.1
   blaze-builder-0.4.0.2
   bytes-0.15.2
   cairo-0.13.3.1
   cereal-0.5.4.0
   circle-packing-0.1.0.5
   colour-2.3.3
   comonad-5
   contravariant-1.4
   cubicbezier-0.5.0.0
   data-default-0.7.1.1
   data-default-class-0.1.2.0
   data-default-instances-containers-0.0.1
   data-default-instances-dlist-0.0.1
   data-default-instances-old-locale-0.0.1
   diagrams-1.4
   diagrams-cairo-1.4
   diagrams-contrib-1.4.0.1
   diagrams-core-1.4
   diagrams-lib-1.4.0.1
   diagrams-solve-0.1.0.1
   diagrams-svg-1.4.1
   distributive-0.5.0.2
   dlist-0.8.0.2
   dual-tree-0.2.0.9
   exceptions-0.8.3
   fail-4.9.0.0
   file-embed-0.0.10
   fingertree-0.1.1.0
   force-layout-0.4.0.6
   free-4.12.4
   fsnotify-0.2.1
   generic-deriving-1.11.1
   glib-0.13.4.1
   groups-0.4.0.0
   gtk2hs-buildtools-0.13.2.2
   hashable-1.2.4.0
   hashtables-1.2.1.1
   hinotify-0.3.9
   integration-0.2.1
   intervals-0.7.2
   kan-extensions-5.0.1
   lens-4.15.1
   linear-1.20.5
   matrices-0.4.4
   mfsolve-0.3.2.0
   microlens-0.4.7.0
   microlens-mtl-0.1.10.0
   microlens-th-0.4.1.0
   monoid-extras-0.4.2
   mtl-2.2.1
   mtl-compat-0.2.1.3
   mwc-random-0.13.5.0
   newtype-0.2
   old-locale-1.0.0.7
   optparse-applicative-0.13.0.0
   pango-0.13.3.1
   parallel-3.2.1.0
   parsec-3.1.11
   prelude-extras-0.4.0.3
   primitive-0.6.1.0
   profunctors-5.2
   random-1.1
   reflection-2.1.2
   scientific-0.3.4.9
   semigroupoids-5.1
   semigroups-0.18.2
   split-0.2.3.1
   statestack-0.2.0.5
   stm-2.4.4.1
   svg-builder-0.1.0.2
   tagged-0.8.5
   text-1.2.2.1
   tf-random-0.5
   transformers-compat-0.5.1.4
   unix-compat-0.4.3.1
   unordered-containers-0.2.7.2
   utf8-string-1.0.1.1
   vector-0.11.0.0
   vector-algorithms-0.7.0.1
   void-0.7.1
   xml-1.3.14
   zlib-0.6.1.2

lub diagram:

 ghc-pkg dot | tred | dot -Tsvg >pkgs.svg

Importowanie[edytuj]

Przykłady. W Kolumnie :[3]

  • pierwszej jest deklaracja importu
  • drugiej dostępne nazwy
 import A                           x, y, A.x, A.y
 import A()	                    (nothing)
 import A(x)	                    x, A.x
 import qualified A	            A.x, A.y
 import qualified A()	            (nothing)
 import qualified A(x)	            A.x
 import A hiding ()	            x, y, A.x, A.y
 import A hiding (x)	            y, A.y
 import qualified A hiding ()	    A.x, A.y
 import qualified A hiding (x)	    A.y
 import A as B	                    x, y, B.x, B.y
 import A as B(x)	            x, B.x
 import qualified A as B	    B.x, B.y


Ścieżka / hierarchia[edytuj]

 import D.E.M

oznacza że w  ścieżce wyszukiwania jest :[4]

  • katalog D, w nim podkatalog E
  • w katalogu E jest plik M.hs

Inna nazwa to hierarchia modułów


 import Cards.Cards as Cards

Oznacza że w katalogu Cards jest plik Cards.hs który immportujemy jako moduł Cards[5]

Moduły standardowe[edytuj]


Źródła[edytuj]

  1. stackoverflow question: whats-the-difference-between-module-package-and-library-in-haskell
  2. stackoverflow question: haskell-cabal-i-just-installed-packages-but-now-the-packages-are-not-found?rq=1
  3. haskell online report: modules
  4. ghc 6.2 docs : separate-compilation
  5. Hierarchical_Imports w ang wikibooks
  6. Moduł Prelude