123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)letbool=1letint8=1letuint8=1letchar=1letint16=2letuint16=2letuint30=4letuint32=4letuint64=8letint31=4letint32=4letint64=8letfloat=8(* this is checked via an assert in binary_length *)letmax_size_of_uint30_like_n=5typetag_size=[`Uint8|`Uint16][@@derivinghash]lettag_size=function`Uint8->uint8|`Uint16->uint16typesigned_integer=[`Int31|`Int16|`Int8][@@derivinghash]typeunsigned_integer=[`Uint30|`Uint16|`Uint8][@@derivinghash]typeinteger=[signed_integer|unsigned_integer][@@derivinghash]typelength=[`N|unsigned_integer][@@derivinghash]letsigned_range_to_sizeminmax:[>signed_integer]=ifmin>=~-128&&max<=127then`Int8elseifmin>=~-32_768&&max<=32_767then`Int16else`Int31(* max should be centered at zero *)letunsigned_range_to_sizemax:[>unsigned_integer]=assert(max>=0);ifmax<=255then`Uint8elseifmax<=65535then`Uint16else`Uint30letinteger_to_size=function|`Int31->int31|`Int16->int16|`Int8->int8|`Uint30->uint30|`Uint16->uint16|`Uint8->uint8letlength_to_max_size=function|`N->max_size_of_uint30_like_n|`Uint30->uint30|`Uint16->uint16|`Uint8->uint8letmax_int=function|`N|`Uint30|`Int31->(1lsl30)-1|`Int16->(1lsl15)-1|`Int8->(1lsl7)-1|`Uint16->(1lsl16)-1|`Uint8->(1lsl8)-1letmin_int=function|`Uint8|`Uint16|`Uint30|`N->0|`Int31->-(1lsl30)|`Int16->-(1lsl15)|`Int8->-(1lsl7)letrange_to_size~minimum~maximum:integer=ifminimum<0thensigned_range_to_sizeminimummaximumelseunsigned_range_to_size(maximum-minimum)letenum_sizearr=unsigned_range_to_size(Array.lengtharr)