-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecification.py
90 lines (64 loc) · 2.15 KB
/
specification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from dataclasses import dataclass
from typing import Any, Generic, List, Optional, TypeVar
from synth.syntax.type_system import FunctionType, EmptyList, Type, guess_type
class TaskSpecification:
def get_specification(self, specification: type) -> "Optional[TaskSpecification]":
"""
Gets the specification of the given TaskSpecification type that may be embed in this possibly compound specification.
If none is found returns None.
"""
if isinstance(self, specification):
return self
return None
@dataclass
class Example:
"""
Represents an example pair of (inputs, output)
"""
inputs: List[Any]
output: Any
def guess_type(self) -> Type:
types = list(map(guess_type, self.inputs)) + [guess_type(self.output)]
return FunctionType(*types)
@dataclass
class PBE(TaskSpecification):
"""
Programming By Example (PBE) specification.
"""
examples: List[Example]
def guess_type(self) -> Type:
i = 0
t = self.examples[i].guess_type()
while EmptyList in t and i + 1 < len(self.examples):
i += 1
t = self.examples[i].guess_type()
return t
@dataclass
class PBEWithConstants(PBE):
"""
Programming By Example (PBE) with constants specification
"""
constants_in: List[Any]
constants_out: List[Any]
@dataclass
class NLP(TaskSpecification):
"""
Natural Language (NLP) specification.
"""
intent: str
@dataclass
class SketchedSpecification(TaskSpecification):
sketch: str
U = TypeVar("U", bound=TaskSpecification, covariant=True)
V = TypeVar("V", bound=TaskSpecification, covariant=True)
@dataclass
class CompoundSpecification(TaskSpecification, Generic[U, V]):
specification1: U
specification2: V
def get_specification(self, specification: type) -> "Optional[TaskSpecification]":
a = self.specification1.get_specification(specification)
if a is not None:
return a
return self.specification2.get_specification(specification)
NLPBE = CompoundSpecification[NLP, PBE]
SketchedPBE = CompoundSpecification[SketchedSpecification, PBE]