diff options
Diffstat (limited to 'src/act4e_solutions')
-rw-r--r-- | src/act4e_solutions/maps.py | 14 | ||||
-rw-r--r-- | src/act4e_solutions/maps_representation.py | 50 |
2 files changed, 48 insertions, 16 deletions
diff --git a/src/act4e_solutions/maps.py b/src/act4e_solutions/maps.py index aff881f..f056da8 100644 --- a/src/act4e_solutions/maps.py +++ b/src/act4e_solutions/maps.py @@ -1,6 +1,7 @@ from typing import TypeVar import act4e_interfaces as I +from .maps_representation import MyFiniteMap A = TypeVar("A") B = TypeVar("B") @@ -10,7 +11,16 @@ C = TypeVar("C") class SolFiniteMapOperations(I.FiniteMapOperations): def identity(self, s: I.Setoid[A]) -> I.Mapping[A, A]: - raise NotImplementedError() + values = [] + for a in s.elements(): + values.append([a, a]) + + return MyFiniteMap(s, s, values) + def compose(self, f: I.FiniteMap[A, B], g: I.FiniteMap[B, C]) -> I.FiniteMap[A, C]: - raise NotImplementedError() + values = [] + for a in f.source().elements(): + values.append([a, g(f(a))]) + + return MyFiniteMap(f.source(), g.target(), values) diff --git a/src/act4e_solutions/maps_representation.py b/src/act4e_solutions/maps_representation.py index 92e7924..ad9cdf5 100644 --- a/src/act4e_solutions/maps_representation.py +++ b/src/act4e_solutions/maps_representation.py @@ -25,20 +25,25 @@ class MyFiniteMap(I.FiniteMap[A, B]): return self._target def __call__(self, a: A) -> B: - print("Function call") - print("src: ", self._source.elements()) - print("dst: ", self._target.elements()) - print("val: ", self._values) - print("inp: ", a) - assert a in self._source.elements() + # print("Function call") + # print("src: ", self._source.elements()) + # print("dst: ", self._target.elements()) + # print("val: ", self._values) + # print("inp: ", a) + # assert a in self._source.elements() + rv = None for v in self._values: - if v[0] == a: - print(f"{v[0]} equals {a}") + if self._source.equal(v[0], a): + # print(f"{v[0]} equals {a}") rv = v[1] - print("ret: ", rv) - print() + # assert self._target.contains(rv) + # print("ret: ", rv) + # print("the type of rv is", type(rv)) + # print() + # assert type(rv) != bool + return rv @@ -48,11 +53,14 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation): A = fsr.load(h, s["source"]) B = fsr.load(h, s["target"]) - values = s["values"] + values = [] # Check that the values are are in the domain and codomain - for v in values: - if not v[0] in A.elements(): + for v in s["values"]: + a, b = A.load(h, v[0]), B.load(h, v[1]) + values.append([a, b]) + + if not A.contains(a): print(f"{v[0]} is not in A: {A.elements()}") raise I.InvalidFormat() @@ -61,6 +69,7 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation): raise I.InvalidFormat() # Check that the domain values are unique + # FIXME: fix this part domvals = [] for d in map(lambda v: v[0], values): if d in domvals: @@ -71,4 +80,17 @@ class SolFiniteMapRepresentation(I.FiniteMapRepresentation): def save(self, h: I.IOHelper, m: I.FiniteMap[Any, Any]) -> I.FiniteMap_desc: - raise NotImplementedError() + fsr = SolFiniteSetRepresentation() + d = { + "source": fsr.save(h, m.source()), + "target": fsr.save(h, m.target()), + "values": [] + } + + # for a in m.source().elements(): + # b = m(a) + # d["values"].append([ + # m.source().save(h, a), + # m.target().save(h, b)]) + + return d |