annotate lib/ur/list.ur @ 826:78504d97410b

Fix EDLet elab_util bug
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 May 2009 12:40:55 -0400
parents d4e811beb8eb
children d07980bf1444
rev   line source
adamc@794 1 datatype t = datatype Basis.list
adamc@794 2
adamc@826 3 val show = fn [a] (_ : show a) =>
adamc@826 4 let
adamc@826 5 fun show' (ls : list a) =
adamc@826 6 case ls of
adamc@826 7 [] => "[]"
adamc@826 8 | x :: ls => show x ^ " :: " ^ show' ls
adamc@826 9 in
adamc@826 10 mkShow show'
adamc@826 11 end
adamc@794 12
adamc@826 13 val rev = fn [a] =>
adamc@826 14 let
adamc@826 15 fun rev' acc (ls : list a) =
adamc@826 16 case ls of
adamc@826 17 [] => acc
adamc@826 18 | x :: ls => rev' (x :: acc) ls
adamc@826 19 in
adamc@826 20 rev' []
adamc@826 21 end
adamc@794 22
adamc@826 23 val revAppend = fn [a] =>
adamc@826 24 let
adamc@826 25 fun ra (ls : list a) acc =
adamc@826 26 case ls of
adamc@826 27 [] => acc
adamc@826 28 | x :: ls => ra ls (x :: acc)
adamc@826 29 in
adamc@826 30 ra
adamc@826 31 end
adamc@821 32
adamc@826 33 fun append [a] (ls1 : t a) (ls2 : t a) = revAppend (rev ls1) ls2
adamc@821 34
adamc@826 35 fun mp [a] [b] f =
adamc@794 36 let
adamc@794 37 fun mp' acc ls =
adamc@794 38 case ls of
adamc@794 39 [] => rev acc
adamc@794 40 | x :: ls => mp' (f x :: acc) ls
adamc@794 41 in
adamc@794 42 mp' []
adamc@794 43 end
adamc@796 44
adamc@826 45 fun mapPartial [a] [b] f =
adamc@821 46 let
adamc@821 47 fun mp' acc ls =
adamc@821 48 case ls of
adamc@821 49 [] => rev acc
adamc@821 50 | x :: ls => mp' (case f x of
adamc@821 51 None => acc
adamc@821 52 | Some y => y :: acc) ls
adamc@821 53 in
adamc@821 54 mp' []
adamc@821 55 end
adamc@821 56
adamc@826 57 fun mapX [a] [ctx ::: {Unit}] f =
adamc@796 58 let
adamc@796 59 fun mapX' ls =
adamc@796 60 case ls of
adamc@796 61 [] => <xml/>
adamc@796 62 | x :: ls => <xml>{f x}{mapX' ls}</xml>
adamc@796 63 in
adamc@796 64 mapX'
adamc@796 65 end
adamc@800 66
adamc@826 67 fun mapM [m ::: (Type -> Type)] (_ : monad m) [a] [b] f =
adamc@800 68 let
adamc@800 69 fun mapM' acc ls =
adamc@800 70 case ls of
adamc@818 71 [] => return (rev acc)
adamc@818 72 | x :: ls => x' <- f x; mapM' (x' :: acc) ls
adamc@800 73 in
adamc@818 74 mapM' []
adamc@800 75 end
adamc@821 76
adamc@826 77 fun filter [a] f =
adamc@821 78 let
adamc@821 79 fun fil acc ls =
adamc@821 80 case ls of
adamc@821 81 [] => rev acc
adamc@821 82 | x :: ls => fil (if f x then x :: acc else acc) ls
adamc@821 83 in
adamc@821 84 fil []
adamc@821 85 end
adamc@822 86
adamc@826 87 fun exists [a] f =
adamc@822 88 let
adamc@822 89 fun ex ls =
adamc@822 90 case ls of
adamc@822 91 [] => False
adamc@822 92 | x :: ls =>
adamc@822 93 if f x then
adamc@822 94 True
adamc@822 95 else
adamc@822 96 ex ls
adamc@822 97 in
adamc@822 98 ex
adamc@822 99 end
adamc@822 100
adamc@826 101 fun foldlMap [a] [b] [c] f =
adamc@822 102 let
adamc@822 103 fun fold ls' st ls =
adamc@822 104 case ls of
adamc@822 105 [] => (rev ls', st)
adamc@822 106 | x :: ls =>
adamc@822 107 case f x st of
adamc@822 108 (y, st) => fold (y :: ls') st ls
adamc@822 109 in
adamc@822 110 fold []
adamc@822 111 end