From 4aec37b8cb4e8ccd7115aedf0a174b8d841f414c Mon Sep 17 00:00:00 2001 From: Valentin Blot <24938579+vblot@users.noreply.github.com> Date: Wed, 11 Jul 2018 17:01:30 +0200 Subject: [PATCH] Fixed extraction --- kernel/uint63.mli | 5 + kernel/uint63_amd64.ml | 16 ++ kernel/uint63_x86.ml | 16 ++ plugins/extraction/ExtractNative.v | 53 +++++ plugins/extraction/ocaml.ml | 2 +- plugins/extraction/vo.itarget | 1 + theories/Array/ExtractNative.v | 53 ----- theories/Array/extrNative.ml | 332 ----------------------------- theories/Array/extrNative.mli | 67 ------ theories/Array/vo.itarget | 3 +- 10 files changed, 93 insertions(+), 455 deletions(-) create mode 100644 plugins/extraction/ExtractNative.v delete mode 100644 theories/Array/ExtractNative.v delete mode 100644 theories/Array/extrNative.ml delete mode 100644 theories/Array/extrNative.mli diff --git a/kernel/uint63.mli b/kernel/uint63.mli index 7285a3a0..49912ff5 100644 --- a/kernel/uint63.mli +++ b/kernel/uint63.mli @@ -50,3 +50,8 @@ val compare : t -> t -> int val head0 : t -> t val tail0 : t -> t +val foldi_cont : + (t -> ('a -> 'b) -> 'a -> 'b) -> t -> t -> ('a -> 'b) -> 'a -> 'b +val foldi_down_cont : + (t -> ('a -> 'b) -> 'a -> 'b) -> t -> t -> ('a -> 'b) -> 'a -> 'b +val print_uint : t -> t diff --git a/kernel/uint63_amd64.ml b/kernel/uint63_amd64.ml index 5c5ac339..c9934c10 100644 --- a/kernel/uint63_amd64.ml +++ b/kernel/uint63_amd64.ml @@ -181,3 +181,19 @@ let tail0 x = if !x land 0x3 = 0 then (x := !x lsr 2; r := !r + 2); if !x land 0x1 = 0 then ( r := !r + 1); !r + +let rec foldi_cont f min max cont a = + if lt min max then f min (foldi_cont f (add min 1) max cont) a + else if min = max then f min cont a + else cont a + +let rec foldi_down_cont f max min cont a = + if lt min max then + f max (foldi_down_cont f (sub max 1) min cont) a + else if min = max then f min cont a + else cont a + +let print_uint x = + Printf.fprintf stderr "%s" (to_string x); + flush stderr; + x \ No newline at end of file diff --git a/kernel/uint63_x86.ml b/kernel/uint63_x86.ml index 3970546f..29cc0966 100644 --- a/kernel/uint63_x86.ml +++ b/kernel/uint63_x86.ml @@ -115,3 +115,19 @@ let tail0 x = if Int64.logand !x 0x3L = 0L then (x := Int64.shift_right !x 2; r := !r + 2); if Int64.logand !x 0x1L = 0L then ( r := !r + 1); Int64.of_int !r + +let rec foldi_cont f min max cont a = + if lt min max then f min (foldi_cont f (add min 1) max cont) a + else if min = max then f min cont a + else cont a + +let rec foldi_down_cont f max min cont a = + if lt min max then + f max (foldi_down_cont f (sub max 1) min cont) a + else if min = max then f min cont a + else cont a + +let print_uint x = + Printf.fprintf stderr "%s" (to_string x); + flush stderr; + x \ No newline at end of file diff --git a/plugins/extraction/ExtractNative.v b/plugins/extraction/ExtractNative.v new file mode 100644 index 00000000..1a410dcc --- /dev/null +++ b/plugins/extraction/ExtractNative.v @@ -0,0 +1,53 @@ +Require Import Int63. +Require Import PArray. + +Extract Inductive bool => "bool" ["true" "false"]. +Extract Inductive comparison => "int" ["0" "(-1)" "1"]. +Extract Inductive prod => "(*)" [ "(,)" ]. +Extract Inductive carry => "Uint63.carry" ["Uint63.C0" "Uint63.C1"]. + +Extract Constant int => "Uint63.t". +Extract Constant lsl => "Uint63.l_sl". +Extract Constant lsr => "Uint63.l_sr". +Extract Constant Int63Native.land => "Uint63.l_and". +Extract Constant Int63Native.lor => "Uint63.l_or". +Extract Constant Int63Native.lxor => "Uint63.l_xor". +Extract Constant add => "Uint63.add". +Extract Constant sub => "Uint63.sub". +Extract Constant mul => "Uint63.mul". +Extract Constant mulc => "Uint63.mulc". +Extract Constant div => "Uint63.div". +Extract Constant Int63Native.mod => "Uint63.rem". +Extract Constant eqb => "Uint63.eq". +Extract Constant ltb => "Uint63.lt". +Extract Constant leb => "Uint63.le". +Extract Constant compare => "Uint63.compare". +Extract Constant head0 => "Uint63.head0". +Extract Constant tail0 => "Uint63.tail0". + +Extract Constant addc => "Uint63.addc". +Extract Constant addcarryc => "Uint63.addcarryc". +Extract Constant subc => "Uint63.subc". +Extract Constant subcarryc => "Uint63.subcarryc". +Extract Constant diveucl => "Uint63.diveucl". + +Extract Constant diveucl_21 => "Uint63.diveucl_21". +Extract Constant addmuldiv => "Uint63.addmuldiv". + +(* Pierre que faut-il faire pour celui la *) +(* Extract Constant eqb_correct => "Uint63.eqb_correct". *) +Extract Constant foldi_cont => "Uint63.foldi_cont". +Extract Constant foldi_down_cont => "Uint63.foldi_down_cont". +Extract Constant print_int => "Uint63.print_uint". + +(** Extraction of Array *) +Extract Constant array "'a" => "'a Parray.t". +Extract Constant make => "Parray.make". +Extract Constant get => "Parray.get". +Extract Constant default => "Parray.default". +Extract Constant set => "Parray.set". +Extract Constant length => "Parray.length". +Extract Constant copy => "Parray.copy". +Extract Constant reroot => "Parray.reroot". +Extract Constant init => "Parray.init". +Extract Constant map => "Parray.map". diff --git a/plugins/extraction/ocaml.ml b/plugins/extraction/ocaml.ml index e73ab9ac..7b09c196 100644 --- a/plugins/extraction/ocaml.ml +++ b/plugins/extraction/ocaml.ml @@ -57,7 +57,7 @@ let keywords = "module"; "mutable"; "new"; "object"; "of"; "open"; "or"; "parser"; "private"; "rec"; "sig"; "struct"; "then"; "to"; "true"; "try"; "type"; "val"; "virtual"; "when"; "while"; "with"; "mod"; - "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr" ; "unit" ; "_" ; "__" ] + "land"; "lor"; "lxor"; "lsl"; "lsr"; "asr" ; "unit" ; "int" ; "_" ; "__" ] Idset.empty let pp_open mp = str ("open "^ string_of_modfile mp ^"\n") diff --git a/plugins/extraction/vo.itarget b/plugins/extraction/vo.itarget index 1fe09f6f..d5d7a403 100644 --- a/plugins/extraction/vo.itarget +++ b/plugins/extraction/vo.itarget @@ -1,3 +1,4 @@ +ExtractNative.vo ExtrOcamlBasic.vo ExtrOcamlIntConv.vo ExtrOcamlBigIntConv.vo diff --git a/theories/Array/ExtractNative.v b/theories/Array/ExtractNative.v deleted file mode 100644 index 9104a731..00000000 --- a/theories/Array/ExtractNative.v +++ /dev/null @@ -1,53 +0,0 @@ -Require Import Int63. -Require Import PArray. - -Extract Inductive bool => "bool" ["true" "false"]. -Extract Inductive comparison => "ExtrNative.comparison" ["ExtrNative.Eq" "ExtrNative.Lt" "ExtrNative.Gt"]. -Extract Inductive prod => "(*)" [ "(,)" ]. -Extract Inductive carry => "ExtrNative.carry" ["ExtrNative.C0" "ExtrNative.C1"]. - -Extract Constant int => "ExtrNative.uint". -Extract Constant lsl => "ExtrNative.l_sl". -Extract Constant lsr => "ExtrNative.l_sr". -Extract Constant Int63Native.land => "ExtrNative.l_and". -Extract Constant Int63Native.lor => "ExtrNative.l_or". -Extract Constant Int63Native.lxor => "ExtrNative.l_xor". -Extract Constant add => "ExtrNative.add". -Extract Constant sub => "ExtrNative.sub". -Extract Constant mul => "ExtrNative.mul". -Extract Constant mulc => "ExtrNative.mulc". -Extract Constant div => "ExtrNative.div". -Extract Constant Int63Native.mod => "ExtrNative.rem". -Extract Constant eqb => "ExtrNative.eq". -Extract Constant ltb => "ExtrNative.lt". -Extract Constant leb => "ExtrNative.le". -Extract Constant compare => "ExtrNative.compare". -Extract Constant head0 => "ExtrNative.head0". -Extract Constant tail0 => "ExtrNative.tail0". - -Extract Constant addc => "ExtrNative.addc". -Extract Constant addcarryc => "ExtrNative.addcarryc". -Extract Constant subc => "ExtrNative.subc". -Extract Constant subcarryc => "ExtrNative.subcarryc". -Extract Constant diveucl => "ExtrNative.diveucl". - -Extract Constant diveucl_21 => "ExtrNative.diveucl_21". -Extract Constant addmuldiv => "ExtrNative.addmuldiv". - -(* Pierre que faut-il faire pour celui la *) -(* Extract Constant eqb_correct => "ExtrNative.eqb_correct". *) -Extract Constant foldi_cont => "ExtrNative.foldi_cont". -Extract Constant foldi_down_cont => "ExtrNative.foldi_down_cont". -Extract Constant print_int => "ExtrNative.print_uint". - -(** Extraction of Array *) -Extract Constant array "'a" => "'a ExtrNative.parray". -Extract Constant make => "ExtrNative.parray_make". -Extract Constant get => "ExtrNative.parray_get". -Extract Constant default => "ExtrNative.parray_default". -Extract Constant set => "ExtrNative.parray_set". -Extract Constant length => "ExtrNative.parray_length". -Extract Constant copy => "ExtrNative.parray_copy". -Extract Constant reroot => "ExtrNative.parray_reroot". -Extract Constant init => "ExtrNative.parray_init". -Extract Constant map => "ExtrNative.parray_map". diff --git a/theories/Array/extrNative.ml b/theories/Array/extrNative.ml deleted file mode 100644 index 4d562873..00000000 --- a/theories/Array/extrNative.ml +++ /dev/null @@ -1,332 +0,0 @@ -type comparison = Eq | Lt | Gt - -type 'a carry = C0 of 'a | C1 of 'a - -type uint = int - - (* to be used only on 32 bits achitectures *) -let maxuint31 = Int32.of_string "0x7FFFFFFF" -let uint_32 i = Int32.logand (Int32.of_int i) maxuint31 - -let select f32 f64 = if Sys.word_size = 64 then f64 else f32 - - (* conversion to an int *) -let to_int i = i - -let of_int_32 i = i -let of_int_64 i = i land 0x7FFFFFFF - -let of_int = select of_int_32 of_int_64 -let of_uint i = i - - (* convertion of an uint31 to a string *) -let to_string_32 i = Int32.to_string (uint_32 i) -let to_string_64 = string_of_int - -let to_string = select to_string_32 to_string_64 -let of_string s = - let i32 = Int32.of_string s in - if Int32.compare Int32.zero i32 <= 0 - && Int32.compare i32 maxuint31 <= 0 - then Int32.to_int i32 - else raise (Failure "int_of_string") - - - - (* logical shift *) -let l_sl x y = - of_int (if 0 <= y && y < 31 then x lsl y else 0) - -let l_sr x y = - if 0 <= y && y < 31 then x lsr y else 0 - -let l_and x y = x land y -let l_or x y = x lor y -let l_xor x y = x lxor y - - (* addition of int31 *) -let add x y = of_int (x + y) - - (* subtraction *) -let sub x y = of_int (x - y) - - (* multiplication *) -let mul x y = of_int (x * y) - - (* exact multiplication *) -let mulc_32 x y = - let x = Int64.of_int32 (uint_32 x) in - let y = Int64.of_int32 (uint_32 y) in - let m = Int64.mul x y in - let l = Int64.to_int m in - let h = Int64.to_int (Int64.shift_right_logical m 31) in - h,l - -let mulc_64 x y = - let m = x * y in - let l = of_int_64 m in - let h = of_int_64 (m lsr 31) in - h, l -let mulc = select mulc_32 mulc_64 - - (* division *) -let div_32 x y = - if y = 0 then 0 else - Int32.to_int (Int32.div (uint_32 x) (uint_32 y)) -let div_64 x y = if y = 0 then 0 else x / y -let div = select div_32 div_64 - - (* modulo *) -let rem_32 x y = - if y = 0 then 0 - else Int32.to_int (Int32.rem (uint_32 x) (uint_32 y)) -let rem_64 x y = if y = 0 then 0 else x mod y -let rem = select rem_32 rem_64 - - (* division of two numbers by one *) -let div21_32 xh xl y = - if y = 0 then (0,0) - else - let x = - Int64.logor - (Int64.shift_left (Int64.of_int32 (uint_32 xh)) 31) - (Int64.of_int32 (uint_32 xl)) in - let y = Int64.of_int32 (uint_32 y) in - let q = Int64.div x y in - let r = Int64.rem x y in - Int64.to_int q, Int64.to_int r -let div21_64 xh xl y = - if y = 0 then (0,0) - else - let x = (xh lsl 31) lor xl in - let q = x / y in - let r = x mod y in - q, r -let div21 = select div21_32 div21_64 - - (* comparison *) -let lt_32 x y = (x lxor 0x40000000) < (y lxor 0x40000000) -(* if 0 <= x then - if 0 <= y then x < y - else true - else if 0 <= y then false - else x < y *) -(* Int32.compare (uint_32 x) (uint_32 y) < 0 *) - -let lt_64 x y = x < y -let lt = select lt_32 lt_64 - -let le_32 x y = - (x lxor 0x40000000) <= (y lxor 0x40000000) -(* - if 0 <= x then - if 0 <= y then x <= y - else true - else if 0 <= y then false - else x <= y -*) -(*Int32.compare (uint_32 x) (uint_32 y) <= 0*) -let le_64 x y = x <= y -let le = select le_32 le_64 - -let eq x y = x == y - -let cmp_32 x y = Int32.compare (uint_32 x) (uint_32 y) -let cmp_64 x y = compare x y -let compare = select cmp_32 cmp_64 - -let compare x y = - match compare x y with - | x when x < 0 -> Lt - | 0 -> Eq - | _ -> Gt - - (* head tail *) - -let head0 x = - let r = ref 0 in - let x = ref x in - if !x land 0x7FFF0000 = 0 then r := !r + 15 - else x := !x lsr 15; - if !x land 0xFF00 = 0 then (x := !x lsl 8; r := !r + 8); - if !x land 0xF000 = 0 then (x := !x lsl 4; r := !r + 4); - if !x land 0xC000 = 0 then (x := !x lsl 2; r := !r + 2); - if !x land 0x8000 = 0 then (x := !x lsl 1; r := !r + 1); - if !x land 0x8000 = 0 then ( r := !r + 1); - !r;; - -let tail0 x = - let r = ref 0 in - let x = ref x in - if !x land 0xFFFF = 0 then (x := !x lsr 16; r := !r + 16); - if !x land 0xFF = 0 then (x := !x lsr 8; r := !r + 8); - if !x land 0xF = 0 then (x := !x lsr 4; r := !r + 4); - if !x land 0x3 = 0 then (x := !x lsr 2; r := !r + 2); - if !x land 0x1 = 0 then ( r := !r + 1); - !r - -let addc x y = - let s = add x y in - if lt s x then C1 s else C0 s - -let addcarryc x y = - let s = add (x+1) y in - if le s x then C1 s else C0 s - -let subc x y = - let s = sub x y in - if lt x y then C1 s else C0 s - -let subcarryc x y = - let s = sub (x-1) y in - if le x y then C1 s else C0 s - -let diveucl x y = div x y, rem x y - -let diveucl_21 = div21 - -let addmuldiv p i j = - let p' = to_int p in - of_uint (l_or - (l_sl i p) - (l_sr j (of_int (31 - p')))) - -let rec foldi_cont f min max cont a = - if lt min max then f min (foldi_cont f (add min 1) max cont) a - else if min = max then f min cont a - else cont a - -let rec foldi_down_cont f max min cont a = - if lt min max then - f max (foldi_down_cont f (sub max 1) min cont) a - else if min = max then f min cont a - else cont a - -let print_uint x = - Printf.fprintf stderr "%s" (to_string x); - flush stderr; - x - -(* Les Tableaux maintenant *) - -let max_array_length32 = 4194303 (* Sys.max_array_length on arch32 *) - -type 'a parray = ('a kind) ref -and 'a kind = - | Array of 'a array - (* | Matrix of 'a array array *) - | Updated of int * 'a * 'a parray - -let of_array t = ref (Array t) - -let parray_make n def = - let n = to_int n in - let n = - if 0 <= n && n < max_array_length32 then n + 1 - else max_array_length32 in - ref (Array (Array.make n def)) - -let rec get_updated p n = - match !p with - | Array t -> - let l = Array.length t in - if 0 <= n && n < l then Array.unsafe_get t n - else (Array.unsafe_get t (l-1)) - | Updated (k,e,p) -> if n = k then e else get_updated p n - -let parray_get p n = - let n = to_int n in - match !p with - | Array t -> - let l = Array.length t in - if 0 <= n && n < l then Array.unsafe_get t n - else (Array.unsafe_get t (l-1)) - | Updated _ -> get_updated p n - - -let rec default_updated p = - match !p with - | Array t -> Array.unsafe_get t (Array.length t - 1) - | Updated (_,_,p) -> default_updated p - -let parray_default p = - match !p with - | Array t -> Array.unsafe_get t (Array.length t - 1) - | Updated (_,_,p) -> default_updated p - -let rec length p = - match !p with - | Array t -> of_int (Array.length t - 1) (* The default value *) - | Updated (_, _, p) -> length p - -let parray_length p = - match !p with - | Array t -> of_int (Array.length t - 1) - | Updated (_, _, p) -> length p - -let parray_set p n e = - let kind = !p in - let n = to_int n in - match kind with - | Array t -> - if 0 <= n && n < Array.length t - 1 then - let res = ref kind in - p := Updated (n, Array.unsafe_get t n, res); - Array.unsafe_set t n e; - res - else p - | Updated _ -> - if 0 <= n && n < to_int (parray_length p) then - ref (Updated(n, e, p)) - else p - - -let rec copy_updated p = - match !p with - | Array t -> Array.copy t - | Updated (n,e,p) -> - let t = copy_updated p in - Array.unsafe_set t n e; t - -let parray_copy p = - let t = - match !p with - | Array t -> Array.copy t - | Updated _ -> copy_updated p in - ref (Array t) - -let rec rerootk t k = - match !t with - | Array _ -> k () - | Updated (i, v, t') -> - let k' () = - begin match !t' with - | Array a as n -> - let v' = a.(i) in - a.(i) <- v; - t := n; - t' := Updated (i, v', t) - | Updated _ -> assert false - end; k() in - rerootk t' k' - -let parray_reroot t = rerootk t (fun () -> t) - -let parray_init n f def = - let n = to_int n in - let n = - if 0 <= n && n < max_array_length32 then n + 1 - else max_array_length32 in - let t = Array.make n def in - for i = 0 to n - 2 do Array.unsafe_set t i (f i) done; - ref (Array t) - -let parray_map f p = - match !p with - | Array t -> ref (Array (Array.map f t)) - | _ -> - let len = to_int (length p) in - ref (Array - (Array.init (len + 1) - (fun i -> f (parray_get p (of_int i))))) - diff --git a/theories/Array/extrNative.mli b/theories/Array/extrNative.mli deleted file mode 100644 index 14eff5f0..00000000 --- a/theories/Array/extrNative.mli +++ /dev/null @@ -1,67 +0,0 @@ -type comparison = Eq | Lt | Gt -type 'a carry = C0 of 'a | C1 of 'a - -(*s Unsigned Int *) -type uint - -(* Conversion with int *) -val to_int : uint -> int -val of_int : int -> uint -val of_uint : int -> uint - -(* Conversion with string *) -val to_string : uint -> string -val of_string : string -> uint - -(* logical operations *) -val l_sl : uint -> uint -> uint -val l_sr : uint -> uint -> uint -val l_and : uint -> uint -> uint -val l_or : uint -> uint -> uint -val l_xor : uint -> uint -> uint - -(* arithmetic operations *) -val add : uint -> uint -> uint -val sub : uint -> uint -> uint -val mul : uint -> uint -> uint -val mulc : uint -> uint -> uint * uint -val div : uint -> uint -> uint -val rem : uint -> uint -> uint - -val lt : uint -> uint -> bool -val le : uint -> uint -> bool -val eq : uint -> uint -> bool -val compare : uint -> uint -> comparison - -val head0 : uint -> uint -val tail0 : uint -> uint - -val addc : uint -> uint -> uint carry -val addcarryc : uint -> uint -> uint carry -val subc : uint -> uint -> uint carry -val subcarryc : uint -> uint -> uint carry -val diveucl : uint -> uint -> uint * uint -val diveucl_21 : uint -> uint -> uint -> uint * uint -val addmuldiv : uint -> uint -> uint -> uint - -val foldi_cont : - (uint -> ('a -> 'b) -> 'a -> 'b) -> uint -> uint -> ('a -> 'b) -> 'a -> 'b -val foldi_down_cont : - (uint -> ('a -> 'b) -> 'a -> 'b) -> uint -> uint -> ('a -> 'b) -> 'a -> 'b -val print_uint : uint -> uint - - -(*s Persistant array *) - -type 'a parray - -val of_array : 'a array -> 'a parray - -val parray_make : uint -> 'a -> 'a parray -val parray_get : 'a parray -> uint -> 'a -val parray_default : 'a parray -> 'a -val parray_length : 'a parray -> uint -val parray_set : 'a parray -> uint -> 'a -> 'a parray -val parray_copy : 'a parray -> 'a parray -val parray_reroot : 'a parray -> 'a parray - diff --git a/theories/Array/vo.itarget b/theories/Array/vo.itarget index c671cb73..fc2f5a98 100644 --- a/theories/Array/vo.itarget +++ b/theories/Array/vo.itarget @@ -1,2 +1 @@ -PArray.vo -ExtractNative.vo \ No newline at end of file +PArray.vo \ No newline at end of file