Nominal/Fv.thy
author Cezary Kaliszyk <kaliszyk@in.tum.de>
Mon, 03 May 2010 09:55:43 +0200
changeset 2015 3e7969262809
parent 2014 17684f7eaeb9
permissions -rw-r--r--
Move old fv_alpha_export to Fv.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     1
theory Fv
1806
Christian Urban <urbanc@in.tum.de>
parents: 1774
diff changeset
     2
imports "../Nominal-General/Nominal2_Atoms" 
Christian Urban <urbanc@in.tum.de>
parents: 1774
diff changeset
     3
        "Abs" "Perm" "Rsp" "Nominal2_FSet"
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     4
begin
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     5
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
     6
(* The bindings data structure:
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
     7
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
     8
  Bindings are a list of lists of lists of triples.
1172
9a609fefcf24 Simplified format of bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1169
diff changeset
     9
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    10
   The first list represents the datatypes defined.
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    11
   The second list represents the constructors.
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    12
   The internal list is a list of all the bndings that
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    13
   concern the constructor.
1172
9a609fefcf24 Simplified format of bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1169
diff changeset
    14
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    15
   Every triple consists of a function, the binding and
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    16
   the body.
1169
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    17
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    18
  Eg:
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    19
nominal_datatype
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    20
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    21
   C1
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    22
 | C2 x y z bind x in z
1172
9a609fefcf24 Simplified format of bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1169
diff changeset
    23
 | C3 x y z bind f x in z bind g y in z
1169
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    24
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    25
yields:
1172
9a609fefcf24 Simplified format of bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1169
diff changeset
    26
[
9a609fefcf24 Simplified format of bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1169
diff changeset
    27
 [],
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    28
 [(NONE, 0, 2)],
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    29
 [(SOME (Const f), 0, 2), (Some (Const g), 1, 2)]]
1185
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
    30
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    31
A SOME binding has to have a function which takes an appropriate
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    32
argument and returns an atom set. A NONE binding has to be on an
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    33
argument that is an atom or an atom set.
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    34
*)
1185
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
    35
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    36
(*
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1508
diff changeset
    37
An overview of the generation of free variables:
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    38
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    39
1) fv_bn functions are generated only for the non-recursive binds.
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    40
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    41
   An fv_bn for a constructor is a union of values for the arguments:
1191
15362b433d64 Description of the fv procedure.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1185
diff changeset
    42
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    43
   For an argument x that is in the bn function
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    44
   - if it is a recursive argument bn' we return: fv_bn' x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    45
   - otherwise empty
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    46
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    47
   For an argument x that is not in the bn function
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    48
   - for atom we return: {atom x}
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    49
   - for atom set we return: atom ` x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    50
   - for a recursive call to type ty' we return: fv_ty' x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    51
     with fv of the appropriate type
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    52
   - otherwise empty
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    53
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
    54
2) fv_ty functions generated for all types being defined:
1191
15362b433d64 Description of the fv procedure.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1185
diff changeset
    55
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
    56
   fv_ty for a constructor is a union of values for the arguments.
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    57
1508
883b38196dba Simplified the description.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1505
diff changeset
    58
   For an argument that is bound in a shallow binding we return empty.
883b38196dba Simplified the description.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1505
diff changeset
    59
883b38196dba Simplified the description.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1505
diff changeset
    60
   For an argument x that bound in a non-recursive deep binding
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    61
   we return: fv_bn x.
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    62
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    63
   Otherwise we return the free variables of the argument minus the
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    64
   bound variables of the argument.
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
    65
1505
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    66
   The free variables for an argument x are:
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    67
   - for an atom: {atom x}
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    68
   - for atom set: atom ` x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    69
   - for recursive call to type ty' return: fv_ty' x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    70
   - for nominal datatype ty' return: fv_ty' x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    71
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    72
   The bound variables are a union of results of all bindings that
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    73
   involve the given argument. For a paricular binding:
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    74
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    75
   - for a binding function bn: bn x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    76
   - for a recursive argument of type ty': fv_fy' x
e12ebfa745f6 Update the description of the generation of fv function.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1498
diff changeset
    77
   - for nominal datatype ty' return: fv_ty' x
1169
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    78
*)
b9d02e0800e9 Description of intended bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1168
diff changeset
    79
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1508
diff changeset
    80
(*
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1508
diff changeset
    81
An overview of the generation of alpha-equivalence:
1513
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    82
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    83
1) alpha_bn relations are generated for binding functions.
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    84
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    85
   An alpha_bn for a constructor is true if a conjunction of
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    86
   propositions for each argument holds.
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    87
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    88
   For an argument a proposition is build as follows from
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    89
   th:
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    90
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    91
   - for a recursive argument in the bn function, we return: alpha_bn argl argr
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    92
   - for a recursive argument for type ty not in bn, we return: alpha_ty argl argr
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    93
   - for other arguments in the bn function we return: True
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    94
   - for other arguments not in the bn function we return: argl = argr
44840614ea0c Description of generation of alpha_bn.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
    95
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
    96
2) alpha_ty relations are generated for all the types being defined:
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
    97
1516
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
    98
   For each constructor we gather all the arguments that are bound,
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
    99
   and for each of those we add a permutation. We associate those
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   100
   permutations with the bindings. Note that two bindings can have
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   101
   the same permutation if the arguments being bound are the same.
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   102
1516
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   103
   An alpha_ty for a constructor is true if there exist permutations
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   104
   as above such that a conjunction of propositions for all arguments holds.
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   105
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   106
   For an argument we allow bindings where only one of the following
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   107
   holds:
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   108
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   109
   - Argument is bound in some shallow bindings: We return true
1516
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   110
   - Argument of type ty is bound recursively in some other
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   111
     arguments [i1, .. in] with one binding function bn.
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   112
     We return:
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   113
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   114
     (bn argl, (argl, argl_i1, ..., argl_in)) \<approx>gen
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   115
     \<lambda>(argl,argl1,..,argln) (argr,argr1,..,argrn). 
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   116
         (alpha_ty argl argr) \<and> (alpha_i1 argl1 argr1) \<and> .. \<and> (alpha_in argln argrn)
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   117
     \<lambda>(arg,arg1,..,argn). (fv_ty arg) \<union> (fv_i1 arg1) \<union> .. \<union> (fv_in argn)
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   118
     pi
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   119
     (bn argr, (argr, argr_i1, ..., argr_in))
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   120
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   121
   - Argument is bound in some deep non-recursive bindings.
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   122
     We return: alpha_bn argl argr
1516
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   123
   - Argument of type ty has some shallow bindings [b1..bn] and/or
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   124
     non-recursive bindings [f1 a1, .., fm am], where the bindings
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   125
     have the permutations p1..pl. We return:
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   126
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   127
     (b1l \<union>..\<union> bnl \<union> f1 a1l \<union>..\<union> fn anl, argl) \<approx>gen
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   128
     alpha_ty fv_ty (p1 +..+ pl)
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   129
     (b1r \<union>..\<union> bnr \<union> f1 a1r \<union>..\<union> fn anr, argr)
e3a82a3529ce Continued description of alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1514
diff changeset
   130
1514
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   131
   - Argument has some recursive bindings. The bindings were
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   132
     already treated in 2nd case so we return: True
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   133
   - Argument has no bindings and is not bound.
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   134
     If it is recursive for type ty, we return: alpha_ty argl argr
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   135
     Otherwise we return: argl = argr
b52b72676e20 First part of the description of alpha_ty.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1513
diff changeset
   136
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1508
diff changeset
   137
*)
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1508
diff changeset
   138
2008
1bddffddc03f attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
parents: 1911
diff changeset
   139
1362
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   140
ML {*
1911
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   141
datatype alpha_mode = AlphaGen | AlphaRes | AlphaLst;
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   142
*}
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   143
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   144
ML {*
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   145
fun atyp_const AlphaGen = @{const_name alpha_gen}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   146
  | atyp_const AlphaRes = @{const_name alpha_res}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   147
  | atyp_const AlphaLst = @{const_name alpha_lst}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   148
*}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   149
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   150
(* TODO: make sure that parser checks that bindings are compatible *)
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   151
ML {*
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   152
fun alpha_const_for_binds [] = atyp_const AlphaGen
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   153
  | alpha_const_for_binds ((NONE, _, _, at) :: t) = atyp_const at
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   154
  | alpha_const_for_binds ((SOME (_, _), _, _, at) :: _) = atyp_const at
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   155
*}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   156
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   157
ML {*
1362
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   158
fun is_atom thy typ =
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   159
  Sign.of_sort thy (typ, @{sort at})
1366
2bf82fa871e7 Proper recognition of atoms and atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1362
diff changeset
   160
2bf82fa871e7 Proper recognition of atoms and atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1362
diff changeset
   161
fun is_atom_set thy (Type ("fun", [t, @{typ bool}])) = is_atom thy t
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   162
  | is_atom_set _ _ = false;
1534
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   163
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   164
fun is_atom_fset thy (Type ("FSet.fset", [t])) = is_atom thy t
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   165
  | is_atom_fset _ _ = false;
1362
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   166
*}
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   167
e72d9d9eada3 Term5 written as nominal_datatype is the recursive let.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1359
diff changeset
   168
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
   169
(* Like map2, only if the second list is empty passes empty lists insted of error *)
1302
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   170
ML {*
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   171
fun map2i _ [] [] = []
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   172
  | map2i f (x :: xs) (y :: ys) = f x y :: map2i f xs ys
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   173
  | map2i f (x :: xs) [] = f x [] :: map2i f xs []
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   174
  | map2i _ _ _ = raise UnequalLengths;
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   175
*}
d3101a5b9c4d Length fix for nested recursions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1301
diff changeset
   176
1358
0c843fcb1d7b Update the comments
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1357
diff changeset
   177
