1 (*********************************************************************************************************************************)
2 (* HaskProofToLatex: render HaskProof's as LaTeX code using trfrac.sty *)
3 (*********************************************************************************************************************************)
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import General.
8 Require Import NaturalDeduction.
9 Require Import NaturalDeductionToLatex.
10 Require Import Coq.Strings.String.
11 Require Import Coq.Lists.List.
12 Require Import HaskKinds.
13 Require Import HaskWeakVars.
14 Require Import HaskWeakTypes.
15 Require Import HaskLiteralsAndTyCons.
16 Require Import HaskStrongTypes.
17 Require Import HaskStrong.
18 Require Import HaskProof.
19 Require Import HaskCoreTypes.
21 Open Scope string_scope.
22 Section HaskProofToLatex.
24 Context {TV:Kind -> Type}.
25 Context {TVLatex:forall k, ToLatex (TV k)}.
26 Context {freshM:FreshMonad (∀ κ, TV κ)}.
27 Definition FreshM' := FMT freshM.
28 Instance FreshMon' : Monad FreshM' := FMT_Monad freshM.
30 Instance ToLatexTyCon : ToLatex TyCon.
34 Fixpoint typeToLatex (needparens:bool){κ}(t:RawHaskType TV κ) {struct t} : FMT freshM Latex :=
36 | TVar _ v => return toLatex v
37 | TCon tc => return (latex "\text{\tt{")+=+tc+=+(latex "}}")
38 | TArrow => return latex "\text{\tt{(->)}}"
39 | TCoerc _ t1 t2 t => bind t1' = typeToLatex false t1
40 ; bind t2' = typeToLatex false t2
41 ; bind t' = typeToLatex needparens t
42 ; return (latex "{(")+=+t1'+=+(latex "{\sim}")+=+
43 t2'+=+(latex ")}\Rightarrow{")+=+t'+=+(latex "}")
44 | TApp _ _ t1 t2 => match t1 with
45 | TApp _ _ TArrow tx => bind t1' = typeToLatex true tx
46 ; bind t2' = typeToLatex true t2
47 ; let body := t1'+=+(latex "{\rightarrow}")+=+t2'
48 in return if needparens then (latex "(")+=+body+=+(latex ")") else body
49 | _ => bind t1' = typeToLatex true t1
50 ; bind t2' = typeToLatex true t2
51 ; let body := t1'+=+(latex " ")+=+t2'
52 in return if needparens then (latex "(")+=+body+=+(latex ")") else body
54 | TCode ec t => bind ec' = typeToLatex true ec
55 ; bind t' = typeToLatex false t
56 ; return (latex "\code{")+=+ec'+=+(latex "}{")+=+t'+=+(latex "}")
57 | TyFunApp tfc lt => bind rest = typeListToLatex false lt
58 ; return (latex "{\text{\tt{")+=+(sanitizeForLatex (toString tfc))+=+(latex "}}}")+=+
59 (*(latex "_{")+=+(latex (toString (tfc:nat)))+=+(latex "}")+=+*)
60 (fold_left (fun x y => (latex " \ ")+=+x+=+y)
62 | TAll k f => (*bind alpha = next
63 ; bind t' = typeToLatex false (f (alpha k))
64 ; *)return (latex "(\forall ")(*+=+(@toLatex _ (TVLatex k) (alpha k))*)
65 +=+(latex "{:}")+=+(kindToLatex k)+=+(latex ")")(*+=+t'*)
67 with typeListToLatex (needparens:bool){κ}(t:RawHaskTypeList κ) {struct t} : FreshM' (list Latex) :=
69 | TyFunApp_nil => return nil
70 | TyFunApp_cons κ kl rhk rhkl => bind r = typeToLatex needparens rhk
71 ; bind rl = typeListToLatex needparens rhkl
75 Definition ltypeToLatex {Γ:TypeEnv}{κ}(t:RawHaskType TV κ)(lev:list nat) : FreshM' Latex :=
77 | nil => typeToLatex false t
78 | lv => bind t' = typeToLatex true t
86 (fold_left (fun x y => x+=+":"+=+y) (map tyvar2greek lv) "")+=+"}"
90 Definition var2string (n:nat) :=
103 | S (S (S (S (S (S (S (S (S (S (S x)))))))))) => "z_{"+++x+++"}"
105 Close Scope nat_scope.
107 Definition covar2greek (n:nat) :=
108 "{\gamma_{"+++n+++"}}".
110 Fixpoint enumerate' (n:nat){T:Type}(l:list T) : list (nat * T) :=
113 | (a::b) => (n,a)::(enumerate' (S n) b)
116 Definition enumerate {T:Type}(l:list T) := enumerate' O l.
118 Fixpoint count (n:nat) : vec nat n :=
121 | S n' => n':::(count n')
124 Fixpoint count' (lk:list Kind)(n:nat) : IList _ (fun _ => nat) lk :=
125 match lk as LK return IList _ _ LK with
127 | h::t => n::::(count' t (S n))
130 Definition InstantiatedTypeEnvString Γ : @InstantiatedTypeEnv (fun _ => nat) Γ := count' Γ O.
131 Definition InstantiatedCoercionEnvString Γ Δ : @InstantiatedCoercionEnv (fun _ => nat) nat Γ Δ := count (length Δ).
132 Definition judgmentToLatex (j:Judg) : string :=
135 let Γ' := InstantiatedTypeEnvString Γ in
136 let Δ' := InstantiatedCoercionEnvString Γ Δ in
137 let lt2l := fun nt:nat*(LeveledHaskType Γ ★) =>
141 (var2string n)+++"{:}"+++(@ltypeToLatex Γ _ (t (fun _ => nat) Γ')
142 (map (fun x:HaskTyVar Γ _ => x (fun _ => nat) Γ') l))
144 let lt2l' := fun lt:(LeveledHaskType Γ ★) =>
147 (@ltypeToLatex Γ _ (t (fun _ => nat) Γ')
148 (map (fun x:HaskTyVar Γ _ => x (fun _ => nat) Γ') l))
150 (listToLatex "" lt2l "\ ,\ " (enumerate (leaves a)))
151 +++" \ \vdash_e\ "(*+++"e{:}"*)
152 +++(listToLatex "" lt2l' "\ ,\ " (leaves b))
155 Definition j2l (j2j:Judg -> Judg) jt :=
156 @judgmentsToLatex Judg judgmentToLatex (mapOptionTree j2j jt).
158 Fixpoint nd_uruleToLatex {Γ}{Δ}{h}{c}(r:@URule Γ Δ h c) : string :=
160 | RLeft _ _ _ r => nd_uruleToLatex r
161 | RRight _ _ _ r => nd_uruleToLatex r
162 | RCanL _ _ => "CanL"
163 | RCanR _ _ => "CanR"
164 | RuCanL _ _ => "uCanL"
165 | RuCanR _ _ => "uCanR"
166 | RAssoc _ _ _ _ => "Assoc"
167 | RCossa _ _ _ _ => "Cossa"
168 | RExch _ _ _ => "Exch"
169 | RWeak _ _ => "Weak"
170 | RCont _ _ => "Cont"
173 Fixpoint nd_ruleToLatex {h}{c}(r:Rule h c) : string :=
175 | RURule _ _ _ _ r => nd_uruleToLatex r
176 | RNote _ _ _ _ _ _ => "Note"
177 | RLit _ _ _ _ => "Lit"
178 | RVar _ _ _ _ => "Var"
179 | RGlobal _ _ _ _ _ => "Global"
180 | RLam _ _ _ _ _ _ => "Abs"
181 | RCast _ _ _ _ _ _ _ => "Cast"
182 | RAbsT _ _ _ _ _ _ => "AbsT"
183 | RAppT _ _ _ _ _ _ _ => "AppT"
184 | RAppCo _ _ _ _ _ _ _ _ _ => "AppCo"
185 | RAbsCo _ _ _ _ _ _ _ _ => "AbsCo"
186 | RApp _ _ _ _ _ _ _ => "App"
187 | RLet _ _ _ _ _ _ _ => "Let"
188 | RBindingGroup _ _ _ _ _ _ => "RBindingGroup"
189 | RLetRec _ _ _ _ _ _ => "LetRec"
190 | RCase _ _ _ _ _ _ _ _ => "Case"
191 | RBrak _ _ _ _ _ _ => "Brak"
192 | REsc _ _ _ _ _ _ => "Esc"
193 | REmptyGroup _ _ => "REmptyGroup"
196 Fixpoint nd_hideURule {Γ}{Δ}{h}{c}(r:@URule Γ Δ h c) : bool :=
198 | RLeft _ _ _ r => nd_hideURule r
199 | RRight _ _ _ r => nd_hideURule r
204 | RAssoc _ _ _ _ => true
205 | RCossa _ _ _ _ => true
206 | RExch _ (T_Leaf None) b => true
207 | RExch _ a (T_Leaf None) => true
208 | RWeak _ (T_Leaf None) => true
209 | RCont _ (T_Leaf None) => true
212 Fixpoint nd_hideRule {h}{c}(r:Rule h c) : bool :=
214 | RURule _ _ _ _ r => nd_hideURule r
215 | REmptyGroup _ _ => true
216 | RBindingGroup _ _ _ _ _ _ => true
221 End HaskProofToLatex.
224 Definition nd_ml_toLatex {c}(pf:@ND _ Rule [] c) :=
227 (fun h c r => @nd_ruleToLatex h c r)
228 (fun h c r => @nd_hideRule h c r)
229 _ _ (mkSCND (systemfc_all_rules_one_conclusion) _ _ _ pf (scnd_weak [])).