## 03 September 2016

### Arithmetic Types in Kitten

In order to motivate myself to work on my programming language, Kitten, I’ve decided to publish my notes on various aspects of the design as I figure them out. This is partly to solicit comments and drum up interest in the project, but mostly an exercise in ensuring my ideas are solid enough to explain clearly.

In these articles, “Kitten” will refer to the “ideal” language and the new work-in-progress compiler that approximates it.

## Explicitness

In non-generic definitions, it should always be evident from the code what type of data you’re using. As such, the type of integer and floating-point literals can be specified with a type suffix: i8, i16, …, u8, …, f32, f64; the unadorned integer literal 1 is equivalent to 1i32, and the float literal 1.0 is equivalent to 1.0f64. I think signed 32-bit integers and 64-bit floats are reasonable defaults for common use cases.

In the future, we could relax this by allowing the suffix to be inferred, or by allowing the default suffix to be locally changed.

// Currently fails to typecheck; 3 could be deduced to mean 3i64.
(2i64 + 3)

// Currently not valid syntax; 2 and 3 could be defaulted to Int64.
assume (Int64)
(2 + 3)

## Uniformity

Because overloading interacts rather poorly with type inference, it would be easiest for me to provide different operators for different types, such as + for integer addition and +. for floating-point addition, as in OCaml. I did this in the old compiler, and it wasn’t great for usability. I solve this through the use of traits—for each operator there is a single generic definition, with instances provided for all the common arithmetic types.

trait + <T> (T, T -> T)

instance + (Int32, Int32 -> Int32):
_::kitten::add_int32

instance + (Float64, Float64 -> Float64):
_::kitten::add_float64

Traits work like template specialisations in C++: if + is inferred to have the type Int32, Int32 -> Int32 at a given call site, then the compiler will emit a call to that particular instance; no generic code makes it into the final executable. And if no such instance exists, you’ll get a straightforward compilation error.

These traits will generally need to be inlined for performance reasons. Luckily, Kitten’s metadata system allows us to specify this easily enough:

about +:
docs: """
Adds two values of some type, producing a result of the same type.
"""
inline: always

The about notation is a dumping ground for all metadata: documentation, examples, tests, operator precedence information, and optimization hints. The idea is that it’s better to have all of this information in a single structured format than to use some combination of magical comments, pragmas, attributes, and otherwise special syntax.

## Safety

We want arithmetic operations to be safe, meaning that failure modes such as implicit overflow and trapping should be opt-in.

Therefore, we should provide an arbitrary-precision integer type (Int) as a sane default, as well as an array of signed and unsigned fixed-precision integer types in common sizes: Int8, Int16, Int32, Int64, and their UIntN counterparts.

Kitten has a permission system for reasoning about the permitted side effects of functions. Operations on fixed-precision integers (both signed and unsigned) are checked by default, requiring the +Fail permission to allow them to raise assertion failures in case of overflow.

instance + (Int32, Int32 -> Int32 +Fail) { … }

However, if most arithmetic operations can fail, then +Fail will proliferate through all the type signatures of calling code, which is a real drag, and might encourage people to use unchecked types more liberally than they should. We can solve this in two ways:

• Make +Fail implicit in type signatures, and introduce a notation -Fail for removing it. This speaks to a more general notion of implicit permissions, which I think would be really valuable: it would let us introduce new implicit permissions in a backward-compatible way, allowing new code to opt out. For example, if we implicitly grant +Alloc to all functions, then they’re permitted to allocate memory on the heap—lists, closures, bignums, and so on. But for performance-critical code, you may want a static guarantee that your code performs no heap allocations, so you can opt out by specifying -Alloc.
• Introduce a wrapper type, Checked<T>, which provides arithmetic operations that don’t require +Fail, but instead return an Optional<T> as the result. This would mean making the signatures of the arithmetic traits more general, or inlining the Optional into the representation of Checked.

Neither is unequivocally better, and they have different use cases, so I think it makes sense to provide both.

## Modifiers

In addition to Checked<T>, Kitten provides wrapper types for different arithmetic modes:

• Wrapped<T> is a T where overflow is defined to wrap modulo the range of T.
• Unchecked<T> is a T where overflow is implementation-defined. I’m tempted to make this type require the +Unsafe permission, but that’s intended for operations that can violate memory safety, which Unchecked<T> cannot.
• Saturating<T> is a T where overflow results in clamping to the range of T; this is useful in some signal-processing applications.

## Platform Independence

All the basic arithmetic types should be provided on all platforms. That may require emulation in some cases, such as 64-bit arithmetic on a 32-bit processor. Implementations can provide additional platform-specific types such as Int128 or Float80.

SIMD operations are implemented on container-like types such as Vector4<Float32>; naïve-looking code such as:

vec { (* 2) } map

Should be lowered to the single instruction:

addps %xmm0, %xmm0

This is subtle, and I haven’t worked out all the details yet, but SIMD operations are important enough for performance-critical code that it seems worthwhile to consider them early on.

## Licensing Problems

For arbitrary-precision types, I’m in a bit of a bind. I’m not equipped to write a native Kitten bignum library with performance on par with GMP, but because GMP is licensed under LGPL, it can’t be used freely. For instance, we can’t statically link it into generated executables, making distribution more difficult and negatively impacting performance.

## TL;DR

• Kitten has all the usual machine integer and float types that systems programmers love.
• Literal types are explicit, with some sane defaults: signed Int32 and Float64.
• Overflow is checked by default, with various type-system features for more control.
• The standard library provides arbitrary-precision and SIMD operations.

#### 91 comments:

1. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic

C and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery

2. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.

Business Analytics Course in Bangalore

3. I will very much appreciate the writer's choice for choosing this excellent article suitable for my topic. Here is a detailed description of the topic of the article that helped me the most.

Data Analytics Course in Bangalore

4. Thanks, this is generally helpful.
Still, I followed step-by-step your method in this Python Online Training
Python Online Course

5. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

6. wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
This article resolved my all queries.good luck an best wishes to the team members.learn digital marketing use these following link
Digital Marketing Course in Chennai

7. Thanks for sharing this in here. You are running a great blog, keep up this good work.
Online degree courses
Distance education

8. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data analytics courses in bangalore

9. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore

10. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Training in Chennai

11. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data analytics courses in bangalore

12. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such amazing content for all the curious readers who are very keen on being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in the future too.

Digital Marketing Training in Bangalore

13. I found Habit to be a transparent site, a social hub that is a conglomerate of buyers and sellers willing to offer digital advice online at a decent cost.

Artificial Intelligence Training in Bangalore

14. I really enjoyed reading your blog. It was very well written and easy to understand. Unlike other blogs that I have read which are actually not very good. Thank you so much!

Data Science Training in Bangalore

15. The Extraordinary blog went amazed with the content that they have developed in a very descriptive manner. This type of content surely ensures the participants explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

Machine Learning Course in Bangalore

16. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Course Syllabus

17. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data science course in jaipur

18. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Training in Chennai

19. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
data science course

20. this is really nice to read..informative post is very good to read..thanks a lot!
data scientist training and placement in hyderabad

21. A good blog always contains new and exciting information, and reading it I feel like this blog really has all of these qualities that make it a blog.

Artificial Intelligence Training in Bangalore

22. It is late to find this act. At least one should be familiar with the fact that such events exist. I agree with your blog and will come back to inspect it further in the future, so keep your performance going.

Digital Marketing Training in Bangalore

23. I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.

Data Science Training in Bangalore

24. I am more curious to take an interest in some of them. I hope you will provide more information on these topics in your next articles.

Machine Learning Course in Bangalore

25. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
business analytics courses

26. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Course in Delhi

27. The article posted was very informative and useful. You people are doing a great job. Keep going.
data science training

28. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!ethical hacking course in patna

29. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
data analytics course in patna

30. I am overwhelmed by your post with such a beautiful subject. I usually visit their blogs and update myself through the information they contain, but today's blog would be most appreciated. Well done!

Machine Learning Course in Bangalore

31. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. data science course in delhi with placement

32. This post is good enough to make somebody understand this amazing thing, and I’m sure everyone will appreciate this interesting things. data science training institute in gurgaon

33. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors
data science training institute in noida

34. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!best data science course in bhubaneswar

35. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.data science training in gwalior

36. I really appreciate this wonderful message you have given us. I assure you that would be beneficial for most people.

Data Science Course in Nagpur

37. Impressive. Your story always bring hope and new energy. Keep up the good work. Data Analytics Course in Chennai

38. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors
data analytics course in trivandrum

39. Impressive. Your story always bring hope and new energy. Keep up the good work. Data Analytics Course in Vadodara

40. Melayu 4D Slot | Online Slots Casino fun88 soikeotot fun88 soikeotot dafabet dafabet 911Golden Race Virtual Racing Experience | Shootercasino

41. Digital Marketing Course can be a vital tool to cultivate your career and business. In this course work, you will discover what is online marketing, why it is significant, how all businesses are turning from traditional marketing to this online branding.

For more details, visit our website -

Best & Affordable Digital Marketing Course in Chennai

42. I love this. It is soo informative. Are you also searching for cheap assignment help we are the best solution for you. We are best known for delivering the best services to students without having to break the bank

43. Your way of telling everything in this article is genuinely pleasant, all can easily understand it, Thanks a lot 온라인카지노

44. This is an awesome article, Given such an extraordinary measure of data in it, These sort of articles keeps the customers excitement for the site, and keep sharing more ... favorable circumstances. 스포츠토토

45. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.

온라인카지노

46. What an incredible message this is. Truly one of the best posts I have ever seen in my life. Wow, keep it up.

Data Scientist Training in Bangalore

47. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.data science course in rohtak

48. I curious more interest in some of them hope you will give more information on this topics in your next articles.
full stack development course

49. Are you searching for the best AWS Training in Chennai Here is the best software training Institute in Chennai. Infycle Technologies gives you finest courses such as Power BI, Cyber Security, Graphic Design and Animation, Block Security, Java, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., and we also provide best technical trainers with best training 100+ Live Practical Sessions with Real-Time scenarios at the end of the course the freshers, experienced, and Tech professionals will be able to obtain more knowledge of the course and be able to crack interviews on top MNC’s with an amazing package. For more details ring us up on 7504633633, 7502633633.

50. Really impressed! Everything is a very open and very clear clarification of the issues. It contains true facts. Your website is very valuable. Thanks for sharing.

Best Data Analytics Courses in Bangalore

51. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors
data scientist course in faridabad

52. Very informative message! There is so much information here that can help any business start a successful social media campaign!
data science training in london

53. If you are searching for the best shopping platform, visit our website. Your article is so good to read waiting for the next update. Keep updating.
Online shopping Platform in chennai
Hosted Ecommerce Platform
Build Your Own e Commerce Web site

54. It is perfect chance to make a couple of game plans for the future and the opportunity has arrived to be sprightly. I've scrutinized this post and if I may I have the option to need to suggest you some interesting things or recommendations. Perhaps you could create next articles insinuating this article. I have to examine more things about it!

55. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

56. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors
data scientist course in delhi

57. I was curious if you ever thought of changing the layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or 2 pictures. Maybe you could space it out better?|data analytics course in jodhpur

58. I see the greatest contents on your blog and I extremely love reading them. Data Scientist Course in Dombivli

59. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting.
A debt of gratitude is in order for sharing.business analytics course in kolhapur

60. It is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it! data science course in mysore

61. I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site. data science training in kanpur

62. Happy to chat on your blog, I feel like I can't wait to read more reliable posts and think we all want to thank many blog posts to share with us.

Data Science Training Institutes in Bangalore

63. I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…https://360digitmg.com/course/certification-program-on-digital-marketing

64. The blog and data is excellent and informative as well your work is very good and I appreciate well hopping for some more informative posts.
Business Analytics Course in Gurgaon

65. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one.
Continue posting. A debt of gratitude is in order for sharing.data science course in warangal

66. I'm genuinely getting a charge out of scrutinizing your richly formed articles. Apparently you consume a huge load of energy and time on your blog. I have bookmarked it and I am expecting scrutinizing new articles. Continue to do amazing.business analytics course in ghaziabad

67. Informative Post. The information you have posted is very useful and sites you have referred was good. Thanks for sharing.
Data Science Course with Placement

68. Everything is very open with a clear clarification of the issues. It was truly informative. Your site is useful. Thank you for sharing!|data analytics course in jodhpur

69. Going to graduate school was a positive decision for me. I enjoyed the coursework, the presentations, the fellow students, and the professors. And since my company reimbursed 100% of the tuition, the only cost that I had to pay on my own was for books and supplies. Otherwise, I received a free master’s degree. All that I had to invest was my time. data science training in surat

70. I just got to this amazing site not long ago was actually captured with the piece of resources you have got here and big thumbs up for making such wonderful blog page!
Data Scientist Course

71. I want to thank you for your time in this wonderful read which is really appreciable and put you in your favorites to see new things on your blog, a must-have blog!
Business Analytics Course in Noida

72. i found your this call despite the fact that searching for a few related reference concerning blog search...Its a nice publicize..store posting and update the mention. NordVPN Crack Pc

73. Your article looks really adorable, here's a site link i dropped for you which you may like. 스포츠토토

74. I really like it when people come together and share ideas.
Great website, keep it up! 경마

75. I love your blog.. very nice colors & theme. 파친코사이트

76. Great web site you have got here.. It’s hard to find quality writing like yours these days. I really appreciate individuals like you! Take care!! 토토사이트

77. They need to be good in python and other programming skills and modern AI tools to develop a website whose success is guaranteed.
data science training in shimla

78. Well done for this excellent article. and really enjoyed reading this article today it might be one of the best articles I have read so far and please keep this work of the same quality.
Data Analytics Course in Noida

79. When you have understood the basic concepts of data science, you must choose a path to move in data science. You need to learn the following courses to be an expert in the field of data science.

data science course in gorakhpur

80. Really this article is truly one of the best in article history and am a collector of old "items" and sometimes read new items if i find them interesting which is one that I found quite fascinating and should be part of my collection. Very good work!
Data Scientist Course in Gurgaon

81. Achieve success with the best Data Analytics Certification training delivered by the pioneers. Hands-on experience, Top industry trainers, World-class curriculum & Assignments. data analytics course in trichy

82. Thanks for sharing this valuable content, keep doing the same.
Digital Marketing Course can be a vital tool to cultivate your career and business. In this course work, you will discover what is online marketing, why it is significant, how all businesses are turning from traditional marketing to this online branding. For more details, visit our website - View Our Website