[Haskell-cafe] Dynamic types through unsafeCoerce
Hi, I've been using Data.Dynamic but the Typeable requirement doesn't go well with FFI declarations (which don't accept type contexts). This is a little example of what I would like to do: data MyDyn = MyDyn myToDyn :: a - MyDyn myToDyn = unsafeCoerce myFromDyn :: MyDyn - a myFromDyn = unsafeCoerce -- this is a type I want to transform back and forth from dynamic -- and store into lists etc etc ... data MyType a b = MyType a b -- This it's abstract Generic type (it's contructor should be hidden) data MyGenType = MyGenType MyDyn mt2mgt :: MyType a b - MyGenType mt2mgt = MyGenType.myToDyn mgt2mt :: MyGentype - MyType a b mgt2mt (MyGenType dyn) = myfromDyn dyn The question is, ¿if only mt2mgt and mgt2mt are used by the user, would the use of unsafeCoerce be dangerous? Thanks in advance, Alfonso Acosta ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] One question...
Hello to everyone! Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice. Now my problem is connected with the non-update object feature. I can't write variable instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( Let's assume that I've got to write the application that should works as follows: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think properly in Haskell to get described action? cheers Waldemar ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] One question...
Hi Waldmar, Now my problem is connected with the non-update object feature. I can't write variable instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( main = do username - read_info_from_user do_whatever_is_needed print username Try not to think of this too hard. Once you start learning Haskell you'll suddenly realise a lot of the problems just don't happen if you think the Haskell way. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] One question...
To think properly in haskell when you need side effects you should look at monads and how they are used in I/O. I'm no expert but most applications i've seen start like this main = do x -getArgs mapM_ somefunction(x) do is a construct that lets you have a bunch of IO statements in succession which in turn starts to look like imperative programming. So this is how you can access the rest of the world from Haskell. (someone please correct me if i'm wrong) -Dan On 12/9/06, Waldemar Biernacki [EMAIL PROTECTED] wrote: Hello to everyone! Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice. Now my problem is connected with the non-update object feature. I can't write variable instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( Let's assume that I've got to write the application that should works as follows: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think properly in Haskell to get described action? cheers Waldemar ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] One question...
wb: Hello to everyone! Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice. Great! Welcome to Haskell. Now my problem is connected with the non-update object feature. I can't write variable instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( It's not a problem :) Let's assume that I've got to write the application that should works as follows: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think properly in Haskell to get described action? Forget about updating values in place: its not necessary, and makes your programs more flexible, and simpler to understand. Here's a short program to read a user's name, and then apply various functions to that name, printing the result. import System.IO import Data.Char import System.Console.Readline main = do name - do m - readline What is your name return (maybe Nobody id m) print (reverse name) print (length name) let n = map ord name print n print (sum n) print name We can run this: $ runhaskell A.hs What is your name lambdaman namadbmal 9 [108,97,109,98,100,97,109,97,110] 925 lambdaman Just dive in (start on http://haskell.org). -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] SMP support in GHC 6.6 on PPC Macs
On Sat, Nov 25, 2006 at 11:28:13PM +, Ivan Tomac wrote: I've noticed that code compiled with -threaded doesn't like +RTS - Nthread count as an argument. This seems to be the case with both the binary distribution of GHC 6.6 at www.haskell.org/ghc as well as the version I built from sources. I got rid of all the old versions of GHC on my system and ghc -- version reports 6.6. So I'm guessing this is a bug? PowerPC isn't listed as an arch that supports SMP, but I can't see a reason why it shouldn't be. I've just pushed a patch adding it to the list. Thanks Ian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] One question...
Hello yourself!-) Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice. you need to try it as well. haskell.org has information, tutorials, interactive implementations (and to answer the likely follow-on question: you will usually need an editor and a Haskell implementation side by side; edit your program in the editor, load the program into the implementation to play with), .. Now my problem is connected with the non-update object feature. I can't write variable instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( a common (mis-)interpretation of variable. a variable is a representation of something that may not be constant, so far we are in agreement. but the updateable variables you are referring to are a special case of variables, those representing storage locations, the _contents_ of which may be changed. it may be useful to think of variables as named placeholders for things, that is, you either have the named placeholder, or you have the thing itself. the process of replacing those placeholders with the things they stand for is called variable substitution. update programming is all about communicating with some external store of boxes (or objects), and updateable variables stand for references to such boxes (or identities of objects). your program is a sequence of updates of the contents of those boxes - while the variables keep refering to the same locations/references/object identities! executing an update program modifies the external store by performing the prescribed updates, step by step. functional programming is all about replacing programs with programs, and variables are simply named placeholders in such programs. your program is a description of a problem, which may contain variables. evaluating a functional program modifies the program, replacing program parts by other program parts (and possibly variables by things), step by step. the aim of update programming is to transform a store whose contents describe a problem into a store whose contents describe a solution. the aim of functional programming is to transform a program which describes a problem into a program which describes a solution. non-declarative languages support very little functional programming (although that is changing;-), so programmers mostly focus on update programming. Haskell supports both functional programming and update programming, and programmers are encouraged to solve the major part of their problems through functional programming, resorting to update programming only where necessary. and no, I haven't forgotten your problem;-) if you want to reap the full benefits of learning functional programming, you need to adapt your way of thinking about programming to the additional flexibility that Haskell provides you with, and think of transforming programs into programs, rather than transforming stores into stores. variables now stand for parts of the programs that you can fill in, not for parts of the store that you can change. in update programming, you need box variables, because the program doesn't change, only the contents of the store change. in functional programming, the whole program is transformed during evaluation - most of the time, no need for an external store because everything is variable. Let's assume that I've got to write the application that should works as follows: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think properly in Haskell to get described action? Haskell implementations interleave the two phases of functional program evaluation and update program execution (the latter most commonly being input/output). in particular, the value of Main.main is expected to evaluate to an update program, the first step of which is then executed, feeding the result of that first step back into the functional evaluation phase, and so on and so on.. you describe an application which requires a dialogue between program and user, so you need for your program to produce such a dialogue as the value of main. in fact, dialogue descriptions can be created and composed just like other functional values: main = do name - getName let output = various name putStrLn output getName = do putStr your name, please? getLine various name = hello, ++name++ (that's ++show (length name)++ letters) note how the value of main is a dialogue, composed of an input phase involving i/o, a computation phase involving no i/o, and an output phase involving i/o again. note also how various is the name of a placeholder variable standing for a specific
Re: [Haskell-cafe] One question...
Hello Waldemar, Saturday, December 9, 2006, 7:57:52 PM, you wrote: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think properly in Haskell to get described action? 1) variables exists, but supported via additional libs instead of being part of language definition 2) in many cases, you don't need vars because function parameters used instead. in particular, recursion used instead of cycles (code generator generates cycles, of course, in terms of raw cpu ocde). also, data input operations, technically speaking, receive continuation procedure which they calls with a data input as a parameter. for example, code: var - getLine putStr var converted internally into the following: getLine (\var - putStr var) you can find details in http://haskell.org/haskellwiki/IO_inside really, changing one's mind to see functional ways to do something instead of imperative ways, and realizing that full imperative power is still available in rare cases when you need it, is main problem when learning Haskell. i personally abandoned my first attempt to learn because i don't understood how arbitrary imperative action can be implemented in Haskell. but now i've written in Haskell I/O library, arraysvariables library, and data-compression utility :) so i suggest you to read as much Haskell code as possible to see how it can be used (in particular, look into sources of Base and MissingH libraries, and http://haskell.org/haskellwiki/Simple_unix_tools ), read briefly how I/O actions work in Haskell ( http://haskell.org/haskellwiki/IO_inside ), and may be look into sources of my utility ( http://www.haskell.org/bz/FreeArc-sources.tar.gz ) - it contains tons of Russian comments, if you know this language this will be much more interesting reading ps: if you are going from OOP world, look also at http://haskell.org/haskellwiki/OOP_vs_type_classes :) -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
On 12/9/06, Alfonso Acosta [EMAIL PROTECTED] wrote: I've been using Data.Dynamic but the Typeable requirement doesn't go well with FFI declarations (which don't accept type contexts). Can you be a little more specific? mt2mgt :: MyType a b - MyGenType mt2mgt = MyGenType.myToDyn mgt2mt :: MyGentype - MyType a b mgt2mt (MyGenType dyn) = myfromDyn dyn The question is, ¿if only mt2mgt and mgt2mt are used by the user, would the use of unsafeCoerce be dangerous? mgt2mt . mt2mgt :: MyType a b - MyType c d Yes, it's dangerous. The reason Dynamic requires Typeable is to be able to check that you're casting Dynamic back to the original type. -- Taral [EMAIL PROTECTED] You can't prove anything. -- Gödel's Incompetence Theorem ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
On 12/9/06, Taral [EMAIL PROTECTED] wrote: On 12/9/06, Alfonso Acosta [EMAIL PROTECTED] wrote: I've been using Data.Dynamic but the Typeable requirement doesn't go well with FFI declarations (which don't accept type contexts). Can you be a little more specific? Functions like this one are not directy exportable myfunc :: (Tyeable a, Typeable b) = MyType a b ... mt2mgt :: MyType a b - MyGenType mt2mgt = MyGenType.myToDyn mgt2mt :: MyGentype - MyType a b mgt2mt (MyGenType dyn) = myfromDyn dyn mgt2mt . mt2mgt :: MyType a b - MyType c d Yes, it's dangerous. The reason Dynamic requires Typeable is to be able to check that you're casting Dynamic back to the original type. Uhm thinking about it right now I realized that mgt2mt is hidden to the user, so I guess it wouldn't be dangerous if transformations like the following one are not. MyType Int Char --- MyGenType MyType a b So to summarize, Is transforming a monomorphic type to it's polymorphic equivalent through unsafeCoerce a dangerous operation? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
Alfonso Acosta wrote: I've been using Data.Dynamic but the Typeable requirement doesn't go well with FFI declarations (which don't accept type contexts). You wouldn't need a Typeable context anyway; what's biting you is that Dynamic is not one of the primitive types that can pass across the FFI. There are good reasons for that and unsafeCoerce certainly cannot invalidate them. You want a StablePtr. would the use of unsafeCoerce be dangerous? If you have to ask, then yes. -Udo -- Never confuse motion with action. -- Ernest Hemingway signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
On 12/9/06, Udo Stenzel [EMAIL PROTECTED] wrote: You wouldn't need a Typeable context anyway; what's biting you is that Dynamic is not one of the primitive types that can pass across the FFI. There are good reasons for that and unsafeCoerce certainly cannot invalidate them. You want a StablePtr. I'm using StablePtrs already, what made you assume I wasn't using them? would the use of unsafeCoerce be dangerous? If you have to ask, then yes. The name of the function makes it clear, it is unsafe, but it is still used in many cases. I just wanted to know if it was dangerous is this concrete example. Taral gave a good reason -Udo -- Never confuse motion with action. -- Ernest Hemingway -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFFexJhc1ZCC9bsOpURAsoCAJ9wSVhSY5+3sYAV0cRwKi7E3QHTvgCfVbVB GiVZSrY28i5FJUs+V1g1I34= =MSt3 -END PGP SIGNATURE- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
On 12/9/06, Alfonso Acosta [EMAIL PROTECTED] wrote: Functions like this one are not directy exportable myfunc :: (Tyeable a, Typeable b) = MyType a b ... Well, that's true. Then again, you can't export that type anyway without use of a StablePtr. All StablePtrs are exportable. Uhm thinking about it right now I realized that mgt2mt is hidden to the user, so I guess it wouldn't be dangerous if transformations like the following one are not. MyType Int Char --- MyGenType MyType a b So to summarize, Is transforming a monomorphic type to it's polymorphic equivalent through unsafeCoerce a dangerous operation? Sure it is. The type you gave (MyType Int Char - MyType a b) can easily crash your program. -- Taral [EMAIL PROTECTED] You can't prove anything. -- Gödel's Incompetence Theorem ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dynamic types through unsafeCoerce
On 12/9/06, Alfonso Acosta [EMAIL PROTECTED] wrote: On 12/9/06, Udo Stenzel [EMAIL PROTECTED] wrote: You wouldn't need a Typeable context anyway; what's biting you is that Dynamic is not one of the primitive types that can pass across the FFI. There are good reasons for that and unsafeCoerce certainly cannot invalidate them. You want a StablePtr. I'm using StablePtrs already, what made you assume I wasn't using them? How about using a StablePtr to a Dynamic? -- Cheers, Lemmih ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] New Layout Rule take 2
I have made some improvements to the algorithm, and I am happy to say that with some minor tweaks, it correctly lays out the programs in the nofib suite. the algorithm is not much more complicated than the current one in the report, but doesn't have the parse-error rule. it does require a single token of lookahead to look for an in. darcs get http://repetae.net/repos/getlaid/ I have also added a mode so it can work as a ghc preprocesor, allowing very easy testing. just compile with. ghc -pgmF /path/to/getlaid -F --make Main.hs and it will automatically process all your files. Now, it isn't perfect. I can construct pathological examples that the old rule would parse, but this one won't. however, if those examples don't actually occur in practice, then that is not so much an issue. my program doesn't handle many non-haskell 98 extensions, but can probably be easily modified to do so. John -- John Meacham - ⑆repetae.net⑆john⑈ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] laziness
Hi -- I'm using ghc 6.6, and I had some questions about the extend of laziness and foldr. I've noticed the following: x = foldr (:) [] [1..] (take 10) x yields [1..10] Which is great.. however, what I'd like to fold the list over a tuple: foo x (l,payload) = ((x:l), payload) (x,_) = foldr foo ([], Nothing) [1..] (take 10) x Doesn't terminate, until the stack overflows So I'm clearly expecting ghc to be more psychic than it's currently capable. But what I'd like to do is pass an infinite list through a processing function which maintains some state (foo could easily be a pseudo random number generator), and take the first bit of it. Any help? Thanks, Ranjan Bagchi ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe