|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright (c) 2010-2016, MIT Probabilistic Computing Project |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import venture.lite.types as vt |
| 18 | +import venture.lite.value as vv |
| 19 | + |
| 20 | +from venture.lite.sp_help import deterministic_typed |
| 21 | + |
| 22 | +from venture.exception import VentureException |
| 23 | + |
| 24 | +# XXX Mutators generally do not exist in Venture. |
| 25 | +def dict_set(d, k, v): |
| 26 | + assert isinstance(d, vv.VentureDict) |
| 27 | + d.dict[k] = v |
| 28 | + |
| 29 | +def dict_pop(d, k): |
| 30 | + assert isinstance(d, vv.VentureDict) |
| 31 | + return d.dict.pop(k) |
| 32 | + |
| 33 | +def dict_pop2(d, k): |
| 34 | + assert isinstance(d, vv.VentureDict) |
| 35 | + return dict_pop(d, k) if k in d.dict else vv.VentureNil() |
| 36 | + |
| 37 | +def check_unbound_sp(ripl, name): |
| 38 | + try: |
| 39 | + ripl.sample(name) |
| 40 | + return False |
| 41 | + except VentureException: |
| 42 | + return True |
| 43 | + |
| 44 | +def __venture_start__(ripl): |
| 45 | + if check_unbound_sp(ripl, 'dict_set'): |
| 46 | + ripl.bind_foreign_sp('dict_set', deterministic_typed( |
| 47 | + dict_set, |
| 48 | + [vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')), |
| 49 | + vt.AnyType('k'), vt.AnyType('v')], |
| 50 | + vt.NilType())) |
| 51 | + if check_unbound_sp(ripl, 'dict_pop'): |
| 52 | + ripl.bind_foreign_sp('dict_pop', deterministic_typed( |
| 53 | + dict_pop, |
| 54 | + [vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')), |
| 55 | + vt.AnyType('k')], |
| 56 | + vt.AnyType('v'))) |
| 57 | + if check_unbound_sp(ripl, 'dict_pop2'): |
| 58 | + ripl.bind_foreign_sp('dict_pop2', deterministic_typed( |
| 59 | + dict_pop2, |
| 60 | + [vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')), |
| 61 | + vt.AnyType('k')], |
| 62 | + vt.AnyType('v'))) |
0 commit comments