Blame view

LCGparser/ENIAM_LCGstringOf.ml 5.46 KB
Wojciech Jaworski authored
1
(*
Wojciech Jaworski authored
2
 *  ENIAM_LCGparser, a parser for Logical Categorial Grammar formalism
Wojciech Jaworski authored
3
4
 *  Copyright (C) 2016-2017 Wojciech Jaworski <wjaworski atSPAMfree mimuw dot edu dot pl>
 *  Copyright (C) 2016-2017 Institute of Computer Science Polish Academy of Sciences
Wojciech Jaworski authored
5
 *
Wojciech Jaworski authored
6
7
 *  This library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
Wojciech Jaworski authored
8
9
10
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
Wojciech Jaworski authored
11
 *  This library is distributed in the hope that it will be useful,
Wojciech Jaworski authored
12
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Wojciech Jaworski authored
14
 *  GNU Lesser General Public License for more details.
Wojciech Jaworski authored
15
 *
Wojciech Jaworski authored
16
 *  You should have received a copy of the GNU Lesser General Public License
Wojciech Jaworski authored
17
18
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *)
Wojciech Jaworski authored
20
open ENIAM_LCGtypes
Wojciech Jaworski authored
21
22
open Xstd
open Printf
Wojciech Jaworski authored
23
Wojciech Jaworski authored
24
25
26
27
28
29
30
let direction = function
    Forward -> "/"
  | Backward  -> "\\"
  | Both -> "|"

let rec linear_term c = function
    Var v -> v
Wojciech Jaworski authored
31
  | Tuple l ->
Wojciech Jaworski authored
32
33
34
    let s = String.concat "⊗" (Xlist.map l (linear_term 2)) in
    if c > 1 then "(" ^ s ^ ")" else s
  (*   | LetIn(l,s,t) -> "let " ^ String.concat "⊗" l ^ " = " ^ (linear_term 0 s) ^ " in " ^ (linear_term 0 t) *)
Wojciech Jaworski authored
35
36
  | Variant(e,l) -> "〈" ^ String.concat "," (Xlist.map l (fun (i,t) -> e^i^": "^linear_term 0 t)) ^ "〉"
  | VariantVar(v,t) -> "〈" ^ linear_term 0 t ^ "〉_" ^ v
Wojciech Jaworski authored
37
  (*   | Proj(n,t) -> "π_" ^ (string_of_int n) ^ (linear_term c t) *)
Wojciech Jaworski authored
38
39
40
41
  | ProjVar(v,t) -> "π_[" ^ v ^ "]" ^ (linear_term c t)
  | SubstVar v -> v
  | Subst(s,v,t) -> "subst(" ^ (linear_term 0 s) ^ "," ^ v ^ "," ^ (linear_term 0 t) ^ ")"
  | Inj(n,t) -> "inj_" ^ (string_of_int n) ^ (linear_term c t)
Wojciech Jaworski authored
42
  | Case(t,l) -> "case " ^ (linear_term 0 t) ^ " of " ^
Wojciech Jaworski authored
43
                 (String.concat " | " (Xlist.map l (fun (v,t) -> v ^ " -> " ^ (linear_term 0 t))))
Wojciech Jaworski authored
44
45
46
47
48
49
50
51
52
53
54
  | Lambda(v,t) -> "λ" ^ v ^ "." ^ (linear_term c t)
  | LambdaSet(l,t) -> "λ" ^ (String.concat "," l) ^ "." ^ (linear_term c t)
  | LambdaRot(n,t) -> "rot_" ^ (string_of_int n) ^ (linear_term c t)
  | App(s,t) -> "(" ^ (linear_term 0 s) ^ ")(" ^ (linear_term 0 t) ^ ")"
  | Dot -> "∙"
  | Val s -> s
  | SetAttr(e,s,t) -> "setattr(" ^ e ^ "," ^ linear_term 0 s ^ "," ^ linear_term 0 t ^ ")"
  | Fix(s,t) -> "fix(" ^ linear_term 0 s ^ "," ^ linear_term 0 t ^ ")"
  | Empty t -> "empty(" ^ linear_term 0 t ^ ")"
  | Apply t -> "apply(" ^ linear_term 0 t ^ ")"
  | Insert(s,t) -> "insert(" ^ linear_term 0 s ^ "," ^ linear_term 0 t ^ ")"
Wojciech Jaworski authored
55
  | Node t ->
Wojciech Jaworski authored
56
    "[" ^
Wojciech Jaworski authored
57
    (String.concat "; " (Xlist.map (["ORTH",Val t.orth;"LEMMA",Val t.lemma;"POS",Val t.pos;"ID",Val (string_of_int t.id);
Wojciech Jaworski authored
58
                                     "WEIGHT",Val (string_of_float t.weight);"SYMBOL",t.symbol;
Wojciech Jaworski authored
59
                                     "ARG_SYMBOL",t.arg_symbol;"ARG_DIR",Val t.arg_dir;"ARGS",t.args] @ t.attrs) (fun (e,t) ->
Wojciech Jaworski authored
60
         e ^ ": " ^ (linear_term 0 t)))) ^ "]"
