/tutorial/backtracking/tiny_prolog/prolog_builtins.e
Specman e | 189 lines | 184 code | 4 blank | 1 comment | 17 complexity | be32044e99ec5228589e8d96b7750df0 MD5 | raw file
1class PROLOG_BUILTINS 2inherit PROLOG_COMMONS ABSTRACT_BACKTRACKING 3creation 4 make 5feature 6 make is 7 do 8 init_builtins 9 end 10feature 11 current_term: PROLOG_TERM 12 current_compound: PROLOG_COMPOUND 13 current_atom: PROLOG_ATOM 14 current_functor: PROLOG_FUNCTOR 15 current_proc: ROUTINE[like Current, TUPLE[]] 16 current_data: LINKED_LIST[LINKED_LIST[PROLOG_TERM]] 17feature 18 evaluate_current_state is 19 local 20 atom: PROLOG_ATOM 21 compound: PROLOG_COMPOUND 22 functor: PROLOG_FUNCTOR 23 do 24 current_compound ?= current_term 25 if current_compound /= Void then 26 current_atom := current_compound.atom 27 check current_atom /= Void end 28 else 29 current_atom ?= current_term 30 end 31 if current_atom = Void then 32 -- TODO gerer les exeception de call 33 partial_fail 34 else 35 current_functor.make(current_atom,current_term.arity) 36 current_proc := builtins.reference_at(current_functor) 37 if current_proc /= Void then 38 current_proc.call([Current]) 39 else 40 evaluate_from_database 41 end 42 end 43 end 44feature 45 evaluate_from_database is 46 do 47 current_data := database.reference_at(current_functor) 48 if current_data = Void then 49 partial_fail 50 else 51 52 end 53 end 54feature {} 55 init_builtins is 56 do 57 if builtins.is_empty then really_init_builtins end 58 end 59 really_init_builtins is 60 do 61 add_bi(once "fail", 0, agent {like Current}.bi_fail) 62 add_bi(once "true", 0, agent {like Current}.bi_true) 63 add_bi(once "atom", 1, agent {like Current}.bi_atom) 64 add_bi(once "integer", 1, agent {like Current}.bi_integer) 65 add_bi(once "float", 1, agent {like Current}.bi_float) 66 add_bi(once "number", 1, agent {like Current}.bi_number) 67 add_bi(once "atomic", 1, agent {like Current}.bi_atomic) 68 add_bi(once "compound", 1, agent {like Current}.bi_compound) 69 add_bi(once "callable", 1, agent {like Current}.bi_callable) 70 add_bi(once "var", 1, agent {like Current}.bi_var) 71 add_bi(once "nonvar", 1, agent {like Current}.bi_nonvar) 72 add_bi(once "=", 2, agent {like Current}.bi_unify) 73 add_bi(once "\=", 2, agent {like Current}.bi_not_unify) 74 end 75 add_bi(name: STRING; arity: INTEGER; proc: like current_proc) is 76 local 77 functor: PROLOG_FUNCTOR 78 do 79 builtins.add(proc,get_functor_by_name(name,arity)) 80 end 81feature {} 82 partial_switch(success: BOOLEAN) is 83 do 84 if success then 85 partial_success 86 else 87 partial_fail 88 end 89 end 90feature {} 91 bi_fail is 92 do 93 partial_fail 94 end 95 bi_true is 96 do 97 partial_success 98 end 99feature {} -- type testing 100 bi_atom is 101 do 102 inspect current_compound.args.first.type 103 when type_atom then partial_success 104 else partial_fail 105 end 106 end 107 bi_integer is 108 do 109 inspect current_compound.args.first.type 110 when type_integer then partial_success 111 else partial_fail 112 end 113 end 114 bi_float is 115 do 116 inspect current_compound.args.first.type 117 when type_float then partial_success 118 else partial_fail 119 end 120 end 121 bi_number is 122 do 123 inspect current_compound.args.first.type 124 when type_integer, type_float then partial_success 125 else partial_fail 126 end 127 end 128 bi_atomic is 129 do 130 inspect current_compound.args.first.type 131 when type_atom, type_integer, type_float then partial_success 132 else partial_fail 133 end 134 end 135 bi_compound is 136 do 137 inspect current_compound.args.first.type 138 when type_compound then partial_success 139 else partial_fail 140 end 141 end 142 bi_callable is 143 do 144 inspect current_compound.args.first.type 145 when type_atom, type_compound then partial_success 146 else partial_fail 147 end 148 end 149 bi_var is 150 do 151 inspect current_compound.args.first.type 152 when type_var then partial_success 153 else partial_fail 154 end 155 end 156 bi_nonvar is 157 do 158 inspect current_compound.args.first.type 159 when type_var then partial_fail 160 else partial_success 161 end 162 end 163feature {} -- unification 164 bi_unify is 165 do 166 if unify(current_compound.args.item(0),current_compound.args.item(1)) then 167 partial_success 168 else 169 partial_fail 170 end 171 end 172 bi_not_unify is 173 do 174 if unify(current_compound.args.item(0),current_compound.args.item(1)) then 175 partial_fail 176 else 177 partial_success 178 end 179 end 180 181 182 bi_functor is 183 do 184 end 185 bi_call is 186 do 187 end 188end 189