3 ms·
So let's try an experiment: (sqrt (car '(1 2 "hello")) How would this work according to you, if the car function will return type Any? And what is the differe
by urs2102 11y ago
So let's try an experiment:
(sqrt (car '(1 2 "hello"))
How would this work according to you, if the car function will return type Any? And what is the difference between ?x and Any?
Thanks by the way!
- DonaldFisk 11y agoIf that's typed into the listener, it should probably just be interpreted and dynamically typed. If it's an expression in the process of being compiled, it can be simplified to (sqrt 1) and thence to 1. car should return the type of the elements of the list it is supplied with, whatever that is. E.g. if x has type (List (List Int)), (car x) should return (List Int). I did this for Full Metal Jacket (see http://web.onetel.com/~hibou/fmj/interpreter.pdf http://web.onetel.com/~hibou/fmj/interpreter.pdf, section 6). If something has a variable type (e.g. declared as being of type ?x) its type can be inferred once known. If it's of type Any, its type cannot be inferred more specifically than that. If, instead of the above example, you declared x of type (List Any), the list '(1 2 "hello") could still be bound to it, and the call to car would still be accepted by the compiler, but not the call to sqrt.
- urs2102 11y ago> If, instead of the above example, you declared x of type (List Any), the list '(1 2 "hello") could still be bound to it, and the call to car would still be accepted by the compiler, but not the call to sqrt. (sqrt (car '(1 2 "hello")) So in this example, since it's a (List Any), in order to get this to work would we cast the Any to be an Int to then put it through sqrt? (sqrt (int (car '(1 2 "hello"))) Also checked out out FMJ - super interesting. I do like the way you used Prolog's unification as way to describe successful type matching. How far along is your work with FMJ?
- DonaldFisk 11y ago> So in this example, since it's a (List Any), in order to get this to work would we cast the Any to be an Int to then put it through sqrt? I wouldn't recommend you use casting the way it's done in C. It defeats the purpose of having a strong type system if you can change the types like that. Having a function which checks the type and gives an error if it's not the type you expect is, I think, a better approach. I'm still actively developing Full Metal Jacket. Glad you find it interesting. An interpreter for the basic language is almost complete. I'll soon add to my tutorials (http://web.onetel.com/~hibou/fmj/tutorials/TOC.html http://web.onetel.com/~hibou/fmj/tutorials/TOC.html) how to do inlining and substitution macros, and how to define new types. Then I'll make my code more robust, possibly add a compiler, and arrange a release on a shared-source licence.
- urs2102 11y agoOh yeah, my mistake - you're totally right. So in this case what type does car output (or how do I write the type definition for car), because it can't output multiple types otherwise I wouldn't be able to check its value at compile time? And yeah, Full Metal Jacket seemed cool (and it's a great name). Do post it here when you start putting those tutorials up!