shift_replace_2.anubis 14.3 KB
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
   
   
                     _____              ___.   .__         ________  
                    /  _  \   ____  __ _\_ |__ |__| ______ \_____  \ 
                   /  /_\  \ /    \|  |  \ __ \|  |/  ___/  /  ____/ 
                  /    |    \   |  \  |  / \_\ \  |\___ \  /       \ 
                  \____|__  /___|  /____/|___  /__/____  > \_______ \
                          \/ The \/  Anubis  \/    2   \/  Project \/
   
   
   Name of this file:           shift_replace_2.anubis
   
   Purpose of this file:        Tools for shifting strong items and replacing de Bruijn 
                                  symbols in strong items. 
   
   

   Authors (Name [initials]):   Alain Proute'   [AP]
   
   Updates ([initials] (date) comment): 
      [AP] (2007 jul 04) Creation of this file.
   
   ---------------------------------------------------------------------------------------   

read tools/basis.anubis   
   
read types1.anubis
   
   
   There are two fundamental operations we must be able to perform on strong items (strong
   terms and strong types). They are named 'shifting' and 'replacing'.
   
   
   
   *** (1) Shifting. 
   
   A  strong item contains  occurrences of  'de Bruijn  symbols'. These  symbols represent
   depths into the  stack of the virtual  machine. It is sometimes necessary  to 'shift' a
   strong item  by a certain  number, say  'n'. The result  is meaningful relative  to any
   extension  of  the  original  stack  obtained  by  pushing  'n'  data  on  top  of  the
   stack. Notice that the stack is heterogeneous,  which means that the slots in the stack
   do not have  the same size. Their size depends  only of the type of  the content of the
   slot. In other words, all data of a given type have the same size (even if some data do
   not use all bits).
   
   For example,  if 'E' is a  statement (i.e. a  strong term of type  'Omega'), meaningful
   relative to  some strong context 'G'  (representing the stack), the  result of shifting
   'E' by '1'  is meaningful relative to any  strong context of the form '[T  . G]', where
   'T' is any type.
   
public define StrongType shift(Int32 n, StrongType t). 
public define StrongTerm shift(Int32 n, StrongTerm t). 
   
   Actually,  the operation  of shifting  by 'n'  just amounts  to add  'n' to  all depths
   encountered in the strong item.
   
   
   
   
   *** (2) Replacing.
   
   Given a  strong item 'E', we  may need to replace  all occurences of a  given de Bruijn
   symbol by a given strong term. The result of replacing all occurrences of the de Bruijn
   symbol 'x' by the strong term 'a' in a strong item 'E' is generally denoted 'E[a/x]' by
   logicians  (read: 'E'  within which  'a'  replaces 'x').  The strong  item 'E[a/x]'  is
   represented in this compiler by 'replace(E,a,x)'. 
   
public define StrongType replace(StrongType t, StrongTerm a, Int32 i).    
public define StrongTerm replace(StrongTerm t, StrongTerm a, Int32 i).    
   
   Notice hat 'E[a/x]' is meaningful relative to the same strong context as 'E'. 
   
   Replacement  of de  Bruijn symbols  is somewhat  simpler than  replacement  of ordinary
   symbols, because there is no need to rename bound symbols.
   

   
   
   *** (3) Replacing type parameters and unknowns. 
   
   We also  need to replace  type parameters  and type unknowns  by strong types.  This is
   achieved by the following functions.
   
public define StrongType
   replace_parameter         // computes _T[_U/x]
     (
       StrongType     _T,
       StrongType     _U,
       String         parameter
     ). 
   
   
   
   
   *** (4) Getting the strong type of a strong term. 
   
public define StrongType type_of(StrongTerm t). 
   
   Strong terms are designed in such a way that no local nor global context is needed.
   
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------
   
   
   
   *** [1] Shifting a strong type. 
   
public define StrongType 
   shift
     (
       Int32       n, 
       StrongType  t
     ) =
   if t is 
     {
       _Unary(StrongType source,StrongType target) then
         _Unary(shift(n,source),shift(n,target)), 
   
       _Binary(StrongType source1,StrongType source2,StrongType target) then
         _Binary(shift(n,source1),shift(n,source2),shift(n,target)), 
      
       _Declarative(StrongType domain,StrongType body_type,StrongType target) then
         _Declarative(shift(n,domain),
                      replace(shift(n,body_type),symbol(domain,0),n),
                      shift(n,target)),
   
       _Parameter(String name) then
          t, 
   
       _Defined(Int32 type_id,List(StrongType) operands) then
         _Defined(type_id,
                  map((StrongType st) |-> shift(n,st),
                      operands)),
   
       _Product(List(StrongType) l) then
         _Product(map((StrongType st) |-> shift(n,st),
                      l)), 
   
       _Functional(StrongType source,StrongType target) then
         _Functional(shift(n,source),
                     replace(shift(n,target),symbol(source,0),n)), 
   
       _Omega then
         _Omega, 
   
       _Witness(StrongTerm statement) then
         _Witness(shift(n,statement)), 
   
       _Quantified(String parameter,StrongType _T) then
         _Quantified(parameter,shift(n,_T))
     }. 
   
   
   
   
   *** [2] Shifting a strong term. 
   
   Shifting a strong  term. 'n' is added to  a depth at the unique place  where depths are
   found, i.e.  in  the alternative 'symbol'. Furthermore, shifting  the declarative terms
   'lambda(...)' and  'forall(...)' requires a replacement because  the symbol 'symbol(0)'
   must not be shifted (the replacement 'undoes' the shifting for this symbol).
   
public define StrongTerm 
   shift
     (
       Int32         n, 
       StrongTerm    t
     ) =
   if t is 
     {
       global(_T,Int32 id) then
         global(shift(n,_T),id),
   
       symbol(_T,Int32 depth) then
         symbol(shift(n,_T),depth + n), 
   
       tuple(List(StrongTerm) l) then
         tuple(map((StrongTerm st) |-> shift(n,st),l)),
   
       proj(Int32 i,StrongTerm st) then
         proj(i,shift(n,st)),
   
       incl(StrongType _T,Int32 i,StrongTerm st) then
         incl(shift(n,_T),i,shift(n,st)), 
   
       cond(StrongType _T,StrongTerm test,List(StrongTerm) cases) then
         cond(shift(n,_T),shift(n,test),
              map((StrongTerm st) |-> shift(n,st),cases)),
   
       lambda(StrongType _T,StrongTerm st) then
         lambda(shift(n,_T),replace(shift(n,st),symbol(_T,0),n)),
   
       app(StrongTerm f,StrongTerm a) then
         app(shift(n,f),shift(n,a)), 
   
       forall(StrongType _T,StrongTerm _E) then
         forall(shift(n,_T),replace(shift(n,_E),symbol(_T,0),n)),
   
       description(StrongTerm p) then
         description(shift(n,p)), 
   
       property(StrongTerm p) then
         property(shift(n,p)),
   
       choice(StrongTerm p) then
         choice(shift(n,p)),
   
       parametric(String parameter,StrongTerm st) then
         parametric(parameter,shift(n,st)), 
   
       parametric_app(StrongTerm st,StrongType _T) then
         parametric_app(shift(n,st),shift(n,_T))
     }. 
   

   
   *** [3] Replacement in strong types. 

   _Parameter(N)[a/i]                  is  _Parameter(N)
   _Defined(N,T_1,...,T_k)[a/i]        is  _Defined(N,T_1[a/i],...,T_k[a/i])
   _Product(T_1,...,T_k)[a/i]          is  _Product(T_1[a/i],...,T_k[a/i])
   _Functional(T,U)[a/i]               is  _Functional(T[a/i],U[a/i+1])
   _Omega[a/i]                         is  _Omega
   _Witness(E)[a/i]                    is  _Witness(E[a/i])
   _Quantified($X T)[a/i]              is  _Quantified($X T[a/i])
   
   
public define StrongType 
   replace                       // computes 't[a/s(i)]'
     (
       StrongType         t, 
       StrongTerm         a, 
       Int32              i
     ) = 
   if t is 
     {
       _Unary(StrongType source,StrongType target) then
         _Unary(replace(source,a,i),replace(target,a,i)),
   
       _Binary(StrongType source1,StrongType source2,StrongType target) then
         _Binary(replace(source1,a,i),replace(source2,a,i),replace(target,a,i)), 
   
       _Declarative(StrongType domain,StrongType body_type,StrongType target) then
         _Declarative(replace(domain,a,i),
                      replace(body_type,a,i+1),
                      replace(target,a,i)), 
   
       _Parameter(String name) then
         t, 
   
       _Defined(Int32 type_id,List(StrongType) operands) then
         _Defined(type_id,
                  map((StrongType st) |-> replace(st,a,i),
                      operands)),
   
       _Product(List(StrongType) l) then
         _Product(map((StrongType st) |-> replace(st,a,i),l)),
   
       _Functional(StrongType source,StrongType target) then
         _Functional(replace(source,a,i),replace(target,a,i+1)),
   
       _Omega then
         _Omega,
   
       _Witness(StrongTerm statement) then
         _Witness(replace(statement,a,i)), 
   
       _Quantified(String parameter,StrongType _T) then
         _Quantified(parameter,replace(_T,a,i))
     }. 
   
   
   
   
   *** [4] Replacement in strong terms. 
   
   global(n)[a/i]                     is  global(n)
   symbol(i)[a/i]                     is  a
   symbol(i)[a/j]                     is  symbol(i)         (if i != j)
   tuple(t_1,...,t_k)[a/i]            is  tuple(t_1[a/i],...,t_k[a/i])
   proj(n,t)[a/i]                     is  proj(n,t[a/i])
   incl(U,n,t)[a/i]                   is  incl(U[a/i],n,t[a/i])
   cond(T,t,c_1,...,c_k)[a/i]         is  cond(T[a/i],t[a/i],c_1[a/i],...,c_k[a/i])
   lambda(T,E)[a/i]                   is  lambda(T[a/i],E[a/i+1])
   app(f,b)[a/i]                      is  app(f[a/i],b[a/i])
   forall(T,E)[a/i]                   is  forall(T[a/i],E[a/i+1])
   description(p)[a/i]                is  description(p[a/i])
   property(p)[a/i]                   is  property(p[a/i])
   choice(p)[a/i]                     is  choice(p[a/i])
   parametric($X,t)[a/i]              is  parametric($X,t[a/i])
   parametric_app(t,T)[a/i]           is  parametric_app(t[a/i],T[a/i])
   
   
public define StrongTerm 
   replace                     // computes 't[a/i]'
     (
       StrongTerm        t, 
       StrongTerm        a, 
       Int32             i
     ) =
   if t is 
     {
       global(_T,Int32 id) then
         global(replace(_T,a,i),id), 
   
       symbol(_T,Int32 depth) then
         if depth = i 
         then if type_of(a) = replace(_T,a,i)
              then a
              else alert
         else symbol(replace(_T,a,i),depth), 
   
       tuple(List(StrongTerm) l) then
         tuple(map((StrongTerm st) |-> replace(st,a,i),l)),
   
       proj(Int32 i,StrongTerm st) then
         proj(i,replace(st,a,i)),
   
       incl(StrongType _T,Int32 j,StrongTerm st) then
         incl(replace(_T,a,i),j,replace(st,a,i)), 
   
       cond(StrongType _T,StrongTerm test,List(StrongTerm) cases) then
         cond(replace(_T,a,i),replace(test,a,i),
              map((StrongTerm c) |-> replace(c,a,i),cases)),
   
       lambda(StrongType _T,StrongTerm _E) then
         lambda(replace(_T,a,i),replace(_E,a,i+1)), 
   
       app(StrongTerm f,StrongTerm b) then
         app(replace(f,a,i),replace(b,a,i)),
   
       forall(StrongType _T,StrongTerm _E) then
         forall(replace(_T,a,i),replace(_E,a,i+1)), 
   
       description(StrongTerm p) then
         description(replace(p,a,i)), 
   
       property(StrongTerm p) then
         property(replace(p,a,i)), 
   
       choice(StrongTerm p) then
         choice(replace(p,a,i)), 
   
       parametric(String parameter,StrongTerm st) then
         parametric(parameter,replace(st,a,i)),
   
       parametric_app(StrongTerm st,StrongType _T) then
         parametric_app(replace(st,a,i),replace(_T,a,i))
     }. 
   

   
   
   
   
   *** [] Getting the type of a datum whose existence is proven. 

   Let p be  an existence proof, i.e. a strong term  of type '_Witness(exists(_T,_E))'. We
   want to get '_T'.  However, 'exists' is not a constructor of  type 'StrongTerm', but is
   defined as follows:
   
     exists(_T,_E)  := 
       forall(_Omega,
            (implies(forall(T,implies(replace(shift(E,2),s(0),2),s(2))),s(1))))
   
   Furthermore, 'implies(_A,_B)' itself is defined as 'forall(_Witness(_A),_B)'.
   
   
define StrongType
   type_of_existing
     (
       StrongTerm    p   // p is supposed to be a proof of existence
     ) =
   if type_of(p) is _Witness(_F)                    // _F must be exists(_T,?)
   then if _F is forall(_O,_E)                      // _O must be _Omega
        then if _O = _Omega
             then if _E is forall(_W,_G)            // _W must be _Witness(forall(_T,?))
                  then if _W is _Witness(_U)        // _U must be forall(_T,?)
                       then if _U is forall(_T,_)
                            then _T
                            else alert
                       else alert
                  else alert
             else alert
        else alert
   else alert.
   
   
   
   
   
   *** [] Getting the strong type of any strong term. 
   
public define StrongType
   type_of
     (
       StrongTerm  t
     ) =
   if t is 
     {
       global(_T,Int32 id) then 
         _T,
   
       symbol(_T,Int32 depth) then
         _T,
   
       tuple(List(StrongTerm) l) then
         _Product(map(type_of,l)),
   
       proj(Int32 i,StrongTerm st) then
         if type_of(st) is _Product(factors) 
         then force_nth(i,factors)
         else alert, 
   
       incl(StrongType _T,Int32 i,StrongTerm st) then
         _T,
   
       cond(StrongType _T,StrongTerm test,List(StrongTerm) cases) then
         _T,
   
       lambda(StrongType _T,StrongTerm _E) then
         _Functional(_T,type_of(_E)), 
   
       app(StrongTerm f,StrongTerm a) then
         if type_of(f) is _Functional(_T,_U) 
         then _U
         else alert, 
   
       forall(StrongType _T,StrongTerm _E) then
         _Omega,
   
       description(StrongTerm p) then
         type_of_existing(p),
   
       property(StrongTerm p) then
         type_of_existing(p),
   
       choice(StrongTerm p) then
         type_of_existing(p),
      
       parametric(String parameter,StrongTerm st) then
         _Quantified(parameter,type_of(st)), 
   
       parametric_app(StrongTerm st,StrongType _T) then
         if type_of(st) is _Quantified(par,type) 
         then replace_parameter(type,_T,par)
         else alert
     }.