hydra-kernel 0.15.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. hydra/adapt.py +623 -0
  2. hydra/analysis.py +294 -0
  3. hydra/annotations.py +241 -0
  4. hydra/arity.py +57 -0
  5. hydra/ast.py +173 -0
  6. hydra/checking.py +400 -0
  7. hydra/classes.py +12 -0
  8. hydra/codegen.py +372 -0
  9. hydra/coders.py +105 -0
  10. hydra/constants.py +38 -0
  11. hydra/context.py +27 -0
  12. hydra/core.py +495 -0
  13. hydra/decode/ast.py +277 -0
  14. hydra/decode/classes.py +29 -0
  15. hydra/decode/coders.py +63 -0
  16. hydra/decode/context.py +60 -0
  17. hydra/decode/core.py +664 -0
  18. hydra/decode/error/checking.py +236 -0
  19. hydra/decode/error/core.py +487 -0
  20. hydra/decode/errors.py +285 -0
  21. hydra/decode/json/model.py +78 -0
  22. hydra/decode/packaging.py +193 -0
  23. hydra/decode/parsing.py +88 -0
  24. hydra/decode/paths.py +264 -0
  25. hydra/decode/phantoms.py +45 -0
  26. hydra/decode/query.py +280 -0
  27. hydra/decode/relational.py +118 -0
  28. hydra/decode/tabular.py +76 -0
  29. hydra/decode/testing.py +154 -0
  30. hydra/decode/topology.py +99 -0
  31. hydra/decode/typing.py +76 -0
  32. hydra/decode/util.py +75 -0
  33. hydra/decode/variants.py +85 -0
  34. hydra/decoding.py +495 -0
  35. hydra/dependencies.py +352 -0
  36. hydra/differentiation.py +98 -0
  37. hydra/dsl/__init__.py +3 -0
  38. hydra/dsl/annotations.py +225 -0
  39. hydra/dsl/ast.py +141 -0
  40. hydra/dsl/classes.py +9 -0
  41. hydra/dsl/coders.py +117 -0
  42. hydra/dsl/context.py +34 -0
  43. hydra/dsl/core.py +385 -0
  44. hydra/dsl/error/checking.py +148 -0
  45. hydra/dsl/error/core.py +430 -0
  46. hydra/dsl/error/packaging.py +74 -0
  47. hydra/dsl/errors.py +152 -0
  48. hydra/dsl/graph.py +78 -0
  49. hydra/dsl/json/model.py +20 -0
  50. hydra/dsl/literal_types.py +142 -0
  51. hydra/dsl/literals.py +172 -0
  52. hydra/dsl/meta/__init__.py +0 -0
  53. hydra/dsl/meta/core.py +190 -0
  54. hydra/dsl/meta/graph.py +55 -0
  55. hydra/dsl/meta/lib/__init__.py +0 -0
  56. hydra/dsl/meta/lib/chars.py +34 -0
  57. hydra/dsl/meta/lib/eithers.py +44 -0
  58. hydra/dsl/meta/lib/equality.py +49 -0
  59. hydra/dsl/meta/lib/lists.py +189 -0
  60. hydra/dsl/meta/lib/literals.py +269 -0
  61. hydra/dsl/meta/lib/logic.py +24 -0
  62. hydra/dsl/meta/lib/maps.py +104 -0
  63. hydra/dsl/meta/lib/math.py +243 -0
  64. hydra/dsl/meta/lib/maybes.py +69 -0
  65. hydra/dsl/meta/lib/pairs.py +22 -0
  66. hydra/dsl/meta/lib/sets.py +74 -0
  67. hydra/dsl/meta/lib/strings.py +80 -0
  68. hydra/dsl/meta/phantom_literals.py +86 -0
  69. hydra/dsl/meta/phantoms.py +658 -0
  70. hydra/dsl/packaging.py +124 -0
  71. hydra/dsl/parsing.py +37 -0
  72. hydra/dsl/paths.py +141 -0
  73. hydra/dsl/prims.py +651 -0
  74. hydra/dsl/py.typed +0 -0
  75. hydra/dsl/python.py +195 -0
  76. hydra/dsl/query.py +144 -0
  77. hydra/dsl/relational.py +73 -0
  78. hydra/dsl/tabular.py +48 -0
  79. hydra/dsl/terms.py +669 -0
  80. hydra/dsl/testing.py +61 -0
  81. hydra/dsl/topology.py +46 -0
  82. hydra/dsl/types.py +436 -0
  83. hydra/dsl/typing.py +84 -0
  84. hydra/dsl/util.py +17 -0
  85. hydra/dsl/variants.py +56 -0
  86. hydra/dsl/yaml/model.py +26 -0
  87. hydra/dsls.py +252 -0
  88. hydra/encode/ast.py +77 -0
  89. hydra/encode/classes.py +15 -0
  90. hydra/encode/coders.py +25 -0
  91. hydra/encode/context.py +16 -0
  92. hydra/encode/core.py +242 -0
  93. hydra/encode/error/checking.py +70 -0
  94. hydra/encode/error/core.py +168 -0
  95. hydra/encode/errors.py +114 -0
  96. hydra/encode/json/model.py +27 -0
  97. hydra/encode/packaging.py +40 -0
  98. hydra/encode/parsing.py +23 -0
  99. hydra/encode/paths.py +109 -0
  100. hydra/encode/phantoms.py +17 -0
  101. hydra/encode/query.py +107 -0
  102. hydra/encode/relational.py +31 -0
  103. hydra/encode/tabular.py +24 -0
  104. hydra/encode/testing.py +23 -0
  105. hydra/encode/topology.py +17 -0
  106. hydra/encode/typing.py +24 -0
  107. hydra/encode/util.py +37 -0
  108. hydra/encode/variants.py +125 -0
  109. hydra/encoding.py +450 -0
  110. hydra/environment.py +156 -0
  111. hydra/error/checking.py +153 -0
  112. hydra/error/core.py +424 -0
  113. hydra/error/packaging.py +83 -0
  114. hydra/errors.py +203 -0
  115. hydra/eval/lib/eithers.py +109 -0
  116. hydra/eval/lib/equality.py +21 -0
  117. hydra/eval/lib/lists.py +89 -0
  118. hydra/eval/lib/logic.py +20 -0
  119. hydra/eval/lib/maps.py +82 -0
  120. hydra/eval/lib/math.py +17 -0
  121. hydra/eval/lib/maybes.py +109 -0
  122. hydra/eval/lib/pairs.py +40 -0
  123. hydra/eval/lib/sets.py +28 -0
  124. hydra/extract/core.py +528 -0
  125. hydra/extract/json.py +68 -0
  126. hydra/extract/util.py +18 -0
  127. hydra/formatting.py +148 -0
  128. hydra/graph.py +52 -0
  129. hydra/hoisting.py +513 -0
  130. hydra/inference.py +495 -0
  131. hydra/json/bootstrap.py +401 -0
  132. hydra/json/decode.py +277 -0
  133. hydra/json/decoding.py +51 -0
  134. hydra/json/encode.py +361 -0
  135. hydra/json/model.py +38 -0
  136. hydra/json/parser.py +89 -0
  137. hydra/json/writer.py +84 -0
  138. hydra/json/yaml/decode.py +77 -0
  139. hydra/json/yaml/encode.py +36 -0
  140. hydra/languages.py +38 -0
  141. hydra/lexical.py +205 -0
  142. hydra/lib/__init__.py +1 -0
  143. hydra/lib/chars.py +33 -0
  144. hydra/lib/eithers.py +179 -0
  145. hydra/lib/equality.py +57 -0
  146. hydra/lib/lists.py +258 -0
  147. hydra/lib/literals.py +551 -0
  148. hydra/lib/logic.py +33 -0
  149. hydra/lib/maps.py +221 -0
  150. hydra/lib/math.py +335 -0
  151. hydra/lib/maybes.py +123 -0
  152. hydra/lib/names.py +271 -0
  153. hydra/lib/pairs.py +25 -0
  154. hydra/lib/py.typed +0 -0
  155. hydra/lib/regex.py +40 -0
  156. hydra/lib/sets.py +90 -0
  157. hydra/lib/strings.py +90 -0
  158. hydra/literals.py +77 -0
  159. hydra/names.py +106 -0
  160. hydra/packaging.py +102 -0
  161. hydra/parsers.py +132 -0
  162. hydra/parsing.py +42 -0
  163. hydra/paths.py +338 -0
  164. hydra/phantoms.py +28 -0
  165. hydra/predicates.py +190 -0
  166. hydra/py.typed +0 -0
  167. hydra/query.py +212 -0
  168. hydra/reduction.py +592 -0
  169. hydra/reflect.py +243 -0
  170. hydra/relational.py +56 -0
  171. hydra/resolution.py +204 -0
  172. hydra/rewriting.py +941 -0
  173. hydra/scoping.py +62 -0
  174. hydra/serialization.py +487 -0
  175. hydra/show/core.py +390 -0
  176. hydra/show/error/core.py +219 -0
  177. hydra/show/errors.py +148 -0
  178. hydra/show/graph.py +16 -0
  179. hydra/show/paths.py +179 -0
  180. hydra/show/typing.py +35 -0
  181. hydra/show/util.py +31 -0
  182. hydra/show/variants.py +93 -0
  183. hydra/sorting.py +160 -0
  184. hydra/sources/__init__.py +3 -0
  185. hydra/sources/libraries.py +1245 -0
  186. hydra/strip.py +165 -0
  187. hydra/substitution.py +150 -0
  188. hydra/tabular.py +40 -0
  189. hydra/templates.py +111 -0
  190. hydra/test/transform.py +81 -0
  191. hydra/test/utils.py +33 -0
  192. hydra/testing.py +53 -0
  193. hydra/tools.py +181 -0
  194. hydra/topology.py +36 -0
  195. hydra/typing.py +58 -0
  196. hydra/unification.py +217 -0
  197. hydra/util.py +39 -0
  198. hydra/validate/core.py +298 -0
  199. hydra/validate/packaging.py +94 -0
  200. hydra/variables.py +236 -0
  201. hydra/variants.py +71 -0
  202. hydra/yaml/model.py +58 -0
  203. hydra_kernel-0.15.0.dist-info/METADATA +12 -0
  204. hydra_kernel-0.15.0.dist-info/RECORD +205 -0
  205. hydra_kernel-0.15.0.dist-info/WHEEL +4 -0
