src/HOL/Library/List_Comprehension.thy
author nipkow
Sat, 26 May 2007 07:26:03 +0200
changeset 23110 f5de7da165bb
permissions -rw-r--r--
What it says
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23110
f5de7da165bb What it says
nipkow
parents:
diff changeset
     1
(*  Title:      HOL/Library/List_Comprehension.thy
f5de7da165bb What it says
nipkow
parents:
diff changeset
     2
    ID:         $Id$
f5de7da165bb What it says
nipkow
parents:
diff changeset
     3
    Author:     Tobias Nipkow
f5de7da165bb What it says
nipkow
parents:
diff changeset
     4
*)
f5de7da165bb What it says
nipkow
parents:
diff changeset
     5
f5de7da165bb What it says
nipkow
parents:
diff changeset
     6
header {* List Comprehension *}
f5de7da165bb What it says
nipkow
parents:
diff changeset
     7
f5de7da165bb What it says
nipkow
parents:
diff changeset
     8
theory List_Comprehension
f5de7da165bb What it says
nipkow
parents:
diff changeset
     9
imports Main
f5de7da165bb What it says
nipkow
parents:
diff changeset
    10
begin
f5de7da165bb What it says
nipkow
parents:
diff changeset
    11
f5de7da165bb What it says
nipkow
parents:
diff changeset
    12
text{* At the moment this theory provides only the input syntax for
f5de7da165bb What it says
nipkow
parents:
diff changeset
    13
list comprehension: @{text"[x. x \<leftarrow> xs, \<dots>]"} rather than
f5de7da165bb What it says
nipkow
parents:
diff changeset
    14
\verb![x| x <- xs, ...]! as in Haskell.
f5de7da165bb What it says
nipkow
parents:
diff changeset
    15
f5de7da165bb What it says
nipkow
parents:
diff changeset
    16
The print translation from internal form to list comprehensions would
f5de7da165bb What it says
nipkow
parents:
diff changeset
    17
be nice. Unfortunately one cannot just turn the translations around
f5de7da165bb What it says
nipkow
parents:
diff changeset
    18
because in the final equality @{text p} occurs twice on the
f5de7da165bb What it says
nipkow
parents:
diff changeset
    19
right-hand side. Due to this restriction, the translation must be hand-coded.
f5de7da165bb What it says
nipkow
parents:
diff changeset
    20
f5de7da165bb What it says
nipkow
parents:
diff changeset
    21
A more substantial extension would be proper theorem proving
f5de7da165bb What it says
nipkow
parents:
diff changeset
    22
support. For example, it would be nice if
f5de7da165bb What it says
nipkow
parents:
diff changeset
    23
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
f5de7da165bb What it says
nipkow
parents:
diff changeset
    24
produced something like
f5de7da165bb What it says
nipkow
parents:
diff changeset
    25
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.  *}
f5de7da165bb What it says
nipkow
parents:
diff changeset
    26
f5de7da165bb What it says
nipkow
parents:
diff changeset
    27
nonterminals lc_gentest
f5de7da165bb What it says
nipkow
parents:
diff changeset
    28
f5de7da165bb What it says
nipkow
parents:
diff changeset
    29
syntax
f5de7da165bb What it says
nipkow
parents:
diff changeset
    30
"_listcompr" :: "'a \<Rightarrow> idt \<Rightarrow> 'b list \<Rightarrow> lc_gentest \<Rightarrow> 'a list" ("[_ . _ \<leftarrow> __")
f5de7da165bb What it says
nipkow
parents:
diff changeset
    31
"_lc_end" :: "lc_gentest" ("]")
f5de7da165bb What it says
nipkow
parents:
diff changeset
    32
"_lc_gen" :: "idt \<Rightarrow> 'a list \<Rightarrow> lc_gentest \<Rightarrow> lc_gentest" (",_ \<leftarrow> __")
f5de7da165bb What it says
nipkow
parents:
diff changeset
    33
"_lc_test" :: "bool \<Rightarrow> lc_gentest \<Rightarrow> lc_gentest" (",__")
f5de7da165bb What it says
nipkow
parents:
diff changeset
    34
f5de7da165bb What it says
nipkow
parents:
diff changeset
    35
f5de7da165bb What it says
nipkow
parents:
diff changeset
    36
translations
f5de7da165bb What it says
nipkow
parents:
diff changeset
    37
 "[e. p\<leftarrow>xs]" => "map (%p. e) xs"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    38
 "_listcompr e p xs (_lc_gen q ys GT)" =>
f5de7da165bb What it says
nipkow
parents:
diff changeset
    39
  "concat (map (%p. _listcompr e q ys GT) xs)"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    40
 "_listcompr e p xs (_lc_test P GT)" => "_listcompr e p (filter (%p. P) xs) GT"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    41
f5de7da165bb What it says
nipkow
parents:
diff changeset
    42
(*
f5de7da165bb What it says
nipkow
parents:
diff changeset
    43
term "[(x,y). x \<leftarrow> xs, x<y]"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    44
term "[(x,y). x \<leftarrow> xs, x<y, z\<leftarrow>zs]"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    45
term "[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x<y]"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    46
term "[(x,y,z). x \<leftarrow> xs, y \<leftarrow> ys, z\<leftarrow> zs]"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    47
term "[x. x \<leftarrow> xs, x < a, x > b]"
f5de7da165bb What it says
nipkow
parents:
diff changeset
    48
*)
f5de7da165bb What it says
nipkow
parents:
diff changeset
    49
f5de7da165bb What it says
nipkow
parents:
diff changeset
    50
end