(* Finds bindings with the same function and binding, and gathers all
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   178
   bodys for such pairs
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   179
 *)
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   180
ML {*
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   181
fun gather_binds binds =
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   182
let
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   183
  fun gather_binds_cons binds =
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   184
    let
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   185
      val common = map (fn (f, bi, _, aty) => (f, bi, aty)) binds
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   186
      val nodups = distinct (op =) common
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   187
      fun find_bodys (sf, sbi, sty) =
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   188
        filter (fn (f, bi, _, aty) => f = sf andalso bi = sbi andalso aty = sty) binds
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   189
      val bodys = map ((map (fn (_, _, bo, _) => bo)) o find_bodys) nodups
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   190
    in
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   191
      nodups ~~ bodys
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   192
    end
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   193
in
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   194
  map (map gather_binds_cons) binds
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   195
end
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   196
*}
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   197
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   198
ML {*
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   199
fun un_gather_binds_cons binds =
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   200
  flat (map (fn (((f, bi, aty), bos), pi) => map (fn bo => ((f, bi, bo, aty), pi)) bos) binds)
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   201
*}
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   202
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   203
ML {*
1175
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   204
  open Datatype_Aux; (* typ_of_dtyp, DtRec, ... *);
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   205
*}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   206
ML {*
1178
275a1cb3f2ba Reorder
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1177
diff changeset
   207
  (* TODO: It is the same as one in 'nominal_atoms' *)
1175
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   208
  fun mk_atom ty = Const (@{const_name atom}, ty --> @{typ atom});
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   209
  val noatoms = @{term "{} :: atom set"};
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   210
  fun mk_single_atom x = HOLogic.mk_set @{typ atom} [mk_atom (type_of x) $ x];
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   211
  fun mk_union sets =
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   212
    fold (fn a => fn b =>
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   213
      if a = noatoms then b else
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   214
      if b = noatoms then a else
1323
acf262971303 Fixes for the fv problem and alpha problem.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1308
diff changeset
   215
      if a = b then a else
1325
0be098c61d00 Add the supp intersection conditions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1323
diff changeset
   216
      HOLogic.mk_binop @{const_name sup} (a, b)) (rev sets) noatoms;
0be098c61d00 Add the supp intersection conditions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1323
diff changeset
   217
  val mk_inter = foldr1 (HOLogic.mk_binop @{const_name inf})
1175
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   218
  fun mk_diff a b =
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   219
    if b = noatoms then a else
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   220
    if b = a then noatoms else
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   221
    HOLogic.mk_binop @{const_name minus} (a, b);
1534
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   222
  fun mk_atom_set t =
1185
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   223
    let
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   224
      val ty = fastype_of t;
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   225
      val atom_ty = HOLogic.dest_setT ty --> @{typ atom};
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   226
      val img_ty = atom_ty --> ty --> @{typ "atom set"};
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   227
    in
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   228
      (Const (@{const_name image}, img_ty) $ Const (@{const_name atom}, atom_ty) $ t)
7566b899ca6a Code for handling atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1180
diff changeset
   229
    end;
1534
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   230
  fun mk_atom_fset t =
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   231
    let
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   232
      val ty = fastype_of t;
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   233
      val atom_ty = dest_fsetT ty --> @{typ atom};
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   234
      val fmap_ty = atom_ty --> ty --> @{typ "atom fset"};
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   235
      val fset_to_set = @{term "fset_to_set :: atom fset \<Rightarrow> atom set"}
1534
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   236
    in
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   237
      fset_to_set $ ((Const (@{const_name fmap}, fmap_ty) $ Const (@{const_name atom}, atom_ty) $ t))
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   238
    end;
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   239
  (* Similar to one in USyntax *)
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   240
  fun mk_pair (fst, snd) =
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   241
    let val ty1 = fastype_of fst
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   242
      val ty2 = fastype_of snd
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   243
      val c = HOLogic.pair_const ty1 ty2
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   244
    in c $ fst $ snd
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   245
    end;
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   246
*}
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   247
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   248
(* Given [fv1, fv2, fv3] creates %(x, y, z). fv1 x u fv2 y u fv3 z *)
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   249
ML {*
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   250
fun mk_compound_fv fvs =
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   251
let
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   252
  val nos = (length fvs - 1) downto 0;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   253
  val fvs_applied = map (fn (fv, no) => fv $ Bound no) (fvs ~~ nos);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   254
  val fvs_union = mk_union fvs_applied;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   255
  val (tyh :: tys) = rev (map (domain_type o fastype_of) fvs);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   256
  fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   257
in
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   258
  fold fold_fun tys (Abs ("", tyh, fvs_union))
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   259
end;
1175
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   260
*}
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   261
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   262
(* Given [R1, R2, R3] creates %(x,x'). %(y,y'). %(z,z'). R x x' \<and> R y y' \<and> R z z' *)
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   263
ML {*
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   264
fun mk_compound_alpha Rs =
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   265
let
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   266
  val nos = (length Rs - 1) downto 0;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   267
  val nos2 = (2 * length Rs - 1) downto length Rs;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   268
  val Rs_applied = map (fn (R, (no2, no)) => R $ Bound no2 $ Bound no) (Rs ~~ (nos2 ~~ nos));
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   269
  val Rs_conj = mk_conjl Rs_applied;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   270
  val (tyh :: tys) = rev (map (domain_type o fastype_of) Rs);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   271
  fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   272
  val abs_rhs = fold fold_fun tys (Abs ("", tyh, Rs_conj))
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   273
in
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   274
  fold fold_fun tys (Abs ("", tyh, abs_rhs))
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   275
end;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   276
*}
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   277
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   278
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   279
ML {*
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   280
fun non_rec_binds l =
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   281
let
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   282
  fun is_non_rec (SOME (f, false), _, _, _) = SOME f
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   283
    | is_non_rec _ = NONE
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   284
in
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   285
  distinct (op =) (map_filter is_non_rec (flat (flat l)))
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   286
end
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   287
*}
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   288
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   289
(* We assume no bindings in the type on which bn is defined *)
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   290
ML {*
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   291
fun fv_bn thy (dt_info : Datatype_Aux.info) fv_frees bn_fvbn (fvbn, (bn, ith_dtyp, args_in_bns)) =
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   292
let
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   293
  val {descr, sorts, ...} = dt_info;
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   294
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
1379
5bb0149329ee Separate lists for separate constructors, to match bn_eqs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1377
diff changeset
   295
  fun fv_bn_constr (cname, dts) args_in_bn =
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   296
  let
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   297
    val Ts = map (typ_of_dtyp descr sorts) dts;
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   298
    val names = Datatype_Prop.make_tnames Ts;
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   299
    val args = map Free (names ~~ Ts);
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   300
    val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   301
    fun fv_arg ((dt, x), arg_no) =
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   302
      let
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   303
        val ty = fastype_of x
1622
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   304
(*        val _ = tracing ("B 1" ^ PolyML.makestring args_in_bn);*)
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   305
(*        val _ = tracing ("B 2" ^ PolyML.makestring bn_fvbn);*)
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   306
      in
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   307
        case AList.lookup (op=) args_in_bn arg_no of
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   308
          SOME NONE => @{term "{} :: atom set"}
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   309
        | SOME (SOME (f : term)) => (the (AList.lookup (op=) bn_fvbn f)) $ x
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   310
        | NONE =>
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   311
            if is_atom thy ty then mk_single_atom x else
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   312
            if is_atom_set thy ty then mk_atom_set x else
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   313
            if is_atom_fset thy ty then mk_atom_fset x else
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   314
            if is_rec_type dt then nth fv_frees (body_index dt) $ x else
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   315
            @{term "{} :: atom set"}
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   316
      end;
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   317
    val arg_nos = 0 upto (length dts - 1)
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   318
  in
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   319
    HOLogic.mk_Trueprop (HOLogic.mk_eq
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   320
      (fvbn $ list_comb (c, args), mk_union (map fv_arg (dts ~~ args ~~ arg_nos))))
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   321
  end;
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   322
  val (_, (_, _, constrs)) = nth descr ith_dtyp;
1379
5bb0149329ee Separate lists for separate constructors, to match bn_eqs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1377
diff changeset
   323
  val eqs = map2i fv_bn_constr constrs args_in_bns
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   324
in
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   325
  ((bn, fvbn), eqs)
1375
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   326
end
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   327
*}
aa787c9b6955 A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1366
diff changeset
   328
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   329
ML {* print_depth 100 *}
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   330
ML {*
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   331
fun fv_bns thy dt_info fv_frees rel_bns =
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   332
let
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   333
  fun mk_fvbn_free (bn, ith, _) =
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   334
    let
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   335
      val fvbn_name = "fv_" ^ (Long_Name.base_name (fst (dest_Const bn)));
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   336
    in
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   337
      (fvbn_name, Free (fvbn_name, fastype_of (nth fv_frees ith)))
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   338
    end;
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   339
  val (fvbn_names, fvbn_frees) = split_list (map mk_fvbn_free rel_bns);
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   340
  val bn_fvbn = (map (fn (bn, _, _) => bn) rel_bns) ~~ fvbn_frees
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   341
  val (l1, l2) = split_list (map (fv_bn thy dt_info fv_frees bn_fvbn) (fvbn_frees ~~ rel_bns));
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   342
in
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   343
  (l1, (fvbn_names ~~ l2))
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   344
end
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   345
*}
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   346
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   347
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   348
ML {*
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   349
fun alpha_bn (dt_info : Datatype_Aux.info) alpha_frees bn_alphabn ((bn, ith_dtyp, args_in_bns), (alpha_bn_free, _ (*is_rec*) )) =
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   350
let
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   351
  val {descr, sorts, ...} = dt_info;
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   352
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
1386
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   353
  fun alpha_bn_constr (cname, dts) args_in_bn =
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   354
  let
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   355
    val Ts = map (typ_of_dtyp descr sorts) dts;
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   356
    val names = Name.variant_list ["pi"] (Datatype_Prop.make_tnames Ts);
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   357
    val names2 = Name.variant_list ("pi" :: names) (Datatype_Prop.make_tnames Ts);
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   358
    val args = map Free (names ~~ Ts);
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   359
    val args2 = map Free (names2 ~~ Ts);
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   360
    val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
1387
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   361
    val rhs = HOLogic.mk_Trueprop
1462
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   362
      (alpha_bn_free $ (list_comb (c, args)) $ (list_comb (c, args2)));
1387
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   363
    fun lhs_arg ((dt, arg_no), (arg, arg2)) =
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   364
      case AList.lookup (op=) args_in_bn arg_no of
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   365
        SOME NONE => @{term True}
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   366
      | SOME (SOME f) => (the (AList.lookup (op=) bn_alphabn f)) $ arg $ arg2
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   367
      | NONE =>
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   368
          if is_rec_type dt then (nth alpha_frees (body_index dt)) $ arg $ arg2
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   369
          else HOLogic.mk_eq (arg, arg2)
1387
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   370
    val arg_nos = 0 upto (length dts - 1)
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   371
    val lhss = mk_conjl (map lhs_arg (dts ~~ arg_nos ~~ (args ~~ args2)))
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   372
    val eq = Logic.mk_implies (HOLogic.mk_Trueprop lhss, rhs)
1386
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   373
  in
1387
9d70a0733786 rhs of alpha_bn, and template for the arguments.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1386
diff changeset
   374
    eq
1386
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   375
  end
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   376
  val (_, (_, _, constrs)) = nth descr ith_dtyp;
1386
0511e879a687 alpha_bn_constr template
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1385
diff changeset
   377
  val eqs = map2i alpha_bn_constr constrs args_in_bns
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   378
in
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   379
  ((bn, alpha_bn_free), eqs)
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   380
end
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   381
*}
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   382
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   383
ML {*
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   384
fun alpha_bns dt_info alpha_frees rel_bns bns_rec =
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   385
let
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   386
  val {descr, sorts, ...} = dt_info;
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   387
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   388
  fun mk_alphabn_free (bn, ith, _) =
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   389
    let
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   390
      val alphabn_name = "alpha_" ^ (Long_Name.base_name (fst (dest_Const bn)));
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   391
      val alphabn_type = nth_dtyp ith --> nth_dtyp ith --> @{typ bool};
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   392
      val alphabn_free = Free(alphabn_name, alphabn_type);
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   393
    in
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   394
      (alphabn_name, alphabn_free)
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   395
    end;
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   396
  val (alphabn_names, alphabn_frees) = split_list (map mk_alphabn_free rel_bns);
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   397
  val bn_alphabn = (map (fn (bn, _, _) => bn) rel_bns) ~~ alphabn_frees;
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   398
  val pair = split_list (map (alpha_bn dt_info alpha_frees bn_alphabn)
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   399
    (rel_bns ~~ (alphabn_frees ~~ bns_rec)))
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   400
in
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   401
  (alphabn_names, pair)
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   402
end
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   403
*}
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   404
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   405
1397
6e2dfe52b271 Allows multiple bindings with same lhs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1394
diff changeset
   406
(* Checks that a list of bindings contains only compatible ones *)
6e2dfe52b271 Allows multiple bindings with same lhs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1394
diff changeset
   407
ML {*
6e2dfe52b271 Allows multiple bindings with same lhs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1394
diff changeset
   408
fun bns_same l =
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   409
  length (distinct (op =) (map (fn ((b, _, _, atyp), _) => (b, atyp)) l)) = 1
1397
6e2dfe52b271 Allows multiple bindings with same lhs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1394
diff changeset
   410
*}
6e2dfe52b271 Allows multiple bindings with same lhs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1394
diff changeset
   411
1679
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   412
ML {*
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   413
fun setify x =
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   414
  if fastype_of x = @{typ "atom list"} then
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   415
  Const (@{const_name set}, @{typ "atom list \<Rightarrow> atom set"}) $ x else x
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   416
*}
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   417
1175
6a3be6ef348d Reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1174
diff changeset
   418
ML {*
1836
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   419
fun define_fv (dt_info : Datatype_Aux.info) bindsall bns lthy =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   420
let
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   421
  val thy = ProofContext.theory_of lthy;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   422
  val {descr, sorts, ...} = dt_info;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   423
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   424
  val fv_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   425
    "fv_" ^ name_of_typ (nth_dtyp i)) descr);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   426
  val fv_types = map (fn (i, _) => nth_dtyp i --> @{typ "atom set"}) descr;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   427
  val fv_frees = map Free (fv_names ~~ fv_types);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   428
