text
stringlengths 0
601k
|
---|
module type FUNCTOR_F = functor ( T : TYPE ) -> FUNCTOR with type ' a t = T . t * ' a |
module type APPLY_F = functor ( S : SEMIGROUP ) -> APPLY with type ' a t = S . t * ' a |
module type APPLICATIVE_F = functor ( M : MONOID ) -> APPLICATIVE with type ' a t = M . t * ' a |
module type MONAD_F = functor ( M : MONOID ) -> MONAD with type ' a t = M . t * ' a |
module type FOLDABLE_F = functor ( T : TYPE ) -> FOLDABLE with type ' a t = T . t * ' a |
module type EQ_F = functor ( First : EQ ) ( Second : EQ ) -> EQ with type t = First . t * Second . t |
module type SHOW_F = functor ( First : SHOW ) ( Second : SHOW ) -> SHOW with type t = First . t * Second . t |
module type TRAVERSABLE_F = functor ( T : TYPE ) ( A : APPLICATIVE ) -> TRAVERSABLE with type ' a t = T . t * ' a and type ' a applicative_t = ' a A . t |
module Magma : MAGMA_F = functor ( First : MAGMA ) ( Second : MAGMA ) -> struct type t = First . t * Second . t let append ( a , b ) ( a ' , b ' ) = First . append a a ' , Second . append b b ' end |
module Semigroup : SEMIGROUP_F = functor ( First : SEMIGROUP ) ( Second : SEMIGROUP ) -> struct include Magma ( First ) ( Second ) end |
module Monoid : MONOID_F = functor ( First : MONOID ) ( Second : MONOID ) -> struct include Semigroup ( First ) ( Second ) let empty = First . empty , Second . empty end |
module Functor : FUNCTOR_F = functor ( T : TYPE ) -> struct type ' a t = T . t * ' a let map f ( a , b ) = a , f b end |
module Apply : APPLY_F = functor ( S : SEMIGROUP ) -> struct include Functor ( S ) let apply ( a , f ) ( a ' , x ) = S . append a a ' , f x end |
module Applicative : APPLICATIVE_F = functor ( M : MONOID ) -> struct include Apply ( M ) let pure a = M . empty , a end |
module Monad : MONAD_F = functor ( M : MONOID ) -> struct include Applicative ( M ) let flat_map ( a , b ) f = match f b with | a ' , c -> M . append a a ' , c end |
module Foldable : FOLDABLE_F = functor ( T : TYPE ) -> struct type ' a t = T . t * ' a let fold_left f init ( _ , x ) = f init x and fold_right f init ( _ , x ) = f x init module Fold = struct let fold_map f ( _ , x ) = f x end module Fold_Map ( M : MONOID ) = struct include Fold end module Fold_Map_Plus ( P : PLUS ) = struct include Fold end module Fold_Map_Any ( M : MONOID_ANY ) = struct include Fold end end |
module Traversable : TRAVERSABLE_F = functor ( T : TYPE ) ( A : APPLICATIVE ) -> struct type ' a t = T . t * ' a and ' a applicative_t = ' a A . t include ( Functor ( T ) : FUNCTOR with type ' a t := ' a t ) include ( Foldable ( T ) : FOLDABLE with type ' a t := ' a t ) let traverse f ( x , y ) = A . map ( fun z -> x , z ) ( f y ) and sequence ( x , y ) = A . map ( fun z -> x , z ) y end |
module Eq : EQ_F = functor ( First : EQ ) ( Second : EQ ) -> struct type t = First . t * Second . t let eq ( a , b ) ( a ' , b ' ) = First . eq a a ' && Second . eq b b ' end |
module Semigroupoid : SEMIGROUPOID with type ( ' a , ' b ) t = ' a * ' b = struct type ( ' a , ' b ) t = ' a * ' b let compose ( _ , c ) ( a , _ ) = a , c end |
module Show : SHOW_F = functor ( First : SHOW ) ( Second : SHOW ) -> struct type t = First . t * Second . t let show ( a , b ) = " ( " ^ First . show a ^ " , " ^ Second . show b ^ " ) " end |
module Bifunctor : BIFUNCTOR with type ( ' a , ' b ) t = ' a * ' b = struct type ( ' a , ' b ) t = ' a * ' b let bimap f g ( a , b ) = f a , g b end |
module Biapply : BIAPPLY with type ( ' a , ' b ) t = ' a * ' b = struct include Bifunctor let biapply ( f , g ) ( a , b ) = f a , g b end |
module Biapplicative : BIAPPLICATIVE with type ( ' a , ' b ) t = ' a * ' b = struct include Biapply let bipure a b = a , b end |
module Bifoldable : BIFOLDABLE with type ( ' a , ' b ) t = ' a * ' b = struct type ( ' a , ' b ) t = ' a * ' b let bifold_left f g init ( a , b ) = g ( f init a ) b and bifold_right f g init ( a , b ) = f a ( g b init ) module Fold_Map ( M : MONOID ) = struct let fold_map f g ( a , b ) = M . append ( f a ) ( g b ) end module Fold_Map_Any ( M : MONOID_ANY ) = struct let fold_map f g ( a , b ) = M . append ( f a ) ( g b ) end module Fold_Map_Plus ( P : PLUS ) = struct let fold_map f g ( a , b ) = P . alt ( f a ) ( g b ) end end |
module type BITRAVERSABLE_F = functor ( A : APPLICATIVE ) -> BITRAVERSABLE with type ' a applicative_t = ' a A . t and type ( ' a , ' b ) t = ' a * ' b |
module Bitraversable : BITRAVERSABLE_F = functor ( A : APPLICATIVE ) -> struct type ( ' a , ' b ) t = ' a * ' b and ' a applicative_t = ' a A . t include ( Bifunctor : BIFUNCTOR with type ( ' a , ' b ) t := ( ' a , ' b ) t ) include ( Bifoldable : BIFOLDABLE with type ( ' a , ' b ) t := ( ' a , ' b ) t ) module I = Infix . Apply ( A ) let bitraverse f g ( a , b ) = let open I in A . map ( fun a b -> a , b ) ( f a ) <*> g b and bisequence ( a , b ) = let open I in A . map ( fun a b -> a , b ) a <*> b end |
module Infix = struct include Infix . Biapply ( Biapply ) end |
module T = Map . Make ( struct type t = name list let compare t1 t2 = Pervasives . compare t1 t2 end ) |
type result = { ty_name : name ; ty_labels : name list ; } |
type return = { def_types : ( name * typ ) Zmisc . Env . t ; table : result T . t ; } |
let empty = { def_types = Zmisc . Env . empty ; table = T . empty } |
let recordtype ( { def_types = dtypes ; table = table } as return ) ty_list = let ( ty , l_list ) , return = try . find ty_list table , return with | Not_found -> let l_list = List . map ( fun _ -> Zident . fresh " l " ) ty_list in let l_ty_list = List . map2 ( fun l ty -> ( l , ty ) ) l_list ty_list in let ty_name = Zident . fresh " ty " in let dtypes = Zmisc . Env . add ty_name l_ty_list dtypes in let table = ( ty_list , ty , l_list ) :: table in ty_name , l_list , { def_types = dtypes ; table = table } in Zaux . record l_list e_list ty |
let tuple_into_record return e_list = let ty_list = List . map ( fun e -> e . e_typ ) e_list in recordtype return ty_list |
let tuplepat_into_record return e_list = recordtype return ty_list |
let rec pattern return ( { p_desc = desc } as p ) = match desc with | Ewildpat | Econstpat _ | Econstr0pat _ | Evarpat _ -> p , return | Etuplepat ( p_list ) -> let p_list , return = Zmisc . map_fold pattern return p_list in tuplepat_into_recordpat return p_list | Etypeconstraintpat ( p , ty ) -> let p , return = pattern return p in { p with p_desc = Etypeconstraintpat ( p , ty ) } , return | Eorpat _ | Erecordpat _ -> assert false |
let rec expression return ( { e_desc = desc } as e ) = match desc with | Elocal _ | Eglobal _ | Econst _ | Econstr0 _ | Elast _ -> e , return | Eapp ( app , e_arg , e_list ) -> let e_arg , return = expression return e_arg in let e_list , return = Zmisc . map_fold expression return e_list in { e with e_desc = Eapp ( app , e_arg , e_list ) } , return | Eop ( op , e_list ) -> let e_list , return = Zmisc . map_fold expression return e_list in { e with e_desc = Eop ( op , e_list ) } , return | Erecord_access ( e , lid ) -> let e , return = expression return e in { e with e_desc = Erecord_access ( e , lid ) } , return | Erecord ( l_e_list ) -> let l_e_list , return = Zmisc . map_fold ( fun return ( l , e ) -> let e , return = expression return e in ( l , e ) , return ) return l_e_list in { e with e_desc = Erecord ( l_e_list ) } , return | Etypeconstraint ( e , ty ) -> let e , return = expression return e in { e with e_desc = Etypeconstraint ( e , ty ) } , return | Etuple ( e_list ) -> let e_list , return = Zmisc . map_fold expression return e_list in tuple_into_record return e_list | Ematch _ | Eseq _ | Elet _ | Eperiod _ | Eblock _ | Epresent _ -> assert false |
let rec equation return ( { eq_desc = desc } as eq ) = match desc with | EQeq ( p , e ) -> let return , e = expression return e in { eq with eq_desc = EQeq ( p , e ) } , return | EQinit ( x , e ) -> let return , e = expression return e in { eq with eq_desc = EQinit ( x , e ) } , return | EQreset ( eq_list , e ) -> let eq_list , return = Zmisc . map_fold equation return eq_list in let return , e = expression return e in { eq with eq_desc = EQreset ( eq_list , e ) } , return | EQmatch ( total , e , p_h_list ) -> let p_h_list , return = Zmisc . map_fold ( fun return ( { m_body = b } as h ) -> let return , b = block return b in { h with m_body = b } , return ) return p_h_list in let e , return = expression return e in { eq with eq_desc = EQmatch ( total , e , p_h_list ) } , return | EQnext _ | EQblock _ | EQemit _ | EQautomaton _ | EQpresent _ | EQder _ | EQpluseq _ | EQand _ | EQbefore _ | EQforall _ -> assert false let eq_list , return = Zmisc . map_fold equation return eq_list in { b with b_body = eq_list } , return |
let local return ( { l_eq = eq_list ; l_env = l_env } as l ) = let eq_list , return = Zmisc . map_fold equation return eq_list in { l with l_eq = eq_list } , return |
let let_expression return ( { e_desc = desc } as e ) = match desc with | Elet ( l , e ) -> let l , return = local return l in let e , return = expression return e in { e with e_desc = Elet ( l , e ) } , return | _ -> expression return e |
let implementation return impl = match impl . desc with | Eopen _ | Etypedecl _ | Econstdecl _ -> impl , return | Efundecl ( n , ( { f_body = e } as body ) ) -> let e , return = expression return e in { impl with desc = Efundecl ( n , { body with f_body = e } ) } , return |
let implementation_list impl_list = let impl_list , return = Zmisc . map_fold implementation empty impl_list in impl_list |
let suite = suite " Tuple2 " ( fun ( ) -> test " make " ( fun ( ) -> expect ( Tuple2 . make 3 4 ) 4 |> toEqual ( let open Eq in pair int int ) int ( 3 , 4 ) 4 ) ; test " first " ( fun ( ) -> expect ( Tuple2 . first ( 3 , 4 ) 4 ) 4 |> toEqual ( let open Eq in int ) int 3 ) ; test " second " ( fun ( ) -> expect ( Tuple2 . second ( 3 , 4 ) 4 ) 4 |> toEqual ( let open Eq in int ) int 4 ) ; test " mapFirst " ( fun ( ) -> expect ( Tuple2 . mapFirst ~ f : String . reverse ( " stressed " , 16 ) 16 ) 16 |> toEqual ( let open Eq in pair string int ) int ( " desserts " , 16 ) 16 ) ; test " mapSecond " ( fun ( ) -> expect ( Tuple2 . mapSecond ~ f : Float . squareRoot ( " stressed " , 16 ) ) . |> toEqual ( let open Eq in pair string float ) float ( " stressed " , 4 ) . ) ; test " mapEach " ( fun ( ) -> expect ( Tuple2 . mapEach ~ f : String . reverse ~ g : Float . squareRoot ( " stressed " , 16 ) . ) |> toEqual ( let open Eq in pair string float ) float ( " desserts " , 4 ) . ) ; test " mapAll " ( fun ( ) -> expect ( Tuple2 . mapAll ~ f : String . reverse ( " was " , " stressed ) ) " |> toEqual ( let open Eq in pair string string ) string ( " saw " , " desserts ) " ) ; test " swap " ( fun ( ) -> expect ( Tuple2 . swap ( 3 , 4 ) 4 ) 4 |> toEqual ( let open Eq in pair int int ) int ( 4 , 3 ) 3 ) ; test " toArray " ( fun ( ) -> expect ( Tuple2 . toArray ( 3 , 4 ) 4 ) 4 |> toEqual ( let open Eq in array int ) int [ | 3 ; 4 ] | ) ; test " toList " ( fun ( ) -> expect ( Tuple2 . toList ( 3 , 4 ) 4 ) 4 |> toEqual ( let open Eq in list int ) int [ 3 ; 4 ] ) ) |
let suite = suite " Tuple3 " ( fun ( ) -> let open Tuple3 in test " make " ( fun ( ) -> expect ( make 3 4 5 ) 5 |> toEqual ( let open Eq in trio int int int ) int ( 3 , 4 , 5 ) 5 ) ; test " first " ( fun ( ) -> expect ( first ( 3 , 4 , 5 ) 5 ) 5 |> toEqual Eq . int 3 ) 3 ; test " second " ( fun ( ) -> expect ( second ( 3 , 4 , 5 ) 5 ) 5 |> toEqual Eq . int 4 ) 4 ; test " third " ( fun ( ) -> expect ( third ( 3 , 4 , 5 ) 5 ) 5 |> toEqual Eq . int 5 ) 5 ; test " initial " ( fun ( ) -> expect ( initial ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in pair int int ) int ( 3 , 4 ) 4 ) ; test " tail " ( fun ( ) -> expect ( tail ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in pair int int ) int ( 4 , 5 ) 5 ) ; test " mapFirst " ( fun ( ) -> expect ( mapFirst ~ f : String . reverse ( " stressed " , 16 , false ) false ) false |> toEqual ( let open Eq in trio string int bool ) bool ( " desserts " , 16 , false ) false ) ; test " mapSecond " ( fun ( ) -> expect ( mapSecond ~ f : Float . squareRoot ( " stressed " , 16 . , false ) false ) false |> toEqual ( let open Eq in trio string float bool ) bool ( " stressed " , 4 . , false ) false ) ; test " mapThird " ( fun ( ) -> expect ( mapThird ~ f : not ( " stressed " , 16 , false ) false ) false |> toEqual ( let open Eq in trio string int bool ) bool ( " stressed " , 16 , true ) true ) ; test " mapEach " ( fun ( ) -> expect ( mapEach ~ f : String . reverse ~ g : Float . squareRoot ~ h : not ( " stressed " , 16 . , false ) false ) |> toEqual ( let open Eq in trio string float bool ) bool ( " desserts " , 4 . , true ) true ) ; test " mapAll " ( fun ( ) -> expect ( mapAll ~ f : String . reverse ( " was " , " stressed " , " now ) ) " |> toEqual ( let open Eq in trio string string string ) string ( " saw " , " desserts " , " won ) " ) ; test " rotateLeft " ( fun ( ) -> expect ( rotateLeft ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in trio int int int ) int ( 4 , 5 , 3 ) 3 ) ; test " rotateRight " ( fun ( ) -> expect ( rotateRight ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in trio int int int ) int ( 5 , 3 , 4 ) 4 ) ; test " toArray " ( fun ( ) -> expect ( toArray ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in array int ) int [ | 3 ; 4 ; 5 ] | ) ; test " toList " ( fun ( ) -> expect ( toList ( 3 , 4 , 5 ) 5 ) 5 |> toEqual ( let open Eq in list int ) int [ 3 ; 4 ; 5 ] ) ) |
let test_check_tuple context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from builtins import return_tuple def derp ( ) -> int : a , b = return_tuple ( ) return a + b } | [ ] ; assert_type_errors { | import typing def f ( l : typing . List [ int ] ) -> int : [ a , b ] = l return a + b } | [ ] ; assert_type_errors { | import typing def foo ( a : typing . Tuple [ int , int ] ) -> None : a . tuple_method ( 1 . 0 ) } | [ " Incompatible parameter type [ 6 ] : In call ` tuple . tuple_method ` , for 1st positional only \ parameter expected ` int ` but got ` float ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , . . . ] : return ( 1 , 2 , 3 ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , str ] : return ( 1 , " string " , 3 ) } | [ " Incompatible return type [ 7 ] : Expected ` Tuple [ int , str ] ` but got ` Tuple [ int , str , int ] ` . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , . . . ] : return ( 1 , " string " , 3 ) } | [ " Incompatible return type [ 7 ] : Expected ` typing . Tuple [ int , . . . ] ` but got ` Tuple [ int , str , \ int ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , . . . ] : return tuple ( [ 1 , 2 , 3 ] ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , . . . ] : return tuple ( [ " " ] ) } | [ " Incompatible return type [ 7 ] : Expected ` typing . Tuple [ int , . . . ] ` but got " ^ " ` typing . Tuple [ str , . . . ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ float , . . . ] : return tuple ( [ 1 ] ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ float , . . . ] : return ( 1 , 2 ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ float , . . . ] : return ( 1 . 0 , 2 . 0 ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ float , . . . ] : return ( 1 . 0 , 2 ) } | [ ] ; assert_type_errors { | import typing def foo ( x : typing . Tuple [ int , int , str ] ) -> typing . Tuple [ str , int ] : a , * b = x return b } | [ " Incompatible return type [ 7 ] : Expected ` Tuple [ str , int ] ` but got ` List [ Union [ int , str ] ] ` . " ] ; assert_type_errors { | def derp ( ) -> int : a , b = [ 1 , 2 , 3 ] return a + b } | [ ] ; assert_type_errors { | def foo ( ) -> int : ( x , y ) , z = 0 return x + y + z } | [ " Unable to unpack [ 23 ] : Unable to unpack ` int ` into 2 values . " ] ; assert_type_errors { | import typing class Foo : def __init__ ( self , coord : typing . Tuple [ int , int ] ) -> None : self . xxx , self . yyy = coord } | [ " Missing attribute annotation [ 4 ] : Attribute ` xxx ` of class ` Foo ` " ^ " has type ` int ` but no type is specified . " ; " Missing attribute annotation [ 4 ] : Attribute ` yyy ` of class ` Foo ` " ^ " has type ` int ` but no type is specified . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . Sized : return ( 1 , ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Sized : return ( 1 , " " ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple : return ( ) } | [ " Invalid type parameters [ 24 ] : Generic type ` tuple ` expects at least 1 type parameter . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ ( ) ] : return ( ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , str ] : return ( ) } | [ " Incompatible return type [ 7 ] : Expected ` Tuple [ int , str ] ` but got ` Tuple [ ] ` . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , . . . ] : return ( ) } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Iterable [ int ] : return ( ) } | [ ] ; assert_type_errors { | from builtins import int_to_int import typing def bar ( z : typing . Optional [ int ] ) -> typing . Tuple [ int , typing . Optional [ int ] ] : return 1 , int_to_int ( z ) if z is not None else None } | [ ] ; assert_type_errors { | import typing def foo ( * args : int ) -> typing . Iterable [ str ] : return args } | [ " Incompatible return type [ 7 ] : Expected ` Iterable [ str ] ` but got ` typing . Tuple [ int , . . . ] ` . " ] ; assert_type_errors { | import collections T = collections . namedtuple ( ' T ' , ' a b c ' ) def b ( d : T ) -> None : a = d . a + d . d } | [ " Missing attribute annotation [ 4 ] : Attribute ` a ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` b ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` c ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` a ` must have a type other than ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` b ` must have a type other than ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` c ` must have a type other than ` Any ` . " ; " Undefined attribute [ 16 ] : ` T ` has no attribute ` d ` . " ; ] ; assert_type_errors { | import collections import typing class C ( collections . namedtuple ( ' T ' , ' a b ' ) ) : def __new__ ( cls , a : int ) -> typing . Type [ C ] : . . . C ( 1 , 2 ) } | [ " Missing attribute annotation [ 4 ] : Attribute ` a ` of class ` C ` must have a type other than \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` b ` of class ` C ` must have a type other than \ ` Any ` . " ; " Uninitialized attribute [ 13 ] : Attribute ` a ` is declared in class ` C ` to have type \ ` typing . Any ` but is never initialized . " ; " Uninitialized attribute [ 13 ] : Attribute ` b ` is declared in class ` C ` to have type \ ` typing . Any ` but is never initialized . " ; " Too many arguments [ 19 ] : Call ` C . __new__ ` expects 1 positional argument , 2 were provided . " ; ] ; assert_type_errors { | import collections T = collections . namedtuple ( ' T ' , ' a b c ' ) def foo ( t : T ) -> None : x , y = t x , y , z = t x , y , z , other = t } | [ " Missing attribute annotation [ 4 ] : Attribute ` a ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` b ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` c ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` a ` must have a type other than ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` b ` must have a type other than ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` c ` must have a type other than ` Any ` . " ; " Unable to unpack [ 23 ] : Unable to unpack 3 values , 2 were expected . " ; " Unable to unpack [ 23 ] : Unable to unpack 3 values , 4 were expected . " ; ] ; assert_type_errors { | import collections T = collections . namedtuple ( ' T ' , ' a ' ) T ( a = 1 ) def foo ( ) -> None : T ( a = 2 ) } | [ " Missing attribute annotation [ 4 ] : Attribute ` a ` of class ` T ` must have a type other than \ ` Any ` . " ; " Missing parameter annotation [ 2 ] : Parameter ` a ` must have a type other than ` Any ` . " ; ] ; assert_type_errors { | import typing T = typing . NamedTuple ( ' T ' , [ ( ' a ' , str ) , ( ' b ' , int ) ] ) def takes_int ( x : int ) -> None : pass def foo ( x : T ) -> None : takes_int ( x . b ) a , b = x reveal_type ( a ) reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` a ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` b ` is ` int ` . " ; ] ; assert_type_errors { | import typing class TestNamedTupleUnpackingFieldsNotInAlphabeticalOrder ( typing . NamedTuple ) : foo : str bar : int baz : typing . List [ str ] hello : typing . List [ int ] def foo ( ) -> None : ( a , b , c , d ) = TestNamedTupleUnpackingFieldsNotInAlphabeticalOrder ( " foo " , 1 , [ " bar " ] , [ 7 ] ) reveal_type ( a ) reveal_type ( b ) reveal_type ( c ) reveal_type ( d ) } | [ " Revealed type [ - 1 ] : Revealed type for ` a ` is ` str ` . " ; " Revealed type [ - 1 ] : Revealed type for ` b ` is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` c ` is ` typing . List [ str ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` d ` is ` typing . List [ int ] ` . " ; ] ; assert_type_errors { | def foo ( input : int ) -> None : x , y = input } | [ " Unable to unpack [ 23 ] : Unable to unpack ` int ` into 2 values . " ] ; assert_type_errors { | def foo ( ) -> bool : return ( 52 , ) < ( 1 , 2 , 3 ) } | [ ] ; assert_type_errors { | import typing class FooNotNamedTuple : bar : typing . Optional [ str ] = None baz : typing . Dict [ int , typing . Any ] = { } hello : typing . Dict [ str , typing . Any ] = { } } | [ " Missing attribute annotation [ 4 ] : Attribute ` baz ` of class ` FooNotNamedTuple ` must have a \ type that does not contain ` Any ` . " ; ] ; assert_type_errors { | import typing class Foo ( typing . NamedTuple ) : bar : typing . Optional [ str ] = None baz : typing . Dict [ int , typing . Any ] = { } hello : typing . Dict [ str , typing . Any ] = { } } | [ " Missing parameter annotation [ 2 ] : Parameter ` baz ` must have a type that does not contain \ ` Any ` . " ; " Missing attribute annotation [ 4 ] : Attribute ` baz ` of class ` Foo ` must have a type that does \ not contain ` Any ` . " ; ] ; assert_type_errors { | import typing class Foo : def __new__ ( cls , foo : typing . Dict [ int , typing . Any ] = { } ) -> Foo : return super ( Foo , cls ) . __new__ ( cls ) } | [ " Missing parameter annotation [ 2 ] : Parameter ` foo ` must have a type that does not contain \ ` Any ` . " ; ] ; assert_type_errors { | import typing def __new__ ( foo : typing . Dict [ int , typing . Any ] = { } ) -> None : pass } | [ " Missing parameter annotation [ 2 ] : Parameter ` foo ` must have a type that does not contain \ ` Any ` . " ; ] ; assert_type_errors { | import typing X = typing . NamedTuple ( " x " , dates = str ) X ( dates " = foo " ) } | [ ] ; assert_type_errors { | from typing import List , Tuple , Union def foo ( ) -> None : union_of_bounded_tuples : Union [ Tuple [ int , str ] , Tuple [ bool , List [ int ] ] ] a , b = union_of_bounded_tuples reveal_type ( a ) reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` a ` is ` Union [ bool , int ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` b ` is ` Union [ List [ int ] , str ] ` . " ; ] ; ( ) |
let test_tuple_literal_access context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | def foo ( ) -> int : x = ( 0 , " one " , 2 ) return x [ 0 ] } | [ ] ; assert_type_errors { | def foo ( ) -> int : x = ( 0 , " one " , 2 ) return x [ 1 ] } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` str ` . " ] ; assert_type_errors { | def foo ( p : int ) -> int : x = ( 0 , " one " , 2 ) return x [ p ] } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` Union [ int , str ] ` . " ] ; assert_type_errors { | def foo ( ) -> int : x = ( 0 , " one " , 2 ) return x [ - 1 ] } | [ ] ; assert_type_errors { | def foo ( ) -> int : x = ( 0 , " one " , 2 ) return x [ - 2 ] } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` str ` . " ] ; assert_type_errors { | def foo ( ) -> int : i = 0 x = ( 0 , " one " , 2 ) return x [ i ] } | [ ] ; assert_type_errors { | def foo ( ) -> int : i = - 2 x = ( 0 , " one " , 2 . ) return x [ - 3 ] } | [ ] ; assert_type_errors { | def foo ( ) -> int : x = ( 0 , " one " , 2 ) return x [ 3 ] } | [ " Incompatible return type [ 7 ] : Expected ` int ` but got ` Union [ int , str ] ` . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Tuple [ int , int ] : x = ( 0 , 1 , " two " ) return x [ 0 : 2 ] } | [ " Incompatible return type [ 7 ] : Expected ` Tuple [ int , int ] ` but got " ^ " ` typing . Tuple [ Union [ int , str ] , . . . ] ` . " ; ] ; assert_type_errors { | def func ( a : int , b : str , c : bool ) -> None : pass x = ( 42 , " bla " , False ) func ( * x ) } | [ ] ; assert_type_errors { | def func ( a : int , b : str , c : bool ) -> None : pass c = ( " bla " , False ) func ( 1 , * c ) } | [ ] ; assert_type_errors { | def func ( a : int , b : str , c : bool ) -> None : pass c = ( " bla " , ) func ( 1 , * c ) } | [ " Missing argument [ 20 ] : Call ` func ` expects argument ` c ` . " ] ; assert_type_errors { | def func ( a : int , b : bool , c : str ) -> None : pass c = ( " bla " , False ) func ( 1 , * c ) } | [ " Incompatible parameter type [ 6 ] : In call ` func ` , for 2nd positional only parameter expected \ ` bool ` but got ` str ` . " ; " Incompatible parameter type [ 6 ] : In call ` func ` , for 3rd positional only parameter expected \ ` str ` but got ` bool ` . " ; ] ; assert_type_errors { | def func ( a : int , b : bool , c : str , d : int , e : str ) -> None : pass c = ( False , " ble " ) d = ( 1 , " abc " ) func ( 1 , * c , * d ) } | [ ] ; ( ) |
let test_custom_tuple context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | class C : def __getitem__ ( self , index : int ) -> int : self . counter += 1 return self . counter def __init__ ( self ) -> None : self . counter = 0 def foo ( ) -> None : x , y , z = C ( ) reveal_type ( x ) reveal_type ( y ) reveal_type ( z ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` z ` is ` int ` . " ; ] ; assert_type_errors { | from typing import Any class C : def __getitem__ ( self , index : int ) -> Any : self . counter += 1 return self . counter def __init__ ( self ) -> None : self . counter = 0 def foo ( ) -> None : x , y , z = C ( ) reveal_type ( x ) reveal_type ( y ) reveal_type ( z ) } | [ " Missing return annotation [ 3 ] : Returning ` int ` but type ` Any ` is specified . " ; " Unable to unpack [ 23 ] : Unable to unpack ` test . C ` into 3 values . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` z ` is ` typing . Any ` . " ; ] ; assert_type_errors { | def foo ( ) -> None : x , y , z = object ( ) reveal_type ( x ) reveal_type ( y ) reveal_type ( z ) } | [ " Unable to unpack [ 23 ] : Unable to unpack ` object ` into 3 values . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing . Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Any ` . " ; " Revealed type [ - 1 ] : Revealed type for ` z ` is ` typing . Any ` . " ; ] |
let test_length context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Tuple def foo ( ) -> None : xs : Tuple [ ( ) ] y = len ( xs ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` int ` . " ] ; ( ) |
let test_unpacking context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from typing import Tuple def foo ( x : int , xs : Tuple [ str , bool ] ) -> None : y = ( x , * xs ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ int , str , bool ] ` . " ] ; assert_type_errors { | from typing import Tuple def foo ( x : int , xs : str ) -> None : y = ( x , * xs ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ str , . . . ] ] ` . " ] ; assert_type_errors { | from typing import Tuple def foo ( ) -> None : y = ( 1 , ( * 2 , 3 ) , ( * 4 , 5 , 6 ) , 7 ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` Tuple [ typing_extensions . Literal [ 1 ] , \ typing_extensions . Literal [ 2 ] , typing_extensions . Literal [ 3 ] , typing_extensions . Literal [ 4 ] , \ typing_extensions . Literal [ 5 ] , typing_extensions . Literal [ 6 ] , typing_extensions . Literal [ 7 ] ] ` . " ; ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def foo ( x : int , xs : Tuple [ str , Unpack [ Ts ] , bool ] ) -> Tuple [ int , str , Unpack [ Ts ] , bool ] : y = ( x , * xs ) reveal_type ( y ) return y } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , str , * test . Ts , bool ] ` . " ] ; assert_type_errors { | from typing import Tuple from pyre_extensions import TypeVarTuple , Unpack Ts = TypeVarTuple ( " Ts " ) def foo ( xs : Tuple [ str , Unpack [ Ts ] , bool ] ) -> None : y = ( * xs , ( * 2 , 3 ) , * xs ) reveal_type ( y ) } | [ " Unable to concatenate tuple [ 60 ] : Concatenation not yet support for multiple variadic \ tuples : ` * xs , * xs ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ typing . Any , . . . ] ` . " ; ] ; assert_type_errors { | from typing import List , Tuple def foo ( x : int , some_list : List [ int ] ) -> Tuple [ int , . . . ] : y = ( x , * some_list ) reveal_type ( y ) return y } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ int , . . . ] ] ` . " ] ; assert_type_errors { | from typing import List def foo ( x : int , some_list : List [ str ] ) -> None : y = ( x , * some_list ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ str , . . . ] ] ` . " ] ; assert_type_errors { | from typing import Tuple , Union def foo ( x : int , union : Union [ Tuple [ int , str ] , Tuple [ bool ] ] ) -> None : y = ( x , * union ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ Union [ bool , int , \ str ] , . . . ] ] ` . " ; ] ; assert_type_errors { | from typing import Any # pyre - ignore [ 2 ] : Explicit Any . def foo ( x : int , any : Any ) -> None : y = ( x , * any ) reveal_type ( y ) } | [ " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ typing . Any , . . . ] ] ` . " ] ; assert_type_errors { | def foo ( x : int , not_iterable : int ) -> None : y = ( x , * not_iterable ) reveal_type ( y ) } | [ " Unable to concatenate tuple [ 60 ] : Expected to unpack an iterable , but got ` int ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing . Tuple [ int , * Tuple [ typing . Any , . . . ] ] ` . " ; ] ; ( ) |
let test_star_args context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : Unpack [ Tuple [ int , Unpack [ Tuple [ int , . . . ] ] ] ] ) -> None : . . . def main ( xs : List [ int ] ) -> None : expect_int ( * xs ) } | [ ] ; assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : Unpack [ Tuple [ int , . . . ] ] ) -> None : . . . def main ( list_of_string : List [ str ] ) -> None : expect_int ( * list_of_string ) } | [ " Incompatible parameter type [ 6 ] : In call ` expect_int ` , for 1st positional only parameter \ expected ` int ` but got ` str ` . " ; ] ; assert_type_errors { | from pyre_extensions import ParameterSpecification , Unpack from typing import Callable , Tuple P = ParameterSpecification ( " P " ) def expect_int ( * args : Unpack [ Tuple [ int , Unpack [ Tuple [ int , . . . ] ] ] ] ) -> None : . . . def expect_object ( * args : Unpack [ Tuple [ object , Unpack [ Tuple [ object , . . . ] ] ] ] ) -> None : . . . def outer ( f : Callable [ P , int ] ) -> None : def inner ( * args : P . args , ** kwargs : P . kwargs ) -> None : expect_object ( * args ) expect_int ( * args ) } | [ " Invalid argument [ 32 ] : Argument types ` * Tuple [ object , . . . ] ` are not compatible with \ expected variadic elements ` int , * Tuple [ int , . . . ] ` . " ; ] ; assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : Unpack [ Tuple [ int , Unpack [ Tuple [ int , . . . ] ] ] ] ) -> None : . . . def main ( xs : List [ int ] ) -> None : expect_int ( * xs , 42 , * xs ) } | [ ] ; assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : Unpack [ Tuple [ int , Unpack [ Tuple [ int , . . . ] ] ] ] ) -> None : . . . def main ( xs : List [ int ] , ys : List [ str ] ) -> None : expect_int ( * xs , * ys ) } | [ " Invalid argument [ 32 ] : Argument types ` * Tuple [ typing . Union [ int , str ] , . . . ] ` are not \ compatible with expected variadic elements ` int , * Tuple [ int , . . . ] ` . " ; ] ; assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : Unpack [ Tuple [ int , Unpack [ Tuple [ int , . . . ] ] ] ] ) -> None : . . . def main ( x : int ) -> None : expect_int ( * x ) } | [ " Invalid argument [ 32 ] : Unpacked argument ` x ` must have an unpackable type but has type ` int ` . " ; ] ; assert_type_errors { | from pyre_extensions import Unpack from typing import List , Tuple def expect_int ( * args : int ) -> None : . . . def main ( x : int ) -> None : expect_int ( * x ) } | [ " Invalid argument [ 32 ] : Unpacked argument ` x ` must have an unpackable type but has type ` int ` . " ; ] ; ( ) |
let ( ) = " tuple " >::: [ " check_tuple " >:: test_check_tuple ; " literal_access " >:: test_tuple_literal_access ; " custom_tuple " >:: test_custom_tuple ; " length " >:: test_length ; " unpacking " >:: test_unpacking ; " star_args " >:: test_star_args ; ] |> Test . run |
if n <= 0 then invalid_arg " duplicate n < 0 " else let arr = Array . init n ( fun x -> x ) x in let lst = Array ( . let patt = <: patt < $ tup : Ast . paCom_of_list lst $ >> in <: expr < fun $ patt $ -> $ tup : Ast . exCom_of_list inherit Ast . map as super ; method ! expr e = match e with [ <: expr @ _loc < Tuple . map $ lid : f $ $ tup : xs $ >> -> <: expr < $ tup : Ast ( . exCom_of_list ( List . map ( fun e -> <: expr < $ lid : f $ $ e $ >> ) | <: expr @ _loc < Tuple . map $ lid : f $ $ int : num $ >> -> super # expr | <: expr @ _loc < Tuple . map $ _ $ >> -> | others -> super # expr others ] ; |
let ( left , right ) = match x with | 0 -> n , 42 | 1 -> 42 , n | _ -> assert false in left - right let ( left , right ) = match x with | 0 -> n , 42 | 1 -> 42 , n | 2 -> 42 - n , 0 | 3 -> 0 , 42 - n | 4 -> n / 2 , n / 2 | 5 -> n , n | _ -> assert false in left - right let ( left , right ) = match x with | " 0 " -> n , 42 | " 1 " -> 42 , n | " 2 " -> 42 - n , 0 | " 3 " -> 0 , 42 - n | " 4 " -> n / 2 , n / 2 | " 5 " -> n , n | _ -> assert false in left - right |
let test f n i = let mw_overhead = let a = Gc . minor_words ( ) in let b = Gc . minor_words ( ) in b . - a in let mw = Gc . minor_words ( ) in let k = f n i in assert ( k = 0 ) ; let mw ' = Gc . minor_words ( ) in let delta = int_of_float ( mw ' . - mw . - mw_overhead ) in printf " allocated % d words \ n " delta |
let ( ) = let n = 42 in printf " small_match :\ n " ; for i = 0 to 1 do test small_match n i done ; printf " big_match :\ n " ; for i = 0 to 5 do test big_match n i done ; printf " string_match :\ n " ; for i = 0 to 5 do test string_match n ( string_of_int i ) done |
let phys_equal = Caml . ( == ) |
let arch_sixtyfour = Sys . word_size = 64 |
module Int = struct let num_bits = Int . num_bits let max_value = Caml . max_int let to_string = string_of_int end |
let concat l = Base . String . concat ~ sep " " : l |
module type S = S |
module Pool = struct let grow_capacity ~ capacity ~ old_capacity = match capacity with | None -> if old_capacity = 0 then 1 else old_capacity * 2 | Some capacity -> if capacity <= old_capacity then failwiths ~ here [ :% here ] " Pool . grow got too small capacity " ( ` capacity capacity , ` old_capacity old_capacity ) [ % sexp_of : [ ` capacity of int ] * [ ` old_capacity of int ] ] ; capacity ; ; module Slots = Tuple_type . Slots let max_slot = 14 module Slot = struct type ( ' slots , ' a ) t = int [ @@ deriving sexp_of ] let equal ( t1 : ( _ , _ ) t ) t2 = t1 = t2 let t0 = 1 let t1 = 2 let t2 = 3 let t3 = 4 let t4 = 5 let t5 = 6 let t6 = 7 let t7 = 8 let t8 = 9 let t9 = 10 let t10 = 11 let t11 = 12 let t12 = 13 let t13 = 14 let % test _ = t13 = max_slot end let array_index_num_bits = if arch_sixtyfour then ( assert ( Int . num_bits = 63 ) ; 30 ) else ( assert ( Int . num_bits = 31 || Int . num_bits = 32 ) ; 22 ) ; ; let masked_tuple_id_num_bits = Int . num_bits - array_index_num_bits let % test _ = array_index_num_bits > 0 let % test _ = masked_tuple_id_num_bits > 0 let % test _ = array_index_num_bits + masked_tuple_id_num_bits <= Int . num_bits let max_array_length = 1 lsl array_index_num_bits module Tuple_id : sig type t = private int [ @@ deriving sexp_of ] include Invariant . S with type t := t val to_string : t -> string val equal : t -> t -> bool val init : t val next : t -> t val of_int : int -> t val to_int : t -> int val examples : t list end = struct type t = int [ @@ deriving sexp_of ] let invariant t = assert ( t >= 0 ) let to_string = Int . to_string let equal ( t1 : t ) t2 = t1 = t2 let init = 0 let next t = if arch_sixtyfour then t + 1 else if t = Int . max_value then 0 else t + 1 let to_int t = t let of_int i = if i < 0 then failwiths ~ here [ :% here ] " Tuple_id . of_int got negative int " i [ % sexp_of : int ] ; i ; ; let examples = [ 0 ; 1 ; 0x1FFF_FFFF ; Int . max_value ] end let tuple_id_mask = ( 1 lsl masked_tuple_id_num_bits ) - 1 module Pointer : sig type ' slots t = private int [ @@ deriving sexp_of , typerep ] include Invariant . S1 with type ' a t := ' a t val phys_compare : ' a t -> ' a t -> int val phys_equal : ' a t -> ' a t -> bool val null : unit -> _ t val is_null : _ t -> bool val create : header_index : int -> Tuple_id . t -> _ t val header_index : _ t -> int val masked_tuple_id : _ t -> int val slot_index : _ t -> ( _ , _ ) Slot . t -> int val first_slot_index : _ t -> int module Id : sig type t [ @@ deriving bin_io , sexp ] val to_int63 : t -> Int63 . t val of_int63 : Int63 . t -> t end val to_id : _ t -> Id . t val of_id_exn : Id . t -> _ t end = struct type ' slots t = int [ @@ deriving typerep ] let sexp_of_t _ t = Sexp . Atom ( sprintf " < Pool . Pointer . t : 0x % 08x " > t ) let phys_equal ( t1 : _ t ) t2 = phys_equal t1 t2 let phys_compare = compare let null ( ) = - max_slot - 1 let is_null t = phys_equal t ( null ( ) ) let % test _ = null ( ) + max_slot < 0 let create ~ header_index ( tuple_id : Tuple_id . t ) = header_index lor ( ( Tuple_id . to_int tuple_id land tuple_id_mask ) lsl array_index_num_bits ) ; ; let header_index_mask = ( 1 lsl array_index_num_bits ) - 1 let masked_tuple_id t = t lsr array_index_num_bits let header_index t = t land header_index_mask let invariant _ t = if not ( is_null t ) then assert ( header_index t > 0 ) let % test_unit _ = invariant ignore ( null ( ) ) let % test_unit _ = List . iter Tuple_id . examples ~ f ( : fun tuple_id -> invariant ignore ( create ~ header_index : 1 tuple_id ) ) ; ; let slot_index t slot = header_index t + slot let first_slot_index t = slot_index t Slot . t0 module Id = struct include Int63 let to_int63 t = t let of_int63 i = i end let to_id t = Id . of_int t let of_id_exn id = try let t = Id . to_int_exn id in if is_null t then t else ( let should_equal = create ~ header_index ( : header_index t ) ( Tuple_id . of_int ( masked_tuple_id t ) ) in if phys_equal t should_equal then t else failwiths ~ here [ :% here ] " should equal " should_equal [ % sexp_of : _ t ] ) with | exn -> failwiths ~ here [ :% here ] " Pointer . of_id_exn got strange id " ( id , exn ) [ % sexp_of : Id . t * exn ] ; ; end module Header : sig type t = private int [ @@ deriving sexp_of ] val null : t val is_null : t -> bool val free : next_free_header_index : int -> t val is_free : t -> bool val next_free_header_index : t -> int val used : Tuple_id . t -> t val is_used : t -> bool val tuple_id : t -> Tuple_id . t end = struct type t = int let null = 0 let is_null t = t = 0 let free ~ next_free_header_index = next_free_header_index let is_free t = t > 0 let next_free_header_index t = t let used ( tuple_id : Tuple_id . t ) = - 1 - ( tuple_id :> int ) let is_used t = t < 0 let tuple_id t = Tuple_id . of_int ( ( - t + 1 ) ) let % test_unit _ = List . iter Tuple_id . examples ~ f ( : fun id -> let t = used id in assert ( is_used t ) ; assert ( Tuple_id . equal ( tuple_id t ) id ) ) ; ; let sexp_of_t t = if is_null t then Sexp . Atom " null " else if is_free t then Sexp . ( List [ Atom " Free " ; Atom ( Int . to_string ( next_free_header_index t ) ) ] ) else Sexp . ( List [ Atom " Used " ; Atom ( Tuple_id . to_string ( tuple_id t ) ) ] ) ; ; end let metadata_index = 0 let start_of_tuples_index = 1 let max_capacity ~ slots_per_tuple = ( max_array_length - start_of_tuples_index ) / ( 1 + slots_per_tuple ) ; ; let % test_unit _ = for slots_per_tuple = 1 to max_slot do assert ( start_of_tuples_index + ( ( 1 + slots_per_tuple ) * max_capacity ~ slots_per_tuple ) <= max_array_length ) done ; ; module Metadata = struct type ' slots t = { slots_per_tuple : int ; capacity : int ; mutable length : int ; mutable next_id : Tuple_id . t ; mutable first_free : Header . t ; dummy : ( Obj . t Uniform_array . t [ @ sexp . opaque ] ) option } [ @@ deriving fields , sexp_of ] let array_indices_per_tuple t = 1 + t . slots_per_tuple let array_length t = start_of_tuples_index + ( t . capacity * array_indices_per_tuple t ) let header_index_to_tuple_num t ~ header_index = ( header_index - start_of_tuples_index ) / array_indices_per_tuple t ; ; let tuple_num_to_header_index t tuple_num = start_of_tuples_index + ( tuple_num * array_indices_per_tuple t ) ; ; let tuple_num_to_first_slot_index t tuple_num = tuple_num_to_header_index t tuple_num + 1 ; ; let is_full t = t . length = t . capacity end open Metadata type ' slots t = Obj . t Uniform_array . t let metadata ( type slots ) ( t : slots t ) = Uniform_array . unsafe_get t metadata_index |> ( Obj . obj : _ -> slots Metadata . t ) ; ; let length t = ( metadata t ) . length let sexp_of_t sexp_of_ty t = Metadata . sexp_of_t sexp_of_ty ( metadata t ) let unsafe_header t ~ header_index = Uniform_array . unsafe_get t header_index |> ( Obj . obj : _ -> Header . t ) ; ; let unsafe_set_header t ~ header_index ( header : Header . t ) = Uniform_array . unsafe_set_int_assuming_currently_int t header_index ( header :> int ) ; ; let header_index_is_in_bounds t ~ header_index = header_index >= start_of_tuples_index && header_index < Uniform_array . length t ; ; let unsafe_pointer_is_live t pointer = let header_index = Pointer . header_index pointer in let header = unsafe_header t ~ header_index in Header . is_used header && Tuple_id . to_int ( Header . tuple_id header ) land tuple_id_mask = Pointer . masked_tuple_id pointer ; ; let pointer_is_valid t pointer = header_index_is_in_bounds t ~ header_index ( : Pointer . header_index pointer ) && unsafe_pointer_is_live t pointer ; ; let id_of_pointer _t pointer = Pointer . to_id pointer let is_valid_header_index t ~ header_index = let metadata = metadata t in header_index_is_in_bounds t ~ header_index && 0 = ( header_index - start_of_tuples_index ) mod Metadata . array_indices_per_tuple metadata ; ; let pointer_of_id_exn t id = try let pointer = Pointer . of_id_exn id in if not ( Pointer . is_null pointer ) then ( let header_index = Pointer . header_index pointer in if not ( is_valid_header_index t ~ header_index ) then failwiths ~ here [ :% here ] " invalid header index " header_index [ % sexp_of : int ] ; if not ( unsafe_pointer_is_live t pointer ) then failwith " pointer not live " ) ; pointer with | exn -> failwiths ~ here [ :% here ] " Pool . pointer_of_id_exn got invalid id " ( id , t , exn ) [ % sexp_of : Pointer . Id . t * _ t * exn ] ; ; let invariant _invariant_a t : unit = try let metadata = metadata t in let check f field = f ( Field . get field metadata ) in Metadata . Fields . iter ~ slots_per_tuple ( : check ( fun slots_per_tuple -> assert ( slots_per_tuple > 0 ) ) ) ~ capacity : ( check ( fun capacity -> assert ( capacity >= 0 ) ; assert ( Uniform_array . length t = Metadata . array_length metadata ) ) ) ~ length : ( check ( fun length -> assert ( length >= 0 ) ; assert ( length <= metadata . capacity ) ) ) ~ next_id ( : check Tuple_id . invariant ) ~ first_free : ( check ( fun first_free -> let free = Array . create ~ len : metadata . capacity false in let r = ref first_free in while not ( Header . is_null ! r ) do let header = ! r in assert ( Header . is_free header ) ; let header_index = Header . next_free_header_index header in assert ( is_valid_header_index t ~ header_index ) ; let tuple_num = header_index_to_tuple_num metadata ~ header_index in if free . ( tuple_num ) then failwiths ~ here [ :% here ] " cycle in free list " tuple_num [ % sexp_of : int ] ; free . ( tuple_num ) <- true ; r := unsafe_header t ~ header_index done ) ) ~ dummy : ( check ( function | Some dummy -> assert ( Uniform_array . length dummy = metadata . slots_per_tuple ) | None -> for tuple_num = 0 to metadata . capacity - 1 do let header_index = tuple_num_to_header_index metadata tuple_num in let header = unsafe_header t ~ header_index in if Header . is_free header then ( let first_slot = tuple_num_to_first_slot_index metadata tuple_num in for slot = 0 to metadata . slots_per_tuple - 1 do assert ( Obj . is_int ( Uniform_array . get t ( first_slot + slot ) ) ) done ) done ) ) with | exn -> failwiths ~ here [ :% here ] " Pool . invariant failed " ( exn , t ) [ % sexp_of : exn * _ t ] ; ; let capacity t = ( metadata t ) . capacity let is_full t = Metadata . is_full ( metadata t ) let unsafe_add_to_free_list t metadata ~ header_index = unsafe_set_header t ~ header_index metadata . first_free ; metadata . first_free <- Header . free ~ next_free_header_index : header_index ; ; let set_metadata ( type slots ) ( t : slots t ) metadata = Uniform_array . set t metadata_index ( Obj . repr ( metadata : slots Metadata . t ) ) ; ; let create_array ( type slots ) ( metadata : slots Metadata . t ) : slots t = let t = Uniform_array . create_obj_array ~ len ( : Metadata . array_length metadata ) in set_metadata t metadata ; t ; ; let unsafe_init_range t metadata ~ lo ~ hi = ( match metadata . dummy with | None -> ( ) | Some dummy -> for tuple_num = lo to hi - 1 do Uniform_array . blit ~ src : dummy ~ src_pos : 0 ~ dst : t ~ dst_pos ( : tuple_num_to_first_slot_index metadata tuple_num ) ~ len : metadata . slots_per_tuple done ) ; for tuple_num = hi - 1 downto lo do unsafe_add_to_free_list t metadata ~ header_index ( : tuple_num_to_header_index metadata tuple_num ) done ; ; let create_with_dummy slots ~ capacity ~ dummy = if capacity < 0 then failwiths ~ here [ :% here ] " Pool . create got invalid capacity " capacity [ % sexp_of : int ] ; let slots_per_tuple = Slots . slots_per_tuple slots in let max_capacity = max_capacity ~ slots_per_tuple in if capacity > max_capacity then failwiths ~ here [ :% here ] " Pool . create got too large capacity " ( capacity , ` max max_capacity ) [ % sexp_of : int * [ ` max of int ] ] ; let metadata = { Metadata . slots_per_tuple ; capacity ; length = 0 ; next_id = Tuple_id . init ; first_free = Header . null ; dummy } in let t = create_array metadata in unsafe_init_range t metadata ~ lo : 0 ~ hi : capacity ; t ; ; let create ( type tuple ) ( slots : ( tuple , _ ) Slots . t ) ~ capacity ~ dummy = let dummy = if Slots . slots_per_tuple slots = 1 then Uniform_array . singleton ( Obj . repr ( dummy : tuple ) ) else ( Obj . magic ( dummy : tuple ) : Obj . t Uniform_array . t ) in create_with_dummy slots ~ capacity ~ dummy ( : Some dummy ) ; ; let destroy t = let metadata = metadata t in ( match metadata . dummy with | None -> for i = start_of_tuples_index to Uniform_array . length t - 1 do Uniform_array . unsafe_set t i ( Obj . repr 0 ) done | Some dummy -> for tuple_num = 0 to metadata . capacity - 1 do let header_index = tuple_num_to_header_index metadata tuple_num in unsafe_set_header t ~ header_index Header . null ; Uniform_array . blit ~ src : dummy ~ src_pos : 0 ~ dst : t ~ dst_pos ( : header_index + 1 ) ~ len : metadata . slots_per_tuple done ) ; let metadata = { Metadata . slots_per_tuple = metadata . slots_per_tuple ; capacity = 0 ; length = 0 ; next_id = metadata . next_id ; first_free = Header . null ; dummy = metadata . dummy } in set_metadata t metadata ; ; let [ @ cold ] grow ? capacity t = let { Metadata . slots_per_tuple ; capacity = old_capacity ; length ; next_id ; first_free = _ ; dummy } = metadata t in let capacity = min ( max_capacity ~ slots_per_tuple ) ( grow_capacity ~ capacity ~ old_capacity ) in if capacity = old_capacity then failwiths ~ here [ :% here ] " Pool . grow cannot grow pool ; capacity already at maximum " capacity [ % sexp_of : int ] ; let metadata = { Metadata . slots_per_tuple ; capacity ; length ; next_id ; first_free = Header . null ; dummy } in let t ' = create_array metadata in Uniform_array . blit ~ src : t ~ src_pos : start_of_tuples_index ~ dst : t ' ~ dst_pos : start_of_tuples_index ~ len ( : old_capacity * Metadata . array_indices_per_tuple metadata ) ; destroy t ; unsafe_init_range t ' metadata ~ lo : old_capacity ~ hi : capacity ; for tuple_num = old_capacity - 1 downto 0 do let header_index = tuple_num_to_header_index metadata tuple_num in let header = unsafe_header t ' ~ header_index in if not ( Header . is_used header ) then unsafe_add_to_free_list t ' metadata ~ header_index done ; t ' ; ; let [ @ cold ] raise_malloc_full t = failwiths ~ here [ :% here ] " Pool . malloc of full pool " t [ % sexp_of : _ t ] ; ; let malloc ( type slots ) ( t : slots t ) : slots Pointer . t = let metadata = metadata t in let first_free = metadata . first_free in if Header . is_null first_free then raise_malloc_full t ; let header_index = Header . next_free_header_index first_free in metadata . first_free <- unsafe_header t ~ header_index ; metadata . length <- metadata . length + 1 ; let tuple_id = metadata . next_id in unsafe_set_header t ~ header_index ( Header . used tuple_id ) ; metadata . next_id <- Tuple_id . next tuple_id ; Pointer . create ~ header_index tuple_id ; ; let unsafe_free ( type slots ) ( t : slots t ) ( pointer : slots Pointer . t ) = let metadata = metadata t in metadata . length <- metadata . length - 1 ; unsafe_add_to_free_list t metadata ~ header_index ( : Pointer . header_index pointer ) ; match metadata . dummy with | None -> let pos = Pointer . first_slot_index pointer in for i = 0 to metadata . slots_per_tuple - 1 do Uniform_array . unsafe_clear_if_pointer t ( pos + i ) done | Some dummy -> Uniform_array . unsafe_blit ~ src : dummy ~ src_pos : 0 ~ len : metadata . slots_per_tuple ~ dst : t ~ dst_pos ( : Pointer . first_slot_index pointer ) ; ; let free ( type slots ) ( t : slots t ) ( pointer : slots Pointer . t ) = if not ( pointer_is_valid t pointer ) then failwiths ~ here [ :% here ] " Pool . free of invalid pointer " ( pointer , t ) [ % sexp_of : _ Pointer . t * _ t ] ; unsafe_free t pointer ; ; let new1 t a0 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; pointer ; ; let new2 t a0 a1 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; pointer ; ; let new3 t a0 a1 a2 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; pointer ; ; let new4 t a0 a1 a2 a3 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; pointer ; ; let new5 t a0 a1 a2 a3 a4 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; pointer ; ; let new6 t a0 a1 a2 a3 a4 a5 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; pointer ; ; let new7 t a0 a1 a2 a3 a4 a5 a6 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; pointer ; ; let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; pointer ; ; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; pointer ; ; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; Uniform_array . unsafe_set t ( offset + 10 ) ( Obj . repr a9 ) ; pointer ; ; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; Uniform_array . unsafe_set t ( offset + 10 ) ( Obj . repr a9 ) ; Uniform_array . unsafe_set t ( offset + 11 ) ( Obj . repr a10 ) ; pointer ; ; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; Uniform_array . unsafe_set t ( offset + 10 ) ( Obj . repr a9 ) ; Uniform_array . unsafe_set t ( offset + 11 ) ( Obj . repr a10 ) ; Uniform_array . unsafe_set t ( offset + 12 ) ( Obj . repr a11 ) ; pointer ; ; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; Uniform_array . unsafe_set t ( offset + 10 ) ( Obj . repr a9 ) ; Uniform_array . unsafe_set t ( offset + 11 ) ( Obj . repr a10 ) ; Uniform_array . unsafe_set t ( offset + 12 ) ( Obj . repr a11 ) ; Uniform_array . unsafe_set t ( offset + 13 ) ( Obj . repr a12 ) ; pointer ; ; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = let pointer = malloc t in let offset = Pointer . header_index pointer in Uniform_array . unsafe_set t ( offset + 1 ) ( Obj . repr a0 ) ; Uniform_array . unsafe_set t ( offset + 2 ) ( Obj . repr a1 ) ; Uniform_array . unsafe_set t ( offset + 3 ) ( Obj . repr a2 ) ; Uniform_array . unsafe_set t ( offset + 4 ) ( Obj . repr a3 ) ; Uniform_array . unsafe_set t ( offset + 5 ) ( Obj . repr a4 ) ; Uniform_array . unsafe_set t ( offset + 6 ) ( Obj . repr a5 ) ; Uniform_array . unsafe_set t ( offset + 7 ) ( Obj . repr a6 ) ; Uniform_array . unsafe_set t ( offset + 8 ) ( Obj . repr a7 ) ; Uniform_array . unsafe_set t ( offset + 9 ) ( Obj . repr a8 ) ; Uniform_array . unsafe_set t ( offset + 10 ) ( Obj . repr a9 ) ; Uniform_array . unsafe_set t ( offset + 11 ) ( Obj . repr a10 ) ; Uniform_array . unsafe_set t ( offset + 12 ) ( Obj . repr a11 ) ; Uniform_array . unsafe_set t ( offset + 13 ) ( Obj . repr a12 ) ; Uniform_array . unsafe_set t ( offset + 14 ) ( Obj . repr a13 ) ; pointer ; ; let get t p slot = Obj . obj ( Uniform_array . get t ( Pointer . slot_index p slot ) ) let unsafe_get t p slot = Obj . obj ( Uniform_array . unsafe_get t ( Pointer . slot_index p slot ) ) ; ; let set t p slot x = Uniform_array . set t ( Pointer . slot_index p slot ) ( Obj . repr x ) let unsafe_set t p slot x = Uniform_array . unsafe_set t ( Pointer . slot_index p slot ) ( Obj . repr x ) ; ; let get_tuple ( type tuple ) ( t : ( tuple , _ ) Slots . t t ) pointer = let metadata = metadata t in let len = metadata . slots_per_tuple in if len = 1 then get t pointer Slot . t0 else ( Obj . magic ( Uniform_array . sub t ~ pos ( : Pointer . first_slot_index pointer ) ~ len : Obj . t Uniform_array . t ) : tuple ) ; ; end |
module Unsafe = struct include Pool let create slots ~ capacity = create_with_dummy slots ~ capacity ~ dummy : None end |
module Debug ( Pool : S ) = struct open Pool let check_invariant = ref true let show_messages = ref true let debug name ts arg sexp_of_arg sexp_of_result f = let prefix = " Pool . " in if ! check_invariant then List . iter ts ~ f ( : invariant ignore ) ; if ! show_messages then Debug . eprints ( concat [ prefix ; name ] ) arg sexp_of_arg ; let result_or_exn = Result . try_with f in if ! show_messages then Debug . eprints ( concat [ prefix ; name ; " result " ] ) result_or_exn [ % sexp_of : ( result , exn ) Result . t ] ; Result . ok_exn result_or_exn ; ; module Slots = Slots module Slot = Slot module Pointer = struct open Pointer type nonrec ' slots t = ' slots t [ @@ deriving sexp_of , typerep ] let phys_compare t1 t2 = debug " Pointer . phys_compare " [ ] ( t1 , t2 ) [ % sexp_of : _ t * _ t ] [ % sexp_of : int ] ( fun ( ) -> phys_compare t1 t2 ) ; ; let phys_equal t1 t2 = debug " Pointer . phys_equal " [ ] ( t1 , t2 ) [ % sexp_of : _ t * _ t ] [ % sexp_of : bool ] ( fun ( ) -> phys_equal t1 t2 ) ; ; let is_null t = debug " Pointer . is_null " [ ] t [ % sexp_of : _ t ] [ % sexp_of : bool ] ( fun ( ) -> is_null t ) ; ; let null = null module Id = struct open Id type nonrec t = t [ @@ deriving bin_io , sexp ] let of_int63 i = debug " Pointer . Id . of_int63 " [ ] i [ % sexp_of : Int63 . t ] [ % sexp_of : t ] ( fun ( ) -> of_int63 i ) ; ; let to_int63 t = debug " Pointer . Id . to_int63 " [ ] t [ % sexp_of : t ] [ % sexp_of : Int63 . t ] ( fun ( ) -> to_int63 t ) ; ; end end type nonrec ' slots t = ' slots t [ @@ deriving sexp_of ] let invariant = invariant let length = length let id_of_pointer t pointer = debug " id_of_pointer " [ t ] pointer [ % sexp_of : _ Pointer . t ] [ % sexp_of : Pointer . Id . t ] ( fun ( ) -> id_of_pointer t pointer ) ; ; let pointer_of_id_exn t id = debug " pointer_of_id_exn " [ t ] id [ % sexp_of : Pointer . Id . t ] [ % sexp_of : _ Pointer . t ] ( fun ( ) -> pointer_of_id_exn t id ) ; ; let pointer_is_valid t pointer = debug " pointer_is_valid " [ t ] pointer [ % sexp_of : _ Pointer . t ] [ % sexp_of : bool ] ( fun ( ) -> pointer_is_valid t pointer ) ; ; let create slots ~ capacity ~ dummy = debug " create " [ ] capacity [ % sexp_of : int ] [ % sexp_of : _ t ] ( fun ( ) -> create slots ~ capacity ~ dummy ) ; ; let max_capacity ~ slots_per_tuple = debug " max_capacity " [ ] slots_per_tuple [ % sexp_of : int ] [ % sexp_of : int ] ( fun ( ) -> max_capacity ~ slots_per_tuple ) ; ; let capacity t = debug " capacity " [ t ] t [ % sexp_of : _ t ] [ % sexp_of : int ] ( fun ( ) -> capacity t ) ; ; let grow ? capacity t = debug " grow " [ t ] ( ` capacity capacity ) [ % sexp_of : [ ` capacity of int option ] ] [ % sexp_of : _ t ] ( fun ( ) -> grow ? capacity t ) ; ; let is_full t = debug " is_full " [ t ] t [ % sexp_of : _ t ] [ % sexp_of : bool ] ( fun ( ) -> is_full t ) ; ; let unsafe_free t p = debug " unsafe_free " [ t ] p [ % sexp_of : _ Pointer . t ] [ % sexp_of : unit ] ( fun ( ) -> unsafe_free t p ) ; ; let free t p = debug " free " [ t ] p [ % sexp_of : _ Pointer . t ] [ % sexp_of : unit ] ( fun ( ) -> free t p ) ; ; let debug_new t f = debug " new " [ t ] ( ) [ % sexp_of : unit ] [ % sexp_of : _ Pointer . t ] f let new1 t a0 = debug_new t ( fun ( ) -> new1 t a0 ) let new2 t a0 a1 = debug_new t ( fun ( ) -> new2 t a0 a1 ) let new3 t a0 a1 a2 = debug_new t ( fun ( ) -> new3 t a0 a1 a2 ) let new4 t a0 a1 a2 a3 = debug_new t ( fun ( ) -> new4 t a0 a1 a2 a3 ) let new5 t a0 a1 a2 a3 a4 = debug_new t ( fun ( ) -> new5 t a0 a1 a2 a3 a4 ) let new6 t a0 a1 a2 a3 a4 a5 = debug_new t ( fun ( ) -> new6 t a0 a1 a2 a3 a4 a5 ) let new7 t a0 a1 a2 a3 a4 a5 a6 = debug_new t ( fun ( ) -> new7 t a0 a1 a2 a3 a4 a5 a6 ) let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = debug_new t ( fun ( ) -> new8 t a0 a1 a2 a3 a4 a5 a6 a7 ) ; ; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = debug_new t ( fun ( ) -> new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 ) ; ; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = debug_new t ( fun ( ) -> new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 ) ; ; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = debug_new t ( fun ( ) -> new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ) ; ; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = debug_new t ( fun ( ) -> new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 ) ; ; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = debug_new t ( fun ( ) -> new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 ) ; ; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = debug_new t ( fun ( ) -> new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 ) ; ; let get_tuple t pointer = debug " get_tuple " [ t ] pointer [ % sexp_of : _ Pointer . t ] [ % sexp_of : _ ] ( fun ( ) -> get_tuple t pointer ) ; ; let debug_get name f t pointer = debug name [ t ] pointer [ % sexp_of : _ Pointer . t ] [ % sexp_of : _ ] ( fun ( ) -> f t pointer ) ; ; let get t pointer slot = debug_get " get " get t pointer slot let unsafe_get t pointer slot = debug_get " unsafe_get " unsafe_get t pointer slot let debug_set name f t pointer slot a = debug name [ t ] pointer [ % sexp_of : _ Pointer . t ] [ % sexp_of : unit ] ( fun ( ) -> f t pointer slot a ) ; ; let set t pointer slot a = debug_set " set " set t pointer slot a let unsafe_set t pointer slot a = debug_set " unsafe_set " unsafe_set t pointer slot a end |
module Error_check ( Pool : S ) = struct open Pool module Slots = Slots module Slot = Slot module Pointer = struct type ' slots t = { mutable is_valid : bool ; pointer : ' slots Pointer . t } [ @@ deriving sexp_of , typerep ] let create pointer = { is_valid = true ; pointer } let null ( ) = { is_valid = false ; pointer = Pointer . null ( ) } let phys_compare t1 t2 = Pointer . phys_compare t1 . pointer t2 . pointer let phys_equal t1 t2 = Pointer . phys_equal t1 . pointer t2 . pointer let is_null t = Pointer . is_null t . pointer let follow t = if not t . is_valid then failwiths ~ here [ :% here ] " attempt to use invalid pointer " t [ % sexp_of : _ t ] ; t . pointer ; ; let invalidate t = t . is_valid <- false module Id = Pointer . Id end type ' slots t = ' slots Pool . t [ @@ deriving sexp_of ] let invariant = invariant let length = length let pointer_is_valid t { Pointer . is_valid ; pointer } = is_valid && pointer_is_valid t pointer ; ; let id_of_pointer t pointer = id_of_pointer t pointer . Pointer . pointer let pointer_of_id_exn t id = let pointer = pointer_of_id_exn t id in let is_valid = Pool . pointer_is_valid t pointer in { Pointer . is_valid ; pointer } ; ; let create = create let capacity = capacity let max_capacity = max_capacity let grow = grow let is_full = is_full let get_tuple t p = get_tuple t ( Pointer . follow p ) let get t p = get t ( Pointer . follow p ) let unsafe_get t p = unsafe_get t ( Pointer . follow p ) let set t p slot v = set t ( Pointer . follow p ) slot v let unsafe_set t p slot v = unsafe_set t ( Pointer . follow p ) slot v let unsafe_free t p = unsafe_free t ( Pointer . follow p ) ; Pointer . invalidate p ; ; let free t p = free t ( Pointer . follow p ) ; Pointer . invalidate p ; ; let new1 t a0 = Pointer . create ( Pool . new1 t a0 ) let new2 t a0 a1 = Pointer . create ( Pool . new2 t a0 a1 ) let new3 t a0 a1 a2 = Pointer . create ( Pool . new3 t a0 a1 a2 ) let new4 t a0 a1 a2 a3 = Pointer . create ( Pool . new4 t a0 a1 a2 a3 ) let new5 t a0 a1 a2 a3 a4 = Pointer . create ( Pool . new5 t a0 a1 a2 a3 a4 ) let new6 t a0 a1 a2 a3 a4 a5 = Pointer . create ( Pool . new6 t a0 a1 a2 a3 a4 a5 ) let new7 t a0 a1 a2 a3 a4 a5 a6 = Pointer . create ( Pool . new7 t a0 a1 a2 a3 a4 a5 a6 ) let new8 t a0 a1 a2 a3 a4 a5 a6 a7 = Pointer . create ( Pool . new8 t a0 a1 a2 a3 a4 a5 a6 a7 ) ; ; let new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 = Pointer . create ( Pool . new9 t a0 a1 a2 a3 a4 a5 a6 a7 a8 ) ; ; let new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 = Pointer . create ( Pool . new10 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 ) ; ; let new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 = Pointer . create ( Pool . new11 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 ) ; ; let new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 = Pointer . create ( Pool . new12 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 ) ; ; let new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 = Pointer . create ( Pool . new13 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 ) ; ; let new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 = Pointer . create ( Pool . new14 t a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 ) ; ; end |
let big = 32 * 1024 |
let check ~ expected ~ f = Caml_modify . reset ( ) ; f ( ) ; Caml_modify . count ( ) = expected ; ; |
let ( ) = let array_imm = Array . init big ~ f : Fn . id in let array_obj = Array . init big ~ f ( : fun i -> ref i ) in let v = ref ( Random . int 42 ) in let array_imm_set ( a : int array ) i ( x : int ) = a . ( i ) <- x in let array_obj_set a i x = a . ( i ) <- x in assert ( check ~ expected : 0 ~ f ( : fun ( ) -> array_imm_set array_imm 0 0 ) ) ; assert ( check ~ expected : 1 ~ f ( : fun ( ) -> array_obj_set array_imm 0 0 ) ) ; assert ( check ~ expected : 1 ~ f ( : fun ( ) -> array_obj_set array_obj 0 v ) ) ; ; |
let ( ) = let open Tuple_pool in let p = create Slots . t3 ~ capacity : 3 ~ dummy ( : Pointer . null ( ) , 0 , " " ) in let e = new3 p ( Pointer . null ( ) ) 0 " " in let v = Int . to_string ( Random . int 42 ) in let n = if Obj . is_int ( Obj . repr e ) then 0 else 1 in assert ( check ~ expected : n ~ f ( : fun ( ) -> set p e Slot . t0 e ) ) ; assert ( check ~ expected : 0 ~ f ( : fun ( ) -> set p e Slot . t1 0 ) ) ; assert ( check ~ expected : 1 ~ f ( : fun ( ) -> set p e Slot . t2 v ) ) ; ; |
module type S = sig module Slots : Tuple_type . Slots module Slot : Tuple_type . Slot module Pointer : sig type ' slots t [ @@ deriving sexp_of , typerep ] val null : unit -> _ t val is_null : _ t -> bool val phys_compare : ' a t -> ' a t -> int val phys_equal : ' a t -> ' a t -> bool module Id : sig type t [ @@ deriving bin_io , sexp ] val to_int63 : t -> Int63 . t val of_int63 : Int63 . t -> t end end type ' slots t [ @@ deriving sexp_of ] include Invariant . S1 with type ' a t := ' a t val pointer_is_valid : ' slots t -> ' slots Pointer . t -> bool val id_of_pointer : ' slots t -> ' slots Pointer . t -> Pointer . Id . t val pointer_of_id_exn : ' slots t -> Pointer . Id . t -> ' slots Pointer . t val create : ( ( ' tuple , _ ) Slots . t as ' slots ) -> capacity : int -> dummy ' : tuple -> ' slots t val max_capacity : slots_per_tuple : int -> int val capacity : _ t -> int val length : _ t -> int val grow : ? capacity : int -> ' a t -> ' a t val is_full : _ t -> bool val free : ' slots t -> ' slots Pointer . t -> unit val unsafe_free : ' slots t -> ' slots Pointer . t -> unit val new1 : ( ' a0 Slots . t1 as ' slots ) t -> ' a0 -> ' slots Pointer . t val new2 : ( ( ' a0 , ' a1 ) Slots . t2 as ' slots ) t -> ' a0 -> ' a1 -> ' slots Pointer . t val new3 : ( ( ' a0 , ' a1 , ' a2 ) Slots . t3 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' slots Pointer . t val new4 : ( ( ' a0 , ' a1 , ' a2 , ' a3 ) Slots . t4 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' slots Pointer . t val new5 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 ) Slots . t5 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' slots Pointer . t val new6 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 ) Slots . t6 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' slots Pointer . t val new7 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 ) Slots . t7 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' slots Pointer . t val new8 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 ) Slots . t8 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' slots Pointer . t val new9 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 ) Slots . t9 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' slots Pointer . t val new10 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 ) Slots . t10 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' a9 -> ' slots Pointer . t val new11 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 ) Slots . t11 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' a9 -> ' a10 -> ' slots Pointer . t val new12 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 ) Slots . t12 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' a9 -> ' a10 -> ' a11 -> ' slots Pointer . t val new13 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 ) Slots . t13 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' a9 -> ' a10 -> ' a11 -> ' a12 -> ' slots Pointer . t val new14 : ( ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 , ' a13 ) Slots . t14 as ' slots ) t -> ' a0 -> ' a1 -> ' a2 -> ' a3 -> ' a4 -> ' a5 -> ' a6 -> ' a7 -> ' a8 -> ' a9 -> ' a10 -> ' a11 -> ' a12 -> ' a13 -> ' slots Pointer . t val get_tuple : ( ( ' tuple , _ ) Slots . t as ' slots ) t -> ' slots Pointer . t -> ' tuple val get : ( ( _ , ' variant ) Slots . t as ' slots ) t -> ' slots Pointer . t -> ( ' variant , ' slot ) Slot . t -> ' slot val unsafe_get : ( ( _ , ' variant ) Slots . t as ' slots ) t -> ' slots Pointer . t -> ( ' variant , ' slot ) Slot . t -> ' slot val set : ( ( _ , ' variant ) Slots . t as ' slots ) t -> ' slots Pointer . t -> ( ' variant , ' slot ) Slot . t -> ' slot -> unit val unsafe_set : ( ( _ , ' variant ) Slots . t as ' slots ) t -> ' slots Pointer . t -> ( ' variant , ' slot ) Slot . t -> ' slot -> unit end |
module type Tuple_pool = sig module Tuple_type = Tuple_type module type S = S include S with type ' a Pointer . t = private int module Unsafe : sig include S with type ' a Pointer . t = private int val create : ( ( _ , _ ) Slots . t as ' slots ) -> capacity : int -> ' slots t end module Debug ( Tuple_pool : S ) : sig include S with type ' a Pointer . t = ' a Tuple_pool . Pointer . t with type Pointer . Id . t = Tuple_pool . Pointer . Id . t with type ' a t = ' a Tuple_pool . t val check_invariant : bool ref val show_messages : bool ref end module Error_check ( Tuple_pool : S ) : S end |
let get0 ( ) = if Random . bool ( ) then 0 else 0 |
let get1 ( ) = if Random . bool ( ) then 1 else 1 |
let f1_0 = Bench . Test . create ~ name " : tuple1_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a , b = match x , y with | 0 , 0 -> 1 , 1 | x , y -> x + 1 , y + 1 in ignore ( a + b ) ) ; ; |
let f1_1 = Bench . Test . create ~ name " : tuple1_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a , b = match x , y with | 0 , 0 -> 1 , 1 | x , y -> x + 1 , y + 1 in ignore ( a + b ) ) ; ; |
let f2_0 = Bench . Test . create ~ name " : tuple2_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | 0 , 0 -> 1 + 1 | x , y -> x + 1 + y + 1 in ignore a ) ; ; |
let f2_1 = Bench . Test . create ~ name " : tuple2_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | 0 , 0 -> 1 + 1 | x , y -> x + 1 + y + 1 in ignore a ) ; ; |
let f3 = Bench . Test . create ~ name " : tuple3 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a , b = match x , y with | a , b -> a , b in ignore ( a + b ) ) ; ; |
let f4 = Bench . Test . create ~ name " : tuple4 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a , b = x , y in ignore ( a + b ) ) ; ; |
let f5 = Bench . Test . create ~ name " : tuple5 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let c = x , y in let a , b = c in ignore ( a + b ) ) ; ; |
let f6_0 = Bench . Test . create ~ name " : tuple6_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | _x -> 20 in ignore a ) ; ; |
let f6_1 = Bench . Test . create ~ name " : tuple6_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | _x -> 20 in ignore a ) ; ; |
let f7_0 = Bench . Test . create ~ name " : tuple7_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | x -> ignore x ; 20 in ignore a ) ; ; |
let f7_1 = Bench . Test . create ~ name " : tuple7_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | x -> ignore x ; 20 in ignore a ) ; ; |
let f8_0 = Bench . Test . create ~ name " : tuple8_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | a , b -> ignore a ; ignore b ; 20 in ignore a ) ; ; |
let f8_1 = Bench . Test . create ~ name " : tuple8_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | 1 , 1 -> 10 | a , b -> ignore a ; ignore b ; 20 in ignore a ) ; ; |
let f9_0 = Bench . Test . create ~ name " : tuple9_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | ( 1 , 1 ) as p -> ignore p ; 10 | x , y -> x + y in ignore a ) ; ; |
let f9_1 = Bench . Test . create ~ name " : tuple9_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | ( 1 , 1 ) as p -> ignore p ; 10 | x , y -> x + y in ignore a ) ; ; |
let f10_0 = Bench . Test . create ~ name " : tuple10_0 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | ( 1 , y ) as p -> ignore p ; 1 + y | x , y -> x + y in ignore a ) ; ; |
let f10_1 = Bench . Test . create ~ name " : tuple10_1 " ( let x = get1 ( ) in let y = get1 ( ) in fun ( ) -> let a = match x , y with | ( 1 , y ) as p -> ignore p ; 1 + y | x , y -> x + y in ignore a ) ; ; |
let f11 = Bench . Test . create ~ name " : tuple11 " ( let x = get0 ( ) in let y = get0 ( ) in fun ( ) -> let a = match x , y with | ( x , y ) as p -> ignore p ; x + y in ignore a ) ; ; |
let tests = [ f1_0 ; f1_1 ; f2_0 ; f2_1 ; f3 ; f4 ; f5 ; f6_0 ; f6_1 ; f7_0 ; f7_1 ; f8_0 ; f8_1 ; f9_0 ; f9_1 ; f10_0 ; f10_1 ; f11 ] ; ; |
let command = Bench . make_command tests |
module Slots = struct type u_ = { slots_per_tuple : int } [ @@ deriving sexp_of ] type ( ' tuple , ' variant ) u = u_ [ @@ deriving sexp_of ] type t_ = [ ` Slots of u_ ] [ @@ deriving sexp_of ] type ( ' tuple , ' variant ) t = t_ [ @@ deriving sexp_of ] let slots_per_tuple ( ` Slots { slots_per_tuple = n } ) = n type ' a0 t1 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 ) t2 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 ) t3 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 ) t4 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 ) t5 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 ) t6 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 ) t7 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 ) t8 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 ) t9 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 ) t10 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 ) t11 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 ) t12 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 ) t13 = t_ [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 , ' a13 ) t14 = t_ [ @@ deriving sexp_of ] let t1 = ` Slots { slots_per_tuple = 1 } let t2 = ` Slots { slots_per_tuple = 2 } let t3 = ` Slots { slots_per_tuple = 3 } let t4 = ` Slots { slots_per_tuple = 4 } let t5 = ` Slots { slots_per_tuple = 5 } let t6 = ` Slots { slots_per_tuple = 6 } let t7 = ` Slots { slots_per_tuple = 7 } let t8 = ` Slots { slots_per_tuple = 8 } let t9 = ` Slots { slots_per_tuple = 9 } let t10 = ` Slots { slots_per_tuple = 10 } let t11 = ` Slots { slots_per_tuple = 11 } let t12 = ` Slots { slots_per_tuple = 12 } let t13 = ` Slots { slots_per_tuple = 13 } let t14 = ` Slots { slots_per_tuple = 14 } end |
module type Slots = sig type ( ' tuple , ' variant ) u type ( ' tuple , ' variant ) t = [ ` Slots of ( ' tuple , ' variant ) u ] [ @@ deriving sexp_of ] val slots_per_tuple : ( _ , _ ) t -> int type ' a0 t1 = ( ' a0 , [ ` S0 of ' a0 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 ) t2 = ( ' a0 * ' a1 , [ ` S0 of ' a0 | ` S1 of ' a1 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 ) t3 = ( ' a0 * ' a1 * ' a2 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 ) t4 = ( ' a0 * ' a1 * ' a2 * ' a3 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 ) t5 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 ) t6 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 ) t7 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 ) t8 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 ) t9 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 ) t10 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 * ' a9 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 | ` S9 of ' a9 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 ) t11 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 * ' a9 * ' a10 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 | ` S9 of ' a9 | ` S10 of ' a10 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 ) t12 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 * ' a9 * ' a10 * ' a11 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 | ` S9 of ' a9 | ` S10 of ' a10 | ` S11 of ' a11 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 ) t13 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 * ' a9 * ' a10 * ' a11 * ' a12 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 | ` S9 of ' a9 | ` S10 of ' a10 | ` S11 of ' a11 | ` S12 of ' a12 ] ) t [ @@ deriving sexp_of ] type ( ' a0 , ' a1 , ' a2 , ' a3 , ' a4 , ' a5 , ' a6 , ' a7 , ' a8 , ' a9 , ' a10 , ' a11 , ' a12 , ' a13 ) t14 = ( ' a0 * ' a1 * ' a2 * ' a3 * ' a4 * ' a5 * ' a6 * ' a7 * ' a8 * ' a9 * ' a10 * ' a11 * ' a12 * ' a13 , [ ` S0 of ' a0 | ` S1 of ' a1 | ` S2 of ' a2 | ` S3 of ' a3 | ` S4 of ' a4 | ` S5 of ' a5 | ` S6 of ' a6 | ` S7 of ' a7 | ` S8 of ' a8 | ` S9 of ' a9 | ` S10 of ' a10 | ` S11 of ' a11 | ` S12 of ' a12 | ` S13 of ' a13 ] ) t [ @@ deriving sexp_of ] val t1 : _ t1 val t2 : ( _ , _ ) t2 val t3 : ( _ , _ , _ ) t3 val t4 : ( _ , _ , _ , _ ) t4 val t5 : ( _ , _ , _ , _ , _ ) t5 val t6 : ( _ , _ , _ , _ , _ , _ ) t6 val t7 : ( _ , _ , _ , _ , _ , _ , _ ) t7 val t8 : ( _ , _ , _ , _ , _ , _ , _ , _ ) t8 val t9 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ ) t9 val t10 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ , _ ) t10 val t11 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ ) t11 val t12 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ ) t12 val t13 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ ) t13 val t14 : ( _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ , _ ) t14 end |
module type Slot = sig type ( ' variant , ' a ) t [ @@ deriving sexp_of ] val equal : ( ' v , ' a ) t -> ( ' v , ' a ) t -> bool val t0 : ( [ > ` S0 of ' a ] , ' a ) t val t1 : ( [ > ` S1 of ' a ] , ' a ) t val t2 : ( [ > ` S2 of ' a ] , ' a ) t val t3 : ( [ > ` S3 of ' a ] , ' a ) t val t4 : ( [ > ` S4 of ' a ] , ' a ) t val t5 : ( [ > ` S5 of ' a ] , ' a ) t val t6 : ( [ > ` S6 of ' a ] , ' a ) t val t7 : ( [ > ` S7 of ' a ] , ' a ) t val t8 : ( [ > ` S8 of ' a ] , ' a ) t val t9 : ( [ > ` S9 of ' a ] , ' a ) t val t10 : ( [ > ` S10 of ' a ] , ' a ) t val t11 : ( [ > ` S11 of ' a ] , ' a ) t val t12 : ( [ > ` S12 of ' a ] , ' a ) t val t13 : ( [ > ` S13 of ' a ] , ' a ) t end |
let config = { config with site_width = 0 . 25 . * config . site_width ; site_height = 0 . 25 . * config . site_height ; state_height = 0 . 7 . * config . state_height ; state_width = 0 . 7 . * config . state_width ; free_width = 0 . 6 . * config . free_width ; free_height = 0 . 4 . * config . free_height ; bound_height = 0 . 3 . * config . bound_height ; rule_length = 0 . 65 . * config . rule_length ; rule_margin = 0 . 2 . * config . rule_margin ; show_agent_names = false ; show_site_names = false ; show_state_names = false ; txt_font = 10 ; } |
let build_rule ? mul = let h = match mul with | None -> 0 . 8 | Some f -> 0 . 8 . * f in build_rule ~ hgap ( : Some h ) let [ a , [ a_x , [ ] ; ] ; b , [ b_x , [ ] ; b_y , [ b_y_u ; b_y_p ] ; ] ] , signature = add_in_signature [ " $\ text { \ agent { A } { } } " , [ $ Shape " circle " ; Width ( 0 . 13 . * config . agent_width ) ; Height ( 0 . 16 . * config . agent_width ) ] , [ " $\ text { \ site { l } { } { } } " , [ $ Direction e ] , [ ] ] ; " $\ text { \ agent { B } { } } " , [ $ Shape " rectangle " ; Height ( 0 . 24 . * config . agent_height ) ; Width ( 0 . 15 . * config . agent_width ) ] , [ " $\ text { \ site { r } { } { } } " , [ $ Direction w ] , [ ] ; " $\ text { \ site { l } { } { } } " , [ $ Direction e ] , [ " u " , [ Direction ne ] ; " p " , [ Direction ne ] ] ] ] ( snd ( init config ) ) |
let [ agent_a , [ site_a , _ ] ; agent_b , [ site_b_x , _ ] ] , couple = add_in_graph [ a , 0 . , 0 . , [ ] , [ a_x , [ ] , [ ] ] ; b , 0 . 6 , 0 . , [ ] , [ b_x , [ ] , [ ] ] ; ] signature |
let add_bond s1 s2 graph = let graph = add_link_list [ s1 , s2 ] graph in ( [ ] , [ ] , [ ] ) , graph |
let add_free s1 s2 graph = let _ , graph = add_free_list [ s1 , [ ] ; s2 , [ ] ] graph in ( [ ] , [ ] , [ ] ) , graph |
let bind ? directives ? file g s1 s2 = build_rule ? directives ? file g ( add_free s1 s2 ) ( add_bond s1 s2 ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.