11import json
2- from typing import Any , Callable , Dict , Tuple , List as TList
2+ from typing import Any , Callable , Dict , List as TList
33
44import tqdm
55
66from synth import Task , Dataset , PBE , Example
77from synth .syntax import (
8- INT ,
98 FunctionType ,
10- List ,
11- Type ,
12- Arrow ,
13- Function ,
14- Primitive ,
159 Program ,
16- Variable ,
1710 UnknownType ,
1811 guess_type ,
1912)
2013
2114from calculator import dsl , evaluator , FLOAT
2215
23- # this dictionary contains the primitives as defined in the dsl
24- name2type : Dict [str , Type ] = {p .primitive : p .type for p in dsl .list_primitives }
25- # this dictionary contains the instantiated primitives, that is to say after removal of polymorphic type
26- name2fulltype = {}
2716dsl .instantiate_polymorphic_types (5 )
28- for p in dsl .list_primitives :
29- if isinstance (p .type , Arrow ):
30- name = str (p ) + str (p .type .arguments ()[0 ])
31- else :
32- name = p .primitive
33- name2fulltype [name ] = p .type
3417
3518
3619def __convert__ (load : Callable [[], Dataset [PBE ]], name : str ) -> None :
@@ -41,7 +24,10 @@ def __convert__(load: Callable[[], Dataset[PBE]], name: str) -> None:
4124 # Integrity check
4225 for task in tqdm .tqdm (tasks , desc = "integrity check" ):
4326 for ex in task .specification .examples :
44- assert evaluator .eval (task .solution , ex .inputs ) == ex .output
27+ obt = evaluator .eval (task .solution , ex .inputs )
28+ assert (
29+ obt == ex .output
30+ ), f"failed on { task .solution } inputs:{ ex .inputs } got:{ obt } target:{ ex .output } "
4531
4632
4733def convert_calculator (
@@ -60,13 +46,13 @@ def load() -> Dataset[PBE]:
6046 args_types = [guess_type (arg ) for arg in inputs [0 ]] + [
6147 guess_type (outputs [0 ])
6248 ]
63- # guess_type doesn't recognise FLOAT but since it is the only type not recognised we know that Unknown TYpe is acutally FLOAT
49+ # guess_type doesn't recognise FLOAT but since it is the only type not recognised we know that Unknown Type is acutally FLOAT
6450 args_types = [
6551 at if not isinstance (at , UnknownType ) else FLOAT
6652 for at in args_types
6753 ]
6854 type_request = FunctionType (* args_types )
69- prog = dsl .parse_program (name , type_request )
55+ prog : Program = dsl .parse_program (name , type_request )
7056 examples = [
7157 Example (inp , out )
7258 for inp , out in zip (inputs , outputs )
@@ -82,47 +68,6 @@ def load() -> Dataset[PBE]:
8268 __convert__ (load , output_file )
8369
8470
85- """
86- Parser of program stored in the json file, for a given task.
87- s: program attribute represented as a string
88- returns: Tuple[Program, Type] where program is the solution of type Type to the task
89- """
90-
91-
92- def __calculator_str2prog (s : str ) -> Tuple [Program , Type ]:
93- parts = s .split ("|" )
94- stack : TList [Program ] = []
95- var : int = 0
96- type_stack : TList [Type ] = []
97- for part in parts :
98- subparts = part .split ("," )
99- name = subparts .pop (0 )
100- # possible inputs, int or float in our case
101- if name == "INT" :
102- stack .append (Variable (var , INT ))
103- var += 1
104- type_stack .append (INT )
105- continue
106- if name == "FLOAT" :
107- stack .append (Variable (var , FLOAT ))
108- var += 1
109- type_stack .append (FLOAT )
110- continue
111- # primitives that serve as constants
112- if name in ["1" , "2" , "3" ]:
113- primitive = Primitive (name , name2fulltype [name ])
114- stack .append (primitive )
115- else : # other primitives are functions, we want to add their type
116- targets = [int (x ) for x in subparts ]
117- arguments = [stack [x ] for x in targets ]
118- longname = name + str (arguments [- 1 ].type )
119- primitive = Primitive (name , name2fulltype [longname ])
120- stack .append (Function (primitive , arguments ))
121- type_stack .append (stack [- 1 ].type )
122- type_request = FunctionType (* type_stack )
123- return stack [- 1 ], type_request
124-
125-
12671if __name__ == "__main__" :
12772 import argparse
12873
0 commit comments