(* TODO: We need a transitive closure, but instead we do this hack considering
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   429
   all binding functions as recursive or not *)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   430
  val nr_bns =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   431
    if (non_rec_binds bindsall) = [] then []
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   432
    else map (fn (bn, _, _) => bn) bns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   433
  val rel_bns = filter (fn (bn, _, _) => bn mem nr_bns) bns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   434
  val (bn_fv_bns, fv_bn_names_eqs) = fv_bns thy dt_info fv_frees rel_bns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   435
  val fvbns = map snd bn_fv_bns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   436
  val (fv_bn_names, fv_bn_eqs) = split_list fv_bn_names_eqs;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   437
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   438
  fun fv_constr ith_dtyp (cname, dts) bindcs =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   439
    let
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   440
      val Ts = map (typ_of_dtyp descr sorts) dts;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   441
      val bindslen = length bindcs
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   442
      val pi_strs_same = replicate bindslen "pi"
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   443
      val pi_strs = Name.variant_list [] pi_strs_same;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   444
      val pis = map (fn ps => Free (ps, @{typ perm})) pi_strs;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   445
      val bind_pis_gath = bindcs ~~ pis;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   446
      val bind_pis = un_gather_binds_cons bind_pis_gath;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   447
      val bindcs = map fst bind_pis;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   448
      val names = Name.variant_list pi_strs (Datatype_Prop.make_tnames Ts);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   449
      val args = map Free (names ~~ Ts);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   450
      val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   451
      val fv_c = nth fv_frees ith_dtyp;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   452
      val arg_nos = 0 upto (length dts - 1)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   453
      fun fv_bind args (NONE, i, _, _) =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   454
            if is_rec_type (nth dts i) then (nth fv_frees (body_index (nth dts i))) $ (nth args i) else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   455
            if ((is_atom thy) o fastype_of) (nth args i) then mk_single_atom (nth args i) else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   456
            if ((is_atom_set thy) o fastype_of) (nth args i) then mk_atom_set (nth args i) else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   457
            if ((is_atom_fset thy) o fastype_of) (nth args i) then mk_atom_fset (nth args i) else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   458
            (* TODO goes the code for preiously defined nominal datatypes *)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   459
            @{term "{} :: atom set"}
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   460
        | fv_bind args (SOME (f, _), i, _, _) = f $ (nth args i)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   461
      fun fv_binds_as_set args relevant = mk_union (map (setify o fv_bind args) relevant)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   462
      fun find_nonrec_binder j (SOME (f, false), i, _, _) = if i = j then SOME f else NONE
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   463
        | find_nonrec_binder _ _ = NONE
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   464
      fun fv_arg ((dt, x), arg_no) =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   465
        case get_first (find_nonrec_binder arg_no) bindcs of
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   466
          SOME f =>
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   467
            (case get_first (fn (x, y) => if x = f then SOME y else NONE) bn_fv_bns of
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   468
                SOME fv_bn => fv_bn $ x
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   469
              | NONE => error "bn specified in a non-rec binding but not in bn list")
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   470
        | NONE =>
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   471
            let
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   472
              val arg =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   473
                if is_rec_type dt then nth fv_frees (body_index dt) $ x else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   474
                if ((is_atom thy) o fastype_of) x then mk_single_atom x else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   475
                if ((is_atom_set thy) o fastype_of) x then mk_atom_set x else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   476
                if ((is_atom_fset thy) o fastype_of) x then mk_atom_fset x else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   477
                (* TODO goes the code for preiously defined nominal datatypes *)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   478
                @{term "{} :: atom set"};
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   479
              (* If i = j then we generate it only once *)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   480
              val relevant = filter (fn (_, i, j, _) => ((i = arg_no) orelse (j = arg_no))) bindcs;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   481
              val sub = fv_binds_as_set args relevant
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   482
            in
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   483
              mk_diff arg sub
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   484
            end;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   485
      val fv_eq = HOLogic.mk_Trueprop (HOLogic.mk_eq
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   486
        (fv_c $ list_comb (c, args), mk_union (map fv_arg  (dts ~~ args ~~ arg_nos))))
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   487
    in
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   488
      fv_eq
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   489
    end;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   490
  fun fv_eq (i, (_, _, constrs)) binds = map2i (fv_constr i) constrs binds;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   491
  val fveqs = map2i fv_eq descr (gather_binds bindsall)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   492
  val fv_eqs_perfv = fveqs
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   493
  val rel_bns_nos = map (fn (_, i, _) => i) rel_bns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   494
  fun filter_fun (_, b) = b mem rel_bns_nos;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   495
  val all_fvs = (fv_names ~~ fv_eqs_perfv) ~~ (0 upto (length fv_names - 1))
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   496
  val (fv_names_fst, fv_eqs_fst) = apsnd flat (split_list (map fst (filter_out filter_fun all_fvs)))
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   497
  val (fv_names_snd, fv_eqs_snd) = apsnd flat (split_list (map fst (filter filter_fun all_fvs)))
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   498
  val fv_eqs_all = fv_eqs_fst @ (flat fv_bn_eqs);
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   499
  val fv_names_all = fv_names_fst @ fv_bn_names;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   500
  val add_binds = map (fn x => (Attrib.empty_binding, x))
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   501
(* Function_Fun.add_fun Function_Common.default_config ... true *)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   502
  val (fvs, lthy') = (Primrec.add_primrec
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   503
    (map (fn s => (Binding.name s, NONE, NoSyn)) fv_names_all) (add_binds fv_eqs_all) lthy)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   504
  val (fvs2, lthy'') =
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   505
    if fv_eqs_snd = [] then (([], []), lthy') else
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   506
   (Primrec.add_primrec
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   507
    (map (fn s => (Binding.name s, NONE, NoSyn)) fv_names_snd) (add_binds fv_eqs_snd) lthy')
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   508
  val ordered_fvs = fv_frees @ fvbns;
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   509
  val all_fvs = (fst fvs @ fst fvs2, snd fvs @ snd fvs2)
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   510
in
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   511
  ((all_fvs, ordered_fvs), lthy'')
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   512
end
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   513
*}
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   514
41054d1eb6f0 Separate define_fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1830
diff changeset
   515
ML {*
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   516
fun define_alpha (dt_info : Datatype_Aux.info) bindsall bns fv_frees lthy =
1178
275a1cb3f2ba Reorder
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1177
diff changeset
   517
let
1366
2bf82fa871e7 Proper recognition of atoms and atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1362
diff changeset
   518
  val thy = ProofContext.theory_of lthy;
1277
6eacf60ce41d Permutation and FV_Alpha interface change.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1258
diff changeset
   519
  val {descr, sorts, ...} = dt_info;
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   520
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
1622
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   521
(* TODO: We need a transitive closure, but instead we do this hack considering
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   522
   all binding functions as recursive or not *)
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   523
  val nr_bns =
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   524
    if (non_rec_binds bindsall) = [] then []
006d81399f6a Compute Fv for non-recursive bn functions calling other bn functions
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   525
    else map (fn (bn, _, _) => bn) bns;
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   526
  val alpha_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   527
    "alpha_" ^ name_of_typ (nth_dtyp i)) descr);
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   528
  val alpha_types = map (fn (i, _) => nth_dtyp i --> nth_dtyp i --> @{typ bool}) descr;
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   529
  val alpha_frees = map Free (alpha_names ~~ alpha_types);
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   530
  (* We assume that a bn is either recursive or not *)
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   531
  val bns_rec = map (fn (bn, _, _) => not (bn mem nr_bns)) bns;
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1609
diff changeset
   532
  val (alpha_bn_names, (bn_alpha_bns, alpha_bn_eqs)) =