Wojciech Jaworski authored
61
62
63
64
65
66
  | Ref i -> "ref " ^ string_of_int i
  | Cut t -> "cut(" ^ linear_term 0 t ^ ")"

let rec internal_grammar_symbol c = function
    Atom x -> x
  | AVar x -> x
Wojciech Jaworski authored
67
  | With l ->
Wojciech Jaworski authored
68
69
    let s = String.concat "&" (Xlist.map l (internal_grammar_symbol 2)) in
    if c > 1 then "(" ^ s ^ ")" else s
Wojciech Jaworski authored
70
71
72
73
  | Zero -> "0"
  | Top -> "⊤"

let rec grammar_symbol c = function
Wojciech Jaworski authored
74
    Tensor l ->
Wojciech Jaworski authored
75
76
    let s = String.concat "⊗" (Xlist.map l (internal_grammar_symbol 2)) in
    if c > 1 then "(" ^ s ^ ")" else s
Wojciech Jaworski authored
77
  | Plus l ->
Wojciech Jaworski authored
78
79
    let s = String.concat "⊕" (Xlist.map l (grammar_symbol 2)) in
    if c > 1 then "(" ^ s ^ ")" else s
Wojciech Jaworski authored
80
81
  | Imp(s,d,t) -> (grammar_symbol 2 s) ^ direction d ^ (grammar_symbol 2 t)
  | One -> "1"
Wojciech Jaworski authored
82
  | ImpSet(s,l) ->
Wojciech Jaworski authored
83
84
    let s = (grammar_symbol 1 s) ^ "{" ^ String.concat "," (Xlist.map l (fun (d,a) -> direction d ^ grammar_symbol 1 a)) ^ "}" in
    if c > 0 then "(" ^ s ^ ")" else s
Wojciech Jaworski authored
85
86
  | WithVar(v,s,e,t) -> "&_" ^ e ^ ": " ^ v ^ ":=" ^ (internal_grammar_symbol 2 s) ^ " " ^ (grammar_symbol 2 t)
  | Star s -> (grammar_symbol 2 s) ^ "^*"
Wojciech Jaworski authored
87
  | Bracket(lf,rf,s) -> "⟨" ^ (if lf then "⟨" else "") ^ (grammar_symbol 0 s) ^ "⟩" ^ (if rf then "⟩" else "")
Wojciech Jaworski authored
88
89
90
  | BracketSet d -> "BracketSet(" ^ direction d ^ ")"
  | Maybe s -> "?" ^ grammar_symbol 2 s
Wojciech Jaworski authored
91
Wojciech Jaworski authored
92
93
94
95
96
97
98
99
100
101
102
103
let rec internal_grammar_symbol_prime = function
    Atom x -> "Atom(" ^ x ^ ")"
  | AVar x -> "AVar(" ^ x ^ ")"
  | With l -> "With[" ^ (String.concat ";" (Xlist.map l (internal_grammar_symbol_prime))) ^ "]"
  | Zero -> "Zero"
  | Top -> "Top"

let rec grammar_symbol_prime = function
    Tensor l -> "Tensor[" ^ (String.concat ";" (Xlist.map l (internal_grammar_symbol_prime))) ^ "]"
  | Plus l -> "Plus[" ^ (String.concat ";" (Xlist.map l (grammar_symbol_prime))) ^ "]"
  | Imp(s,d,t) -> "Imp(" ^ (grammar_symbol_prime s) ^ "," ^ direction d ^ "," ^ (grammar_symbol_prime t) ^ ")"
  | One -> "One"
Wojciech Jaworski authored
104
  | ImpSet(s,l) -> "ImpSet(" ^ (grammar_symbol_prime s) ^ ",[" ^ String.concat ";" (Xlist.map l (fun (d,a) -> direction d ^ grammar_symbol_prime a)) ^ "])"
Wojciech Jaworski authored
105
106
107
108
109
110
  | WithVar(v,s,e,t) -> "WithVar(" ^ v ^ "," ^ (internal_grammar_symbol_prime s) ^ "," ^ e ^ "," ^ (grammar_symbol_prime t) ^ ")"
  | Star s -> "Star(" ^ (grammar_symbol_prime s) ^ ")"
  | Bracket(lf,rf,s) -> "Bracket(" ^ string_of_bool lf ^ "," ^ string_of_bool rf ^ "," ^ (grammar_symbol_prime s) ^ ")"
  | BracketSet d -> "BracketSet(" ^ direction d ^ ")"
  | Maybe s -> "Maybe(" ^ grammar_symbol_prime s ^ ")"
Wojciech Jaworski authored
111
112
let symbol_sem_list l =
  String.concat "\n  " (Xlist.map l (fun (symbol,sem) ->
Wojciech Jaworski authored
113
      grammar_symbol 0 symbol ^ ": " ^ linear_term 0 sem))