hydra/adapt.py ADDED
@@ -0,0 +1,623 @@
1
+ # Note: this is an automatically generated file. Do not edit.
2
+ r"""Simple, one-way adapters for types and terms."""
3
+ from __future__ import annotations
4
+ from collections.abc import Callable
5
+ from decimal import Decimal
6
+ from functools import lru_cache
7
+ from hydra.dsl.python import Either, FrozenDict, Just, Left, Maybe, Nothing, Right, frozenlist
8
+ from typing import TypeVar, cast
9
+ import hydra.coders
10
+ import hydra.core
11
+ import hydra.dependencies
12
+ import hydra.environment
13
+ import hydra.errors
14
+ import hydra.graph
15
+ import hydra.hoisting
16
+ import hydra.inference
17
+ import hydra.lexical
18
+ import hydra.lib.eithers
19
+ import hydra.lib.equality
20
+ import hydra.lib.lists
21
+ import hydra.lib.literals
22
+ import hydra.lib.logic
23
+ import hydra.lib.maps
24
+ import hydra.lib.maybes
25
+ import hydra.lib.pairs
26
+ import hydra.lib.sets
27
+ import hydra.lib.strings
28
+ import hydra.literals
29
+ import hydra.names
30
+ import hydra.packaging
31
+ import hydra.reduction
32
+ import hydra.reflect
33
+ import hydra.resolution
34
+ import hydra.rewriting
35
+ import hydra.scoping
36
+ import hydra.show.core
37
+ import hydra.strip
38
+ import hydra.variables
39
+ T0 = TypeVar("T0")
40
+ T1 = TypeVar("T1")
41
+ T2 = TypeVar("T2")
42
+ def literal_type_supported(constraints: hydra.coders.LanguageConstraints, lt: hydra.core.LiteralType) -> bool:
43
+ r"""Check if a literal type is supported by the given language constraints."""
44
+ def for_type(lt2: hydra.core.LiteralType) -> bool:
45
+ match lt2:
46
+ case hydra.core.LiteralTypeFloat(value=ft):
47
+ return hydra.lib.sets.member(ft, constraints.float_types)
48
+ case hydra.core.LiteralTypeInteger(value=it):
49
+ return hydra.lib.sets.member(it, constraints.integer_types)
50
+ case _:
51
+ return True
52
+ return hydra.lib.logic.if_else(hydra.lib.sets.member(hydra.reflect.literal_type_variant(lt), constraints.literal_variants), (lambda : for_type(lt)), (lambda : False))
53
+ def type_alternatives(type: hydra.core.Type) -> frozenlist[hydra.core.Type]:
54
+ r"""Find a list of alternatives for a given type, if any."""
55
+ match type:
56
+ case hydra.core.TypeAnnotated(value=at):
57
+ type2 = at.body
58
+ return (type2,)
59
+ case hydra.core.TypeMaybe(value=ot):
60
+ return (cast(hydra.core.Type, hydra.core.TypeList(ot)),)
61
+ case hydra.core.TypeUnion(value=rt):
62
+ def to_opt_field(f: hydra.core.FieldType) -> hydra.core.FieldType:
63
+ return hydra.core.FieldType(f.name, cast(hydra.core.Type, hydra.core.TypeMaybe(f.type)))
64
+ @lru_cache(1)
65
+ def opt_fields() -> frozenlist[hydra.core.FieldType]:
66
+ return hydra.lib.lists.map((lambda x1: to_opt_field(x1)), rt)
67
+ return (cast(hydra.core.Type, hydra.core.TypeRecord(opt_fields())),)
68
+ case hydra.core.TypeUnit():
69
+ return (cast(hydra.core.Type, hydra.core.TypeLiteral(cast(hydra.core.LiteralType, hydra.core.LiteralTypeBoolean()))),)
70
+ case hydra.core.TypeVoid():
71
+ return (cast(hydra.core.Type, hydra.core.TypeUnit()),)
72
+ case _:
73
+ return ()
74
+ def adapt_type(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], type0: hydra.core.Type) -> Either[hydra.errors.Error, hydra.core.Type]:
75
+ r"""Adapt a type using the given language constraints."""
76
+ def for_supported(typ: hydra.core.Type) -> Maybe[hydra.core.Type]:
77
+ match typ:
78
+ case hydra.core.TypeLiteral(value=lt):
79
+ return hydra.lib.logic.if_else(literal_type_supported(constraints, lt), (lambda : Just(typ)), (lambda : hydra.lib.maybes.maybe((lambda : Just(cast(hydra.core.Type, hydra.core.TypeLiteral(cast(hydra.core.LiteralType, hydra.core.LiteralTypeString()))))), (lambda lt2: Just(cast(hydra.core.Type, hydra.core.TypeLiteral(lt2)))), hydra.lib.maps.lookup(lt, litmap))))
80
+ case _:
81
+ return Just(typ)
82
+ def for_unsupported(typ: hydra.core.Type) -> Maybe[hydra.core.Type]:
83
+ def try_alts(alts: frozenlist[hydra.core.Type]) -> Maybe[hydra.core.Type]:
84
+ return hydra.lib.maybes.bind(hydra.lib.lists.uncons(alts), (lambda uc: hydra.lib.maybes.maybe((lambda : try_alts(hydra.lib.pairs.second(uc))), (lambda t: Just(t)), try_type(hydra.lib.pairs.first(uc)))))
85
+ @lru_cache(1)
86
+ def alts0() -> frozenlist[hydra.core.Type]:
87
+ return type_alternatives(typ)
88
+ return try_alts(alts0())
89
+ def try_type(typ: hydra.core.Type) -> Maybe[hydra.core.Type]:
90
+ @lru_cache(1)
91
+ def supported_variant() -> bool:
92
+ return hydra.lib.sets.member(hydra.reflect.type_variant(typ), constraints.type_variants)
93
+ return hydra.lib.logic.if_else(supported_variant(), (lambda : for_supported(typ)), (lambda : for_unsupported(typ)))
94
+ def rewrite(recurse: Callable[[hydra.core.Type], Either[hydra.errors.Error, hydra.core.Type]], typ: hydra.core.Type) -> Either[hydra.errors.Error, hydra.core.Type]:
95
+ return hydra.lib.eithers.bind(recurse(typ), (lambda type1: hydra.lib.maybes.maybe((lambda : Left(cast(hydra.errors.Error, hydra.errors.ErrorOther(hydra.errors.OtherError(hydra.lib.strings.cat2("no alternatives for type: ", hydra.show.core.type(typ))))))), (lambda type2: Right(type2)), try_type(type1))))
96
+ return hydra.rewriting.rewrite_type_m((lambda x1, x2: rewrite(x1, x2)), type0)
97
+ def adapt_graph_schema(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], types0: FrozenDict[T0, hydra.core.Type]) -> Either[hydra.errors.Error, FrozenDict[T0, hydra.core.Type]]:
98
+ r"""Adapt a schema graph to the given language constraints."""
99
+ def map_pair(pair: tuple[T1, hydra.core.Type]) -> Either[hydra.errors.Error, tuple[T1, hydra.core.Type]]:
100
+ @lru_cache(1)
101
+ def name() -> T1:
102
+ return hydra.lib.pairs.first(pair)
103
+ @lru_cache(1)
104
+ def typ() -> hydra.core.Type:
105
+ return hydra.lib.pairs.second(pair)
106
+ return hydra.lib.eithers.bind(adapt_type(constraints, litmap, typ()), (lambda typ1: Right((name(), typ1))))
107
+ return hydra.lib.eithers.bind(hydra.lib.eithers.map_list((lambda x1: map_pair(x1)), hydra.lib.maps.to_list(types0)), (lambda pairs: Right(hydra.lib.maps.from_list(pairs))))
108
+ def adapt_lambda_domains(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], recurse: Callable[[T0], Either[hydra.errors.Error, hydra.core.Term]], term: T0):
109
+ def _hoist_hydra_adapt_adapt_lambda_domains_1(constraints, litmap, rewritten, v1):
110
+ match v1:
111
+ case hydra.core.TermLambda(value=l):
112
+ return hydra.lib.eithers.bind(hydra.lib.maybes.maybe((lambda : Right(Nothing())), (lambda dom: hydra.lib.eithers.bind(adapt_type(constraints, litmap, dom), (lambda dom1: Right(Just(dom1))))), l.domain), (lambda adapted_domain: Right(cast(hydra.core.Term, hydra.core.TermLambda(hydra.core.Lambda(l.parameter, adapted_domain, l.body))))))
113
+ case _:
114
+ return Right(rewritten)
115
+ return hydra.lib.eithers.bind(recurse(term), (lambda rewritten: _hoist_hydra_adapt_adapt_lambda_domains_1(constraints, litmap, rewritten, rewritten)))
116
+ def adapt_float_type(constraints: hydra.coders.LanguageConstraints, ft: hydra.core.FloatType) -> Maybe[hydra.core.FloatType]:
117
+ r"""Attempt to adapt a floating-point type using the given language constraints."""
118
+ @lru_cache(1)
119
+ def supported() -> bool:
120
+ return hydra.lib.sets.member(ft, constraints.float_types)
121
+ def alt(v1: hydra.core.FloatType) -> Maybe[hydra.core.FloatType]:
122
+ return adapt_float_type(constraints, v1)
123
+ def for_unsupported(ft2: hydra.core.FloatType) -> Maybe[hydra.core.FloatType]:
124
+ match ft2:
125
+ case hydra.core.FloatType.BIGFLOAT:
126
+ return alt(hydra.core.FloatType.FLOAT64)
127
+ case hydra.core.FloatType.FLOAT32:
128
+ return alt(hydra.core.FloatType.FLOAT64)
129
+ case hydra.core.FloatType.FLOAT64:
130
+ return alt(hydra.core.FloatType.BIGFLOAT)
131
+ case _:
132
+ raise AssertionError("Unreachable: all variants handled")
133
+ return hydra.lib.logic.if_else(supported(), (lambda : Just(ft)), (lambda : for_unsupported(ft)))
134
+ def adapt_integer_type(constraints: hydra.coders.LanguageConstraints, it: hydra.core.IntegerType) -> Maybe[hydra.core.IntegerType]:
135
+ r"""Attempt to adapt an integer type using the given language constraints."""
136
+ @lru_cache(1)
137
+ def supported() -> bool:
138
+ return hydra.lib.sets.member(it, constraints.integer_types)
139
+ def alt(v1: hydra.core.IntegerType) -> Maybe[hydra.core.IntegerType]:
140
+ return adapt_integer_type(constraints, v1)
141
+ def for_unsupported(it2: hydra.core.IntegerType) -> Maybe[hydra.core.IntegerType]:
142
+ match it2:
143
+ case hydra.core.IntegerType.BIGINT:
144
+ return Nothing()
145
+ case hydra.core.IntegerType.INT8:
146
+ return alt(hydra.core.IntegerType.UINT16)
147
+ case hydra.core.IntegerType.INT16:
148
+ return alt(hydra.core.IntegerType.UINT32)
149
+ case hydra.core.IntegerType.INT32:
150
+ return alt(hydra.core.IntegerType.UINT64)
151
+ case hydra.core.IntegerType.INT64:
152
+ return alt(hydra.core.IntegerType.BIGINT)
153
+ case hydra.core.IntegerType.UINT8:
154
+ return alt(hydra.core.IntegerType.INT16)
155
+ case hydra.core.IntegerType.UINT16:
156
+ return alt(hydra.core.IntegerType.INT32)
157
+ case hydra.core.IntegerType.UINT32:
158
+ return alt(hydra.core.IntegerType.INT64)
159
+ case hydra.core.IntegerType.UINT64:
160
+ return alt(hydra.core.IntegerType.BIGINT)
161
+ case _:
162
+ raise AssertionError("Unreachable: all variants handled")
163
+ return hydra.lib.logic.if_else(supported(), (lambda : Just(it)), (lambda : for_unsupported(it)))
164
+ def adapt_literal_type(constraints: hydra.coders.LanguageConstraints, lt: hydra.core.LiteralType) -> Maybe[hydra.core.LiteralType]:
165
+ r"""Attempt to adapt a literal type using the given language constraints."""
166
+ def for_unsupported(lt2: hydra.core.LiteralType) -> Maybe[hydra.core.LiteralType]:
167
+ match lt2:
168
+ case hydra.core.LiteralTypeBinary():
169
+ return Just(cast(hydra.core.LiteralType, hydra.core.LiteralTypeString()))
170
+ case hydra.core.LiteralTypeBoolean():
171
+ return hydra.lib.maybes.map((lambda x: cast(hydra.core.LiteralType, hydra.core.LiteralTypeInteger(x))), adapt_integer_type(constraints, hydra.core.IntegerType.INT8))
172
+ case hydra.core.LiteralTypeDecimal():
173
+ return Just(cast(hydra.core.LiteralType, hydra.core.LiteralTypeFloat(hydra.core.FloatType.FLOAT64)))
174
+ case hydra.core.LiteralTypeFloat(value=ft):
175
+ return hydra.lib.maybes.map((lambda x: cast(hydra.core.LiteralType, hydra.core.LiteralTypeFloat(x))), adapt_float_type(constraints, ft))
176
+ case hydra.core.LiteralTypeInteger(value=it):
177
+ return hydra.lib.maybes.map((lambda x: cast(hydra.core.LiteralType, hydra.core.LiteralTypeInteger(x))), adapt_integer_type(constraints, it))
178
+ case _:
179
+ return Nothing()
180
+ return hydra.lib.logic.if_else(literal_type_supported(constraints, lt), (lambda : Nothing()), (lambda : for_unsupported(lt)))
181
+ def adapt_literal_types_map(constraints: hydra.coders.LanguageConstraints) -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
182
+ r"""Derive a map of adapted literal types for the given language constraints."""
183
+ def try_type(lt: hydra.core.LiteralType) -> Maybe[tuple[hydra.core.LiteralType, hydra.core.LiteralType]]:
184
+ return hydra.lib.maybes.maybe((lambda : Nothing()), (lambda lt2: Just((lt, lt2))), adapt_literal_type(constraints, lt))
185
+ return hydra.lib.maps.from_list(hydra.lib.maybes.cat(hydra.lib.lists.map((lambda x1: try_type(x1)), hydra.reflect.literal_types())))
186
+ def adapt_type_scheme(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], ts0: hydra.core.TypeScheme) -> Either[hydra.errors.Error, hydra.core.TypeScheme]:
187
+ r"""Adapt a type scheme to the given language constraints, prior to inference."""
188
+ vars0 = ts0.variables
189
+ t0 = ts0.body
190
+ return hydra.lib.eithers.bind(adapt_type(constraints, litmap, t0), (lambda t1: Right(hydra.core.TypeScheme(vars0, t1, ts0.constraints))))
191
+ def adapt_nested_types(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], recurse: Callable[[T0], Either[hydra.errors.Error, hydra.core.Term]], term: T0):
192
+ def _hoist_hydra_adapt_adapt_nested_types_1(constraints, litmap, rewritten, v1):
193
+ match v1:
194
+ case hydra.core.TermLet(value=lt):
195
+ def adapt_b(b: hydra.core.Binding) -> Either[hydra.errors.Error, hydra.core.Binding]:
196
+ return hydra.lib.eithers.bind(hydra.lib.maybes.maybe((lambda : Right(Nothing())), (lambda ts: hydra.lib.eithers.bind(adapt_type_scheme(constraints, litmap, ts), (lambda ts1: Right(Just(ts1))))), b.type_scheme), (lambda adapted_b_type: Right(hydra.core.Binding(b.name, b.term, adapted_b_type))))
197
+ return hydra.lib.eithers.bind(hydra.lib.eithers.map_list((lambda x1: adapt_b(x1)), lt.bindings), (lambda adapted_bindings: Right(cast(hydra.core.Term, hydra.core.TermLet(hydra.core.Let(adapted_bindings, lt.body))))))
198
+ case _:
199
+ return Right(rewritten)
200
+ return hydra.lib.eithers.bind(recurse(term), (lambda rewritten: _hoist_hydra_adapt_adapt_nested_types_1(constraints, litmap, rewritten, rewritten)))
201
+ def adapt_primitive(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], prim0: hydra.graph.Primitive) -> Either[hydra.errors.Error, hydra.graph.Primitive]:
202
+ r"""Adapt a primitive to the given language constraints, prior to inference."""
203
+ ts0 = prim0.type_scheme
204
+ return hydra.lib.eithers.bind(adapt_type_scheme(constraints, litmap, ts0), (lambda ts1: Right(hydra.graph.Primitive(prim0.name, ts1, prim0.implementation))))
205
+ def adapt_literal(lt: hydra.core.LiteralType, l: hydra.core.Literal):
206
+ def _hoist_hydra_adapt_adapt_literal_1(b, v1):
207
+ match v1:
208
+ case hydra.core.LiteralTypeString():
209
+ return cast(hydra.core.Literal, hydra.core.LiteralString(hydra.lib.literals.binary_to_string(b)))
210
+ case _:
211
+ raise TypeError("Unsupported LiteralType")
212
+ def _hoist_hydra_adapt_adapt_literal_2(b, v1):
213
+ match v1:
214
+ case hydra.core.LiteralTypeInteger(value=it):
215
+ return cast(hydra.core.Literal, hydra.core.LiteralInteger(hydra.literals.bigint_to_integer_value(it, hydra.lib.logic.if_else(b, (lambda : 1), (lambda : 0)))))
216
+ case _:
217
+ raise TypeError("Unsupported LiteralType")
218
+ def _hoist_hydra_adapt_adapt_literal_3(d, v1):
219
+ match v1:
220
+ case hydra.core.LiteralTypeFloat():
221
+ return cast(hydra.core.Literal, hydra.core.LiteralFloat(cast(hydra.core.FloatValue, hydra.core.FloatValueFloat64(hydra.lib.literals.decimal_to_float64(d)))))
222
+ case hydra.core.LiteralTypeString():
223
+ return cast(hydra.core.Literal, hydra.core.LiteralString(hydra.lib.literals.show_decimal(d)))
224
+ case _:
225
+ raise TypeError("Unsupported LiteralType")
226
+ def _hoist_hydra_adapt_adapt_literal_4(f, v1):
227
+ match v1:
228
+ case hydra.core.LiteralTypeFloat(value=ft):
229
+ return cast(hydra.core.Literal, hydra.core.LiteralFloat(hydra.literals.bigfloat_to_float_value(ft, hydra.literals.float_value_to_bigfloat(f))))
230
+ case _:
231
+ raise TypeError("Unsupported LiteralType")
232
+ def _hoist_hydra_adapt_adapt_literal_5(i, v1):
233
+ match v1:
234
+ case hydra.core.LiteralTypeInteger(value=it):
235
+ return cast(hydra.core.Literal, hydra.core.LiteralInteger(hydra.literals.bigint_to_integer_value(it, hydra.literals.integer_value_to_bigint(i))))
236
+ case _:
237
+ raise TypeError("Unsupported LiteralType")
238
+ match l:
239
+ case hydra.core.LiteralBinary(value=b):
240
+ return _hoist_hydra_adapt_adapt_literal_1(b, lt)
241
+ case hydra.core.LiteralBoolean(value=b2):
242
+ return _hoist_hydra_adapt_adapt_literal_2(b2, lt)
243
+ case hydra.core.LiteralDecimal(value=d):
244
+ return _hoist_hydra_adapt_adapt_literal_3(d, lt)
245
+ case hydra.core.LiteralFloat(value=f):
246
+ return _hoist_hydra_adapt_adapt_literal_4(f, lt)
247
+ case hydra.core.LiteralInteger(value=i):
248
+ return _hoist_hydra_adapt_adapt_literal_5(i, lt)
249
+ case _:
250
+ raise TypeError("Unsupported Literal")
251
+ def adapt_literal_value(litmap: FrozenDict[T0, hydra.core.LiteralType], lt: T0, l: hydra.core.Literal) -> hydra.core.Literal:
252
+ r"""Adapt a literal value using the given language constraints."""
253
+ return hydra.lib.maybes.maybe((lambda : cast(hydra.core.Literal, hydra.core.LiteralString(hydra.show.core.literal(l)))), (lambda lt2: adapt_literal(lt2, l)), hydra.lib.maps.lookup(lt, litmap))
254
+ def term_alternatives(cx: T0, graph: hydra.graph.Graph, term: hydra.core.Term) -> Either[hydra.errors.Error, frozenlist[hydra.core.Term]]:
255
+ r"""Find a list of alternatives for a given term, if any."""
256
+ match term:
257
+ case hydra.core.TermAnnotated(value=at):
258
+ term2 = at.body
259
+ return Right((term2,))
260
+ case hydra.core.TermMaybe(value=ot):
261
+ return Right((cast(hydra.core.Term, hydra.core.TermList(hydra.lib.maybes.maybe((lambda : ()), (lambda term2: (term2,)), ot))),))
262
+ case hydra.core.TermTypeLambda(value=abs):
263
+ term2 = abs.body
264
+ return Right((term2,))
265
+ case hydra.core.TermTypeApplication(value=ta):
266
+ term2 = ta.body
267
+ return Right((term2,))
268
+ case hydra.core.TermInject(value=inj):
269
+ tname = inj.type_name
270
+ field = inj.field
271
+ fname = field.name
272
+ fterm = field.term
273
+ def for_field_type(ft: hydra.core.FieldType) -> hydra.core.Field:
274
+ ftname = ft.name
275
+ return hydra.core.Field(fname, cast(hydra.core.Term, hydra.core.TermMaybe(hydra.lib.logic.if_else(hydra.lib.equality.equal(ftname, fname), (lambda : Just(fterm)), (lambda : Nothing())))))
276
+ return hydra.lib.eithers.bind(hydra.resolution.require_union_type(cx, graph, tname), (lambda rt: Right((cast(hydra.core.Term, hydra.core.TermRecord(hydra.core.Record(tname, hydra.lib.lists.map((lambda x1: for_field_type(x1)), rt)))),))))
277
+ case hydra.core.TermUnit():
278
+ return Right((cast(hydra.core.Term, hydra.core.TermLiteral(cast(hydra.core.Literal, hydra.core.LiteralBoolean(True)))),))
279
+ case hydra.core.TermWrap(value=wt):
280
+ term2 = wt.body
281
+ return Right((term2,))
282
+ case _:
283
+ return Right(())
284
+ def adapt_term(constraints: hydra.coders.LanguageConstraints, litmap: FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType], cx: T0, graph: hydra.graph.Graph, term0: hydra.core.Term) -> Either[hydra.errors.Error, hydra.core.Term]:
285
+ r"""Adapt a term using the given language constraints."""
286
+ def rewrite(recurse: Callable[[T1], Either[hydra.errors.Error, hydra.core.Term]], term02: T1):
287
+ def for_supported(term: hydra.core.Term) -> Either[T2, Maybe[hydra.core.Term]]:
288
+ match term:
289
+ case hydra.core.TermLiteral(value=l):
290
+ @lru_cache(1)
291
+ def lt() -> hydra.core.LiteralType:
292
+ return hydra.reflect.literal_type(l)
293
+ return Right(Just(hydra.lib.logic.if_else(literal_type_supported(constraints, lt()), (lambda : term), (lambda : cast(hydra.core.Term, hydra.core.TermLiteral(adapt_literal_value(litmap, lt(), l)))))))
294
+ case _:
295
+ return Right(Just(term))
296
+ def for_unsupported(term: hydra.core.Term) -> Either[hydra.errors.Error, Maybe[hydra.core.Term]]:
297
+ def try_alts(alts: frozenlist[hydra.core.Term]) -> Either[hydra.errors.Error, Maybe[hydra.core.Term]]:
298
+ return hydra.lib.maybes.maybe((lambda : Right(Nothing())), (lambda uc: hydra.lib.eithers.bind(try_term(hydra.lib.pairs.first(uc)), (lambda mterm: hydra.lib.maybes.maybe((lambda : try_alts(hydra.lib.pairs.second(uc))), (lambda t: Right(Just(t))), mterm)))), hydra.lib.lists.uncons(alts))
299
+ return hydra.lib.eithers.bind(term_alternatives(cx, graph, term), (lambda alts0: try_alts(alts0)))
300
+ def try_term(term: hydra.core.Term) -> Either[hydra.errors.Error, Maybe[hydra.core.Term]]:
301
+ @lru_cache(1)
302
+ def supported_variant() -> bool:
303
+ return hydra.lib.sets.member(hydra.reflect.term_variant(term), constraints.term_variants)
304
+ return hydra.lib.logic.if_else(supported_variant(), (lambda : for_supported(term)), (lambda : for_unsupported(term)))
305
+ def _hoist_for_supported_body_1(term1, v1):
306
+ match v1:
307
+ case hydra.core.TermTypeApplication(value=ta):
308
+ return hydra.lib.eithers.bind(adapt_type(constraints, litmap, ta.type), (lambda atyp: Right(cast(hydra.core.Term, hydra.core.TermTypeApplication(hydra.core.TypeApplicationTerm(ta.body, atyp))))))
309
+ case hydra.core.TermTypeLambda():
310
+ return Right(term1)
311
+ case _:
312
+ return hydra.lib.eithers.bind(try_term(term1), (lambda mterm: hydra.lib.maybes.maybe((lambda : Left(cast(hydra.errors.Error, hydra.errors.ErrorOther(hydra.errors.OtherError(hydra.lib.strings.cat2("no alternatives for term: ", hydra.show.core.term(term1))))))), (lambda term2: Right(term2)), mterm)))
313
+ return hydra.lib.eithers.bind(recurse(term02), (lambda term1: _hoist_for_supported_body_1(term1, term1)))
314
+ return hydra.rewriting.rewrite_term_m((lambda x1, x2: rewrite(x1, x2)), term0)
315
+ def push_type_apps_inward(term: hydra.core.Term) -> hydra.core.Term:
316
+ r"""Normalize a term by pushing TermTypeApplication inward past TermApplication and TermLambda. This corrects structures produced by poly-let hoisting and eta expansion, where type applications from inference end up wrapping term applications or lambda abstractions instead of being directly on the polymorphic variable."""
317
+ def push(body: hydra.core.Term, typ: hydra.core.Type) -> hydra.core.Term:
318
+ match body:
319
+ case hydra.core.TermApplication(value=a):
320
+ return go(cast(hydra.core.Term, hydra.core.TermApplication(hydra.core.Application(cast(hydra.core.Term, hydra.core.TermTypeApplication(hydra.core.TypeApplicationTerm(a.function, typ))), a.argument))))
321
+ case hydra.core.TermLambda(value=l):
322
+ return go(cast(hydra.core.Term, hydra.core.TermLambda(hydra.core.Lambda(l.parameter, l.domain, cast(hydra.core.Term, hydra.core.TermTypeApplication(hydra.core.TypeApplicationTerm(l.body, typ)))))))
323
+ case hydra.core.TermLet(value=lt):
324
+ return go(cast(hydra.core.Term, hydra.core.TermLet(hydra.core.Let(lt.bindings, cast(hydra.core.Term, hydra.core.TermTypeApplication(hydra.core.TypeApplicationTerm(lt.body, typ)))))))
325
+ case _:
326
+ return cast(hydra.core.Term, hydra.core.TermTypeApplication(hydra.core.TypeApplicationTerm(body, typ)))
327
+ def go(t: hydra.core.Term) -> hydra.core.Term:
328
+ def for_field(fld: hydra.core.Field) -> hydra.core.Field:
329
+ return hydra.core.Field(fld.name, go(fld.term))
330
+ def for_let(lt: hydra.core.Let) -> hydra.core.Let:
331
+ def map_binding(b: hydra.core.Binding) -> hydra.core.Binding:
332
+ return hydra.core.Binding(b.name, go(b.term), b.type_scheme)
333
+ return hydra.core.Let(hydra.lib.lists.map((lambda x1: map_binding(x1)), lt.bindings), go(lt.body))
334
+ def for_map(m: FrozenDict[hydra.core.Term, hydra.core.Term]) -> FrozenDict[hydra.core.Term, hydra.core.Term]:
335
+ def for_pair(p: tuple[hydra.core.Term, hydra.core.Term]) -> tuple[hydra.core.Term, hydra.core.Term]:
336
+ return (go(hydra.lib.pairs.first(p)), go(hydra.lib.pairs.second(p)))
337
+ return hydra.lib.maps.from_list(hydra.lib.lists.map((lambda x1: for_pair(x1)), hydra.lib.maps.to_list(m)))
338
+ match t:
339
+ case hydra.core.TermAnnotated(value=at):
340
+ return cast(hydra.core.Term, hydra.core.TermAnnotated(hydra.core.AnnotatedTerm(go(at.body), at.annotation)))
341
+ case hydra.core.TermApplication(value=a):
342
+ return cast(hydra.core.Term, hydra.core.TermApplication(hydra.core.Application(go(a.function), go(a.argument))))
343
+ case hydra.core.TermCases(value=cs):
344
+ return cast(hydra.core.Term, hydra.core.TermCases(hydra.core.CaseStatement(cs.type_name, hydra.lib.maybes.map((lambda x1: go(x1)), cs.default), hydra.lib.lists.map((lambda x1: for_field(x1)), cs.cases))))
345
+ case hydra.core.TermEither(value=e):
346
+ return cast(hydra.core.Term, hydra.core.TermEither(hydra.lib.eithers.either((lambda l: Left(go(l))), (lambda r: Right(go(r))), e)))
347
+ case hydra.core.TermLambda(value=l):
348
+ return cast(hydra.core.Term, hydra.core.TermLambda(hydra.core.Lambda(l.parameter, l.domain, go(l.body))))
349
+ case hydra.core.TermLet(value=lt):
350
+ return cast(hydra.core.Term, hydra.core.TermLet(for_let(lt)))
351
+ case hydra.core.TermList(value=els):
352
+ return cast(hydra.core.Term, hydra.core.TermList(hydra.lib.lists.map((lambda x1: go(x1)), els)))
353
+ case hydra.core.TermLiteral(value=v):
354
+ return cast(hydra.core.Term, hydra.core.TermLiteral(v))
355
+ case hydra.core.TermMap(value=m):
356
+ return cast(hydra.core.Term, hydra.core.TermMap(for_map(m)))
357
+ case hydra.core.TermMaybe(value=m2):
358
+ return cast(hydra.core.Term, hydra.core.TermMaybe(hydra.lib.maybes.map((lambda x1: go(x1)), m2)))
359
+ case hydra.core.TermPair(value=p):
360
+ return cast(hydra.core.Term, hydra.core.TermPair((go(hydra.lib.pairs.first(p)), go(hydra.lib.pairs.second(p)))))
361
+ case hydra.core.TermProject(value=p2):
362
+ return cast(hydra.core.Term, hydra.core.TermProject(p2))
363
+ case hydra.core.TermRecord(value=r):
364
+ return cast(hydra.core.Term, hydra.core.TermRecord(hydra.core.Record(r.type_name, hydra.lib.lists.map((lambda x1: for_field(x1)), r.fields))))
365
+ case hydra.core.TermSet(value=s):
366
+ return cast(hydra.core.Term, hydra.core.TermSet(hydra.lib.sets.from_list(hydra.lib.lists.map((lambda x1: go(x1)), hydra.lib.sets.to_list(s)))))
367
+ case hydra.core.TermTypeApplication(value=tt):
368
+ @lru_cache(1)
369
+ def body1() -> hydra.core.Term:
370
+ return go(tt.body)
371
+ return push(body1(), tt.type)
372
+ case hydra.core.TermTypeLambda(value=ta):
373
+ return cast(hydra.core.Term, hydra.core.TermTypeLambda(hydra.core.TypeLambda(ta.parameter, go(ta.body))))
374
+ case hydra.core.TermInject(value=i):
375
+ return cast(hydra.core.Term, hydra.core.TermInject(hydra.core.Injection(i.type_name, for_field(i.field))))
376
+ case hydra.core.TermUnit():
377
+ return cast(hydra.core.Term, hydra.core.TermUnit())
378
+ case hydra.core.TermUnwrap(value=n):
379
+ return cast(hydra.core.Term, hydra.core.TermUnwrap(n))
380
+ case hydra.core.TermVariable(value=v2):
381
+ return cast(hydra.core.Term, hydra.core.TermVariable(v2))
382
+ case hydra.core.TermWrap(value=wt):
383
+ return cast(hydra.core.Term, hydra.core.TermWrap(hydra.core.WrappedTerm(wt.type_name, go(wt.body))))
384
+ case _:
385
+ raise AssertionError("Unreachable: all variants handled")
386
+ return go(term)
387
+ def adapt_data_graph(constraints: hydra.coders.LanguageConstraints, do_expand: bool, els0: frozenlist[hydra.core.Binding], cx: T0, graph0: hydra.graph.Graph) -> Either[hydra.errors.Error, tuple[hydra.graph.Graph, frozenlist[hydra.core.Binding]]]:
388
+ r"""Adapt a graph and its schema to the given language constraints. The doExpand flag controls eta expansion of partial applications. Adaptation is type-preserving: binding-level TypeSchemes are adapted (not stripped). Note: case statement hoisting is done separately, prior to adaptation. The els0 parameter provides the original ordered bindings. Returns both the adapted graph and the ordered adapted bindings."""
389
+ def transform_term(g: hydra.graph.Graph, term: hydra.core.Term) -> hydra.core.Term:
390
+ tx = g
391
+ @lru_cache(1)
392
+ def t1() -> hydra.core.Term:
393
+ return hydra.variables.unshadow_variables(push_type_apps_inward(term))
394
+ @lru_cache(1)
395
+ def t2() -> hydra.core.Term:
396
+ return hydra.variables.unshadow_variables(hydra.lib.logic.if_else(do_expand, (lambda : push_type_apps_inward(hydra.reduction.eta_expand_term(tx, t1()))), (lambda : t1())))
397
+ return hydra.dependencies.lift_lambda_above_let(t2())
398
+ def transform_binding(g: hydra.graph.Graph, el: hydra.core.Binding) -> hydra.core.Binding:
399
+ return hydra.core.Binding(el.name, transform_term(g, el.term), el.type_scheme)
400
+ @lru_cache(1)
401
+ def litmap() -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
402
+ return adapt_literal_types_map(constraints)
403
+ prims0 = graph0.primitives
404
+ schema_types0 = graph0.schema_types
405
+ @lru_cache(1)
406
+ def schema_bindings() -> frozenlist[hydra.core.Binding]:
407
+ return hydra.environment.types_to_definitions(hydra.lib.maps.map((lambda ts: hydra.scoping.type_scheme_to_f_type(ts)), schema_types0))
408
+ return hydra.lib.eithers.bind(hydra.lib.logic.if_else(hydra.lib.maps.null(schema_types0), (lambda : Right(hydra.lib.maps.empty())), (lambda : hydra.lib.eithers.bind(hydra.lib.eithers.bimap((lambda e: cast(hydra.errors.Error, hydra.errors.ErrorDecoding(e))), (lambda x: x), hydra.environment.graph_as_types(graph0, schema_bindings())), (lambda tmap0: hydra.lib.eithers.bind(adapt_graph_schema(constraints, litmap(), tmap0), (lambda tmap1: Right(hydra.lib.maps.map((lambda t: hydra.resolution.type_to_type_scheme(t)), tmap1)))))))), (lambda schema_result: (adapted_schema_types := schema_result, adapt_binding := (lambda el: (transformed := transform_binding(graph0, el), wrapped := cast(hydra.core.Term, hydra.core.TermLet(hydra.core.Let(hydra.lib.lists.pure(transformed), cast(hydra.core.Term, hydra.core.TermUnit())))), hydra.lib.eithers.bind(adapt_term(constraints, litmap(), cx, graph0, wrapped), (lambda adapted: hydra.rewriting.rewrite_term_m((lambda v1, v2: adapt_lambda_domains(constraints, litmap(), v1, v2)), adapted))))[2]), hydra.lib.eithers.bind(hydra.lib.eithers.map_list((lambda x1: adapt_binding(x1)), els0), (lambda adapted_terms: (els1_raw := hydra.lib.lists.concat(hydra.lib.lists.map((lambda x1: hydra.environment.term_as_bindings(x1)), adapted_terms)), process_binding := (lambda el: hydra.lib.eithers.bind(hydra.rewriting.rewrite_term_m((lambda v1, v2: adapt_nested_types(constraints, litmap(), v1, v2)), el.term), (lambda new_term: hydra.lib.eithers.bind(hydra.lib.maybes.maybe((lambda : Right(Nothing())), (lambda ts: hydra.lib.eithers.bind(adapt_type_scheme(constraints, litmap(), ts), (lambda ts1: Right(Just(ts1))))), el.type_scheme), (lambda adapted_type: Right(hydra.core.Binding(el.name, new_term, adapted_type))))))), hydra.lib.eithers.bind(hydra.lib.eithers.map_list((lambda x1: process_binding(x1)), els1_raw), (lambda els1: hydra.lib.eithers.bind(hydra.lib.eithers.map_list((lambda kv: hydra.lib.eithers.bind(adapt_primitive(constraints, litmap(), hydra.lib.pairs.second(kv)), (lambda prim1: Right((hydra.lib.pairs.first(kv), prim1))))), hydra.lib.maps.to_list(prims0)), (lambda prim_pairs: (prims1 := hydra.lib.maps.from_list(prim_pairs), adapted_graph_raw := hydra.lexical.build_graph(els1, hydra.lib.maps.empty(), prims1), adapted_graph := hydra.graph.Graph(adapted_graph_raw.bound_terms, adapted_graph_raw.bound_types, adapted_graph_raw.class_constraints, adapted_graph_raw.lambda_variables, adapted_graph_raw.metadata, adapted_graph_raw.primitives, adapted_schema_types, adapted_graph_raw.type_variables), Right((adapted_graph, els1)))[3])))))[2])))[2]))
409
+ def adapt_term_for_language(lang: hydra.coders.Language, cx: T0, g: hydra.graph.Graph, term: hydra.core.Term) -> Either[hydra.errors.Error, hydra.core.Term]:
410
+ r"""Adapt a term using the constraints of a given language."""
411
+ constraints = lang.constraints
412
+ @lru_cache(1)
413
+ def litmap() -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
414
+ return adapt_literal_types_map(constraints)
415
+ return adapt_term(constraints, litmap(), cx, g, term)
416
+ def adapt_type_for_language(lang: hydra.coders.Language, typ: hydra.core.Type) -> Either[hydra.errors.Error, hydra.core.Type]:
417
+ r"""Adapt a type using the constraints of a given language."""
418
+ constraints = lang.constraints
419
+ @lru_cache(1)
420
+ def litmap() -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
421
+ return adapt_literal_types_map(constraints)
422
+ return adapt_type(constraints, litmap(), typ)
423
+ def compose_coders(c1: hydra.coders.Coder[T0, T1], c2: hydra.coders.Coder[T1, T2]) -> hydra.coders.Coder[T0, T2]:
424
+ r"""Compose two coders into a single coder."""
425
+ return hydra.coders.Coder((lambda cx, a: hydra.lib.eithers.bind(c1.encode(cx, a), (lambda b1: c2.encode(cx, b1)))), (lambda cx, c: hydra.lib.eithers.bind(c2.decode(cx, c), (lambda b2: c1.decode(cx, b2)))))
426
+ def data_graph_to_definitions(constraints: hydra.coders.LanguageConstraints, do_infer: bool, do_expand: bool, do_hoist_case_statements: bool, do_hoist_polymorphic_let_bindings: bool, original_bindings: frozenlist[hydra.core.Binding], graph0: hydra.graph.Graph, namespaces: frozenlist[hydra.packaging.Namespace], cx: hydra.context.Context) -> Either[hydra.errors.Error, tuple[hydra.graph.Graph, frozenlist[frozenlist[hydra.packaging.TermDefinition]]]]:
427
+ r"""Given a data graph along with language constraints, original ordered bindings, and a designated list of namespaces, adapt the graph to the language constraints, then return the processed graph along with term definitions grouped by namespace (in the order of the input namespaces). Inference is performed before adaptation if bindings lack type annotations. Hoisting must preserve type schemes; if any binding loses its type scheme after hoisting, the pipeline fails. Adaptation preserves type application/lambda wrappers and adapts embedded types. Post-adaptation inference is performed to ensure binding TypeSchemes are fully consistent. The doExpand flag controls eta expansion. The doHoistCaseStatements flag controls case statement hoisting (needed for Python). The doHoistPolymorphicLetBindings flag controls polymorphic let binding hoisting (needed for Java). The originalBindings parameter provides the original ordered bindings (from module elements)."""
428
+ @lru_cache(1)
429
+ def namespaces_set() -> frozenset[hydra.packaging.Namespace]:
430
+ return hydra.lib.sets.from_list(namespaces)
431
+ def is_parent_binding(b: hydra.core.Binding) -> bool:
432
+ return hydra.lib.maybes.maybe((lambda : False), (lambda ns: hydra.lib.sets.member(ns, namespaces_set())), hydra.names.namespace_of(b.name))
433
+ def hoist_cases(bindings: frozenlist[hydra.core.Binding]) -> frozenlist[hydra.core.Binding]:
434
+ @lru_cache(1)
435
+ def stripped() -> frozenlist[hydra.core.Binding]:
436
+ return hydra.lib.lists.map((lambda b: hydra.core.Binding(b.name, hydra.strip.strip_type_lambdas(b.term), b.type_scheme)), bindings)
437
+ @lru_cache(1)
438
+ def term0() -> hydra.core.Term:
439
+ return cast(hydra.core.Term, hydra.core.TermLet(hydra.core.Let(stripped(), cast(hydra.core.Term, hydra.core.TermUnit()))))
440
+ @lru_cache(1)
441
+ def unshadowed0() -> frozenlist[hydra.core.Binding]:
442
+ return hydra.environment.term_as_bindings(hydra.variables.unshadow_variables(term0()))
443
+ @lru_cache(1)
444
+ def hoisted() -> frozenlist[hydra.core.Binding]:
445
+ return hydra.hoisting.hoist_case_statements_in_graph(unshadowed0())
446
+ @lru_cache(1)
447
+ def term1() -> hydra.core.Term:
448
+ return cast(hydra.core.Term, hydra.core.TermLet(hydra.core.Let(hoisted(), cast(hydra.core.Term, hydra.core.TermUnit()))))
449
+ return hydra.environment.term_as_bindings(hydra.variables.unshadow_variables(term1()))
450
+ def hoist_poly(bindings: frozenlist[hydra.core.Binding]) -> frozenlist[hydra.core.Binding]:
451
+ @lru_cache(1)
452
+ def let_before() -> hydra.core.Let:
453
+ return hydra.core.Let(bindings, cast(hydra.core.Term, hydra.core.TermUnit()))
454
+ @lru_cache(1)
455
+ def let_after() -> hydra.core.Let:
456
+ return hydra.hoisting.hoist_polymorphic_let_bindings((lambda x1: is_parent_binding(x1)), let_before())
457
+ return let_after().bindings
458
+ def check_bindings_typed(debug_label: str, bindings: frozenlist[hydra.core.Binding]) -> Either[hydra.errors.Error, frozenlist[hydra.core.Binding]]:
459
+ @lru_cache(1)
460
+ def untyped_bindings() -> frozenlist[str]:
461
+ return hydra.lib.lists.map((lambda b: b.name.value), hydra.lib.lists.filter((lambda b: hydra.lib.logic.not_(hydra.lib.maybes.is_just(b.type_scheme))), bindings))
462
+ return hydra.lib.logic.if_else(hydra.lib.lists.null(untyped_bindings()), (lambda : Right(bindings)), (lambda : Left(cast(hydra.errors.Error, hydra.errors.ErrorOther(hydra.errors.OtherError(hydra.lib.strings.cat(("Found untyped bindings (", debug_label, "): ", hydra.lib.strings.intercalate(", ", untyped_bindings())))))))))
463
+ def normalize_bindings(bindings: frozenlist[hydra.core.Binding]) -> frozenlist[hydra.core.Binding]:
464
+ return hydra.lib.lists.map((lambda b: hydra.core.Binding(b.name, push_type_apps_inward(b.term), b.type_scheme)), bindings)
465
+ def rebuild_graph(bindings: frozenlist[hydra.core.Binding]) -> hydra.graph.Graph:
466
+ @lru_cache(1)
467
+ def g() -> hydra.graph.Graph:
468
+ return hydra.lexical.build_graph(bindings, hydra.lib.maps.empty(), graph0.primitives)
469
+ return hydra.graph.Graph(g().bound_terms, g().bound_types, g().class_constraints, g().lambda_variables, g().metadata, g().primitives, graph0.schema_types, g().type_variables)
470
+ bins0 = original_bindings
471
+ @lru_cache(1)
472
+ def bins1() -> frozenlist[hydra.core.Binding]:
473
+ return hydra.lib.logic.if_else(do_hoist_case_statements, (lambda : hoist_cases(bins0)), (lambda : bins0))
474
+ return hydra.lib.eithers.bind(hydra.lib.logic.if_else(do_infer, (lambda : hydra.lib.eithers.map((lambda result: hydra.lib.pairs.second(hydra.lib.pairs.first(result))), hydra.inference.infer_graph_types(cx, bins1(), rebuild_graph(bins1())))), (lambda : check_bindings_typed("after case hoisting", bins1()))), (lambda bins2: hydra.lib.eithers.bind(hydra.lib.logic.if_else(do_hoist_polymorphic_let_bindings, (lambda : check_bindings_typed("after let hoisting", hoist_poly(bins2))), (lambda : Right(bins2))), (lambda bins3: hydra.lib.eithers.bind(adapt_data_graph(constraints, do_expand, bins3, cx, rebuild_graph(bins3)), (lambda adapt_result: (adapted := hydra.lib.pairs.first(adapt_result), adapted_bindings := hydra.lib.pairs.second(adapt_result), hydra.lib.eithers.bind(check_bindings_typed("after adaptation", adapted_bindings), (lambda bins4: (bins5 := normalize_bindings(bins4), to_def := (lambda el: hydra.lib.maybes.map((lambda ts: hydra.packaging.TermDefinition(el.name, el.term, Just(ts))), el.type_scheme)), selected_elements := hydra.lib.lists.filter((lambda el: hydra.lib.maybes.maybe((lambda : False), (lambda ns: hydra.lib.sets.member(ns, namespaces_set())), hydra.names.namespace_of(el.name))), bins5), elements_by_namespace := hydra.lib.lists.foldl((lambda acc, el: hydra.lib.maybes.maybe((lambda : acc), (lambda ns: (existing := hydra.lib.maybes.maybe((lambda : ()), (lambda x1: hydra.lib.equality.identity(x1)), hydra.lib.maps.lookup(ns, acc)), hydra.lib.maps.insert(ns, hydra.lib.lists.concat2(existing, (el,)), acc))[1]), hydra.names.namespace_of(el.name))), hydra.lib.maps.empty(), selected_elements), defs_grouped := hydra.lib.lists.map((lambda ns: (els_for_ns := hydra.lib.maybes.maybe((lambda : ()), (lambda x1: hydra.lib.equality.identity(x1)), hydra.lib.maps.lookup(ns, elements_by_namespace)), hydra.lib.maybes.cat(hydra.lib.lists.map((lambda x1: to_def(x1)), els_for_ns)))[1]), namespaces), g := hydra.lexical.build_graph(bins5, hydra.lib.maps.empty(), adapted.primitives), Right((hydra.graph.Graph(g.bound_terms, g.bound_types, g.class_constraints, g.lambda_variables, g.metadata, g.primitives, adapted.schema_types, g.type_variables), defs_grouped)))[6])))[2]))))))
475
+ def prepare_same(x: T0) -> tuple[T0, tuple[Callable[[T1], T1], frozenset[T2]]]:
476
+ r"""Return a value unchanged with identity transform and no messages."""
477
+ return (x, ((lambda y: y), hydra.lib.sets.empty()))
478
+ def prepare_float_type(ft: hydra.core.FloatType):
479
+ def _hoist_hydra_adapt_prepare_float_type_1(v, v1):
480
+ match v1:
481
+ case hydra.core.FloatValueBigfloat(value=d):
482
+ return cast(hydra.core.FloatValue, hydra.core.FloatValueFloat64(hydra.lib.literals.bigfloat_to_float64(d)))
483
+ case _:
484
+ return v
485
+ match ft:
486
+ case hydra.core.FloatType.BIGFLOAT:
487
+ return (hydra.core.FloatType.FLOAT64, ((lambda v: _hoist_hydra_adapt_prepare_float_type_1(v, v)), hydra.lib.sets.from_list(("replace arbitrary-precision floating-point numbers with 64-bit floating-point numbers (doubles)",))))
488
+ case _:
489
+ return prepare_same(ft)
490
+ def prepare_integer_type(it: hydra.core.IntegerType):
491
+ def _hoist_hydra_adapt_prepare_integer_type_1(v, v1):
492
+ match v1:
493
+ case hydra.core.IntegerValueBigint(value=i):
494
+ return cast(hydra.core.IntegerValue, hydra.core.IntegerValueInt64(hydra.lib.literals.bigint_to_int64(i)))
495
+ case _:
496
+ return v
497
+ def _hoist_hydra_adapt_prepare_integer_type_2(v, v1):
498
+ match v1:
499
+ case hydra.core.IntegerValueUint8(value=i):
500
+ return cast(hydra.core.IntegerValue, hydra.core.IntegerValueInt8(hydra.lib.literals.bigint_to_int8(hydra.lib.literals.uint8_to_bigint(i))))
501
+ case _:
502
+ return v
503
+ def _hoist_hydra_adapt_prepare_integer_type_3(v, v1):
504
+ match v1:
505
+ case hydra.core.IntegerValueUint32(value=i):
506
+ return cast(hydra.core.IntegerValue, hydra.core.IntegerValueInt32(hydra.lib.literals.bigint_to_int32(hydra.lib.literals.uint32_to_bigint(i))))
507
+ case _:
508
+ return v
509
+ def _hoist_hydra_adapt_prepare_integer_type_4(v, v1):
510
+ match v1:
511
+ case hydra.core.IntegerValueUint64(value=i):
512
+ return cast(hydra.core.IntegerValue, hydra.core.IntegerValueInt64(hydra.lib.literals.bigint_to_int64(hydra.lib.literals.uint64_to_bigint(i))))
513
+ case _:
514
+ return v
515
+ match it:
516
+ case hydra.core.IntegerType.BIGINT:
517
+ return (hydra.core.IntegerType.INT64, ((lambda v: _hoist_hydra_adapt_prepare_integer_type_1(v, v)), hydra.lib.sets.from_list(("replace arbitrary-precision integers with 64-bit integers",))))
518
+ case hydra.core.IntegerType.UINT8:
519
+ return (hydra.core.IntegerType.INT8, ((lambda v: _hoist_hydra_adapt_prepare_integer_type_2(v, v)), hydra.lib.sets.from_list(("replace unsigned 8-bit integers with signed 8-bit integers",))))
520
+ case hydra.core.IntegerType.UINT32:
521
+ return (hydra.core.IntegerType.INT32, ((lambda v: _hoist_hydra_adapt_prepare_integer_type_3(v, v)), hydra.lib.sets.from_list(("replace unsigned 32-bit integers with signed 32-bit integers",))))
522
+ case hydra.core.IntegerType.UINT64:
523
+ return (hydra.core.IntegerType.INT64, ((lambda v: _hoist_hydra_adapt_prepare_integer_type_4(v, v)), hydra.lib.sets.from_list(("replace unsigned 64-bit integers with signed 64-bit integers",))))
524
+ case _:
525
+ return prepare_same(it)
526
+ def prepare_literal_type(at: hydra.core.LiteralType):
527
+ def _hoist_hydra_adapt_prepare_literal_type_1(v, v1):
528
+ match v1:
529
+ case hydra.core.LiteralBinary(value=b):
530
+ return cast(hydra.core.Literal, hydra.core.LiteralString(hydra.lib.literals.binary_to_string(b)))
531
+ case _:
532
+ return v
533
+ def _hoist_hydra_adapt_prepare_literal_type_2(v, v1):
534
+ match v1:
535
+ case hydra.core.LiteralDecimal(value=d):
536
+ return cast(hydra.core.Literal, hydra.core.LiteralFloat(cast(hydra.core.FloatValue, hydra.core.FloatValueFloat64(hydra.lib.literals.decimal_to_float64(d)))))
537
+ case _:
538
+ return v
539
+ match at:
540
+ case hydra.core.LiteralTypeBinary():
541
+ return (cast(hydra.core.LiteralType, hydra.core.LiteralTypeString()), ((lambda v: _hoist_hydra_adapt_prepare_literal_type_1(v, v)), hydra.lib.sets.from_list(("replace binary strings with character strings",))))
542
+ case hydra.core.LiteralTypeDecimal():
543
+ return (cast(hydra.core.LiteralType, hydra.core.LiteralTypeFloat(hydra.core.FloatType.FLOAT64)), ((lambda v: _hoist_hydra_adapt_prepare_literal_type_2(v, v)), hydra.lib.sets.from_list(("replace arbitrary-precision decimal numbers with 64-bit floating-point numbers (doubles)",))))
544
+ case hydra.core.LiteralTypeFloat(value=ft):
545
+ @lru_cache(1)
546
+ def result() -> tuple[hydra.core.FloatType, tuple[Callable[[hydra.core.FloatValue], hydra.core.FloatValue], frozenset[str]]]:
547
+ return prepare_float_type(ft)
548
+ @lru_cache(1)
549
+ def rtyp() -> hydra.core.FloatType:
550
+ return hydra.lib.pairs.first(result())
551
+ @lru_cache(1)
552
+ def rep() -> Callable[[hydra.core.FloatValue], hydra.core.FloatValue]:
553
+ return hydra.lib.pairs.first(hydra.lib.pairs.second(result()))
554
+ @lru_cache(1)
555
+ def msgs() -> frozenset[str]:
556
+ return hydra.lib.pairs.second(hydra.lib.pairs.second(result()))
557
+ def _hoist_result_body_1(v, v1):
558
+ match v1:
559
+ case hydra.core.LiteralFloat(value=fv):
560
+ return cast(hydra.core.Literal, hydra.core.LiteralFloat(rep(fv)))
561
+ case _:
562
+ return v
563
+ return (cast(hydra.core.LiteralType, hydra.core.LiteralTypeFloat(rtyp())), ((lambda v: _hoist_result_body_1(v, v)), msgs()))
564
+ case hydra.core.LiteralTypeInteger(value=it):
565
+ @lru_cache(1)
566
+ def result() -> tuple[hydra.core.IntegerType, tuple[Callable[[hydra.core.IntegerValue], hydra.core.IntegerValue], frozenset[str]]]:
567
+ return prepare_integer_type(it)
568
+ @lru_cache(1)
569
+ def rtyp() -> hydra.core.IntegerType:
570
+ return hydra.lib.pairs.first(result())
571
+ @lru_cache(1)
572
+ def rep() -> Callable[[hydra.core.IntegerValue], hydra.core.IntegerValue]:
573
+ return hydra.lib.pairs.first(hydra.lib.pairs.second(result()))
574
+ @lru_cache(1)
575
+ def msgs() -> frozenset[str]:
576
+ return hydra.lib.pairs.second(hydra.lib.pairs.second(result()))
577
+ def _hoist_result_body_1(v, v1):
578
+ match v1:
579
+ case hydra.core.LiteralInteger(value=iv):
580
+ return cast(hydra.core.Literal, hydra.core.LiteralInteger(rep(iv)))
581
+ case _:
582
+ return v
583
+ return (cast(hydra.core.LiteralType, hydra.core.LiteralTypeInteger(rtyp())), ((lambda v: _hoist_result_body_1(v, v)), msgs()))
584
+ case _:
585
+ return prepare_same(at)
586
+ def prepare_type(cx: T0, typ: hydra.core.Type):
587
+ r"""Prepare a type, substituting unsupported literal types."""
588
+ match hydra.strip.deannotate_type(typ):
589
+ case hydra.core.TypeLiteral(value=at):
590
+ @lru_cache(1)
591
+ def result() -> tuple[hydra.core.LiteralType, tuple[Callable[[hydra.core.Literal], hydra.core.Literal], frozenset[str]]]:
592
+ return prepare_literal_type(at)
593
+ @lru_cache(1)
594
+ def rtyp() -> hydra.core.LiteralType:
595
+ return hydra.lib.pairs.first(result())
596
+ @lru_cache(1)
597
+ def rep() -> Callable[[hydra.core.Literal], hydra.core.Literal]:
598
+ return hydra.lib.pairs.first(hydra.lib.pairs.second(result()))
599
+ @lru_cache(1)
600
+ def msgs() -> frozenset[str]:
601
+ return hydra.lib.pairs.second(hydra.lib.pairs.second(result()))
602
+ def _hoist_result_body_1(v, v1):
603
+ match v1:
604
+ case hydra.core.TermLiteral(value=av):
605
+ return cast(hydra.core.Term, hydra.core.TermLiteral(rep(av)))
606
+ case _:
607
+ return v
608
+ return (cast(hydra.core.Type, hydra.core.TypeLiteral(rtyp())), ((lambda v: _hoist_result_body_1(v, v)), msgs()))
609
+ case _:
610
+ return prepare_same(typ)
611
+ def schema_graph_to_definitions(constraints: hydra.coders.LanguageConstraints, graph: hydra.graph.Graph, name_lists: frozenlist[frozenlist[hydra.core.Name]], cx: T0) -> Either[hydra.errors.Error, tuple[FrozenDict[hydra.core.Name, hydra.core.Type], frozenlist[frozenlist[hydra.packaging.TypeDefinition]]]]:
612
+ r"""Given a schema graph along with language constraints and a designated list of element names, adapt the graph to the language constraints, then return a corresponding type definition for each element name."""
613
+ @lru_cache(1)
614
+ def litmap() -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
615
+ return adapt_literal_types_map(constraints)
616
+ return hydra.lib.eithers.bind(hydra.lib.eithers.bimap((lambda e: cast(hydra.errors.Error, hydra.errors.ErrorDecoding(e))), (lambda x: x), hydra.environment.graph_as_types(graph, hydra.lexical.graph_to_bindings(graph))), (lambda tmap0: hydra.lib.eithers.bind(adapt_graph_schema(constraints, litmap(), tmap0), (lambda tmap1: (to_def := (lambda pair: hydra.packaging.TypeDefinition(hydra.lib.pairs.first(pair), hydra.core.TypeScheme((), hydra.lib.pairs.second(pair), Nothing()))), Right((tmap1, hydra.lib.lists.map((lambda names: hydra.lib.lists.map((lambda x1: to_def(x1)), hydra.lib.maybes.map_maybe((lambda n: hydra.lib.maybes.map((lambda t: (n, t)), hydra.lib.maps.lookup(n, tmap1))), names))), name_lists))))[1]))))
617
+ def simple_language_adapter(lang: hydra.coders.Language, cx: T0, g: hydra.graph.Graph, typ: hydra.core.Type) -> Either[hydra.errors.Error, hydra.coders.Adapter[hydra.core.Type, hydra.core.Type, hydra.core.Term, hydra.core.Term]]:
618
+ r"""Given a target language and a source type, produce an adapter which rewrites the type and its terms according to the language's constraints. The encode direction adapts terms; the decode direction is identity."""
619
+ constraints = lang.constraints
620
+ @lru_cache(1)
621
+ def litmap() -> FrozenDict[hydra.core.LiteralType, hydra.core.LiteralType]:
622
+ return adapt_literal_types_map(constraints)
623
+ return hydra.lib.eithers.bind(adapt_type(constraints, litmap(), typ), (lambda adapted_type: Right(hydra.coders.Adapter(False, typ, adapted_type, hydra.coders.Coder((lambda cx2, term: adapt_term(constraints, litmap(), cx2, g, term)), (lambda cx2, term: Right(term)))))))