1669
9911824a5396 Minor cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1658
diff changeset
   533
    alpha_bns dt_info alpha_frees bns bns_rec
1389
d0ba4829a76c include alpha in the definitions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1388
diff changeset
   534
  val alpha_bn_frees = map snd bn_alpha_bns;
d0ba4829a76c include alpha in the definitions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1388
diff changeset
   535
  val alpha_bn_types = map fastype_of alpha_bn_frees;
1679
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   536
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   537
  fun alpha_constr ith_dtyp (cname, dts) bindcs =
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   538
    let
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   539
      val Ts = map (typ_of_dtyp descr sorts) dts;
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   540
      val bindslen = length bindcs
1323
acf262971303 Fixes for the fv problem and alpha problem.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1308
diff changeset
   541
      val pi_strs_same = replicate bindslen "pi"
acf262971303 Fixes for the fv problem and alpha problem.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1308
diff changeset
   542
      val pi_strs = Name.variant_list [] pi_strs_same;
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   543
      val pis = map (fn ps => Free (ps, @{typ perm})) pi_strs;
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   544
      val bind_pis_gath = bindcs ~~ pis;
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   545
      val bind_pis = un_gather_binds_cons bind_pis_gath;
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   546
      val names = Name.variant_list pi_strs (Datatype_Prop.make_tnames Ts);
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   547
      val args = map Free (names ~~ Ts);
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   548
      val names2 = Name.variant_list (pi_strs @ names) (Datatype_Prop.make_tnames Ts);
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   549
      val args2 = map Free (names2 ~~ Ts);
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   550
      val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   551
      val alpha = nth alpha_frees ith_dtyp;
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   552
      val arg_nos = 0 upto (length dts - 1)
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   553
      fun fv_bind args (NONE, i, _, _) =
