comparison lib/ur/top.ur @ 653:e5894f0e541a

Change location/type of [fold] to be more uniform w.r.t. derived folders
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Mar 2009 10:26:04 -0400
parents fcf0bd3d1667
children 5bbb542243e8
comparison
equal deleted inserted replaced
652:9db6880184d0 653:e5894f0e541a
1 (** Row folding *) 1 (** Row folding *)
2 2
3 con folder = K ==> fn r :: {K} => 3 con folder = K ==> fn r :: {K} =>
4 tf :: ({K} -> Type) 4 tf :: ({K} -> Type)
5 -> (nm :: Name -> v :: K -> r :: {K} -> tf r 5 -> (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
6 -> [[nm] ~ r] => tf ([nm = v] ++ r)) 6 tf r -> tf ([nm = v] ++ r))
7 -> tf [] -> tf r 7 -> tf [] -> tf r
8 8
9 fun fold K (tf :: {K} -> Type)
10 (f : (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
11 tf r -> tf ([nm = v] ++ r)))
12 (i : tf []) (r :: {K}) (fl : folder r) = fl [tf] f i
13
9 structure Folder = struct 14 structure Folder = struct
10 fun fold K (r :: {K}) (fl : folder r) = fl
11
12 fun nil K (tf :: {K} -> Type) 15 fun nil K (tf :: {K} -> Type)
13 (f : nm :: Name -> v :: K -> r :: {K} -> tf r 16 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
14 -> [[nm] ~ r] => tf ([nm = v] ++ r)) 17 tf r -> tf ([nm = v] ++ r))
15 (i : tf []) = i 18 (i : tf []) = i
16 19
17 fun cons K (r ::: {K}) (nm :: Name) (v :: K) [[nm] ~ r] (fold : folder r) 20 fun cons K (r ::: {K}) (nm :: Name) (v :: K) [[nm] ~ r] (fold : folder r)
18 (tf :: {K} -> Type) 21 (tf :: {K} -> Type)
19 (f : nm :: Name -> v :: K -> r :: {K} -> tf r 22 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
20 -> [[nm] ~ r] => tf ([nm = v] ++ r)) 23 tf r -> tf ([nm = v] ++ r))
21 (i : tf []) = f [nm] [v] [r] (fold [tf] f i) ! 24 (i : tf []) = f [nm] [v] [r] ! (fold [tf] f i)
22 25
23 fun concat K (r1 ::: {K}) (r2 ::: {K}) [r1 ~ r2] 26 fun concat K (r1 ::: {K}) (r2 ::: {K}) [r1 ~ r2]
24 (f1 : folder r1) (f2 : folder r2) 27 (f1 : folder r1) (f2 : folder r2)
25 (tf :: {K} -> Type) 28 (tf :: {K} -> Type)
26 (f : nm :: Name -> v :: K -> r :: {K} -> tf r 29 (f : nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] =>
27 -> [[nm] ~ r] => tf ([nm = v] ++ r)) 30 tf r -> tf ([nm = v] ++ r))
28 (i : tf []) = 31 (i : tf []) =
29 f1 [fn r1' => [r1' ~ r2] => tf (r1' ++ r2)] 32 f1 [fn r1' => [r1' ~ r2] => tf (r1' ++ r2)]
30 (fn (nm :: Name) (v :: K) (r1' :: {K}) (acc : [r1' ~ r2] => tf (r1' ++ r2)) 33 (fn (nm :: Name) (v :: K) (r1' :: {K}) [[nm] ~ r1']
31 [[nm] ~ r1'] [[nm = v] ++ r1' ~ r2] => 34 (acc : [r1' ~ r2] => tf (r1' ++ r2))
32 f [nm] [v] [r1' ++ r2] acc !) 35 [[nm = v] ++ r1' ~ r2] =>
36 f [nm] [v] [r1' ++ r2] ! acc)
33 (fn [[] ~ r2] => f2 [tf] f i) ! 37 (fn [[] ~ r2] => f2 [tf] f i) !
34 38
35 fun mp K1 K2 (f ::: K1 -> K2) (r ::: {K1}) 39 fun mp K1 K2 (f ::: K1 -> K2) (r ::: {K1})
36 (fold : folder r) 40 (fold : folder r)
37 (tf :: {K2} -> Type) 41 (tf :: {K2} -> Type)
38 (f : nm :: Name -> v :: K2 -> r :: {K2} -> tf r 42 (f : nm :: Name -> v :: K2 -> r :: {K2} -> [[nm] ~ r] =>
39 -> [[nm] ~ r] => tf ([nm = v] ++ r)) 43 tf r -> tf ([nm = v] ++ r))
40 (i : tf []) = 44 (i : tf []) =
41 fold [fn r => tf (map f r)] 45 fold [fn r => tf (map f r)]
42 (fn (nm :: Name) (v :: K1) (rest :: {K1}) (acc : tf (map f rest)) [[nm] ~ rest] => 46 (fn (nm :: Name) (v :: K1) (rest :: {K1}) [[nm] ~ rest] (acc : tf (map f rest)) =>
43 f [nm] [f v] [map f rest] acc !) 47 f [nm] [f v] [map f rest] ! acc)
44 i 48 i
45 end 49 end
46 50
47 51
48 fun not b = if b then False else True 52 fun not b = if b then False else True
72 76
73 fun foldUR (tf :: Type) (tr :: {Unit} -> Type) 77 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
74 (f : nm :: Name -> rest :: {Unit} 78 (f : nm :: Name -> rest :: {Unit}
75 -> [[nm] ~ rest] => 79 -> [[nm] ~ rest] =>
76 tf -> tr rest -> tr ([nm] ++ rest)) 80 tf -> tr rest -> tr ([nm] ++ rest))
77 (i : tr []) (r :: {Unit}) (fold : folder r)= 81 (i : tr []) (r :: {Unit}) (fl : folder r)=
78 fold [fn r :: {Unit} => $(mapU tf r) -> tr r] 82 fl [fn r :: {Unit} => $(mapU tf r) -> tr r]
79 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc 83 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r =>
80 [[nm] ~ rest] r => 84 f [nm] [rest] ! r.nm (acc (r -- nm)))
81 f [nm] [rest] ! r.nm (acc (r -- nm))) 85 (fn _ => i)
82 (fn _ => i)
83 86
84 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type) 87 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
85 (f : nm :: Name -> rest :: {Unit} 88 (f : nm :: Name -> rest :: {Unit}
86 -> [[nm] ~ rest] => 89 -> [[nm] ~ rest] =>
87 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest)) 90 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
88 (i : tr []) (r :: {Unit}) (fold : folder r) = 91 (i : tr []) (r :: {Unit}) (fl : folder r) =
89 fold [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r] 92 fl [fn r :: {Unit} => $(mapU tf1 r) -> $(mapU tf2 r) -> tr r]
90 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc 93 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) [[nm] ~ rest] acc r1 r2 =>
91 [[nm] ~ rest] r1 r2 => 94 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
92 f [nm] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm))) 95 (fn _ _ => i)
93 (fn _ _ => i)
94 96
95 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit}) 97 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
96 (f : nm :: Name -> rest :: {Unit} 98 (f : nm :: Name -> rest :: {Unit}
97 -> [[nm] ~ rest] => 99 -> [[nm] ~ rest] =>
98 tf1 -> tf2 -> xml ctx [] []) = 100 tf1 -> tf2 -> xml ctx [] []) =
103 105
104 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type) 106 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type)
105 (f : nm :: Name -> t :: K -> rest :: {K} 107 (f : nm :: Name -> t :: K -> rest :: {K}
106 -> [[nm] ~ rest] => 108 -> [[nm] ~ rest] =>
107 tf t -> tr rest -> tr ([nm = t] ++ rest)) 109 tf t -> tr rest -> tr ([nm = t] ++ rest))
108 (i : tr []) (r :: {K}) (fold : folder r) = 110 (i : tr []) (r :: {K}) (fl : folder r) =
109 fold [fn r :: {K} => $(map tf r) -> tr r] 111 fl [fn r :: {K} => $(map tf r) -> tr r]
110 (fn (nm :: Name) (t :: K) (rest :: {K}) (acc : _ -> tr rest) 112 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] (acc : _ -> tr rest) r =>
111 [[nm] ~ rest] r => 113 f [nm] [t] [rest] ! r.nm (acc (r -- nm)))
112 f [nm] [t] [rest] ! r.nm (acc (r -- nm))) 114 (fn _ => i)
113 (fn _ => i)
114 115
115 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type) 116 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type)
116 (f : nm :: Name -> t :: K -> rest :: {K} 117 (f : nm :: Name -> t :: K -> rest :: {K}
117 -> [[nm] ~ rest] => 118 -> [[nm] ~ rest] =>
118 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest)) 119 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
119 (i : tr []) (r :: {K}) (fold : folder r) = 120 (i : tr []) (r :: {K}) (fl : folder r) =
120 fold [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r] 121 fl [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
121 (fn (nm :: Name) (t :: K) (rest :: {K}) 122 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest]
122 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 => 123 (acc : _ -> _ -> tr rest) r1 r2 =>
123 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm))) 124 f [nm] [t] [rest] ! r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
124 (fn _ _ => i) 125 (fn _ _ => i)
125 126
126 fun foldRX K (tf :: K -> Type) (ctx :: {Unit}) 127 fun foldRX K (tf :: K -> Type) (ctx :: {Unit})
127 (f : nm :: Name -> t :: K -> rest :: {K} 128 (f : nm :: Name -> t :: K -> rest :: {K}
128 -> [[nm] ~ rest] => 129 -> [[nm] ~ rest] =>
129 tf t -> xml ctx [] []) = 130 tf t -> xml ctx [] []) =