User loginNavigation |
archivesAutomatically Deriving Mutable Data Structures?Suppose we have the typical immutable list, with some immutable operations: type 'a list = Nil | Cons 'a * 'a list let head l = match l with Nil -> raise "Error!" | Cons(x,next) -> x let add l x = Cons(x,l) let map f l = match l with Nil -> Nil | Cons(x,next) -> Cons(f x, map f next) ... let update l item newitem = match l with Nil as cur -> cur | Cons(x,next) as cur -> if item = x then Cons(newitem, next) else let u = update next item newitem in if u = next then cur else Cons(x, u) let append x l = match l with Nil -> Cons(x, Nil) | Cons(x,next) -> Cons(x, append x next) ... Is there any reason in principle we wouldn't be able to derive a mutable version automatically? At first blush, mutability seems somewhat distributive up to polymorphic parameters. Any recursive function on abstraction Foo that returns a Foo of the exact same type is an mutable update candidate. Transforming outwards-in on a Foo: 1. Return types are replaced by unit. This translation is predicated on a "mutable Foo" being like a "ref Foo". The derived mutable abstraction would look something like: let mutable 'a list = Nil | Cons mutable 'a * mutable 'a list let update l item newitem = match !l with Nil -> () | Cons(x,next) as cur -> if item = !x then x := newitem else update next item newitem let append x l = match !l with Nil as x -> l := Cons(x, Nil) | Cons(x,next) -> append x next I can't think of any language that allows this, or any work along these lines, but I don't really know what to search for. It seems like it would be a pretty handy language feature though. Sometimes you really need a mutable data structure, and even though you have a perfectly good immutable equivalent at hand for which the mutable version seems like a mechanical translation, you still have to translate it by hand. Can anyone provide pointers to work like this or explain the difficulties why it's not feasible in general? TypeScript: Design-Time tool for Application-scale JavaScript developmentTypeScript starts from the syntax and semantics that millions of JavaScript developers know today. With TypeScript, you can use existing JavaScript code, incorporate popular JavaScript libraries, and be called from other JavaScript code. TypeScript compiles to clean, simple JavaScript code which runs on any browser, in Node.js, or in any other ES3-compatible environment. TypeScript offers classes, modules, and interfaces to help you build robust components. These features are available at development time for high-confidence application development, but are compiled into simple JavaScript. TypeScript types let you define interfaces between software components and to gain insight into the behavior of existing JavaScript libraries. |
Browse archivesActive forum topics |
Recent comments
36 weeks 1 day ago
36 weeks 1 day ago
36 weeks 1 day ago
1 year 6 weeks ago
1 year 10 weeks ago
1 year 12 weeks ago
1 year 12 weeks ago
1 year 14 weeks ago
1 year 19 weeks ago
1 year 19 weeks ago