1177
6f01720fe520 Add bindings of recursive types by free_variables.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1176
diff changeset
   554
            if is_rec_type (nth dts i) then (nth fv_frees (body_index (nth dts i))) $ (nth args i) else
1366
2bf82fa871e7 Proper recognition of atoms and atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1362
diff changeset
   555
            if ((is_atom thy) o fastype_of) (nth args i) then mk_single_atom (nth args i) else
1534
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   556
            if ((is_atom_set thy) o fastype_of) (nth args i) then mk_atom_set (nth args i) else
984ea1299cd7 The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1516
diff changeset
   557
            if ((is_atom_fset thy) o fastype_of) (nth args i) then mk_atom_fset (nth args i) else
1830
8db45a106569 Initial cleaning/reorganization in Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   558
            (* TODO goes the code for preiously defined nominal datatypes *)
1366
2bf82fa871e7 Proper recognition of atoms and atom sets.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1362
diff changeset
   559
            @{term "{} :: atom set"}
1679
5c4566797bcb Accepts lists in FV.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1677
diff changeset
   560
        | fv_bind args (SOME (f, _), i, _, _) = f $ (nth args i)
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   561
      fun fv_binds args relevant = mk_union (map (fv_bind args) relevant)
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   562
      val alpha_rhs =
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   563
        HOLogic.mk_Trueprop (alpha $ (list_comb (c, args)) $ (list_comb (c, args2)));
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   564
      fun alpha_arg ((dt, arg_no), (arg, arg2)) =
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   565
        let
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   566
          val rel_in_simp_binds = filter (fn ((NONE, i, _, _), _) => i = arg_no | _ => false) bind_pis;
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   567
          val rel_in_comp_binds = filter (fn ((SOME _, i, _, _), _) => i = arg_no | _ => false) bind_pis;
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   568
          val rel_has_binds = filter (fn ((NONE, _, j, _), _) => j = arg_no
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   569
                                       | ((SOME (_, false), _, j, _), _) => j = arg_no
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   570
                                       | _ => false) bind_pis;
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   571
          val rel_has_rec_binds = filter
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   572
            (fn ((SOME (_, true), _, j, _), _) => j = arg_no | _ => false) bind_pis;
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   573
        in
1472
4fa5365cd871 Fix in alpha; support of the recursive Let works :)
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1468
diff changeset
   574
          case (rel_in_simp_binds, rel_in_comp_binds, rel_has_binds, rel_has_rec_binds) of
4fa5365cd871 Fix in alpha; support of the recursive Let works :)
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1468
diff changeset
   575
            ([], [], [], []) =>
1383
8399236f901b Use alpha_bns in normal alpha defs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1382
diff changeset
   576
              if is_rec_type dt then (nth alpha_frees (body_index dt) $ arg $ arg2)
8399236f901b Use alpha_bns in normal alpha defs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1382
diff changeset
   577
              else (HOLogic.mk_eq (arg, arg2))
1472
4fa5365cd871 Fix in alpha; support of the recursive Let works :)
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1468
diff changeset
   578
          | (_, [], [], []) => @{term True}
4fa5365cd871 Fix in alpha; support of the recursive Let works :)
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1468
diff changeset
   579
          | ([], [], [], _) => @{term True}
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   580
          | ([], ((((SOME (bn, is_rec)), _, _, atyp), _) :: _), [], []) =>
1462
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   581
            if not (bns_same rel_in_comp_binds) then error "incompatible bindings for an argument" else
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   582
            if is_rec then
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   583
              let
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   584
                val (rbinds, rpis) = split_list rel_in_comp_binds
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   585
                val bound_in_nos = map (fn (_, _, i, _) => i) rbinds
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   586
                val bound_in_ty_nos = map (fn i => body_index (nth dts i)) bound_in_nos;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   587
                val bound_args = arg :: map (nth args) bound_in_nos;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   588
                val bound_args2 = arg2 :: map (nth args2) bound_in_nos;
1462
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   589
                val lhs_binds = fv_binds args rbinds
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   590
                val lhs_arg = foldr1 HOLogic.mk_prod bound_args
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   591
                val lhs = mk_pair (lhs_binds, lhs_arg);
1462
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   592
                val rhs_binds = fv_binds args2 rbinds;
1468
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   593
                val rhs_arg = foldr1 HOLogic.mk_prod bound_args2;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   594
                val rhs = mk_pair (rhs_binds, rhs_arg);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   595
                val fvs = map (nth fv_frees) ((body_index dt) :: bound_in_ty_nos);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   596
                val fv = mk_compound_fv fvs;
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   597
                val alphas = map (nth alpha_frees) ((body_index dt) :: bound_in_ty_nos);
416c9c5a1126 Generate compound FV and Alpha for recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1464
diff changeset
   598
                val alpha = mk_compound_alpha alphas;
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1838
diff changeset
   599
                val pi = foldr1 (uncurry mk_plus) (distinct (op =) rpis);
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   600
                val alpha_gen_pre = Const (atyp_const atyp, dummyT) $ lhs $ alpha $ fv $ pi $ rhs;
1462
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   601
                val alpha_gen = Syntax.check_term lthy alpha_gen_pre
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   602
              in
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   603
                alpha_gen
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   604
              end
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   605
            else
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   606
              let
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   607
                val alpha_bn_const =
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   608
                  nth alpha_bn_frees (find_index (fn (b, _, _) => b = bn) bns)
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   609
              in
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   610
                alpha_bn_const $ arg $ arg2
7c59dd8e5435 The old recursive alpha works fine.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1457
diff changeset
   611
              end
1472
4fa5365cd871 Fix in alpha; support of the recursive Let works :)
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1468
diff changeset
   612
          | ([], [], relevant, []) =>
1383
8399236f901b Use alpha_bns in normal alpha defs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1382
diff changeset
   613
            let
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   614
              val (rbinds, rpis) = split_list relevant
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   615
              val lhs_binds = fv_binds args rbinds
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   616
              val lhs = mk_pair (lhs_binds, arg);
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   617
              val rhs_binds = fv_binds args2 rbinds;
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   618
              val rhs = mk_pair (rhs_binds, arg2);
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   619
              val alpha = nth alpha_frees (body_index dt);
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   620
              val fv = nth fv_frees (body_index dt);
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1838
diff changeset
   621
              val pi = foldr1 (uncurry mk_plus) (distinct (op =) rpis);
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   622
              val alpha_const = alpha_const_for_binds rbinds;
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1669
diff changeset
   623
              val alpha_gen_pre = Const (alpha_const, dummyT) $ lhs $ alpha $ fv $ pi $ rhs;
1325
0be098c61d00 Add the supp intersection conditions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1323
diff changeset
   624
              val alpha_gen = Syntax.check_term lthy alpha_gen_pre
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   625
            in
1357
42b7abf779ec Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   626
              alpha_gen
1383
8399236f901b Use alpha_bns in normal alpha defs.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1382
diff changeset
   627
            end
1385
51b8e6dd72d5 exported template for alpha_bn
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1383
diff changeset
   628
          | _ => error "Fv.alpha: not supported binding structure"
1288
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   629
        end
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   630
      val alphas = map alpha_arg (dts ~~ arg_nos ~~ (args ~~ args2))
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   631
      val alpha_lhss = mk_conjl alphas
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   632
      val alpha_lhss_ex =
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   633
        fold (fn pi_str => fn t => HOLogic.mk_exists (pi_str, @{typ perm}, t)) pi_strs alpha_lhss
0203cd5cfd6c The new alpha-equivalence and testing in Trm2 and Trm5.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   634
      val alpha_eq = Logic.mk_implies (HOLogic.mk_Trueprop alpha_lhss_ex, alpha_rhs)
1173
9cb99a28b40e Some optimizations and fixes.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1172
diff changeset
   635
    in
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   636
      alpha_eq
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   637
    end;
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   638
  fun alpha_eq (i, (_, _, constrs)) binds = map2i (alpha_constr i) constrs binds;
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   639
  val alphaeqs = map2i alpha_eq descr (gather_binds bindsall)
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   640
  val alpha_eqs = flat alphaeqs
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   641
  val add_binds = map (fn x => (Attrib.empty_binding, x))
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   642
  val (alphas, lthy') = (Inductive.add_inductive_i
1325
0be098c61d00 Add the supp intersection conditions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1323
diff changeset
   643
     {quiet_mode = true, verbose = false, alt_name = Binding.empty,
1193
a228acf2907e Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1192
diff changeset
   644
      coind = false, no_elim = false, no_ind = false, skip_mono = true, fork_mono = false}
1389
d0ba4829a76c include alpha in the definitions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1388
diff changeset
   645
     (map2 (fn x => fn y => ((Binding.name x, y), NoSyn)) (alpha_names @ alpha_bn_names)
d0ba4829a76c include alpha in the definitions.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1388
diff changeset
   646
     (alpha_types @ alpha_bn_types)) []
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   647
     (add_binds (alpha_eqs @ flat alpha_bn_eqs)) [] lthy)
1178
275a1cb3f2ba Reorder
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1177
diff changeset
   648
in
1838
9978fc6d91e9 Separate alpha_definition.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1836
diff changeset
   649
  (alphas, lthy')
1178
275a1cb3f2ba Reorder
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1177
diff changeset
   650
end
1168
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   651
*}
5c1e16806901 Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   652
2015
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   653
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   654
ML {*
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   655
fun define_fv_alpha_export dt binds bns ctxt =
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   656
let
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   657
  val (((fv_ts_loc, fv_def_loc), ord_fv_ts_loc), ctxt') =
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   658
    define_fv dt binds bns ctxt;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   659
  val (alpha, ctxt'') =
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   660
    define_alpha dt binds bns fv_ts_loc ctxt';
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   661
  val alpha_ts_loc = #preds alpha
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   662
  val alpha_induct_loc = #induct alpha
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   663
  val alpha_intros_loc = #intrs alpha;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   664
  val alpha_cases_loc = #elims alpha
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   665
  val morphism = ProofContext.export_morphism ctxt'' ctxt;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   666
  val fv_ts = map (Morphism.term morphism) fv_ts_loc;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   667
  val ord_fv_ts = map (Morphism.term morphism) ord_fv_ts_loc;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   668
  val fv_def = Morphism.fact morphism fv_def_loc;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   669
  val alpha_ts = map (Morphism.term morphism) alpha_ts_loc;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   670
  val alpha_induct = Morphism.thm morphism alpha_induct_loc;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   671
  val alpha_intros = Morphism.fact morphism alpha_intros_loc
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   672
  val alpha_cases = Morphism.fact morphism alpha_cases_loc
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   673
in
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   674
  ((((fv_ts, ord_fv_ts), fv_def), ((alpha_ts, alpha_intros), (alpha_cases, alpha_induct))), ctxt'')
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   675
end;
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   676
*}
3e7969262809 Move old fv_alpha_export to Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2014
diff changeset
   677
1196
4efbaba9d754 Constructing alpha_inj goal.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1193
diff changeset
   678
end