text
stringlengths
0
601k
let mkVar x = mkNode zero x one
let andslot1 = Array . make cacheSize 0
let andslot2 = Array . make cacheSize 0
let andslot3 = Array . make cacheSize zero
let xorslot1 = Array . make cacheSize 0
let xorslot2 = Array . make cacheSize 0
let xorslot3 = Array . make cacheSize zero
let notslot1 = Array . make cacheSize 0
let notslot2 = Array . make cacheSize one
let hash x y = ( ( x lsl 1 ) + y ) mod cacheSize
let rec not n = Zero -> One in if id = notslot1 . ( h ) then notslot2 . ( h ) else let f = mkNode ( not l ) v ( not r ) in notslot1 . ( h ) <- id ; notslot2 . ( h ) <- f ; f
let rec and2 n1 n2 = Node ( l1 , v1 , i1 , r1 ) -> ( match n2 with Node ( l2 , v2 , i2 , r2 ) -> let h = hash i1 i2 in if i1 = andslot1 . ( h ) && i2 = andslot2 . ( h ) then andslot3 . ( h ) else let f = match cmpVar v1 v2 with EQUAL -> mkNode ( and2 l1 l2 ) v1 ( and2 r1 r2 ) | LESS -> mkNode ( and2 l1 n2 ) v1 ( and2 r...
let rec xor n1 n2 = Node ( l1 , v1 , i1 , r1 ) -> ( match n2 with Node ( l2 , v2 , i2 , r2 ) -> let h = hash i1 i2 in if i1 = andslot1 . ( h ) && i2 = andslot2 . ( h ) then andslot3 . ( h ) else let f = match cmpVar v1 v2 with EQUAL -> mkNode ( xor l1 l2 ) v1 ( xor r1 r2 ) | LESS -> mkNode ( xor l1 n2 ) v1 ( xor r1 n2 ...
let hwb n = let rec h i j = if i = j then mkVar i else xor ( and2 ( not ( mkVar j ) ) ( h i ( j - 1 ) ) ) ( and2 ( mkVar j ) ( g i ( j - 1 ) ) ) and g i j = if i = j then mkVar i else xor ( and2 ( not ( mkVar i ) ) ( h ( i + 1 ) j ) ) ( and2 ( mkVar i ) ( g ( i + 1 ) j ) ) in h 0 ( n - 1 )
let seed = ref 0
let random ( ) = seed := ! seed * 25173 + 17431 ; ! seed land 1 > 0
let random_vars n = let vars = Array . make n false in for i = 0 to n - 1 do vars . ( i ) <- random ( ) done ; vars
let test_hwb bdd vars = let ntrue = ref 0 in for i = 0 to Array . length vars - 1 do if vars . ( i ) then incr ntrue done ; eval bdd vars = ( if ! ntrue > 0 then vars . ( ! ntrue - 1 ) else false )
let main ( ) = let n = if Array . length Sys . argv >= 2 then int_of_string Sys . argv . ( 1 ) else 20 in let ntests = if Array . length Sys . argv >= 3 then int_of_string Sys . argv . ( 2 ) else 10 in let bdd = hwb n in let succeeded = ref true in for i = 1 to ntests do succeeded := ! succeeded && test_hwb bdd ( rando...
type modifier = | Set | Clear | Set_all
type token = | Letter of char * modifier option | Num of int * int * modifier
let parse_warnings s = let error ( ) = raise ( Arg . Bad " Ill - formed list of warnings " ) in let rec get_num n i = if i >= String . length s then i , n else match s . [ i ] with | ' 0 ' . . ' 9 ' -> get_num ( 10 * n + Char . code s . [ i ] - Char . code ' 0 ' ) ( i + 1 ) | _ -> i , n in let get_range i = let i , n1 ...
let string_of_mod = function | Set -> " " + | Clear -> " " - | Set_all -> " " @
let print_warnings toks = List . iter ( function | Letter ( c , None ) -> Testprint . string " letter " ( String . make 1 c ) | Letter ( c , Some m ) -> Testprint . string " letter " ( string_of_mod m ^ String . make 1 c ) | Num ( n1 , n2 , m ) -> Testprint . string " num " ( sprintf " % s % d . . % d " ( string_of_mod...
let f ( ) = parse_warnings " + a - 4 - 32 . . 42 + 66 . . 69 - x " |> print_warnings
type term = Var of int | Prop of head * term list { name : string ; mutable props : ( term * term ) list }
let rec print_term = function Var v -> print_string " v " ; print_int v | Prop ( head , argl ) -> print_string " ( " ; print_string head . name ; List . iter ( fun t -> print_string " " ; print_term t ) argl ; print_string " ) "
let lemmas = ref ( [ ] : head list )
let get name = let rec get_rec = function hd1 :: hdl -> if hd1 . name = name then hd1 else get_rec hdl | [ ] -> let entry = { name = name ; props = [ ] } in lemmas := entry :: ! lemmas ; entry in get_rec ! lemmas
let add_lemma = function | Prop ( _ , [ ( Prop ( headl , _ ) as left ) ; right ] ) -> headl . props <- ( left , right ) :: headl . props | _ -> assert false
type subst = Bind of int * term
let get_binding v list = let rec get_rec = function [ ] -> failwith " unbound " | Bind ( w , t ) :: rest -> if v = w then t else get_rec rest in get_rec list
let apply_subst alist term = let rec as_rec = function Var v -> begin try get_binding v alist with Failure _ -> term end | Prop ( head , argl ) -> Prop ( head , List . map as_rec argl ) in as_rec term
let rec unify term1 term2 = unify1 term1 term2 [ ] match term2 with Var v -> begin try if get_binding v unify_subst = term1 then unify_subst else raise Unify with Failure _ -> Bind ( v , term1 ) :: unify_subst end | Prop ( head2 , argl2 ) -> match term1 with Var _ -> raise Unify | Prop ( head1 , argl1 ) -> if head1 == ...
let rec rewrite = function Var _ as term -> term | Prop ( head , argl ) -> rewrite_with_lemmas ( Prop ( head , List . map rewrite argl ) ) head . props match lemmas with [ ] -> term | ( t1 , t2 ) :: rest -> try rewrite ( apply_subst ( unify term t1 ) t2 ) with Unify -> rewrite_with_lemmas term rest
type cterm = CVar of int | CProp of string * cterm list
let rec cterm_to_term = function CVar v -> Var v | CProp ( p , l ) -> Prop ( get p , List . map cterm_to_term l )
let add t = add_lemma ( cterm_to_term t )
let _ = ( " equal " , [ CProp ( " compile " , [ CVar 5 ] ) ; CProp ( " reverse " , [ CProp ( " codegen " , [ CProp ( " optimize " , [ CVar 5 ] ) ; CProp ( " nil " , [ ] ) ] ) ] ) ] ) ) ; ( " equal " , [ CProp ( " eqp " , [ CVar 23 ; CVar 24 ] ) ; CProp ( " equal " , [ CProp ( " fix " , [ CVar 23 ] ) ; CProp ( " fix " ,...
let truep x lst = match x with Prop ( head , _ ) -> head . name = " true " || List . mem x lst | _ -> List . mem x lst match x with Prop ( head , _ ) -> head . name = " false " || List . mem x lst | _ -> List . mem x lst
let rec tautologyp x true_lst false_lst = if truep x true_lst then true else if falsep x false_lst then false else begin match x with Var _ -> false | Prop ( head , [ test ; yes ; no ] ) -> if head . name = " if " then if truep test true_lst then tautologyp yes true_lst false_lst else if falsep test false_lst then taut...
let tautp x = let y = rewrite x in tautologyp y [ ] [ ]
let subst = CProp ( " f " , [ CProp ( " plus " , [ CProp ( " plus " , [ CVar 0 ; CVar 1 ] ) ; CProp ( " plus " , [ CVar 2 ; CProp ( " zero " , [ ] ) ] ) ] ) ] ) ) ) ; Bind ( 24 , cterm_to_term ( CProp ( " f " , [ CProp ( " times " , [ CProp ( " times " , [ CVar 0 ; CVar 1 ] ) ; CProp ( " plus " , [ CVar 2 ; CVar 3 ] ) ...
let term = cterm_to_term ( CProp ( " implies " , [ CProp ( " and " , [ CProp ( " implies " , [ CVar 23 ; CVar 24 ] ) ; CProp ( " and " , [ CProp ( " implies " , [ CVar 24 ; CVar 25 ] ) ; CProp ( " and " , [ CProp ( " implies " , [ CVar 25 ; CVar 20 ] ) ; CProp ( " implies " , [ CVar 20 ; CVar 22 ] ) ] ) ] ) ] ) ; CProp...
let _ = let t0 = Testprint . clock64 ( ) in let ok = ref true in for i = 1 to 10 do if not ( tautp ( apply_subst subst term ) ) then ok := false done ; if ! ok then print_string " Proved !\ n " else print_string " Cannot prove !\ n " ; let t1 = Testprint . clock64 ( ) in Printf . eprintf " time =% Ld ms \ n " %! ( Int6...
let f _ = raise End_of_file in try f 0 ; raise Not_found ; ; ;
let x = 5 in
let y = 2 in
let z = 1 in
let f _ = ignore x ; ignore y ; z in ; ;
let x = 5 in
let y = 2 in
let z = 1 in
let a = 4 in
let f _ = ignore x ; ignore y ; ignore z ; a in ; ;
let x = 5 in
let y = 2 in
let z = 1 in
let a = 4 in
let b = 3 in
let f _ = ignore x ; ignore y ; ignore z ; ignore a ; b in ; ;
let x = 5 in
let y = 4 in
let z = 3 in
let f _ = z + y + x in ; ;
let x = 5 in
let y = 4 in
let z = 3 in
let a = 2 in
let f _ = a + z + y + x in ; ;
let x = 5 in
let y = 4 in
let z = 3 in
let a = 2 in
let b = 1 in
let f _ = b + a + z + y + x in ; ;
type t = { mutable a : int ; mutable b : int ; mutable c : int ; mutable d : int ; } ; ;
let x = { a = 7 ; b = 6 ; c = 5 ; d = 4 } in x ; ;
type t = { mutable a : int ; mutable b : int ; mutable c : int ; mutable d : int ; mutable e : int ; } ; ;
let x = { a = 7 ; b = 6 ; c = 5 ; d = 4 ; e = 5 } in x ; ;
let test number result expected = if result = expected then Printf . printf " Test % d passed . \ n " number else Printf . printf " Test % d FAILED . \ n " number ; flush stdout
module rec A : sig type t = Leaf of int | Node of ASet . t val compare : t -> t -> int end = struct type t = Leaf of int | Node of ASet . t let compare x y = match ( x , y ) with ( Leaf i , Leaf j ) -> Stdlib . compare i j | ( Leaf i , Node t ) -> - 1 | ( Node s , Leaf j ) -> 1 | ( Node s , Node t ) -> ASet . compare s...
let _ = let x = A . Node ( ASet . add ( A . Leaf 3 ) ( ASet . singleton ( A . Leaf 2 ) ) ) in let y = A . Node ( ASet . add ( A . Leaf 1 ) ( ASet . singleton x ) ) in test 10 ( A . compare x x ) 0 ; test 11 ( A . compare x ( A . Leaf 3 ) ) 1 ; test 12 ( A . compare ( A . Leaf 0 ) x ) ( - 1 ) ; test 13 ( A . compare y y...
module rec Fib : sig val f : int -> int end = struct let f x = if x < 2 then 1 else Fib . f ( x - 1 ) + Fib . f ( x - 2 ) end ; ;
let _ = test 20 ( Fib . f 10 ) 89 ; ;
module rec Fib2 : sig val f : int -> int end = struct let rec g x = Fib2 . f ( x - 1 ) + Fib2 . f ( x - 2 ) and f x = if x < 2 then 1 else g x end ; ;
let _ = test 21 ( Fib2 . f 10 ) 89 ; ;
let _ = let res = try let module A = struct module rec Bad : sig val f : int -> int end = struct let f = let y = Bad . f 5 in fun x -> x + y end end in false with Undefined_recursive_module _ -> true in test 30 res true ; ;
module rec After : sig val x : int end = struct let x = Before . x + 1 end : sig val x : int end = struct let x = 3 end ; ;
let _ = test 40 After . x 4 ; ;
module rec Strengthen : sig type t val f : t -> t end = struct type t = A | B let _ = ( A : Strengthen . t ) let f x = if true then A else Strengthen . f B end ; ;
module rec Strengthen2 : sig type t val f : t -> t module M : sig type u end module R : sig type v end end = struct type t = A | B let _ = ( A : Strengthen2 . t ) let f x = if true then A else Strengthen2 . f B module M = struct type u = C let _ = ( C : Strengthen2 . M . u ) end module rec R : sig type v = Strengthen2 ...
module rec PolyRec : sig type ' a t = Leaf of ' a | Node of ' a list t * ' a list t val depth : ' a t -> int end = struct type ' a t = Leaf of ' a | Node of ' a list t * ' a list t let x = ( PolyRec . Leaf 1 : int t ) let depth = function Leaf x -> 0 | Node ( l , r ) -> 1 + max ( PolyRec . depth l ) ( PolyRec . depth r...
module rec Expr : sig type t = Var of string | Const of int | Add of t * t | Binding of Binding . t * t val make_let : string -> t -> t -> t val fv : t -> StringSet . t val simpl : t -> t end = struct type t = Var of string | Const of int | Add of t * t | Binding of Binding . t * t let make_let id e1 e2 = Binding ( [ i...
let _ = let e = Expr . make_let " x " ( Expr . Add ( Expr . Var " y " , Expr . Const 0 ) ) ( Expr . Var " x " ) in let e ' = Expr . make_let " x " ( Expr . Var " y " ) ( Expr . Var " x " ) in test 50 ( StringSet . elements ( Expr . fv e ) ) [ " y " ] ; test 51 ( Expr . simpl e ) e ' ; ;
module type ORDERED = sig type t val eq : t -> t -> bool val lt : t -> t -> bool val leq : t -> t -> bool end
module type HEAP = sig module Elem : ORDERED type heap val empty : heap val isEmpty : heap -> bool val insert : Elem . t -> heap -> heap val merge : heap -> heap -> heap val findMin : heap -> Elem . t val deleteMin : heap -> heap end
module Bootstrap ( MakeH : functor ( Element : ORDERED ) -> HEAP with module Elem = Element ) ( Element : ORDERED ) : HEAP with module Elem = Element = struct module Elem = Element module rec BE : sig type t = E | H of Elem . t * PrimH . heap val eq : t -> t -> bool val lt : t -> t -> bool val leq : t -> t -> bool end ...
module LeftistHeap ( Element : ORDERED ) : HEAP with module Elem = Element = struct module Elem = Element type heap = E | T of int * Elem . t * heap * heap let rank = function E -> 0 | T ( r , _ , _ , _ ) -> r let make x a b = if rank a >= rank b then T ( rank b + 1 , x , a , b ) else T ( rank a + 1 , x , b , a ) let e...
module Ints = struct type t = int let eq = ( ) = let lt = ( ) < let leq = ( ) <= end ; ;