From hjgtuyl at chello.nl Fri Mar 1 18:02:42 2019 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Fri, 01 Mar 2019 19:02:42 +0100 Subject: [Haskell-beginners] Error Message: Can not find C library. How can I do this? In-Reply-To: References: Message-ID: Did you try setting environment variable LIBRARY_PATH? Something like: Set LIBRARY_PATH=%LIBRARY_PATH%;C:\wxWidgets\lib You might need to set C_INCLUDE_PATH as well, for the header files. Regards, Henk-Jan van Tuyl On Tue, 26 Feb 2019 07:47:59 +0100, S. H. Aegis wrote: > Hello. > > I’m new to stack & FFI. > > I created a dll file by http://www.mingw.org/wiki/sampleDLL > [...] > *Configuring test2-0.1.0.0...* > *Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe: Missing dependency on a > foreign* > *library:* > ** Missing (or bad) C library: example_dll.a* [...] > How can I fix this? I did a lot of things on the webpages, but I could > not. > > Please, help me. > > > > Sincerely, S. Chang. -- Message from Stanford University: Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://foldingathome.stanford.edu/ -- http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From edgar.klerks at gmail.com Fri Mar 1 19:42:01 2019 From: edgar.klerks at gmail.com (Edgar Klerks) Date: Fri, 1 Mar 2019 20:42:01 +0100 Subject: [Haskell-beginners] Error Message: Can not find C library. How can I do this? In-Reply-To: References: Message-ID: You made a small typo: example_dll.cpp, example_dll.dll, example_dll.h, example_dll.o, example_exe.cpp, *libexample_dll.a* And in your code, you call it example_dll.a instead of libexample_dll.a: > > extra-libraries: > - example_dll.a > That should probably be: extra-libraries: - libexample_dll.a On Tue, Feb 26, 2019 at 7:48 AM S. H. Aegis wrote: > Hello. > > I’m new to stack & FFI. > > I created a dll file by http://www.mingw.org/wiki/sampleDLL > > And I put all files into dll folder. (Those files are example_dll.cpp, > example_dll.dll, example_dll.h, example_dll.o, example_exe.cpp, > libexample_dll.a) I test example_exe.exe file, and confirmed that works. > > (Windows7 Pro, stack 1.9.3) > > > > executables: > > test2-exe: > > main: Main.hs > > source-dirs: app > > ghc-options: > > - -threaded > > - -rtsopts > > - -with-rtsopts=-N > > dependencies: > > - test2 > > extra-libraries: > > - example_dll.a > > extra-lib-dirs: > > - dll > > > > # Extra directories used by stack for building > > extra-include-dirs: > > - dll > > extra-lib-dirs: > > - dll > > > > {-# LANGUAGE ForeignFunctionInterface #-} > > > > module Main where > > > > -- import Lib > > import Foreign > > > > foreign import ccall "example_dll.h hello" > > c_hello :: IO() > > > > main :: IO () > > main = c_hello > > > > #include > > #include "example_dll.h" > > > > __stdcall void hello(const char *s) > > { > > printf("Hello %s\n", s); > > } > > int Double(int x) > > { > > return 2 * x; > > } > > void CppFunc(void) > > { > > puts("CppFunc"); > > } > > void MyClass::func(void) > > { > > puts("MyClass.func()"); > > } > > > > When I try to stack build, I get error messages. > > > > *C:\Users\shaegis\Documents\Haskell\test2\dll>stack build* > > *Building all executables for `test2' once. After a successful build of > all of them, only specified executables will be rebuilt.* > > *test2-0.1.0.0: configure (lib + exe)* > > *Configuring test2-0.1.0.0...* > > *Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe: Missing dependency on a > foreign* > > *library:* > > ** Missing (or bad) C library: example_dll.a* > > *This problem can usually be solved by installing the system package that* > > *provides this library (you may need the "-dev" version). If the library > is* > > *already installed but in a non-standard location then you can use the > flags* > > *--extra-include-dirs= and --extra-lib-dirs= to specify where it is.If the* > > *library file does exist, it may contain errors that are caught by the C* > > *compiler at the preprocessing stage. In this case you can re-run > configure* > > *with the verbosity flag -v3 to see the error messages.* > > > > *-- While building package test2-0.1.0.0 using:* > > * > C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe > --builddir=.stack-work\dist\e626a42b configure > --with-ghc=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\ghc-8.6.3\bin\ghc.EXE > --with-ghc-pkg=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\ghc-8.6.3\bin\ghc-pkg.EXE > --user --package-db=clear --package-db=global > --package-db=C:\sr\snapshots\3233b5e2\pkgdb > --package-db=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\pkgdb > --libdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\lib > --bindir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\bin > --datadir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\share > --libexecdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\libexec > --sysconfdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\etc > --docdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 > --htmldir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 > --haddockdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 > --dependency=base=base-4.12.0.0 > --extra-include-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\include > --extra-include-dirs=C:\Users\shaegis\Documents\Haskell\test2\dll > --extra-lib-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\bin > --extra-lib-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\lib > --extra-lib-dirs=C:\Users\shaegis\Documents\Haskell\test2\dll > --enable-tests --enable-benchmarks* > > *Process exited with code: ExitFailure 1* > > > > How can I fix this? I did a lot of things on the webpages, but I could not. > > Please, help me. > > > > Sincerely, S. Chang. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sat Mar 2 02:35:47 2019 From: shaegis at gmail.com (S. H. Aegis) Date: Sat, 2 Mar 2019 11:35:47 +0900 Subject: [Haskell-beginners] Error Message: Can not find C library. How can I do this? In-Reply-To: References: Message-ID: Thank you to your kind reply. =) I tried several times, but still got a same error message. (ㅠㅠ) But I'm going to googling with key-word that you told me. Thank you again. Best reguard S. Chang. 2019년 3월 2일 (토) 오전 3:03, Henk-Jan van Tuyl 님이 작성: > > Did you try setting environment variable LIBRARY_PATH? > Something like: > Set LIBRARY_PATH=%LIBRARY_PATH%;C:\wxWidgets\lib > You might need to set C_INCLUDE_PATH as well, for the header files. > > Regards, > Henk-Jan van Tuyl > > > > On Tue, 26 Feb 2019 07:47:59 +0100, S. H. Aegis wrote: > > > Hello. > > > > I’m new to stack & FFI. > > > > I created a dll file by http://www.mingw.org/wiki/sampleDLL > > > [...] > > *Configuring test2-0.1.0.0...* > > *Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe: Missing dependency on a > > foreign* > > *library:* > > ** Missing (or bad) C library: example_dll.a* > [...] > > How can I fix this? I did a lot of things on the webpages, but I could > > not. > > > > Please, help me. > > > > > > > > Sincerely, S. Chang. > > > -- > Message from Stanford University: > > Folding at home > > What if you could share your unused computer power to help find a cure? In > just 5 minutes you can join the world's biggest networked computer and get > us closer sooner. Watch the video. > http://foldingathome.stanford.edu/ > > -- > http://members.chello.nl/hjgtuyl/tourdemonad.html > Haskell programming > -- > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- Sok Ha, CHANG Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul. Tel: +82-2-442-7585 -------------- next part -------------- An HTML attachment was scrubbed... URL: From shaegis at gmail.com Sat Mar 2 02:37:12 2019 From: shaegis at gmail.com (S. H. Aegis) Date: Sat, 2 Mar 2019 11:37:12 +0900 Subject: [Haskell-beginners] Error Message: Can not find C library. How can I do this? In-Reply-To: References: Message-ID: Thank you to your kind reply. =) Unfortunately, I've got same error message. Thank you again. Best reguard S. Chang. 2019년 3월 2일 (토) 오전 4:42, Edgar Klerks 님이 작성: > You made a small typo: > > example_dll.cpp, example_dll.dll, example_dll.h, example_dll.o, > example_exe.cpp, *libexample_dll.a* > > And in your code, you call it example_dll.a instead of libexample_dll.a: > >> >> extra-libraries: >> - example_dll.a >> > > That should probably be: > > extra-libraries: > - libexample_dll.a > > > On Tue, Feb 26, 2019 at 7:48 AM S. H. Aegis wrote: > >> Hello. >> >> I’m new to stack & FFI. >> >> I created a dll file by http://www.mingw.org/wiki/sampleDLL >> >> And I put all files into dll folder. (Those files are example_dll.cpp, >> example_dll.dll, example_dll.h, example_dll.o, example_exe.cpp, >> libexample_dll.a) I test example_exe.exe file, and confirmed that works. >> >> (Windows7 Pro, stack 1.9.3) >> >> >> >> executables: >> >> test2-exe: >> >> main: Main.hs >> >> source-dirs: app >> >> ghc-options: >> >> - -threaded >> >> - -rtsopts >> >> - -with-rtsopts=-N >> >> dependencies: >> >> - test2 >> >> extra-libraries: >> >> - example_dll.a >> >> extra-lib-dirs: >> >> - dll >> >> >> >> # Extra directories used by stack for building >> >> extra-include-dirs: >> >> - dll >> >> extra-lib-dirs: >> >> - dll >> >> >> >> {-# LANGUAGE ForeignFunctionInterface #-} >> >> >> >> module Main where >> >> >> >> -- import Lib >> >> import Foreign >> >> >> >> foreign import ccall "example_dll.h hello" >> >> c_hello :: IO() >> >> >> >> main :: IO () >> >> main = c_hello >> >> >> >> #include >> >> #include "example_dll.h" >> >> >> >> __stdcall void hello(const char *s) >> >> { >> >> printf("Hello %s\n", s); >> >> } >> >> int Double(int x) >> >> { >> >> return 2 * x; >> >> } >> >> void CppFunc(void) >> >> { >> >> puts("CppFunc"); >> >> } >> >> void MyClass::func(void) >> >> { >> >> puts("MyClass.func()"); >> >> } >> >> >> >> When I try to stack build, I get error messages. >> >> >> >> *C:\Users\shaegis\Documents\Haskell\test2\dll>stack build* >> >> *Building all executables for `test2' once. After a successful build of >> all of them, only specified executables will be rebuilt.* >> >> *test2-0.1.0.0: configure (lib + exe)* >> >> *Configuring test2-0.1.0.0...* >> >> *Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe: Missing dependency on a >> foreign* >> >> *library:* >> >> ** Missing (or bad) C library: example_dll.a* >> >> *This problem can usually be solved by installing the system package that* >> >> *provides this library (you may need the "-dev" version). If the library >> is* >> >> *already installed but in a non-standard location then you can use the >> flags* >> >> *--extra-include-dirs= and --extra-lib-dirs= to specify where it is.If >> the* >> >> *library file does exist, it may contain errors that are caught by the C* >> >> *compiler at the preprocessing stage. In this case you can re-run >> configure* >> >> *with the verbosity flag -v3 to see the error messages.* >> >> >> >> *-- While building package test2-0.1.0.0 using:* >> >> * >> C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.4.0.1_ghc-8.6.3.exe >> --builddir=.stack-work\dist\e626a42b configure >> --with-ghc=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\ghc-8.6.3\bin\ghc.EXE >> --with-ghc-pkg=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\ghc-8.6.3\bin\ghc-pkg.EXE >> --user --package-db=clear --package-db=global >> --package-db=C:\sr\snapshots\3233b5e2\pkgdb >> --package-db=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\pkgdb >> --libdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\lib >> --bindir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\bin >> --datadir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\share >> --libexecdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\libexec >> --sysconfdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\etc >> --docdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 >> --htmldir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 >> --haddockdir=C:\Users\shaegis\Documents\Haskell\test2\.stack-work\install\784ab3f0\doc\test2-0.1.0.0 >> --dependency=base=base-4.12.0.0 >> --extra-include-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\include >> --extra-include-dirs=C:\Users\shaegis\Documents\Haskell\test2\dll >> --extra-lib-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\bin >> --extra-lib-dirs=C:\Users\shaegis\AppData\Local\Programs\stack\x86_64-windows\msys2-20180531\mingw64\lib >> --extra-lib-dirs=C:\Users\shaegis\Documents\Haskell\test2\dll >> --enable-tests --enable-benchmarks* >> >> *Process exited with code: ExitFailure 1* >> >> >> >> How can I fix this? I did a lot of things on the webpages, but I could >> not. >> >> Please, help me. >> >> >> >> Sincerely, S. Chang. >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- Sok Ha, CHANG Dr. Chang's Clinic. #203. 503-23. AmSa-Dong, GangDong-Gu, Seoul. Tel: +82-2-442-7585 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Thu Mar 14 11:10:06 2019 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Thu, 14 Mar 2019 11:10:06 +0000 Subject: [Haskell-beginners] Matrix and types Message-ID: Hi, As an exercise I want to write a Matrix library. Multiplication of two matrices is only defined when the the number of columns in the first matrix equals the number of rows in the second matrix. i.e. c1 == r2 So when writing the multiplication function I can check that c1 == r2 and do something. However what I really want to do, if possible, is to have the compiler catch the error. I’d appreciate any advice on how to approach this. I don’t want a full description of exactly what to do as that way I won’t have struggled or argued with the compiler - which for me is the best way to learn Haskell :) Thanks Mike -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Thu Mar 14 11:37:20 2019 From: fa-ml at ariis.it (Francesco Ariis) Date: Thu, 14 Mar 2019 12:37:20 +0100 Subject: [Haskell-beginners] Matrix and types In-Reply-To: References: Message-ID: <20190314113720.xim35mdypfxxqpol@x60s.casa> Hello Mike, On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: > Multiplication of two matrices is only defined when the the number of columns in the first matrix > equals the number of rows in the second matrix. i.e. c1 == r2 > > So when writing the multiplication function I can check that c1 == r2 and do something. > However what I really want to do, if possible, is to have the compiler catch the error. Type-level literals [1] or any kind of similar trickery should help you with having matrices checked at compile-time. [1] https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html From frederic.cogny at gmail.com Thu Mar 14 13:12:46 2019 From: frederic.cogny at gmail.com (Frederic Cogny) Date: Thu, 14 Mar 2019 14:12:46 +0100 Subject: [Haskell-beginners] Matrix and types In-Reply-To: <20190314113720.xim35mdypfxxqpol@x60s.casa> References: <20190314113720.xim35mdypfxxqpol@x60s.casa> Message-ID: The (experimental) Static module of hmatrix seems (I've used the packaged but not that module) to do exactly that: http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis wrote: > Hello Mike, > > On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: > > Multiplication of two matrices is only defined when the the number of > columns in the first matrix > > equals the number of rows in the second matrix. i.e. c1 == r2 > > > > So when writing the multiplication function I can check that c1 == r2 > and do something. > > However what I really want to do, if possible, is to have the compiler > catch the error. > > Type-level literals [1] or any kind of similar trickery should help you > with having matrices checked at compile-time. > > [1] > https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- Frederic Cogny +33 7 83 12 61 69 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Thu Mar 14 15:19:42 2019 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Thu, 14 Mar 2019 15:19:42 +0000 Subject: [Haskell-beginners] Matrix and types In-Reply-To: References: <20190314113720.xim35mdypfxxqpol@x60s.casa> Message-ID: Hi, Thanks for the pointers. So I’ve got data M (n :: Nat) a = M [a] deriving Show t2 :: M 2 Int t2 = M [1,2] t3 :: M 3 Int t3 = M [1,2,3] fx :: Num a => M n a -> M n a -> M n a fx (M xs) (M ys) = M (zipWith (+) xs ys) and having g = fx t2 t3 won’t compile. Which is what I want. However… t2 :: M 2 Int t2 = M [1,2] is ‘hardwired’ to 2 and clearly I could make t2 return a list of any length. So what I then tried to look at was a general function that would take a list of Int and create the M type using the length of the supplied list. In other words if I supply a list, xs, of length n then I wan’t M n xs Like this createIntM xs = (M xs) :: M (length xs) Int which compile and has type λ-> :t createIntM createIntM :: [Int] -> M (length xs) Int and all Ms created using createIntM have the same type irrespective of the length of the supplied list. What’s the type jiggery I need or is this not the right way to go? Thanks Mike > On 14 Mar 2019, at 13:12, Frederic Cogny wrote: > > The (experimental) Static module of hmatrix seems (I've used the packaged but not that module) to do exactly that: http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html > > > > On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis > wrote: > Hello Mike, > > On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: > > Multiplication of two matrices is only defined when the the number of columns in the first matrix > > equals the number of rows in the second matrix. i.e. c1 == r2 > > > > So when writing the multiplication function I can check that c1 == r2 and do something. > > However what I really want to do, if possible, is to have the compiler catch the error. > > Type-level literals [1] or any kind of similar trickery should help you > with having matrices checked at compile-time. > > [1] https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- > Frederic Cogny > +33 7 83 12 61 69 > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From frederic.cogny at gmail.com Fri Mar 15 11:37:34 2019 From: frederic.cogny at gmail.com (Frederic Cogny) Date: Fri, 15 Mar 2019 12:37:34 +0100 Subject: [Haskell-beginners] Matrix and types In-Reply-To: References: <20190314113720.xim35mdypfxxqpol@x60s.casa> Message-ID: I'm not sure I understand your question Mike. Are you saying createIntM behaves as desired but the data constructor M could let you build a data M with the wrong type. for instance M [1,2] :: M 1 Int ? If that is your question, then one way to handle this is to have a separate module where you define the data type and the proper constructor (here M and createIntM) but where you do not expose the type constructor. so something like module MyModule ( M -- as opposed to M(..) to not expose the type constructor , createIntM ) where Then, outside of MyModule, you can not create an incorrect lentgh annotated list since the only way to build it is through createIntM Does that make sense? On Thu, Mar 14, 2019 at 4:19 PM mike h wrote: > Hi, > Thanks for the pointers. So I’ve got > > data M (n :: Nat) a = M [a] deriving Show > > t2 :: M 2 Int > t2 = M [1,2] > > t3 :: M 3 Int > t3 = M [1,2,3] > > fx :: Num a => M n a -> M n a -> M n a > fx (M xs) (M ys) = M (zipWith (+) xs ys) > > and having > g = fx t2 t3 > > won’t compile. Which is what I want. > However… > > t2 :: M 2 Int > t2 = M [1,2] > > is ‘hardwired’ to 2 and clearly I could make t2 return a list of > any length. > So what I then tried to look at was a general function that would take a > list of Int and create the M type using the length of the supplied list. > In other words if I supply a list, xs, of length n then I wan’t M n xs > Like this > > createIntM xs = (M xs) :: M (length xs) Int > > which compile and has type > λ-> :t createIntM > createIntM :: [Int] -> M (length xs) Int > > and all Ms created using createIntM have the same type irrespective of > the length of the supplied list. > > What’s the type jiggery I need or is this not the right way to go? > > Thanks > > Mike > > > > > On 14 Mar 2019, at 13:12, Frederic Cogny wrote: > > The (experimental) Static module of hmatrix seems (I've used the packaged > but not that module) to do exactly that: > http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html > > > > On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis wrote: > >> Hello Mike, >> >> On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: >> > Multiplication of two matrices is only defined when the the number of >> columns in the first matrix >> > equals the number of rows in the second matrix. i.e. c1 == r2 >> > >> > So when writing the multiplication function I can check that c1 == r2 >> and do something. >> > However what I really want to do, if possible, is to have the compiler >> catch the error. >> >> Type-level literals [1] or any kind of similar trickery should help you >> with having matrices checked at compile-time. >> >> [1] >> https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > -- > Frederic Cogny > +33 7 83 12 61 69 > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > -- Frederic Cogny +33 7 83 12 61 69 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Fri Mar 15 12:57:13 2019 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Fri, 15 Mar 2019 12:57:13 +0000 Subject: [Haskell-beginners] Matrix and types In-Reply-To: References: <20190314113720.xim35mdypfxxqpol@x60s.casa> Message-ID: <542C66B8-2502-4A2D-92C2-7AC382FAAF98@yahoo.co.uk> Hi Frederic, Yeh, my explanation is a bit dubious :) What I’m trying to say is: Looking at the type M (n::Nat) If I want an M 2 of Ints say, then I need to write the function with signature f :: M 2 Int If I want a M 3 then I need to explicitly write the function with signature M 3 Int and so on for every possible instance that I might want. What I would like to do is have just one function that is somehow parameterised to create the M tagged with the required value of (n::Nat) In pseudo Haskell create :: Int -> [Int] -> M n create size ns = (M ns) :: M size Int where its trying to coerce (M ns) into the type (M size Int) where size is an Int but needs to be a Nat. It’s sort of like parameterising the signature of the function. Thanks Mike > On 15 Mar 2019, at 11:37, Frederic Cogny wrote: > > I'm not sure I understand your question Mike. > Are you saying createIntM behaves as desired but the data constructor M could let you build a data M with the wrong type. for instance M [1,2] :: M 1 Int ? > > If that is your question, then one way to handle this is to have a separate module where you define the data type and the proper constructor (here M and createIntM) but where you do not expose the type constructor. so something like > > module MyModule > ( M -- as opposed to M(..) to not expose the type constructor > , createIntM > ) where > > Then, outside of MyModule, you can not create an incorrect lentgh annotated list since the only way to build it is through createIntM > > Does that make sense? > > On Thu, Mar 14, 2019 at 4:19 PM mike h > wrote: > Hi, > Thanks for the pointers. So I’ve got > > data M (n :: Nat) a = M [a] deriving Show > > t2 :: M 2 Int > t2 = M [1,2] > > t3 :: M 3 Int > t3 = M [1,2,3] > > fx :: Num a => M n a -> M n a -> M n a > fx (M xs) (M ys) = M (zipWith (+) xs ys) > > and having > g = fx t2 t3 > > won’t compile. Which is what I want. > However… > > t2 :: M 2 Int > t2 = M [1,2] > > is ‘hardwired’ to 2 and clearly I could make t2 return a list of any length. > So what I then tried to look at was a general function that would take a list of Int and create the M type using the length of the supplied list. > In other words if I supply a list, xs, of length n then I wan’t M n xs > Like this > > createIntM xs = (M xs) :: M (length xs) Int > > which compile and has type > λ-> :t createIntM > createIntM :: [Int] -> M (length xs) Int > > and all Ms created using createIntM have the same type irrespective of the length of the supplied list. > > What’s the type jiggery I need or is this not the right way to go? > > Thanks > > Mike > > > > >> On 14 Mar 2019, at 13:12, Frederic Cogny > wrote: >> >> The (experimental) Static module of hmatrix seems (I've used the packaged but not that module) to do exactly that: http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html >> >> >> >> On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis > wrote: >> Hello Mike, >> >> On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: >> > Multiplication of two matrices is only defined when the the number of columns in the first matrix >> > equals the number of rows in the second matrix. i.e. c1 == r2 >> > >> > So when writing the multiplication function I can check that c1 == r2 and do something. >> > However what I really want to do, if possible, is to have the compiler catch the error. >> >> Type-level literals [1] or any kind of similar trickery should help you >> with having matrices checked at compile-time. >> >> [1] https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> -- >> Frederic Cogny >> +33 7 83 12 61 69 >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -- > Frederic Cogny > +33 7 83 12 61 69 -------------- next part -------------- An HTML attachment was scrubbed... URL: From sylvain at haskus.fr Fri Mar 15 13:48:06 2019 From: sylvain at haskus.fr (Sylvain Henry) Date: Fri, 15 Mar 2019 14:48:06 +0100 Subject: [Haskell-beginners] Matrix and types In-Reply-To: <542C66B8-2502-4A2D-92C2-7AC382FAAF98@yahoo.co.uk> References: <20190314113720.xim35mdypfxxqpol@x60s.casa> <542C66B8-2502-4A2D-92C2-7AC382FAAF98@yahoo.co.uk> Message-ID: Hi, The problem is that Haskell lists don't carry their length in their type hence you cannot enforce their length at compile time. But you can define your M this way instead: {-# LANGUAGE TypeOperators #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE KindSignatures #-} import GHC.TypeNats import Data.Proxy data M (n :: Nat) a where    MNil  :: M 0 a    MCons :: a -> M (n-1) a -> M n a infixr 5 `MCons` toList :: M k a -> [a] toList MNil         = [] toList (MCons a as) = a:toList as instance (KnownNat n, Show a) => Show (M n a) where    show xs = mconcat       [ "M @"       , show (natVal (Proxy :: Proxy n))       , " "       , show (toList xs)       ] --t2 :: M 2 Integer t2 = 1 `MCons` 2 `MCons` MNil --t3 :: M 3 Integer t3 = 1 `MCons` 2 `MCons` 3 `MCons` MNil zipM :: (a -> b -> c) -> M n a -> M n b -> M n c zipM _f MNil         MNil         = MNil zipM  f (MCons a as) (MCons b bs) = MCons (f a b) (zipM f as bs) fx :: Num a => M n a -> M n a -> M n a fx = zipM (+) Test: > t2 M @2 [1,2] > fx t2 t2 M @2 [2,4] > fx t2 t3 :38:7: error:     • Couldn't match type ‘3’ with ‘2’       Expected type: M 2 Integer         Actual type: M 3 Integer Cheers, Sylvain On 15/03/2019 13:57, mike h wrote: > > Hi Frederic, > > Yeh, my explanation is a bit dubious :) > What I’m trying to say is: > Looking at the type M (n::Nat) > If I want an M 2  of Ints say,  then I need to write the function with > signature > >  f :: M 2 Int > > If I want a M 3 then I need to explicitly write the function with > signature > M 3 Int > > and so on for every possible instance that I might want. > > What I would like to do is have just one function that is somehow > parameterised to create the M tagged with the required value of (n::Nat) > In pseudo Haskell > > create :: Int -> [Int] -> M n > create size ns = (M ns) ::  M size Int > > where  its trying to coerce (M ns) into the type (M size Int) where > size is an Int but needs to be a Nat. > > It’s sort of like parameterising the signature of the function. > > Thanks > > Mike > >> On 15 Mar 2019, at 11:37, Frederic Cogny > > wrote: >> >> I'm not sure I understand your question Mike. >> Are you saying createIntM behaves as desired but the data constructor >> M could let you build a data M with the wrong type. for instance M >> [1,2] :: M 1 Int ? >> >> If that is your question, then one way to handle this is to have a >> separate module where you define the data type and the proper >> constructor (here M and createIntM) but where you do not expose the >> type constructor. so something like >> >> module MyModule >>   ( M -- as opposed to M(..) to not expose the type constructor >> , createIntM >>   ) where >> >> Then, outside of MyModule, you can not create an incorrect lentgh >> annotated list since the only way to build it is through createIntM >> >> Does that make sense? >> >> On Thu, Mar 14, 2019 at 4:19 PM mike h > > wrote: >> >> Hi, >> Thanks for the pointers. So I’ve got >> >> data M (n :: Nat) a = M [a] deriving Show >> >> t2 :: M 2 Int >> t2  = M [1,2] >> >> t3 :: M 3 Int >> t3 = M [1,2,3] >> >> fx :: Num a => M n a -> M n a -> M n a >> fx (M xs) (M ys) = M (zipWith (+) xs ys) >> >> and having >> g = fx t2 t3 >> >> won’t compile. Which is what I want. >> However… >> >> t2::M 2 Int >> t2 =M [1,2] >> >> is ‘hardwired’ to 2 and clearly I could make t2 return  a list of >> any length. >> So what I then tried to look at was a general function that would >> take a list of Int and create the M type using the length of >> the supplied list. >> In other words if I supply a list, xs, of length n then I wan’t >>  M n xs >> Like this >> >> createIntM xs = (M xs) ::M (lengthxs) Int >> >> which compile and has type >> λ-> :t createIntM >> createIntM :: [Int] -> M (length xs) Int >> >> and all Ms created using createIntM  have the same type >> irrespective of the length of the supplied list. >> >> What’s the type jiggery I need or is this not the right way to go? >> >> Thanks >> >> Mike >> >> >> >> >>> On 14 Mar 2019, at 13:12, Frederic Cogny >>> > wrote: >>> >>> The (experimental) Static module of hmatrix seems (I've used the >>> packaged but not that module) to do exactly that: >>> http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html >>> >>> >>> >>> >>> On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis >> > wrote: >>> >>> Hello Mike, >>> >>> On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: >>> > Multiplication of two matrices is only defined when the >>> the number of columns in the first matrix >>> > equals the number of rows in the second matrix. i.e. c1 == r2 >>> > >>> > So when writing the multiplication function I can check >>> that  c1 == r2 and do something. >>> > However what I really want to do, if possible, is to have >>> the compiler catch the error. >>> >>> Type-level literals [1] or any kind of similar trickery >>> should help you >>> with having matrices checked at compile-time. >>> >>> [1] >>> https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >>> -- >>> Frederic Cogny >>> +33 7 83 12 61 69 >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> -- >> Frederic Cogny >> +33 7 83 12 61 69 > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From mike_k_houghton at yahoo.co.uk Fri Mar 15 20:44:46 2019 From: mike_k_houghton at yahoo.co.uk (mike h) Date: Fri, 15 Mar 2019 20:44:46 +0000 Subject: [Haskell-beginners] Matrix and types In-Reply-To: References: <20190314113720.xim35mdypfxxqpol@x60s.casa> <542C66B8-2502-4A2D-92C2-7AC382FAAF98@yahoo.co.uk> Message-ID: <0E90C918-DC68-4D92-AF80-44F09CFE1B7B@yahoo.co.uk> Thanks. > On 15 Mar 2019, at 13:48, Sylvain Henry wrote: > > Hi, > > The problem is that Haskell lists don't carry their length in their type hence you cannot enforce their length at compile time. But you can define your M this way instead: > > {-# LANGUAGE TypeOperators #-} > {-# LANGUAGE GADTs #-} > {-# LANGUAGE DataKinds #-} > {-# LANGUAGE ScopedTypeVariables #-} > {-# LANGUAGE KindSignatures #-} > > import GHC.TypeNats > import Data.Proxy > > data M (n :: Nat) a where > MNil :: M 0 a > MCons :: a -> M (n-1) a -> M n a > > infixr 5 `MCons` > > toList :: M k a -> [a] > toList MNil = [] > toList (MCons a as) = a:toList as > > instance (KnownNat n, Show a) => Show (M n a) where > show xs = mconcat > [ "M @" > , show (natVal (Proxy :: Proxy n)) > , " " > , show (toList xs) > ] > > --t2 :: M 2 Integer > t2 = 1 `MCons` 2 `MCons` MNil > > --t3 :: M 3 Integer > t3 = 1 `MCons` 2 `MCons` 3 `MCons` MNil > > zipM :: (a -> b -> c) -> M n a -> M n b -> M n c > zipM _f MNil MNil = MNil > zipM f (MCons a as) (MCons b bs) = MCons (f a b) (zipM f as bs) > > fx :: Num a => M n a -> M n a -> M n a > fx = zipM (+) > > > Test: > > > t2 > M @2 [1,2] > > fx t2 t2 > M @2 [2,4] > > fx t2 t3 > > :38:7: error: > • Couldn't match type ‘3’ with ‘2’ > Expected type: M 2 Integer > Actual type: M 3 Integer > > > > Cheers, > Sylvain > > > > On 15/03/2019 13:57, mike h wrote: >> >> Hi Frederic, >> >> Yeh, my explanation is a bit dubious :) >> What I’m trying to say is: >> Looking at the type M (n::Nat) >> If I want an M 2 of Ints say, then I need to write the function with signature >> >> f :: M 2 Int >> >> >> If I want a M 3 then I need to explicitly write the function with signature >> M 3 Int >> >> and so on for every possible instance that I might want. >> >> What I would like to do is have just one function that is somehow parameterised to create the M tagged with the required value of (n::Nat) >> In pseudo Haskell >> >> create :: Int -> [Int] -> M n >> create size ns = (M ns) :: M size Int >> >> where its trying to coerce (M ns) into the type (M size Int) where size is an Int but needs to be a Nat. >> >> It’s sort of like parameterising the signature of the function. >> >> Thanks >> >> Mike >> >>> On 15 Mar 2019, at 11:37, Frederic Cogny > wrote: >>> >>> I'm not sure I understand your question Mike. >>> Are you saying createIntM behaves as desired but the data constructor M could let you build a data M with the wrong type. for instance M [1,2] :: M 1 Int ? >>> >>> If that is your question, then one way to handle this is to have a separate module where you define the data type and the proper constructor (here M and createIntM) but where you do not expose the type constructor. so something like >>> >>> module MyModule >>> ( M -- as opposed to M(..) to not expose the type constructor >>> , createIntM >>> ) where >>> >>> Then, outside of MyModule, you can not create an incorrect lentgh annotated list since the only way to build it is through createIntM >>> >>> Does that make sense? >>> >>> On Thu, Mar 14, 2019 at 4:19 PM mike h > wrote: >>> Hi, >>> Thanks for the pointers. So I’ve got >>> >>> data M (n :: Nat) a = M [a] deriving Show >>> >>> t2 :: M 2 Int >>> t2 = M [1,2] >>> >>> t3 :: M 3 Int >>> t3 = M [1,2,3] >>> >>> fx :: Num a => M n a -> M n a -> M n a >>> fx (M xs) (M ys) = M (zipWith (+) xs ys) >>> >>> and having >>> g = fx t2 t3 >>> >>> won’t compile. Which is what I want. >>> However… >>> >>> t2 :: M 2 Int >>> t2 = M [1,2] >>> >>> is ‘hardwired’ to 2 and clearly I could make t2 return a list of any length. >>> So what I then tried to look at was a general function that would take a list of Int and create the M type using the length of the supplied list. >>> In other words if I supply a list, xs, of length n then I wan’t M n xs >>> Like this >>> >>> createIntM xs = (M xs) :: M (length xs) Int >>> >>> which compile and has type >>> λ-> :t createIntM >>> createIntM :: [Int] -> M (length xs) Int >>> >>> and all Ms created using createIntM have the same type irrespective of the length of the supplied list. >>> >>> What’s the type jiggery I need or is this not the right way to go? >>> >>> Thanks >>> >>> Mike >>> >>> >>> >>> >>>> On 14 Mar 2019, at 13:12, Frederic Cogny > wrote: >>>> >>>> The (experimental) Static module of hmatrix seems (I've used the packaged but not that module) to do exactly that: http://hackage.haskell.org/package/hmatrix-0.19.0.0/docs/Numeric-LinearAlgebra-Static.html >>>> >>>> >>>> >>>> On Thu, Mar 14, 2019, 12:37 PM Francesco Ariis > wrote: >>>> Hello Mike, >>>> >>>> On Thu, Mar 14, 2019 at 11:10:06AM +0000, mike h wrote: >>>> > Multiplication of two matrices is only defined when the the number of columns in the first matrix >>>> > equals the number of rows in the second matrix. i.e. c1 == r2 >>>> > >>>> > So when writing the multiplication function I can check that c1 == r2 and do something. >>>> > However what I really want to do, if possible, is to have the compiler catch the error. >>>> >>>> Type-level literals [1] or any kind of similar trickery should help you >>>> with having matrices checked at compile-time. >>>> >>>> [1] https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/type-level-literals.html >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> -- >>>> Frederic Cogny >>>> +33 7 83 12 61 69 >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >>> -- >>> Frederic Cogny >>> +33 7 83 12 61 69 >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From jaysinhp at gmail.com Sun Mar 17 08:22:11 2019 From: jaysinhp at gmail.com (Jaysinh shukla) Date: Sun, 17 Mar 2019 13:52:11 +0530 Subject: [Haskell-beginners] Learn you a Haskell for great good! group reading session Message-ID: Respected Member, I am trying to learn Haskell from the book Learn you a Haskell for great good! [1]. I livestream my reading sessions at this [2] Twitch channel. Group reading is helping me to learn better. It is way more productive than reading alone. If you are learning Haskell, please join us. If you already know Haskell, please join to teach. Teaching will give you a good revision. Don't worry about the commitment to attend each and every session. It is fine to attend sessions on the day you are available and skip the rest. The fun part is, all the sessions will be on weekends. You can watch videos of past sessions here [3]. I am trying to maintain an event calendar for upcoming sessions here [4]. Please share these resources with whoever learning the Haskell. Don't hesitate to ask your questions here or in a personal mail. Thanks! 1: http://learnyouahaskell.com 2: https://www.twitch.tv/jaysinhp 3: https://www.twitch.tv/jaysinhp/videos 4: https://www.twitch.tv/jaysinhp/events From mlang at delysid.org Wed Mar 27 01:08:15 2019 From: mlang at delysid.org (Mario Lang) Date: Wed, 27 Mar 2019 02:08:15 +0100 Subject: [Haskell-beginners] A sort of fold for bits Message-ID: <87bm1xjf9c.fsf@fx.blind.guru> Hi. I have written the following function for "iterating" over all set bits in a Bits instance: foldBits :: (Bits bits, Num bits) => (a -> Int -> a) -> a -> bits -> a foldBits _ a 0 = a foldBits f a n = foldBits f (f a lsb) (n `clearBit` lsb) where lsb = countTrailingZeros n The type signature mimmicks foldl. However, composing several calls to foldBits ends up pretty messy: foldBits f (foldBits f (foldBits f [] value1) value2) value3 So I was thinking, maybe I should just flip the final arguments to make composition easier: fooBits :: (Bits bits, Num bits) => (a -> Int -> a) -> bits -> a -> a fooBits _ 0 a = a foldBits f n a = foldBits f (n `clearBit` lsb) (f a lsb) where lsb = countTrailingZeros n fooBits f value3 . fooBits f value2 . fooBits f value1 $ [] This looks more tidy. However, now that I am no longer mimmicking foldl, I am sort of at a loss how to name this beast. iterBits comes to bind, but this isn't really like iterate. I know, this question might look silly, but good naming conventions seem to make it easier for me to reason about the code. Any suggestions? P.S.: Mimmicking foldl is probably wrong as well, since this isn't really a fold, is it? -- CYa, ⡍⠁⠗⠊⠕ From kothariyugesh at gmail.com Wed Mar 27 03:35:14 2019 From: kothariyugesh at gmail.com (Yugesh Kothari) Date: Wed, 27 Mar 2019 09:05:14 +0530 Subject: [Haskell-beginners] IO String and String using readFile Message-ID: Hi, I am trying to read data from a file. When I do- ghci> contents <- readFile "file.txt" ghci> words contents Then everything works. The same thing when done in a .hs file gives an IO String vs [String] error. Why is that so? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian at zenhack.net Wed Mar 27 03:40:36 2019 From: ian at zenhack.net (Ian Denhardt) Date: Tue, 26 Mar 2019 23:40:36 -0400 Subject: [Haskell-beginners] IO String and String using readFile In-Reply-To: References: Message-ID: <155365803596.1175.5211347879418303338@localhost.localdomain> It would help to see the complete source file, but I'll hazard a guess you're doing something like: main = do contents <- readFile "file.txt" words contents At the GHCi prompt, each line can be any expression, and GHCi will evaluate it and then display it. The two lines in your session aren't really related. In contrast, the do block in the source file expects the expression at the end to be an IO action. What do you want your program to do when you get to "words contents"? display it? (If you want to display it, pass it to `print`). Quoting Yugesh Kothari (2019-03-26 23:35:14) > Hi,� > I am trying to read data from a file. > When I do- > ghci> contents <- readFile "file.txt" > ghci> words contents > Then everything works. > The same thing when done in a .hs file gives an IO String vs [String] > error. > Why is that so? From kothariyugesh at gmail.com Wed Mar 27 03:48:35 2019 From: kothariyugesh at gmail.com (Yugesh Kothari) Date: Wed, 27 Mar 2019 09:18:35 +0530 Subject: [Haskell-beginners] IO String and String using readFile In-Reply-To: <155365803596.1175.5211347879418303338@localhost.localdomain> References: <155365803596.1175.5211347879418303338@localhost.localdomain> Message-ID: I see. anyway, my use case is something like this- I have two functions fromFile :: [String] -> [Int] func :: String -> [Int] I want to use the "words contents" output and pass each element in it to func and send back [Int] from "fromFile" function (where I originally read the file.) Could you suggest a better way to do this? Quoting Ian Denhardt On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, wrote: > It would help to see the complete source file, but I'll hazard a guess > you're doing something like: > > main = do > contents <- readFile "file.txt" > words contents > > At the GHCi prompt, each line can be any expression, and GHCi will > evaluate it and then display it. The two lines in your session aren't > really related. > > In contrast, the do block in the source file expects the expression at > the end to be an IO action. > > What do you want your program to do when you get to "words contents"? > display it? (If you want to display it, pass it to `print`). > > Quoting Yugesh Kothari (2019-03-26 23:35:14) > > Hi,� > > I am trying to read data from a file. > > When I do- > > ghci> contents <- readFile "file.txt" > > ghci> words contents > > Then everything works. > > The same thing when done in a .hs file gives an IO String vs [String] > > error. > > Why is that so? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tanuki at gmail.com Wed Mar 27 03:49:39 2019 From: tanuki at gmail.com (Theodore Lief Gannon) Date: Tue, 26 Mar 2019 20:49:39 -0700 Subject: [Haskell-beginners] IO String and String using readFile In-Reply-To: References: Message-ID: I'm guessing in the .hs file, you have this in a do block? Something like: foo = do contents <- readFile words contents If so, the problem is the return type of `words`. Each line of the do block has to have an IO value, and words is a pure function. To lift its result into the monad, use the aptly-named `pure` function: foo = do contents <- readFile pure $ words contents On Tue, Mar 26, 2019, 8:35 PM Yugesh Kothari wrote: > Hi, > > I am trying to read data from a file. > When I do- > ghci> contents <- readFile "file.txt" > ghci> words contents > > Then everything works. > > The same thing when done in a .hs file gives an IO String vs [String] > error. > > Why is that so? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kothariyugesh at gmail.com Wed Mar 27 03:54:40 2019 From: kothariyugesh at gmail.com (Yugesh Kothari) Date: Wed, 27 Mar 2019 09:24:40 +0530 Subject: [Haskell-beginners] Fwd: IO String and String using readFile In-Reply-To: References: <155365803596.1175.5211347879418303338@localhost.localdomain> Message-ID: Extremely sorry, The definition of fromFile is String -> [Int] where input parameter is the name of the file. ---------- Forwarded message --------- From: Yugesh Kothari Date: Wed, 27 Mar, 2019, 9:18 AM Subject: Re: [Haskell-beginners] IO String and String using readFile To: Cc: I see. anyway, my use case is something like this- I have two functions fromFile :: [String] -> [Int] func :: String -> [Int] I want to use the "words contents" output and pass each element in it to func and send back [Int] from "fromFile" function (where I originally read the file.) Could you suggest a better way to do this? Quoting Ian Denhardt On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, wrote: > It would help to see the complete source file, but I'll hazard a guess > you're doing something like: > > main = do > contents <- readFile "file.txt" > words contents > > At the GHCi prompt, each line can be any expression, and GHCi will > evaluate it and then display it. The two lines in your session aren't > really related. > > In contrast, the do block in the source file expects the expression at > the end to be an IO action. > > What do you want your program to do when you get to "words contents"? > display it? (If you want to display it, pass it to `print`). > > Quoting Yugesh Kothari (2019-03-26 23:35:14) > > Hi,� > > I am trying to read data from a file. > > When I do- > > ghci> contents <- readFile "file.txt" > > ghci> words contents > > Then everything works. > > The same thing when done in a .hs file gives an IO String vs [String] > > error. > > Why is that so? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian at zenhack.net Wed Mar 27 03:57:13 2019 From: ian at zenhack.net (Ian Denhardt) Date: Tue, 26 Mar 2019 23:57:13 -0400 Subject: [Haskell-beginners] IO String and String using readFile In-Reply-To: References: <155365803596.1175.5211347879418303338@localhost.localdomain> Message-ID: <155365903334.2487.7573599623212981998@localhost.localdomain> Sounds like you want concatMap: https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:concatMap Quoting Yugesh Kothari (2019-03-26 23:48:35) > I see. anyway, my use case is something like this- > I have two functions > fromFile :: [String] -> [Int] > func :: String -> [Int] > I want to use the "words contents" output and pass each element in it > to func and send back [Int] from "fromFile" function (where I > originally read the file.) > Could you suggest a better way to do this? > Quoting� Ian Denhardt > On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[1]ian at zenhack.net> wrote: > > It would help to see the complete source file, but I'll hazard a > guess > you're doing something like: > main = do > � � contents <- readFile "file.txt" > � � words contents > At the GHCi prompt, each line can be any expression, and GHCi will > evaluate it and then display it. The two lines in your session > aren't > really related. > In contrast, the do block in the source file expects the expression > at > the end to be an IO action. > What do you want your program to do when you get to "words > contents"? > display it? (If you want to display it, pass it to `print`). > Quoting Yugesh Kothari (2019-03-26 23:35:14) > >� � Hi,� > >� � I am trying to read data from a file. > >� � When I do- > >� � ghci> contents <- readFile "file.txt" > >� � ghci> words contents > >� � Then everything works. > >� � The same thing when done in a .hs file gives an IO String vs > [String] > >� � error. > >� � Why is that so? > > Verweise > > 1. mailto:ian at zenhack.net From ian at zenhack.net Wed Mar 27 04:02:49 2019 From: ian at zenhack.net (Ian Denhardt) Date: Wed, 27 Mar 2019 00:02:49 -0400 Subject: [Haskell-beginners] Fwd: IO String and String using readFile In-Reply-To: References: <155365803596.1175.5211347879418303338@localhost.localdomain> Message-ID: <155365936937.2487.14589775321104928555@localhost.localdomain> Ah, in that case you'll need the type to be String -> IO [Int] In Haskell, functions *only* do computation -- no side effects, like reading a file. So a String -> [Int] can't do what you want. We have a separate type, IO, for doing things that affect the outside world. For example: ghci> :type readFile readFile :: FilePath -> IO String Also, just a style tip: there's a type alias for String defined called FilePath, which would make the type signature more clear in this case. Quoting Yugesh Kothari (2019-03-26 23:54:40) > Extremely sorry, > The definition of fromFile is String -> [Int] where input parameter is > the name of the file. > ---------- Forwarded message --------- > From: Yugesh Kothari <[1]kothariyugesh at gmail.com> > Date: Wed, 27 Mar, 2019, 9:18 AM > Subject: Re: [Haskell-beginners] IO String and String using readFile > To: > Cc: <[2]beginners at haskell.org> > I see. anyway, my use case is something like this- > I have two functions > fromFile :: [String] -> [Int] > func :: String -> [Int] > I want to use the "words contents" output and pass each element in it > to func and send back [Int] from "fromFile" function (where I > originally read the file.) > Could you suggest a better way to do this? > Quoting� Ian Denhardt > On Wed, 27 Mar, 2019, 9:13 AM Ian Denhardt, <[3]ian at zenhack.net> wrote: > > It would help to see the complete source file, but I'll hazard a > guess > you're doing something like: > main = do > � � contents <- readFile "file.txt" > � � words contents > At the GHCi prompt, each line can be any expression, and GHCi will > evaluate it and then display it. The two lines in your session > aren't > really related. > In contrast, the do block in the source file expects the expression > at > the end to be an IO action. > What do you want your program to do when you get to "words > contents"? > display it? (If you want to display it, pass it to `print`). > Quoting Yugesh Kothari (2019-03-26 23:35:14) > >� � Hi,� > >� � I am trying to read data from a file. > >� � When I do- > >� � ghci> contents <- readFile "file.txt" > >� � ghci> words contents > >� � Then everything works. > >� � The same thing when done in a .hs file gives an IO String vs > [String] > >� � error. > >� � Why is that so? > > Verweise > > 1. mailto:kothariyugesh at gmail.com > 2. mailto:beginners at haskell.org > 3. mailto:ian at zenhack.net From noelkwan1998 at gmail.com Sun Mar 31 12:55:04 2019 From: noelkwan1998 at gmail.com (Noel Kwan) Date: Sun, 31 Mar 2019 20:55:04 +0800 Subject: [Haskell-beginners] Integrating external dependencies with stack Message-ID: I am trying to use stack to build a web scraper with scalpel. However, it requires curl-1.8.0 as a dependency and I have no clue how to integrate this dependency. Any insight would be greatly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: