[Haskell-cafe] Embedded Functions in Algebraic Data Types?
On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Michael ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?
2008/2/10, Michael Feathers [EMAIL PROTECTED]: On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Yes, anyway you can embed function in Maybe a or Either a b for example, since they're polymorphic. Embedding functions in ADT purposefully happens quite often too, just look at the State Monad, the functionnal references proposal and so on... Recently I embedded functions into a denotational semantic ADT for a tiny DSL, there's plenty of use cases. -- Jedaï ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?
On Feb 10, 2008 1:34 PM, Michael Feathers [EMAIL PROTECTED] wrote: On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Yes, this is quite common. Here's a quick example (untested): data MyMap key value = M (key - Maybe value) lookup (M m) key = m key insert (M m) key value = M (\key' - if key' == key then Just value else m key') This is a naive data structure for storing a map as a function from the key to the value. Not very efficient, perhaps, but you use similar concepts in more useful scenarios (e.g. the State monad). -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?
Quite frequently. Here are a few examples from my own code: For functional references (representing a bidirectional function from a data type to a part of itself (for example the first element of a pair)). data Accessor s a = Accessor { get :: s - a , set :: a - s - s } My quantum computation arrow (really in the realm of concrete, useful things, huh? :-) data Operator b c = Op (forall d. QStateVec (b,d) - IO (QStateVec (c,d))) | ... The ubiquitous FRP Behavior, comprising a current value and a function which takes a timestep and returns the next value. data Behavior a = Behavior a (Double - Behavior a) The suspend monad, representing a computation which can either finish now with a value of type a, or suspends to request a value of type v before continuing. data Suspend v a = Return a | Suspend (v - Suspend v a) It seems that most frequently, functions in ADTs are used when implementing monads or arrows, but that doens't need to be the case. A lot of times a function is the best way to represent a particular part of a data structure :-) Luke On Feb 10, 2008 1:34 PM, Michael Feathers [EMAIL PROTECTED] wrote: On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Michael ___ 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] Embedded Functions in Algebraic Data Types?
On Feb 10, 2008 1:42 PM, Sebastian Sylvan [EMAIL PROTECTED] wrote: On Feb 10, 2008 1:34 PM, Michael Feathers [EMAIL PROTECTED] wrote: On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Yes, this is quite common. Here's a quick example (untested): data MyMap key value = M (key - Maybe value) lookup (M m) key = m key insert (M m) key value = M (\key' - if key' == key then Just value else m key') This is a naive data structure for storing a map as a function from the key to the value. Not very efficient, perhaps, but you use similar concepts in more useful scenarios (e.g. the State monad). Perhaps I should add an empty MyMap too: empty = M (\_- Nothing ) -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?
Great. Thanks to everyone! Michael Luke Palmer wrote: Quite frequently. Here are a few examples from my own code: For functional references (representing a bidirectional function from a data type to a part of itself (for example the first element of a pair)). data Accessor s a = Accessor { get :: s - a , set :: a - s - s } My quantum computation arrow (really in the realm of concrete, useful things, huh? :-) data Operator b c = Op (forall d. QStateVec (b,d) - IO (QStateVec (c,d))) | ... The ubiquitous FRP Behavior, comprising a current value and a function which takes a timestep and returns the next value. data Behavior a = Behavior a (Double - Behavior a) The suspend monad, representing a computation which can either finish now with a value of type a, or suspends to request a value of type v before continuing. data Suspend v a = Return a | Suspend (v - Suspend v a) It seems that most frequently, functions in ADTs are used when implementing monads or arrows, but that doens't need to be the case. A lot of times a function is the best way to represent a particular part of a data structure :-) Luke On Feb 10, 2008 1:34 PM, Michael Feathers [EMAIL PROTECTED] wrote: On a lark, I loaded this into Hugs this morning, and it didn't complain: data Thing = Thing (Integer - Integer) But, I've never seen that sort of construct in an example. Do people ever embed functions in ADTs? Michael ___ 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] Embedded Functions in Algebraic Data Types?
On Sun, 10 Feb 2008, Henning Thielemann wrote: On Sun, 10 Feb 2008, Luke Palmer wrote: Quite frequently. Here are a few examples from my own code: For functional references (representing a bidirectional function from a data type to a part of itself (for example the first element of a pair)). data Accessor s a = Accessor { get :: s - a , set :: a - s - s } My quantum computation arrow (really in the realm of concrete, useful things, huh? :-) The pattern seems to be common enough to be turned into a package. http://www.haskell.org/haskellwiki/?title=Record_accessaction=history I meant http://www.haskell.org/haskellwiki/Record_access ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?
On Sun, 10 Feb 2008, Luke Palmer wrote: Quite frequently. Here are a few examples from my own code: For functional references (representing a bidirectional function from a data type to a part of itself (for example the first element of a pair)). data Accessor s a = Accessor { get :: s - a , set :: a - s - s } My quantum computation arrow (really in the realm of concrete, useful things, huh? :-) The pattern seems to be common enough to be turned into a package. http://www.haskell.org/haskellwiki/?title=Record_accessaction=history ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe