Skip to content

Commit

Permalink
Replace Yojson's to_float with to_number. (ellisk42#79)
Browse files Browse the repository at this point in the history
`Yojson.Basic.Util.to_float` fails to parse numbers written without a
decimal point (e.g. 0 instead of 0.0). Most json tools don't make
this distinction, so this is a compatibility hazard. The `to_number`
parser combinator can handle numbers with or without decimal points.
  • Loading branch information
rosekunkel authored May 19, 2021
1 parent db2d0ef commit b0c0336
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 25 deletions.
Binary file modified compression
Binary file not shown.
Binary file modified helmholtz
Binary file not shown.
Binary file modified logoDrawString
Binary file not shown.
Binary file modified protonet-tester
Binary file not shown.
Binary file modified solver
Binary file not shown.
2 changes: 1 addition & 1 deletion solvers/Dreaming.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ let rec unpack x =
let open Yojson.Basic.Util in

try magical (x |> to_int) with _ ->
try magical (x |> to_float) with _ ->
try magical (x |> to_number) with _ ->
try magical (x |> to_bool) with _ ->
try
let v = x |> to_string in
Expand Down
4 changes: 2 additions & 2 deletions solvers/Helmholtz.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ let run_job channel =
let open Yojson.Basic.Util in
let j = Yojson.Basic.from_channel channel in
let request = j |> member "request" |> deserialize_type in
let timeout = j |> member "timeout" |> to_float in
let timeout = j |> member "timeout" |> to_number in
let evaluationTimeout =
try j |> member "evaluationTimeout" |> to_float
try j |> member "evaluationTimeout" |> to_number
with _ -> 0.001
in
let nc =
Expand Down
6 changes: 3 additions & 3 deletions solvers/compression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,9 @@ let () =
let topI = j |> member "topI" |> to_int in
let bs = j |> member "bs" |> to_int in
let arity = j |> member "arity" |> to_int in
let aic = j |> member "aic" |> to_float in
let pseudoCounts = j |> member "pseudoCounts" |> to_float in
let structurePenalty = j |> member "structurePenalty" |> to_float in
let aic = j |> member "aic" |> to_number in
let pseudoCounts = j |> member "pseudoCounts" |> to_number in
let structurePenalty = j |> member "structurePenalty" |> to_number in

verbose_compression := (try
j |> member "verbose" |> to_bool
Expand Down
2 changes: 1 addition & 1 deletion solvers/enumeration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let deserialize_frontier j =
let request = j |> member "request" |> deserialize_type in
let programs = j |> member "programs" |> to_list |> List.map ~f:(fun j ->
(j |> member "program" |> to_string |> parse_program |> get_some |> strip_primitives,
j |> member "logLikelihood" |> to_float))
j |> member "logLikelihood" |> to_number))
in
{programs;request}

Expand Down
4 changes: 2 additions & 2 deletions solvers/grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ let make_dummy_contextual g =

let deserialize_grammar g =
let open Yojson.Basic.Util in
let logVariable = g |> member "logVariable" |> to_float in
let logVariable = g |> member "logVariable" |> to_number in
let productions = g |> member "productions" |> to_list |> List.map ~f:(fun p ->
let source = p |> member "expression" |> to_string in
let e = parse_program source |> safe_get_some ("Error parsing: "^source) in
Expand All @@ -294,7 +294,7 @@ let deserialize_grammar g =
infer_program_type empty_context [] e |> snd
with UnificationFailure -> raise (Failure ("Could not type "^source))
in
let logProbability = p |> member "logProbability" |> to_float in
let logProbability = p |> member "logProbability" |> to_number in

(e,t,logProbability,compile_unifier t))
in
Expand Down
2 changes: 1 addition & 1 deletion solvers/logoDrawString.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let _ =
with _ -> false
in
let timeout = try
to_float (member "timeout" j)
to_number (member "timeout" j)
with _ -> 0.01
in

Expand Down
10 changes: 5 additions & 5 deletions solvers/physics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ register_special_task "physics"

let maybe_float name default =
try
extra |> member name |> to_float
extra |> member name |> to_number
with _ -> default
in
let maybe_int name default =
Expand All @@ -77,11 +77,11 @@ register_special_task "physics"
let lr = maybe_float "lr" 0.5 in
let decay = maybe_float "decay" 0.5 in
let grow = maybe_float "grow" 1.2 in
let lossThreshold = try Some(extra |> member "lossThreshold" |> to_float) with _ -> None in
let clipOutput = try Some(extra |> member "clipOutput" |> to_float) with _ -> None in
let clipLoss = try Some(extra |> member "clipLoss" |> to_float) with _ -> None in
let lossThreshold = try Some(extra |> member "lossThreshold" |> to_number) with _ -> None in
let clipOutput = try Some(extra |> member "clipOutput" |> to_number) with _ -> None in
let clipLoss = try Some(extra |> member "clipLoss" |> to_number) with _ -> None in

let unpack_real j = ~$(j |> to_float) |> magical in
let unpack_real j = ~$(j |> to_number) |> magical in
let unpack_vector j = j |> to_list |> List.map ~f:unpack_real |> magical in
let unpack_object j =
magical {position = j |> member "position" |> unpack_vector;
Expand Down
12 changes: 6 additions & 6 deletions solvers/solver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let load_problems channel =
in

let timeout = try
j |> member "programTimeout" |> to_float
j |> member "programTimeout" |> to_number
with _ ->
begin
let defaultTimeout = 0.1 in
Expand All @@ -45,7 +45,7 @@ let load_problems channel =

let rec unpack x =
try magical (x |> to_int) with _ ->
try magical (x |> to_float) with _ ->
try magical (x |> to_number) with _ ->
try magical (x |> to_bool) with _ ->
try
let v = x |> to_string in
Expand Down Expand Up @@ -91,21 +91,21 @@ let load_problems channel =


let lowerBound =
try j |> member "lowerBound" |> to_float
try j |> member "lowerBound" |> to_number
with _ -> 0.
in

let upperBound =
try j |> member "upperBound" |> to_float
try j |> member "upperBound" |> to_number
with _ -> 99.
in

let budgetIncrement =
try j |> member "budgetIncrement" |> to_float
try j |> member "budgetIncrement" |> to_number
with _ -> 1.
in

let timeout = j |> member "timeout" |> to_float in
let timeout = j |> member "timeout" |> to_number in
let nc =
try
j |> member "nc" |> to_int
Expand Down
Loading

0 comments on commit b0c0336

Please sign in to comment.