* [Caml-list] polymorphic type constructor deconstructor @ 2003-06-06 21:30 Jeffrey J. Cook 2003-06-06 22:37 ` Jeffrey J. Cook 2003-06-10 15:28 ` Damien Doligez 0 siblings, 2 replies; 5+ messages in thread From: Jeffrey J. Cook @ 2003-06-06 21:30 UTC (permalink / raw) To: caml-list Is there any way to polymorphically deconstruct a type constructor, thus not requiring a pattern match statement? I am using socket communication between processes and would like to utilize type checking to ensure I always have well-formed messages. Right now I'm doing something like (many more cases of course): type request = REQ_do_this of int | REQ_do_that of int * int type reply = RELPY_do_this of successlevel | REPLY_do_that of successlevel * int and am using input_value and output_value with sockets to send/receive messages. My approach right now requires me to do something similar to: let request = REQ_do_this 5 in let reply = Socket.client "mysocket" request in let success = match reply with | REPLY_do_this x -> x | _ -> failwith "this case shouldn't happen and is annoying" however, I would like to do something more like: let request = REQ_do_this(5) in let success = deconstruct (REPLY_do_this(socket_send request)) in where the polymorphic 'deconstruct' declaration is: val deconstruct : 'a -> 'b Where 'a is a type constructed type such as type foo = Bar of int * int and thus 'b is of type (int * int) My complication is that I have multiple sets of request and reply types in different modules which I would like to deconstruct without duplicating the function, not to meantion not manually enumerating all types and their straightforward deconstruction. Any ideas on how to do or rework to avoid this? Thanks. Jeff -- Jeffrey J. Cook Graduate Student, Electrical Engineering University of Illinois at Urbana-Champaign jjcook@uiuc.edu ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] polymorphic type constructor deconstructor 2003-06-06 21:30 [Caml-list] polymorphic type constructor deconstructor Jeffrey J. Cook @ 2003-06-06 22:37 ` Jeffrey J. Cook 2003-06-07 0:09 ` John Max Skaller 2003-06-07 8:22 ` Marcin 'Qrczak' Kowalczyk 2003-06-10 15:28 ` Damien Doligez 1 sibling, 2 replies; 5+ messages in thread From: Jeffrey J. Cook @ 2003-06-06 22:37 UTC (permalink / raw) To: caml-list Hmmm, I didn't make much sense, I'll try again. I form requests sent to a server using a type constructor of type 'request': type request = REQ_do_this of int | REQ_do_that of int * int which pattern matches on the request type, does some processing, and then sends back a loosely paired 'reply': type reply = RELPY_do_this of successlevel | REPLY_do_that of successlevel * int Using the constructors helps to both ensure the message is well formed, plus on the server side it lets me decide what was requested. On the client side, I only expect a single case of type constructored value of type 'reply', and thus needing a match statement is overly verbose. I would like to simplify this into a function taking a type constructor of type 'reply' and a type constructed value of type 'reply' and return the contents of the type constructed value, such as (int * int) from Foo of int * int. This function would internally have a match statement and throw an exception if its not of the right type constructor. To accomplish this, I would assume I would need a deconstructor function that at most takes a type constructor of type 'reply' and a value of type 'reply'. However, of course, one cannot pass a type constructor as an argument. The only solution, using this approach, that I can think of, is by passing a dummy constructed value, and pattern matching it against the real constructed value, throwing an exception if it isn't of the right constructor, otherwise decontructing the value and returning the data within. This approach, however, is difficult since the dummy value must be populated with dummy data, and each case would need to be pattern matched for by hand. Furthermore, if I have seperate 'request' and 'reply' type pairs, I need to create this hand crafted function for each. So, is there any way to polymorphically deconstruct a type constructed value, which is type safe by providing either a dummy constructed value or a dummy constructor? Thanks. Jeff -- Jeffrey J. Cook Graduate Student, Electrical Engineering University of Illinois at Urbana-Champaign jjcook@uiuc.edu ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] polymorphic type constructor deconstructor 2003-06-06 22:37 ` Jeffrey J. Cook @ 2003-06-07 0:09 ` John Max Skaller 2003-06-07 8:22 ` Marcin 'Qrczak' Kowalczyk 1 sibling, 0 replies; 5+ messages in thread From: John Max Skaller @ 2003-06-07 0:09 UTC (permalink / raw) To: Jeffrey J. Cook; +Cc: caml-list Jeffrey J. Cook wrote: > Hmmm, I didn't make much sense, I'll try again. It made sense to me, Felix has such a combinator for internal use called 'case_arg_n', it returns the argument of a the n'th kind of constructor of a union without any run time checking: there's a function 'check_case' that checks if a value is a particular case, so a match on a union type is done by: if (check_case (1, v)) handler1(case_arg_n(1,v)); else if ... I guess an implementation dependent C function could be written which checks Ocaml union tag values. Still, you'd have to manually magic the return type for each case .. OcamlP4 might be able to solve the latter problem so you could write type t = Ctor1 ctor1_arg_t | Ctor2 ... deconstruct(Ctor1, v) which would have type ctor_arg_t as desired. Deconstruct here would call a Camlp4 macro which looked up a table generated from the union declaration for t to find the right argument type, and to calculate the tag index to pass to the C function for the run time check. [I'm assuming in total ignorance the tags are sequential from the same origin for each union declared] -- John Max Skaller, mailto:skaller@ozemail.com.au snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia. voice:61-2-9660-0850 ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] polymorphic type constructor deconstructor 2003-06-06 22:37 ` Jeffrey J. Cook 2003-06-07 0:09 ` John Max Skaller @ 2003-06-07 8:22 ` Marcin 'Qrczak' Kowalczyk 1 sibling, 0 replies; 5+ messages in thread From: Marcin 'Qrczak' Kowalczyk @ 2003-06-07 8:22 UTC (permalink / raw) To: caml-list Dnia sob 7. czerwca 2003 00:37, Jeffrey J. Cook napisał: > I would like to simplify this into a function taking a type constructor > of type 'reply' and a type constructed value of type 'reply' and return > the contents of the type constructed value, such as (int * int) from > Foo of int * int. This function would internally have a match statement > and throw an exception if its not of the right type constructor. I see no other way than writing these functions manually. Just write them once and use them instead of passing constructors. BTW, the contents of 'REPLY_do_that of successlevel * int' is two things separately, not a pair. You can't match against 'Reply_do_that p'. Similarly for construction. > The only solution, using this approach, that I can think of, is by passing > a dummy constructed value, and pattern matching it against the real > constructed value, throwing an exception if it isn't of the right > constructor, otherwise decontructing the value and returning the data > within. There is no need for dummy values. Just write a matching function for each constructor manually. It's not that painful and there is no good other way anyway. > So, is there any way to polymorphically deconstruct a type constructed > value, which is type safe by providing either a dummy constructed value or > a dummy constructor? What type would such a deconstructor have? -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] polymorphic type constructor deconstructor 2003-06-06 21:30 [Caml-list] polymorphic type constructor deconstructor Jeffrey J. Cook 2003-06-06 22:37 ` Jeffrey J. Cook @ 2003-06-10 15:28 ` Damien Doligez 1 sibling, 0 replies; 5+ messages in thread From: Damien Doligez @ 2003-06-10 15:28 UTC (permalink / raw) To: Jeffrey J. Cook; +Cc: caml-list On Friday, June 6, 2003, at 11:30 PM, Jeffrey J. Cook wrote: > let request = REQ_do_this 5 in > let reply = Socket.client "mysocket" request in > let success = > match reply with > | REPLY_do_this x -> x > | _ -> failwith "this case shouldn't happen and is annoying" You can use patterns in the left-hand part of a let: let request = REQ_do_this 5 in let reply = Socket.client "mysocket" request in let REPLY_do_this success = reply in ... success ... The compiler will complain about a non-exhaustive pattern matching, but that's only fair. -- Damien ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-06-10 15:29 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-06-06 21:30 [Caml-list] polymorphic type constructor deconstructor Jeffrey J. Cook 2003-06-06 22:37 ` Jeffrey J. Cook 2003-06-07 0:09 ` John Max Skaller 2003-06-07 8:22 ` Marcin 'Qrczak' Kowalczyk 2003-06-10 15:28 ` Damien Doligez
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox