-
Notifications
You must be signed in to change notification settings - Fork 4
/
constraint.rkt
97 lines (83 loc) · 3.08 KB
/
constraint.rkt
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
91
92
93
94
95
96
97
;; zKanren: MicroKanren with Constraints and noto
;; Copyright (C) 2017 Zaoqi
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
#lang racket
(provide
(struct-out constraints)
(contract-out (struct constraint
[(type id?)
(v constraintv?)]))
new-constraints
new-constraint
define-constraints-
define-constraints
get-constraints-
constraintv?
constraintsv?
get-constraintsv
set-constraintsv
)
(require "id.rkt")
(require "types.rkt")
(require "contract.rkt")
#| ConstraintV = Any |#
(define constraintv? any/c)
#| ConstraintsV = Any |#
(define constraintsv? any/c)
#| ID →
ConstraintsV →
(ConstraintV → State → Maybe (State × [Var])) →
([Var] → State → U Bool State) →
(State → Maybe State) →
(State → Symbol × [Any]) →
Constraints |#
(struct constraints (id empty add checkm clean show))
#| ConstraintsV →
(ConstraintV → State → Maybe (State × [Var])) →
([Var] → State → U Bool State) →
(State → Maybe State) →
(State → Symbol × [Any]) →
Constraints |#
(define/contract (new-constraints empty add check clean show)
(-> constraintsv?
(-> constraintv? state? (maybe (cons/c state? (listof var?))))
(-> (listof var?) state? (or/c state? boolean?))
(-> state? (maybe state?))
(-> state? (cons/c symbol? list?))
constraints?)
(constraints (new-id) empty add check clean show))
#| ID → ConstraintV → Constraint |#
(struct constraint (type v))
#| Constraints → ConstraintV → Constraint |#
(define/contract (new-constraint cs v)
(-> constraints? constraintv? constraint?)
(constraint (constraints-id cs) v))
#| Hash ID Constraints |#
(define constraintss (make-hash))
(define/contract (define-constraints- x)
(-> constraints? void?)
(let ([id (constraints-id x)])
(if (hash-has-key? constraintss id)
(error 'define-constraints-)
(hash-set! constraintss id x))))
#| ID → Constraints |#
(define (get-constraints- id) (hash-ref constraintss id))
(define-syntax-rule (define-constraints name empty add check clean show)
(begin
(define name (new-constraints empty add check clean show))
(define-constraints- name)))
(define/contract (get-constraintsv s cs)
(state? constraints? . -> . constraintsv?)
(hash-ref (state-c s) (constraints-id cs) (constraints-empty cs)))
(define/contract (set-constraintsv s cs v)
(state? constraints? constraintsv? . -> . state?)
(state (state-g s) (hash-set (state-c s) (constraints-id cs) v) (state-hg s)))