|
Generated by diff2html © Yves Bailly, MandrakeSoft S.A. 2001 diff2html is licensed under the GNU GPL. |
| support-v2.1/basic.lisp | support-v2.2/basic.lisp | ||||
|---|---|---|---|---|---|
|
515 lines 16236 bytes Last modified : Wed Jun 29 13:32:19 2005 |
541 lines 17205 bytes Last modified : Wed Jun 29 13:33:46 2005 |
||||
| 1 | ;;; Mode: Lisp; Package: ocml | 1 | ;;; Mode: Lisp; Package: ocml | ||
| 2 | 2 | ||||
| 3 | ;;; The Open University | 3 | ;;; The Open University | ||
| 4 | 4 | ||||
| 5 | (in-package "OCML") | 5 | (in-package "OCML") | ||
| 6 | 6 | ||||
| 7 | (in-ontology akt-support-ontology) | 7 | (in-ontology akt-support-ontology) | ||
| 8 | 8 | ||||
| 9 | ;;;Here we introduce a number of definitions, which provide | 9 | ;;;Here we introduce a number of definitions, which provide | ||
| 10 | ;;;the basic representational layer to define entities in the ontology. | 10 | ;;;the basic representational layer to define entities in the ontology. | ||
| 11 | ;;;Here we include basic data types, such | 11 | ;;;Here we include basic data types, such | ||
| 12 | ;;;as strings, lists, sets and numbers, as well as basic logical concepts, such | 12 | ;;;as strings, lists, sets and numbers, as well as basic logical concepts, such | ||
| 13 | ;;;as FUNCTION and RELATION. It also provides equality constructs and a meta-level | 13 | ;;;as FUNCTION and RELATION. It also provides equality constructs and a meta-level | ||
| 14 | ;;;relation HOLDS, which takes a rel, say ?rel, and a number of args, say ?args | 14 | ;;;relation HOLDS, which takes a rel, say ?rel, and a number of args, say ?args | ||
| 15 | ;;;and it is satisfied iff ?rel is satisfied by ?args. | 15 | ;;;and it is satisfied iff ?rel is satisfied by ?args. | ||
| 16 | ;;;The advantage of expliciting including here the representational layer | 16 | ;;;The advantage of expliciting including here the representational layer | ||
| 17 | ;;;for the set of AKT ontologies is that these become completely self-contained: | 17 | ;;;for the set of AKT ontologies is that these become completely self-contained: | ||
| 18 | ;;;all the notions required to specify any concept in the ontology are themselves | 18 | ;;;all the notions required to specify any concept in the ontology are themselves | ||
| 19 | ;;;to be found in the ontologies | 19 | ;;;to be found in the ontologies | ||
| 20 | 20 | ||||
| 21 | ;;;BASIC UNIFICATION MECHANISMS | 21 | ;;;BASIC UNIFICATION MECHANISMS | ||
| 22 | 22 | ||||
| 23 | 23 | ||||
| 24 | ;;;RELATION = | 24 | ;;;RELATION = | ||
| 25 | (def-relation = (?x ?y) | 25 | (def-relation = (?x ?y) | ||
| 26 | "True if ?x and ?y do unify" | 26 | "True if ?x and ?y do unify" | ||
| 27 | :lisp-fun #'(lambda ( x y env) | 27 | :lisp-fun #'(lambda ( x y env) | ||
| 28 | (Let ((result (unify x y env))) | 28 | (Let ((result (unify x y env))) | ||
| 29 | (if (eq result :fail) | 29 | (if (eq result :fail) | ||
| 30 | :fail | 30 | :fail | ||
| 31 | (List result))))) | 31 | (List result))))) | ||
| 32 | 32 | ||||
| 33 | ;;;RELATION == | 33 | ;;;RELATION == | ||
| 34 | (def-relation == (?x ?y) | 34 | (def-relation == (?x ?y) | ||
| 35 | "True if ?x and ?y do unify and they also have the same structure. | 35 | "True if ?x and ?y do unify and they also have the same structure. | ||
| 36 | This means that either they are both atoms, or they are lists with | 36 | This means that either they are both atoms, or they are lists with | ||
| 37 | the same structure" | 37 | the same structure" | ||
| 38 | :lisp-fun #'(lambda ( x y env) | 38 | :lisp-fun #'(lambda ( x y env) | ||
| 39 | (Let ((result (unify-strong x y env))) | 39 | (Let ((result (unify-strong x y env))) | ||
| 40 | (if (eq result :fail) | 40 | (if (eq result :fail) | ||
| 41 | :fail | 41 | :fail | ||
| 42 | (List result))))) | 42 | (List result))))) | ||
| 43 | 43 | ||||
| 44 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 44 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 45 | 45 | ||||
| 46 | (def-class LIST (Intangible-Thing) ?x | 46 | (def-class LIST (Intangible-Thing) ?x | ||
| 47 | "A class representing lists." | 47 | "A class representing lists." | ||
| 48 | :iff-def (or (= ?x nil) | 48 | :iff-def (or (= ?x nil) | ||
| 49 | (= ?x (?a . ?b))) | 49 | (= ?x (?a . ?b))) | ||
| 50 | :prove-by (or (= ?x nil) | 50 | :prove-by (or (= ?x nil) | ||
| 51 | (and (variable-bound ?x) | 51 | (and (variable-bound ?x) | ||
| 52 | (= ?x (?a . ?b)))) | 52 | (= ?x (?a . ?b)))) | ||
| 53 | :no-proofs-by (:iff-def)) | 53 | :no-proofs-by (:iff-def)) | ||
| 54 | 54 | ||||
| 55 | 55 | ||||
| 56 | (def-instance NIL list | 56 | (def-instance NIL list | ||
| 57 | "The empty list") | 57 | "The empty list") | ||
| 58 | 58 | ||||
| 59 | (def-relation NULL (?l) | 59 | (def-relation NULL (?l) | ||
| 60 | "True if ?l is the empty list" | 60 | "True if ?l is the empty list" | ||
| 61 | :iff-def (= ?l nil)) | 61 | :iff-def (= ?l nil)) | ||
| 62 | 62 | ||||
| 63 | (def-function FIRST (?l) | 63 | (def-function FIRST (?l) | ||
| 64 | "Takes the first element of a list. If the list is empty | 64 | "Takes the first element of a list. If the list is empty | ||
| 65 | the function returns :nothing" | 65 | the function returns :nothing" | ||
| 66 | :constraint (list ?l) | 66 | :constraint (list ?l) | ||
| 67 | :body (if (== ?l (?a . ?b)) | 67 | :body (if (== ?l (?a . ?b)) | ||
| 68 | ?a | 68 | ?a | ||
| 69 | :nothing)) | 69 | :nothing)) | ||
| 70 | 70 | ||||
| 71 | (def-function REST (?l) | 71 | (def-function REST (?l) | ||
| 72 | "Takes a list as argument, say ?l, removes the first element of ?s | 72 | "Takes a list as argument, say ?l, removes the first element of ?s | ||
| 73 | and returns the resulting list. If ?l = nil, then :nothing is returned" | 73 | and returns the resulting list. If ?l = nil, then :nothing is returned" | ||
| 74 | :constraint (list ?l) | 74 | :constraint (list ?l) | ||
| 75 | :body (if (== ?l (?a . ?b)) | 75 | :body (if (== ?l (?a . ?b)) | ||
| 76 | ?b | 76 | ?b | ||
| 77 | :nothing)) | 77 | :nothing)) | ||
| 78 | 78 | ||||
| 79 | (def-function LIST-OF (&rest ?els) | 79 | (def-function LIST-OF (&rest ?els) | ||
| 80 | "This is the primitive list constructor. It is implemented in terms of | 80 | "This is the primitive list constructor. It is implemented in terms of | ||
| 81 | the underlying LISP list construction primitive, LIST" | 81 | the underlying LISP list construction primitive, LIST" | ||
| 82 | :lisp-fun #'(lambda (&rest els) | 82 | :lisp-fun #'(lambda (&rest els) | ||
| 83 | (apply #'list els))) | 83 | (apply #'list els))) | ||
| 84 | 84 | ||||
| 85 | (def-function APPEND (?l1 &rest ?ls) | 85 | (def-function APPEND (?l1 &rest ?ls) | ||
| 86 | "Appends together a number of lists. I cannot be bothered giving its operational | 86 | "Appends together a number of lists. I cannot be bothered giving its operational | ||
| 87 | spec...so you only get a lisp attachment" | 87 | spec...so you only get a lisp attachment" | ||
| 88 | :constraint (and (list ?l1)(every ?ls list)) | 88 | :constraint (and (list ?l1)(every ?ls list)) | ||
| 89 | :lisp-fun #'append) | 89 | :lisp-fun #'append) | ||
| 90 | 90 | ||||
| 91 | 91 | ||||
| 92 | (def-function CONS (?el ?l) | 92 | (def-function CONS (?el ?l) | ||
| 93 | "Adds ?el to the beginning of list ?l. | 93 | "Adds ?el to the beginning of list ?l. | ||
| 94 | Note that this cons is less generic than the lisp function with the same name. | 94 | Note that this cons is less generic than the lisp function with the same name. | ||
| 95 | Here the 2nd argument must be a list (in lisp, it can also be an atom)." | 95 | Here the 2nd argument must be a list (in lisp, it can also be an atom)." | ||
| 96 | :constraint (list ?l) | 96 | :constraint (list ?l) | ||
| 97 | :body (append (list-of ?el) | 97 | :body (append (list-of ?el) | ||
| 98 | ?l)) | 98 | ?l)) | ||
| 99 | 99 | ||||
| 100 | 100 | ||||
| 101 | (def-function LENGTH (?l) | 101 | (def-function LENGTH (?l) | ||
| 102 | "Computes the number of elements in a list" | 102 | "Computes the number of elements in a list" | ||
| 103 | :constraint (list ?l) | 103 | :constraint (list ?l) | ||
| 104 | :body (if (= ?l nil) | 104 | :body (if (= ?l nil) | ||
| 105 | 0 | 105 | 0 | ||
| 106 | (if (== ?l (?first . ?rest)) | 106 | (if (== ?l (?first . ?rest)) | ||
| 107 | (+ 1 | 107 | (+ 1 | ||
| 108 | (length ?rest))))) | 108 | (length ?rest))))) | ||
| 109 | 109 | ||||
| 110 | (def-function REMOVE1 (?el ?l) | 110 | (def-function REMOVE1 (?el ?l) | ||
| 111 | "Removes the first occurrence of ?el in ?l and returns the resulting list. | 111 | "Removes the first occurrence of ?el in ?l and returns the resulting list. | ||
| 112 | If ?el is not a member of ?l then the result is ?l" | 112 | If ?el is not a member of ?l then the result is ?l" | ||
| 113 | :constraint (list ?l) | 113 | :constraint (list ?l) | ||
| 114 | :body (if (= ?l nil) | 114 | :body (if (= ?l nil) | ||
| 115 | ?l | 115 | ?l | ||
| 116 | (if (== ?l (?el . ?rest)) | 116 | (if (== ?l (?el . ?rest)) | ||
| 117 | ?rest | 117 | ?rest | ||
| 118 | (if (== ?l (?first . ?rest)) | 118 | (if (== ?l (?first . ?rest)) | ||
| 119 | (cons ?first | 119 | (cons ?first | ||
| 120 | (remove1 ?el ?rest)))))) | 120 | (remove1 ?el ?rest)))))) | ||
| 121 | 121 | ||||
| 122 | (def-function REMOVE (?el ?l) | 122 | (def-function REMOVE (?el ?l) | ||
| 123 | "Removes all occurrences of ?el in ?l and returns the resulting list. | 123 | "Removes all occurrences of ?el in ?l and returns the resulting list. | ||
| 124 | If ?el is not a member of ?l then the result is ?l" | 124 | If ?el is not a member of ?l then the result is ?l" | ||
| 125 | :constraint (list ?l) | 125 | :constraint (list ?l) | ||
| 126 | :body (if (= ?l nil) | 126 | :body (if (= ?l nil) | ||
| 127 | ?l | 127 | ?l | ||
| 128 | (if (== ?l (?el . ?rest)) | 128 | (if (== ?l (?el . ?rest)) | ||
| 129 | (remove ?el ?rest) | 129 | (remove ?el ?rest) | ||
| 130 | (if (== ?l (?first . ?rest)) | 130 | (if (== ?l (?first . ?rest)) | ||
| 131 | (cons ?first | 131 | (cons ?first | ||
| 132 | (remove ?el ?rest)))))) | 132 | (remove ?el ?rest)))))) | ||
| 133 | 133 | ||||
| 134 | 134 | ||||
| 135 | 135 | ||||
| 136 | 136 | ||||
| 137 | (def-relation MEMBER (?el ?list) | 137 | (def-relation MEMBER (?el ?list) | ||
| 138 | "A relation to check whether something is a member of a list" | 138 | "A relation to check whether something is a member of a list" | ||
| 139 | :constraint (list ?list) | 139 | :constraint (list ?list) | ||
| 140 | :iff-def (or (== ?list (?el . ?rest)) | 140 | :iff-def (or (== ?list (?el . ?rest)) | ||
| 141 | (and (== ?list (?first . ?rest)) | 141 | (and (== ?list (?first . ?rest)) | ||
| 142 | (member ?el ?rest)))) | 142 | (member ?el ?rest))) | ||
| 3d) | 143 | :prove-by (member-aux ?el ?list) | |||
| 144 | :no-proofs-by (:iff-def)) | ||||
| 145 | |||||
| 146 | |||||
| 147 | |||||
| 148 | |||||
| 143 | 149 | ||||
| 144 | (def-relation EVERY (?l ?rel) | 150 | (def-relation EVERY (?l ?rel) | ||
| 145 | "True if for each term in ?l, say ?term, (holds ?rel ?term) is true. | 151 | "True if for each term in ?l, say ?term, (holds ?rel ?term) is true. | ||
| 146 | For instance, (every '(1 2 3) 'number) is satisfied, while | 152 | For instance, (every '(1 2 3) 'number) is satisfied, while | ||
| 147 | (every '(1 2 pippo) 'number) is not" | 153 | (every '(1 2 pippo) 'number) is not" | ||
| 148 | :constraint (unary-relation ?rel) | 154 | :constraint (unary-relation ?rel) | ||
| 149 | :iff-def (or (= ?l nil) | 155 | :iff-def (or (= ?l nil) | ||
| 150 | (and (== ?l (?first . ?rest)) | 156 | (and (== ?l (?first . ?rest)) | ||
| 151 | (holds ?rel ?first) | 157 | (holds ?rel ?first) | ||
| 152 | (every ?rest ?rel)))) | 158 | (every ?rest ?rel)))) | ||
| 153 | 159 | ||||
| 154 | ;;;FUNCTION BUTLAST | 160 | ;;;FUNCTION BUTLAST | ||
| 155 | (def-function BUTLAST (?list) | 161 | (def-function BUTLAST (?list) | ||
| 156 | "Returns all the element of ?list, except the last one. | 162 | "Returns all the element of ?list, except the last one. | ||
| 157 | If ?list = NIL, then :nothing is returned. If ?list has | 163 | If ?list = NIL, then :nothing is returned. If ?list has | ||
| 158 | length 1, then nil is returned" | 164 | length 1, then nil is returned" | ||
| 159 | :constraint (list ?l) | 165 | :constraint (list ?l) | ||
| 160 | :body (cond ((null ?list) :nothing) | 166 | :body (cond ((null ?list) :nothing) | ||
| 161 | ((null (rest ?list)) nil) | 167 | ((null (rest ?list)) nil) | ||
| 162 | ((true) | 168 | ((true) | ||
| 163 | (cons (first ?list) (butlast (rest ?list)))))) | 169 | (cons (first ?list) (butlast (rest ?list)))))) | ||
| 164 | 170 | ||||
| 165 | 171 | ||||
| 166 | ;;;FUNCTION LAST | 172 | ;;;FUNCTION LAST | ||
| 167 | (def-function LAST (?list) | 173 | (def-function LAST (?list) | ||
| 168 | "Returns the last element of a list. If ?list is empty | 174 | "Returns the last element of a list. If ?list is empty | ||
| 169 | then :nothing is returned" | 175 | then :nothing is returned" | ||
| 170 | :constraint (list ?list) | 176 | :constraint (list ?list) | ||
| 171 | :body (cond ((null ?list) :nothing) | 177 | :body (cond ((null ?list) :nothing) | ||
| 172 | ((null (rest ?list)) (first ?list)) | 178 | ((null (rest ?list)) (first ?list)) | ||
| 173 | ((true) (last (rest ?list))))) | 179 | ((true) (last (rest ?list))))) | ||
| 174 | 180 | ||||
| 175 | 181 | ||||
| 176 | ;;;;;;;;;;;;;;;;;;;;;;;; | 182 | ;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 177 | 183 | ||||
| 178 | 184 | ||||
| 179 | ;;;SET | 185 | ;;;SET | ||
| 180 | (def-class SET (Intangible-Thing) | 186 | (def-class SET (Intangible-Thing) | ||
| 181 | "A set is something which is not an individual. In cyc sets are distinguished | 187 | "A set is something which is not an individual. In cyc sets are distinguished | ||
| 182 | from collections. Here we just use the term generically to refer to | 188 | from collections. Here we just use the term generically to refer to | ||
| 183 | something which denotes a collection of elements, whether abstract or | 189 | something which denotes a collection of elements, whether abstract or | ||
| 184 | concrete. | 190 | concrete. | ||
| 185 | Functions and relations represent sets. ") | 191 | Functions and relations represent sets. ") | ||
| 186 | 192 | ||||
| 187 | (def-axiom BASIC-SET-TYPES-ARE-DISJOINT | 193 | (def-axiom BASIC-SET-TYPES-ARE-DISJOINT | ||
| 188 | "There are three basic types of sets in our ontology, functions | 194 | "There are three basic types of sets in our ontology, functions | ||
| 189 | relations and enumerated sets and these do not intersect" | 195 | relations and enumerated sets and these do not intersect" | ||
| 190 | (subclass-partition set (set-of function relation enumerated-set))) | 196 | (subclass-partition set (set-of function relation enumerated-set))) | ||
| 191 | 197 | ||||
| 192 | 198 | ||||
| 193 | (def-function SET-OF (&rest ?args) | 199 | (def-function SET-OF (&rest ?args) | ||
| 194 | "This is the basic set constructor to create a set by enumerating its elements. | 200 | "This is the basic set constructor to create a set by enumerating its elements. | ||
| 195 | For instance, (setof 1 2) denotes the set {1 2}. | 201 | For instance, (setof 1 2) denotes the set {1 2}. | ||
| 196 | We represent such a set as a list whose first item is :set" | 202 | We represent such a set as a list whose first item is :set" | ||
| 197 | :body (cons :set ?args)) | 203 | :body (cons :set ?args)) | ||
| 198 | 204 | ||||
| 199 | 205 | ||||
| 200 | (def-class ENUMERATED-SET (set) ?x | 206 | (def-class ENUMERATED-SET (set) ?x | ||
| 201 | "A set represented as (:set-of el1 el_2...el_n), where no el_i is repeated" | 207 | "A set represented as (:set-of el1 el_2...el_n), where no el_i is repeated" | ||
| 202 | :constraint (list ?x) | 208 | :constraint (list ?x) | ||
| 203 | :iff-def (and (= ?x (:set . ?elements)) | 209 | :iff-def (and (= ?x (:set . ?elements)) | ||
| 204 | (not (exists ?el | 210 | (not (exists ?el | ||
| 205 | (and (member ?el ?elements) | 211 | (and (member ?el ?elements) | ||
| 206 | (member ?el (remove1 ?el ?elements)))))) | 212 | (member ?el (remove1 ?el ?elements)))))) | ||
| 207 | :prove-by (and (variable-bound ?x) | 213 | :prove-by (and (variable-bound ?x) | ||
| 208 | (= ?x (:set . ?elements)) | 214 | (= ?x (:set . ?elements)) | ||
| 209 | (not (exists ?el | 215 | (not (exists ?el | ||
| 210 | (and (member ?el ?elements) | 216 | (and (member ?el ?elements) | ||
| 211 | (member ?el (remove1 ?el ?elements)))))) | 217 | (member ?el (remove1 ?el ?elements)))))) | ||
| 212 | :no-proofs-by (:iff-def)) | 218 | :no-proofs-by (:iff-def)) | ||
| 213 | 219 | ||||
| 214 | (def-relation ELEMENT-OF (?el ?set) | 220 | (def-relation ELEMENT-OF (?el ?set) | ||
| 215 | "A relation to check whether something is an element of a set. | 221 | "A relation to check whether something is an element of a set. | ||
| 216 | Note that because functions and relations are sets, we can prove | 222 | Note that because functions and relations are sets, we can prove | ||
| 217 | whether a tuple satisfies a relation or a function using ELEMENT-OF. | 223 | whether a tuple satisfies a relation or a function using ELEMENT-OF. | ||
| 218 | For instance, (element-of '(1 2 3) '+) is satisfied because | 224 | For instance, (element-of '(1 2 3) '+) is satisfied because | ||
| 219 | 1+2=3 - see definitions below for an operationalization of this approach") | 225 | 1+2=3 - see definitions below for an operationalization of this approach") | ||
| 220 | 226 | ||||
| 221 | (def-rule ELEMENT-OF-ENUMERATED-SET | 227 | (def-rule ELEMENT-OF-ENUMERATED-SET | ||
| 222 | "An operationalization of element-of, which works with sets represented as lists." | 228 | "An operationalization of element-of, which works with sets represented as lists." | ||
| 223 | ((element-of ?el ?l) if | 229 | ((element-of ?el ?l) if | ||
| 224 | (enumerated-set ?l) | 230 | (enumerated-set ?l) | ||
| 225 | (member ?el (rest ?l)))) | 231 | (member ?el (rest ?l)))) | ||
| 226 | 232 | ||||
| 227 | (def-rule ELEMENT-OF-SET-AS-RELATION | 233 | (def-rule ELEMENT-OF-SET-AS-RELATION | ||
| 228 | "A tuple, say <t1 t2 t3> is an element of relation r iff | 234 | "A tuple, say <t1 t2 t3> is an element of relation r iff | ||
| 229 | (holds r ti t2 t3) is satisfied" | 235 | (holds r ti t2 t3) is satisfied" | ||
| 230 | ((element-of ?tuple ?rel) if | 236 | ((element-of ?tuple ?rel) if | ||
| 231 | (relation ?rel) | 237 | (relation ?rel) | ||
| 232 | (List ?tuple) | 238 | (List ?tuple) | ||
| 233 | (sentence-holds (cons ?rel ?tuple)))) | 239 | (sentence-holds (cons ?rel ?tuple)))) | ||
| 234 | 240 | ||||
| 235 | (def-rule ELEMENT-OF-SET-AS-FUNCTION | 241 | (def-rule ELEMENT-OF-SET-AS-FUNCTION | ||
| 236 | "A tuple, say <t1 t2 t3> is an element of function f iff | 242 | "A tuple, say <t1 t2 t3> is an element of function f iff | ||
| 237 | (= (apply f ti t2) t3) is satisfied" | 243 | (= (apply f ti t2) t3) is satisfied" | ||
| 238 | ((element-of ?el ?fun) if | 244 | ((element-of ?el ?fun) if | ||
| 239 | (function ?fun) | 245 | (function ?fun) | ||
| 240 | (list ?el) | 246 | (list ?el) | ||
| 241 | (= (last ?el) (apply ?fun (butlast ?el))))) | 247 | (= (last ?el) (apply ?fun (butlast ?el))))) | ||
| 242 | 248 | ||||
| 243 | 249 | ||||
| 244 | 250 | ||||
| 245 | ;;;UNION - The union of a number of sets | 251 | ;;;UNION - The union of a number of sets | ||
| 246 | ;;;For instance | 252 | ;;;For instance | ||
| 247 | ;;; (ocml-eval (union (set-of 1 2)(set-of 1 2 3))) | 253 | ;;; (ocml-eval (union (set-of 1 2)(set-of 1 2 3))) | ||
| 248 | ;;; (:SET 3 2 1) | 254 | ;;; (:SET 3 2 1) | ||
| 249 | (def-function UNION (&rest ?sets) | 255 | (def-function UNION (&rest ?sets) | ||
| 250 | :constraint (every ?sets set) | 256 | :constraint (every ?sets set) | ||
| 251 | :body (apply set-of (setofall ?x | 257 | :body (apply set-of (setofall ?x | ||
| 252 | (exists ?set (and (member ?set ?sets) | 258 | (exists ?set (and (member ?set ?sets) | ||
| 253 | (element-of ?x ?set)))))) | 259 | (element-of ?x ?set)))))) | ||
| 254 | 260 | ||||
| 255 | 261 | ||||
| 256 | (def-function INTERSECTION (&rest ?sets) | 262 | (def-function INTERSECTION (&rest ?sets) | ||
| 257 | :constraint (every ?sets set) | 263 | :constraint (every ?sets set) | ||
| 258 | :body (in-environment ((?all . (apply union ?sets))) | 264 | :body (in-environment ((?all . (apply union ?sets))) | ||
| 259 | (apply set-of (setofall ?x | 265 | (apply set-of (setofall ?x | ||
| 260 | (and (element-of ?x ?all) | 266 | (and (element-of ?x ?all) | ||
| 261 | (not (exists ?set | 267 | (not (exists ?set | ||
| 262 | (and (member ?set ?sets) | 268 | (and (member ?set ?sets) | ||
| 263 | (not (element-of ?x ?set)))))))))) | 269 | (not (element-of ?x ?set)))))))))) | ||
| 264 | 270 | ||||
| 265 | 271 | ||||
| 266 | 272 | ||||
| 267 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 273 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| 268 | 274 | ||||
| 269 | ;;;NUMBER | 275 | ;;;NUMBER | ||
| 270 | (def-class NUMBER (Quantity) | 276 | (def-class NUMBER (Quantity) | ||
| 271 | "The class of all numbers" | 277 | "The class of all numbers" | ||
| 272 | :lisp-fun #'(lambda (x env) | 278 | :lisp-fun #'(lambda (x env) | ||
| 273 | (let ((y (unbound-variable? x env))) | 279 | (let ((y (unbound-variable? x env))) | ||
| 274 | (if y ;;if y is unbound we return a 'sample' number | 280 | (if y ;;if y is unbound we return a 'sample' number | ||
| 275 | (list (cons (cons y 0) env)) | 281 | (list (cons (cons y 0) env)) | ||
| 276 | (if (numberp (instantiate x env)) ;;;make sure to instantiate x | 282 | (if (numberp (instantiate x env)) ;;;make sure to instantiate x | ||
| 277 | (list env) | 283 | (list env) | ||
| 278 | :fail))))) | 284 | :fail))))) | ||
| 279 | 285 | ||||
| 280 | (def-class REAL-NUMBER (number)) | 286 | (def-class REAL-NUMBER (number) | ||
| 3d) | 287 | :lisp-fun #'(lambda (x env) | |||
| 288 | (let ((y (unbound-variable? x env))) | ||||
| 289 | (if y ;;if y is unbound we return a 'sample' number | ||||
| 290 | (list (cons (cons y 1.0) env)) | ||||
| 291 | (if (realp (instantiate x env)) ;;;make sure to instantiate x | ||||
| 292 | (list env) | ||||
| 293 | :fail))))) | ||||
| 294 | |||||
| 295 | |||||
| 296 | (def-class INTEGER (real-number) | ||||
| 3d) | 297 | :lisp-fun #'(lambda (x env) | |||
| 298 | (let ((y (unbound-variable? x env))) | ||||
| 299 | (if y ;;if y is unbound we return a 'sample' number | ||||
| 300 | (list (cons (cons y 1) env)) | ||||
| 301 | (if (integerp (instantiate x env)) ;;;make sure to instantiate x | ||||
| 302 | (list env) | ||||
| 303 | :fail))))) | ||||
| 281 | 304 | ||||
| 282 | (def-class INTEGER (real-number)) | ||||
| 283 | |||||
| 284 | 305 | ||||
| 285 | ;;; RELATION < | 306 | ;;; RELATION < | ||
| 286 | (def-relation < (?x ?y) | 307 | (def-relation < (?x ?y) | ||
| 287 | "A predicate to test whether a number is less than another" | 308 | "A predicate to test whether a number is less than another" | ||
| 288 | :constraint (and (number ?x)(number ?y)) | 309 | :constraint (and (number ?x)(number ?y)) | ||
| 289 | :lisp-fun #'(lambda (x y env) | 310 | :lisp-fun #'(lambda (x y env) | ||
| 290 | (if (< (instantiate x env) | 311 | (if (< (instantiate x env) | ||
| 291 | (instantiate y env)) | 312 | (instantiate y env)) | ||
| 292 | (List env) | 313 | (List env) | ||
| 293 | :fail))) | 314 | :fail))) | ||
| 294 | ;;;RELATION > | 315 | ;;;RELATION > | ||
| 295 | (def-relation > (?x ?y) | 316 | (def-relation > (?x ?y) | ||
| 296 | "A predicate to test whether a number is greater than another" | 317 | "A predicate to test whether a number is greater than another" | ||
| 297 | :constraint (and (number ?x)(number ?y)) | 318 | :constraint (and (number ?x)(number ?y)) | ||
| 298 | :lisp-fun #'(lambda (x y env) | 319 | :lisp-fun #'(lambda (x y env) | ||
| 299 | (if (> (instantiate x env) | 320 | (if (> (instantiate x env) | ||
| 300 | (instantiate y env)) | 321 | (instantiate y env)) | ||
| 301 | (List env) | 322 | (List env) | ||
| 302 | :fail))) | 323 | :fail))) | ||
| 303 | 324 | ||||
| 304 | 325 | ||||
| 305 | (def-class POSITIVE-NUMBER (number) ?x | 326 | (def-class POSITIVE-NUMBER (number) ?x | ||
| 306 | :iff-def (and (number ?x) | 327 | :iff-def (and (number ?x) | ||
| 307 | (> ?x 0))) | 328 | (> ?x 0))) | ||
| 308 | 329 | ||||
| 309 | (def-class NEGATIVE-NUMBER (number) ?x | 330 | (def-class NEGATIVE-NUMBER (number) ?x | ||
| 310 | :iff-def (and (number ?x) | 331 | :iff-def (and (number ?x) | ||
| 311 | (< ?x 0))) | 332 | (< ?x 0))) | ||
| 312 | 333 | ||||
| 313 | (def-class NON-NEGATIVE-NUMBER (number) ?x | 334 | (def-class NON-NEGATIVE-NUMBER (number) ?x | ||
| 314 | :iff-def (and (number ?x) | 335 | :iff-def (and (number ?x) | ||
| 315 | (not (negative-number ?x)))) | 336 | (not (negative-number ?x)))) | ||
| 316 | 337 | ||||
| 317 | 338 | ||||
| 318 | (def-axiom POS-NEG-NUMBERS-EXHAUSTIVE-PARTITION | 339 | (def-axiom POS-NEG-NUMBERS-EXHAUSTIVE-PARTITION | ||
| 319 | (exhaustive-subclass-partition number (set-of positive-number negative-number))) | 340 | (exhaustive-subclass-partition number (set-of positive-number negative-number))) | ||
| 320 | 341 | ||||
| 321 | 342 | ||||
| 322 | (def-class POSITIVE-INTEGER (integer) ?x | 343 | (def-class POSITIVE-INTEGER (integer) ?x | ||
| 323 | :iff-def (and (integer ?x) | 344 | :iff-def (and (integer ?x) | ||
| 324 | (> ?x 0))) | 345 | (> ?x 0))) | ||
| 325 | 346 | ||||
| 326 | (def-class NEGATIVE-INTEGER (integer) ?x | 347 | (def-class NEGATIVE-INTEGER (integer) ?x | ||
| 327 | :iff-def (and (integer ?x) | 348 | :iff-def (and (integer ?x) | ||
| 328 | (< ?x 0))) | 349 | (< ?x 0))) | ||
| 329 | 350 | ||||
| 330 | (def-class NON-NEGATIVE-INTEGER (integer) ?x | 351 | (def-class NON-NEGATIVE-INTEGER (integer) ?x | ||
| 331 | :iff-def (and (integer ?x) | 352 | :iff-def (and (integer ?x) | ||
| 332 | (not (negative-integer ?x)))) | 353 | (not (negative-integer ?x))) | ||
| 3d) | 354 | :prove-by (or (and (variable-bound ?x) | |||
| 355 | (and (integer ?X) | ||||
| 356 | (or (= ?x 0) (> ?x 0)))) | ||||
| 357 | (= ?x 0)) | ||||
| 358 | :no-proofs-by (:iff-def)) | ||||
| 333 | 359 | ||||
| 334 | 360 | ||||
| 335 | (def-function + (?X &rest ?y) | 361 | (def-function + (?X &rest ?y) | ||
| 336 | "Adds numbers" | 362 | "Adds numbers" | ||
| 337 | :constraint (and (number ?x) (number ?y)) | 363 | :constraint (and (number ?x) (number ?y)) | ||
| 338 | :lisp-fun #'+) | 364 | :lisp-fun #'+) | ||
| 339 | 365 | ||||
| 340 | (def-function * (?X ?y) | 366 | (def-function * (?X ?y) | ||
| 341 | "Multiplies numbers" | 367 | "Multiplies numbers" | ||
| 342 | :constraint (and (number ?x) (number ?y)) | 368 | :constraint (and (number ?x) (number ?y)) | ||
| 343 | :lisp-fun #'*) | 369 | :lisp-fun #'*) | ||
| 344 | 370 | ||||
| 345 | (def-function SQUARE (?X) | 371 | (def-function SQUARE (?X) | ||
| 346 | :constraint (number ?x) | 372 | :constraint (number ?x) | ||
| 347 | :body (* ?X ?X)) | 373 | :body (* ?X ?X)) | ||
| 348 | 374 | ||||
| 349 | (def-function - (?X &rest ?y) | 375 | (def-function - (?X &rest ?y) | ||
| 350 | "Subtracts numbers" | 376 | "Subtracts numbers" | ||
| 351 | :constraint (and (number ?x) (number ?y)) | 377 | :constraint (and (number ?x) (number ?y)) | ||
| 352 | :lisp-fun #'(lambda (x &rest y) | 378 | :lisp-fun #'(lambda (x &rest y) | ||
| 353 | (apply #'- x y))) | 379 | (apply #'- x y))) | ||
| 354 | 380 | ||||
| 355 | (def-function / (?X ?y) | 381 | (def-function / (?X ?y) | ||
| 356 | "Divides numbers" | 382 | "Divides numbers" | ||
| 357 | :constraint (and (number ?x) (number ?y)) | 383 | :constraint (and (number ?x) (number ?y)) | ||
| 358 | :lisp-fun #'/) | 384 | :lisp-fun #'/) | ||
| 359 | 385 | ||||
| 360 | 386 | ||||
| 361 | (def-function log (?number ?base) | 387 | (def-function log (?number ?base) | ||
| 362 | :lisp-fun #'log) | 388 | :lisp-fun #'log) | ||
| 363 | 389 | ||||
| 364 | (def-function round (?number) -> ?int | 390 | (def-function round (?number) -> ?int | ||
| 365 | :def (integer ?int) | 391 | :def (integer ?int) | ||
| 366 | :lisp-fun #'round) | 392 | :lisp-fun #'round) | ||
| 367 | 393 | ||||
| 368 | 394 | ||||
| 369 | 395 | ||||
| 370 | 396 | ||||
| 371 | ;;;;;;;;;;;;;;;;;;;;; | 397 | ;;;;;;;;;;;;;;;;;;;;; | ||
| 372 | 398 | ||||
| 373 | (def-class STRING (individual intangible-thing) | 399 | (def-class STRING (individual intangible-thing) | ||
| 374 | "A primitive class representing strings" | 400 | "A primitive class representing strings" | ||
| 375 | :lisp-fun #'(lambda (x env) | 401 | :lisp-fun #'(lambda (x env) | ||
| 376 | (let ((y (unbound-variable? x env))) | 402 | (let ((y (unbound-variable? x env))) | ||
| 377 | (if y | 403 | (if y | ||
| 378 | (list (cons (cons y "SAMPLE-STRING") env)) | 404 | (list (cons (cons y "SAMPLE-STRING") env)) | ||
| 379 | (if (stringp (instantiate x env)) | 405 | (if (stringp (instantiate x env)) | ||
| 380 | (list env) | 406 | (list env) | ||
| 381 | :fail))))) | 407 | :fail))))) | ||
| 382 | 408 | ||||
| 383 | ;;;;;;;;;;;;;;;;;;;;; | 409 | ;;;;;;;;;;;;;;;;;;;;; | ||
| 384 | 410 | ||||
| 385 | (def-class RELATION (set) | 411 | (def-class RELATION (set) | ||
| 386 | "The class of defined relations. We assume fixed arity" | 412 | "The class of defined relations. We assume fixed arity" | ||
| 387 | :lisp-fun #'(lambda (x env) | 413 | :lisp-fun #'(lambda (x env) | ||
| 388 | (let ((y (unbound-variable? x env))) | 414 | (let ((y (unbound-variable? x env))) | ||
| 389 | (if y | 415 | (if y | ||
| 390 | (mapcar #'(lambda (rel) | 416 | (mapcar #'(lambda (rel) | ||
| 391 | (cons (cons y rel) env)) | 417 | (cons (cons y rel) env)) | ||
| 392 | (all-relations)) | 418 | (all-relations)) | ||
| 393 | (if (get-relation (instantiate x env)) ;;;make sure to instantiate x | 419 | (if (get-relation (instantiate x env)) ;;;make sure to instantiate x | ||
| 394 | (list env) | 420 | (list env) | ||
| 395 | :fail))))) | 421 | :fail))))) | ||
| 396 | 422 | ||||
| 397 | 423 | ||||
| 398 | (def-class UNARY-RELATION (relation) ?r | 424 | (def-class UNARY-RELATION (relation) ?r | ||
| 399 | :iff-def (and (relation ?r) | 425 | :iff-def (and (relation ?r) | ||
| 400 | (= (arity ?r) 1))) | 426 | (= (arity ?r) 1))) | ||
| 401 | 427 | ||||
| 402 | 428 | ||||
| 403 | (def-class BINARY-RELATION (relation) ?r | 429 | (def-class BINARY-RELATION (relation) ?r | ||
| 404 | :iff-def (and (relation ?r) | 430 | :iff-def (and (relation ?r) | ||
| 405 | (= (arity ?r) 2))) | 431 | (= (arity ?r) 2))) | ||
| 406 | 432 | ||||
| 407 | (def-relation TRUE () | 433 | (def-relation TRUE () | ||
| 408 | "This is always satisfied" | 434 | "This is always satisfied" | ||
| 409 | :lisp-fun #'(lambda (env) (list env))) | 435 | :lisp-fun #'(lambda (env) (list env))) | ||
| 410 | 436 | ||||
| 411 | (def-relation FALSE () | 437 | (def-relation FALSE () | ||
| 412 | "This is never satisfied" | 438 | "This is never satisfied" | ||
| 413 | :lisp-fun #'(lambda (env) :fail)) | 439 | :lisp-fun #'(lambda (env) :fail)) | ||
| 414 | 440 | ||||
| 415 | 441 | ||||
| 416 | (def-function ARITY (?x) | 442 | (def-function ARITY (?x) | ||
| 417 | "The arity of a function or relation. If a function or relation | 443 | "The arity of a function or relation. If a function or relation | ||
| 418 | has variable arity, then we treat the last argument as if it were | 444 | has variable arity, then we treat the last argument as if it were | ||
| 419 | a sequence variable. For instance the argument list for + is | 445 | a sequence variable. For instance the argument list for + is | ||
| 420 | (?x &rest ?y). In this case we say that + has arity 2. | 446 | (?x &rest ?y). In this case we say that + has arity 2. | ||
| 421 | It is important to note that OCML does not support relations with variable | 447 | It is important to note that OCML does not support relations with variable | ||
| 422 | arity. The only exception is HOLDS, which has variable arity and is built in | 448 | arity. The only exception is HOLDS, which has variable arity and is built in | ||
| 423 | the OCML proof system" | 449 | the OCML proof system" | ||
| 424 | 450 | ||||
| 425 | :constraint (or (function ?X)(relation ?X)) | 451 | :constraint (or (function ?X)(relation ?X)) | ||
| 426 | :body (in-environment | 452 | :body (in-environment | ||
| 427 | ((?l . (the-schema ?x)) | 453 | ((?l . (the-schema ?x)) | ||
| 428 | (?n . (length ?l))) | 454 | (?n . (length ?l))) | ||
| 429 | (if (every ?l variable) | 455 | (if (every ?l variable) | ||
| 430 | ?n | 456 | ?n | ||
| 431 | ;we assume that the only non-var can be &rest | 457 | ;we assume that the only non-var can be &rest | ||
| 432 | (- ?n 1)))) | 458 | (- ?n 1)))) | ||
| 433 | 459 | ||||
| 434 | (def-relation VARIABLE (?x) | 460 | (def-relation VARIABLE (?x) | ||
| 435 | "True of an OCML variable" | 461 | "True of an OCML variable" | ||
| 436 | :lisp-fun #'(lambda (x env) | 462 | :lisp-fun #'(lambda (x env) | ||
| 437 | (if | 463 | (if | ||
| 438 | (variable? (instantiate x env)) | 464 | (variable? (instantiate x env)) | ||
| 439 | (list env) | 465 | (list env) | ||
| 440 | :fail))) | 466 | :fail))) | ||
| 441 | 467 | ||||
| 442 | (def-relation VARIABLE-BOUND (?var) | 468 | (def-relation VARIABLE-BOUND (?var) | ||
| 443 | "True if ?var is bound in teh current environment" | 469 | "True if ?var is bound in teh current environment" | ||
| 444 | :lisp-fun #'(lambda (x env) | 470 | :lisp-fun #'(lambda (x env) | ||
| 445 | (if | 471 | (if | ||
| 446 | (unbound-variable? x env) | 472 | (unbound-variable? x env) | ||
| 447 | :fail | 473 | :fail | ||
| 448 | (list env)))) | 474 | (list env)))) | ||
| 449 | 475 | ||||
| 450 | (def-function THE-SCHEMA (?x) | 476 | (def-function THE-SCHEMA (?x) | ||
| 451 | "The schema of a function or relation" | 477 | "The schema of a function or relation" | ||
| 452 | :constraint (or (function ?X)(relation ?X)) | 478 | :constraint (or (function ?X)(relation ?X)) | ||
| 453 | :body (if (relation ?x) | 479 | :body (if (relation ?x) | ||
| 454 | (relation-schema ?x) | 480 | (relation-schema ?x) | ||
| 455 | (if (function ?x) | 481 | (if (function ?x) | ||
| 456 | (function-schema ?x) | 482 | (function-schema ?x) | ||
| 457 | :nothing))) | 483 | :nothing))) | ||
| 458 | 484 | ||||
| 459 | (def-function FUNCTION-SCHEMA (?f) | 485 | (def-function FUNCTION-SCHEMA (?f) | ||
| 460 | :constraint (function ?f) | 486 | :constraint (function ?f) | ||
| 461 | :lisp-fun #'(lambda (x) | 487 | :lisp-fun #'(lambda (x) | ||
| 462 | (rename-variables (schema (get-function x))))) | 488 | (rename-variables (schema (get-function x))))) | ||
| 463 | 489 | ||||
| 464 | (def-function RELATION-SCHEMA (?rel) | 490 | (def-function RELATION-SCHEMA (?rel) | ||
| 465 | :constraint (relation ?rel) | 491 | :constraint (relation ?rel) | ||
| 466 | :lisp-fun #'(lambda (x) | 492 | :lisp-fun #'(lambda (x) | ||
| 467 | (rename-variables (schema (get-relation x))))) | 493 | (rename-variables (schema (get-relation x))))) | ||
| 468 | 494 | ||||
| 469 | 495 | ||||
| 470 | 496 | ||||
| 471 | (def-relation HOLDS (?rel &rest ?args) | 497 | (def-relation HOLDS (?rel &rest ?args) | ||
| 472 | "A meta-level relation which is true iff the sentence (?rel . ?args) is true. | 498 | "A meta-level relation which is true iff the sentence (?rel . ?args) is true. | ||
| 473 | The length of ?args must be consistent with the arity of the relation" | 499 | The length of ?args must be consistent with the arity of the relation" | ||
| 474 | :constraint (and (relation ?r) | 500 | :constraint (and (relation ?r) | ||
| 475 | (= (arity ?rel) | 501 | (= (arity ?rel) | ||
| 476 | (length ?args)))) | 502 | (length ?args)))) | ||
| 477 | 503 | ||||
| 478 | (def-relation SENTENCE-HOLDS (?sentence) | 504 | (def-relation SENTENCE-HOLDS (?sentence) | ||
| 479 | "The same as HOLDS, but takes only one argument, a sentence whose truth | 505 | "The same as HOLDS, but takes only one argument, a sentence whose truth | ||
| 480 | value is to be checked" | 506 | value is to be checked" | ||
| 481 | :constraint (and (== ?sentence (?rel . ?args )) | 507 | :constraint (and (== ?sentence (?rel . ?args )) | ||
| 482 | (relation ?rel) | 508 | (relation ?rel) | ||
| 483 | (= (arity ?rel) | 509 | (= (arity ?rel) | ||
| 484 | (length ?args))) | 510 | (length ?args))) | ||
| 485 | :lisp-fun #'(lambda (sent env) | 511 | :lisp-fun #'(lambda (sent env) | ||
| 486 | (ask-top-level | 512 | (ask-top-level | ||
| 487 | (cons 'holds (instantiate sent env)) | 513 | (cons 'holds (instantiate sent env)) | ||
| 488 | :env env | 514 | :env env | ||
| 489 | :all t))) | 515 | :all t))) | ||
| 490 | 516 | ||||
| 491 | 517 | ||||
| 492 | 518 | ||||
| 493 | (def-class FUNCTION (set) | 519 | (def-class FUNCTION (set) | ||
| 494 | "The class of all defined functions" | 520 | "The class of all defined functions" | ||
| 495 | :lisp-fun #'(lambda (x env) | 521 | :lisp-fun #'(lambda (x env) | ||
| 496 | (let ((y (unbound-variable? x env))) | 522 | (let ((y (unbound-variable? x env))) | ||
| 497 | (if y | 523 | (if y | ||
| 498 | (mapcar #'(lambda (rel) | 524 | (mapcar #'(lambda (rel) | ||
| 499 | (cons (cons y rel) env)) | 525 | (cons (cons y rel) env)) | ||
| 500 | (all-functions)) | 526 | (all-functions)) | ||
| 501 | (if (ocml-function? | 527 | (if (ocml-function? | ||
| 502 | (instantiate x env)) | 528 | (instantiate x env)) | ||
| 503 | (list env) | 529 | (list env) | ||
| 504 | :fail))))) | 530 | :fail))))) | ||
| 505 | 531 | ||||
| 506 | 532 | ||||
| 507 | 533 | ||||
| 508 | 534 | ||||
| 509 | (def-function APPLY (?f ?args) | 535 | (def-function APPLY (?f ?args) | ||
| 510 | "(apply f (arg1 .....argn)) is the same as | 536 | "(apply f (arg1 .....argn)) is the same as | ||
| 511 | (f arg1 ....argn)" | 537 | (f arg1 ....argn)" | ||
| 512 | :constraint (and (function ?f) | 538 | :constraint (and (function ?f) | ||
| 513 | (list ?args))) | 539 | (list ?args))) | ||
| 514 | 540 | ||||
| 515 | 541 |